Beispiel #1
0
        public virtual int Step(Func <SQLiteStatement, int, bool> func, Func <SQLiteError, SQLiteOnErrorAction> errorHandler)
        {
            if (func == null)
            {
                throw new ArgumentNullException(nameof(func));
            }

            int index  = 0;
            var handle = CheckDisposed();

            do
            {
                SQLiteErrorCode code = SQLiteDatabase._sqlite3_step(handle);
                if (code == SQLiteErrorCode.SQLITE_DONE)
                {
                    index++;
                    Database.Log(TraceLevel.Verbose, "Step done at index " + index);
                    break;
                }

                if (code == SQLiteErrorCode.SQLITE_ROW)
                {
                    bool cont = func(this, index);
                    if (!cont)
                    {
                        Database.Log(TraceLevel.Verbose, "Step break at index " + index);
                        break;
                    }

                    index++;
                    continue;
                }

                if (errorHandler != null)
                {
                    var error  = new SQLiteError(this, index, code);
                    var action = errorHandler(error);
                    index = error.Index;
                    code  = error.Code;
                    if (action == SQLiteOnErrorAction.Break) // don't increment index
                    {
                        break;
                    }

                    if (action == SQLiteOnErrorAction.Continue)
                    {
                        index++;
                        continue;
                    }

                    // else throw
                }

                Database.CheckError(code);
            }while (true);
            return(index);
        }
Beispiel #2
0
        public SQLiteStatement(SQLiteDatabase database, string sql, Func <SQLiteError, SQLiteOnErrorAction> prepareErrorHandler)
        {
            if (database == null)
            {
                throw new ArgumentNullException(nameof(database));
            }

            if (sql == null)
            {
                throw new ArgumentNullException(nameof(sql));
            }

            Database = database;
            Sql      = sql;
            database.Log(TraceLevel.Verbose, "Preparing statement `" + sql + "`", nameof(SQLiteStatement) + ".ctor");

            if (prepareErrorHandler != null)
            {
                var code = SQLiteDatabase._sqlite3_prepare16_v2(database.CheckDisposed(), sql, sql.Length * 2, out _handle, IntPtr.Zero);
                if (code != SQLiteErrorCode.SQLITE_OK)
                {
                    var error  = new SQLiteError(this, -1, code);
                    var action = prepareErrorHandler(error);
                    if (action == SQLiteOnErrorAction.Break || action == SQLiteOnErrorAction.Continue)
                    {
                        return;
                    }

                    database.CheckError(code, sql, true);
                }
            }
            else
            {
                database.CheckError(SQLiteDatabase._sqlite3_prepare16_v2(database.CheckDisposed(), sql, sql.Length * 2, out _handle, IntPtr.Zero), sql, true);
            }
        }