Exemplo n.º 1
0
 public void Close()
 {
     if (_open && Handle != NullHandle)
     {
         try
         {
             if (_mappings != null)
             {
                 foreach (TableMapping value in _mappings.Values)
                 {
                     value.Dispose();
                 }
             }
             SQLite3.Result result = SQLite3.Close(Handle);
             if (result != 0)
             {
                 string errmsg = SQLite3.GetErrmsg(Handle);
                 throw SQLiteException.New(result, errmsg);
             }
         }
         finally
         {
             Handle = NullHandle;
             _open  = false;
         }
     }
 }
Exemplo n.º 2
0
        public int BackupDataBase(string backupPath)
        {
            Sqlite3DatabaseHandle backupHandle;

            if (string.IsNullOrEmpty(backupPath))
            {
                throw new ArgumentException("Must be specified", "databasePath");
            }
            var backupPathAsBytes = GetNullTerminatedUtf8(backupPath);
            //A
            var r = SQLite3.Open(backupPathAsBytes, out backupHandle,
                                 (int)(SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create), IntPtr.Zero);

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

            //Открыли БД, пора приступить к бэкапу

            Sqlite3Backup pBackup;

            //1
            pBackup = BackupInit(backupHandle, "main", Handle, "main");

            if (pBackup != IntPtr.Zero)
            {
                while (true)
                {
                    //2
                    r = BackupStep(pBackup, 25);

                    if (BackupProgress != null)
                    {
                        BackupProgress(BackupRemaining(pBackup), BackupPagecount(pBackup));
                    }

                    if (r == SQLite3.Result.OK || r == SQLite3.Result.Busy || r == SQLite3.Result.Locked)
                    {
                        Thread.Sleep(100);
                    }
                    else
                    {
                        break;
                    }
                }

                //3
                BackupFinish(pBackup);
            }
            //B
            r = SQLite3.Close(backupHandle);
            if (r != SQLite3.Result.OK)
            {
                string msg = SQLite3.GetErrmsg(backupHandle);
                throw SQLiteException.New(r, msg);
            }

            return((int)r);
        }
Exemplo n.º 3
0
        public int ExecuteNonQuery()
        {
            if (_conn.Trace)
            {
                _conn.InvokeTrace("Executing: " + this);
            }
            SQLite3.Result result = SQLite3.Result.OK;
            lock (_conn.SyncObject)
            {
                IntPtr stmt = Prepare();
                result = SQLite3.Step(stmt);
                Finalize(stmt);
            }
            switch (result)
            {
            case SQLite3.Result.Done:
                return(SQLite3.Changes(_conn.Handle));

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

            case SQLite3.Result.Constraint:
                if (SQLite3.ExtendedErrCode(_conn.Handle) == SQLite3.ExtendedResult.ConstraintNotNull)
                {
                    throw NotNullConstraintViolationException.New(result, SQLite3.GetErrmsg(_conn.Handle));
                }
                break;
            }
            throw SQLiteException.New(result, result.ToString());
        }
Exemplo n.º 4
0
        public int ExecuteNonQuery()
        {
            var r = SQLite3.Result.OK;

            for (int i = 0; i < _conn.MaxExecuteAttempts; i++)
            {
                var stmt = Prepare();
                r = SQLite3.Step(stmt);
                SQLite3.Finalize(stmt);
                if (r == SQLite3.Result.Error)
                {
                    string msg = SQLite3.GetErrmsg(_conn.Handle);
                    throw SQLiteException.New(r, msg);
                }
                else if (r == SQLite3.Result.Done)
                {
                    int rowsAffected = SQLite3.Changes(_conn.Handle);
                    return(rowsAffected);
                }
                else if (r == SQLite3.Result.Busy)
                {
                    // We will retry
                    System.Threading.Thread.Sleep(1000);
                }
                else
                {
                    throw SQLiteException.New(r, r.ToString());
                }
            }
            throw SQLiteException.New(r, r.ToString());
        }
Exemplo n.º 5
0
        public T ExecuteScalar <T>()
        {
            if (_conn.Trace)
            {
                _conn.InvokeTrace("Executing Query: " + this);
            }
            T result = default(T);

            lock (_conn.SyncObject)
            {
                IntPtr stmt = Prepare();
                try
                {
                    SQLite3.Result result2 = SQLite3.Step(stmt);
                    switch (result2)
                    {
                    case SQLite3.Result.Row:
                    {
                        SQLite3.ColType type = SQLite3.ColumnType(stmt, 0);
                        return((T)ReadCol(stmt, 0, type, typeof(T)));
                    }

                    default:
                        throw SQLiteException.New(result2, SQLite3.GetErrmsg(_conn.Handle));

                    case SQLite3.Result.Done:
                        return(result);
                    }
                }
                finally
                {
                    Finalize(stmt);
                }
            }
        }
Exemplo n.º 6
0
        public int ExecuteNonQuery()
        {
            if (_conn.Trace)
            {
                Debug.WriteLine("Executing: " + this);
            }

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

            r = SQLite3.Step(stmt);
            Finalize(stmt);
            if (r == SQLite3.Result.Done)
            {
                int rowsAffected = SQLite3.Changes(_conn.Handle);
                return(rowsAffected);
            }
            else if (r == SQLite3.Result.Error)
            {
                string msg = SQLite3.GetErrmsg(_conn.Handle);
                throw SQLiteException.New(r, msg);
            }
            else if (r == SQLite3.Result.Constraint)
            {
                if (SQLite3.ExtendedErrCode(_conn.Handle) == SQLite3.ExtendedResult.ConstraintNotNull)
                {
                    throw NotNullConstraintViolationException.New(r, SQLite3.GetErrmsg(_conn.Handle));
                }
            }

            throw SQLiteException.New(r, r.ToString());
        }
Exemplo n.º 7
0
        public T ExecuteScalar <T>()
        {
            if (_conn.Trace)
            {
                Debug.WriteLine("Executing Query: " + this);
            }

            T val = default(T);

            var stmt = Prepare();

            try {
                var r = SQLite3.Step(stmt);
                if (r == SQLite3.Result.Row)
                {
                    var colType = SQLite3.ColumnType(stmt, 0);
                    val = (T)ReadCol(stmt, 0, colType, typeof(T));
                }
                else if (r == SQLite3.Result.Done)
                {
                }
                else
                {
                    throw SQLiteException.New(r, SQLite3.GetErrmsg(_conn.Handle));
                }
            } finally {
                Finalize(stmt);
            }

            return(val);
        }
Exemplo n.º 8
0
 public void EnableLoadExtension(int onoff)
 {
     SQLite3.Result result = SQLite3.EnableLoadExtension(Handle, onoff);
     if (result != 0)
     {
         string errmsg = SQLite3.GetErrmsg(Handle);
         throw SQLiteException.New(result, errmsg);
     }
 }
Exemplo n.º 9
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());
            }
        }
Exemplo n.º 10
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());
        }