|
<?php |
|
|
|
// |
|
// Open Web Analytics - An Open Source Web Analytics Framework |
|
// |
|
// Copyright 2006 Peter Adams. All rights reserved. |
|
// |
|
// Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html |
|
// |
|
// Unless required by applicable law or agreed to in writing, software |
|
// distributed under the License is distributed on an "AS IS" BASIS, |
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
// See the License for the specific language governing permissions and |
|
// limitations under the License. |
|
// |
|
// $Id$ |
|
// |
|
|
|
require_once(OWA_BASE_DIR.DIRECTORY_SEPARATOR.'owa_base.php'); |
|
|
|
/** |
|
* Database Connection Class |
|
* |
|
* @author Peter Adams <peter@openwebanalytics.com> |
|
* @copyright Copyright © 2006 Peter Adams <peter@openwebanalytics.com> |
|
* @license http://www.gnu.org/copyleft/gpl.html GPL v2.0 |
|
* @category owa |
|
* @package owa |
|
* @version $Revision$ |
|
* @since owa 1.0.0 |
|
*/ |
|
class owa_db extends owa_base { |
|
|
|
/** |
|
* Database Connection |
|
* |
|
* @var object |
|
*/ |
|
var $connection; |
|
|
|
var $connectionParams; |
|
|
|
/** |
|
* Number of queries |
|
* |
|
* @var integer |
|
*/ |
|
var $num_queries; |
|
|
|
/** |
|
* Raw result object |
|
* |
|
* @var object |
|
*/ |
|
var $new_result; |
|
|
|
/** |
|
* Rows |
|
* |
|
* @var array |
|
*/ |
|
var $result; |
|
|
|
/** |
|
* Caller Params |
|
* |
|
* @var array |
|
*/ |
|
var $params = array(); |
|
|
|
/** |
|
* Status of selecting a databse |
|
* |
|
* @var boolean |
|
*/ |
|
var $database_selection; |
|
|
|
/** |
|
* Status of connection |
|
* |
|
* @var boolean |
|
*/ |
|
var $connection_status; |
|
|
|
/** |
|
* Number of rows in result set |
|
* |
|
* @var integer |
|
*/ |
|
var $num_rows; |
|
|
|
/** |
|
* Number of rows affected by insert/update/delete statements |
|
* |
|
* @var integer |
|
*/ |
|
var $rows_affected; |
|
|
|
/** |
|
* Microtime Start of Query |
|
* |
|
* @var unknown_type |
|
*/ |
|
var $_start_time; |
|
|
|
/** |
|
* Total Elapsed time of query |
|
* |
|
* @var unknown_type |
|
*/ |
|
var $_total_time; |
|
|
|
/** |
|
* Storage Array for components of sql queries |
|
* |
|
* @var array |
|
*/ |
|
var $_sqlParams = array(); |
|
|
|
/** |
|
* Sql Statement |
|
* |
|
* @var string |
|
*/ |
|
var $_sql_statement; |
|
|
|
/** |
|
* Last Sql Statement |
|
* |
|
* @var string |
|
*/ |
|
var $_last_sql_statement; |
|
|
|
function __construct($db_host, $db_name, $db_user, $db_password, $open_new_connection = true, $persistant = false) { |
|
|
|
$this->connectionParams = array('host' => $db_host, |
|
'user' => $db_user, |
|
'password' => $db_password, |
|
'name' => $db_name, |
|
'open_new_connection' => $open_new_connection, |
|
'persistant' => $persistant); |
|
|
|
return parent::__construct(); |
|
} |
|
|
|
function __destruct() { |
|
|
|
$this->close(); |
|
} |
|
|
|
function connect() { |
|
|
|
|
|
return false; |
|
} |
|
|
|
function pconnect() { |
|
|
|
return false; |
|
} |
|
|
|
function close() { |
|
|
|
return false; |
|
} |
|
|
|
function getConnectionParam($name) { |
|
|
|
if (array_key_exists($name, $this->connectionParams)) { |
|
return $this->connectionParams[$name]; |
|
} |
|
} |
|
|
|
/** |
|
* Prepare string |
|
* |
|
* @param string $string |
|
* @return string |
|
*/ |
|
function prepare_string($string) { |
|
|
|
$chars = array("\t", "\n"); |
|
return str_replace($chars, " ", $string); |
|
} |
|
|
|
/** |
|
* Starts the query microtimer |
|
* |
|
*/ |
|
function _timerStart() { |
|
|
|
$mtime = microtime(); |
|
//$mtime = explode(' ', $mtime); |
|
//$this->_start_time = $mtime[1].substr(round($mtime[0], 4), 1); |
|
$this->_start_time = microtime(); |
|
return; |
|
} |
|
|
|
/** |
|
* Ends the query microtimer and populates $this->_total_time |
|
* |
|
*/ |
|
function _timerEnd() { |
|
|
|
$mtime = microtime(); |
|
//$mtime = explode(" ", $mtime); |
|
//$endtime = $mtime[1].substr(round($mtime[0], 4), 1); |
|
$endtime = microtime(); |
|
//$this->_total_time = bcsub($endtime, $this->_start_time, 4); |
|
$this->_total_time = number_format(((substr($endtime,0,9)) + (substr($endtime,-10)) - (substr($this->_start_time,0,9)) - (substr($this->_start_time,-10))),6); |
|
|
|
return; |
|
|
|
} |
|
|
|
function selectColumn($name, $as = '') { |
|
|
|
if (is_array($name)) { |
|
$as = $name[1]; |
|
$name = $name[0]; |
|
} |
|
|
|
$this->_sqlParams['select_values'][] = array('name' => $name, 'as' => $as); |
|
|
|
return; |
|
} |
|
|
|
function select($name, $as = '') { |
|
return $this->selectColumn($name, $as = ''); |
|
} |
|
|
|
function where($name, $value, $operator = '') { |
|
|
|
if (empty($operator)): |
|
$operator = '='; |
|
endif; |
|
|
|
if (!empty($value)): |
|
|
|
// hack for intentional empty value |
|
if($value == ' '): |
|
$value = ''; |
|
endif; |
|
|
|
$this->_sqlParams['where'][$name] = array('name' => $name, 'value' => $value, 'operator' => $operator); |
|
endif; |
|
|
|
return; |
|
} |
|
|
|
function multiWhere($where_array = array()) { |
|
|
|
if (!empty($where_array)): |
|
|
|
foreach ($where_array as $k => $v) { |
|
if (!empty($v)): |
|
|
|
if (empty($v['operator'])): |
|
$v['operator'] = '='; |
|
endif; |
|
|
|
$this->_sqlParams['where'][$k] = array('name' => $k, 'value' => $v['value'], 'operator' => $v['operator']); |
|
endif; |
|
} |
|
|
|
endif; |
|
} |
|
|
|
function groupBy($col) { |
|
|
|
$this->_sqlParams['groupby'][] = $col; |
|
return; |
|
} |
|
|
|
function orderBy($col, $flag = '') { |
|
|
|
$this->_sqlParams['orderby'][] = array($col, $flag); |
|
return; |
|
} |
|
|
|
function order($flag) { |
|
|
|
$this->_sqlParams['order'] = $flag; |
|
return; |
|
} |
|
|
|
function limit($value) { |
|
|
|
$this->_sqlParams['limit'] = $value; |
|
return; |
|
} |
|
|
|
function offset($value) { |
|
|
|
$this->_sqlParams['offset'] = $value; |
|
return; |
|
} |
|
|
|
function set($name, $value) { |
|
|
|
$this->_sqlParams['set_values'][] = array('name' => $name, 'value' => $value); |
|
return; |
|
} |
|
|
|
function executeQuery() { |
|
|
|
switch($this->_sqlParams['query_type']) { |
|
|
|
case 'insert': |
|
|
|
return $this->_insertQuery(); |
|
|
|
case 'select': |
|
|
|
return $this->_selectQuery(); |
|
|
|
case 'update': |
|
|
|
return $this->_updateQuery(); |
|
|
|
case 'delete': |
|
|
|
return $this->_deleteQuery(); |
|
|
|
default: |
|
|
|
return $this->_query(); |
|
} |
|
} |
|
|
|
function getAllRows() { |
|
|
|
return $this->_selectQuery(); |
|
} |
|
|
|
function getOneRow() { |
|
|
|
$ret = $this->_selectQuery(); |
|
return $ret[0]; |
|
} |
|
|
|
function _setSql($sql) { |
|
$this->_sql_statement = $sql; |
|
} |
|
|
|
function selectFrom($name, $as = '') { |
|
|
|
if (is_array($name)) { |
|
$as = $name[1]; |
|
$name = $name[0]; |
|
} |
|
|
|
$this->_sqlParams['query_type'] = 'select'; |
|
$this->_sqlParams['from'][$name] = array('name' => $name, 'as' => $as); |
|
} |
|
|
|
function from($name, $as = '') { |
|
return $this->selectFrom($name, $as = ''); |
|
} |
|
|
|
function insertInto($table) { |
|
|
|
$this->_sqlParams['query_type'] = 'insert'; |
|
$this->_sqlParams['table'] = $table; |
|
} |
|
|
|
function deleteFrom($table) { |
|
|
|
$this->_sqlParams['query_type'] = 'delete'; |
|
$this->_sqlParams['table'] = $table; |
|
} |
|
|
|
function updateTable($table) { |
|
|
|
$this->_sqlParams['query_type'] = 'update'; |
|
$this->_sqlParams['table'] = $table; |
|
} |
|
|
|
function _insertQuery() { |
|
owa_coreAPI::profile($this, __FUNCTION__, __LINE__); |
|
$params = $this->_fetchSqlParams('set_values'); |
|
|
|
$count = count($params); |
|
|
|
$i = 0; |
|
|
|
$sql_cols = ''; |
|
$sql_values = ''; |
|
|
|
foreach ($params as $k => $v) { |
|
|
|
$sql_cols .= $v['name']; |
|
$sql_values .= "'".$this->prepare($v['value'])."'"; |
|
|
|
$i++; |
|
|
|
// Add commas |
|
if ($i < $count): |
|
|
|
$sql_cols .= ", "; |
|
$sql_values .= ", "; |
|
|
|
endif; |
|
} |
|
owa_coreAPI::profile($this, __FUNCTION__, __LINE__); |
|
$this->_setSql(sprintf(OWA_SQL_INSERT_ROW, $this->_sqlParams['table'], $sql_cols, $sql_values)); |
|
owa_coreAPI::profile($this, __FUNCTION__, __LINE__); |
|
$ret = $this->_query(); |
|
owa_coreAPI::profile($this, __FUNCTION__, __LINE__); |
|
return $ret; |
|
|
|
} |
|
|
|
function _selectQuery() { |
|
|
|