public static void Delete(MySQLDataStoreBase store, MySqlConnection cnn, ModelBase instance, IDataStoreKey key, object[] extra) { var record = GeneratorUtils.AsSuitableRecordInstance(instance, true); using (var cmd = cnn.CreateCommand()) { var pk = key ?? record.DataStoreKey; if (pk == null) throw new MySQLDataAccessException(StringConsts.KEY_UNAVAILABLE_ERROR); var where = GeneratorUtils.KeyToWhere(pk, cmd.Parameters); if (!string.IsNullOrEmpty(where)) cmd.CommandText = string.Format("DELETE T1 FROM `{0}` T1 WHERE {1}", record.TableName, where); else cmd.CommandText = string.Format("DELETE T1 FROM `{0}` T1", record.TableName); var affected = 0; try { affected = cmd.ExecuteNonQuery(); GeneratorUtils.LogCommand(store.LogLevel, "rmdelete-ok", cmd, null); } catch(Exception error) { GeneratorUtils.LogCommand(store.LogLevel, "rmdelete-error", cmd, error); throw; } if (affected == 0) throw new MySQLDataAccessException(string.Format(StringConsts.NO_ROWS_AFFECTED_ERROR, "Delete")); }//using command }
/// <summary> /// Auto generates select sql and params. If sqlStatement!=null then params are added to that statement /// </summary> public static void Load(MySQLDataStoreBase store, MySqlConnection cnn, string sqlStatement, ModelBase instance, IDataStoreKey key, object[] extra) { var autoSql = string.IsNullOrEmpty(sqlStatement); var record = GeneratorUtils.AsSuitableRecordInstance(instance, autoSql); var select = new StringBuilder(); if (autoSql) { foreach (var fld in record.Fields) if (fld.StoreFlag == StoreFlag.LoadAndStore || fld.StoreFlag == StoreFlag.OnlyLoad) select.AppendFormat(" T1.`{0}`,", fld.FieldName); if (select.Length > 0) select.Remove(select.Length - 1, 1);// remove "," else throw new MySQLDataAccessException(StringConsts.LOAD_NO_SELECT_COLUMNS_ERROR); } var pk = key ?? record.DataStoreKey; if (pk == null) throw new MySQLDataAccessException(StringConsts.KEY_UNAVAILABLE_ERROR); using (var cmd = cnn.CreateCommand()) { var where = GeneratorUtils.KeyToWhere(pk, cmd.Parameters); if (autoSql) cmd.CommandText = string.Format("SELECT {0} FROM `{1}` T1 WHERE {2}", select, record.TableName, where); else cmd.CommandText = string.Format(sqlStatement, where); MySqlDataReader reader = null; try { reader = cmd.ExecuteReader(); GeneratorUtils.LogCommand(store.LogLevel, "rmload-ok", cmd, null); } catch(Exception error) { GeneratorUtils.LogCommand(store.LogLevel, "rmload-error", cmd, error); throw; } using (reader) { if (reader.Read()) reader.CopyFieldsToRecordFields(record); else throw new MySQLDataAccessException(string.Format(StringConsts.LOADING_ENTITY_NOT_FOUND_ERROR, pk)); }//using reader }//using command }
public static Record AsSuitableRecordInstance(ModelBase model, bool autoSql) { var record = model as Record; if (record == null) throw new MySQLDataAccessException(string.Format(StringConsts.MODEL_TYPE_NOT_RECORD_ERROR, model.GetType().Name)); if (autoSql) { if (string.IsNullOrEmpty(record.TableName)) throw new MySQLDataAccessException(string.Format(StringConsts.RECORD_TABLE_NAME_ERROR, model.GetType().Name)); } return record; }
/// <summary> /// Performs model save into MySQL server by generating appropriate SQL statement automatically /// Override to do custom saving for particular model instance /// </summary> protected internal virtual void DoSave(MySqlConnection cnn, ModelBase instance, IDataStoreKey key, object[] extra) { RecordModelGenerator.Save(this, cnn, instance, key, extra); }
public void Save(ModelBase instance, IDataStoreKey key, params object[] extra) { using (var cnn = GetConnection()) DoSave(cnn, instance, key, extra); }
private void removeOwnedItem(ModelBase anItem) { if (m_OwnedItems == null) return; if (m_OwnedItems.Contains(anItem)) { BeforeOwnedItemRemoval(anItem); m_OwnedItems.Remove(anItem); try { AfterOwnedItemRemoval(anItem); } catch {} } }
protected virtual void _CheckForDuplicateOwnedItemRegistration(ModelBase anItem) { if (m_OwnedItems.Contains(anItem, ReferenceEqualityComparer<ModelBase>.Instance)) throw new RecordModelException(StringConsts.ITEM_ALREADY_EXISTS_ERROR); }
private void addOwnedItem(ModelBase anItem) { //20130228 Dkh Lazy create if (m_OwnedItems==null) m_OwnedItems = new List<ModelBase>(32);//average field count per record - experimentaly this uses less ram while allocating 1000000+ records //check for dbl-registration this._CheckForDuplicateOwnedItemRegistration(anItem); //we expect exceptions (when applicable) that may "spoil" addition of owned items // i.e. can't add item with duplicate logical name etc.. this is implemented in derived classes BeforeOwnedItemAddition(anItem); m_OwnedItems.Add(anItem); try { AfterOwnedItemAddition(anItem); } catch {} }
/// <summary> /// Called after an item was removed from owning context. Exceptions are handled /// </summary> protected virtual void AfterOwnedItemRemoval(ModelBase removedItem) { //base implementation does nothing //does not require unneeded implementation in derived classes as it will be blank most of the time }
/// <summary> /// Called before owned item is added to owner collection. Override this method /// to perform checks before item actually added, such as: ensure unique item identification among siblings, etc. /// Throw exception to prohibit addition /// </summary> protected virtual void BeforeOwnedItemAddition(ModelBase addingItem) { //base implementation does nothing //does not require unneeded implementation in derived classes as it will be blank most of the time }
public static void Save(MySQLDataStoreBase store, MySqlConnection cnn, ModelBase instance, IDataStoreKey key, object[] extra) { var record = GeneratorUtils.AsSuitableRecordInstance(instance, true); if (record.LastPostedChange != ChangeType.Created && record.LastPostedChange != ChangeType.Edited) throw new MySQLDataAccessException(string.Format(StringConsts.MODEL_INVALID_STATE_ERROR, "Created || Edited", instance.LastPostedChange)); bool insert = instance.LastPostedChange == ChangeType.Created; var cnames = new StringBuilder(); var values = new StringBuilder(); var vparams = new List<MySqlParameter>(); var vpidx = 0; foreach (var fld in record.Fields) if (fld.StoreFlag == StoreFlag.LoadAndStore || fld.StoreFlag == StoreFlag.OnlyStore) if ( insert || fld.Modified ) { cnames.AppendFormat(" `{0}`,", fld.FieldName); if (fld.HasValue) { var pname = string.Format("?VAL{0}", vpidx); if (insert) values.AppendFormat(" {0},", pname); else values.AppendFormat(" `{0}` = {1},", fld.FieldName, pname); var par = new MySqlParameter(); par.ParameterName = pname; par.Value = fld.ValueAsObject; vparams.Add(par); vpidx++; } else { if (insert) values.Append(" NULL,"); else values.AppendFormat(" `{0}` = NULL,", fld.FieldName); } } if (cnames.Length > 0) { cnames.Remove(cnames.Length - 1, 1);// remove "," if (values.Length > 0) values.Remove(values.Length - 1, 1);// remove "," } else return;//nothing has been modified using (var cmd = cnn.CreateCommand()) { var sql = string.Empty; if (insert) //INSERT { sql = string.Format("INSERT INTO `{0}` ({1}) VALUES ({2})", record.TableName, cnames, values); } else //UPDATE { var pk = key ?? record.DataStoreKey; if (pk == null) throw new MySQLDataAccessException(StringConsts.KEY_UNAVAILABLE_ERROR); var where = GeneratorUtils.KeyToWhere(pk, cmd.Parameters); if (!string.IsNullOrEmpty(where)) sql = string.Format("UPDATE `{0}` T1 SET {1} WHERE {2}", record.TableName, values, where); else sql = string.Format("UPDATE `{0}` T1 SET {1}", record.TableName, values); } cmd.CommandText = sql; cmd.Parameters.AddRange(vparams.ToArray()); var affected = 0; try { affected = cmd.ExecuteNonQuery(); GeneratorUtils.LogCommand(store.LogLevel, "rmsave-ok", cmd, null); } catch(Exception error) { GeneratorUtils.LogCommand(store.LogLevel, "rmsave-error", cmd, error); throw; } if (affected == 0) throw new MySQLDataAccessException(string.Format(StringConsts.NO_ROWS_AFFECTED_ERROR, instance.LastPostedChange == ChangeType.Created ? "Insert" : "Update")); }//using command }
public RequiredValidationException(string message, ModelBase source) : base(message, source) { }
public ModelValidationException(string message, ModelBase erroredModel) : base(message) { ErroredModel = erroredModel; }
public LookupValidationException(string message, ModelBase source) : base(message, source) { }
public MinMaxValidationException(string message, ModelBase source) : base(message, source) { }