Exemplo n.º 1
0
        public int ExecuteNonQuery()
        {
            _conn.TraceListener.WriteLine("Executing: {0}", this);

            var          r    = Result.OK;
            IDbStatement stmt = Prepare();

            r = _sqlitePlatform.SQLiteApi.Step(stmt);
            Finalize(stmt);
            if (r == Result.Done)
            {
                int rowsAffected = _sqlitePlatform.SQLiteApi.Changes(_conn.Handle);
                return(rowsAffected);
            }
            if (r == Result.Error)
            {
                string msg = _sqlitePlatform.SQLiteApi.Errmsg16(_conn.Handle);
                throw SQLiteException.New(r, msg);
            }
            else if (r == Result.Constraint)
            {
                if (_sqlitePlatform.SQLiteApi.ExtendedErrCode(_conn.Handle) == ExtendedResult.ConstraintNotNull)
                {
                    throw NotNullConstraintViolationException.New(r, _sqlitePlatform.SQLiteApi.Errmsg16(_conn.Handle));
                }
            }
            throw SQLiteException.New(r, r.ToString());
        }
Exemplo n.º 2
0
        public T ExecuteScalar <T>()
        {
            _conn.TraceListener.WriteLine("Executing Query: {0}", this);

            var val = default(T);

            var stmt = Prepare();

            try
            {
                var r = _sqlitePlatform.SQLiteApi.Step(stmt);
                if (r == Result.Row)
                {
                    var colType = _sqlitePlatform.SQLiteApi.ColumnType(stmt, 0);
                    var clrType = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);
                    if (colType != ColType.Null)
                    {
                        val = (T)ReadCol(stmt, 0, colType, clrType);
                    }
                }
                else if (r == Result.Done)
                {
                }
                else
                {
                    throw SQLiteException.New(r, _sqlitePlatform.SQLiteApi.Errmsg16(_conn.Handle));
                }
            }
            finally
            {
                Finalize(stmt);
            }

            return(val);
        }
Exemplo n.º 3
0
        public void EnableLoadExtension(int onoff)
        {
            var r = Platform.SQLiteApi.EnableLoadExtension(Handle, onoff);

            if (r != Result.OK)
            {
                var msg = Platform.SQLiteApi.Errmsg16(Handle);
                throw SQLiteException.New(r, msg);
            }
        }
Exemplo n.º 4
0
        public int ExecuteNonQuery(object[] source)
        {
            Connection.TraceListener.WriteLine("Executing: {0}", CommandText);
            if (!Initialized)
            {
                Statement   = Prepare();
                Initialized = true;
            }

            var sqlitePlatform = Connection.Platform;

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

            Result r;

            lock (_locker)
            {
                r = sqlitePlatform.SQLiteApi.Step(Statement);
            }

            if (r == Result.Done)
            {
                var rowsAffected = sqlitePlatform.SQLiteApi.Changes(Connection.Handle);
                sqlitePlatform.SQLiteApi.Reset(Statement);
                return(rowsAffected);
            }
            if (r == Result.Error)
            {
                var msg = sqlitePlatform.SQLiteApi.Errmsg16(Connection.Handle);
                sqlitePlatform.SQLiteApi.Reset(Statement);
                throw SQLiteException.New(r, msg);
            }
            if (r == Result.Constraint && sqlitePlatform.SQLiteApi.ExtendedErrCode(Connection.Handle) == ExtendedResult.ConstraintNotNull)
            {
                sqlitePlatform.SQLiteApi.Reset(Statement);
                throw NotNullConstraintViolationException.New(r, sqlitePlatform.SQLiteApi.Errmsg16(Connection.Handle));
            }
            sqlitePlatform.SQLiteApi.Reset(Statement);

            throw SQLiteException.New(r, r.ToString());
        }
Exemplo n.º 5
0
 public void Close()
 {
     if (_open && Handle != NullHandle)
     {
         try
         {
             var r = Platform.SQLiteApi.Close(Handle);
             if (r != Result.OK)
             {
                 var msg = Platform.SQLiteApi.Errmsg16(Handle);
                 throw SQLiteException.New(r, msg);
             }
         }
         finally
         {
             Handle = NullHandle;
             _open  = false;
         }
     }
 }
        public int ExecuteNonQuery(object[] source)
        {
            if (Connection.Trace)
            {
                Debug.WriteLine("Executing: " + CommandText);
            }

            var r = Result.OK;

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

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

            if (r == Result.Done)
            {
                int rowsAffected = _sqlitePlatform.SQLiteApi.Changes(Connection.Handle);
                _sqlitePlatform.SQLiteApi.Reset(Statement);
                return(rowsAffected);
            }
            if (r == Result.Error)
            {
                string msg = _sqlitePlatform.SQLiteApi.Errmsg16(Connection.Handle);
                _sqlitePlatform.SQLiteApi.Reset(Statement);
                throw SQLiteException.New(r, msg);
            }
            _sqlitePlatform.SQLiteApi.Reset(Statement);
            throw SQLiteException.New(r, r.ToString());
        }
Exemplo n.º 7
0
        /// <summary>
        ///     Creates a new SQLiteCommand given the command text with arguments. Place a '?'
        ///     in the command text for each of the arguments.
        /// </summary>
        /// <param name="cmdText">
        ///     The fully escaped SQL.
        /// </param>
        /// <param name="args">
        ///     Arguments to substitute for the occurences of '?' in the command text.
        /// </param>
        /// <returns>
        ///     A <see cref="SQLiteCommand" />
        /// </returns>

        public SQLiteCommand CreateCommand(string cmdText, params object[] args)
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            if (!_open)
            {
                throw SQLiteException.New(Result.Error, "Cannot create commands from unopened database");
            }

            var cmd = NewCommand();

            cmd.CommandText = cmdText;
            foreach (var o in args)
            {
                cmd.Bind(o);
            }
            return(cmd);
        }
Exemplo n.º 8
0
        public int ExecuteNonQuery()
        {
            if (_conn.Trace)
            {
                Debug.WriteLine("Executing: " + this);
            }

            var          r    = Result.OK;
            IDbStatement stmt = Prepare();

            r = _sqlitePlatform.SQLiteApi.Step(stmt);
            Finalize(stmt);
            if (r == Result.Done)
            {
                int rowsAffected = _sqlitePlatform.SQLiteApi.Changes(_conn.Handle);
                return(rowsAffected);
            }
            if (r == Result.Error)
            {
                string msg = _sqlitePlatform.SQLiteApi.Errmsg16(_conn.Handle);
                throw SQLiteException.New(r, msg);
            }
            throw SQLiteException.New(r, r.ToString());
        }
Exemplo n.º 9
0
        public string CreateDatabaseBackup(ISQLitePlatform platform)
        {
            if (platform == null)
            {
                throw new ArgumentNullException(nameof(platform));
            }

            var sqliteApi = platform.SQLiteApi as ISQLiteApiExt;

            if (sqliteApi == null)
            {
                return(null);
            }

            var destDBPath = DatabasePath + "." + DateTime.UtcNow.ToString("yyyy-MM-dd_HH-mm-ss-fff");

            var databasePathAsBytes = GetNullTerminatedUtf8(destDBPath);
            var r = sqliteApi.Open(databasePathAsBytes, out IDbHandle destDB,
                                   (int)(SQLiteOpenFlags.Create | SQLiteOpenFlags.ReadWrite), IntPtr.Zero);

            if (r != Result.OK)
            {
                throw SQLiteException.New(r, String.Format("Could not open backup database file: {0} ({1})", destDBPath, r));
            }

            /* Open the backup object used to accomplish the transfer */
            var bHandle = sqliteApi.BackupInit(destDB, "main", Handle, "main");

            if (bHandle == null)
            {
                // Close the database connection
                sqliteApi.Close(destDB);

                throw SQLiteException.New(r, String.Format("Could not initiate backup process: {0}", destDBPath));
            }

            /* Each iteration of this loop copies 5 database pages from database
            ** pDb to the backup database. If the return value of backup_step()
            ** indicates that there are still further pages to copy, sleep for
            ** 250 ms before repeating. */
            do
            {
                r = sqliteApi.BackupStep(bHandle, 5);

                if (r == Result.OK || r == Result.Busy || r == Result.Locked)
                {
                    sqliteApi.Sleep(250);
                }
            } while (r == Result.OK || r == Result.Busy || r == Result.Locked);

            /* Release resources allocated by backup_init(). */
            r = sqliteApi.BackupFinish(bHandle);

            if (r != Result.OK)
            {
                // Close the database connection
                sqliteApi.Close(destDB);

                throw SQLiteException.New(r, String.Format("Could not finish backup process: {0} ({1})", destDBPath, r));
            }

            // Close the database connection
            sqliteApi.Close(destDB);

            return(destDBPath);
        }
 internal static NotNullConstraintViolationException New(SQLiteException exception, TableMapping mapping, object obj)
 {
     return(new NotNullConstraintViolationException(exception.Result, exception.Message, mapping, obj));
 }
 public static NotNullConstraintViolationException New(SQLiteException exception, TableMapping mapping, object obj)
 {
     return new NotNullConstraintViolationException(exception.Result, exception.Message, mapping, obj);
 }