Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DAL_Query
- {
- private $_INFOS = array(
- 'server' => '',
- 'login' => '',
- 'pass' => '',
- 'database' => '',
- 'handle' => '');
- private $_QUERY = array(
- 'string' => '',
- 'field' => array(),
- 'table' => array(),
- 'search' => array(),
- 'value' => array(),
- 'filter' => array(),
- 'order' => array(),
- 'limit1' => 0,
- 'limit2' => 0);
- private $_RESULT = array(
- 'result' => array(),
- 'count' => 0);
- public function __construct($server, $login, $pass, $database)
- {
- $this->_INFOS['server'] = $server;
- $this->_INFOS['login'] = $login;
- $this->_INFOS['pass'] = $pass;
- $this->_INFOS['database'] = $database;
- }
- private function connect()
- {
- $this->_INFOS['handle'] =
- mysql_connect($this->_INFOS['server'], $this->_INFOS['login'], $this->_INFOS['pass']);
- mysql_select_db($this->_INFOS['database'], $this->_INFOS['handle']);
- }
- private function disconnect()
- {
- mysql_close($this->_INFOS['handle']);
- }
- public function setDatabase($database)
- {
- $this->_INFOS['database'] = $database;
- mysql_select_db($this->_INFOS['database'], $this->_INFOS['handle']);
- }
- public function getQuery()
- {
- return $this->_QUERY['string'];
- }
- public function setQuery($query)
- {
- $this->_QUERY['string'] = $query;
- }
- public function resetQuery()
- {
- $this->_QUERY = array( 'string' => '',
- 'field' => array(),
- 'table' => array(),
- 'search' => array(),
- 'value' => array(),
- 'filter' => array(),
- 'order' => array(),
- 'limit1' => 0,
- 'limit2' => 0);
- $this->_RESULT = array( 'result' => array(),
- 'count' => 0);
- }
- private function secure($value)
- {
- strip_tags($value);
- if(get_magic_quotes_gpc())
- {
- $value = stripslashes($value);
- }
- $value = mysql_real_escape_string($value);
- $value = '\'' . $value . '\'';
- return $value;
- }
- private function adapt($field)
- {
- $field = '`' . implode('`.`', explode('.', $field)) . '`';
- return $field;
- }
- public function addField($field)
- {
- $this->_QUERY['field'][] = $this->adapt($field);
- }
- public function resetField()
- {
- $this->_QUERY['field'] = array();
- }
- public function addTable($table)
- {
- $this->_QUERY['table'][] = $this->adapt($table);
- }
- public function resetTable()
- {
- $this->_QUERY['table'] = array();
- }
- public function addSearch($field, $op, $value)
- {
- $this->_QUERY['search'][] = $this->adapt($field) . $op . $this->secure($value);
- }
- public function addSearchWithField($field, $op, $field2)
- {
- $this->_QUERY['search'][] = $this->adapt($field) . $op . $this->adapt($field2);
- }
- public function resetSearch()
- {
- $this->_QUERY['search'] = array();
- }
- public function addValue($value)
- {
- $this->_QUERY['value'][] = $this->secure($value);
- }
- public function resetValue()
- {
- $this->_QUERY['value'] = array();
- }
- public function addFilter($field, $value)
- {
- $this->_QUERY['filter'][] = $this->adapt($field) . ' LIKE ' . $this->secure($value);
- }
- public function resetFilter()
- {
- $this->_QUERY['filter'] = array();
- }
- public function addOrder($field, $order)
- {
- $this->_QUERY['order'][] = $this->adapt($field) . ' ' . $order;
- }
- public function resetOrder()
- {
- $this->_QUERY['order'] = array();
- }
- public function setLimit2($limit1, $limit2)
- {
- $this->_QUERY['limit1'] = $this->secure($limit1);
- $this->_QUERY['limit2'] = $this->secure($limit2);
- }
- public function setLimit1($limit)
- {
- $this->_QUERY['limit1'] = $this->secure(0);
- $this->_QUERY['limit2'] = $this->secure($limit);
- }
- public function buildSelect()
- {
- $this->_QUERY['string'] = 'SELECT ' . implode(',', $this->_QUERY['field']);
- $this->_QUERY['string'] .= ' FROM ' . implode(',', $this->_QUERY['table']);
- $temp = array_merge($this->_QUERY['search'], $this->_QUERY['filter']);
- if(!empty($temp))
- {
- $this->_QUERY['string'] .= ' WHERE ' . implode(' AND ', $temp);
- }
- if(!empty($this->_QUERY['order']))
- {
- $this->_QUERY['string'] .= ' ORDER BY ' . implode(',', $this->_QUERY['order']);
- }
- if($this->_QUERY['limit2'] > 0)
- {
- $this->_QUERY['string'] .= ' LIMIT ' . $this->_QUERY['limit1'];
- $this->_QUERY['string'] . ',' . $this->_QUERY['limit2'];
- }
- //$this->_QUERY['string'] .= ';';
- }
- public function buildInsert()
- {
- $this->_QUERY['string'] = 'INSERT INTO ' . $this->_QUERY['table'][0] . '(';
- $this->_QUERY['string'] .= implode(',', $this->_QUERY['field']) . ')';
- $this->_QUERY['string'] .= ' VALUES (' . implode(',', $this->_QUERY['value']) . ')';
- //$this->_QUERY['string'] .= ';';
- }
- public function buildUpdate()
- {
- $this->_QUERY['string'] = 'UPDATE ' . $this->_QUERY['table'][0];
- $this->_QUERY['string'] .= ' SET ';
- $temp = array();
- for($i = 0; $i < count($this->_QUERY['field']); ++$i)
- {
- $temp[] = $this->_QUERY['field'][$i] . '=' . $this->_QUERY['value'][$i];
- }
- $this->_QUERY['string'] .= implode(',', $temp);
- $temp = array_merge($this->_QUERY['search'], $this->_QUERY['filter']);
- if(!empty($temp))
- {
- $this->_QUERY['string'] .= ' WHERE ' . implode(' AND ', $temp);
- }
- //$this->_QUERY['string'] .= ';';
- }
- public function buildDelete()
- {
- $this->_QUERY['string'] = 'DELETE FROM ' . $this->_QUERY['table'][0];
- $temp = array_merge($this->_QUERY['search'], $this->_QUERY['filter']);
- if(!empty($temp))
- {
- $this->_QUERY['string'] .= ' WHERE ' . implode(' AND ', $temp);
- }
- //$this->_QUERY['string'] .= ';';
- }
- public function execute()
- {
- $this->connect();
- $res = mysql_query($this->_QUERY['string']);
- while($this->_RESULT['result'][] = mysql_fetch_array($res, MYSQL_ASSOC));
- $this->_RESULT['count'] = mysql_num_rows($res);
- unset($this->_RESULT['result'][$this->resultCount()];
- $this->disconnect();
- }
- public function resultCount()
- {
- return $this->_RESULT['count'];
- }
- public function getResult($field)
- {
- for($i = 0; $i < $this->resultCount(); $i++)
- {
- $temp[] = $this->_RESULT['result'][$i][$field];
- }
- return $temp;
- }
- public function getEntireResult()
- {
- return $this->_RESULT['result'];
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement