Skip to content

Commit

Permalink
Fix bug in upsert where the object is not updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuliu committed Mar 2, 2021
1 parent 87cdc79 commit 033150d
Show file tree
Hide file tree
Showing 15 changed files with 391 additions and 135 deletions.
10 changes: 5 additions & 5 deletions app/Benchmarks/doc_generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public struct zzz_DflatGen_TextContent: FlatBufferObject {
}
public static func createTextContent(
_ fbb: inout FlatBufferBuilder,
offsetOfText text: Offset<String> = Offset()
textOffset text: Offset<String> = Offset()
) -> Offset<UOffset> {
let __start = zzz_DflatGen_TextContent.startTextContent(&fbb)
zzz_DflatGen_TextContent.add(text: text, &fbb)
Expand Down Expand Up @@ -159,7 +159,7 @@ public struct zzz_DflatGen_ImageContent: FlatBufferObject {
}
public static func createImageContent(
_ fbb: inout FlatBufferBuilder,
vectorOfImages images: Offset<UOffset> = Offset()
imagesVectorOffset images: Offset<UOffset> = Offset()
) -> Offset<UOffset> {
let __start = zzz_DflatGen_ImageContent.startImageContent(&fbb)
zzz_DflatGen_ImageContent.addVectorOf(images: images, &fbb)
Expand Down Expand Up @@ -266,10 +266,10 @@ public struct zzz_DflatGen_BenchDoc: FlatBufferObject {
_ fbb: inout FlatBufferBuilder,
pos: zzz_DflatGen_Vec3? = nil,
color: zzz_DflatGen_Color = .red,
offsetOfTitle title: Offset<String> = Offset(),
titleOffset title: Offset<String> = Offset(),
contentType: zzz_DflatGen_Content = .none_,
offsetOfContent content: Offset<UOffset> = Offset(),
offsetOfTag tag: Offset<String> = Offset(),
contentOffset content: Offset<UOffset> = Offset(),
tagOffset tag: Offset<String> = Offset(),
priority: Int32 = 0
) -> Offset<UOffset> {
let __start = zzz_DflatGen_BenchDoc.startBenchDoc(&fbb)
Expand Down
20 changes: 14 additions & 6 deletions app/Benchmarks/doc_mutating_generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ public final class BenchDocChangeRequest: Dflat.ChangeRequest {
transactionContext.connection, ofType: BenchDoc.self, for: key)
return u.map { BenchDocChangeRequest(type: .update, $0) }
}
public static func upsertRequest(_ o: BenchDoc) -> BenchDocChangeRequest {
let transactionContext = SQLiteTransactionContext.current!
let key: SQLiteObjectKey = o._rowid >= 0 ? .rowid(o._rowid) : .primaryKey([o.title])
guard
let u = transactionContext.objectRepository.object(
transactionContext.connection, ofType: BenchDoc.self, for: key)
else {
return Self.creationRequest(o)
}
let changeRequest = BenchDocChangeRequest(type: .update, o)
changeRequest._o = u
changeRequest._rowid = u._rowid
return changeRequest
}
public static func creationRequest(_ o: BenchDoc) -> BenchDocChangeRequest {
let creationRequest = BenchDocChangeRequest(type: .creation, o)
creationRequest._rowid = -1
Expand All @@ -161,12 +175,6 @@ public final class BenchDocChangeRequest: Dflat.ChangeRequest {
public static func creationRequest() -> BenchDocChangeRequest {
return BenchDocChangeRequest(type: .creation)
}
public static func upsertRequest(_ o: BenchDoc) -> BenchDocChangeRequest {
guard let changeRequest = Self.changeRequest(o) else {
return Self.creationRequest(o)
}
return changeRequest
}
public static func deletionRequest(_ o: BenchDoc) -> BenchDocChangeRequest? {
let transactionContext = SQLiteTransactionContext.current!
let key: SQLiteObjectKey = o._rowid >= 0 ? .rowid(o._rowid) : .primaryKey([o.title])
Expand Down
6 changes: 3 additions & 3 deletions app/Benchmarks/doc_v2_generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ public struct zzz_DflatGen_BenchDocV2: FlatBufferObject {
public static func createBenchDocV2(
_ fbb: inout FlatBufferBuilder,
color: zzz_DflatGen_ColorV2 = .red,
offsetOfTitle title: Offset<String> = Offset(),
offsetOfTag tag: Offset<String> = Offset(),
titleOffset title: Offset<String> = Offset(),
tagOffset tag: Offset<String> = Offset(),
priority: Int32 = 0,
offsetOfText text: Offset<String> = Offset()
textOffset text: Offset<String> = Offset()
) -> Offset<UOffset> {
let __start = zzz_DflatGen_BenchDocV2.startBenchDocV2(&fbb)
zzz_DflatGen_BenchDocV2.add(color: color, &fbb)
Expand Down
20 changes: 14 additions & 6 deletions app/Benchmarks/doc_v2_mutating_generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ public final class BenchDocV2ChangeRequest: Dflat.ChangeRequest {
transactionContext.connection, ofType: BenchDocV2.self, for: key)
return u.map { BenchDocV2ChangeRequest(type: .update, $0) }
}
public static func upsertRequest(_ o: BenchDocV2) -> BenchDocV2ChangeRequest {
let transactionContext = SQLiteTransactionContext.current!
let key: SQLiteObjectKey = o._rowid >= 0 ? .rowid(o._rowid) : .primaryKey([o.title])
guard
let u = transactionContext.objectRepository.object(
transactionContext.connection, ofType: BenchDocV2.self, for: key)
else {
return Self.creationRequest(o)
}
let changeRequest = BenchDocV2ChangeRequest(type: .update, o)
changeRequest._o = u
changeRequest._rowid = u._rowid
return changeRequest
}
public static func creationRequest(_ o: BenchDocV2) -> BenchDocV2ChangeRequest {
let creationRequest = BenchDocV2ChangeRequest(type: .creation, o)
creationRequest._rowid = -1
Expand All @@ -82,12 +96,6 @@ public final class BenchDocV2ChangeRequest: Dflat.ChangeRequest {
public static func creationRequest() -> BenchDocV2ChangeRequest {
return BenchDocV2ChangeRequest(type: .creation)
}
public static func upsertRequest(_ o: BenchDocV2) -> BenchDocV2ChangeRequest {
guard let changeRequest = Self.changeRequest(o) else {
return Self.creationRequest(o)
}
return changeRequest
}
public static func deletionRequest(_ o: BenchDocV2) -> BenchDocV2ChangeRequest? {
let transactionContext = SQLiteTransactionContext.current!
let key: SQLiteObjectKey = o._rowid >= 0 ? .rowid(o._rowid) : .primaryKey([o.title])
Expand Down
6 changes: 3 additions & 3 deletions app/Benchmarks/doc_v3_generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ public struct zzz_DflatGen_BenchDocV3: FlatBufferObject {
}
public static func createBenchDocV3(
_ fbb: inout FlatBufferBuilder,
offsetOfTitle title: Offset<String> = Offset(),
offsetOfTag tag: Offset<String> = Offset(),
titleOffset title: Offset<String> = Offset(),
tagOffset tag: Offset<String> = Offset(),
priority: Int32 = 0,
offsetOfText text: Offset<String> = Offset()
textOffset text: Offset<String> = Offset()
) -> Offset<UOffset> {
let __start = zzz_DflatGen_BenchDocV3.startBenchDocV3(&fbb)
zzz_DflatGen_BenchDocV3.add(title: title, &fbb)
Expand Down
20 changes: 14 additions & 6 deletions app/Benchmarks/doc_v3_mutating_generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ public final class BenchDocV3ChangeRequest: Dflat.ChangeRequest {
transactionContext.connection, ofType: BenchDocV3.self, for: key)
return u.map { BenchDocV3ChangeRequest(type: .update, $0) }
}
public static func upsertRequest(_ o: BenchDocV3) -> BenchDocV3ChangeRequest {
let transactionContext = SQLiteTransactionContext.current!
let key: SQLiteObjectKey = o._rowid >= 0 ? .rowid(o._rowid) : .primaryKey([o.title])
guard
let u = transactionContext.objectRepository.object(
transactionContext.connection, ofType: BenchDocV3.self, for: key)
else {
return Self.creationRequest(o)
}
let changeRequest = BenchDocV3ChangeRequest(type: .update, o)
changeRequest._o = u
changeRequest._rowid = u._rowid
return changeRequest
}
public static func creationRequest(_ o: BenchDocV3) -> BenchDocV3ChangeRequest {
let creationRequest = BenchDocV3ChangeRequest(type: .creation, o)
creationRequest._rowid = -1
Expand All @@ -71,12 +85,6 @@ public final class BenchDocV3ChangeRequest: Dflat.ChangeRequest {
public static func creationRequest() -> BenchDocV3ChangeRequest {
return BenchDocV3ChangeRequest(type: .creation)
}
public static func upsertRequest(_ o: BenchDocV3) -> BenchDocV3ChangeRequest {
guard let changeRequest = Self.changeRequest(o) else {
return Self.creationRequest(o)
}
return changeRequest
}
public static func deletionRequest(_ o: BenchDocV3) -> BenchDocV3ChangeRequest? {
let transactionContext = SQLiteTransactionContext.current!
let key: SQLiteObjectKey = o._rowid >= 0 ? .rowid(o._rowid) : .primaryKey([o.title])
Expand Down
6 changes: 3 additions & 3 deletions app/Benchmarks/doc_v4_generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ public struct zzz_DflatGen_BenchDocV4: FlatBufferObject {
}
public static func createBenchDocV4(
_ fbb: inout FlatBufferBuilder,
offsetOfTitle title: Offset<String> = Offset(),
offsetOfTag tag: Offset<String> = Offset(),
titleOffset title: Offset<String> = Offset(),
tagOffset tag: Offset<String> = Offset(),
priority: Int32 = 0,
offsetOfText text: Offset<String> = Offset()
textOffset text: Offset<String> = Offset()
) -> Offset<UOffset> {
let __start = zzz_DflatGen_BenchDocV4.startBenchDocV4(&fbb)
zzz_DflatGen_BenchDocV4.add(title: title, &fbb)
Expand Down
20 changes: 14 additions & 6 deletions app/Benchmarks/doc_v4_mutating_generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ public final class BenchDocV4ChangeRequest: Dflat.ChangeRequest {
transactionContext.connection, ofType: BenchDocV4.self, for: key)
return u.map { BenchDocV4ChangeRequest(type: .update, $0) }
}
public static func upsertRequest(_ o: BenchDocV4) -> BenchDocV4ChangeRequest {
let transactionContext = SQLiteTransactionContext.current!
let key: SQLiteObjectKey = o._rowid >= 0 ? .rowid(o._rowid) : .primaryKey([o.title])
guard
let u = transactionContext.objectRepository.object(
transactionContext.connection, ofType: BenchDocV4.self, for: key)
else {
return Self.creationRequest(o)
}
let changeRequest = BenchDocV4ChangeRequest(type: .update, o)
changeRequest._o = u
changeRequest._rowid = u._rowid
return changeRequest
}
public static func creationRequest(_ o: BenchDocV4) -> BenchDocV4ChangeRequest {
let creationRequest = BenchDocV4ChangeRequest(type: .creation, o)
creationRequest._rowid = -1
Expand All @@ -71,12 +85,6 @@ public final class BenchDocV4ChangeRequest: Dflat.ChangeRequest {
public static func creationRequest() -> BenchDocV4ChangeRequest {
return BenchDocV4ChangeRequest(type: .creation)
}
public static func upsertRequest(_ o: BenchDocV4) -> BenchDocV4ChangeRequest {
guard let changeRequest = Self.changeRequest(o) else {
return Self.creationRequest(o)
}
return changeRequest
}
public static func deletionRequest(_ o: BenchDocV4) -> BenchDocV4ChangeRequest? {
let transactionContext = SQLiteTransactionContext.current!
let key: SQLiteObjectKey = o._rowid >= 0 ? .rowid(o._rowid) : .primaryKey([o.title])
Expand Down
21 changes: 14 additions & 7 deletions src/parser/dflatc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,20 @@ func GenChangeRequest(_ structDef: Struct, code: inout String) {
" let u = transactionContext.objectRepository.object(transactionContext.connection, ofType: \(structDef.name).self, for: key)\n"
code += " return u.map { \(structDef.name)ChangeRequest(type: .update, $0) }\n"
code += " }\n"
code +=
" public static func upsertRequest(_ o: \(structDef.name)) -> \(structDef.name)ChangeRequest {\n"
code += " let transactionContext = SQLiteTransactionContext.current!\n"
code +=
" let key: SQLiteObjectKey = o._rowid >= 0 ? .rowid(o._rowid) : .primaryKey([\(primaryKeys.map { "o." + $0.name }.joined(separator: ", "))])\n"
code +=
" guard let u = transactionContext.objectRepository.object(transactionContext.connection, ofType: \(structDef.name).self, for: key) else {\n"
code += " return Self.creationRequest(o)\n"
code += " }\n"
code += " let changeRequest = \(structDef.name)ChangeRequest(type: .update, o)\n"
code += " changeRequest._o = u\n"
code += " changeRequest._rowid = u._rowid\n"
code += " return changeRequest\n"
code += " }\n"
code +=
" public static func creationRequest(_ o: \(structDef.name)) -> \(structDef.name)ChangeRequest {\n"
code += " let creationRequest = \(structDef.name)ChangeRequest(type: .creation, o)\n"
Expand All @@ -970,13 +984,6 @@ func GenChangeRequest(_ structDef: Struct, code: inout String) {
code += " public static func creationRequest() -> \(structDef.name)ChangeRequest {\n"
code += " return \(structDef.name)ChangeRequest(type: .creation)\n"
code += " }\n"
code +=
" public static func upsertRequest(_ o: \(structDef.name)) -> \(structDef.name)ChangeRequest {\n"
code += " guard let changeRequest = Self.changeRequest(o) else {\n"
code += " return Self.creationRequest(o)\n"
code += " }\n"
code += " return changeRequest\n"
code += " }\n"
code +=
" public static func deletionRequest(_ o: \(structDef.name)) -> \(structDef.name)ChangeRequest? {\n"
code += " let transactionContext = SQLiteTransactionContext.current!\n"
Expand Down
38 changes: 38 additions & 0 deletions src/tests/SQLiteWorkspaceCRUDTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,44 @@ class SQLiteWorkspaceCRUDTests: XCTestCase {
XCTAssertEqual(firstMonster.name, "What's my name")
}

func testObjectCreationAndUpsert() {
guard let dflat = dflat else { return }
let expectation = XCTestExpectation(description: "transcation done")
dflat.performChanges(
[MyGame.Sample.Monster.self],
changesHandler: { (txnContext) in
let creationRequest = MyGame.Sample.MonsterChangeRequest.creationRequest()
creationRequest.name = "What's my name"
try! txnContext.submit(creationRequest)
}
) { success in
expectation.fulfill()
}
wait(for: [expectation], timeout: 10.0)
let fetchedResult = dflat.fetch(for: MyGame.Sample.Monster.self).where(
MyGame.Sample.Monster.name == "What's my name")
let firstMonster = fetchedResult[0]
XCTAssertEqual(firstMonster.name, "What's my name")
XCTAssertEqual(firstMonster.hp, 100)
let upsertExpectation = XCTestExpectation(description: "transcation done")
dflat.performChanges(
[MyGame.Sample.Monster.self],
changesHandler: { (txnContext) in
let monster = MyGame.Sample.Monster(name: "What's my name", color: .blue, hp: 10)
let upsertRequest = MyGame.Sample.MonsterChangeRequest.upsertRequest(monster)
try! txnContext.submit(upsertRequest)
}
) { success in
upsertExpectation.fulfill()
}
wait(for: [upsertExpectation], timeout: 10.0)
let secondFetchedResult = dflat.fetch(for: MyGame.Sample.Monster.self).where(
MyGame.Sample.Monster.name == "What's my name")
let secondMonster = secondFetchedResult[0]
XCTAssertEqual(secondMonster.name, "What's my name")
XCTAssertEqual(secondMonster.hp, 10)
}

func testObjectCreationAndQueryByNoneIndexedProperty() {
guard let dflat = dflat else { return }
let expectation = XCTestExpectation(description: "transcation done")
Expand Down
Loading

0 comments on commit 033150d

Please sign in to comment.