示例#1
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());
        }
示例#3
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);
        }
示例#4
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);
        }
示例#5
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;
         }
     }
 }
示例#6
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;
         }
     }
 }