Skip to content

Commit

Permalink
Make the entity in insertRow non-optional
Browse files Browse the repository at this point in the history
The entity is required to know the table we insert
into, can't be optional.
`AdaptorRow` does not know the entity/table.
Affects all adaptors.
  • Loading branch information
helje5 committed Nov 24, 2024
1 parent deb6c98 commit 5145a15
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 56 deletions.
22 changes: 7 additions & 15 deletions Sources/ZeeQL/Access/AdaptorChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public protocol AdaptorChannel : AdaptorQueryType, ModelNameMapper {
* entity are being refetched. Requires the entity!
* - returns: the record, potentially refetched and updated
*/
func insertRow(_ row: AdaptorRow, _ entity: Entity?, refetchAll: Bool) throws
func insertRow(_ row: AdaptorRow, _ entity: Entity, refetchAll: Bool) throws
-> AdaptorRow
}

Expand Down Expand Up @@ -505,13 +505,13 @@ public extension AdaptorChannel { // MARK: - Operations
* entity are being refetched. Requires the entity!
* - Returns: the record, potentially refetched and updated
*/
func insertRow(_ row: AdaptorRow, _ entity: Entity?, refetchAll: Bool)
@inlinable @discardableResult
func insertRow(_ row: AdaptorRow, _ entity: Entity, refetchAll: Bool)
throws -> AdaptorRow
{
return try defaultInsertRow(row, entity, refetchAll: refetchAll)
}

func defaultInsertRow(_ row: AdaptorRow, _ entity: Entity?, refetchAll: Bool)
/**
* This method inserts the given row into the table represented by the entity.
* To produce the `INSERT` statement using the ``expressionFactory`` of the
Expand All @@ -527,12 +527,10 @@ public extension AdaptorChannel { // MARK: - Operations
* - Returns: the record, potentially refetched and updated
*/
@inlinable
func defaultInsertRow(_ row: AdaptorRow, _ entity: Entity, refetchAll: Bool)
throws -> AdaptorRow
{
// So that we can reuse the default implementation ...
if refetchAll && entity == nil {
throw AdaptorError.InsertRefetchRequiresEntity
}

let expr = expressionFactory.insertStatementForRow(row, entity)

Expand All @@ -546,12 +544,6 @@ public extension AdaptorChannel { // MARK: - Operations
// => only if refetchAll is enabled, or if the value of the pkey is
// missing in the row.

guard let entity = entity else {
// Note: we don't know the pkey w/o entity and we don't want to reflect in
// here
return row
}

if let pkey = entity.primaryKeyForRow(row) {
// already had the pkey assigned

Expand All @@ -576,11 +568,11 @@ public extension AdaptorChannel { // MARK: - Operations
* - entity: optionally an entity representing the table
* - Returns: the record, potentially refetched and updated
*/
@inlinable
func insertRow(_ row: AdaptorRow, _ entity: Entity? = nil)
@inlinable @discardableResult
func insertRow(_ row: AdaptorRow, _ entity: Entity)
throws -> AdaptorRow
{
return try insertRow(row, entity, refetchAll: entity != nil)
return try insertRow(row, entity, refetchAll: true)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/ZeeQL/Access/SQLExpressionFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ open class SQLExpressionFactory {
return e
}

open func insertStatementForRow(_ row : AdaptorRow, _ entity: Entity?)
open func insertStatementForRow(_ row : AdaptorRow, _ entity: Entity)
-> SQLExpression
{
let e = createExpression(entity)
Expand Down
69 changes: 29 additions & 40 deletions Sources/ZeeQL/SQLite3Adaptor/SQLite3AdaptorChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,9 @@ open class SQLite3AdaptorChannel : AdaptorChannel {
return Int(sqlite3_changes(handle))
}

open func insertRow(_ row: AdaptorRow, _ entity: Entity?, refetchAll: Bool)
open func insertRow(_ row: AdaptorRow, _ entity: Entity, refetchAll: Bool)
throws -> AdaptorRow
{
if refetchAll && entity == nil {
throw AdaptorError.InsertRefetchRequiresEntity
}

let expr = expressionFactory.insertStatementForRow(row, entity)

// In SQLite we need a transaction for the refetch
Expand All @@ -274,48 +270,41 @@ open class SQLite3AdaptorChannel : AdaptorChannel {
throw AdaptorError.OperationDidNotAffectOne
}

if let entity = entity {
let pkey : AdaptorRow
if let epkey = entity.primaryKeyForRow(row), !epkey.isEmpty {
// already had the primary key assigned
pkey = epkey
}
else if let pkeys = entity.primaryKeyAttributeNames, pkeys.count == 1 {
let lastRowId = sqlite3_last_insert_rowid(handle)
pkey = [ pkeys[0] : lastRowId ]
}
else {
throw AdaptorError.FailedToGrabNewPrimaryKey(entity: entity, row: row)
}

if refetchAll {
let q = qualifierToMatchAllValues(pkey)
let fs = ModelFetchSpecification(entity: entity, qualifier: q,
sortOrderings: nil, limit: 2)
var rec : AdaptorRecord? = nil
try selectAttributes(entity.attributes, fs, lock: false, entity) {
record in
guard rec == nil else { // multiple matched!
throw AdaptorError.FailedToRefetchInsertedRow(
entity: entity, row: row)
}
rec = record
}
guard let rrec = rec else { // none matched!
let pkey : AdaptorRow
if let epkey = entity.primaryKeyForRow(row), !epkey.isEmpty {
// already had the primary key assigned
pkey = epkey
}
else if let pkeys = entity.primaryKeyAttributeNames, pkeys.count == 1 {
let lastRowId = sqlite3_last_insert_rowid(handle)
pkey = [ pkeys[0] : lastRowId ]
}
else {
throw AdaptorError.FailedToGrabNewPrimaryKey(entity: entity, row: row)
}

if refetchAll {
let q = qualifierToMatchAllValues(pkey)
let fs = ModelFetchSpecification(entity: entity, qualifier: q,
sortOrderings: nil, limit: 2)
var rec : AdaptorRecord? = nil
try selectAttributes(entity.attributes, fs, lock: false, entity) {
record in
guard rec == nil else { // multiple matched!
throw AdaptorError.FailedToRefetchInsertedRow(
entity: entity, row: row)
}

result = rrec.asAdaptorRow
rec = record
}
else {
result = pkey
guard let rrec = rec else { // none matched!
throw AdaptorError.FailedToRefetchInsertedRow(
entity: entity, row: row)
}

result = rrec.asAdaptorRow
}
else {
// Note: we don't know the pkey w/o entity and we don't want to reflect in
// here
result = row
result = pkey
}
}
catch {
Expand Down

0 comments on commit 5145a15

Please sign in to comment.