 						return SQLiteResult(database: self, resultset: resultSet!, rows: true)
 
 					default:
+						Log.debug("[SQL] ERROR: \(self.lastError)")
 						throw DatabaseError.error(self.lastError)
 					}
 				}
 				else {
+					Log.debug("[SQL] ERROR in prepare: \(self.lastError)")
 					throw DatabaseError.error(self.lastError)
 				}
 			}

	private var db: OpaquePointer? = nil
	private let mutex = Mutex()
	private var counter = 0
	let dialect: SQLDialect = SQLStandardDialect()

	enum DatabaseError: LocalizedError {

		return String(cString: sqlite3_errmsg(self.db))
	}

	func transaction<T>(name: String? = nil, alwaysRollback: Bool, callback: (() throws -> (T))) throws -> T {
		return try self.mutex.locked { () -> T in
			if alwaysRollback {
				self.hypotheticalCounter += 1
			}
			self.counter += 1

			defer {
				self.counter -= 1

				if alwaysRollback {
					self.hypotheticalCounter -= 1
				}
			}

			let savepointName = self.dialect.literalString("tx\(self.hypotheticalCounter)c\(self.counter)")

			if self.counter == 1 {
				try _ = self.perform("BEGIN")
			}
			else {
				try _ = self.perform("SAVEPOINT \(savepointName)")
			}

			do {
				let t = try callback()

				if alwaysRollback {
					if self.counter == 1 {
						try _ = self.perform("ROLLBACK")
					}
					else {
						try _ = self.perform("ROLLBACK TO SAVEPOINT \(savepointName)")
					}
				}
				else {
					if self.counter == 1 {
						try _ = self.perform("COMMIT")
					}
					else {
						try _ = self.perform("RELEASE SAVEPOINT \(savepointName)")
					}
				}

				return t
			}
			catch {
				if self.counter == 1 {
					try _ = self.perform("COMMIT")
				}
				else {
					try _ = self.perform("RELEASE SAVEPOINT \(savepointName)")
				}
				throw error
			}
		}
	}

