示例#1
0
        public T ExecuteScalar <T>()
        {
            if (_conn.Trace)
            {
                Debug.WriteLine("Executing Query: " + this);
            }

            T val = default(T);

            IntPtr stmt = Prepare();

            try {
                SQLite3.Result r = SQLite3.Step(stmt);
                if (r == SQLite3.Result.Row)
                {
                    SQLite3.ColType 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);
        }
示例#2
0
        public int ExecuteNonQuery()
        {
            if (_conn.Trace)
            {
                Debug.WriteLine("Executing: " + this);
            }

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

            r = SQLite3.Step(stmt);
            Finalize(stmt);
            if (r == SQLite3.Result.Done)
            {
                int rowsAffected = SQLite3.Changes(_conn.Handle);
                return(rowsAffected);
            }

            if (r == SQLite3.Result.Error)
            {
                string msg = SQLite3.GetErrmsg(_conn.Handle);
                throw SQLiteException.New(r, msg);
            }

            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());
        }
示例#3
0
        public string SaveTransactionPoint()
        {
            int    depth  = Interlocked.Increment(ref TransactionDepth) - 1;
            string retVal = "S" + Rand.Next(short.MaxValue) + "D" + depth;

            try {
                Execute("savepoint " + retVal);
            } catch (Exception ex) {
                SQLiteException sqlExp = ex as SQLiteException;
                if (sqlExp != null)
                {
                    switch (sqlExp.Result)
                    {
                    case SQLite3.Result.IOError:
                    case SQLite3.Result.Full:
                    case SQLite3.Result.Busy:
                    case SQLite3.Result.NoMem:
                    case SQLite3.Result.Interrupt:
                        RollbackTo(null, true);
                        break;
                    }
                }
                else
                {
                    Interlocked.Decrement(ref TransactionDepth);
                }

                throw;
            }

            return(retVal);
        }
示例#4
0
        public void BeginTransaction()
        {
            if (Interlocked.CompareExchange(ref TransactionDepth, 1, 0) == 0)
            {
                try {
                    Execute("begin transaction");
                } catch (Exception ex) {
                    SQLiteException sqlExp = ex as SQLiteException;
                    if (sqlExp != null)
                    {
                        switch (sqlExp.Result)
                        {
                        case SQLite3.Result.IOError:
                        case SQLite3.Result.Full:
                        case SQLite3.Result.Busy:
                        case SQLite3.Result.NoMem:
                        case SQLite3.Result.Interrupt:
                            RollbackTo(null, true);
                            break;
                        }
                    }
                    else
                    {
                        Interlocked.Decrement(ref TransactionDepth);
                    }

                    throw;
                }
            }
            else
            {
                throw new InvalidOperationException("Cannot begin a transaction while already in a transaction.");
            }
        }
示例#5
0
 public void EnableLoadExtension(int onoff)
 {
     SQLite3.Result r = SQLite3.EnableLoadExtension(this.Handle, onoff);
     if (r != SQLite3.Result.OK)
     {
         string msg = SQLite3.GetErrmsg(this.Handle);
         throw SQLiteException.New(r, msg);
     }
 }
        public int ExecuteNonQuery(object[] source)
        {
            if (this.Connection.Trace)
            {
                Debug.WriteLine("Executing: " + this.CommandText);
            }

            SQLite3.Result r = SQLite3.Result.OK;

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

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

            if (r == SQLite3.Result.Done)
            {
                int rowsAffected = SQLite3.Changes(this.Connection.Handle);
                SQLite3.Reset(this.Statement);
                return(rowsAffected);
            }

            if (r == SQLite3.Result.Error)
            {
                string msg = SQLite3.GetErrmsg(this.Connection.Handle);
                SQLite3.Reset(this.Statement);
                throw SQLiteException.New(r, msg);
            }

            if (r == SQLite3.Result.Constraint &&
                SQLite3.ExtendedErrCode(this.Connection.Handle) == SQLite3.ExtendedResult.ConstraintNotNull)
            {
                SQLite3.Reset(this.Statement);
                throw NotNullConstraintViolationException.New(r, SQLite3.GetErrmsg(this.Connection.Handle));
            }

            SQLite3.Reset(this.Statement);
            throw SQLiteException.New(r, r.ToString());
        }
示例#7
0
        public SQLiteCommand CreateCommand(string cmdText, params object[] ps)
        {
            if (!Opened)
            {
                throw SQLiteException.New(SQLite3.Result.Error, "Cannot create commands from unopened database");
            }

            SQLiteCommand cmd = NewCommand();

            cmd.CommandText = cmdText;
            foreach (object o in ps)
            {
                cmd.Bind(o);
            }
            return(cmd);
        }
示例#8
0
        public SQLiteConnection(string databasePath, string password, SQLiteOpenFlags openFlags,
                                bool storeDateTimeAsTicks = false)
        {
            if (string.IsNullOrEmpty(databasePath))
            {
                throw new ArgumentException("Must be specified", "databasePath");
            }

            DatabasePath = databasePath;

            Sqlite3DatabaseHandle handle;

            byte[]         databasePathAsBytes = GetNullTerminatedUtf8(this.DatabasePath);
            SQLite3.Result r = SQLite3.Open(databasePathAsBytes, out handle, (int)openFlags, IntPtr.Zero);

            Handle = handle;
            if (r != SQLite3.Result.OK)
            {
                throw SQLiteException.New(r,
                                          string.Format("Could not open database file: {0} ({1})", this.DatabasePath, r));
            }

            if (!string.IsNullOrEmpty(password))
            {
                SQLite3.Result result = SQLite3.Key(handle, password, password.Length);
                if (result != SQLite3.Result.OK)
                {
                    throw SQLiteException.New(r,
                                              string.Format("Could not open database file: {0} ({1})", this.DatabasePath, r));
                }
            }

            Opened = true;

            StoreDateTimeAsTicks = storeDateTimeAsTicks;
            BusyTimeout          = TimeSpan.FromSeconds(0.1);
        }
示例#9
0
 public void Close()
 {
     if (Opened && this.Handle != NullHandle)
     {
         try {
             if (Mappings != null)
             {
                 foreach (TableMapping sqlInsertCommand in Mappings.Values)
                 {
                     sqlInsertCommand.Dispose();
                 }
             }
             SQLite3.Result r = SQLite3.Close(this.Handle);
             if (r != SQLite3.Result.OK)
             {
                 string msg = SQLite3.GetErrmsg(this.Handle);
                 throw SQLiteException.New(r, msg);
             }
         } finally {
             this.Handle = NullHandle;
             Opened      = false;
         }
     }
 }
示例#10
0
 private void Dispose(bool disposing)
 {
     if (Opened && NullHandle != Handle)
     {
         try {
             if (null != Mappings)
             {
                 foreach (var item in Mappings.Values)
                 {
                     item.Dispose();
                 }
             }
             SQLite3.Result r = SQLite3.Close(Handle);
             if (r != SQLite3.Result.OK)
             {
                 string msg = SQLite3.GetErrmsg(Handle);
                 throw SQLiteException.New(r, msg);
             }
         } finally {
             Handle = NullHandle;
             Opened = false;
         }
     }
 }
示例#11
0
 public static NotNullConstraintViolationException New(SQLiteException exception, TableMapping mapping,
                                                       object obj)
 {
     return(new NotNullConstraintViolationException(exception.Result, exception.Message, mapping, obj));
 }