/// <summary> /// Executes a sql statement on a given table in local SQLite database. /// </summary> /// <param name="table">The table definition.</param> /// <param name="sql">The SQL query to execute.</param> /// <param name="parameters">The query parameters.</param> /// <returns>The result of query.</returns> protected virtual IList <JObject> ExecuteQueryInternal(TableDefinition table, string sql, IDictionary <string, object> parameters) { table = table ?? new TableDefinition(); parameters = parameters ?? new Dictionary <string, object>(); var rows = new List <JObject>(); sqlite3_stmt statement = SQLitePCLRawHelpers.GetSqliteStatement(sql, connection); using (statement) { foreach (KeyValuePair <string, object> parameter in parameters) { var index = raw.sqlite3_bind_parameter_index(statement, parameter.Key); SQLitePCLRawHelpers.Bind(connection, statement, index, parameter.Value); } int rc; while ((rc = raw.sqlite3_step(statement)) == raw.SQLITE_ROW) { var row = ReadRow(table, statement); rows.Add(row); } SQLitePCLRawHelpers.VerifySQLiteResponse(rc, raw.SQLITE_DONE, connection); } return(rows); }
/// <summary> /// Initializes a new instance of <see cref="MobileServiceSQLiteStore"/> /// </summary> /// <param name="fileName">Name of the local SQLite database file.</param> public MobileServiceSQLiteStore(string fileName) { if (fileName == null) { throw new ArgumentNullException("fileName"); } if (this.connection == null) { this.connection = SQLitePCLRawHelpers.GetSqliteConnection(fileName); } }
/// <summary> /// Initializes a new instance of <see cref="MobileServiceSQLiteStore"/> /// </summary> /// <param name="fileName">Name of the local SQLite database file.</param> public MobileServiceSQLiteStore(string fileName) { if (fileName == null) { throw new ArgumentNullException("fileName"); } if (this.connection == null) { // Fully qualify the path var dbPath = fileName.StartsWith("/") ? fileName : Path.Combine(MobileServiceClient.DefaultDatabasePath, fileName); MobileServiceClient.EnsureFileExists(dbPath); this.connection = SQLitePCLRawHelpers.GetSqliteConnection(dbPath); } }
/// <summary> /// Executes a sql statement on a given table in local SQLite database. /// </summary> /// <param name="sql">The SQL statement to execute.</param> /// <param name="parameters">The query parameters.</param> protected virtual void ExecuteNonQueryInternal(string sql, IDictionary <string, object> parameters) { parameters = parameters ?? new Dictionary <string, object>(); int rc = raw.sqlite3_prepare_v2(connection, sql, out sqlite3_stmt stmt); SQLitePCLRawHelpers.VerifySQLiteResponse(rc, raw.SQLITE_OK, connection); using (stmt) { foreach (KeyValuePair <string, object> parameter in parameters) { var index = raw.sqlite3_bind_parameter_index(stmt, parameter.Key); SQLitePCLRawHelpers.Bind(connection, stmt, index, parameter.Value); } int result = raw.sqlite3_step(stmt); SQLitePCLRawHelpers.VerifySQLiteResponse(result, raw.SQLITE_DONE, connection); } }
private JObject ReadRow(TableDefinition table, sqlite3_stmt statement) { var row = new JObject(); for (int i = 0; i < raw.sqlite3_column_count(statement); i++) { string name = raw.sqlite3_column_name(statement, i).utf8_to_string(); object value = SQLitePCLRawHelpers.GetValue(statement, i); if (table.TryGetValue(name, out ColumnDefinition column)) { JToken jVal = SqlHelpers.DeserializeValue(value, column.StoreType, column.JsonType); row[name] = jVal; } else { row[name] = value == null ? null : JToken.FromObject(value); } } return(row); }