Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once(dirname(__FILE__) . '/../../conf/paths.inc.php');
- require_once(dirname(__FILE__) . '/core/MySQLDatabase.class.php');
- require_once(dirname(__FILE__) . '/core/QueryIterator.class.php');
- require_once(dirname(__FILE__) . '/../constants.inc.php');
- define('NEW_LINE', '\n');
- define('BREAK', '<br />');
- define('RECORD_ID_FIELD_NAME', 'record_id_field_name');
- define('EMPTY_DB_DATE', '0000-00-00');
- class BaseDao {
- var $conn = null;
- function BaseDao() {
- $this->connect();
- }
- function getRows($sql, $params, $test=false){
- if(!is_array($params)){
- $params = array($params);
- }
- $this->prepare($sql, $params);
- if($test){
- Util::doTest(array($sql, $params));
- }
- if(!$this->getConnected()){
- $this->connect();
- }
- $queryResult = $this->conn->query($sql);
- $it = new QueryIterator($queryResult);
- $rows = array();
- while ($it->hasNext()) {
- $row = $it->next();
- $rows[] = $row;
- }
- return $rows;
- }
- function getListCount($where, $field=null){
- $this->connect();
- $field = (null != $field) ? $field : 'ID';
- $sql = 'select '
- . ' count('.$field.') as retval '
- . $where
- . '';
- $queryResult = $this->conn->query($sql);
- $it = new QueryIterator($queryResult);
- while ($it->hasNext()) {
- $row = $it->next();
- return $row['retval'];
- }
- }
- function buildListFromQuery($sql, $object){
- $queryResult = $this->conn->query($sql);
- $it = new QueryIterator($queryResult);
- $objectName = ucfirst(get_class($object));
- $array = array();
- while ($it->hasNext()) {
- $row = $it->next();
- $object = new $objectName();
- BeanMapper::populateBean($object, $row);
- $array[] = $object;
- }
- return $array;
- }
- function connect() {
- if (!class_exists('BootstrapConfig')) {
- require_once(dirname(__FILE__) . '/SimpleConfig.class.php');
- }
- $bootstrap = BootstrapConfig::getInstance();
- if (null == $this->conn || !$this->conn->isConnected()) {
- require_once(dirname(__FILE__) . '/Util.class.php');
- $dbHost = $bootstrap->getValue(DB_HOST);
- $userName = $bootstrap->getValue(DB_USER);
- $password = $bootstrap->getValue(DB_PASS);
- $dbName = $bootstrap->getValue(DB_NAME);
- $this->conn =& MySQLDatabase::getInstance($dbName, $dbHost);
- if (!$this->conn->connect($userName, $password)) {
- trigger_error($this->conn->getError(), E_USER_ERROR);
- }
- }
- }
- function disconnect() {
- $this->conn->disconnect();
- }
- function buildList($arr) {
- $first = true;
- foreach ($arr as $val) {
- if (!$first) {
- $sql .= ',';
- }
- $sql = $sql . "'" . $val . "'";
- $first = false;
- }
- return $sql;
- }
- function setString($sql, $val) {
- if (!get_magic_quotes_gpc()) {
- // This is for things like "c:\test"
- $val = preg_replace("/\\\\/", "\\\\\\", $val);
- }
- $tempval = $val;
- // This escapes $ in insert values. preg_replace thinks it's a backreference otherwise.
- $val = preg_replace('/\$/i', '\\\\$', $tempval);
- $val = '\'' . $val . '\'';
- $sql = preg_replace('/\?/', $val, $sql, 1);
- return $sql;
- }
- function setNumber($sql, $val) {
- if (is_numeric($val)) {
- $sql = preg_replace("/\?/", $val, $sql, 1);
- }
- return $sql;
- }
- function setNull($sql) {
- $sql = preg_replace("/\?/", "null", $sql, 1);
- return $sql;
- }
- function setString2($sql, $val) {
- if (!get_magic_quotes_gpc()) {
- // This is for things like "c:\test"
- $val = preg_replace("/\\\\/", "\\\\\\", $val);
- }
- $tempval = $val;
- // This escapes $ in insert values. preg_replace thinks it's a backreference otherwise.
- $val = preg_replace('/\$/i', '\\\\$', $tempval);
- $val = '\'' . $val . '\'';
- $sql = preg_replace('/\|/', $val, $sql, 1);
- return $sql;
- }
- function setNull2($sql) {
- $sql = preg_replace("/\|/", "null", $sql, 1);
- return $sql;
- }
- function prepare2(&$sql, $paramsArr) {
- $isArray = is_array($paramsArr);
- $isEmptyArray = (0 == count($paramsArr));
- if (!$isArray || $isEmptyArray) {
- return;
- }
- $localSql = $sql;
- $counter = 0;
- for ($i=0; $i<strlen($sql); $i++) {
- if ('|' == $sql[$i]) {
- if (null === $paramsArr[$counter]) {
- $localSql = $this->setNull2($localSql);
- // PHP autoconverts strings to numbers, so a string like 08000 is recognized
- // as the number 8000. This is not what we want in the case of zips and elsewhere.
- /*} elseif (is_numeric($paramsArr[$counter])) {
- $localSql = $this->setNumber($localSql, $paramsArr[$counter]);*/
- } else {
- $paramsArr[$counter] = str_replace('|', '', $paramsArr[$counter]);
- if (get_magic_quotes_gpc()) {
- $fieldValue = stripslashes($paramsArr[$counter]);
- }
- $fieldValue = Util::mysql_real_escape_string($paramsArr[$counter]);
- $localSql = $this->setString2($localSql, $fieldValue);
- }
- $counter++;
- }
- }
- $sql = $localSql;
- }
- /**
- * Right now it just replaces ? w the corresponding value in the $paramsArr.
- */
- function prepare(&$sql, $paramsArr) {
- return $this->prepare3($sql, $paramsArr);
- }
- function createTempData($token, $val, $key=null) {
- $sql = 'insert into ' .
- ' se_session ' .
- ' ( ' .
- ' token ' .
- ' ,`key` ' .
- ' ,val ' .
- ' ,date ' .
- ' ) ' .
- ' VALUES ' .
- ' (?, ?, ?, sysdate()) ';
- $this->connect();
- $params = array();
- $params[] = $token;
- $params[] = $key;
- $params[] = $val;
- $this->prepare3($sql, $params);
- return $this->conn->query($sql);
- }
- function retrieveTempData($token, $key=null) {
- require_once(dirname(__FILE__) . '/core/QueryIterator.class.php');
- $sql = ' select distinct val ' .
- ' from se_session ' .
- ' where token = ? ';
- if (isset($key) && $key) {
- $sql .= ' and `key` = ? ';
- }
- $params[] = $token;
- if (isset($key) && $key) {
- $params[] = $key;
- }
- $this->connect();
- $this->prepare3($sql, $params);
- $queryResult = $this->conn->query($sql);
- $it = new QueryIterator($queryResult);
- $retVal = null;
- while ($it->hasNext()) {
- $row = $it->next();
- $retVal = $row['val'];
- }
- return $retVal;
- }
- function updateTempData($token, $val, $key=null) {
- $this->connect();
- $sql = ' update se_session ' .
- ' set val = ? ' .
- ' where token = ? ';
- $data[] = $val;
- $data[] = $token;
- if (isset($key) && $key) {
- $sql .= ' and `key` = ? ';
- $data[] = $key;
- }
- $this->prepare($sql, $data);
- return $this->conn->query($sql);
- }
- /**
- * Gets the id of the record based on the unique parameters in the given sql.
- */
- function getRecordId($sql, $params) {
- $this->connect();
- $this->prepare($sql, $params);
- $queryResult = $this->conn->query($sql);
- $id = null;
- if ($queryResult && 0 < $queryResult->getRowCount()) {
- require_once(dirname(__FILE__) . '/core/QueryIterator.class.php');
- $it = new QueryIterator($queryResult);
- while ($it->hasNext()) {
- $row = $it->next();
- $id = $row[RECORD_ID_FIELD_NAME];
- }
- }
- return $id;
- }
- function basicQuery($sql, $params=null, $test=false) {
- $this->connect();
- $this->prepare($sql, $params);
- if ($test) {
- print $sql;
- exit;
- }
- $succeeded = $this->conn->query($sql);
- if (is_bool($succeeded) && !$succeeded) {
- trigger_error('Database Action Failed', E_USER_ERROR);
- //require_once(dirname(__FILE__) . '/Exception.class.php');
- //require_once('lib/constants.errors.inc.php');
- //Exception::throw(DATABASE_ACTION_FAILED);
- }
- return $succeeded;
- }
- function unlockTables() {
- global $config;
- if (!($config->getBoolValue('lockTables'))) {
- return;
- }
- $sql = 'UNLOCK TABLES';
- $this->conn->query($sql);
- }
- function lockTable($table) {
- global $config;
- if (!($config->getBoolValue('lockTables'))) {
- return;
- }
- if ($table) {
- $this->connect();
- $sql = "LOCK TABLE $table WRITE";
- $this->conn->query($sql);
- }
- }
- /*
- * The prepare3 function is the same as the prepare function in that it takes a string with ?'s and
- * replaces them with the corresponding values in the paramsArr. It is unlike prepare in that if
- * the paramsArr contains a param with a ? in it that gets left alone and it will not replace the
- * params ? with the next param. This basically means that you can dump any characters into here
- * and it will work fine.
- */
- function prepare3(&$sql, $paramsArr, $leaveChars=null, $replaceChar=null, $test=null) {
- $length = strlen($sql);
- $markers = array();
- $current = 0;
- $replaceChar = $replaceChar ? $replaceChar : '?';
- for($i=0;$i<$length;$i++){
- if($sql[$i] == $replaceChar){
- $skipQuotes = ($paramsArr[$current] && is_array($paramsArr[$current])) ? $paramsArr[$current][1] : null;
- $paramsArr[$current] = ($paramsArr[$current] && is_array($paramsArr[$current])) ? $paramsArr[$current][0] : $paramsArr[$current];
- if(!$leaveChars){
- if (get_magic_quotes_gpc()) {
- $paramsArr[$current] = stripslashes($paramsArr[$current]);
- }
- $value = addslashes($paramsArr[$current]);
- } else {
- $value = $paramsArr[$current];
- }
- $value = !isset($value) ? 'null' : $value;
- $value = $value != 'null' ? ($skipQuotes ? $value : '\'' . $value . '\'') : $value;
- $markers[$i] = array('length' => strlen($value) - 1, 'value' => $value);
- $current++;
- }
- }
- if($markers && is_array($markers) && count($markers) > 0){
- $previousLength = 0;
- foreach($markers as $start => $marker){
- $sql = substr_replace($sql, $marker['value'], $start + $previousLength, 1);
- $previousLength += $marker['length'];
- }
- }
- }
- function setConnected($value) {$this->connected = $value;}
- function getConnected() {return $this->connected;}
- }
- ?>
Add Comment
Please, Sign In to add comment