#if !USING_BUILTIN_SQLITE #if os(OSX) import SQLiteMacOSX #elseif os(iOS) #if (arch(i386) || arch(x86_64)) import SQLiteiPhoneSimulator #else import SQLiteiPhoneOS #endif #elseif os(watchOS) #if (arch(i386) || arch(x86_64)) import SQLiteWatchSimulator #else import SQLiteWatchOS #endif #endif #endif /// When a type adopts both DatabaseValueConvertible and /// StatementColumnConvertible, it is granted with faster access to the SQLite /// database values. public protocol StatementColumnConvertible { /// Returns a value initialized from a raw SQLite statement pointer. /// /// As an example, here is the how Int64 adopts StatementColumnConvertible: /// /// extension Int64: StatementColumnConvertible { /// public init(sqliteStatement: SQLiteStatement, index: Int32) { /// self = sqlite3_column_int64(sqliteStatement, index) /// } /// } /// /// When you implement this method, don't check for NULL. /// /// See https://www.sqlite.org/c3ref/column_blob.html for more information. /// /// - parameters: /// - sqliteStatement: A pointer to an SQLite statement. /// - index: The column index. init(sqliteStatement: SQLiteStatement, index: Int32) } /// Types that adopt both DatabaseValueConvertible and /// StatementColumnConvertible can be efficiently initialized from /// database values. /// /// See DatabaseValueConvertible for more information. extension DatabaseValueConvertible where Self: StatementColumnConvertible { // MARK: Fetching From SelectStatement /// Returns a cursor over values fetched from a prepared statement. /// /// let statement = try db.makeSelectStatement("SELECT name FROM ...") /// let names = try String.fetchCursor(statement) // DatabaseCursor /// while let name = try names.next() { // String /// ... /// } /// /// If the database is modified during the cursor iteration, the remaining /// elements are undefined. /// /// The cursor must be iterated in a protected dispath queue. /// /// - parameters: /// - statement: The statement to run. /// - arguments: Optional statement arguments. /// - adapter: Optional RowAdapter /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> DatabaseCursor { // We'll read from leftmost column at index 0, unless adapter mangles columns let columnIndex = try Int32(adapter?.baseColumIndex(adaptedIndex: 0, with: statement) ?? 0) let sqliteStatement = statement.sqliteStatement return statement.fetchCursor(arguments: arguments) { if sqlite3_column_type(sqliteStatement, columnIndex) == SQLITE_NULL { throw DatabaseError(code: SQLITE_ERROR, message: "could not convert database value NULL to \(Self.self)", sql: statement.sql, arguments: arguments) } return Self.init(sqliteStatement: sqliteStatement, index: columnIndex) } } /// Returns an array of values fetched from a prepared statement. /// /// let statement = try db.makeSelectStatement("SELECT name FROM ...") /// let names = try String.fetchAll(statement) // [String] /// /// - parameters: /// - statement: The statement to run. /// - arguments: Optional statement arguments. /// - adapter: Optional RowAdapter /// - returns: An array of values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchAll(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> [Self] { return try Array(fetchCursor(statement, arguments: arguments, adapter: adapter)) } /// Returns a single value fetched from a prepared statement. /// /// let statement = try db.makeSelectStatement("SELECT name FROM ...") /// let name = try String.fetchOne(statement) // String? /// /// - parameters: /// - statement: The statement to run. /// - arguments: Optional statement arguments. /// - adapter: Optional RowAdapter /// - returns: An optional value. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchOne(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> Self? { // We'll read from leftmost column at index 0, unless adapter mangles columns let columnIndex = try Int32(adapter?.baseColumIndex(adaptedIndex: 0, with: statement) ?? 0) let sqliteStatement = statement.sqliteStatement let cursor = statement.fetchCursor(arguments: arguments) { () -> Self? in if sqlite3_column_type(sqliteStatement, columnIndex) == SQLITE_NULL { return nil } return Self.init(sqliteStatement: sqliteStatement, index: columnIndex) } return try cursor.next() ?? nil } } extension DatabaseValueConvertible where Self: StatementColumnConvertible { // MARK: Fetching From FetchRequest /// Returns a cursor over values fetched from a fetch request. /// /// let nameColumn = Column("name") /// let request = Person.select(nameColumn) /// let names = try String.fetchCursor(db, request) // DatabaseCursor /// while let name = try names.next() { // String /// ... /// } /// /// If the database is modified during the cursor iteration, the remaining /// elements are undefined. /// /// The cursor must be iterated in a protected dispath queue. /// /// - parameters: /// - db: A database connection. /// - request: A fetch request. /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchCursor(_ db: Database, _ request: FetchRequest) throws -> DatabaseCursor { let (statement, adapter) = try request.prepare(db) return try fetchCursor(statement, adapter: adapter) } /// Returns an array of values fetched from a fetch request. /// /// let nameColumn = Column("name") /// let request = Person.select(nameColumn) /// let names = try String.fetchAll(db, request) // [String] /// /// - parameter db: A database connection. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchAll(_ db: Database, _ request: FetchRequest) throws -> [Self] { let (statement, adapter) = try request.prepare(db) return try fetchAll(statement, adapter: adapter) } /// Returns a single value fetched from a fetch request. /// /// The result is nil if the query returns no row, or if no value can be /// extracted from the first row. /// /// let nameColumn = Column("name") /// let request = Person.select(nameColumn) /// let name = try String.fetchOne(db, request) // String? /// /// - parameter db: A database connection. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchOne(_ db: Database, _ request: FetchRequest) throws -> Self? { let (statement, adapter) = try request.prepare(db) return try fetchOne(statement, adapter: adapter) } } extension DatabaseValueConvertible where Self: StatementColumnConvertible { // MARK: Fetching From SQL /// Returns a cursor over values fetched from an SQL query. /// /// let names = try String.fetchCursor(db, "SELECT name FROM ...") // DatabaseCursor /// while let name = try names.next() { // String /// ... /// } /// /// If the database is modified during the cursor iteration, the remaining /// elements are undefined. /// /// The cursor must be iterated in a protected dispath queue. /// /// - parameters: /// - db: A database connection. /// - sql: An SQL query. /// - arguments: Optional statement arguments. /// - adapter: Optional RowAdapter /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchCursor(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> DatabaseCursor { return try fetchCursor(db, SQLFetchRequest(sql: sql, arguments: arguments, adapter: adapter)) } /// Returns an array of values fetched from an SQL query. /// /// let names = try String.fetchAll(db, "SELECT name FROM ...") // [String] /// /// - parameters: /// - db: A database connection. /// - sql: An SQL query. /// - arguments: Optional statement arguments. /// - adapter: Optional RowAdapter /// - returns: An array of values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchAll(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> [Self] { return try fetchAll(db, SQLFetchRequest(sql: sql, arguments: arguments, adapter: adapter)) } /// Returns a single value fetched from an SQL query. /// /// let name = try String.fetchOne(db, "SELECT name FROM ...") // String? /// /// - parameters: /// - db: A database connection. /// - sql: An SQL query. /// - arguments: Optional statement arguments. /// - adapter: Optional RowAdapter /// - returns: An optional value. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchOne(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> Self? { return try fetchOne(db, SQLFetchRequest(sql: sql, arguments: arguments, adapter: adapter)) } }