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()); }
public int Insert(object obj, string extra, Type objType) { if (obj == null || objType == null) { return(0); } TableMapping map = GetMapping(objType); if (map.PK != null && map.PK.IsAutoGuid) { PropertyInfo prop = objType.GetProperty(map.PK.PropertyName); if (prop != null) { if (prop.GetValue(obj, null).Equals(Guid.Empty)) { prop.SetValue(obj, Guid.NewGuid(), null); } } } bool replacing = string.Compare(extra, "OR REPLACE", StringComparison.OrdinalIgnoreCase) == 0; TableMapping.Column[] cols = replacing ? map.InsertOrReplaceColumns : map.InsertColumns; object[] vals = new object[cols.Length]; for (int i = 0; i < vals.Length; i++) { vals[i] = cols[i].GetValue(obj); } PreparedSqliteInsertCommand insertCmd = map.GetInsertCommand(this, extra); int count; try { count = insertCmd.ExecuteNonQuery(vals); } catch (SQLiteException ex) { if (SQLite3.ExtendedErrCode(this.Handle) == SQLite3.ExtendedResult.ConstraintNotNull) { throw NotNullConstraintViolationException.New(ex.Result, ex.Message, map, obj); } throw; } if (map.HasAutoIncPK) { long id = SQLite3.LastInsertRowid(this.Handle); map.SetAutoIncPK(obj, id); } return(count); }
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()); }
public int Update(object obj, Type objType) { int rowsAffected = 0; if (obj == null || objType == null) { return(0); } TableMapping map = GetMapping(objType); TableMapping.Column pk = map.PK; if (pk == null) { throw new NotSupportedException("Cannot update " + map.TableName + ": it has no PK"); } IEnumerable <TableMapping.Column> cols = from p in map.Columns where p != pk select p; IEnumerable <object> vals = from c in cols select c.GetValue(obj); List <object> ps = new List <object>(vals); ps.Add(pk.GetValue(obj)); string q = string.Format("update \"{0}\" set {1} where {2} = ? ", map.TableName, string.Join(",", (from c in cols select "\"" + c.Name + "\" = ? ").ToArray()), pk.Name); try { rowsAffected = Execute(q, ps.ToArray()); } catch (SQLiteException ex) { if (ex.Result == SQLite3.Result.Constraint && SQLite3.ExtendedErrCode(this.Handle) == SQLite3.ExtendedResult.ConstraintNotNull) { throw NotNullConstraintViolationException.New(ex, map, obj); } throw ex; } return(rowsAffected); }