Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class ICDataException extends Exception{}
- class ICData{
- private $connection = 'db';
- private $db_host = 'localhost';
- private $db_user = 'user200324_moroz';
- private $db_password = '4C6o9F8u';
- private $db_name = 'moroz';
- private $_sql_table = null;
- private $_sql_fields = null;
- private $_sql_joins = [];
- private $_sql_conditions = null;
- private $_sql_prefix = null;
- private $_sql_bind = [];
- private $_sql_callback = null;
- private $_sql_limit = null;
- public $src;
- public function __construct($connection){
- if($connection == '') throw new ICDataException('Connection is undefined. Hint: ICData(<connection type>).');
- switch($connection){
- case 'db':
- try{
- $this->src = new PDO('mysql:host=' . $this->db_host . ';dbname=' . $this->db_name, $this->db_user, $this->db_password);
- }catch(PDOException $e){
- throw new ICDataException($e->getMessage());
- }
- break;
- default:
- throw new ICDataException('Wrong connection type.');
- }
- }
- public function delete($table, array $ids){
- if(empty($ids)) throw new ICDataException('Empty parameters at delete.');
- $bind = $conds = [];
- foreach($ids as $field=>$value){
- $conds[] = $field . "=:" . $field;
- $bind[':'.$field] = $value;
- }
- $sql = "DELETE FROM " . $table . " WHERE " . implode(' AND ', $conds);
- $que = $this->src->prepare($sql);
- return $que->execute($bind);
- }
- public function insertRow($table, array $parameters, array $fields = null, $debug = false){
- if(empty($parameters)) throw new ICDataException('Empty parameters at insert.');
- $bind = $flds = $vals = $upd = [];
- foreach($parameters as $field=>$value)
- if(is_null($fields) || array_key_exists($field, $fields)){
- $bind[':'.$field.''] = $value;
- $flds[] = '`'.$field.'`';
- $vals[] = ':'. $field;
- $upd[] = '`'.$field . '`=VALUES(`' . $field . '`)';
- }
- $sql = "INSERT INTO " . $table . "(" . implode(',', $flds) . ") VALUES (" . implode(',', $vals) . ") ON DUPLICATE KEY UPDATE " . implode(',', $upd);
- if($debug){
- foreach($bind as $key=>$val)
- $sql = str_replace($key, "'" . $val . "'", $sql);
- echo $sql;
- return;
- }
- $que = $this->src->prepare($sql);
- return $que->execute($bind);
- }
- public function insertRows($table, array $parameters)
- {
- $fields = $columns = $bind = $values = [];
- $i = 0;
- foreach($parameters as $parameter){
- $value = [];
- foreach($parameter as $field => $val){
- if($i == 0){
- $columns[] = '`' . $field . '`';
- $fields[] = '`' . $field . '`=VALUES(`' . $field . '`)';
- }
- $bind[':'.$i.$field] = $val;
- $value[] = ':' . $i . $field;
- }
- $values[] = '(' . implode(', ', $value) . ')';
- ++$i;
- }
- $sql = "INSERT INTO " . $table . " (" . implode(', ', $columns) . ") VALUES " . implode(', ', $values) . " ON DUPLICATE KEY UPDATE " . implode(', ', $fields);
- $que = $this->src->prepare($sql);
- return $que->execute($bind);
- }
- public function newSql($table, $prefix, $columns, $conditions, $limit = null)
- {
- $this->_sql_prefix = $prefix;
- $this->_sql_table = $table;
- $this->_sql_fields = $columns;
- $this->_sql_conditions = $conditions;
- $this->_sql_limit = $limit;
- }
- public function join($table, $prefix, $columns, $condition){
- $this->_sql_joins = array_merge($this->_sql_joins, [['table' => $table, 'prefix' => $prefix, 'columns' => $columns,'condition' => $condition]]);
- }
- public function resetSql()
- {
- $this->_sql_prefix = $this->_sql_table = $this->_sql_fields = $this->_sql_conditions = $this->_sql_limit = null;
- $this->_sql_joins = $this->_sql_bind = [];
- }
- public function sqlCallback($callback)
- {
- $this->_sql_callback = $callback;
- }
- public function query()
- {
- if(is_array($this->_sql_fields)){
- $cols = [];
- foreach($this->_sql_fields as $field)
- $cols[] = '`' . $this->_sql_table . '`.`' . $field . '` AS ' . $this->_sql_prefix . $field;
- $cols = implode(', ', $cols);
- }else{
- $cols = '`' . $this->_sql_table . '`.*';
- }
- $__join = '';
- if(!empty($this->_sql_joins)){
- $_jcols = $_join = [];
- foreach($this->_sql_joins as $join){
- if(is_array($join['columns'])){
- $jcols = [];
- foreach($join['columns'] as $field)
- $jcols[] = '`' . $join['table'] . '`.`' . $field . '` AS ' . $join['prefix'] . $field;
- $jcols = implode(', ', $jcols);
- }else{
- $jcols = '`' . $join['table'] . '`.*';
- }
- $_jcols[] = $jcols;
- $_join[] = " LEFT JOIN `" . $join['table'] . "` ON (" . $join['condition'] . ")";
- }
- $cols .= ', ' . implode(', ', $_jcols);
- $__join = implode(' ', $_join);
- }
- if(count($this->_sql_conditions) > 0){
- foreach($this->_sql_conditions as $field=>$val){
- if($field[0] == '#'){ // If hardlogic found
- $fieldz = explode('#', $field);
- switch($fieldz[1]){
- case 'inlist':
- $_condition[] = '`' . $this->_sql_table . '`.`' . $fieldz[2].'` IN (' . implode(',', $val) . ')';
- break;
- }
- }else{
- $_condition[]= '`' . $this->_sql_table . '`.`' . $field . "`=:" . $field;
- $this->_sql_bind[':'.$field] = $val;
- }
- }
- $_condition = " WHERE " . implode(' AND ', $_condition);
- }else{
- $_condition = '';
- }
- $sql = "SELECT " . $cols . " FROM `" . $this->_sql_table . "`" . $__join . $_condition . ($this->_sql_limit !== NULL ? " LIMIT " . $this->_sql_limit : "");
- $que = $this->src->prepare($sql);
- $que->execute($this->_sql_bind);
- if($que->rowCount() > 0)
- if(!is_null($this->_sql_callback) && is_callable($this->_sql_callback)){
- $ret = [];
- while($item = $que->fetch(PDO::FETCH_ASSOC))
- $ret[] = call_user_func($this->_sql_callback, $item);
- return $ret;
- }else{
- return $que->fetchAll(PDO::FETCH_ASSOC);
- }
- else
- return false;
- }
- public function addZ($table, array $parameters, array $fields){
- if(empty($parameters)) throw new ICDataException('Empty parameters on add.');
- $bind = $pars = $vals = [];
- foreach($parameters as $field=>$value)
- if(array_key_exists($field, $fields)){
- $pars[] = '`' . $field . '`';
- $vals[] = ':' . $field;
- $bind[':'.$field] = $value;
- }
- $sql = "INSERT INTO `" . $table . "` (" . implode(', ', $pars) . ") VALUES (" . implode(', ', $vals) . ")";
- $que = $this->src->prepare($sql);
- return $que->execute($bind) ? $this->src->lastInsertId() : false;
- }
- public function update($table, array $conds, array $parameters, array $fields = null){
- if(empty($conds) || empty($parameters)) throw new ICDataException('Empty parameters on update.');
- $q = $cond = $bind = [];
- foreach($parameters as $field=>$value)
- if(is_null($fields) || in_array($field, $fields)){
- $q[] = $field . "=:" . $field;
- $bind[':'.$field] = $value;
- }
- foreach($conds as $field=>$value)
- if(is_null($fields) || in_array($field, $fields)){
- $cond[] = $field . "=:" . $field;
- $bind[':'.$field] = $value;
- }
- if(empty($bind)) throw new ICDataException('Wront parameters on update.');
- $sql = "UPDATE " . $table . " SET " . implode(", ", $q) . " WHERE " . implode(" AND ", $cond);
- $que = $this->src->prepare($sql);
- return $que->execute($bind);
- }
- public function getArray($table, array $select, array $conds, $callback = false, array $fields = null, array $sort = null){
- $cond = $bind = $srt = [];
- $xtraCond = '';
- if(!empty($conds))
- foreach($conds as $field=>$value)
- if(is_null($fields) || array_key_exists($field, $fields)){
- if($field[0] == '#'){ // If hardlogic found
- $fieldz = explode('#', $field);
- switch($fieldz[1]){
- case 'inlist':
- $xtraCond = ' AND `'.$fieldz[2].'` IN (' . implode(',', $value) . ')';
- break;
- case 'notinlist':
- $xtraCond = ' AND `'.$fieldz[2].'` NOT IN (' . implode(',', $value) . ')';
- break;
- }
- }else{
- $cond[] = $field . "=:" . $field;
- $bind[':'.$field] = $value;
- }
- }
- if($sort != null && !empty($sort))
- foreach($sort as $f=>$d)
- $srt[] = $f . " " . $d;
- $sortStr = empty($srt) ? '' : " ORDER BY " . implode(',', $srt);
- $sql = "SELECT " . (empty($select) ? '*' : implode(',', $select)) . " FROM " . $table . " WHERE " . (empty($cond) ? '1=1' : implode(" AND ", $cond)) . $xtraCond . $sortStr;
- $que = $this->src->prepare($sql);
- $que->execute($bind);
- if($que->rowCount() > 0)
- if(is_callable($callback)){
- $ret = [];
- while($item = $que->fetch(PDO::FETCH_ASSOC))
- $ret[] = $callback($item);
- return $ret;
- }else{
- return $que->fetchAll(PDO::FETCH_ASSOC);
- }
- else
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement