public override IEnumerable <T> ExecuteQueryScalars <T>() { var stmt = Prepare(); try { if (SQLite3.ColumnCount(stmt) < 1) { throw new InvalidOperationException("QueryScalars should return at least one column"); } while (SQLite3.Step(stmt) == SQLite3.Result.Row) { var colType = SQLite3.ColumnType(stmt, 0); var val = ReadCol(stmt, 0, colType, typeof(T)); if (val == null) { yield return(default(T)); } else { yield return((T)val); } } } finally { Finalize(stmt); } }
public override int ExecuteNonQuery() { var r = SQLite3.Result.OK; var stmt = Prepare(); r = SQLite3.Step(stmt); Finalize(stmt); if (r == SQLite3.Result.Done) { int rowsAffected = SQLite3.Changes(_conn.Handle); return(rowsAffected); } else if (r == SQLite3.Result.Error) { string msg = SQLite3.GetErrmsg(_conn.Handle); throw SQLiteException.New(r, msg); } else if (r == SQLite3.Result.Constraint) { if (SQLite3.ExtendedErrCode(_conn.Handle) == SQLite3.ExtendedResult.ConstraintNotNull) { throw NotNullConstraintViolationException.New(r, SQLite3.GetErrmsg(_conn.Handle)); } } throw SQLiteException.New(r, SQLite3.GetErrmsg(_conn.Handle)); }
public override T ExecuteScalar <T>() { T val = default(T); var stmt = Prepare(); try { var r = SQLite3.Step(stmt); if (r == SQLite3.Result.Row) { var colType = SQLite3.ColumnType(stmt, 0); var colval = ReadCol(stmt, 0, colType, typeof(T)); if (colval != null) { val = (T)colval; } } else if (r == SQLite3.Result.Done) { } else { throw SQLiteException.New(r, SQLite3.GetErrmsg(_conn.Handle)); } } finally { Finalize(stmt); } return(val); }
public int ExecuteNonQuery(object[] source) { if (Initialized && Statement == NullStatement) { throw new ObjectDisposedException(nameof(PreparedSqlLiteInsertCommand)); } if (Connection.IsClosed) { Connection.RenewConnection(); } var r = SQLite3.Result.OK; if (!Initialized) { Statement = SQLite3.Prepare2(Connection.Handle, CommandText); Initialized = true; } //bind the values. if (source != null) { for (int i = 0; i < source.Length; i++) { SQLiteCommand.BindParameter(Statement, i + 1, source[i], Connection.StoreDateTimeAsTicks, Connection.DateTimeStringFormat, Connection.StoreTimeSpanAsTicks); } } r = SQLite3.Step(Statement); if (r == SQLite3.Result.Done) { int rowsAffected = SQLite3.Changes(Connection.Handle); SQLite3.Reset(Statement); return(rowsAffected); } else if (r == SQLite3.Result.Error) { string msg = SQLite3.GetErrmsg(Connection.Handle); SQLite3.Reset(Statement); throw SQLiteException.New(r, msg); } else if (r == SQLite3.Result.Constraint && SQLite3.ExtendedErrCode(Connection.Handle) == SQLite3.ExtendedResult.ConstraintNotNull) { SQLite3.Reset(Statement); throw NotNullConstraintViolationException.New(r, SQLite3.GetErrmsg(Connection.Handle)); } else if (r == SQLite3.Result.Constraint && SQLite3.ExtendedErrCode(Connection.Handle) == SQLite3.ExtendedResult.ConstraintUnique) { SQLite3.Reset(Statement); throw NotNullConstraintViolationException.New(r, SQLite3.GetErrmsg(Connection.Handle)); } else { SQLite3.Reset(Statement); throw SQLiteException.New(r, SQLite3.GetErrmsg(Connection.Handle)); } }
public IEnumerable <T> ExecuteDeferredQuery <T>(Base.TableMapping map, DaemonCompiledSetter _compiledSetter) where T : class { DaemonCompiledSetter <T, sqlite3_stmt> compiledSetter = _compiledSetter as DaemonCompiledSetter <T, sqlite3_stmt>; List <T> result = new List <T>(); if (_conn.IsClosed) { _conn.RenewConnection(); } Log.Logger.Debug(this.CommandText); var stmt = Prepare(); try { while (SQLite3.Step(stmt) == SQLite3.Result.Row) { var obj = Activator.CreateInstance(map.MappedType); for (int i = 0; i < compiledSetter.Columns.Length; i++) { if (compiledSetter.Columns[i] == null) { continue; } if (compiledSetter.Setters[i] != null) { if (obj is not T) { } compiledSetter.Setters[i].Invoke((T)obj, stmt, i); } else { var colType = SQLite3.ColumnType(stmt, i); var val = ReadCol(stmt, i, colType, compiledSetter.Columns[i].ColumnType); compiledSetter.Columns[i].SetValue(obj, val); } } OnInstanceCreated(obj); result.Add((T)obj); } } catch (Exception ex) { Log.Logger.Error(ex, "SqliteCommand.ExecuteDeferredQuery"); } finally { SQLite3.Finalize(stmt); } return(result); }
public override IEnumerable <T> ExecuteDeferredQuery <T>(Kit.Sql.Base.TableMapping map) { List <T> result = new List <T>(); if (_conn.IsClosed) { _conn.RenewConnection(); } Log.Logger.Debug(this.CommandText); var stmt = Prepare(); try { var cols = new Column[SQLite3.ColumnCount(stmt)]; var fastColumnSetters = new Action <T, sqlite3_stmt, int> [SQLite3.ColumnCount(stmt)]; for (int i = 0; i < cols.Length; i++) { var name = SQLite3.ColumnName16(stmt, i); cols[i] = map.FindColumn(name); if (cols[i] != null) { fastColumnSetters[i] = FastColumnSetter.GetFastSetter <T>(_conn, cols[i]); } } while (SQLite3.Step(stmt) == SQLite3.Result.Row) { var obj = Activator.CreateInstance(map.MappedType); for (int i = 0; i < cols.Length; i++) { if (cols[i] == null) { continue; } if (fastColumnSetters[i] != null) { if (obj is not T) { } fastColumnSetters[i].Invoke((T)obj, stmt, i); } else { var colType = SQLite3.ColumnType(stmt, i); var val = ReadCol(stmt, i, colType, cols[i].ColumnType); cols[i].SetValue(obj, val); } } OnInstanceCreated(obj); result.Add((T)obj); } } catch (Exception ex) { Log.Logger.Error(ex, "SqliteCommand.ExecuteDeferredQuery"); } finally { SQLite3.Finalize(stmt); } return(result); }