123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876 |
- <?php
- // +--------------------------------------------------------------------------
- // | Senthot [ DEVELOPED BY ME ]
- // +--------------------------------------------------------------------------
- // | Copyright (c) 2005-2013 http://www.senthot.com All rights reserved.
- // | License ( http://www.apache.org/licenses/LICENSE-2.0 )
- // | Author: ms134n ( [email protected] )
- // +--------------------------------------------------------------------------
- /**
- * Senthot Database middle tier implementation class
- * @category Sen
- * @package Sen
- * @subpackage Core
- * @author ms134n <[email protected]>
- */
- class Db {
- // Database Type
- protected $dbType = null;
- // Whether to automatically release the results
- protected $autoFree = false;
- // Current operation belongs to the model name
- protected $model = '_sen_';
- // Whether to use persistent connections
- protected $pconnect = false;
- // Current SQL commands
- protected $queryStr = '';
- protected $modelSql = array();
- // Last inserted ID
- protected $lastInsID = null;
- // Or to affect the number of records returned
- protected $numRows = 0;
- // Returns the number of fields
- protected $numCols = 0;
- // Service Instructions
- protected $transTimes = 0;
- // Error Messages
- protected $error = '';
- // Database Connection ID Support multiple connections
- protected $linkID = array();
- // Current connection ID
- protected $_linkID = null;
- // Current query ID
- protected $queryID = null;
- // Is already connected to the database
- protected $connected = false;
- // Database connection configuration parameters
- protected $config = '';
- // Database Expressions
- protected $comparison = array('eq'=>'=','neq'=>'<>','gt'=>'>','egt'=>'>=','lt'=>'<','elt'=>'<=','notlike'=>'NOT LIKE','like'=>'LIKE','in'=>'IN','notin'=>'NOT IN');
- // Query Expressions
- protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% %UNION%%COMMENT%';
- /**
- * Get database class instance
- * @static
- * @access public
- * @return mixed Returns the database driver class
- */
- public static function getInstance() {
- $args = func_get_args();
- return get_instance_of(__CLASS__,'factory',$args);
- }
- /**
- * Loading the database Support configuration file or DSN
- * @access public
- * @param mixed $db_config Database configuration information
- * @return string
- */
- public function factory($db_config='') {
- // Read database configuration
- $db_config = $this->parseConfig($db_config);
- if(empty($db_config['dbms']))
- throw_exception(L('_NO_DB_CONFIG_'));
- // Database Type
- $this->dbType = ucwords(strtolower($db_config['dbms']));
- $class = 'Db'. $this->dbType;
- // Check the driver class
- if(class_exists($class)) {
- $db = new $class($db_config);
- // Get the current database type
- if( 'pdo' != strtolower($db_config['dbms']) )
- $db->dbType = strtoupper($this->dbType);
- else
- $db->dbType = $this->_getDsnType($db_config['dsn']);
- }else {
- // Class does not define
- throw_exception(L('_NO_DB_DRIVER_').': ' . $class);
- }
- return $db;
- }
- /**
- * According DSN for the database type Back to uppercase
- * @access protected
- * @param string $dsn dsn string
- * @return string
- */
- protected function _getDsnType($dsn) {
- $match = explode(':',$dsn);
- $dbType = strtoupper(trim($match[0]));
- return $dbType;
- }
- /**
- * Analysis of the database configuration information, Support and DSN array
- * @access private
- * @param mixed $db_config Database configuration information
- * @return string
- */
- private function parseConfig($db_config='') {
- if ( !empty($db_config) && is_string($db_config)) {
- // If the DSN string is parsed
- $db_config = $this->parseDSN($db_config);
- }elseif(is_array($db_config)) { // Array Configuration
- $db_config = array_change_key_case($db_config);
- $db_config = array(
- 'dbms' => $db_config['db_type'],
- 'username' => $db_config['db_user'],
- 'password' => $db_config['db_pwd'],
- 'hostname' => $db_config['db_host'],
- 'hostport' => $db_config['db_port'],
- 'database' => $db_config['db_name'],
- 'dsn' => $db_config['db_dsn'],
- 'params' => $db_config['db_params'],
- );
- }elseif(empty($db_config)) {
- // If the configuration is empty, reads the configuration file settings
- if( C('DB_DSN') && 'pdo' != strtolower(C('DB_TYPE')) ) { // If you set the DB_DSN Preferentially
- $db_config = $this->parseDSN(C('DB_DSN'));
- }else{
- $db_config = array (
- 'dbms' => C('DB_TYPE'),
- 'username' => C('DB_USER'),
- 'password' => C('DB_PWD'),
- 'hostname' => C('DB_HOST'),
- 'hostport' => C('DB_PORT'),
- 'database' => C('DB_NAME'),
- 'dsn' => C('DB_DSN'),
- 'params' => C('DB_PARAMS'),
- );
- }
- }
- return $db_config;
- }
- /**
- * Initialize the database connection
- * @access protected
- * @param boolean $master Master Server
- * @return void
- */
- protected function initConnect($master=true) {
- if(1 == C('DB_DEPLOY_TYPE'))
- // Using a distributed database
- $this->_linkID = $this->multiConnect($master);
- else
- // Default single database
- if ( !$this->connected ) $this->_linkID = $this->connect();
- }
- /**
- * Distributed server connection
- * @access protected
- * @param boolean $master Master Server
- * @return void
- */
- protected function multiConnect($master=false) {
- static $_config = array();
- if(empty($_config)) {
- // Distributed database configuration parsing cache
- foreach ($this->config as $key=>$val){
- $_config[$key] = explode(',',$val);
- }
- }
- // Whether separate database literacy
- if(C('DB_RW_SEPARATE')){
- // Master-slave using separate read and write
- if($master)
- // Primary server writes
- $r = floor(mt_rand(0,C('DB_MASTER_NUM')-1));
- else{
- if(is_numeric(C('DB_SLAVE_NO'))) {// Read the specified server
- $r = C('DB_SLAVE_NO');
- }else{
- // Read from the server connection
- $r = floor(mt_rand(C('DB_MASTER_NUM'),count($_config['hostname'])-1)); // Each randomly connected database
- }
- }
- }else{
- // The server does not distinguish between read and write operations
- $r = floor(mt_rand(0,count($_config['hostname'])-1)); // Each randomly connected database
- }
- $db_config = array(
- 'username' => isset($_config['username'][$r])?$_config['username'][$r]:$_config['username'][0],
- 'password' => isset($_config['password'][$r])?$_config['password'][$r]:$_config['password'][0],
- 'hostname' => isset($_config['hostname'][$r])?$_config['hostname'][$r]:$_config['hostname'][0],
- 'hostport' => isset($_config['hostport'][$r])?$_config['hostport'][$r]:$_config['hostport'][0],
- 'database' => isset($_config['database'][$r])?$_config['database'][$r]:$_config['database'][0],
- 'dsn' => isset($_config['dsn'][$r])?$_config['dsn'][$r]:$_config['dsn'][0],
- 'params' => isset($_config['params'][$r])?$_config['params'][$r]:$_config['params'][0],
- );
- return $this->connect($db_config,$r);
- }
- /**
- * DSN parsing
- * Format: mysql://username:passwd@localhost:3306/DbName
- * @static
- * @access public
- * @param string $dsnStr
- * @return array
- */
- public function parseDSN($dsnStr) {
- if( empty($dsnStr) ){return false;}
- $info = parse_url($dsnStr);
- if($info['scheme']){
- $dsn = array(
- 'dbms' => $info['scheme'],
- 'username' => isset($info['user']) ? $info['user'] : '',
- 'password' => isset($info['pass']) ? $info['pass'] : '',
- 'hostname' => isset($info['host']) ? $info['host'] : '',
- 'hostport' => isset($info['port']) ? $info['port'] : '',
- 'database' => isset($info['path']) ? substr($info['path'],1) : ''
- );
- }else {
- preg_match('/^(.*?)\:\/\/(.*?)\:(.*?)\@(.*?)\:([0-9]{1, 6})\/(.*?)$/',trim($dsnStr),$matches);
- $dsn = array (
- 'dbms' => $matches[1],
- 'username' => $matches[2],
- 'password' => $matches[3],
- 'hostname' => $matches[4],
- 'hostport' => $matches[5],
- 'database' => $matches[6]
- );
- }
- $dsn['dsn'] = ''; // Compatible with an array of configuration information
- return $dsn;
- }
- /**
- * Database Debugging Record the current SQL
- * @access protected
- */
- protected function debug() {
- $this->modelSql[$this->model] = $this->queryStr;
- $this->model = '_sen_';
- // End time recording operation
- if (C('DB_SQL_LOG')) {
- G('queryEndTime');
- trace($this->queryStr.' [ RunTime:'.G('queryStartTime','queryEndTime',6).'s ]','','SQL');
- }
- }
- /**
- * Set the lock mechanism
- * @access protected
- * @return string
- */
- protected function parseLock($lock=false) {
- if(!$lock) return '';
- if('ORACLE' == $this->dbType) {
- return ' FOR UPDATE NOWAIT ';
- }
- return ' FOR UPDATE ';
- }
- /**
- * set analysis
- * @access protected
- * @param array $data
- * @return string
- */
- protected function parseSet($data) {
- foreach ($data as $key=>$val){
- $value = $this->parseValue($val);
- if(is_scalar($value)) // Filtering non-scalar data
- $set[] = $this->parseKey($key).'='.$value;
- }
- return ' SET '.implode(',',$set);
- }
- /**
- * Analysis of field names
- * @access protected
- * @param string $key
- * @return string
- */
- protected function parseKey(&$key) {
- return $key;
- }
-
- /**
- * value analysis
- * @access protected
- * @param mixed $value
- * @return string
- */
- protected function parseValue($value) {
- if(is_string($value)) {
- $value = '\''.$this->escapeString($value).'\'';
- }elseif(isset($value[0]) && is_string($value[0]) && strtolower($value[0]) == 'exp'){
- $value = $this->escapeString($value[1]);
- }elseif(is_array($value)) {
- $value = array_map(array($this, 'parseValue'),$value);
- }elseif(is_bool($value)){
- $value = $value ? '1' : '0';
- }elseif(is_null($value)){
- $value = 'null';
- }
- return $value;
- }
- /**
- * field analysis
- * @access protected
- * @param mixed $fields
- * @return string
- */
- protected function parseField($fields) {
- if(is_string($fields) && strpos($fields,',')) {
- $fields = explode(',',$fields);
- }
- if(is_array($fields)) {
- // Perfect way to pass an array of field names Support
- // Support 'field1'=>'field2' Such a field alias definitions
- $array = array();
- foreach ($fields as $key=>$field){
- if(!is_numeric($key))
- $array[] = $this->parseKey($key).' AS '.$this->parseKey($field);
- else
- $array[] = $this->parseKey($field);
- }
- $fieldsStr = implode(',', $array);
- }elseif(is_string($fields) && !empty($fields)) {
- $fieldsStr = $this->parseKey($fields);
- }else{
- $fieldsStr = '*';
- }
- //TODO If all the fields in the query , and is a join of the way, then put the table to check plus individual names , so the field is covered
- return $fieldsStr;
- }
- /**
- * table analysis
- * @access protected
- * @param mixed $table
- * @return string
- */
- protected function parseTable($tables) {
- if(is_array($tables)) {// Support alias definitions
- $array = array();
- foreach ($tables as $table=>$alias){
- if(!is_numeric($table))
- $array[] = $this->parseKey($table).' '.$this->parseKey($alias);
- else
- $array[] = $this->parseKey($table);
- }
- $tables = $array;
- }elseif(is_string($tables)){
- $tables = explode(',',$tables);
- array_walk($tables, array(&$this, 'parseKey'));
- }
- return implode(',',$tables);
- }
- /**
- * where analysis
- * @access protected
- * @param mixed $where
- * @return string
- */
- protected function parseWhere($where) {
- $whereStr = '';
- if(is_string($where)) {
- // Conditions of use strings directly
- $whereStr = $where;
- }else{ // Using array expression
- $operate = isset($where['_logic'])?strtoupper($where['_logic']):'';
- if(in_array($operate,array('AND','OR','XOR'))){
- // Define logical rules For example, OR XOR AND NOT
- $operate = ' '.$operate.' ';
- unset($where['_logic']);
- }else{
- // The default for AND Computing
- $operate = ' AND ';
- }
- foreach ($where as $key=>$val){
- $whereStr .= '( ';
- if(0===strpos($key,'_')) {
- // Special conditions analytic expressions
- $whereStr .= $this->parseSenWhere($key,$val);
- }else{
- // Security filtering query fields
- if(!preg_match('/^[A-Z_\|\&\-.a-z0-9\(\)\,]+$/',trim($key))){
- throw_exception(L('_EXPRESS_ERROR_').':'.$key);
- }
- // Support multi-condition
- $multi = is_array($val) && isset($val['_multi']);
- $key = trim($key);
- if(strpos($key,'|')) { // Support name|title|nickname Define the query field
- $array = explode('|',$key);
- $str = array();
- foreach ($array as $m=>$k){
- $v = $multi?$val[$m]:$val;
- $str[] = '('.$this->parseWhereItem($this->parseKey($k),$v).')';
- }
- $whereStr .= implode(' OR ',$str);
- }elseif(strpos($key,'&')){
- $array = explode('&',$key);
- $str = array();
- foreach ($array as $m=>$k){
- $v = $multi?$val[$m]:$val;
- $str[] = '('.$this->parseWhereItem($this->parseKey($k),$v).')';
- }
- $whereStr .= implode(' AND ',$str);
- }else{
- $whereStr .= $this->parseWhereItem($this->parseKey($key),$val);
- }
- }
- $whereStr .= ' )'.$operate;
- }
- $whereStr = substr($whereStr,0,-strlen($operate));
- }
- return empty($whereStr)?'':' WHERE '.$whereStr;
- }
- // where sub- element analysis
- protected function parseWhereItem($key,$val) {
- $whereStr = '';
- if(is_array($val)) {
- if(is_string($val[0])) {
- if(preg_match('/^(EQ|NEQ|GT|EGT|LT|ELT)$/i',$val[0])) { // Comparison Operators
- $whereStr .= $key.' '.$this->comparison[strtolower($val[0])].' '.$this->parseValue($val[1]);
- }elseif(preg_match('/^(NOTLIKE|LIKE)$/i',$val[0])){// Fuzzy Lookup
- if(is_array($val[1])) {
- $likeLogic = isset($val[2])?strtoupper($val[2]):'OR';
- if(in_array($likeLogic,array('AND','OR','XOR'))){
- $likeStr = $this->comparison[strtolower($val[0])];
- $like = array();
- foreach ($val[1] as $item){
- $like[] = $key.' '.$likeStr.' '.$this->parseValue($item);
- }
- $whereStr .= '('.implode(' '.$likeLogic.' ',$like).')';
- }
- }else{
- $whereStr .= $key.' '.$this->comparison[strtolower($val[0])].' '.$this->parseValue($val[1]);
- }
- }elseif('exp'==strtolower($val[0])){ // Using Expressions
- $whereStr .= ' ('.$key.' '.$val[1].') ';
- }elseif(preg_match('/IN/i',$val[0])){ // IN Computing
- if(isset($val[2]) && 'exp'==$val[2]) {
- $whereStr .= $key.' '.strtoupper($val[0]).' '.$val[1];
- }else{
- if(is_string($val[1])) {
- $val[1] = explode(',',$val[1]);
- }
- $zone = implode(',',$this->parseValue($val[1]));
- $whereStr .= $key.' '.strtoupper($val[0]).' ('.$zone.')';
- }
- }elseif(preg_match('/BETWEEN/i',$val[0])){ // BETWEEN operator
- $data = is_string($val[1])? explode(',',$val[1]):$val[1];
- $whereStr .= ' ('.$key.' '.strtoupper($val[0]).' '.$this->parseValue($data[0]).' AND '.$this->parseValue($data[1]).' )';
- }else{
- throw_exception(L('_EXPRESS_ERROR_').':'.$val[0]);
- }
- }else {
- $count = count($val);
- $rule = isset($val[$count-1])?strtoupper($val[$count-1]):'';
- if(in_array($rule,array('AND','OR','XOR'))) {
- $count = $count -1;
- }else{
- $rule = 'AND';
- }
- for($i=0;$i<$count;$i++) {
- $data = is_array($val[$i])?$val[$i][1]:$val[$i];
- if('exp'==strtolower($val[$i][0])) {
- $whereStr .= '('.$key.' '.$data.') '.$rule.' ';
- }else{
- $op = is_array($val[$i])?$this->comparison[strtolower($val[$i][0])]:'=';
- $whereStr .= '('.$key.' '.$op.' '.$this->parseValue($data).') '.$rule.' ';
- }
- }
- $whereStr = substr($whereStr,0,-4);
- }
- }else {
- //The string type field using fuzzy matching
- if(C('DB_LIKE_FIELDS') && preg_match('/('.C('DB_LIKE_FIELDS').')/i',$key)) {
- $val = '%'.$val.'%';
- $whereStr .= $key.' LIKE '.$this->parseValue($val);
- }else {
- $whereStr .= $key.' = '.$this->parseValue($val);
- }
- }
- return $whereStr;
- }
- /**
- * Special Conditions
- * @access protected
- * @param string $key
- * @param mixed $val
- * @return string
- */
- protected function parseSenWhere($key,$val) {
- $whereStr = '';
- switch($key) {
- case '_string':
- // Query string pattern
- $whereStr = $val;
- break;
- case '_complex':
- // Composite query
- $whereStr = substr($this->parseWhere($val),6);
- break;
- case '_query':
- // Query string pattern
- parse_str($val,$where);
- if(isset($where['_logic'])) {
- $op = ' '.strtoupper($where['_logic']).' ';
- unset($where['_logic']);
- }else{
- $op = ' AND ';
- }
- $array = array();
- foreach ($where as $field=>$data)
- $array[] = $this->parseKey($field).' = '.$this->parseValue($data);
- $whereStr = implode($op,$array);
- break;
- }
- return $whereStr;
- }
- /**
- * limit analysis
- * @access protected
- * @param mixed $lmit
- * @return string
- */
- protected function parseLimit($limit) {
- return !empty($limit)? ' LIMIT '.$limit.' ':'';
- }
- /**
- * join analysis
- * @access protected
- * @param mixed $join
- * @return string
- */
- protected function parseJoin($join) {
- $joinStr = '';
- if(!empty($join)) {
- if(is_array($join)) {
- foreach ($join as $key=>$_join){
- if(false !== stripos($_join,'JOIN'))
- $joinStr .= ' '.$_join;
- else
- $joinStr .= ' LEFT JOIN ' .$_join;
- }
- }else{
- $joinStr .= ' LEFT JOIN ' .$join;
- }
- }
- //This string will __TABLE_NAME__ replace regular table name, and bring a prefix and suffix
- $joinStr = preg_replace("/__([A-Z_-]+)__/esU",C("DB_PREFIX").".strtolower('$1')",$joinStr);
- return $joinStr;
- }
- /**
- * order analysis
- * @access protected
- * @param mixed $order
- * @return string
- */
- protected function parseOrder($order) {
- if(is_array($order)) {
- $array = array();
- foreach ($order as $key=>$val){
- if(is_numeric($key)) {
- $array[] = $this->parseKey($val);
- }else{
- $array[] = $this->parseKey($key).' '.$val;
- }
- }
- $order = implode(',',$array);
- }
- return !empty($order)? ' ORDER BY '.$order:'';
- }
- /**
- * group analysis
- * @access protected
- * @param mixed $group
- * @return string
- */
- protected function parseGroup($group) {
- return !empty($group)? ' GROUP BY '.$group:'';
- }
- /**
- * having analyzed
- * @access protected
- * @param string $having
- * @return string
- */
- protected function parseHaving($having) {
- return !empty($having)? ' HAVING '.$having:'';
- }
- /**
- * comment analysis
- * @access protected
- * @param string $comment
- * @return string
- */
- protected function parseComment($comment) {
- return !empty($comment)? ' /* '.$comment.' */':'';
- }
- /**
- * distinct analysis
- * @access protected
- * @param mixed $distinct
- * @return string
- */
- protected function parseDistinct($distinct) {
- return !empty($distinct)? ' DISTINCT ' :'';
- }
- /**
- * union analysis
- * @access protected
- * @param mixed $union
- * @return string
- */
- protected function parseUnion($union) {
- if(empty($union)) return '';
- if(isset($union['_all'])) {
- $str = 'UNION ALL ';
- unset($union['_all']);
- }else{
- $str = 'UNION ';
- }
- foreach ($union as $u){
- $sql[] = $str.(is_array($u)?$this->buildSelectSql($u):$u);
- }
- return implode(' ',$sql);
- }
- /**
- * Insert records
- * @access public
- * @param mixed $data Data
- * @param array $options Parameter expression
- * @param boolean $replace Whether or replace
- * @return false | integer
- */
- public function insert($data,$options=array(),$replace=false) {
- $values = $fields = array();
- $this->model = $options['model'];
- foreach ($data as $key=>$val){
- $value = $this->parseValue($val);
- if(is_scalar($value)) { // Filtering non-scalar data
- $values[] = $value;
- $fields[] = $this->parseKey($key);
- }
- }
- $sql = ($replace?'REPLACE':'INSERT').' INTO '.$this->parseTable($options['table']).' ('.implode(',', $fields).') VALUES ('.implode(',', $values).')';
- $sql .= $this->parseLock(isset($options['lock'])?$options['lock']:false);
- $sql .= $this->parseComment(!empty($options['comment'])?$options['comment']:'');
- return $this->execute($sql);
- }
- /**
- * Inserted through the Select Records
- * @access public
- * @param string $fields To insert a data table field names
- * @param string $table To insert the data table name
- * @param array $option Query data parameters
- * @return false | integer
- */
- public function selectInsert($fields,$table,$options=array()) {
- $this->model = $options['model'];
- if(is_string($fields)) $fields = explode(',',$fields);
- array_walk($fields, array($this, 'parseKey'));
- $sql = 'INSERT INTO '.$this->parseTable($table).' ('.implode(',', $fields).') ';
- $sql .= $this->buildSelectSql($options);
- return $this->execute($sql);
- }
- /**
- * Update records
- * @access public
- * @param mixed $data Data
- * @param array $options Expression
- * @return false | integer
- */
- public function update($data,$options) {
- $this->model = $options['model'];
- $sql = 'UPDATE '
- .$this->parseTable($options['table'])
- .$this->parseSet($data)
- .$this->parseWhere(!empty($options['where'])?$options['where']:'')
- .$this->parseOrder(!empty($options['order'])?$options['order']:'')
- .$this->parseLimit(!empty($options['limit'])?$options['limit']:'')
- .$this->parseLock(isset($options['lock'])?$options['lock']:false)
- .$this->parseComment(!empty($options['comment'])?$options['comment']:'');
- return $this->execute($sql);
- }
- /**
- * Delete records
- * @access public
- * @param array $options Expression
- * @return false | integer
- */
- public function delete($options=array()) {
- $this->model = $options['model'];
- $sql = 'DELETE FROM '
- .$this->parseTable($options['table'])
- .$this->parseWhere(!empty($options['where'])?$options['where']:'')
- .$this->parseOrder(!empty($options['order'])?$options['order']:'')
- .$this->parseLimit(!empty($options['limit'])?$options['limit']:'')
- .$this->parseLock(isset($options['lock'])?$options['lock']:false)
- .$this->parseComment(!empty($options['comment'])?$options['comment']:'');
- return $this->execute($sql);
- }
- /**
- * Find Record
- * @access public
- * @param array $options Expression
- * @return mixed
- */
- public function select($options=array()) {
- $this->model = $options['model'];
- $sql = $this->buildSelectSql($options);
- $cache = isset($options['cache'])?$options['cache']:false;
- if($cache) { // Query cache detection
- $key = is_string($cache['key'])?$cache['key']:md5($sql);
- $value = S($key,'',$cache);
- if(false !== $value) {
- return $value;
- }
- }
- $result = $this->query($sql);
- if($cache && false !== $result ) { // Write the query cache
- S($key,$result,$cache);
- }
- return $result;
- }
- /**
- * Generate SQL queries
- * @access public
- * @param array $options Expression
- * @return string
- */
- public function buildSelectSql($options=array()) {
- if(isset($options['page'])) {
- // According Pages calculated limit
- if(strpos($options['page'],',')) {
- list($page,$listRows) = explode(',',$options['page']);
- }else{
- $page = $options['page'];
- }
- $page = $page?$page:1;
- $listRows= isset($listRows)?$listRows:(is_numeric($options['limit'])?$options['limit']:20);
- $offset = $listRows*((int)$page-1);
- $options['limit'] = $offset.','.$listRows;
- }
- if(C('DB_SQL_BUILD_CACHE')) { // SQL to create the cache
- $key = md5(serialize($options));
- $value = S($key);
- if(false !== $value) {
- return $value;
- }
- }
- $sql = $this->parseSql($this->selectSql,$options);
- $sql .= $this->parseLock(isset($options['lock'])?$options['lock']:false);
- if(isset($key)) { // Write SQL to create the cache
- S($key,$sql,array('expire'=>0,'length'=>C('DB_SQL_BUILD_LENGTH'),'queue'=>C('DB_SQL_BUILD_QUEUE')));
- }
- return $sql;
- }
- /**
- * Replace expressions in SQL statements
- * @access public
- * @param array $options Expression
- * @return string
- */
- public function parseSql($sql,$options=array()){
- $sql = str_replace(
- array('%TABLE%','%DISTINCT%','%FIELD%','%JOIN%','%WHERE%','%GROUP%','%HAVING%','%ORDER%','%LIMIT%','%UNION%','%COMMENT%'),
- array(
- $this->parseTable($options['table']),
- $this->parseDistinct(isset($options['distinct'])?$options['distinct']:false),
- $this->parseField(!empty($options['field'])?$options['field']:'*'),
- $this->parseJoin(!empty($options['join'])?$options['join']:''),
- $this->parseWhere(!empty($options['where'])?$options['where']:''),
- $this->parseGroup(!empty($options['group'])?$options['group']:''),
- $this->parseHaving(!empty($options['having'])?$options['having']:''),
- $this->parseOrder(!empty($options['order'])?$options['order']:''),
- $this->parseLimit(!empty($options['limit'])?$options['limit']:''),
- $this->parseUnion(!empty($options['union'])?$options['union']:''),
- $this->parseComment(!empty($options['comment'])?$options['comment']:'')
- ),$sql);
- return $sql;
- }
- /**
- * Get the last query sql statement
- * @param string $model Model Name
- * @access public
- * @return string
- */
- public function getLastSql($model='') {
- return $model?$this->modelSql[$model]:$this->queryStr;
- }
- /**
- * Get the last inserted ID
- * @access public
- * @return string
- */
- public function getLastInsID() {
- return $this->lastInsID;
- }
- /**
- * Get the most recent error message
- * @access public
- * @return string
- */
- public function getError() {
- return $this->error;
- }
- /**
- * SQL commands security filtering
- * @access public
- * @param string $str SQL string
- * @return string
- */
- public function escapeString($str) {
- return addslashes($str);
- }
- /**
- * Set the current operational model
- * @access public
- * @param string $model Model Name
- * @return void
- */
- public function setModel($model){
- $this->model = $model;
- }
- /**
- * Destructor
- * @access public
- */
- public function __destruct() {
- // Release inquiries
- if ($this->queryID){
- $this->free();
- }
- // Close the connection
- $this->close();
- }
- // Close the database Defined by the driver class
- public function close(){}
- }
|