Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.50 KB | None | 0 0
  1. <?php
  2.  
  3. class ICDataException extends Exception{}
  4.  
  5. class ICData{
  6. private $connection = 'db';
  7. private $db_host = 'localhost';
  8. private $db_user = 'user200324_moroz';
  9. private $db_password = '4C6o9F8u';
  10. private $db_name = 'moroz';
  11. private $_sql_table = null;
  12. private $_sql_fields = null;
  13. private $_sql_joins = [];
  14. private $_sql_conditions = null;
  15. private $_sql_prefix = null;
  16. private $_sql_bind = [];
  17. private $_sql_callback = null;
  18. private $_sql_limit = null;
  19. public $src;
  20.  
  21. public function __construct($connection){
  22. if($connection == '') throw new ICDataException('Connection is undefined. Hint: ICData(<connection type>).');
  23. switch($connection){
  24. case 'db':
  25. try{
  26. $this->src = new PDO('mysql:host=' . $this->db_host . ';dbname=' . $this->db_name, $this->db_user, $this->db_password);
  27. }catch(PDOException $e){
  28. throw new ICDataException($e->getMessage());
  29. }
  30. break;
  31. default:
  32. throw new ICDataException('Wrong connection type.');
  33. }
  34. }
  35.  
  36. public function delete($table, array $ids){
  37. if(empty($ids)) throw new ICDataException('Empty parameters at delete.');
  38. $bind = $conds = [];
  39. foreach($ids as $field=>$value){
  40. $conds[] = $field . "=:" . $field;
  41. $bind[':'.$field] = $value;
  42. }
  43. $sql = "DELETE FROM " . $table . " WHERE " . implode(' AND ', $conds);
  44. $que = $this->src->prepare($sql);
  45. return $que->execute($bind);
  46. }
  47. public function insertRow($table, array $parameters, array $fields = null, $debug = false){
  48. if(empty($parameters)) throw new ICDataException('Empty parameters at insert.');
  49. $bind = $flds = $vals = $upd = [];
  50. foreach($parameters as $field=>$value)
  51. if(is_null($fields) || array_key_exists($field, $fields)){
  52. $bind[':'.$field.''] = $value;
  53. $flds[] = '`'.$field.'`';
  54. $vals[] = ':'. $field;
  55. $upd[] = '`'.$field . '`=VALUES(`' . $field . '`)';
  56. }
  57. $sql = "INSERT INTO " . $table . "(" . implode(',', $flds) . ") VALUES (" . implode(',', $vals) . ") ON DUPLICATE KEY UPDATE " . implode(',', $upd);
  58. if($debug){
  59. foreach($bind as $key=>$val)
  60. $sql = str_replace($key, "'" . $val . "'", $sql);
  61. echo $sql;
  62. return;
  63. }
  64. $que = $this->src->prepare($sql);
  65. return $que->execute($bind);
  66. }
  67. public function insertRows($table, array $parameters)
  68. {
  69. $fields = $columns = $bind = $values = [];
  70. $i = 0;
  71. foreach($parameters as $parameter){
  72. $value = [];
  73. foreach($parameter as $field => $val){
  74. if($i == 0){
  75. $columns[] = '`' . $field . '`';
  76. $fields[] = '`' . $field . '`=VALUES(`' . $field . '`)';
  77. }
  78. $bind[':'.$i.$field] = $val;
  79. $value[] = ':' . $i . $field;
  80. }
  81. $values[] = '(' . implode(', ', $value) . ')';
  82. ++$i;
  83. }
  84. $sql = "INSERT INTO " . $table . " (" . implode(', ', $columns) . ") VALUES " . implode(', ', $values) . " ON DUPLICATE KEY UPDATE " . implode(', ', $fields);
  85. $que = $this->src->prepare($sql);
  86. return $que->execute($bind);
  87. }
  88. public function newSql($table, $prefix, $columns, $conditions, $limit = null)
  89. {
  90. $this->_sql_prefix = $prefix;
  91. $this->_sql_table = $table;
  92. $this->_sql_fields = $columns;
  93. $this->_sql_conditions = $conditions;
  94. $this->_sql_limit = $limit;
  95. }
  96. public function join($table, $prefix, $columns, $condition){
  97. $this->_sql_joins = array_merge($this->_sql_joins, [['table' => $table, 'prefix' => $prefix, 'columns' => $columns,'condition' => $condition]]);
  98. }
  99. public function resetSql()
  100. {
  101. $this->_sql_prefix = $this->_sql_table = $this->_sql_fields = $this->_sql_conditions = $this->_sql_limit = null;
  102. $this->_sql_joins = $this->_sql_bind = [];
  103. }
  104. public function sqlCallback($callback)
  105. {
  106. $this->_sql_callback = $callback;
  107. }
  108. public function query()
  109. {
  110. if(is_array($this->_sql_fields)){
  111. $cols = [];
  112. foreach($this->_sql_fields as $field)
  113. $cols[] = '`' . $this->_sql_table . '`.`' . $field . '` AS ' . $this->_sql_prefix . $field;
  114. $cols = implode(', ', $cols);
  115. }else{
  116. $cols = '`' . $this->_sql_table . '`.*';
  117. }
  118.  
  119. $__join = '';
  120. if(!empty($this->_sql_joins)){
  121. $_jcols = $_join = [];
  122. foreach($this->_sql_joins as $join){
  123. if(is_array($join['columns'])){
  124. $jcols = [];
  125. foreach($join['columns'] as $field)
  126. $jcols[] = '`' . $join['table'] . '`.`' . $field . '` AS ' . $join['prefix'] . $field;
  127. $jcols = implode(', ', $jcols);
  128. }else{
  129. $jcols = '`' . $join['table'] . '`.*';
  130. }
  131. $_jcols[] = $jcols;
  132. $_join[] = " LEFT JOIN `" . $join['table'] . "` ON (" . $join['condition'] . ")";
  133. }
  134. $cols .= ', ' . implode(', ', $_jcols);
  135. $__join = implode(' ', $_join);
  136. }
  137.  
  138. if(count($this->_sql_conditions) > 0){
  139. foreach($this->_sql_conditions as $field=>$val){
  140. if($field[0] == '#'){ // If hardlogic found
  141. $fieldz = explode('#', $field);
  142. switch($fieldz[1]){
  143. case 'inlist':
  144. $_condition[] = '`' . $this->_sql_table . '`.`' . $fieldz[2].'` IN (' . implode(',', $val) . ')';
  145. break;
  146. }
  147. }else{
  148. $_condition[]= '`' . $this->_sql_table . '`.`' . $field . "`=:" . $field;
  149. $this->_sql_bind[':'.$field] = $val;
  150. }
  151. }
  152. $_condition = " WHERE " . implode(' AND ', $_condition);
  153. }else{
  154. $_condition = '';
  155. }
  156.  
  157. $sql = "SELECT " . $cols . " FROM `" . $this->_sql_table . "`" . $__join . $_condition . ($this->_sql_limit !== NULL ? " LIMIT " . $this->_sql_limit : "");
  158.  
  159. $que = $this->src->prepare($sql);
  160. $que->execute($this->_sql_bind);
  161. if($que->rowCount() > 0)
  162. if(!is_null($this->_sql_callback) && is_callable($this->_sql_callback)){
  163. $ret = [];
  164. while($item = $que->fetch(PDO::FETCH_ASSOC))
  165. $ret[] = call_user_func($this->_sql_callback, $item);
  166. return $ret;
  167. }else{
  168. return $que->fetchAll(PDO::FETCH_ASSOC);
  169. }
  170. else
  171. return false;
  172. }
  173. public function addZ($table, array $parameters, array $fields){
  174. if(empty($parameters)) throw new ICDataException('Empty parameters on add.');
  175. $bind = $pars = $vals = [];
  176. foreach($parameters as $field=>$value)
  177. if(array_key_exists($field, $fields)){
  178. $pars[] = '`' . $field . '`';
  179. $vals[] = ':' . $field;
  180. $bind[':'.$field] = $value;
  181. }
  182. $sql = "INSERT INTO `" . $table . "` (" . implode(', ', $pars) . ") VALUES (" . implode(', ', $vals) . ")";
  183. $que = $this->src->prepare($sql);
  184. return $que->execute($bind) ? $this->src->lastInsertId() : false;
  185. }
  186. public function update($table, array $conds, array $parameters, array $fields = null){
  187. if(empty($conds) || empty($parameters)) throw new ICDataException('Empty parameters on update.');
  188. $q = $cond = $bind = [];
  189. foreach($parameters as $field=>$value)
  190. if(is_null($fields) || in_array($field, $fields)){
  191. $q[] = $field . "=:" . $field;
  192. $bind[':'.$field] = $value;
  193. }
  194. foreach($conds as $field=>$value)
  195. if(is_null($fields) || in_array($field, $fields)){
  196. $cond[] = $field . "=:" . $field;
  197. $bind[':'.$field] = $value;
  198. }
  199. if(empty($bind)) throw new ICDataException('Wront parameters on update.');
  200. $sql = "UPDATE " . $table . " SET " . implode(", ", $q) . " WHERE " . implode(" AND ", $cond);
  201. $que = $this->src->prepare($sql);
  202. return $que->execute($bind);
  203. }
  204. public function getArray($table, array $select, array $conds, $callback = false, array $fields = null, array $sort = null){
  205. $cond = $bind = $srt = [];
  206. $xtraCond = '';
  207. if(!empty($conds))
  208. foreach($conds as $field=>$value)
  209. if(is_null($fields) || array_key_exists($field, $fields)){
  210. if($field[0] == '#'){ // If hardlogic found
  211. $fieldz = explode('#', $field);
  212. switch($fieldz[1]){
  213. case 'inlist':
  214. $xtraCond = ' AND `'.$fieldz[2].'` IN (' . implode(',', $value) . ')';
  215. break;
  216. case 'notinlist':
  217. $xtraCond = ' AND `'.$fieldz[2].'` NOT IN (' . implode(',', $value) . ')';
  218. break;
  219. }
  220. }else{
  221. $cond[] = $field . "=:" . $field;
  222. $bind[':'.$field] = $value;
  223. }
  224. }
  225. if($sort != null && !empty($sort))
  226. foreach($sort as $f=>$d)
  227. $srt[] = $f . " " . $d;
  228. $sortStr = empty($srt) ? '' : " ORDER BY " . implode(',', $srt);
  229. $sql = "SELECT " . (empty($select) ? '*' : implode(',', $select)) . " FROM " . $table . " WHERE " . (empty($cond) ? '1=1' : implode(" AND ", $cond)) . $xtraCond . $sortStr;
  230. $que = $this->src->prepare($sql);
  231. $que->execute($bind);
  232. if($que->rowCount() > 0)
  233. if(is_callable($callback)){
  234. $ret = [];
  235. while($item = $que->fetch(PDO::FETCH_ASSOC))
  236. $ret[] = $callback($item);
  237. return $ret;
  238. }else{
  239. return $que->fetchAll(PDO::FETCH_ASSOC);
  240. }
  241. else
  242. return false;
  243. }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement