Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.39 KB | None | 0 0
  1. <?php
  2.     class DAL_Query
  3.     {
  4.         private $_INFOS = array(
  5.             'server' => '',
  6.             'login' => '',
  7.             'pass' => '',
  8.             'database' => '',
  9.             'handle' => '');
  10.  
  11.         private $_QUERY = array(
  12.             'string' => '',
  13.             'field' => array(),
  14.             'table' => array(),
  15.             'search' => array(),
  16.             'value' => array(),
  17.             'filter' => array(),
  18.             'order' => array(),
  19.             'limit1' => 0,
  20.             'limit2' => 0);
  21.  
  22.         private $_RESULT = array(
  23.             'result' => array(),
  24.             'count' => 0);
  25.  
  26.         public function __construct($server, $login, $pass, $database)
  27.         {
  28.             $this->_INFOS['server'] = $server;
  29.             $this->_INFOS['login'] = $login;
  30.             $this->_INFOS['pass'] = $pass;
  31.             $this->_INFOS['database'] = $database;
  32.         }
  33.  
  34.         private function connect()
  35.         {
  36.             $this->_INFOS['handle'] =
  37.             mysql_connect($this->_INFOS['server'], $this->_INFOS['login'], $this->_INFOS['pass']);
  38.             mysql_select_db($this->_INFOS['database'], $this->_INFOS['handle']);
  39.         }
  40.  
  41.         private function disconnect()
  42.         {
  43.             mysql_close($this->_INFOS['handle']);
  44.         }
  45.  
  46.         public function setDatabase($database)
  47.         {
  48.             $this->_INFOS['database'] = $database;
  49.             mysql_select_db($this->_INFOS['database'], $this->_INFOS['handle']);
  50.         }
  51.  
  52.         public function getQuery()
  53.         {
  54.             return $this->_QUERY['string'];
  55.         }
  56.        
  57.         public function setQuery($query)
  58.         {
  59.             $this->_QUERY['string'] = $query;
  60.         }
  61.  
  62.         public function resetQuery()
  63.         {
  64.             $this->_QUERY = array(  'string' => '',
  65.                                     'field' => array(),
  66.                                     'table' => array(),
  67.                                     'search' => array(),
  68.                                     'value' => array(),
  69.                                     'filter' => array(),
  70.                                     'order' => array(),
  71.                                     'limit1' => 0,
  72.                                     'limit2' => 0);
  73.             $this->_RESULT = array( 'result' => array(),
  74.                                     'count' => 0);
  75.         }
  76.        
  77.         private function secure($value)
  78.         {
  79.             strip_tags($value);
  80.             if(get_magic_quotes_gpc())
  81.             {
  82.                 $value = stripslashes($value);
  83.             }
  84.             $value = mysql_real_escape_string($value);
  85.             $value = '\'' . $value . '\'';
  86.             return $value;
  87.         }
  88.        
  89.         private function adapt($field)
  90.         {
  91.             $field = '`' . implode('`.`', explode('.', $field)) . '`';
  92.             return $field;
  93.         }
  94.        
  95.         public function addField($field)
  96.         {
  97.             $this->_QUERY['field'][] = $this->adapt($field);
  98.         }
  99.        
  100.         public function resetField()
  101.         {
  102.             $this->_QUERY['field'] = array();
  103.         }
  104.        
  105.         public function addTable($table)
  106.         {
  107.             $this->_QUERY['table'][] = $this->adapt($table);
  108.         }
  109.        
  110.         public function resetTable()
  111.         {
  112.             $this->_QUERY['table'] = array();
  113.         }
  114.        
  115.         public function addSearch($field, $op, $value)
  116.         {
  117.             $this->_QUERY['search'][] = $this->adapt($field) . $op . $this->secure($value);
  118.         }
  119.        
  120.         public function addSearchWithField($field, $op, $field2)
  121.         {
  122.             $this->_QUERY['search'][] = $this->adapt($field) . $op . $this->adapt($field2);
  123.         }
  124.        
  125.         public function resetSearch()
  126.         {
  127.             $this->_QUERY['search'] = array();
  128.         }
  129.        
  130.         public function addValue($value)
  131.         {
  132.             $this->_QUERY['value'][] = $this->secure($value);
  133.         }
  134.        
  135.         public function resetValue()
  136.         {
  137.             $this->_QUERY['value'] = array();
  138.         }
  139.        
  140.         public function addFilter($field, $value)
  141.         {
  142.             $this->_QUERY['filter'][] = $this->adapt($field) . ' LIKE ' . $this->secure($value);
  143.         }
  144.        
  145.         public function resetFilter()
  146.         {
  147.             $this->_QUERY['filter'] = array();
  148.         }
  149.        
  150.         public function addOrder($field, $order)
  151.         {
  152.             $this->_QUERY['order'][] = $this->adapt($field) . ' ' . $order;
  153.         }
  154.        
  155.         public function resetOrder()
  156.         {
  157.             $this->_QUERY['order'] = array();
  158.         }
  159.        
  160.         public function setLimit2($limit1, $limit2)
  161.         {
  162.             $this->_QUERY['limit1'] = $this->secure($limit1);
  163.             $this->_QUERY['limit2'] = $this->secure($limit2);
  164.         }
  165.        
  166.         public function setLimit1($limit)
  167.         {
  168.             $this->_QUERY['limit1'] = $this->secure(0);
  169.             $this->_QUERY['limit2'] = $this->secure($limit);
  170.         }
  171.        
  172.         public function buildSelect()
  173.         {
  174.             $this->_QUERY['string'] = 'SELECT ' . implode(',', $this->_QUERY['field']);
  175.             $this->_QUERY['string'] .= ' FROM ' . implode(',', $this->_QUERY['table']);
  176.             $temp = array_merge($this->_QUERY['search'], $this->_QUERY['filter']);
  177.             if(!empty($temp))
  178.             {
  179.                 $this->_QUERY['string'] .= ' WHERE ' . implode(' AND ', $temp);
  180.             }
  181.             if(!empty($this->_QUERY['order']))
  182.             {
  183.                 $this->_QUERY['string'] .= ' ORDER BY ' . implode(',', $this->_QUERY['order']);
  184.             }
  185.             if($this->_QUERY['limit2'] > 0)
  186.             {
  187.                 $this->_QUERY['string'] .= ' LIMIT ' . $this->_QUERY['limit1'];
  188.                 $this->_QUERY['string'] . ',' . $this->_QUERY['limit2'];
  189.             }
  190.             //$this->_QUERY['string'] .= ';';
  191.         }
  192.        
  193.         public function buildInsert()
  194.         {
  195.             $this->_QUERY['string'] = 'INSERT INTO ' . $this->_QUERY['table'][0] . '(';
  196.             $this->_QUERY['string'] .= implode(',', $this->_QUERY['field']) . ')';
  197.             $this->_QUERY['string'] .= ' VALUES (' . implode(',', $this->_QUERY['value']) . ')';
  198.             //$this->_QUERY['string'] .= ';';
  199.         }
  200.        
  201.         public function buildUpdate()
  202.         {
  203.             $this->_QUERY['string'] = 'UPDATE ' . $this->_QUERY['table'][0];
  204.             $this->_QUERY['string'] .= ' SET ';
  205.             $temp = array();
  206.             for($i = 0; $i < count($this->_QUERY['field']); ++$i)
  207.             {
  208.                 $temp[] = $this->_QUERY['field'][$i] . '=' . $this->_QUERY['value'][$i];
  209.             }
  210.             $this->_QUERY['string'] .= implode(',', $temp);
  211.             $temp = array_merge($this->_QUERY['search'], $this->_QUERY['filter']);
  212.             if(!empty($temp))
  213.             {
  214.                 $this->_QUERY['string'] .= ' WHERE ' . implode(' AND ', $temp);
  215.             }
  216.             //$this->_QUERY['string'] .= ';';
  217.         }
  218.        
  219.         public function buildDelete()
  220.         {
  221.             $this->_QUERY['string'] = 'DELETE FROM ' . $this->_QUERY['table'][0];
  222.             $temp = array_merge($this->_QUERY['search'], $this->_QUERY['filter']);
  223.             if(!empty($temp))
  224.             {
  225.                 $this->_QUERY['string'] .= ' WHERE ' . implode(' AND ', $temp);
  226.             }
  227.             //$this->_QUERY['string'] .= ';';
  228.         }
  229.        
  230.         public function execute()
  231.         {
  232.             $this->connect();
  233.             $res = mysql_query($this->_QUERY['string']);
  234.             while($this->_RESULT['result'][] = mysql_fetch_array($res, MYSQL_ASSOC));
  235.             $this->_RESULT['count'] = mysql_num_rows($res);
  236.             unset($this->_RESULT['result'][$this->resultCount()];
  237.             $this->disconnect();
  238.         }
  239.        
  240.         public function resultCount()
  241.         {
  242.             return $this->_RESULT['count'];
  243.         }
  244.        
  245.         public function getResult($field)
  246.         {
  247.             for($i = 0; $i < $this->resultCount(); $i++)
  248.             {
  249.                 $temp[] = $this->_RESULT['result'][$i][$field];
  250.             }
  251.             return $temp;
  252.         }
  253.        
  254.         public function getEntireResult()
  255.         {
  256.             return $this->_RESULT['result'];
  257.         }
  258.     }
  259. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement