Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * class.db.php
- *
- * Copyright 2011 Andrei Aleksandovich Bagrintsev <a.bagrintsev@imedia.ru>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- */
- require ('config.db.php');
- class db_i {
- public $mysqli = null;
- private $init_time = 0;
- private $current_db = '';
- private $log = array ();
- public $no_tpl_slash = false;
- public $table_prefix = '';
- public $last_insert_id = 0;
- public $no_quote = array ();
- public $query_info = array ();
- function __construct ()
- {
- if (!extension_loaded('mysqli'))
- exit ("Can't find MySQLi extension. Try run `php -m` in your server terminal.");
- $this->init_time = $this->get_mt();
- $this->do_log ('info', array ('data'=>'Starting generic Database layer with MySQLi extension. Version 0.2.'));
- if (!$GLOBALS['db_init'])
- {
- $GLOBALS['db'] =& $this;
- }
- }
- function __destruct ()
- {
- if ( !is_null($this->mysqli) && !mysqli_connect_errno() )
- $this->mysqli->close();
- }
- private function get_mt ()
- {
- list($usec, $sec) = explode(" ", microtime());
- return ((float)$usec + (float)$sec);
- }
- function init ($db_host=null, $db_user=null, $db_pass=null, $db_name=false, $force_init=false)
- {
- if ($force_init || $GLOBALS['db_init']) return;
- $conn_time = $this->get_mt();
- $this->mysqli = new mysqli ($db_host, $db_user, $db_pass, $db_name);
- if (mysqli_connect_errno())
- {
- // Error!
- $this->do_log ('error',array('data'=>mysqli_connect_error()));
- return false;
- }
- $conn_time = $this->get_mt() - $conn_time;
- $this->send_charset ($GLOBALS['config']['db_charset']);
- $this->do_log ('info',array('data'=>'Connected to server "'.$db_host.'" in '.round($conn_time,4).' sec'));
- $GLOBALS['db_init'] = true;
- if ($db_name)
- return $this->select_db ($db_name);
- return true;
- }
- function db_table_name ($table)
- {
- $ss = ($this->no_tpl_slash?'':'`');
- return ($this->current_db?$ss.$this->current_db.$ss.'.':'').$ss.$this->table_prefix.$table.$ss;
- }
- function get_html_log ($show_types = array('info','error','query'))
- {
- $out = '<table cellpadding="3" cellspacing="0" border="1" width="100%">';
- $out.= '<tr><th align="center">#</th><th align="center">Type</th><th>Time</th><th>Record</th></tr>';
- foreach ($this->log as $n=>$rec)
- {
- if (!in_array($rec['type'], $show_types))
- continue;
- $out .= '<tr><td align="center">'.($n+1).'</td>';
- if ($rec['type'] == 'info')
- {
- $out .= '<td align="center" style="color:navy">Info</td>';
- $out .= '<td>+ '.round($rec['time']-$this->init_time,4).' sec</td>';
- $out .= '<td><code>'.$rec['data'].'</code></td>';
- }
- if ($rec['type'] == 'error')
- {
- $out .= '<td align="center" style="color:red">Error</td>';
- $out .= '<td>+ '.round($rec['time']-$this->init_time,4).' sec</td>';
- $out .= '<td><code>'.$rec['data'].'</code></td>';
- }
- if ($rec['type'] == 'query')
- {
- $out .= '<td align="center" style="color:green">Query</td>';
- $out .= '<td>+ '.round($rec['time']-$this->init_time,4).' sec</td>';
- // Build query info
- $out .= '<td>';
- $out .= '<b>Query</b> : <code>'.$rec['query'].'</code><br/>';
- if (@$rec['error'])
- {
- $out .= '<b>Error</b> : <code style="color:red;">'.$rec['error'].'</code><br/>';
- }
- if (!@$rec['error'])
- {
- $out .= '<b>Result</b> : <b><code style="font-weight:bold;font-size:large;">'.$rec['result_rows'].'</code></b> rows<br/>';
- }
- if (@$rec['query_time'])
- {
- $out .= '<b>Time</b> : <code>'.round($rec['query_time'],4).'</code> sec<br/>';
- }
- $out .= '</td>';
- }
- $out .= '</tr>';
- }
- $out.='</table>';
- return $out;
- }
- function quotes ($in)
- {
- if (!is_numeric($in))
- {
- $in = "'".$in."'";
- }
- return $this->escape_string ($in);
- }
- function select_db ($db_name)
- {
- if (!$this->mysqli->select_db($db_name))
- {
- $this->do_log ('error',array('data'=>$this->mysqli->error));
- return false;
- } else {
- $this->do_log ('info',array('data'=>"Selection DB '".$db_name."' is OK"));
- $this->current_db = $db_name;
- return true;
- }
- }
- function escape_string ($txt)
- {
- // Init DB if not connected
- if (is_null ($this->mysqli))
- {
- $this->init($GLOBALS['config']['db_host'],
- $GLOBALS['config']['db_user'],
- $GLOBALS['config']['db_pass'],
- $GLOBALS['config']['db_base']);
- }
- return $this->mysqli->real_escape_string ($txt);
- }
- // QUERY FUNCTIONS
- function db_query ($query, $table='manual', $query_info=false)
- {
- // Init DB if not connected
- if (is_null ($this->mysqli))
- {
- if (!$this->init($GLOBALS['config']['db_host'],
- $GLOBALS['config']['db_user'],
- $GLOBALS['config']['db_pass'],
- $GLOBALS['config']['db_base']))
- return false;
- }
- // do query
- $qw_time = $this->get_mt();
- $res = $this->mysqli->query ($query, MYSQLI_USE_RESULT);
- $qw_time = $this->get_mt()-$qw_time;
- if (!$res || $this->mysqli->errno)
- {
- // Error!
- $this->do_log ('query',array('query'=>$query, 'query_time'=>$qw_time, 'result_rows'=>0, 'error'=>$this->mysqli->error));
- return false;
- }
- // if query type is INSERT or similar, method query() returning bool TRUE or FALSE. We returning it to sub-methods of class.
- if (is_bool ($res))
- {
- $out_data = $res;
- $result_rows = 1;
- // Query have insert type? Writing-in last insert id.
- if ($this->mysqli->insert_id)
- $this->last_insert_id = $this->mysqli->insert_id;
- } else {
- // Prepare data
- while ($line = $res->fetch_assoc())
- {
- $out_data[] = $line;
- }
- $result_rows = $res->num_rows;
- }
- if ($query_info)
- {
- $query_info = array ('current_field'=>$res->current_field, 'field_count'=>$res->field_count,
- 'lenghts'=>$res->field_count, 'num_rows'=>$res->num_rows, 'type'=>$res->type);
- $this->query_info = $query_info;
- }
- $this->do_log ('query',array('query'=>$query, 'query_time'=>$qw_time, 'result_rows'=>$result_rows, 'error'=>false));
- if (is_object($res))
- $res->free ();
- return $out_data;
- }
- private function do_log ($type, $data)
- {
- if (!@$data['type'])
- $data['type'] = $type;
- if (!@$data['time'])
- $data['time'] = $this->get_mt();
- $this->log[] = $data;
- }
- function send_charset ($charset = 'UTF8')
- {
- $this->mysqli->set_charset ($charset);
- $this->do_log ('info', array ('data'=>"Selection ".$charset." charset"));
- }
- function select ($table, $what = '*', $exp = false)
- {
- if (is_array($what))
- {
- $what = '`'.implode('`,`', $what).'`';
- }
- if (!$exp) $exp = '1';
- $query = 'SELECT '.$what.' FROM '.$this->db_table_name($table).' WHERE '.$exp;
- return $this->db_query($query, $table);
- }
- function select_one ($table, $what = '*', $id, $id_text = 'id')
- {
- if (is_array($what))
- {
- $what = '`'.implode('`,`', $what).'`';
- }
- $exp = '`'.$id_text.'` = '.$this->quotes($id);
- $query = 'SELECT '.$what.' FROM '.$this->db_table_name($table).' WHERE '.$exp;
- $res = $this->db_query($query, $table, false);
- if (!@$res[0]) return $this->return_on_false;
- return $res[0];
- }
- function insert ($table, $data, $no_quote = array ())
- {
- $upd_data = array ();
- $upd_keys = array ();
- foreach ($data as $k=>$v)
- {
- if (!in_array($k, $no_quote))
- $v = "'".$this->escape_string ($v)."'";
- $upd_keys[] = '`'.$k.'`';
- $upd_data[] = $v;
- }
- $query = 'INSERT INTO '.$this->db_table_name($table).' ('.implode(',',$upd_keys).') VALUES ('.implode(',',$upd_data).')';
- // if query was success, return to user last insert id
- if ($this->db_query($query, $table))
- return $this->last_insert_id;
- }
- function insert_id ()
- {
- if ($this->last_insert_id)
- return $this->last_insert_id;
- elseif ($this->mysqli->insert_id)
- return $this->mysqli->insert_id;
- else return false;
- }
- function update_by_id ($table, $id, $data, $id_text = 'id', $no_quote = array ())
- {
- $upd_data = array ();
- foreach ($data as $k=>$v)
- {
- if (!in_array($k, $no_quote))
- $v = $this->quote ($v);
- $upd_data[] = '`'.$k.'`='.$v;
- }
- $query = 'UPDATE '.$this->db_table_name($table).' SET '.implode(',',$upd_data).' WHERE `'.$id_text.'`='.$this->quote ($id);
- return $this->db_query($query, $table);
- }
- function update_by_exp ($table, $data, $exp='1')
- {
- $upd_data = array ();
- $upd_keys = array ();
- foreach ($data as $k=>$v)
- {
- if (!in_array($k, $this->no_quote))
- $v = $this->quotes ($v);
- $upd_data[] = '`'.$k.'`='.$v;
- }
- $query = 'UPDATE '.$this->db_table_name($table).' SET '.implode(',',$upd_data).' WHERE '.$exp;
- return $this->db_query($query, $table);
- }
- function delete ($table, $what = false)
- {
- if ($table)
- $table_name = " FROM `".$table."`";
- if (is_array($what))
- {
- //...
- foreach ($what as $k=>$v)
- {
- $where[] = "`".$k."`=".$this->quotes ($v);
- }
- $where = implode (' AND ', $where);
- } elseif (!$what) {
- $where = 1;
- } else {
- $where = $what;
- }
- return $this->db_query ("DELETE".( ($table)?$table_name:'')." WHERE ".$where, $table );
- }
- function count ($table, $exp = false)
- {
- if (!$exp) $exp = '1';
- $query = 'SELECT COUNT(*) FROM '.$this->db_table_name($table).' WHERE '.$exp;
- $res = $this->db_query($query, $table, false);
- if (!@$res[0]['COUNT(*)']) return $this->return_on_false;
- return $res[0]['COUNT(*)'];
- }
- }
- function select_db ($name)
- {
- $GLOBALS['db']->select_db ($name) or die ('Database change to "'.$name.'" failed!');
- return true;
- }
- function db_c ($db)
- {
- $GLOBALS['db']->current_db = $db;
- }
- function db_select ($table, $what = '*', $exp = false)
- {
- return $GLOBALS['db']->select($table, $what, $exp);
- }
- function db_select_one ($table, $what = '*', $id, $id_text = 'id')
- {
- return $GLOBALS['db']->select_one($table, $what, $id, $id_text);
- }
- function db_count ($table, $exp = false)
- {
- return $GLOBALS['db']->count($table, $exp);
- }
- function db_insert ($table, $data, $no_quote = array ())
- {
- return $GLOBALS['db']->insert($table, $data, $no_quote);
- }
- function db_insert_id ()
- {
- return $GLOBALS['db']->insert_id();
- }
- function db_update_by_id ($table, $id, $data, $id_text = 'id', $no_quote = array ())
- {
- return $GLOBALS['db']->update_by_id ($table, $id, $data, $id_text, $no_quote);
- }
- function db_update_by_exp ($table, $data, $exp='1')
- {
- return $GLOBALS['db']->update_by_exp ($table, $data, $exp);
- }
- function db_query ($query, $table='direct_api', $drop = true)
- {
- return $GLOBALS['db']->db_query ($query, $table, $drop);
- }
- function db_stats ()
- {
- return $GLOBALS['db']->log;
- }
- function db_show_tables ($table = false)
- {
- return $GLOBALS['db']->show_tables ($table);
- }
- // alias for compatibility with old programs
- function db_del_row ($table, $where = false)
- {
- return $GLOBALS['db']->delete ($table, $where);
- }
- function db_delete ($table, $where = false)
- {
- return $GLOBALS['db']->delete($table, $where);
- }
- function real_escape_string ($txt)
- {
- return $GLOBALS['db']->escape_string ($txt);
- }
- function db_get_last_query_info ()
- {
- if ($GLOBALS['db']->query_info)
- return $GLOBALS['db']->query_info;
- else return NULL;
- }
- if (!$GLOBALS['db_init'])
- $GLOBALS['db'] = new db_i;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement