internal SqliteDataReader (SqliteCommand cmd, csSQLite.Vdbe pVm, int version) { command = cmd; rows = new ArrayList (); column_names_sens = new Hashtable (); #if NET_2_0 column_names_insens = new Hashtable (StringComparer.InvariantCultureIgnoreCase); #else column_names_insens = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant); #endif closed = false; current_row = -1; reading = true; ReadpVm (pVm, version, cmd); ReadingDone (); }
internal void ReadpVm (csSQLite.Vdbe pVm, int version, SqliteCommand cmd) { int pN; IntPtr pazValue; IntPtr pazColName; bool first = true; int[] declmode = null; while (true) { bool hasdata = cmd.ExecuteStatement(pVm, out pN, out pazValue, out pazColName); // For the first row, get the column information if (first) { first = false; if (version == 3) { // A decltype might be null if the type is unknown to sqlite. decltypes = new string[pN]; declmode = new int[pN]; // 1 == integer, 2 == datetime for (int i = 0; i < pN; i++) { string decl = csSQLite.sqlite3_column_decltype (pVm, i); if (decl != null) { decltypes[i] = decl.ToLower(System.Globalization.CultureInfo.InvariantCulture); if (decltypes[i] == "int" || decltypes[i] == "integer") declmode[i] = 1; else if (decltypes[i] == "date" || decltypes[i] == "datetime") declmode[i] = 2; } } } columns = new string[pN]; for (int i = 0; i < pN; i++) { string colName; //if (version == 2) { // IntPtr fieldPtr = Marshal.ReadIntPtr (pazColName, i*IntPtr.Size); // colName = Sqlite.HeapToString (fieldPtr, ((SqliteConnection)cmd.Connection).Encoding); //} else { colName = csSQLite.sqlite3_column_name (pVm, i); //} columns[i] = colName; column_names_sens [colName] = i; column_names_insens [colName] = i; } } if (!hasdata) break; object[] data_row = new object [pN]; for (int i = 0; i < pN; i++) { /* if (version == 2) { IntPtr fieldPtr = Marshal.ReadIntPtr (pazValue, i*IntPtr.Size); data_row[i] = Sqlite.HeapToString (fieldPtr, ((SqliteConnection)cmd.Connection).Encoding); } else { */ switch (csSQLite.sqlite3_column_type (pVm, i)) { case 1: long val = csSQLite.sqlite3_column_int64 (pVm, i); // If the column was declared as an 'int' or 'integer', let's play // nice and return an int (version 3 only). if (declmode[i] == 1 && val >= int.MinValue && val <= int.MaxValue) data_row[i] = (int)val; // Or if it was declared a date or datetime, do the reverse of what we // do for DateTime parameters. else if (declmode[i] == 2) data_row[i] = DateTime.FromFileTime(val); else data_row[i] = val; break; case 2: data_row[i] = csSQLite.sqlite3_column_double (pVm, i); break; case 3: data_row[i] = csSQLite.sqlite3_column_text (pVm, i); // If the column was declared as a 'date' or 'datetime', let's play // nice and return a DateTime (version 3 only). if (declmode[i] == 2) data_row[i] = DateTime.Parse((string)data_row[i]); break; case 4: int blobbytes = csSQLite.sqlite3_column_bytes16 (pVm, i); byte[] blob = csSQLite.sqlite3_column_blob(pVm, i); //byte[] blob = new byte[blobbytes]; //Marshal.Copy (blobptr, blob, 0, blobbytes); data_row[i] = blob; break; case 5: data_row[i] = null; break; default: throw new ApplicationException ("FATAL: Unknown sqlite3_column_type"); //} } } rows.Add (data_row); } }
// Executes a statement and returns whether there is more data available. internal bool ExecuteStatement(csSQLite.Vdbe pStmt, out int cols, out IntPtr pazValue, out IntPtr pazColName) { SqliteError err; //if (parent_conn.Version == 3) //{ err = (SqliteError)csSQLite.sqlite3_step (pStmt); if (err == SqliteError.ERROR) throw new SqliteExecutionException (GetError3()); pazValue = IntPtr.Zero; pazColName = IntPtr.Zero; // not used for v=3 cols = csSQLite.sqlite3_column_count (pStmt); /* } else { err = (SqliteError)csSQLite.sqlite3_step(pStmt, out cols, out pazValue, out pazColName); if (err == SqliteError.ERROR) throw new SqliteExecutionException (); } */ if (err == SqliteError.BUSY) throw new SqliteBusyException(); if (err == SqliteError.MISUSE) throw new SqliteExecutionException(); // err is either ROW or DONE. return err == SqliteError.ROW; }
// Executes a statement and ignores its result. private void ExecuteStatement(csSQLite.Vdbe pStmt) { int cols; IntPtr pazValue, pazColName; ExecuteStatement (pStmt, out cols, out pazValue, out pazColName); }
private void GetNextStatement (string pzStart, ref string pzTail, ref csSQLite.Vdbe pStmt) { //if (parent_conn.Version == 3) //{ UTF8Encoding encoding = new UTF8Encoding(); SqliteError err = (SqliteError)csSQLite.sqlite3_prepare(parent_conn.Handle2, pzStart, encoding.GetBytes(pzStart).Length, ref pStmt, ref pzTail); //SqliteError err = (SqliteError)csSQLite.sqlite3_prepare(parent_conn.Handle2, pzStart, encoding.GetBytes(pzStart).Length, ref pStmt, ref pzTail); //SqliteError err = Sqlite.sqlite3_prepare16 (parent_conn.Handle, pzStart, -1, out pStmt, out pzTail); if (err != SqliteError.OK) throw new SqliteSyntaxException (GetError3()); /* } else { IntPtr errMsg; SqliteError err = Sqlite.sqlite_compile (parent_conn.Handle, pzStart, out pzTail, out pStmt, out errMsg); if (err != SqliteError.OK) { string msg = "unknown error"; if (errMsg != IntPtr.Zero) { msg = Marshal.PtrToStringAnsi (errMsg); Sqlite.sqliteFree (errMsg); } throw new SqliteSyntaxException (msg); } } */ }
private void BindParameters3 (csSQLite.Vdbe pStmt) { if (sql_params == null) return; if (sql_params.Count == 0) return; int pcount = csSQLite.sqlite3_bind_parameter_count (pStmt); for (int i = 1; i <= pcount; i++) { String name = csSQLite.sqlite3_bind_parameter_name (pStmt, i); SqliteParameter param = null; if (name != null) param = sql_params[name] as SqliteParameter; else param = sql_params[i-1] as SqliteParameter; if (param.Value == null) { csSQLite.sqlite3_bind_null (pStmt, i); continue; } Type ptype = param.Value.GetType (); if (ptype.IsEnum) ptype = Enum.GetUnderlyingType (ptype); SqliteError err; if (ptype.Equals (typeof (String))) { String s = (String)param.Value; err = (SqliteError)csSQLite.sqlite3_bind_text(pStmt, i, s, -1, null); } else if (ptype.Equals (typeof (DBNull))) { err = (SqliteError)csSQLite.sqlite3_bind_null(pStmt, i); } else if (ptype.Equals (typeof (Boolean))) { bool b = (bool)param.Value; err = (SqliteError)csSQLite.sqlite3_bind_int(pStmt, i, b ? 1 : 0); } else if (ptype.Equals (typeof (Byte))) { err = (SqliteError)csSQLite.sqlite3_bind_int(pStmt, i, (Byte)param.Value); } else if (ptype.Equals (typeof (Char))) { err = (SqliteError)csSQLite.sqlite3_bind_int(pStmt, i, (Char)param.Value); } else if (ptype.IsEnum) { err = (SqliteError)csSQLite.sqlite3_bind_int(pStmt, i, (Int32)param.Value); } else if (ptype.Equals (typeof (Int16))) { err = (SqliteError)csSQLite.sqlite3_bind_int(pStmt, i, (Int16)param.Value); } else if (ptype.Equals (typeof (Int32))) { err = (SqliteError)csSQLite.sqlite3_bind_int(pStmt, i, (Int32)param.Value); } else if (ptype.Equals (typeof (SByte))) { err = (SqliteError)csSQLite.sqlite3_bind_int(pStmt, i, (SByte)param.Value); } else if (ptype.Equals (typeof (UInt16))) { err = (SqliteError)csSQLite.sqlite3_bind_int(pStmt, i, (UInt16)param.Value); } else if (ptype.Equals (typeof (DateTime))) { DateTime dt = (DateTime)param.Value; err = (SqliteError)csSQLite.sqlite3_bind_int64(pStmt, i, dt.ToFileTime()); } else if (ptype.Equals (typeof (Double))) { err = (SqliteError)csSQLite.sqlite3_bind_double(pStmt, i, (Double)param.Value); } else if (ptype.Equals (typeof (Single))) { err = (SqliteError)csSQLite.sqlite3_bind_double(pStmt, i, (Single)param.Value); } else if (ptype.Equals (typeof (UInt32))) { err = (SqliteError)csSQLite.sqlite3_bind_int64(pStmt, i, (UInt32)param.Value); } else if (ptype.Equals (typeof (Int64))) { err = (SqliteError)csSQLite.sqlite3_bind_int64(pStmt, i, (Int64)param.Value); } else if (ptype.Equals (typeof (Byte[]))) { err = (SqliteError)csSQLite.sqlite3_bind_blob(pStmt, i, (byte[])param.Value, ((byte[])param.Value).Length, null); } else { throw new ApplicationException("Unkown Parameter Type"); } if (err != SqliteError.OK) { throw new ApplicationException ("Sqlite error in bind " + err); } } }