Exemple #1
0
        public int ExecuteNonQuery(object[] source)
        {
            CheckDisposed();
            Log(nameof(ExecuteNonQuery), source);
            OnExecutionStarted();

            var sw = Stopwatch.StartNew();

            try
            {
                var r = InternalExecute(source);

                if (r == Result.Done || r == Result.OK || r == Result.Row)
                {
                    int rowsAffected = SQLite3.Changes(Connection.Handle);
                    return(rowsAffected);
                }

                return(0);
            }
            finally
            {
                if (Statement != null)
                {
                    SQLite3.Reset(Statement);
                }

                sw.Stop();
                Log(sw);
                OnExecutionEnded();
            }
        }
Exemple #2
0
        public int ExecuteNonQuery(object[] source)
        {
            if (Connection.Trace)
            {
                Debug.WriteLine("Executing: " + CommandText);
            }

            var r = SQLite3.Result.OK;

            if (!Initialized)
            {
                Statement   = Prepare();
                Initialized = true;
            }

            //bind the values.
            if (source != null)
            {
                for (int i = 0; i < source.Length; i++)
                {
                    SQLiteCommand.BindParameter(Statement, i + 1, source[i], Connection.StoreDateTimeAsTicks);
                }
            }
            r = SQLite3.Step(Statement);

            if (r == SQLite3.Result.Done)
            {
                int rowsAffected = SQLite3.Changes(Connection.Handle);
                SQLite3.Reset(Statement);
                return(rowsAffected);
            }
            else if (r == SQLite3.Result.Error)
            {
                string msg = SQLite3.GetErrmsg(Connection.Handle);
                SQLite3.Reset(Statement);
                throw SQLiteException.New(r, msg);
            }
            else if (r == SQLite3.Result.Constraint && SQLite3.ExtendedErrCode(Connection.Handle) == SQLite3.ExtendedResult.ConstraintNotNull)
            {
                SQLite3.Reset(Statement);
                throw NotNullConstraintViolationException.New(r, SQLite3.GetErrmsg(Connection.Handle));
            }
            else
            {
                SQLite3.Reset(Statement);
                throw SQLiteException.New(r, r.ToString());
            }
        }
Exemple #3
0
        public int ExecuteNonQuery(object[] source)
        {
            if (Connection.Trace)
            {
                Connection.InvokeTrace("Executing: " + CommandText);
            }
            SQLite3.Result result = SQLite3.Result.OK;
            if (!Initialized)
            {
                Statement   = Prepare();
                Initialized = true;
            }
            if (source != null)
            {
                for (int i = 0; i < source.Length; i++)
                {
                    SQLiteCommand.BindParameter(Statement, i + 1, source[i], Connection.StoreDateTimeAsTicks);
                }
            }
            result = SQLite3.Step(Statement);
            switch (result)
            {
            case SQLite3.Result.Done:
            {
                int result2 = SQLite3.Changes(Connection.Handle);
                SQLite3.Reset(Statement);
                return(result2);
            }

            case SQLite3.Result.Error:
            {
                string errmsg = SQLite3.GetErrmsg(Connection.Handle);
                SQLite3.Reset(Statement);
                throw SQLiteException.New(result, errmsg);
            }

            case SQLite3.Result.Constraint:
                if (SQLite3.ExtendedErrCode(Connection.Handle) == SQLite3.ExtendedResult.ConstraintNotNull)
                {
                    SQLite3.Reset(Statement);
                    throw NotNullConstraintViolationException.New(result, SQLite3.GetErrmsg(Connection.Handle));
                }
                break;
            }
            SQLite3.Reset(Statement);
            throw SQLiteException.New(result, result.ToString());
        }
Exemple #4
0
        public Result Execute(object[] source)
        {
            CheckDisposed();
            Log(nameof(Execute), source);
            OnExecutionStarted();

            var sw = Stopwatch.StartNew();

            try
            {
                return(InternalExecute(source));
            }
            finally
            {
                if (Statement != null)
                {
                    SQLite3.Reset(Statement);
                }

                sw.Stop();
                Log(sw);
                OnExecutionEnded();
            }
        }
Exemple #5
0
        public T ExecuteScalar <T>(object[] source)
        {
            CheckDisposed();
            Log(nameof(ExecuteScalar), source);
            OnExecutionStarted();

            var sw  = Stopwatch.StartNew();
            T   val = default(T);

            try
            {
                var r = InternalExecute(source);

                if (r == Result.Row)
                {
                    var colType = SQLite3.ColumnType(Statement, 0);
                    val = ReadCol <T>(Statement, 0, colType);
                }
                else if (r == Result.Done || r == Result.OK)
                {
                }
            }
            finally
            {
                if (Statement != null)
                {
                    SQLite3.Reset(Statement);
                }

                sw.Stop();
                Log(sw);
                OnExecutionEnded();
            }

            return(val);
        }
Exemple #6
0
        public IEnumerable <T> ExecuteQuery <T>(TableMapping map, object[] source)
        {
            CheckDisposed();
            Log(nameof(ExecuteQuery), source);
            OnExecutionStarted();

            var sw = Stopwatch.StartNew();

            try
            {
                var r = Result.OK;

                if (!Prepared)
                {
                    Statement = Prepare();
                    Prepared  = true;
                }

                //bind the values.
                if (source != null)
                {
                    for (int i = 0; i < source.Length; i++)
                    {
                        BindParameter(Statement, i + 1, source[i], Connection.StoreDateTimeAsTicks);
                    }
                }

                var cols = new TableColumn[SQLite3.ColumnCount(Statement)];

                for (int i = 0; i < cols.Length; i++)
                {
                    var name = SQLite3.ColumnNameUTF8(Statement, i);
                    cols[i] = map.FindColumn(name);
                }

                while (SQLite3.Step(Statement) == Result.Row)
                {
                    var obj = map.CreateInstance();
                    for (int i = 0; i < cols.Length; i++)
                    {
                        if (cols[i] == null)
                        {
                            continue;
                        }
                        var colType = SQLite3.ColumnType(Statement, i);
                        var val     = ReadCol(Statement, i, colType, cols[i].ColumnType, cols[i].PropertyDefaultValue);
                        cols[i].SetValue(obj, val);
                    }
                    OnInstanceCreated(obj);
                    yield return((T)obj);
                }

                if (r == Result.Done || r == Result.OK)
                {
                }
                else
                {
                    var msg = SQLite3.GetErrorMessageUTF8(Connection.Handle);
                    var ex  = new SQLiteException(r, msg, sql: CommandText);
                    ex.PopulateColumnFromTableMapping(map);

                    throw ex;
                }
            }
            finally
            {
                if (Statement != null)
                {
                    SQLite3.Reset(Statement);
                }

                sw.Stop();
                Log(sw);
                OnExecutionEnded();
            }
        }