Skip to content

Commit dcc24cb

Browse files
committed
Merge branch 'master' into newmaster
2 parents e5cbca2 + 0c7ddd2 commit dcc24cb

File tree

6 files changed

+224
-48
lines changed

6 files changed

+224
-48
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ build/logs/clover.xml
1414
*.key
1515
test.php
1616
build
17+
tmp

shared/ezFunctions.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,17 @@
186186

187187
const _DS = \DIRECTORY_SEPARATOR;
188188

189+
\define('MYSQL', 'mysqli', true);
190+
\define('MYSQLI', 'mysqli', true);
191+
\define('Pdo', 'pdo');
192+
\define('PGSQL', 'pgsql', true);
193+
\define('POSTGRESQL', 'pgsql', true);
194+
\define('SQLITE', 'sqlite3', true);
195+
\define('SQLITE3', 'sqlite3', true);
196+
\define('SQLSRV', 'sqlsrv');
197+
\define('SQLSERVER', 'sqlsrv', true);
198+
\define('MSSQL', 'sqlsrv', true);
199+
189200
// Global class instances, will be used to create and call methods directly.
190201
global $ezInstance;
191202

shared/ezSchema.php

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,64 @@ class ezSchema
55
{
66
const STRINGS = [
77
'common' => ['CHAR', 'VARCHAR', 'TEXT'],
8-
'mysql' => ['TINYTEXT', 'MEDIUMTEXT', 'LONGTEXT', 'BINARY', 'VARBINARY'],
9-
'postgresql' => ['character', 'character varying'],
10-
'sqlserver' => ['NCHAR', 'NVARCHAR', 'NTEXT', 'BINARY', 'VARBINARY', 'IMAGE'],
8+
'mysqli' => ['TINYTEXT', 'MEDIUMTEXT', 'LONGTEXT', 'BINARY', 'VARBINARY'],
9+
'pgsql' => ['character', 'character varying'],
10+
'sqlsrv' => ['NCHAR', 'NVARCHAR', 'NTEXT', 'BINARY', 'VARBINARY', 'IMAGE'],
1111
'sqlite3' => ['TINYTEXT', 'MEDIUMTEXT', 'LONGTEXT', 'NCHAR', 'NVARCHAR', 'CLOB']
1212
];
1313

1414
const NUMBERS = [
1515
'common' => ['INT'],
16-
'mysql' => ['BIT', 'INTEGER', 'TINYINT', 'SMALLINT', 'MEDIUMINT', 'BIGINT', 'FLOAT',
16+
'mysqli' => ['BIT', 'INTEGER', 'TINYINT', 'SMALLINT', 'MEDIUMINT', 'BIGINT', 'FLOAT',
1717
'BOOL', 'BOOLEAN'],
18-
'postgresql' => ['bit', 'varbit', 'bit varying', 'smallint', 'int', 'integer',
18+
'pgsql' => ['bit', 'varbit', 'bit varying', 'smallint', 'int', 'integer',
1919
'bigint', 'smallserial', 'serial', 'bigserial', 'double precision', 'real',
2020
'money', 'bool', 'boolean'],
21-
'sqlserver' => ['BIT', 'TINYINT', 'SMALLINT', 'BIGINT', 'SMALLMONEY', 'MONEY',
21+
'sqlsrv' => ['BIT', 'TINYINT', 'SMALLINT', 'BIGINT', 'SMALLMONEY', 'MONEY',
2222
'FLOAT', 'REAL'],
2323
'sqlite3' => ['TINYINT', 'SMALLINT', 'MEDIUMINT', 'BIGINT', 'INTEGER', 'INT2',
2424
'INT4', 'INT8', 'REAL', 'DOUBLE', 'DOUBLE PRECISION', 'FLOAT', 'BOOLEAN']
2525
];
2626

2727
const NUMERICS = [
2828
'common' => ['NUMERIC', 'DECIMAL'],
29-
'mysql' => ['IDENTITY','DEC', 'FIXED', 'FLOAT', 'DOUBLE', 'DOUBLE PRECISION', 'REAL'],
30-
'sqlserver' => ['DEC'],
31-
'postgresql' => [],
29+
'mysqli' => ['IDENTITY','DEC', 'FIXED', 'FLOAT', 'DOUBLE', 'DOUBLE PRECISION', 'REAL'],
30+
'sqlsrv' => ['DEC'],
31+
'pgsql' => [],
3232
'sqlite3' => []
3333
];
3434

3535
const DATE_TIME = [
3636
'common' => ['DATE', 'TIMESTAMP', 'TIME'],
37-
'mysql' => ['DATETIME', 'YEAR'],
38-
'postgresql' => [
37+
'mysqli' => ['DATETIME', 'YEAR'],
38+
'pgsql' => [
3939
'timestamp without time zone', 'timestamp with time zone',
4040
'time without time zone', 'time with time zone'
4141
],
42-
'sqlserver' => ['DATETIME', 'DATETIME2', 'SMALLDATETIME', 'DATETIMEOFFSET'],
42+
'sqlsrv' => ['DATETIME', 'DATETIME2', 'SMALLDATETIME', 'DATETIMEOFFSET'],
4343
'sqlite3' => ['DATETIME']
4444
];
4545

4646
const OBJECTS = [
47-
'mysql' => ['TINYBLOB', 'BLOB', 'MEDIUMBLOB', 'LONGTEXT'],
47+
'mysqli' => ['TINYBLOB', 'BLOB', 'MEDIUMBLOB', 'LONGTEXT'],
4848
'sqlite3' => ['BLOB'],
49-
'postgresql' => [],
50-
'sqlserver' => []
49+
'pgsql' => [],
50+
'sqlsrv' => []
5151
];
5252

5353
const OPTIONS = ['CONSTRAINT', 'PRIMARY KEY', 'FOREIGN KEY', 'UNIQUE', 'INDEX', 'REFERENCES'];
5454
const ALTERS = ['ADD', 'DROP COLUMN', 'CHANGE COLUMN', 'RENAME TO', 'MODIFY', 'ALTER COLUMN'];
5555
const CHANGES = [
56-
'mysql' => 'MODIFY',
57-
'postgresql' => 'ALTER COLUMN',
58-
'sqlserver' => 'ALTER COLUMN',
56+
'mysqli' => 'MODIFY',
57+
'pgsql' => 'ALTER COLUMN',
58+
'sqlsrv' => 'ALTER COLUMN',
5959
'sqlite3' => ''
6060
];
6161

6262
const autoNUMBERS = [
63-
'mysql' => 'AUTO_INCREMENT',
64-
'postgresql' => 'SERIAL',
65-
'sqlserver' => 'IDENTITY(1,1)',
63+
'mysqli' => 'AUTO_INCREMENT',
64+
'pgsql' => 'SERIAL',
65+
'sqlsrv' => 'IDENTITY(1,1)',
6666
'sqlite3' => 'AUTOINCREMENT'
6767
];
6868

@@ -111,7 +111,7 @@ public function __call($type, $args)
111111
// check for string data type
112112
$numberOrString = $args[0];
113113
$store = \is_int($numberOrString) ? '('.$numberOrString.')' : '';
114-
$store = empty($store) && !empty($numberOrString) ? $numberOrString : $store;
114+
$store = empty($store) && !empty($numberOrString) ? ' '.$numberOrString : $store;
115115
$value = !empty($args[1]) ? ' '.$args[1] : '';
116116
$options = !empty($args[2]) ? ' '.$args[2] : '';
117117
$extra = !empty($args[3]) ? ' '.$args[3] : '';
@@ -128,7 +128,7 @@ public function __call($type, $args)
128128
// check for numeric data type
129129
$numberOrString = $args[0];
130130
$store = \is_int($numberOrString) ? '('.$numberOrString.')' : '';
131-
$store = empty($store) && !empty($numberOrString) ? $numberOrString : $store;
131+
$store = empty($store) && !empty($numberOrString) ? ' '.$numberOrString : $store;
132132
$value = !empty($args[1]) ? ' '.$args[1] : '';
133133
$options = !empty($args[2]) ? ' '.$args[2] : '';
134134
$extra = !empty($args[3]) ? ' '.$args[3] : '';
@@ -137,7 +137,7 @@ public function __call($type, $args)
137137
// check for date time data type
138138
$numberOrString = $args[0];
139139
$store = \is_int($numberOrString) ? '('.$numberOrString.')' : '';
140-
$fraction = empty($store) && !empty($numberOrString) ? $numberOrString : $store;
140+
$fraction = empty($store) && !empty($numberOrString) ? ' '.$numberOrString : $store;
141141
$value = !empty($args[1]) ? ' '.$args[1] : '';
142142
$options = !empty($args[2]) ? ' '.$args[2] : '';
143143
$data = $type.$fraction.$value.$options;
@@ -164,21 +164,21 @@ public static function vendor()
164164
if ($dbSqlite === $instance && !empty($dbSqlite))
165165
$type = 'sqlite3';
166166
elseif ($dbPgsql === $instance && !empty($dbPgsql))
167-
$type = 'postgresql';
167+
$type = 'pgsql';
168168
elseif ($dbMysqli === $instance && !empty($dbMysqli))
169-
$type = 'mysql';
169+
$type = 'mysqli';
170170
elseif ($dbMssql === $instance && !empty($dbMssql))
171-
$type = 'sqlserver';
171+
$type = 'sqlsrv';
172172
elseif ($dbPdo === $instance && !empty($dbPdo)) {
173173
$dbh = $dbPdo->connection();
174174
if (strpos($dbh->getAttribute(\PDO::ATTR_CLIENT_VERSION), 'mysql') !== false)
175-
$type = 'mysql';
175+
$type = 'mysqli';
176176
elseif (strpos($dbh->getAttribute(\PDO::ATTR_CLIENT_VERSION), 'pgsql') !== false)
177-
$type = 'postgresql';
177+
$type = 'pgsql';
178178
elseif (strpos($dbh->getAttribute(\PDO::ATTR_CLIENT_VERSION), 'sqlite') !== false)
179179
$type = 'sqlite3';
180180
elseif (strpos($dbh->getAttribute(\PDO::ATTR_CLIENT_VERSION), 'sqlsrv') !== false)
181-
$type = 'sqlserver';
181+
$type = 'sqlsrv';
182182
}
183183

184184
return $type;

shared/ez_sql_core.php

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,38 @@ class ezSQLcore extends ezQuery
2424
protected $show_errors = true;
2525
protected $num_queries = 0;
2626
protected $conn_queries = 0;
27+
2728
protected $captured_errors = array();
28-
protected $cache_dir = false;
29+
30+
31+
// Specify a cache dir. Path is taken from calling script
32+
protected $cache_dir = 'tmp'.\_DS.'ez_cache';
33+
34+
/**
35+
* if you want to cache EVERYTHING just do..
36+
*
37+
* $use_disk_cache = true;
38+
* $cache_queries = true;
39+
* $cache_timeout = 24;
40+
*/
2941
protected $cache_queries = false;
3042
protected $cache_inserts = false;
3143
protected $use_disk_cache = false;
32-
protected $cache_timeout = 24; // hours
44+
45+
// Cache expiry
46+
protected $cache_timeout = 24; // Note: this is hours
47+
3348
protected $db_connect_time = 0;
3449
protected $sql_log_file = false;
3550
protected $profile_times = array();
3651
protected $insert_id = null;
3752

53+
/**
54+
* Log number of rows the query returned
55+
* @var int Default is null
56+
*/
57+
protected $num_rows = null;
58+
3859
public $last_query = null;
3960
public $last_error = null;
4061
public $col_info = null;
@@ -90,7 +111,7 @@ class ezSQLcore extends ezQuery
90111
*/
91112
public function __construct()
92113
{
93-
parent::__construct();
114+
parent::__construct();
94115
}
95116

96117
/**
@@ -311,19 +332,31 @@ public function get_col_info($info_type = "name", $col_offset = -1)
311332
}
312333
}
313334

335+
/**
336+
* create cache directory if doesn't exists
337+
*/
338+
public function create_cache(string $path = null)
339+
{
340+
$cache_dir = empty($path) ? $this->cache_dir : $path;
341+
if ( ! \is_dir($cache_dir) ) {
342+
$this->cache_dir = $cache_dir;
343+
@\mkdir($cache_dir, ('\\' == \DIRECTORY_SEPARATOR ? null : 0755), true);
344+
}
345+
}
346+
314347
/**
315348
* store_cache
316349
*/
317350
public function store_cache(string $query, $is_insert)
318351
{
319352
// The would be cache file for this query
320-
$cache_file = $this->cache_dir.'/'.\md5($query);
353+
$cache_file = $this->cache_dir.\_DS.\md5($query);
321354

322355
// disk caching of queries
323356
if ( $this->use_disk_cache
324-
&& ( $this->cache_queries && ! $is_insert )
325-
|| ( $this->cache_inserts && $is_insert )
357+
&& ( $this->cache_queries && ! $is_insert ) || ( $this->cache_inserts && $is_insert )
326358
) {
359+
$this->create_cache();
327360
if ( ! \is_dir($this->cache_dir) ) {
328361
$this->register_error("Could not open cache dir: $this->cache_dir");
329362
$this->show_errors ? \trigger_error("Could not open cache dir: $this->cache_dir", \E_USER_WARNING) : null;
@@ -338,7 +371,7 @@ public function store_cache(string $query, $is_insert)
338371

339372
\file_put_contents($cache_file, \serialize($result_cache));
340373
if( \file_exists($cache_file . ".updating") )
341-
\unlink($cache_file . ".updating");
374+
\unlink($cache_file . ".updating");
342375
}
343376
}
344377
}
@@ -349,14 +382,14 @@ public function store_cache(string $query, $is_insert)
349382
public function get_cache(string $query)
350383
{
351384
// The would be cache file for this query
352-
$cache_file = $this->cache_dir.'/'.\md5($query);
385+
$cache_file = $this->cache_dir.\_DS.\md5($query);
353386

354387
// Try to get previously cached version
355388
if ( $this->use_disk_cache && \file_exists($cache_file) ) {
356389
// Only use this cache file if less than 'cache_timeout' (hours)
357390
if ( (\time() - \filemtime($cache_file)) > ($this->cache_timeout*3600)
358-
&& !(\file_exists($cache_file . ".updating")
359-
&& (\time() - \filemtime($cache_file . ".updating") < 60))
391+
&& !(\file_exists($cache_file . ".updating")
392+
&& (\time() - \filemtime($cache_file . ".updating") < 60))
360393
) {
361394
\touch($cache_file . ".updating"); // Show that we in the process of updating the cache
362395
} else {
@@ -370,7 +403,6 @@ public function get_cache(string $query)
370403

371404
// If debug ALL queries
372405
$this->trace || $this->debug_all ? $this->debug() : null ;
373-
374406
return $result_cache['return_value'];
375407
}
376408
}

tests/shared/ezSQLcoreTest.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,21 +195,27 @@ public function testGet_col_info() {
195195
*/
196196
public function testStore_cache() {
197197
$sql = 'SELECT * FROM ez_test';
198+
$this->object->setCache_Timeout(1);
199+
$this->object->setUse_Disk_Cache(true);
200+
$this->object->setCache_Queries(true);
201+
$this->object->setNum_Rows(5);
202+
$this->object->store_cache($sql, false);
198203

199-
$this->object->store_cache($sql, true);
200-
201-
$this->assertNull($this->object->get_cache($sql));
204+
$this->assertEquals(5, $this->object->get_cache($sql));
202205
} // testStore_cache
203206

204207
/**
205208
* @covers ezSQLcore::get_cache
206209
*/
207210
public function testGet_cache() {
208211
$sql = 'SELECT * FROM ez_test';
209-
210-
$this->object->store_cache($sql, true);
211-
212-
$this->assertNull($this->object->get_cache($sql));
212+
$this->object->setCache_Timeout(1);
213+
$this->object->setUse_Disk_Cache(true);
214+
$this->object->setCache_Queries(true);
215+
$this->object->setNum_Rows(2);
216+
$this->object->store_cache($sql, false);
217+
218+
$this->assertEquals(2, $this->object->get_cache($sql));
213219
} // testGet_cache
214220

215221
/**

0 commit comments

Comments
 (0)