internal SqliteDataReader (SqliteCommand cmd, Sqlite3.Vdbe pVm, int version) { command = cmd; rows = new List<object[]>(); column_names_sens = new Dictionary<String, Object>(); column_names_insens = new Dictionary<String, Object>( StringComparer.InvariantCultureIgnoreCase ); closed = false; current_row = -1; reading = true; ReadpVm (pVm, version, cmd); ReadingDone (); }
internal void ReadpVm (Sqlite3.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, 3 == guid for (int i = 0; i < pN; i++) { string decl = Sqlite3.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; else if (decltypes[i] == "guid") declmode[i] = 3; } } } 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 = Sqlite3.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 (Sqlite3.sqlite3_column_type (pVm, i)) { case 1: long val = Sqlite3.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] = Sqlite3.sqlite3_column_double (pVm, i); break; case 3: data_row[i] = Sqlite3.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) if (data_row[i] == null) data_row[i] = null; else data_row[i] = DateTime.Parse((string)data_row[i], System.Globalization.CultureInfo.InvariantCulture); else if (declmode[i] == 3) data_row[i] = Guid.Parse((string) data_row[i]); break; case 4: int blobbytes = Sqlite3.sqlite3_column_bytes16 (pVm, i); byte[] blob = Sqlite3.sqlite3_column_blob(pVm, i); //byte[] blob = new byte[blobbytes]; //Marshal.Copy (blobptr, blob, 0, blobbytes); data_row[i] = blob ?? new byte[0]; break; case 5: data_row[i] = null; break; default: throw new Exception ("FATAL: Unknown sqlite3_column_type"); //} } } rows.Add (data_row); } }
DataTable GetSchemaDataTable( SqliteCommand cmd, string[] restrictionValues ) { if ( restrictionValues != null && cmd.Parameters.Count > 0 ) { for ( int i = 0; i < restrictionValues.Length; i++ ) cmd.Parameters[i].Value = restrictionValues[i]; } SqliteDataAdapter adapter = new SqliteDataAdapter( cmd ); DataTable dt = new DataTable(); adapter.Fill( dt ); return dt; }
private void CreateNewCommand (ref SqliteCommand command) { SqliteCommand sourceCommand = SourceCommand; if (command == null) { command = sourceCommand.Connection.CreateCommand () as SqliteCommand; command.CommandTimeout = sourceCommand.CommandTimeout; command.Transaction = sourceCommand.Transaction; } command.CommandType = CommandType.Text; command.UpdatedRowSource = UpdateRowSource.None; command.Parameters.Clear (); }
DataTable GetSchemaViews( string[] restrictionValues ) { SqliteCommand cmd = new SqliteCommand( "SELECT type, name, tbl_name, rootpage, sql " + " FROM sqlite_master " + " WHERE (name = :pname or :pname is null) " + " AND type = 'view' " + " ORDER BY name", this ); cmd.Parameters.Add( "pname", DbType.String ).Value = DBNull.Value; return GetSchemaDataTable( cmd, restrictionValues ); }