public override int ExecuteSql(string sql, QueryParameterCollection parameters = null) { lock (this) // although sqlite3 is thread-safe, we need to make sure that the last_insert_rowid is correct { var stopwatch = SqlLogger != null?Stopwatch.StartNew() : null; var stmt = CreateCommand(sql, parameters); try { int returnCode = SQLitePCL.raw.sqlite3_step(stmt); if (returnCode == SQLitePCL.raw.SQLITE_ROW) { return(0); // quietly eat any rows being returned } if (returnCode != SQLitePCL.raw.SQLITE_DONE) { throw new SqliteException((SqliteReturnCode)returnCode, (SqliteExtendedErrorCode)SQLitePCL.raw.sqlite3_extended_errcode(DbHandle), SQLitePCL.raw.sqlite3_errmsg(DbHandle).utf8_to_string()); } _lastRowId.Value = SQLitePCL.raw.sqlite3_last_insert_rowid(DbHandle); return(SQLitePCL.raw.sqlite3_changes(DbHandle)); } finally { SQLitePCL.raw.sqlite3_finalize(stmt); SqlLogger?.LogSql(sql, parameters?.ToDictionary(p => SqlDialect.CreateParameterExpression(p.Name), p => p.Value), stopwatch?.Elapsed ?? TimeSpan.Zero); } } }
public ExecutionContext Prepare(string sql, ParameterContext parameterContext) { var cloneParameterContext = CloneParameters(parameterContext); var routeContext = ExecuteRoute(sql, cloneParameterContext); ExecutionContext result = new ExecutionContext(routeContext.GetSqlCommandContext()); var executionUnits = ExecuteRewrite(sql, cloneParameterContext, routeContext); result.GetExecutionUnits().AddAll(executionUnits); if (true) { SqlLogger.LogSql(sql, false, result.GetSqlCommandContext(), result.GetExecutionUnits()); } return(result); }
public override IEnumerable <Dictionary <string, object> > ExecuteSqlReader(string sql, QueryParameterCollection parameters) { var stopwatch = SqlLogger != null?Stopwatch.StartNew() : null; try { BeginTransaction(IsolationLevel.None); List <Dictionary <string, object> > records = new List <Dictionary <string, object> >(); using (var cmd = CreateCommand(sql, parameters, CommandType.Text)) { using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { Dictionary <string, object> rec = new Dictionary <string, object>(); for (int i = 0; i < reader.FieldCount; i++) { string fieldName = reader.GetName(i); if (reader.IsDBNull(i)) { rec[fieldName] = null; } else { rec[fieldName] = reader.GetValue(i); } } records.Add(rec); } } } return(records); } finally { CommitTransaction(); SqlLogger?.LogSql(sql, parameters?.ToDictionary(p => SqlDialect.CreateParameterExpression(p.Name), p => p.Value), stopwatch?.Elapsed ?? TimeSpan.Zero); } }
public override int ExecuteProcedure(string procName, QueryParameterCollection parameters = null) { var stopwatch = SqlLogger != null?Stopwatch.StartNew() : null; try { BeginTransaction(IsolationLevel.None); using (var cmd = CreateCommand(procName, parameters, CommandType.StoredProcedure)) { return(cmd.ExecuteNonQuery()); } } finally { CommitTransaction(); SqlLogger?.LogSql("EXEC " + procName, parameters?.ToDictionary(p => SqlDialect.CreateParameterExpression(p.Name), p => p.Value), stopwatch?.Elapsed ?? TimeSpan.Zero); } }
public override IEnumerable <Dictionary <string, object> > ExecuteSqlReader(string sql, QueryParameterCollection parameters) { var stopwatch = SqlLogger != null?Stopwatch.StartNew() : null; var stmt = CreateCommand(sql, parameters); try { for (;;) { var returnCode = SQLitePCL.raw.sqlite3_step(stmt); if (returnCode == SQLitePCL.raw.SQLITE_BUSY) { Task.Delay(100).Wait(); continue; } if (returnCode == SQLitePCL.raw.SQLITE_DONE) { break; } if (returnCode != SQLitePCL.raw.SQLITE_ROW) { throw new SqliteException((SqliteReturnCode)returnCode, (SqliteExtendedErrorCode)SQLitePCL.raw.sqlite3_extended_errcode(DbHandle), SQLitePCL.raw.sqlite3_errmsg(DbHandle).utf8_to_string()); } Dictionary <string, object> record = new Dictionary <string, object>(); for (int i = 0; i < SQLitePCL.raw.sqlite3_column_count(stmt); i++) { string fieldName = SQLitePCL.raw.sqlite3_column_name(stmt, i).utf8_to_string(); var columnType = SQLitePCL.raw.sqlite3_column_type(stmt, i); switch (columnType) { case SQLitePCL.raw.SQLITE_BLOB: record[fieldName] = SQLitePCL.raw.sqlite3_column_blob(stmt, i).ToArray(); break; case SQLitePCL.raw.SQLITE_TEXT: record[fieldName] = SQLitePCL.raw.sqlite3_column_text(stmt, i).utf8_to_string(); break; case SQLitePCL.raw.SQLITE_FLOAT: record[fieldName] = SQLitePCL.raw.sqlite3_column_double(stmt, i); break; case SQLitePCL.raw.SQLITE_INTEGER: record[fieldName] = SQLitePCL.raw.sqlite3_column_int64(stmt, i); break; case SQLitePCL.raw.SQLITE_NULL: record[fieldName] = null; break; } } yield return(record); } } finally { SQLitePCL.raw.sqlite3_finalize(stmt); } SqlLogger?.LogSql(sql, parameters?.ToDictionary(p => SqlDialect.CreateParameterExpression(p.Name), p => p.Value), stopwatch?.Elapsed ?? TimeSpan.Zero); }