Skip to content

Commit

Permalink
create table if need before load and update database
Browse files Browse the repository at this point in the history
  • Loading branch information
sudopark committed Jan 1, 2024
1 parent e2997a0 commit d99a413
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Sources/SQLiteService/SQLiteDataBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,31 +239,39 @@ extension SQLiteDataBase {

public func loadValue<T: Table, S: ScalarType>(_ query: SelectQuery<T>) throws -> S? {

try self.createTableOrNot(T.self)
return try iterateDeserialize(query: query, deserialize: { $0.next() }).first
}

public func load<T: Table, R: RowValueType>(_ query: SelectQuery<T>) throws -> [R] {

try self.createTableOrNot(T.self)
return try iterateDeserialize(query: query, deserialize: R.init)
}

public func load<T: Table>(_ table: T.Type, query: SelectQuery<T>) throws -> [T.EntityType] {

try self.createTableOrNot(T.self)
return try iterateDeserialize(query: query, deserialize: T.EntityType.init)
}

public func load<T: Table, V>(_ query: SelectQuery<T>,
mapping: (CursorIterator) throws -> V) throws -> [V] {

try self.createTableOrNot(T.self)
return try iterateDeserialize(query: query, deserialize: mapping)
}

public func load<T: Table, R: RowValueType>(_ query: JoinQuery<T>) throws -> [R] {

try self.createTableOrNot(T.self)
return try iterateDeserialize(query: query, deserialize: R.init)
}

public func load<T: Table, V>(_ query: JoinQuery<T>,
mapping: (CursorIterator) throws -> V) throws -> [V] {

try self.createTableOrNot(T.self)
return try iterateDeserialize(query: query, deserialize: mapping)
}

Expand Down Expand Up @@ -302,6 +310,8 @@ extension SQLiteDataBase {

public func update<T>(_ table: T.Type, query: UpdateQuery<T>) throws where T : Table {

try self.createTableOrNot(table)

let stmt = try prepare(statement: query.asStatement())

defer {
Expand Down
1 change: 0 additions & 1 deletion Tests/SQLiteServiceTests/SQLiteDatabaseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ extension SQLiteDatabaseTests {
func testDatabase_insertModels() {
// given
self.openDataBase()
try? self.database.createTableOrNot(self.table)
var inserted: Bool = false

// when
Expand Down

0 comments on commit d99a413

Please sign in to comment.