public T Create(T model) { MySqlParameter[] parameters = GetStoreValues(model); if (parameters.Length == 0) { throw new Exception("The array cannot be empty"); } string columns = parameters[0].ParameterName, values = String.Concat("@", parameters[0].ParameterName); for (int i = 1; i < parameters.Length; i++) { columns = string.Concat(columns, ", ", parameters[i].ParameterName); values = string.Concat(values, ", @", parameters[i].ParameterName); } string query = String.Format("INSERT INTO {0} ({1}) VALUES ({2})", TableName, columns, values); var result = MyDbConnection.Query(query, parameters) == 1; var id = Convert.ToInt32(MyDbConnection.ResponseQuery("SELECT LAST_INSERT_ID() as id").Rows[0][0]); if (SetId(model, id)) { return(model); } else { throw new Exception("Invalid primary key"); } }
public T FirstOrDefault(int id) { DataRow row = MyDbConnection.SelectById(TableName, id); if (row == null) { return(EmptyModel()); } return(CreateModel(row)); }
public T FirstOrFail(int id) { DataRow row = MyDbConnection.SelectById(TableName, id); if (row == null) { throw new Exception("Not found"); } return(CreateModel(row)); }
public T Update(T model) { MySqlParameter[] parameters = GetUpdateValues(model); if (parameters.Length == 0) { throw new Exception("The array cannot be empty"); } string sintax = String.Format("{0} = @{0}", parameters[0].ParameterName); for (int i = 1; i < parameters.Length; i++) { sintax = String.Format("{0}, {1} = @{1}", sintax, parameters[i].ParameterName); } string query = String.Format("UPDATE {0} SET {1} WHERE id={2}", TableName, sintax, model.Id); MyDbConnection.Query(query, parameters); return(model); }
public int Delete(T model) { return(MyDbConnection.Delete(TableName, model.Id)); }
public IEnumerable <T> ExecuteSelectQuery(string query, DbParameter[] parameters) { var result = MyDbConnection.ResponseQuery(query, parameters); return(CreateModelCollection(result.Rows)); }