Skip to content

Commit

Permalink
Swift cannot differentiate StaticString and String. Fixed that.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuliu committed Jun 28, 2020
1 parent 7df4078 commit dc62593
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 63 deletions.
6 changes: 3 additions & 3 deletions app/Benchmarks/doc_mutating_generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public final class BenchDocChangeRequest: Dflat.ChangeRequest {
guard let toolbox = toolbox as? SQLitePersistenceToolbox else { return nil }
switch _type {
case .creation:
guard let insert = toolbox.connection.prepareStatement("INSERT INTO benchdoc (__pk0, p) VALUES (?1, ?2)") else { return nil }
guard let insert = toolbox.connection.prepareStaticStatement("INSERT INTO benchdoc (__pk0, p) VALUES (?1, ?2)") else { return nil }
title.bindSQLite(insert, parameterId: 1)
let atom = self._atom
toolbox.flatBufferBuilder.clear()
Expand All @@ -205,7 +205,7 @@ public final class BenchDocChangeRequest: Dflat.ChangeRequest {
_type = .none
return .updated(atom)
}
guard let update = toolbox.connection.prepareStatement("REPLACE INTO benchdoc (__pk0, p, rowid) VALUES (?1, ?2, ?3)") else { return nil }
guard let update = toolbox.connection.prepareStaticStatement("REPLACE INTO benchdoc (__pk0, p, rowid) VALUES (?1, ?2, ?3)") else { return nil }
title.bindSQLite(update, parameterId: 1)
toolbox.flatBufferBuilder.clear()
let offset = atom.to(flatBufferBuilder: &toolbox.flatBufferBuilder)
Expand All @@ -219,7 +219,7 @@ public final class BenchDocChangeRequest: Dflat.ChangeRequest {
_type = .none
return .updated(atom)
case .deletion:
guard let deletion = toolbox.connection.prepareStatement("DELETE FROM benchdoc WHERE rowid=?1") else { return nil }
guard let deletion = toolbox.connection.prepareStaticStatement("DELETE FROM benchdoc WHERE rowid=?1") else { return nil }
_rowid.bindSQLite(deletion, parameterId: 1)
guard SQLITE_DONE == sqlite3_step(deletion) else { return nil }
_type = .none
Expand Down
14 changes: 7 additions & 7 deletions src/parser/dflatc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ func GenChangeRequest(_ structDef: Struct, code: inout String) {
code += " switch field {\n"
for indexedField in indexedFields {
code += " case \"\(indexedField.keyName)\":\n"
code += " guard let insert = sqlite.prepareStatement(\"INSERT INTO \(tableName)__\(indexedField.keyName) (rowid, \(indexedField.keyName)) VALUES (?1, ?2)\") else { return false }\n"
code += " guard let insert = sqlite.prepareStaticStatement(\"INSERT INTO \(tableName)__\(indexedField.keyName) (rowid, \(indexedField.keyName)) VALUES (?1, ?2)\") else { return false }\n"
code += " rowid.bindSQLite(insert, parameterId: 1)\n"
code += " let retval = \(GetIndexedFieldExpr(structDef, indexedField: indexedField)).evaluate(object: .table(table))\n"
code += " if retval.unknown {\n"
Expand Down Expand Up @@ -895,7 +895,7 @@ func GenChangeRequest(_ structDef: Struct, code: inout String) {
if indexedFields.count > 0 {
code += " let indexSurvey = toolbox.connection.indexSurvey(\(structDef.name).indexFields, table: \(structDef.name).table)\n"
}
code += " guard let insert = toolbox.connection.prepareStatement(\"INSERT INTO \(tableName) (\(primaryKeys.enumerated().map { "__pk\($0.offset)" }.joined(separator: ", ")), p) VALUES (?1, \(primaryKeys.enumerated().map { "?\($0.offset + 2)" }.joined(separator: ", ")))\") else { return nil }\n"
code += " guard let insert = toolbox.connection.prepareStaticStatement(\"INSERT INTO \(tableName) (\(primaryKeys.enumerated().map { "__pk\($0.offset)" }.joined(separator: ", ")), p) VALUES (?1, \(primaryKeys.enumerated().map { "?\($0.offset + 2)" }.joined(separator: ", ")))\") else { return nil }\n"
for (i, field) in primaryKeys.enumerated() {
code += " \(field.name).bindSQLite(insert, parameterId: \(i + 1))\n"
}
Expand All @@ -911,7 +911,7 @@ func GenChangeRequest(_ structDef: Struct, code: inout String) {
code += " _rowid = sqlite3_last_insert_rowid(toolbox.connection.sqlite)\n"
for (i, indexedField) in indexedFields.enumerated() {
code += " if indexSurvey.full.contains(\"\(indexedField.keyName)\") {\n"
code += " guard let i\(i) = toolbox.connection.prepareStatement(\"INSERT INTO \(tableName)__\(indexedField.keyName) (rowid, \(indexedField.keyName)) VALUES (?1, ?2)\") else { return nil }\n"
code += " guard let i\(i) = toolbox.connection.prepareStaticStatement(\"INSERT INTO \(tableName)__\(indexedField.keyName) (rowid, \(indexedField.keyName)) VALUES (?1, ?2)\") else { return nil }\n"
code += " _rowid.bindSQLite(i\(i), parameterId: 1)\n"
code += " let r\(i) = \(GetIndexedFieldExpr(structDef, indexedField: indexedField)).evaluate(object: .object(atom))\n"
code += " if r\(i).unknown {\n"
Expand All @@ -935,7 +935,7 @@ func GenChangeRequest(_ structDef: Struct, code: inout String) {
if indexedFields.count > 0 {
code += " let indexSurvey = toolbox.connection.indexSurvey(\(structDef.name).indexFields, table: \(structDef.name).table)\n"
}
code += " guard let update = toolbox.connection.prepareStatement(\"REPLACE INTO \(tableName) (\(primaryKeys.enumerated().map { "__pk\($0.offset)" }.joined(separator: ", ")), p, rowid) VALUES (?1, ?2, \(primaryKeys.enumerated().map { "?\($0.offset + 3)" }.joined(separator: ", ")))\") else { return nil }\n"
code += " guard let update = toolbox.connection.prepareStaticStatement(\"REPLACE INTO \(tableName) (\(primaryKeys.enumerated().map { "__pk\($0.offset)" }.joined(separator: ", ")), p, rowid) VALUES (?1, ?2, \(primaryKeys.enumerated().map { "?\($0.offset + 3)" }.joined(separator: ", ")))\") else { return nil }\n"
for (i, field) in primaryKeys.enumerated() {
code += " \(field.name).bindSQLite(update, parameterId: \(i + 1))\n"
}
Expand All @@ -953,7 +953,7 @@ func GenChangeRequest(_ structDef: Struct, code: inout String) {
code += " let or\(i) = \(GetIndexedFieldExpr(structDef, indexedField: indexedField)).evaluate(object: .object(o))\n"
code += " let r\(i) = \(GetIndexedFieldExpr(structDef, indexedField: indexedField)).evaluate(object: .object(atom))\n"
code += " if or\(i).unknown != r\(i).unknown || or\(i).result != r\(i).result {\n"
code += " guard let u\(i) = toolbox.connection.prepareStatement(\"REPLACE INTO \(tableName)__\(indexedField.keyName) (rowid, \(indexedField.keyName)) VALUES (?1, ?2)\") else { return nil }\n"
code += " guard let u\(i) = toolbox.connection.prepareStaticStatement(\"REPLACE INTO \(tableName)__\(indexedField.keyName) (rowid, \(indexedField.keyName)) VALUES (?1, ?2)\") else { return nil }\n"
code += " _rowid.bindSQLite(u\(i), parameterId: 1)\n"
code += " let r\(i) = \(GetIndexedFieldExpr(structDef, indexedField: indexedField)).evaluate(object: .object(atom))\n"
code += " if r\(i).unknown {\n"
Expand All @@ -968,11 +968,11 @@ func GenChangeRequest(_ structDef: Struct, code: inout String) {
code += " _type = .none\n"
code += " return .updated(atom)\n"
code += " case .deletion:\n"
code += " guard let deletion = toolbox.connection.prepareStatement(\"DELETE FROM \(tableName) WHERE rowid=?1\") else { return nil }\n"
code += " guard let deletion = toolbox.connection.prepareStaticStatement(\"DELETE FROM \(tableName) WHERE rowid=?1\") else { return nil }\n"
code += " _rowid.bindSQLite(deletion, parameterId: 1)\n"
code += " guard SQLITE_DONE == sqlite3_step(deletion) else { return nil }\n"
for (i, indexedField) in indexedFields.enumerated() {
code += " if let d\(i) = toolbox.connection.prepareStatement(\"DELETE FROM \(tableName)__\(indexedField.keyName) WHERE rowid=?1\") {\n"
code += " if let d\(i) = toolbox.connection.prepareStaticStatement(\"DELETE FROM \(tableName)__\(indexedField.keyName) WHERE rowid=?1\") {\n"
code += " _rowid.bindSQLite(d\(i), parameterId: 1)\n"
code += " sqlite3_step(d\(i))\n"
code += " }\n"
Expand Down
4 changes: 2 additions & 2 deletions src/sqlite/SQLiteConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public final class SQLiteConnection {
}
return prepared
}
public func prepareStatement(_ statement: StaticString) -> OpaquePointer? {
public func prepareStaticStatement(_ statement: StaticString) -> OpaquePointer? {
guard let sqlite = sqlite else { return nil }
let identifier = statement.utf8Start
if let prepared = staticPool[identifier] {
Expand Down Expand Up @@ -110,7 +110,7 @@ extension SQLiteConnection {
}
// Otherwise, we need to check the database. First setup a SAVEPOINT so we can view the rowid in a consistent view.
if !savepoint {
let sp = self.prepareStatement("SAVEPOINT dflat_idx")
let sp = self.prepareStaticStatement("SAVEPOINT dflat_idx")
guard SQLITE_DONE == sqlite3_step(sp) else { return IndexSurvey() }
savepoint = true
}
Expand Down
8 changes: 4 additions & 4 deletions src/sqlite/SQLiteTransactionContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public final class SQLiteTransactionContext: TransactionContext {
}

static func transactionalUpdate(toolbox: SQLitePersistenceToolbox, updater: (_: SQLitePersistenceToolbox) -> UpdatedObject?) throws -> UpdatedObject {
let savepoint = toolbox.connection.prepareStatement("SAVEPOINT dflat_txn")
let savepoint = toolbox.connection.prepareStaticStatement("SAVEPOINT dflat_txn")
guard SQLITE_DONE == sqlite3_step(savepoint) else { throw TransactionError.others }
let retval = updater(toolbox)
guard let updatedObject = retval else {
let errcode = sqlite3_extended_errcode(toolbox.connection.sqlite)
let rollback = toolbox.connection.prepareStatement("ROLLBACK TO dflat_txn")
let rollback = toolbox.connection.prepareStaticStatement("ROLLBACK TO dflat_txn")
// We cannot handle the situation where the rollback failed.
let status = sqlite3_step(rollback)
assert(status == SQLITE_DONE)
Expand All @@ -67,11 +67,11 @@ public final class SQLiteTransactionContext: TransactionContext {
throw TransactionError.others
}
}
let release = toolbox.connection.prepareStatement("RELEASE dflat_txn")
let release = toolbox.connection.prepareStaticStatement("RELEASE dflat_txn")
let status = sqlite3_step(release)
if status == SQLITE_FULL {
// In case of disk full, rollback.
let rollback = toolbox.connection.prepareStatement("ROLLBACK TO dflat_txn")
let rollback = toolbox.connection.prepareStaticStatement("ROLLBACK TO dflat_txn")
let status = sqlite3_step(rollback)
assert(status == SQLITE_DONE)
throw TransactionError.diskFull
Expand Down
18 changes: 9 additions & 9 deletions src/sqlite/SQLiteWorkspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ public final class SQLiteWorkspace: Workspace {
Self.snapshot = nil
return retval
}
let begin = pointee.prepareStatement("BEGIN")
let begin = pointee.prepareStaticStatement("BEGIN")
sqlite3_step(begin)
let retval = closure()
let commit = pointee.prepareStatement("COMMIT")
let commit = pointee.prepareStaticStatement("COMMIT")
sqlite3_step(commit)
Self.snapshot = nil
return retval
Expand Down Expand Up @@ -397,7 +397,7 @@ public final class SQLiteWorkspace: Workspace {

private func invokeChangesHandler(_ transactionalObjectTypes: [ObjectIdentifier], connection: SQLiteConnection, resultPublishers: [ObjectIdentifier: ResultPublisher], tableState: SQLiteTableState, changesHandler: Workspace.ChangesHandler) -> Bool {
let txnContext = SQLiteTransactionContext(state: tableState, objectTypes: transactionalObjectTypes, changesTimestamp: state.changesTimestamp.load(), connection: connection)
let begin = connection.prepareStatement("BEGIN")
let begin = connection.prepareStaticStatement("BEGIN")
guard SQLITE_DONE == sqlite3_step(begin) else {
return false
}
Expand All @@ -406,15 +406,15 @@ public final class SQLiteWorkspace: Workspace {
txnContext.destroy()
// This transaction is aborted by user. rollback.
if txnContext.aborted {
let rollback = connection.prepareStatement("ROLLBACK")
let rollback = connection.prepareStaticStatement("ROLLBACK")
let status = sqlite3_step(rollback)
precondition(status == SQLITE_DONE)
return false
}
let commit = connection.prepareStatement("COMMIT")
let commit = connection.prepareStaticStatement("COMMIT")
let status = sqlite3_step(commit)
if SQLITE_FULL == status {
let rollback = connection.prepareStatement("ROLLBACK")
let rollback = connection.prepareStaticStatement("ROLLBACK")
let status = sqlite3_step(rollback)
precondition(status == SQLITE_DONE)
return false
Expand Down Expand Up @@ -481,7 +481,7 @@ extension SQLiteWorkspace {
// It is OK to create connection, etc. before acquiring the lock as long as we don't do mutation.
tableSpace.lock()
defer { tableSpace.unlock() }
let begin = connection.prepareStatement("BEGIN")
let begin = connection.prepareStaticStatement("BEGIN")
// If we cannot start a transaction, nothing we can do, just wait for re-trigger.
guard SQLITE_DONE == sqlite3_step(begin) else { return }
// Make sure the table exists before we query.
Expand All @@ -505,10 +505,10 @@ extension SQLiteWorkspace {
}
}
// Try to commit.
let commit = connection.prepareStatement("COMMIT")
let commit = connection.prepareStaticStatement("COMMIT")
let status = sqlite3_step(commit)
if SQLITE_FULL == status {
let rollback = connection.prepareStatement("ROLLBACK")
let rollback = connection.prepareStaticStatement("ROLLBACK")
let status = sqlite3_step(rollback)
precondition(status == SQLITE_DONE)
// In case we failed, trigger a redo a few seconds later.
Expand Down
Loading

0 comments on commit dc62593

Please sign in to comment.