public T ExecuteScalar <T>() { if (_conn.Trace) { _conn.InvokeTrace("Executing Query: " + this); } T result = default(T); lock (_conn.SyncObject) { IntPtr stmt = Prepare(); try { SQLite3.Result result2 = SQLite3.Step(stmt); switch (result2) { case SQLite3.Result.Row: { SQLite3.ColType type = SQLite3.ColumnType(stmt, 0); return((T)ReadCol(stmt, 0, type, typeof(T))); } default: throw SQLiteException.New(result2, SQLite3.GetErrmsg(_conn.Handle)); case SQLite3.Result.Done: return(result); } } finally { Finalize(stmt); } } }
public T ExecuteScalar <T>() { if (_conn.Trace) { Debug.WriteLine("Executing Query: " + this); } T val = default(T); var stmt = Prepare(); try { var r = SQLite3.Step(stmt); if (r == SQLite3.Result.Row) { var colType = SQLite3.ColumnType(stmt, 0); val = (T)ReadCol(stmt, 0, colType, typeof(T)); } else if (r == SQLite3.Result.Done) { } else { throw SQLiteException.New(r, SQLite3.GetErrmsg(_conn.Handle)); } } finally { Finalize(stmt); } return(val); }
/// <summary> /// Executes the query. /// </summary> /// <param name="maxRows"> /// The maximum rows. /// </param> /// <returns> /// The <see cref="ResultCollection"/>. /// </returns> public virtual ResultCollection ExecuteQuery(int maxRows) { ResultCollection results; IntPtr stmt = IntPtr.Zero; try { stmt = this.Prepare(); var cols = new ColumnInfoExt[SQLite3.ColumnCount(stmt)]; for (var i = 0; i < cols.Length; i++) { cols[i] = new ColumnInfoExt { Name = SQLite3.ColumnName16(stmt, i), ColumnIndex = i }; } results = new ResultCollection(cols); var rows = 0; while (SQLite3.Step(stmt) == SQLite3.Result.Row) { var obj = new ResultRow(); for (var i = 0; i < cols.Length; i++) { if (cols[i] == null) { continue; } var colType = SQLite3.ColumnType(stmt, i); cols[i].ColumnMainType = colType; var val = this.ReadCol(stmt, i, colType, cols[i].ColumnClrType); obj[cols[i].ColumnIndex] = val; } results.Add(obj); if (maxRows != 0 && ++rows == maxRows) { break; } } } finally { if (stmt != IntPtr.Zero) { SQLite3.Finalize(stmt); } } return(results); }
object ReadCol(IntPtr stmt, int index, Type clrType) { var type = SQLite3.ColumnType(stmt, index); if (type == SQLite3.ColType.Null) { return(null); } else { if (clrType == typeof(Byte) || clrType == typeof(UInt16) || clrType == typeof(SByte) || clrType == typeof(Int16) || clrType == typeof(Int32)) { return(Convert.ChangeType(SQLite3.ColumnInt(stmt, index), clrType)); } else if (clrType == typeof(Boolean)) { return((Byte)Convert.ChangeType(SQLite3.ColumnInt(stmt, index), typeof(Byte)) == 1); } else if (clrType == typeof(UInt32) || clrType == typeof(Int64)) { return(Convert.ChangeType(SQLite3.ColumnInt64(stmt, index), clrType)); } else if (clrType == typeof(Single) || clrType == typeof(Double) || clrType == typeof(Decimal)) { return(Convert.ChangeType(SQLite3.ColumnDouble(stmt, index), clrType)); } else if (clrType == typeof(String)) { var text = Marshal.PtrToStringUni(SQLite3.ColumnText16(stmt, index)); return(text); } else if (clrType == typeof(DateTime)) { var text = Marshal.PtrToStringUni(SQLite3.ColumnText16(stmt, index)); return(Convert.ChangeType(text, clrType)); } else if (clrType.IsEnum) { return(SQLite3.ColumnInt(stmt, index)); } else { throw new NotSupportedException("Don't know how to read " + clrType); } } }
public IEnumerable <T> ExecuteDeferredQuery <T>(TableMapping map) { if (_conn.Trace) { _conn.InvokeTrace("Executing Query: " + this); } lock (_conn.SyncObject) { IntPtr stmt = Prepare(); try { TableMapping.Column[] cols = new TableMapping.Column[SQLite3.ColumnCount(stmt)]; for (int j = 0; j < cols.Length; j++) { string name = SQLite3.ColumnName16(stmt, j); cols[j] = map.FindColumn(name); } while (SQLite3.Step(stmt) == SQLite3.Result.Row) { object obj = Activator.CreateInstance(map.MappedType); for (int i = 0; i < cols.Length; i++) { if (cols[i] != null) { SQLite3.ColType colType = SQLite3.ColumnType(stmt, i); object val = ReadCol(stmt, i, colType, cols[i].ColumnType); cols[i].SetValue(obj, val); } } OnInstanceCreated(obj); yield return((T)obj); } } finally { ((_003CExecuteDeferredQuery_003Ec__Iterator3 <T>) /*Error near IL_0234: stateMachine*/)._003C_003E__Finally0(); } } }
public IEnumerable <T> ExecuteDeferredQuery <T>(TableMapping map) { if (_conn.Trace) { Debug.WriteLine("Executing Query: " + this); } var stmt = Prepare(); try { var cols = new TableMapping.Column[SQLite3.ColumnCount(stmt)]; for (int i = 0; i < cols.Length; i++) { var name = SQLite3.ColumnName16(stmt, i); cols[i] = map.FindColumn(name); } 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; } var colType = SQLite3.ColumnType(stmt, i); var val = ReadCol(stmt, i, colType, cols[i].ColumnType); cols[i].SetValue(obj, val); } OnInstanceCreated(obj); yield return((T)obj); } } finally { SQLite3.Finalize(stmt); } }
public T ExecuteScalar <T>(object[] source) { CheckDisposed(); Log(nameof(ExecuteScalar), source); OnExecutionStarted(); var sw = Stopwatch.StartNew(); T val = default(T); try { var r = InternalExecute(source); if (r == Result.Row) { var colType = SQLite3.ColumnType(Statement, 0); val = ReadCol <T>(Statement, 0, colType); } else if (r == Result.Done || r == Result.OK) { } } finally { if (Statement != null) { SQLite3.Reset(Statement); } sw.Stop(); Log(sw); OnExecutionEnded(); } return(val); }
public IEnumerable <T> ExecuteQuery <T>(TableMapping map, object[] source) { CheckDisposed(); Log(nameof(ExecuteQuery), source); OnExecutionStarted(); var sw = Stopwatch.StartNew(); try { var r = Result.OK; if (!Prepared) { Statement = Prepare(); Prepared = true; } //bind the values. if (source != null) { for (int i = 0; i < source.Length; i++) { BindParameter(Statement, i + 1, source[i], Connection.StoreDateTimeAsTicks); } } var cols = new TableColumn[SQLite3.ColumnCount(Statement)]; for (int i = 0; i < cols.Length; i++) { var name = SQLite3.ColumnNameUTF8(Statement, i); cols[i] = map.FindColumn(name); } while (SQLite3.Step(Statement) == Result.Row) { var obj = map.CreateInstance(); for (int i = 0; i < cols.Length; i++) { if (cols[i] == null) { continue; } var colType = SQLite3.ColumnType(Statement, i); var val = ReadCol(Statement, i, colType, cols[i].ColumnType, cols[i].PropertyDefaultValue); cols[i].SetValue(obj, val); } OnInstanceCreated(obj); yield return((T)obj); } if (r == Result.Done || r == Result.OK) { } else { var msg = SQLite3.GetErrorMessageUTF8(Connection.Handle); var ex = new SQLiteException(r, msg, sql: CommandText); ex.PopulateColumnFromTableMapping(map); throw ex; } } finally { if (Statement != null) { SQLite3.Reset(Statement); } sw.Stop(); Log(sw); OnExecutionEnded(); } }