Skip to content

Commit

Permalink
refs #1575 : modified - setup routine for sqlite (work in progress)
Browse files Browse the repository at this point in the history
 -
  • Loading branch information
inureyes committed Apr 13, 2015
1 parent 9e1c59a commit 722f92b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
67 changes: 35 additions & 32 deletions framework/data/SQLite3/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

global $fileCachedResult;

class DBAdapter implements IAdapter {
class DBAdapter implements IAdapter {
static $db;
static $cachedResult, $dbProperties, $escapeTag, $lastQueryType;
public static function bind($database) {
Expand All @@ -20,8 +20,11 @@ public static function bind($database) {
$database['server'] = $port[0];
$database['port'] = $port[1];
}
self::$db = new SQLite3(__TEXTCUBE_CACHE_DIR__.'/'.$database['database'].'.db');

if(!file_exists(__TEXTCUBE_CACHE_DIR__)) {
@mkdir(__TEXTCUBE_CACHE_DIR__);
}
self::$db = new SQLite3(__TEXTCUBE_CACHE_DIR__.'/'.$database['database'].'.db',SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);

if(!self::$db) return false;

return true;
Expand Down Expand Up @@ -51,7 +54,7 @@ public static function version($mode = "server") {
}

public static function tableList($condition = null) {
if (!array_key_exists('tableList', self::$dbProperties)) {
if (!array_key_exists('tableList', self::$dbProperties)) {
$tableData = self::queryAll('SELECT name from sqlite_master WHERE type="table"');
self::$dbProperties['tableList'] = array();
foreach($tableData as $tbl) {
Expand All @@ -71,20 +74,20 @@ public static function tableList($condition = null) {
return self::$dbProperties['tableList'];
}
}

public static function setTimezone($time) {
return true;
# 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 (self::$db->changes() > 0) {
Expand All @@ -95,7 +98,7 @@ public static function queryExistence($query) {
}
return false;
}

public static function queryCount($query) {
$count = 0;
$query = trim($query);
Expand Down Expand Up @@ -140,7 +143,7 @@ public static function queryCell($query, $field = 0, $useCache=true) {
}
return $result[0][$field];
}

public static function queryRow($query, $type = 'both', $useCache=true) {
if( $useCache ) {
$result = self::queryAllWithCache($query, $type, 1);
Expand All @@ -152,7 +155,7 @@ public static function queryRow($query, $type = 'both', $useCache=true) {
}
return $result[0];
}

public static function queryColumn($query, $useCache=true) {
$cacheKey = "{$query}_queryColumn";
if( $useCache && isset( self::$cachedResult[$cacheKey] ) ) {
Expand All @@ -167,7 +170,7 @@ public static function queryColumn($query, $useCache=true) {
$column = null;
if ($result = self::query($query)) {
$column = array();

while ($row = $result->fetchArray())
array_push($column, $row[0]);
$result->finalize();
Expand All @@ -178,7 +181,7 @@ public static function queryColumn($query, $useCache=true) {
}
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. :)
Expand All @@ -195,7 +198,7 @@ public static function queryAllWithoutCache($query, $type = 'both', $count = -1)
}
return null;
}

public static function queryAllWithCache($query, $type = 'both', $count = -1) {
$cacheKey = "{$query}_{$type}_{$count}";
if( isset( $cachedResult[$cacheKey] ) ) {
Expand All @@ -210,7 +213,7 @@ public static function queryAllWithCache($query, $type = 'both', $count = -1) {
self::$cachedResult[$cacheKey] = array( 1, $all );
return $all;
}

public static function execute($query) {
return self::$db->exec($query);
}
Expand All @@ -231,15 +234,15 @@ public static function multiQuery() {
public static function query($query, $compatibility = true) {
// var_dump($query);
if($compatibility) {
$query = str_replace('UNIX_TIMESTAMP()',Timestamp::getUNIXtime(),$query); // compatibility issue.
$query = str_replace('RAND()','RANDOM()',$query); // compatibility issue.
$query = str_replace('UNIX_TIMESTAMP()',Timestamp::getUNIXtime(),$query); // compatibility issue.
$query = str_replace('RAND()','RANDOM()',$query); // compatibility issue.
$origPagingInst = array(
'/CHAR_LENGTH(.*) /si'
);
$descPagingInst = array(
'LENGTH($1) '
);
$query = preg_replace($origPagingInst, $descPagingInst,$query);
$query = preg_replace($origPagingInst, $descPagingInst,$query);
}
if( function_exists( '__tcSqlLogBegin' ) ) {
__tcSqlLogBegin($query);
Expand All @@ -257,7 +260,7 @@ public static function query($query, $compatibility = true) {
}
return $result;
}

public static function escapeString($string, $link = null){
return self::$db->escapeString($string);
}
Expand All @@ -274,23 +277,23 @@ public static function clearCache() {
public static function cacheLoad() {
global $fileCachedResult;
}

public static function cacheSave() {
global $fileCachedResult;
}
public static function rollback() {
return self::$db->rollback();
}
public static function commit() {
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->lastInsertRowID();
}

public static function num_rows($handle = null) {
switch(self::$lastQueryType) {
case 'select':
Expand All @@ -302,42 +305,42 @@ public static function num_rows($handle = null) {
}
return null;
}

public static function free($handle = null) {
sqlite_free_result($handle);
}

public static function fetch($handle = null, $type = 'assoc') {
if($type == 'array') return sqlite_fetch_array($handle); // Can I use mysqli_fetch_row instead?
else if ($type == 'row') return sqlite_fetch_row($handle);
else return sqlite_fetch_assoc($handle);
}

public static function error($err = null) {
return sqlite_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 SQLITE3_NUM;
case 'assoc':
return SQLITE3_ASSOC;
return SQLITE3_ASSOC;
case 'both':
default:
return SQLITE3_BOTH;
}
}

public static function fieldType($abstractType) {
if(isset($typeTable[$abstractType])) return $typeTable[$abstractType];
}

static $typeTable = array(
"integer" => "int",
"int" => "int",
Expand All @@ -346,6 +349,6 @@ public static function fieldType($abstractType) {
"timestamp" => "int",
"mediumtext" => "mediumtext",
"varchar" => "varchar",
"text" => "text");
"text" => "text");
}
?>
8 changes: 6 additions & 2 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
ini_set('display_errors', 'on');

define('ROOT','.');
//if (!defined('__TEXTCUBE_CACHE_DIR__')) {
define('__TEXTCUBE_CACHE_DIR__', ROOT . '/cache');
//}
require ROOT.'/framework/id/textcube/config.default.php';

if (version_compare(PHP_VERSION,'5.4.0', '<')) {
Expand Down Expand Up @@ -52,14 +55,15 @@
if (isset($_POST['dbPrefix']) && $_POST['dbPrefix'] == '') {
$_POST['dbPrefix'] == 'tc_';
}
$__requireBasics = array(
$context = Model_Context::getInstance();
$context->setProperty('import.library', array(
'function/string',
'function/time',
'function/javascript',
'function/html',
'function/xml',
'function/misc',
'function/mail');
'function/mail'));
if(isset($_POST['dbms'])) $database['dbms'] = $_POST['dbms'];
require ROOT.'/library/include.php';

Expand Down

0 comments on commit 722f92b

Please sign in to comment.