Example #1
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());
        }
Example #2
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());
        }
Example #3
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());
            }
        }
Example #4
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());
        }
Example #5
0
        public int Insert(object obj, string extra, Type objType)
        {
            if (obj == null || objType == null)
            {
                return(0);
            }
            TableMapping mapping = GetMapping(objType);

            if (mapping.PK != null && mapping.PK.IsAutoGuid)
            {
                PropertyInfo property = objType.GetProperty(mapping.PK.PropertyName);
                if (property != null && property.GetGetMethod().Invoke(obj, null).Equals(Guid.Empty))
                {
                    property.SetValue(obj, Guid.NewGuid(), null);
                }
            }
            TableMapping.Column[] array = (string.Compare(extra, "OR REPLACE", StringComparison.OrdinalIgnoreCase) != 0) ? mapping.InsertColumns : mapping.InsertOrReplaceColumns;
            object[] array2             = new object[array.Length];
            for (int i = 0; i < array2.Length; i++)
            {
                array2[i] = array[i].GetValue(obj);
            }
            PreparedSqlLiteInsertCommand insertCommand = mapping.GetInsertCommand(this, extra);
            int result;

            try
            {
                result = insertCommand.ExecuteNonQuery(array2);
            }
            catch (SQLiteException ex)
            {
                if (SQLite3.ExtendedErrCode(Handle) == SQLite3.ExtendedResult.ConstraintNotNull)
                {
                    throw NotNullConstraintViolationException.New(ex.Result, ex.Message, mapping, obj);
                }
                throw;
                IL_0120 :;
            }
            if (mapping.HasAutoIncPK)
            {
                long id = SQLite3.LastInsertRowid(Handle);
                mapping.SetAutoIncPK(obj, id);
            }
            return(result);
        }
Example #6
0
        public int Update(object obj, Type objType)
        {
            int num = 0;

            if (obj == null || objType == null)
            {
                return(0);
            }
            TableMapping mapping = GetMapping(objType);

            TableMapping.Column pk = mapping.PK;
            if (pk != null)
            {
                IEnumerable <TableMapping.Column> source = from p in mapping.Columns
                                                           where p != pk
                                                           select p;
                IEnumerable <object> collection = from c in source
                                                  select c.GetValue(obj);

                List <object> list = new List <object>(collection);
                list.Add(pk.GetValue(obj));
                string query = string.Format("update \"{0}\" set {1} where {2} = ? ", mapping.TableName, string.Join(",", (from c in source
                                                                                                                           select "\"" + c.Name + "\" = ? ").ToArray()), pk.Name);
                try
                {
                    return(Execute(query, list.ToArray()));
                }
                catch (SQLiteException ex)
                {
                    if (ex.Result == SQLite3.Result.Constraint && SQLite3.ExtendedErrCode(Handle) == SQLite3.ExtendedResult.ConstraintNotNull)
                    {
                        throw NotNullConstraintViolationException.New(ex, mapping, obj);
                    }
                    throw ex;
IL_014e:
                    return(num);
                }
            }
            throw new NotSupportedException("Cannot update " + mapping.TableName + ": it has no PK");
        }