Exemplo n.º 1
0
        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");
            }
        }
Exemplo n.º 2
0
        public T FirstOrDefault(int id)
        {
            DataRow row = MyDbConnection.SelectById(TableName, id);

            if (row == null)
            {
                return(EmptyModel());
            }
            return(CreateModel(row));
        }
Exemplo n.º 3
0
        public T FirstOrFail(int id)
        {
            DataRow row = MyDbConnection.SelectById(TableName, id);

            if (row == null)
            {
                throw new Exception("Not found");
            }
            return(CreateModel(row));
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
 public int Delete(T model)
 {
     return(MyDbConnection.Delete(TableName, model.Id));
 }
Exemplo n.º 6
0
        public IEnumerable <T> ExecuteSelectQuery(string query, DbParameter[] parameters)
        {
            var result = MyDbConnection.ResponseQuery(query, parameters);

            return(CreateModelCollection(result.Rows));
        }