Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Sources/FeatherDatabase/Database/DatabaseFilterInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,63 @@ extension SQLDeleteBuilder {
)
}
}

extension SQLDeleteBuilder {

func applyFilter(
_ filter: (any DatabaseTableFilterInterface)?
) -> Self {
guard let filter else {
return self
}
var res = self
for group in filter.groups {
switch filter.relation {
case .and:
res = res.where { p in
var p = p
for filter in group.columns {
switch group.relation {
case .and:
p = p.where(
filter.column.sqlValue,
filter.operator,
filter.value
)
case .or:
p = p.orWhere(
filter.column.sqlValue,
filter.operator,
filter.value
)
}
}
return p
}
case .or:
res = res.orWhere { p in
var p = p
for filter in group.columns {
switch group.relation {
case .and:
p = p.where(
filter.column.sqlValue,
filter.operator,
filter.value
)
case .or:
p = p.orWhere(
filter.column.sqlValue,
filter.operator,
filter.value
)
}
}
return p
}
}

}
return res
}
}
19 changes: 19 additions & 0 deletions Sources/FeatherDatabase/Database/Query/DatabaseQueryCount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,32 @@ public protocol DatabaseQueryCount: DatabaseQueryInterface {
filter: DatabaseFilter<Row.ColumnNames>?,
on db: Database
) async throws -> UInt

static func count(
filter: DatabaseTableFilter<Row.ColumnNames>,
on db: Database
) async throws -> UInt
}

extension DatabaseQueryCount {

public static func count(
filter: DatabaseFilter<Row.ColumnNames>? = nil,
on db: Database
) async throws -> UInt {
if let filter {
return try await count(
filter: .init(groups: [.init(columns: [filter])]),
on: db
)
}

return try await count(filter: .init(groups: []), on: db)
}

public static func count(
filter: DatabaseTableFilter<Row.ColumnNames>,
on db: Database
) async throws -> UInt {
try await db.run { sql in
let value =
Expand Down
15 changes: 15 additions & 0 deletions Sources/FeatherDatabase/Database/Query/DatabaseQueryDelete.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,28 @@ public protocol DatabaseQueryDelete: DatabaseQueryInterface {
filter: DatabaseFilter<Row.ColumnNames>,
on db: Database
) async throws

static func delete(
filter: DatabaseTableFilter<Row.ColumnNames>,
on db: Database
) async throws
}

extension DatabaseQueryDelete {

public static func delete(
filter: DatabaseFilter<Row.ColumnNames>,
on db: Database
) async throws {
try await delete(
filter: .init(groups: [.init(columns: [filter])]),
on: db
)
}

public static func delete(
filter: DatabaseTableFilter<Row.ColumnNames>,
on db: Database
) async throws {
try await db.run { sql in
try await sql
Expand Down
18 changes: 18 additions & 0 deletions Sources/FeatherDatabase/Database/Query/DatabaseQueryGet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ public protocol DatabaseQueryGet: DatabaseQueryInterface {
order: DatabaseOrder<Row.ColumnNames>?,
on db: Database
) async throws -> Row?

static func getFirst(
filter: DatabaseTableFilter<Row.ColumnNames>,
order: DatabaseOrder<Row.ColumnNames>?,
on db: Database
) async throws -> Row?
}

extension DatabaseQueryGet {
Expand All @@ -22,6 +28,18 @@ extension DatabaseQueryGet {
filter: DatabaseFilter<Row.ColumnNames>,
order: DatabaseOrder<Row.ColumnNames>? = nil,
on db: Database
) async throws -> Row? {
try await getFirst(
filter: .init(groups: [.init(columns: [filter])]),
order: order,
on: db
)
}

public static func getFirst(
filter: DatabaseTableFilter<Row.ColumnNames>,
order: DatabaseOrder<Row.ColumnNames>? = nil,
on db: Database
) async throws -> Row? {
try await db.run { sql in
try await sql
Expand Down
21 changes: 21 additions & 0 deletions Sources/FeatherDatabaseTesting/DatabaseTestSuite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public struct DatabaseTestSuite {
let tests: [(Database) async throws -> Void] = [
testInsert,
testCount,
testCountFilterGroup,
testGet,
testFirst,
testUpdateOne,
Expand Down Expand Up @@ -118,6 +119,26 @@ extension DatabaseTestSuite {
XCTAssertEqual(count3, 11)
}

public func testCountFilterGroup(_ db: Database) async throws {
let models: [Blog.Tag.Model] = (1...50)
.map {
.mock($0)
}
try await Blog.Tag.Query.insert(models, on: db)

let count = try await Blog.Tag.Query.count(
filter: .init(groups: [
.init(columns: [
.init(column: .name, operator: .like, value: ["name-1%"]),
.init(column: .notes, operator: .like, value: ["notes-1%"]),
])
]),
on: db
)

XCTAssertEqual(count, 11)
}

public func testGet(_ db: Database) async throws {

let test = Blog.Tag.Model(
Expand Down