ADDataStore

public class ADDataStore

Represents an in-memory Data Store used to provide SQL ability to data sources that typically do not support SQL (such as JSON or XML). ADDataStore works with ADTableStore to define both the structure of the tables and to contain the rows of data stored in each table. ADDataStore understands a subset of the full SQL instruction set (as known by SQLite).

Use the execute method to execute non-query SQL statements against the Data Store. For example:

let datastore = ADDataStore()

var sql = """
CREATE TABLE IF NOT EXISTS parts
(
    part_id           INTEGER   PRIMARY KEY AUTOINCREMENT,
    stock             INTEGER   DEFAULT 0   NOT NULL,
    name              TEXT,
    description       TEXT      CHECK( description != '' ),    -- empty strings not allowed
    available         BOOL      DEFAULT true
);
"""

try datastore.execute(sql)

Use the query method to execute query SQL statements and return rows matching the result. For example:

let results = try datastore.query("SELECT * FROM parts")
print(results)

Remark

Since a ADDataStore holds all data in-memory, care should be taken not to overload memory. As a result, an ADDataStore is not a good choice for working with large amounts of data.
  • The collection of tables stored in the Data Store.

    Declaration

    Swift

    public var tables: [String:ADTableStore] = [:]
  • Returns true if a transaction is currently open, else returns false.

    Declaration

    Swift

    public var isTransactionOpen: Bool
  • Returns the row ID of the table that a record was last inserted into.

    Declaration

    Swift

    public var lastInsertedRowID: Int
  • Returns the number of rows modified by the last SQL command.

    Declaration

    Swift

    public var numberOfRecordsChanged: Int
  • Initializes a new instance of the Data Store.

    Declaration

    Swift

    public init()
  • Initializes a new ADDataStore and sets its initial properties.

    Declaration

    Swift

    public init(fromInstance dictionary: ADInstanceDictionary)

    Parameters

    dictionary

    A ADInstanceDictionary to initialize the store from.

  • Initializes a new ADDataStore and sets its initial properties.

    Swift Portable Object Notation (SPON) data format that allows complex data models to be encoded in a portable text string that encodes not only property keys and data, but also includes type information about the encoded data. For example, using the Address struct above:

    @obj:Address<state$=`TX` city$=`Seabrook` addr1$=`25 Nasa Rd 1` zip$=`77586` addr2$=`Apt #123`>
    

    The portable, human-readable string format encodes values with a single character type designator as follows:

    • % - Bool
    • ! - Int
    • $ - String
    • ^ - Float
    • & - Double
    • * - Embedded NSData or Data value

    Additionally, embedded arrays will be in the @array[...] format and embedded dictionaries in the @obj:type<...> format.

    Declaration

    Swift

    public init(fromSPON spon: String)

    Parameters

    spon

    A string holding the SPON encoded data to initialize the store from.

  • Checks to see if the Data Source contains a table with the given name.

    Declaration

    Swift

    public func hasTable(named table: String) -> Bool

    Parameters

    table

    The name of the table to look for.

    Return Value

    true if the Data Store contains the table, else false.

  • Prepares a SQL Statement for execution by replacing instances of the ? character with the values from the given array of parameter values. The values will be properly escaped before being added to the data stream.

    Example:

    let datastore = ADDataStore()
    let sql = try datastore.prepareSQL("SELECT * FROM parts WHERE part_id = ?", [4])
    

    Declaration

    Swift

    public func prepareSQL(_ sql: String, parameters: [Any]) throws -> String

    Parameters

    sql

    The SQL statement to prepare for execution.

    parameters

    An array of parameter values to insert into the SQL statement.

    Return Value

    The SQL statement with all ? characters replaced with escaped values from the parameters array.

  • Executes a non-query SQL statement against the Data Store. ADDataStore understands a subset of the full SQL instruction set (as known by SQLite) and as such, creating indexes, triggers, views and named save points are not currently supported.

    Example:

    let datastore = ADDataStore()
    
    var sql = """
    CREATE TABLE IF NOT EXISTS parts
    (
        part_id           INTEGER   PRIMARY KEY AUTOINCREMENT,
        stock             INTEGER   DEFAULT 0   NOT NULL,
        name              TEXT,
        description       TEXT      CHECK( description != '' ),    -- empty strings not allowed
        available         BOOL      DEFAULT true
    );
    """
    
    try datastore.execute(sql)
    

    Declaration

    Swift

    public func execute(_ sql: String, parameters: [Any] = []) throws

    Parameters

    sql

    The SQL statements to execute against the Data Store.

    parameters

    An array of optional parameter values used to replace any ? characters in the SQL Statement.

  • Use the query method to execute query SQL statements and return rows matching the result. ADDataStore understands a subset of the full SQL instruction set (as known by SQLite), however, most common SELECT statements should work as expected.

    Example:

    let results = try datastore.query("SELECT * FROM parts WHERE part_id = ?", [2])
    print(results)
    

    Declaration

    Swift

    public func query(_ sql: String, parameters: [Any] = []) throws -> ADRecordSet

    Parameters

    sql

    The SQL statements to execute against the Data Store.

    parameters

    An array of optional parameter values used to replace any ? characters in the SQL Statement.

    Return Value

    An ADRecordSet containing all of the rows from the Data Store matching the given query or an empty set if no rows matched.

  • Encodes the data store into an Instance Dictionary for storage in a Swift Portable Object Notation (SPON) format. -Returns: The data store represented as an Instance Dictionary.

    Declaration

    Swift

    public func encode() -> ADInstanceDictionary
  • Decodes the data store from an Instance Dictionary that has been read from a Swift Portable Object Notation (SPON) stream.

    Declaration

    Swift

    public func decode(fromInstance dictionary: ADInstanceDictionary)

    Parameters

    dictionary

    A ADInstanceDictionary representing the values for the data store.