Beispiel #1
0
 internal override void SetUpdateHook(SQLiteUpdateCallback func)
 {
     UnsafeNativeMethods.sqlite3_update_hook(_sql, func, IntPtr.Zero);
 }
Beispiel #2
0
 internal override void ReturnText(IntPtr context, string value)
 {
     byte[] b = ToUTF8(value);
     UnsafeNativeMethods.sqlite3_result_text(context, ToUTF8(value), b.Length - 1, (IntPtr)(-1));
 }
Beispiel #3
0
 internal override IntPtr AggregateContext(IntPtr context)
 {
     return(UnsafeNativeMethods.sqlite3_aggregate_context(context, 1));
 }
Beispiel #4
0
 internal override void ReturnInt32(IntPtr context, int value)
 {
     UnsafeNativeMethods.sqlite3_result_int(context, value);
 }
Beispiel #5
0
 internal override void ReturnNull(IntPtr context)
 {
     UnsafeNativeMethods.sqlite3_result_null(context);
 }
Beispiel #6
0
 internal override int Bind_ParamCount(SqliteStatement stmt)
 {
     return(UnsafeNativeMethods.sqlite3_bind_parameter_count(stmt._sqlite_stmt));
 }
Beispiel #7
0
 internal override void ReturnError(IntPtr context, string value)
 {
     UnsafeNativeMethods.sqlite3_result_error(context, ToUTF8(value), value.Length);
 }
Beispiel #8
0
 internal override int GetParamValueInt32(IntPtr ptr)
 {
     return(UnsafeNativeMethods.sqlite3_value_int(ptr));
 }
Beispiel #9
0
 internal override TypeAffinity GetParamValueType(IntPtr ptr)
 {
     return(UnsafeNativeMethods.sqlite3_value_type(ptr));
 }
Beispiel #10
0
 internal override int GetInt32(SqliteStatement stmt, int index)
 {
     return(UnsafeNativeMethods.sqlite3_column_int(stmt._sqlite_stmt, index));
 }
Beispiel #11
0
 internal override void Cancel()
 {
     UnsafeNativeMethods.sqlite3_interrupt(_sql);
 }
Beispiel #12
0
 internal override TypeAffinity ColumnAffinity(SqliteStatement stmt, int index)
 {
     return(UnsafeNativeMethods.sqlite3_column_type(stmt._sqlite_stmt, index));
 }
Beispiel #13
0
 internal override int ColumnCount(SqliteStatement stmt)
 {
     return(UnsafeNativeMethods.sqlite3_column_count(stmt._sqlite_stmt));
 }
Beispiel #14
0
 internal override int Bind_ParamIndex(SqliteStatement stmt, string paramName)
 {
     return(UnsafeNativeMethods.sqlite3_bind_parameter_index(stmt._sqlite_stmt, ToUTF8(paramName)));
 }
Beispiel #15
0
 internal override void SetCommitHook(SQLiteCommitCallback func)
 {
     UnsafeNativeMethods.sqlite3_commit_hook(_sql, func, IntPtr.Zero);
 }
Beispiel #16
0
 internal override void ReturnBlob(IntPtr context, byte[] value)
 {
     UnsafeNativeMethods.sqlite3_result_blob(context, value, value.Length, (IntPtr)(-1));
 }
Beispiel #17
0
 internal override void SetRollbackHook(SQLiteRollbackCallback func)
 {
     UnsafeNativeMethods.sqlite3_rollback_hook(_sql, func, IntPtr.Zero);
 }
Beispiel #18
0
        internal override SqliteStatement Prepare(SqliteConnection cnn, string strSql, SqliteStatement previous, uint timeoutMS, out string strRemain)
        {
            IntPtr stmt    = IntPtr.Zero;
            IntPtr ptr     = IntPtr.Zero;
            int    len     = 0;
            int    n       = 17;
            int    retries = 0;

            byte[]          b         = ToUTF8(strSql);
            string          typedefs  = null;
            SqliteStatement cmd       = null;
            Random          rnd       = null;
            uint            starttick = (uint)Environment.TickCount;

            GCHandle handle = GCHandle.Alloc(b, GCHandleType.Pinned);
            IntPtr   psql   = handle.AddrOfPinnedObject();

            try
            {
                while ((n == 17 || n == 6 || n == 5) && retries < 3)
                {
#if !SQLITE_STANDARD
                    n = UnsafeNativeMethods.sqlite3_prepare_interop(_sql, psql, b.Length - 1, out stmt, out ptr, out len);
#else
                    n   = UnsafeNativeMethods.sqlite3_prepare(_sql, psql, b.Length - 1, out stmt, out ptr);
                    len = -1;
#endif

                    if (n == 17)
                    {
                        retries++;
                    }
                    else if (n == 1)
                    {
                        if (String.Compare(SQLiteLastError(), "near \"TYPES\": syntax error", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            int pos = strSql.IndexOf(';');
                            if (pos == -1)
                            {
                                pos = strSql.Length - 1;
                            }

                            typedefs = strSql.Substring(0, pos + 1);
                            strSql   = strSql.Substring(pos + 1);

                            strRemain = "";

                            while (cmd == null && strSql.Length > 0)
                            {
                                cmd    = Prepare(cnn, strSql, previous, timeoutMS, out strRemain);
                                strSql = strRemain;
                            }

                            if (cmd != null)
                            {
                                cmd.SetTypes(typedefs);
                            }

                            return(cmd);
                        }
#if !PLATFORM_COMPACTFRAMEWORK
                        else if (_buildingSchema == false && String.Compare(SQLiteLastError(), 0, "no such table: TEMP.SCHEMA", 0, 26, StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            strRemain       = "";
                            _buildingSchema = true;
                            try
                            {
                                ISQLiteSchemaExtensions ext = ((IServiceProvider)SqliteFactory.Instance).GetService(typeof(ISQLiteSchemaExtensions)) as ISQLiteSchemaExtensions;

                                if (ext != null)
                                {
                                    ext.BuildTempSchema(cnn);
                                }

                                while (cmd == null && strSql.Length > 0)
                                {
                                    cmd    = Prepare(cnn, strSql, previous, timeoutMS, out strRemain);
                                    strSql = strRemain;
                                }

                                return(cmd);
                            }
                            finally
                            {
                                _buildingSchema = false;
                            }
                        }
#endif
                    }
                    else if (n == 6 || n == 5) // Locked -- delay a small amount before retrying
                    {
                        // Keep trying
                        if (rnd == null) // First time we've encountered the lock
                        {
                            rnd = new Random();
                        }

                        // If we've exceeded the command's timeout, give up and throw an error
                        if ((uint)Environment.TickCount - starttick > timeoutMS)
                        {
                            throw new SqliteException(n, SQLiteLastError());
                        }
                        else
                        {
                            // Otherwise sleep for a random amount of time up to 150ms
                            System.Threading.Thread.CurrentThread.Join(rnd.Next(1, 150));
                        }
                    }
                }

                if (n > 0)
                {
                    throw new SqliteException(n, SQLiteLastError());
                }

                strRemain = UTF8ToString(ptr, len);

                if (stmt != IntPtr.Zero)
                {
                    cmd = new SqliteStatement(this, stmt, strSql.Substring(0, strSql.Length - strRemain.Length), previous);
                }

                return(cmd);
            }
            finally
            {
                handle.Free();
            }
        }