public void Reload (IDB db) { if (id <= 0) throw new ArgumentException ("There's no id to reload from."); using (IDbCommand cmd = db.CreateCommand ()) { cmd.CommandText = "SELECT * FROM " + Table + " WHERE id = " + id.ToString (); using (IDataReader reader = cmd.ExecuteReader ()) { if (!reader.Read ()) throw new Exception ("No records found"); Load (reader); if (reader.Read ()) throw new Exception ("More than one record found."); } } }
/// <summary> /// A default Delete implementation /// </summary> /// <param name="connection"></param> public static void DeleteInternal(IDB db, int id, string Table) { if (id <= 0) { throw new Exception(Table + " doesn't have an id."); } using (IDbCommand cmd = db.CreateCommand()) { cmd.CommandText = "DELETE FROM " + Table + " WHERE id = " + id.ToString(); cmd.ExecuteNonQuery(); } }
public void Reload(IDB db) { if (id <= 0) { throw new ArgumentException("There's no id to reload from."); } using (IDbCommand cmd = db.CreateCommand()) { cmd.CommandText = "SELECT * FROM " + Table + " WHERE id = " + id.ToString(); using (IDataReader reader = cmd.ExecuteReader()) { if (!reader.Read()) { throw new Exception("No records found"); } Load(reader); if (reader.Read()) { throw new Exception("More than one record found."); } } } }
/// <summary> /// A default Save implementation using reflection /// </summary> /// <param name="connection"></param> protected void SaveInternal(IDB db) { string sql; string [] fields = Fields; using (IDbCommand cmd = db.CreateCommand()) { if (id == 0) { sql = "INSERT INTO " + Table + "("; sql += string.Join(", ", fields); sql += ") VALUES ("; sql += "@" + string.Join(", @", fields); sql += ");\n"; sql += "SELECT currval(pg_get_serial_sequence('" + Table + "', 'id'));"; id = -1; // Don't know how to get the id from the db once the record has been saved, disable multiple save until then } else if (id > 0) { sql = "UPDATE " + Table + " SET "; for (int i = 0; i < fields.Length; i++) { sql += fields [i] + " = @" + fields [i]; if (i != fields.Length - 1) { sql += ", "; } } sql += " WHERE id = " + id.ToString(); } else //if (id == -1) { { throw new Exception("Can't save more than once unless you loaded the revision from db."); } CreateParameter(cmd, "id", id); foreach (string field in fields) { FieldInfo fieldinfo = GetField(GetType(), field); object value = fieldinfo.GetValue(this); if (value == null && fieldinfo.FieldType == typeof(string)) { value = string.Empty; } else if (value is DateTime && ((DateTime)value) == DatabaseNow) { sql = sql.Replace("@" + field, "now () AT TIME ZONE 'UTC'"); continue; } CreateParameter(cmd, field, value); } cmd.CommandText = sql; if (id == -1) { object o = cmd.ExecuteScalar(); id = (int)(long)o; } else { cmd.ExecuteNonQuery(); } } }
/// <summary> /// A default Delete implementation /// </summary> /// <param name="connection"></param> public static void DeleteInternal (IDB db, int id, string Table) { if (id <= 0) throw new Exception (Table + " doesn't have an id."); using (IDbCommand cmd = db.CreateCommand ()) { cmd.CommandText = "DELETE FROM " + Table + " WHERE id = " + id.ToString (); cmd.ExecuteNonQuery (); } }
/// <summary> /// A default Save implementation using reflection /// </summary> /// <param name="connection"></param> protected void SaveInternal (IDB db) { string sql; string [] fields = Fields; using (IDbCommand cmd = db.CreateCommand ()) { if (id == 0) { sql = "INSERT INTO " + Table + "("; sql += string.Join (", ", fields); sql += ") VALUES ("; sql += "@" + string.Join (", @", fields); sql += ");\n"; sql += "SELECT currval(pg_get_serial_sequence('" + Table + "', 'id'));"; id = -1; // Don't know how to get the id from the db once the record has been saved, disable multiple save until then } else if (id > 0) { sql = "UPDATE " + Table + " SET "; for (int i = 0; i < fields.Length; i++) { sql += fields [i] + " = @" + fields [i]; if (i != fields.Length - 1) sql += ", "; } sql += " WHERE id = " + id.ToString (); } else { //if (id == -1) { throw new Exception ("Can't save more than once unless you loaded the revision from db."); } CreateParameter (cmd, "id", id); foreach (string field in fields) { FieldInfo fieldinfo = GetField (field); object value = fieldinfo.GetValue (this); if (value == null && fieldinfo.FieldType == typeof (string)) { value = string.Empty; } else if (value is DateTime && ((DateTime) value) == DatabaseNow) { sql = sql.Replace ("@" + field, "now () AT TIME ZONE 'UTC'"); continue; } CreateParameter (cmd, field, value); } cmd.CommandText = sql; if (id == -1) { object o = cmd.ExecuteScalar (); id = (int) (long) o; } else { cmd.ExecuteNonQuery (); } } }