public ModelCancelEventArgs(Model model, bool cancel) : this(model) { Cancel = cancel; }
public ModelEventArgs(Model model) { Model = model; }
public ModelCancelEventArgs(Model model) : base(model) { }
public virtual void CreateTable(Model model) { ExecuteNonQuery(ToSqlCreateStatement(model), null); }
public virtual bool Delete(Model model) { QParam pkParam = new QParam(model.ModelPKName, model.PK, 1); string deleteSql = string.Format("DELETE FROM {0} WHERE {1}={2}", model.ModelDbName, model.ModelPKName, pkParam.Id); ExecuteNonQuery(deleteSql, new QParam[] { pkParam }); return true; }
protected string ToFieldValuesList(Model model) { return string.Join(",", model.Fields.Select(field => ToSqlFormat(field))); }
protected abstract string ToSqlCreateStatement(Model model);
public virtual bool Update(Model model) { List<QParam> qParams = QParamsFromModel(model); string update_set_expr = ""; QParam pkParam = null; for (int i = 0; i < qParams.Count; i++) { if (qParams[i].FieldName == model.ModelPKName){ pkParam = qParams[i]; continue; } if (update_set_expr != "") update_set_expr += ","; update_set_expr += qParams[i].FieldName + "=" + qParams[i].Id; } string updateSql = string.Format("UPDATE {0} SET {1} WHERE {2}={3}", model.ModelDbName, update_set_expr, model.ModelPKName, pkParam.Id); ExecuteNonQuery(updateSql, qParams); return true; }
protected List<QParam> QParamsFromModel(Model model) { List<QParam> qparams = new List<QParam>(); List<Field> fields = model.Fields; for (int i = 0; i < fields.Count; i++) { qparams.Add(new QParam(fields[i].DbName, fields[i].Value, i + 1)); } return qparams; }
protected virtual bool OnSaving(Model model) { bool cancel = false; ModelCancelEventHandler ceSaving = ModelSaving; if (ceSaving != null) { ModelCancelEventArgs mcea = new ModelCancelEventArgs(model, cancel); ceSaving(this, mcea); cancel = mcea.Cancel; } return cancel; }
public virtual int Insert(Model model) { List<QParam> qParams = QParamsFromModel(model); string paramsListAsString = ""; for (int i = 0; i < qParams.Count; i++) { if (i > 0) paramsListAsString += ","; paramsListAsString += qParams[i].Id; } string insertSql = string.Format("INSERT INTO {0} ({1}) VALUES ({2})", model.ModelDbName, ToFieldNamesList(model, removeAlias:true), paramsListAsString); return Convert.ToInt32(ExecuteNonQuery(insertSql, qParams, true)); }
protected virtual void OnSaved(Model model) { ModelEventHandler eSaved = ModelSaved; if (eSaved != null) eSaved(this, new ModelEventArgs(model)); }
protected virtual void OnDeleted(Model model) { ModelEventHandler eDeleted = ModelDeleted; if (eDeleted != null) eDeleted(this, new ModelEventArgs(model)); }
public bool Save(Model model) { int insertedId = -1; bool updated = false; if (model.ModelType == ModelInfo.ModelType) if (!OnSaving(model)) { if (model.Status == ModelStatus.New) { insertedId = DbEngine.Insert(model); if (insertedId != -1) model[model.ModelPKName] = insertedId; } else { updated = DbEngine.Update(model); } if (insertedId != -1 || updated) { model.Status = ModelStatus.Loaded; OnSaved(model); } } return insertedId != -1 || updated; }
public bool Delete(Model model) { bool result = false; if (model.ModelType == ModelInfo.ModelType) if (!OnDeleting(model)) if ((result = DbEngine.Delete(model))) { model.Status = ModelStatus.Deleted; OnDeleted(model); } return result; }
public ModelInfo(Model model) { _model = model; Fields = new List<FieldInfo>(); foreach (Field field in _model.Fields) { if (field == _model.PKField) PKField = new FieldInfo(field); Fields.Add(new FieldInfo(field)); } }