From 9e18976446597126ddcdfbd6f111d437cdee5e19 Mon Sep 17 00:00:00 2001 From: Jeongkyu Shin Date: Thu, 12 Mar 2015 01:46:18 +0900 Subject: [PATCH] refs #1753 : modified - DBModel to accept more types. - --- framework/data/Cubrid/Adapter.php | 451 ++++++++++++++++++++++++++ framework/data/MySQL/Adapter.php | 389 ++++++++++++++++++++++ framework/data/MySQLi/Adapter.php | 379 ++++++++++++++++++++++ framework/data/PostgreSQL/Adapter.php | 84 +++++ framework/data/SQLite3/Adapter.php | 3 + 5 files changed, 1306 insertions(+) diff --git a/framework/data/Cubrid/Adapter.php b/framework/data/Cubrid/Adapter.php index 5628ddc4f..70b153d59 100644 --- a/framework/data/Cubrid/Adapter.php +++ b/framework/data/Cubrid/Adapter.php @@ -7,6 +7,7 @@ global $fileCachedResult; +<<<<<<< HEAD class DBAdapter implements IAdapter { static $dbProperties, $cachedResult,$lastQueryType; /*@static@*/ @@ -424,6 +425,456 @@ public static function fieldType($abstractType) { "timestamp" => "integer", "mediumtext" => "varchar(512)", "text" => "text"); +======= +class DBAdapter implements IAdapter { + static $dbProperties, $cachedResult, $lastQueryType; + + /*@static@*/ + public static function bind($database) { + // Connects DB and set environment variables + // $database array should contain 'server','username','password'. + if (!isset($database) || empty($database)) { + return false; + } + $handle = @cubrid_connect($database['server'], intval($database['port']), $database['database'], $database['username'], $database['password']); + if (!$handle) { + return false; + } + self::$dbProperties['handle'] = $handle; // Keeping handle + self::$dbProperties['charset'] = 'utf8'; + return true; + } + + public static function unbind() { + @cubrid_commit(self::$dbProperties['handle']); + cubrid_disconnect(self::$dbProperties['handle']); + return true; + } + + public static function charset() { + if (array_key_exists('charset', self::$dbProperties)) { + return self::$dbProperties['charset']; + } else { + return null; + } + } + + public static function dbms() { + return 'Cubrid'; + } + + public static function version($mode = 'server') { + if (array_key_exists('version', self::$dbProperties)) { + return self::$dbProperties['version']; + } else { + self::$dbProperties['version'] = cubrid_version(); + return self::$dbProperties['version']; + } + } + + public static function tableList($condition = null) { + if (!array_key_exists('tableList', self::$dbProperties)) { + self::$dbProperties['tableList'] = self::queryColumn("SELECT class_name FROM db_class WHERE is_system_class = 'NO'"); + } + if (!is_null($condition)) { + $result = array(); + foreach (self::$dbProperties['tableList'] as $item) { + if (strpos($item, $condition) === 0) { + array_push($result, $item); + } + } + return $result; + } else { + return self::$dbProperties['tableList']; + } + } + + public static function reservedFieldNames() { + return array('date', 'value', 'data', 'count', 'year', 'month', 'type', 'size'); + } + + public static function reservedFunctionNames() { + return array('UNIX_TIMESTAMP()'); + } + + public static function setTimezone($time) { + return true; + return self::query('SET time_zone = \'' . Timezone::getCanonical() . '\''); + } + + /*@static@*/ + public static function query($query, $compatibility = true) { + /// Bypassing compatiblitiy issue : will be replace to NAF2. + if ($compatibility) { + $query = str_replace('UNIX_TIMESTAMP()', Timestamp::getUNIXtime(), $query); // compatibility issue. + if (stripos($query, "ORDER BY") !== false) { + $origPagingInst = array( + '/(ASC|DESC) LIMIT ([0-9]+) OFFSET 0/si', + '/(ASC|DESC) LIMIT ([0-9]+) OFFSET ([0-9]+)/si', + '/(ASC|DESC) LIMIT 1(^[0-9])/si', + '/(ASC|DESC) LIMIT ([0-9]+)/si', + '/RAND\(\) LIMIT ([0-9]+)/si' + ); + $descPagingInst = array( + '$1 FOR ORDERBY_NUM() BETWEEN 1 AND $2', + '$1 FOR ORDERBY_NUM() BETWEEN ($3+1) AND ($2+$3)', + '$1 FOR ORDERBY_NUM() = 1', + '$1 FOR ORDERBY_NUM() BETWEEN 1 AND $2', + 'RANDOM() FOR ORDERBY_NUM() BETWEEN 1 AND $1' + ); + } else { + if (stripos($query, "GROUP BY") !== false) { + $origPagingInst = array( + '/GROUP BY(.*)(ORDER BY)(.*)(ASC|DESC) LIMIT ([0-9]+) OFFSET 0/si', + '/GROUP BY(.*)(ORDER BY)(.*)(ASC|DESC) LIMIT ([0-9]+) OFFSET ([0-9]+)/si', + '/GROUP BY(.*)(ORDER BY)(.*)(ASC|DESC) LIMIT 1(^[0-9])/si', + '/GROUP BY(.*)(ORDER BY)(.*)(ASC|DESC) LIMIT ([0-9]+)/si', + '/GROUP BY(.*)(ORDER BY)(.*)RAND\(\) LIMIT ([0-9]+)/si' + ); + $descPagingInst = array( + 'GROUP BY $1 HAVING GROUPBY_NUM() = $5 $2 $3 $4', + 'GROUP BY $1 HAVING GROUPBY_NUM() BETWEEN ($6+1) AND $5 $2 $3 $4', + 'GROUP BY $1 HAVING GROUPBY_NUM() = 1 $2 $3 $4', + 'GROUP BY $1 HAVING GROUPBY_NUM() BETWEEN 1 AND $5 $2 $3 $4', + 'GROUP BY $1 HAVING GROUPBY_NUM() BETWEEN 1 AND $4 $2 RANDOM() $3' + ); + } else { + $origPagingInst = array( + '/WHERE(.*)LIMIT ([0-9]+) OFFSET 0/si', + '/WHERE(.*)LIMIT ([0-9]+) OFFSET ([0-9]+)/si', + '/WHERE(.*)LIMIT 1(^[0-9])/si', + '/WHERE(.*)LIMIT ([0-9]+)/si', + '/SUM\((size|value)\)/si' + ); + $descPagingInst = array( + 'WHERE ROWNUM BETWEEN 1 AND $2 AND $1', + 'WHERE ROWNUM BETWEEN ($3+1) AND ($2+$3) AND $1', + 'WHERE ROWNUM = 1 AND $1', + 'WHERE ROWNUM BETWEEN 1 AND $2 AND $1', + 'SUM("$1")' + ); + } + } + $query = preg_replace($origPagingInst, $descPagingInst, $query); + + // CONCAT + $ppos = -1; + $length = strlen($query); + do { + $pos = strpos($query, '\'', $ppos + 1); + if ($pos === false) { + $pos = strlen($query); + } + + while (true) { + $concat = stripos($query, 'CONCAT', $ppos + 1); + if ($concat === false || $concat >= $pos) { + break; + } + + $depth = 0; + $quote = null; + for ($i = $concat + 6; $i < $length; $i++) { + if ($quote === null) { + if ($query[$i] == '\'' || $query[$i] == '"') { + $quote = $query[$i]; + } elseif ($query[$i] == ',') { + $query = substr($query, 0, $i) . ' || ' . substr($query, $i + 1); + } elseif ($query[$i] == '(') { + $depth++; + } elseif ($query[$i] == ')') { + if (--$depth == 0) { + break; + } + } + } else { + if ($query[$i] == $quote && $query[$i - 1] != '\\') { + $quote = null; + } + } + } + $query = substr($query, 0, $concat) . substr($query, $concat + 6); + + $pos = strpos($query, '\'', $ppos + 1); + $length = strlen($query); + } + + $ppos = $pos; + while ($ppos < $length) { + $ppos = strpos($query, '\'', $ppos + 1); + if ($query[$ppos - 1] != '\\') { + break; + } + } + } while ($ppos < $length); + } + + if (function_exists('__tcSqlLogBegin')) { + __tcSqlLogBegin($query); + $result = cubrid_execute(self::$dbProperties['handle'], $query); + __tcSqlLogEnd($result, 0); + } else { + $result = cubrid_execute(self::$dbProperties['handle'], $query); + } + self::$lastQueryType = strtolower(substr($query, 0, 6)); + if (in_array(self::$lastQueryType, array('insert', 'update', 'delete', 'replac'))) { + self::commit(); + self::clearCache(); + } + return $result; + } + + /*@static@*/ + public static function queryExistence($query) { + if ($result = self::query($query)) { + if (cubrid_num_rows($result) > 0) { + cubrid_close_request($result); + return true; + } + cubrid_close_request($result); + } + return false; + } + + /*@static@*/ + public static function queryCount($query) { + $count = 0; + $query = trim($query); + if ($result = self::query($query)) { + $operation = strtolower(substr($query, 0, 6)); + self::$lastQueryType = $operation; + switch ($operation) { + case 'select': + $count = cubrid_num_rows($result); + cubrid_close_request($result); + break; + case 'insert': + case 'update': + case 'delete': + case 'replac': + default: + $count = cubrid_affected_rows($result); + break; + } + } + return $count; + } + + /*@static@*/ + public static function queryCell($query, $field = 0, $useCache = true) { + $type = 'both'; + if (is_numeric($field)) { + $type = 'num'; + } else { + $type = 'assoc'; + } + + if ($useCache) { + $result = self::queryAllWithCache($query, $type); + } else { + $result = self::queryAllWithoutCache($query, $type); + } + if (empty($result)) { + return null; + } + return $result[0][$field]; + } + + /*@static@*/ + public static function queryRow($query, $type = 'both', $useCache = true) { + if ($useCache) { + $result = self::queryAllWithCache($query, $type, 1); + } else { + $result = self::queryAllWithoutCache($query, $type, 1); + } + if (empty($result)) { + return null; + } + return $result[0]; + } + + /*@static@*/ + public static function queryColumn($query, $useCache = true) { + $cacheKey = "{$query}_queryColumn"; + if ($useCache && isset(self::$cachedResult[$cacheKey])) { + if (function_exists('__tcSqlLogBegin')) { + __tcSqlLogBegin($query); + __tcSqlLogEnd(null, 1); + } + self::$cachedResult[$cacheKey][0]++; + return self::$cachedResult[$cacheKey][1]; + } + + $column = null; + if ($result = self::query($query)) { + $column = array(); + while ($row = cubrid_fetch($result)) + array_push($column, $row[0]); + cubrid_close_request($result); + } + + if ($useCache) { + self::$cachedResult[$cacheKey] = array(1, $column); + } + return $column; + } + + /*@static@*/ + public static function queryAll($query, $type = 'both', $count = -1) { + return self::queryAllWithCache($query, $type, $count); + //return self::queryAllWithoutCache($query, $type, $count); // Your choice. :) + } + + public static function queryAllWithoutCache($query, $type = 'both', $count = -1) { + $all = array(); + $realtype = self::__queryType($type); + if ($result = self::query($query)) { + while (($count-- != 0) && $row = cubrid_fetch($result, $realtype)) + array_push($all, $row); + cubrid_close_request($result); + return $all; + } + return null; + } + + public static function queryAllWithCache($query, $type = 'both', $count = -1) { + $cacheKey = "{$query}_{$type}_{$count}"; + if (isset(self::$cachedResult[$cacheKey])) { + if (function_exists('__tcSqlLogBegin')) { + __tcSqlLogBegin($query); + __tcSqlLogEnd(null, 1); + } + self::$cachedResult[$cacheKey][0]++; + return self::$cachedResult[$cacheKey][1]; + } + $all = self::queryAllWithoutCache($query, $type, $count); + self::$cachedResult[$cacheKey] = array(1, $all); + return $all; + } + + /*@static@*/ + public static function execute($query) { + return self::query($query) ? true : false; + } + + /*@static@*/ + public static function multiQuery() { + $result = false; + foreach (func_get_args() as $query) { + if (is_array($query)) { + foreach ($query as $subquery) + if (($result = self::query($subquery)) === false) { + return false; + } + } else { + if (($result = self::query($query)) === false) { + return false; + } + } + } + return $result; + } + + public static function insertId() { + return cubrid_insert_id(); + } + + public static function escapeString($string, $link = null) { + return preg_replace("/'/", "''", $string); + } + + public static function clearCache() { + self::$cachedResult = array(); + if (function_exists('__tcSqlLogBegin')) { + __tcSqlLogBegin("Cache cleared"); + __tcSqlLogEnd(null, 2); + } + } + + public static function cacheLoad() { + global $fileCachedResult; + } + + public static function cacheSave() { + @self::commit(); + } + + public static function commit() { + @cubrid_commit(self::$dbProperties['handle']); + } + + /* Raw public static functions (to easier adoptation) */ + /*@static@*/ + public static function num_rows($handle = null) { + switch (self::$lastQueryType) { + case 'select': + return cubrid_num_rows($handle); + break; + default: + return cubrid_affected_rows($handle); + break; + } + return null; + } + + /*@static@*/ + public static function free($handle = null) { + cubrid_close_request($handle); + } + + /*@static@*/ + public static function fetch($handle = null, $type = 'assoc') { + $realtype = self::__queryType($type); + return cubrid_fetch($handle, $realtype); + } + + /*@static@*/ + public static function error($err = null) { + if ($err === null) { + return cubrid_error(); + } else { + return cubrid_error($err); + } + } + + /*@static@*/ + public static function stat($stat = null) { + if ($stat === null) { + return cubrid_stat(); + } else { + return cubrid_stat($stat); + } + } + + /*@static@*/ + public static function __queryType($type) { + switch (strtolower($type)) { + case 'num': + return CUBRID_NUM; + case 'assoc': + return CUBRID_ASSOC; + case 'both': + default: + return CUBRID_BOTH; + } + } + + public static function fieldType($abstractType) { + if (isset(self::$typeTable[$abstractType])) { + return self::$typeTable[$abstractType]; + } + } + + static $typeTable = array( + "integer" => "integer", + "int" => "integer", + "float" => "float", + "double" => "float", + "timestamp" => "integer", + "mediumtext" => "varchar(512)", + "varchar" => "varchar", + "text" => "text"); +>>>>>>> d3cefd6... refs #1753 : modified - DBModel to accept more types. } ?> diff --git a/framework/data/MySQL/Adapter.php b/framework/data/MySQL/Adapter.php index bae407b9e..30e1e5f30 100644 --- a/framework/data/MySQL/Adapter.php +++ b/framework/data/MySQL/Adapter.php @@ -8,6 +8,7 @@ global $fileCachedResult; class DBAdapter implements IAdapter { +<<<<<<< HEAD static $db = null; static $cachedResult, $dbProperties, $escapeTag, $lastQueryType; @@ -351,5 +352,393 @@ public static function fieldType($abstractType) { "mediumtext" => "mediumtext", "text" => "text"); +======= + static $db = null; + static $cachedResult, $dbProperties, $escapeTag, $lastQueryType; + + /*@static@*/ + public static function bind($database) { + self::$cachedResult = self::$dbProperties = array(); + // Connects DB and set environment variables + // $database array should contain 'server','username','password'. + if (!isset($database) || empty($database)) { + return false; + } + self::$db = @mysql_connect($database['server'] . (isset($database['port']) ? ':' . intval($database['port']) : ''), $database['username'], $database['password']); + if (!self::$db) { + return false; + } + self::$db = @mysql_select_db($database['database']); + if (!self::$db) { + return false; + } + + if (self::query('SET CHARACTER SET utf8')) { + self::$dbProperties['charset'] = 'utf8'; + } else { + self::$dbProperties['charset'] = 'default'; + } + @self::query('SET SESSION collation_connection = \'utf8_general_ci\''); + return true; + } + + public static function unbind() { + mysql_close(); + return true; + } + + public static function charset() { + if (array_key_exists('charset', self::$dbProperties)) { + return self::$dbProperties['charset']; + } else { + return null; + } + } + + public static function dbms() { + return 'MySQL'; + } + + public static function version($mode = 'server') { + if (array_key_exists('version', self::$dbProperties)) { + return self::$dbProperties['version']; + } else { + self::$dbProperties['version'] = self::queryCell("SELECT VERSION()"); + return self::$dbProperties['version']; + } + } + + public static function tableList($condition = null) { + if (!array_key_exists('tableList', self::$dbProperties)) { + $tableData = self::queryAll('SHOW TABLES'); + self::$dbProperties['tableList'] = array(); + foreach ($tableData as $tbl) { + array_push(self::$dbProperties['tableList'], $tbl[0]); + } + } + $result = array(); + if (!is_null($condition)) { + $result = array(); + foreach (self::$dbProperties['tableList'] as $item) { + if (strpos($item, $condition) === 0) { + array_push($result, $item); + } + } + return $result; + } else { + return self::$dbProperties['tableList']; + } + } + + public static function setTimezone($time) { + return self::query('SET time_zone = \'' . Timezone::getCanonical() . '\''); + } + + public static function reservedFieldNames() { + return null; + } + + public static function reservedFunctionNames() { + return array('UNIX_TIMESTAMP()'); + } + + /*@static@*/ + public static function queryExistence($query) { + if ($result = self::query($query)) { + if (mysql_num_rows($result) > 0) { + mysql_free_result($result); + return true; + } + mysql_free_result($result); + } + return false; + } + + /*@static@*/ + public static function queryCount($query) { + $count = 0; + $query = trim($query); + if ($result = self::query($query)) { + $operation = strtolower(substr($query, 0, 6)); + self::$lastQueryType = $operation; + switch ($operation) { + case 'select': + $count = mysql_num_rows($result); + mysql_free_result($result); + break; + case 'insert': + case 'update': + case 'delete': + case 'replac': + default: + $count = mysql_affected_rows(); + //mysql_free_result(); + break; + } + } + return $count; + } + + /*@static@*/ + public static function queryCell($query, $field = 0, $useCache = true) { + $type = 'both'; + if (is_numeric($field)) { + $type = 'num'; + } else { + $type = 'assoc'; + } + + if ($useCache) { + $result = self::queryAllWithCache($query, $type); + } else { + $result = self::queryAllWithoutCache($query, $type); + } + if (empty($result)) { + return null; + } + return $result[0][$field]; + } + + /*@static@*/ + public static function queryRow($query, $type = 'both', $useCache = true) { + if ($useCache) { + $result = self::queryAllWithCache($query, $type, 1); + } else { + $result = self::queryAllWithoutCache($query, $type, 1); + } + if (empty($result)) { + return null; + } + return $result[0]; + } + + /*@static@*/ + public static function queryColumn($query, $useCache = true) { + $cacheKey = "{$query}_queryColumn"; + if ($useCache && isset(self::$cachedResult[$cacheKey])) { + if (function_exists('__tcSqlLogBegin')) { + __tcSqlLogBegin($query); + __tcSqlLogEnd(null, 1); + } + self::$cachedResult[$cacheKey][0]++; + return self::$cachedResult[$cacheKey][1]; + } + + $column = null; + if ($result = self::query($query)) { + $column = array(); + while ($row = mysql_fetch_row($result)) + array_push($column, $row[0]); + mysql_free_result($result); + } + + if ($useCache) { + self::$cachedResult[$cacheKey] = array(1, $column); + } + return $column; + } + + /*@static@*/ + public static function queryAll($query, $type = 'both', $count = -1) { + return self::queryAllWithCache($query, $type, $count); + //return self::queryAllWithoutCache($query, $type, $count); // Your choice. :) + } + + public static function queryAllWithoutCache($query, $type = 'both', $count = -1) { + $all = array(); + $realtype = self::__queryType($type); + if ($result = self::query($query)) { + if (is_resource($result)) { + while (($count-- != 0) && $row = mysql_fetch_array($result, $realtype)) + array_push($all, $row); + mysql_free_result($result); + return $all; + } else { + return $result; + } + } + return null; + } + + public static function queryAllWithCache($query, $type = 'both', $count = -1) { + $cacheKey = "{$query}_{$type}_{$count}"; + if (isset(self::$cachedResult[$cacheKey])) { + if (function_exists('__tcSqlLogBegin')) { + __tcSqlLogBegin($query); + __tcSqlLogEnd(null, 1); + } + self::$cachedResult[$cacheKey][0]++; + return self::$cachedResult[$cacheKey][1]; + } + $all = self::queryAllWithoutCache($query, $type, $count); + self::$cachedResult[$cacheKey] = array(1, $all); + return $all; + } + + /*@static@*/ + public static function execute($query) { + return self::query($query) ? true : false; + } + + /*@static@*/ + public static function multiQuery() { + $result = false; + foreach (func_get_args() as $query) { + if (is_array($query)) { + foreach ($query as $subquery) + if (($result = self::query($subquery)) === false) { + return false; + } + } else { + if (($result = self::query($query)) === false) { + return false; + } + } + } + return $result; + } + + /*@static@*/ + public static function query($query) { + if (function_exists('__tcSqlLogBegin')) { + __tcSqlLogBegin($query); + $result = mysql_query($query); + __tcSqlLogEnd($result, 0); + } else { + $result = mysql_query($query); + } + self::$lastQueryType = strtolower(substr($query, 0, 6)); + if (stristr($query, 'update ') || + stristr($query, 'insert ') || + stristr($query, 'delete ') || + stristr($query, 'replace ') + ) { + self::clearCache(); + } + return $result; + } + + public static function insertId() { + return mysql_insert_id(); + } + + public static function escapeString($string, $link = null) { + if (!self::$db) { + return mysql_escape_string($string); + } + if (is_null(self::$escapeTag)) { + if (function_exists('mysql_real_escape_string') && (mysql_real_escape_string('ㅋ') == 'ㅋ')) { + self::$escapeTag = 'real'; + } else { + self::$escapeTag = 'none'; + } + } + if (self::$escapeTag == 'real') { + return is_null($link) ? mysql_real_escape_string($string) : mysql_real_escape_string($string, $link); + } else { + return mysql_escape_string($string); + } + } + + public static function clearCache() { + self::$cachedResult = array(); + if (function_exists('__tcSqlLogBegin')) { + __tcSqlLogBegin("Cache cleared"); + __tcSqlLogEnd(null, 2); + } + } + + public static function cacheLoad() { + global $fileCachedResult; + } + + public static function cacheSave() { + global $fileCachedResult; + } + + public static function commit() { + return true; // Auto commit. + } + /* Raw public static functions (to easier adoptation) */ + /*@static@*/ + public static function num_rows($handle = null) { + switch (self::$lastQueryType) { + case 'select': + return mysql_num_rows($handle); + break; + default: + return mysql_affected_rows($handle); + break; + } + return null; + } + + /*@static@*/ + public static function free($handle = null) { + mysql_free_result($handle); + } + + /*@static@*/ + public static function fetch($handle = null, $type = 'assoc') { + if ($type == 'array') { + return mysql_fetch_array($handle); + } // Can I use mysql_fetch_row instead? + else { + if ($type == 'row') { + return mysql_fetch_row($handle); + } else { + return mysql_fetch_assoc($handle); + } + } + } + + /*@static@*/ + public static function error($err = null) { + if ($err === null) { + return mysql_error(); + } else { + return mysql_error($err); + } + } + + /*@static@*/ + public static function stat($stat = null) { + if ($stat === null) { + return mysql_stat(); + } else { + return mysql_stat($stat); + } + } + + /*@static@*/ + public static function __queryType($type) { + switch (strtolower($type)) { + case 'num': + return MYSQL_NUM; + case 'assoc': + return MYSQL_ASSOC; + case 'both': + default: + return MYSQL_BOTH; + } + } + + public static function fieldType($abstractType) { + if (isset(self::$typeTable[$abstractType])) { + return self::$typeTable[$abstractType]; + } + } + + public static $typeTable = array( + "integer" => "int", + "int" => "int", + "float" => "float", + "double" => "double", + "timestamp" => "int", + "mediumtext" => "mediumtext", + "vartext" => "vartext", + "text" => "text"); + +>>>>>>> d3cefd6... refs #1753 : modified - DBModel to accept more types. } ?> diff --git a/framework/data/MySQLi/Adapter.php b/framework/data/MySQLi/Adapter.php index 78d956bdf..6849e3333 100644 --- a/framework/data/MySQLi/Adapter.php +++ b/framework/data/MySQLi/Adapter.php @@ -7,6 +7,7 @@ global $fileCachedResult; +<<<<<<< HEAD class DBAdapter implements IAdapter { static $db; static $cachedResult, $dbProperties, $escapeTag, $lastQueryType; @@ -350,5 +351,383 @@ public static function fieldType($abstractType) { "timestamp" => "int", "mediumtext" => "mediumtext", "text" => "text"); +======= +class DBAdapter implements IAdapter { + static $db; + static $cachedResult, $dbProperties, $escapeTag, $lastQueryType; + + public static function bind($database) { + // Connects DB and set environment variables + // $database array should contain 'server','username','password'. + self::$cachedResult = self::$dbProperties = array(); + if (!isset($database) || empty($database)) { + return false; + } + if (!isset($database['port']) && strpos($database['server'], ':')) { + $port = explode(":", $database['server']); + $database['server'] = $port[0]; + $database['port'] = $port[1]; + } + if (isset($database['port'])) { + self::$db = new mysqli($database['server'], $database['username'], $database['password'], $database['database'], intval($database['port'])); + } else { + self::$db = new mysqli($database['server'], $database['username'], $database['password'], $database['database']); + } + if (!self::$db) { + return false; + } + if (!self::$db->select_db($database['database'])) { + die("Connection error :" . self::$db->errorno . " - " . self::$db->error); + } + //self::$db->autocommit(false); // Turns off autocommit. + self::$db->autocommit(true); // Turns off autocommit. + if (self::$db->set_charset("utf8")) { + self::$dbProperties['charset'] = 'utf8'; + } else { + self::$dbProperties['charset'] = 'default'; + } + @self::query('SET SESSION collation_connection = \'utf8_general_ci\''); + return true; + } + + public static function unbind() { + self::$db->close(); + return true; + } + + public static function charset() { + if (array_key_exists('charset', self::$dbProperties)) { + return self::$dbProperties['charset']; + } else { + return null; + } + } + + public static function dbms() { + return 'MySQLi'; + } + + public static function version($mode = "server") { + if (array_key_exists('version', self::$dbProperties)) { + return self::$dbProperties['version']; + } else { + self::$dbProperties['version'] = self::queryCell("SELECT VERSION()"); + return self::$dbProperties['version']; + } + } + + public static function tableList($condition = null) { + if (!array_key_exists('tableList', self::$dbProperties)) { + $tableData = self::queryAll('SHOW TABLES'); + self::$dbProperties['tableList'] = array(); + foreach ($tableData as $tbl) { + array_push(self::$dbProperties['tableList'], $tbl[0]); + } + } + $result = array(); + if (!is_null($condition)) { + $result = array(); + foreach (self::$dbProperties['tableList'] as $item) { + if (strpos($item, $condition) === 0) { + array_push($result, $item); + } + } + return $result; + } else { + return self::$dbProperties['tableList']; + } + } + + public static function setTimezone($time) { + return self::query('SET time_zone = \'' . Timezone::getCanonical() . '\''); + } + + public static function reservedFieldNames() { + return null; + } + + public static function reservedFunctionNames() { + return array('UNIX_TIMESTAMP()'); + } + + public static function queryExistence($query) { + if ($result = self::query($query)) { + if ($result->num_rows > 0) { + $result->free(); + return true; + } + $result->free(); + } + return false; + } + + public static function queryCount($query) { + $count = 0; + $query = trim($query); + if ($result = self::query($query)) { + $operation = strtolower(substr($query, 0, 6)); + self::$lastQueryType = $operation; + switch ($operation) { + case 'select': + $count = $result->num_rows; + $result->free(); + break; + case 'insert': + case 'update': + case 'delete': + case 'replac': + default: + $count = self::$db->affected_rows; + //mysqli_free_result(); + break; + } + } + return $count; + } + + public static function queryCell($query, $field = 0, $useCache = true) { + $type = MYSQL_BOTH; + if (is_numeric($field)) { + $type = MYSQL_NUM; + } else { + $type = MYSQL_ASSOC; + } + + if ($useCache) { + $result = self::queryAllWithCache($query, $type); + } else { + $result = self::queryAllWithoutCache($query, $type); + } + if (empty($result)) { + return null; + } + return $result[0][$field]; + } + + public static function queryRow($query, $type = 'both', $useCache = true) { + if ($useCache) { + $result = self::queryAllWithCache($query, $type, 1); + } else { + $result = self::queryAllWithoutCache($query, $type, 1); + } + if (empty($result)) { + return null; + } + return $result[0]; + } + + public static function queryColumn($query, $useCache = true) { + $cacheKey = "{$query}_queryColumn"; + if ($useCache && isset(self::$cachedResult[$cacheKey])) { + if (function_exists('__tcSqlLogBegin')) { + __tcSqlLogBegin($query); + __tcSqlLogEnd(null, 1); + } + self::$cachedResult[$cacheKey][0]++; + return self::$cachedResult[$cacheKey][1]; + } + + $column = null; + if ($result = self::query($query)) { + $column = array(); + + while ($row = $result->fetch_row()) + array_push($column, $row[0]); + $result->free(); + } + + if ($useCache) { + self::$cachedResult[$cacheKey] = array(1, $column); + } + return $column; + } + + public static function queryAll($query, $type = 'both', $count = -1) { + return self::queryAllWithCache($query, $type, $count); + //return self::queryAllWithoutCache($query, $type, $count); // Your choice. :) + } + + public static function queryAllWithoutCache($query, $type = 'both', $count = -1) { + $all = array(); + $realtype = self::__queryType($type); + if ($result = self::query($query)) { + while (($count-- != 0) && $row = $result->fetch_array($realtype)) + array_push($all, $row); + $result->free(); + return $all; + } + return null; + } + + public static function queryAllWithCache($query, $type = 'both', $count = -1) { + $cacheKey = "{$query}_{$type}_{$count}"; + if (isset($cachedResult[$cacheKey])) { + if (function_exists('__tcSqlLogBegin')) { + __tcSqlLogBegin($query); + __tcSqlLogEnd(null, 1); + } + self::$cachedResult[$cacheKey][0]++; + return self::$cachedResult[$cacheKey][1]; + } + $all = self::queryAllWithoutCache($query, $type, $count); + self::$cachedResult[$cacheKey] = array(1, $all); + return $all; + } + + public static function execute($query) { + return self::query($query) ? true : false; + } + + public static function multiQuery() { + $result = false; + foreach (func_get_args() as $query) { + if (is_array($query)) { + foreach ($query as $subquery) + if (($result = self::query($subquery)) === false) { + return false; + } + } else { + if (($result = self::query($query)) === false) { + return false; + } + } + } + return $result; + } + + public static function query($query) { + if (function_exists('__tcSqlLogBegin')) { + __tcSqlLogBegin($query); + $result = self::$db->query($query); + __tcSqlLogEnd($result, 0); + } else { + $result = self::$db->query($query); + } + self::$lastQueryType = strtolower(substr($query, 0, 6)); + if (stristr($query, 'update ') || + stristr($query, 'insert ') || + stristr($query, 'delete ') || + stristr($query, 'replace ') + ) { + self::clearCache(); + } + return $result; + } + + public static function escapeString($string, $link = null) { + if (is_null(self::$escapeTag)) { + if (self::$db->real_escape_string('ㅋ') == 'ㅋ') { + self::$escapeTag = 'real'; + } else { + self::$escapeTag = 'none'; + } + } + if (self::$escapeTag == 'real') { + return self::$db->real_escape_string($string);; + } else { + return self::$db->escape_string($string); + } + } + + /*** Instant cache functions ***/ + public static function clearCache() { + self::$cachedResult = array(); + if (function_exists('__tcSqlLogBegin')) { + __tcSqlLogBegin("Cache cleared"); + __tcSqlLogEnd(null, 2); + } + } + + public static function cacheLoad() { + global $fileCachedResult; + } + + public static function cacheSave() { + global $fileCachedResult; + } + + public static function rollback() { + return self::$db->rollback(); + } + + public static function commit() { + self::$db->commit(); // Auto commit. + return true; + } + + /*** Raw functions (to easier adoptation from traditional queries) ***/ + public static function insertId() { + return self::$db->insert_id; + } + + public static function num_rows($handle = null) { + switch (self::$lastQueryType) { + case 'select': + return mysqli_num_rows($handle); + break; + default: + return self::$db->affected_rows; + break; + } + return null; + } + + public static function free($handle = null) { + mysqli_free_result($handle); + } + + public static function fetch($handle = null, $type = 'assoc') { + if ($type == 'array') { + return mysqli_fetch_array($handle); + } // Can I use mysqli_fetch_row instead? + else { + if ($type == 'row') { + return mysqli_fetch_row($handle); + } else { + return mysqli_fetch_assoc($handle); + } + } + } + + public static function error($err = null) { + return mysqli_error($err); + } + + public static function stat($stat = null) { + if ($stat === null) { + return self::$db->stat(); + } else { + return self::$db->stat($stat); + } + } + + public static function __queryType($type) { + switch (strtolower($type)) { + case 'num': + return MYSQL_NUM; + case 'assoc': + return MYSQL_ASSOC; + case 'both': + default: + return MYSQL_BOTH; + } + } + + public static function fieldType($abstractType) { + if (isset(self::$typeTable[$abstractType])) { + return self::$typeTable[$abstractType]; + } + } + + private static $typeTable = array( + "integer" => "int", + "int" => "int", + "float" => "float", + "double" => "double", + "timestamp" => "int", + "mediumtext" => "mediumtext", + "varchar" => "varchar", + "text" => "text"); +>>>>>>> d3cefd6... refs #1753 : modified - DBModel to accept more types. } ?> diff --git a/framework/data/PostgreSQL/Adapter.php b/framework/data/PostgreSQL/Adapter.php index f5ddee08c..45af77f00 100644 --- a/framework/data/PostgreSQL/Adapter.php +++ b/framework/data/PostgreSQL/Adapter.php @@ -323,6 +323,7 @@ public static function cacheSave() { public static function commit() { return pg_query("commit"); // return true; // Auto commit. +<<<<<<< HEAD } /* Raw public static functions (to easier adoptation) */ @@ -386,5 +387,88 @@ public static function fieldType($abstractType) { "timestamp" => "integer", "mediumtext" => "varchar(512)", "text" => "text"); +======= + } + + /* Raw public static functions (to easier adoptation) */ + /*@static@*/ + public static function num_rows($handle = null) { + switch (self::$lastQueryType) { + case 'select': + return pg_num_rows($handle); + break; + default: + return pg_affected_rows($handle); + break; + } + return null; + } + + /*@static@*/ + public static function free($handle = null) { + pg_free_result($handle); + } + + /*@static@*/ + public static function fetch($handle = null, $type = 'assoc') { + if ($type == 'array') { + return pg_fetch_array($handle); + } // Can I use mysql_fetch_row instead? + else { + if ($type == 'row') { + return pg_fetch_row($handle); + } else { + return pg_fetch_assoc($handle); + } + } + } + + /*@static@*/ + public static function error($err = null) { + if ($err === null) { + return pg_error(); + } else { + return pg_error($err); + } + } + + /*@static@*/ + public static function stat($stat = null) { + if ($stat === null) { + return pg_connection_status(); + } else { + return pg_connection_status($stat); + } + } + + /*@static@*/ + public static function __queryType($type) { + switch (strtolower($type)) { + case 'num': + return PGSQL_NUM; + case 'assoc': + return PGSQL_ASSOC; + case 'both': + default: + return PGSQL_BOTH; + } + } + + public static function fieldType($abstractType) { + if (isset(self::$typeTable[$abstractType])) { + return self::$typeTable[$abstractType]; + } + } + + static $typeTable = array( + "integer" => "integer", + "int" => "integer", + "float" => "float", + "double" => "float", + "timestamp" => "integer", + "mediumtext" => "varchar(512)", + "varchar" => "varchar", + "text" => "text"); +>>>>>>> d3cefd6... refs #1753 : modified - DBModel to accept more types. } ?> diff --git a/framework/data/SQLite3/Adapter.php b/framework/data/SQLite3/Adapter.php index 0beef91c7..f37584bdf 100644 --- a/framework/data/SQLite3/Adapter.php +++ b/framework/data/SQLite3/Adapter.php @@ -340,9 +340,12 @@ public static function fieldType($abstractType) { static $typeTable = array( "integer" => "int", + "int" => "int", "float" => "float", + "double" => "float", "timestamp" => "int", "mediumtext" => "mediumtext", + "varchar" => "varchar", "text" => "text"); } ?>