예제 #1
0
        // Executes a statement and ignores its result.
        private void ExecuteStatement(Sqlite3.Vdbe pStmt)
        {
            int    cols;
            IntPtr pazValue, pazColName;

            ExecuteStatement(pStmt, out cols, out pazValue, out pazColName);
        }
예제 #2
0
        private void GetNextStatement(string pzStart, ref string pzTail, ref Sqlite3.Vdbe pStmt)
        {
            SQLiteError err = (SQLiteError)Sqlite3.sqlite3_prepare_v2(parent_conn.Handle2, pzStart, pzStart.Length, ref pStmt, ref pzTail);

            if (err != SQLiteError.OK)
            {
                throw new SQLiteSyntaxException(parent_conn.Handle2.errCode, GetError3());
            }
        }
예제 #3
0
 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();
 }
예제 #4
0
        // Executes a statement and returns whether there is more data available.
        internal bool ExecuteStatement(Sqlite3.Vdbe pStmt, out int cols, out IntPtr pazValue, out IntPtr pazColName)
        {
            SQLiteError err;

            //if (parent_conn.Version == 3)
            //{
            err = (SQLiteError)Sqlite3.sqlite3_step(pStmt);

            if (err == SQLiteError.ERROR)
            {
                throw new SQLiteExecutionException(parent_conn.Handle2.errCode, GetError3() + "\n" + pStmt.zErrMsg);
            }

            pazValue   = IntPtr.Zero;
            pazColName = IntPtr.Zero;             // not used for v=3
            cols       = Sqlite3.sqlite3_column_count(pStmt);

            /*
             * }
             * else
             * {
             * err = (SqliteError)Sqlite3.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();
            }
            if (err == SQLiteError.CONSTRAINT)
            {
                throw new SQLiteException(parent_conn.Handle2.errCode, GetError3() + "\n" + pStmt.zErrMsg);
            }
            if (err == SQLiteError.MISMATCH)
            {
                throw new SQLiteException(parent_conn.Handle2.errCode, GetError3() + "\n" + pStmt.zErrMsg);
            }
            // err is either ROW or DONE.
            return(err == SQLiteError.ROW);
        }
예제 #5
0
        internal void ReadpVm(Sqlite3.Vdbe pVm, int version, SQLiteCommand cmd)
        {
            int    pN;
            IntPtr pazValue;
            IntPtr pazColName;
            bool   first = true;

            DeclaredMode[] 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 DeclaredMode[pN]; // 1 == integer, 2 == datetime
                        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] = DeclaredMode.Integer;
                                }
                                else if (decltypes[i] == "date" || decltypes[i] == "datetime")
                                {
                                    declmode[i] = DeclaredMode.DateTime;
                                }
                                else if (decltypes[i] == "uniqueidentifier" || decltypes[i] == "guid")
                                {
                                    declmode[i] = DeclaredMode.Guid;
                                }
                            }
                        }
                    }

                    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++)
                {
                    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' OR the type of column is unknown
                        // let's play nice and return an int (version 3 only).
                        if ((declmode[i] == DeclaredMode.Integer || decltypes[i] == null) && 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] == DeclaredMode.DateTime)
                        {
                            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] == DeclaredMode.DateTime)
                        {
                            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] == DeclaredMode.Guid)
                        {
                            Guid g;
                            if (data_row[i] == null)
                            {
                                data_row[i] = null;
                            }
                            else if (Guid.TryParse((string)data_row[i], out g))
                            {
                                data_row[i] = g;
                            }
                        }
                        break;

                    case 4:
                        byte[] blob = Sqlite3.sqlite3_column_blob(pVm, i);
                        data_row[i] = blob;
                        break;

                    case 5:
                        data_row[i] = null;
                        break;

                    default:
                        throw new Exception("FATAL: Unknown sqlite3_column_type");
                    }
                }
                rows.Add(data_row);
            }
        }
예제 #6
0
        public SQLiteDataReader ExecuteReader(CommandBehavior behavior, bool want_results, out int rows_affected)
        {
            Prepare();

            // The SQL string may contain multiple sql commands, so the main
            // thing to do is have Sqlite iterate through the commands.
            // If want_results, only the last command is returned as a
            // DataReader.	Otherwise, no command is returned as a
            // DataReader.

            //IntPtr psql; // pointer to SQL command

            // Sqlite 2 docs say this: By default, SQLite assumes that all data uses a fixed-size 8-bit
            // character (iso8859).	But if you give the --enable-utf8 option to the configure script, then the
            // library assumes UTF-8 variable sized characters. This makes a difference for the LIKE and GLOB
            // operators and the LENGTH() and SUBSTR() functions. The static string sqlite_encoding will be set
            // to either "UTF-8" or "iso8859" to indicate how the library was compiled. In addition, the sqlite.h
            // header file will define one of the macros SQLITE_UTF8 or SQLITE_ISO8859, as appropriate.
            //
            // We have no way of knowing whether Sqlite 2 expects ISO8859 or UTF-8, but ISO8859 seems to be the
            // default.	Therefore, we need to use an ISO8859(-1) compatible encoding, like ANSI.
            // OTOH, the user may want to specify the encoding of the bytes stored in the database, regardless
            // of what Sqlite is treating them as,

            // For Sqlite 3, we use the UTF-16 prepare function, so we need a UTF-16 string.

            /*
             * if (parent_conn.Version == 2)
             * psql = Sqlite.StringToHeap (sql.Trim(), parent_conn.Encoding);
             * else
             * psql = Marshal.StringToHGlobalUni (sql.Trim());
             */
            string queryval = sql.Trim();
            string pzTail   = sql.Trim();
            IntPtr errMsgPtr;

            parent_conn.StartExec();

            rows_affected = 0;

            try
            {
                while (true)
                {
                    Sqlite3.Vdbe pStmt = null;

                    queryval = pzTail;
                    GetNextStatement(queryval, ref pzTail, ref pStmt);

                    if (pStmt == null)
                    {
                        throw new Exception();
                    }

                    // pzTail is positioned after the last byte in the
                    // statement, which will be the NULL character if
                    // this was the last statement.
                    bool last = pzTail.Length == 0;

                    try
                    {
                        if (parent_conn.Version == 3)
                        {
                            BindParameters3(pStmt);
                        }

                        if (last && want_results)
                        {
                            return(new SQLiteDataReader(this, pStmt, parent_conn.Version));
                        }

                        ExecuteStatement(pStmt);

                        if (last)                        // rows_affected is only used if !want_results
                        {
                            rows_affected = NumChanges();
                        }
                    }
                    finally
                    {
                        //if (parent_conn.Version == 3)
                        Sqlite3.sqlite3_finalize(pStmt);
                        //else
                        //	Sqlite.sqlite_finalize (pStmt, out errMsgPtr);
                    }

                    if (last)
                    {
                        break;
                    }
                }

                return(null);
            }
            //alxwest: Console.WriteLine in shared functionality.
            //catch ( Exception ex )
            //{
            //    Console.WriteLine( ex.Message );
            //    return null;
            //}
            finally
            {
                parent_conn.EndExec();
                //Marshal.FreeHGlobal (psql);
            }
        }
예제 #7
0
        private void BindParameters3(Sqlite3.Vdbe pStmt)
        {
            if (sql_params == null)
            {
                return;
            }
            if (sql_params.Count == 0)
            {
                return;
            }

            int pcount = Sqlite3.sqlite3_bind_parameter_count(pStmt);

            for (int i = 1; i <= pcount; i++)
            {
                String name = Sqlite3.sqlite3_bind_parameter_name(pStmt, i);

                SQLiteParameter param = null;
                if (!String.IsNullOrEmpty(name))
                {
                    param = sql_params[name] as SQLiteParameter;
                }
                else
                {
                    param = sql_params[i - 1] as SQLiteParameter;
                }

                if (param.Value == null)
                {
                    Sqlite3.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)Sqlite3.sqlite3_bind_text(pStmt, i, s, -1, null);
                }
                else if (ptype.Equals(typeof(DBNull)))
                {
                    err = (SQLiteError)Sqlite3.sqlite3_bind_null(pStmt, i);
                }
                else if (ptype.Equals(typeof(Boolean)))
                {
                    bool b = (bool)param.Value;
                    err = (SQLiteError)Sqlite3.sqlite3_bind_int(pStmt, i, b ? 1 : 0);
                }
                else if (ptype.Equals(typeof(Byte)))
                {
                    err = (SQLiteError)Sqlite3.sqlite3_bind_int(pStmt, i, (Byte)param.Value);
                }
                else if (ptype.Equals(typeof(Char)))
                {
                    err = (SQLiteError)Sqlite3.sqlite3_bind_int(pStmt, i, (Char)param.Value);
                }
                else if (ptype.IsEnum)
                {
                    err = (SQLiteError)Sqlite3.sqlite3_bind_int(pStmt, i, (Int32)param.Value);
                }
                else if (ptype.Equals(typeof(Int16)))
                {
                    err = (SQLiteError)Sqlite3.sqlite3_bind_int(pStmt, i, (Int16)param.Value);
                }
                else if (ptype.Equals(typeof(Int32)))
                {
                    err = (SQLiteError)Sqlite3.sqlite3_bind_int(pStmt, i, (Int32)param.Value);
                }
                else if (ptype.Equals(typeof(SByte)))
                {
                    err = (SQLiteError)Sqlite3.sqlite3_bind_int(pStmt, i, (SByte)param.Value);
                }
                else if (ptype.Equals(typeof(UInt16)))
                {
                    err = (SQLiteError)Sqlite3.sqlite3_bind_int(pStmt, i, (UInt16)param.Value);
                }
                else if (ptype.Equals(typeof(DateTime)))
                {
                    DateTime dt = (DateTime)param.Value;
                    err = (SQLiteError)Sqlite3.sqlite3_bind_text(pStmt, i, dt.ToString("yyyy-MM-dd HH:mm:ss.fff"), -1, null);
                }
                else if (ptype.Equals(typeof(Decimal)))
                {
                    string val = ((Decimal)param.Value).ToString(CultureInfo.InvariantCulture);
                    err = (SQLiteError)Sqlite3.sqlite3_bind_text(pStmt, i, val, val.Length, null);
                }
                else if (ptype.Equals(typeof(Double)))
                {
                    err = (SQLiteError)Sqlite3.sqlite3_bind_double(pStmt, i, (Double)param.Value);
                }
                else if (ptype.Equals(typeof(Single)))
                {
                    err = (SQLiteError)Sqlite3.sqlite3_bind_double(pStmt, i, (Single)param.Value);
                }
                else if (ptype.Equals(typeof(UInt32)))
                {
                    err = (SQLiteError)Sqlite3.sqlite3_bind_int64(pStmt, i, (UInt32)param.Value);
                }
                else if (ptype.Equals(typeof(Int64)))
                {
                    err = (SQLiteError)Sqlite3.sqlite3_bind_int64(pStmt, i, (Int64)param.Value);
                }
                else if (ptype.Equals(typeof(Byte[])))
                {
                    err = (SQLiteError)Sqlite3.sqlite3_bind_blob(pStmt, i, (byte[])param.Value, ((byte[])param.Value).Length, null);
                }
                else if (ptype.Equals(typeof(Guid)))
                {
                    err = (SQLiteError)Sqlite3.sqlite3_bind_text(pStmt, i, param.Value.ToString(), param.Value.ToString().Length, null);
                }
                else
                {
                    throw new ApplicationException("Unkown Parameter Type");
                }
                if (err != SQLiteError.OK)
                {
                    throw new ApplicationException("Sqlite error in bind " + err);
                }
            }
        }