Skip to content

Commit eb1c746

Browse files
committed
Fix error on php 8.1+
Add DBCacheQuery class to insert a large number of records using fewer queries Little fixes
1 parent b773c1b commit eb1c746

File tree

4 files changed

+103
-7
lines changed

4 files changed

+103
-7
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ All methods available in both class types
88
```php
99
use \Power\DB;
1010

11-
// Non static
11+
// Non-static
1212
$db = new \Power\mDB('localhost', 'users', 'passwd', 'dbname', 'utf8mb4');
1313
// Select all records from users table
1414
$data = $db->getAll('SELECT * FROM ?n', 'users');
@@ -101,4 +101,26 @@ use \Power\DB;
101101
DB::Init('localhost', 'user', 'passwd', 'dbname', 'utf8mb4');
102102
DB::SetErrorLog(__DIR__.'/mysql_error.log');
103103
DB::SetLogSql(__DIR__.'/mysql_sql.log', false);
104+
```
105+
106+
#### Use ``DBCacheQuery`` class to insert a large number of records using fewer queries
107+
```php
108+
// Create class and point table name and col names for inserting records
109+
$cache_items = new \DBCacheQuery('item_list', ['id', 'name', 'icon', 'list', 'data_type']);
110+
foreach ($some_data as $data)
111+
{
112+
// Use Add method for each new row
113+
// Make sure, that param array has the same data order as you make in class creation
114+
$cache_items->Add(
115+
[
116+
$data['id'],
117+
$data['name'],
118+
$data['icon'],
119+
$data['list'],
120+
$data['type']
121+
]);
122+
}
123+
// Then use Flush method to send the remaining data from the cache
124+
$cache_items->Flush();
125+
// That's all
104126
```

src/DB.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ public static function pure(string $sql): stdClass
399399
}
400400

401401
/**
402-
* DONT'T USE - function in development
402+
* DON'T USE - function in development
403403
* Make condition for where/having and other parameters
404404
*
405405
* Example:
@@ -420,4 +420,28 @@ public static function cond(string $field, string $operator, $value): string
420420
return self::instance()->parse('?n '.$operator.' '.$placeholder, $field, $value);
421421
}
422422

423+
public static function escapeParam($val)
424+
{
425+
if (is_int($val) || is_float($val)) {
426+
return self::escapeInt($val);
427+
}
428+
if (is_object($val) && isset($val->sql)) {
429+
return $val->sql;
430+
}
431+
return self::escapeString($val);
432+
}
433+
434+
private static function escapeInt($value)
435+
{
436+
if ($value === NULL)
437+
{
438+
return 'NULL';
439+
}
440+
if (is_float($value))
441+
{
442+
$value = number_format($value, 6, '.', ''); // may lose precision on big numbers
443+
}
444+
return $value;
445+
}
446+
423447
}

src/DBCacheQuery.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Power;
4+
5+
class DBCacheQuery
6+
{
7+
8+
private $fields;
9+
private $table_name;
10+
private $insert_ignore;
11+
private $sql_cache = '';
12+
private $query_size_limit;
13+
14+
public function __construct(string $table_name, array $field_names, bool $insert_ignore = false, int $query_size_limit = 250000)
15+
{
16+
$this->fields = $field_names;
17+
$this->table_name = $table_name;
18+
$this->insert_ignore = $insert_ignore;
19+
$this->query_size_limit = $query_size_limit;
20+
}
21+
22+
public function Flush()
23+
{
24+
if ($this->sql_cache !== '') {
25+
DB::query('?p;', $this->sql_cache);
26+
}
27+
$this->sql_cache = '';
28+
}
29+
30+
public function Add(array $values)
31+
{
32+
if (strlen($this->sql_cache) >= $this->query_size_limit) {
33+
$this->Flush();
34+
}
35+
$v = '';
36+
foreach ($values as $val) {
37+
if ($v !== '') {
38+
$v .= ',';
39+
}
40+
$v .= DB::escapeParam($val);
41+
}
42+
if ($this->sql_cache === '') {
43+
$f = '`' . implode('`,`', $this->fields) . '`';
44+
$this->sql_cache = 'INSERT ' . ($this->insert_ignore ? 'IGNORE ' : '') . 'INTO `' . $this->table_name . '` (' . $f . ') VALUES (' . $v . ')';
45+
} else {
46+
$this->sql_cache .= ', (' . $v . ')';
47+
}
48+
}
49+
50+
}

src/mDB.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ public function parse(string $sql, ...$arg): string
459459
*
460460
* @param string $input - field name to test
461461
* @param array $allowed - an array with allowed variants
462-
* @param string $default - optional variable to set if no match found. Default to false.
462+
* @param string|bool $default - optional variable to set if no match found. Default to false.
463463
* @return string|bool - either sanitized value or FALSE
464464
*/
465465
public function whiteList(string $input, array $allowed, $default=false)
@@ -625,13 +625,13 @@ public function prepareQuery($args):string
625625
$this->ConnectBase();
626626
$query = '';
627627
$raw = array_shift($args);
628-
$array = preg_split('~(\?[nsiuap])~u',$raw,null,PREG_SPLIT_DELIM_CAPTURE);
628+
$array = preg_split('~(\?[nsiuap])~u',$raw,-1,PREG_SPLIT_DELIM_CAPTURE);
629629
$arguments_num = count($args);
630630
$placeholders_num = (int)floor(count($array) / 2);
631631
if ( $placeholders_num !== $arguments_num )
632632
{
633633
$this->ShowError("Number of args ($arguments_num) doesn't match number of placeholders ($placeholders_num) in [$raw]", true);
634-
die();
634+
return '';
635635
}
636636

637637
foreach ($array as $i => $part)
@@ -678,7 +678,7 @@ protected function escapeInt($value)
678678
if(!is_numeric($value))
679679
{
680680
$this->ShowError( "Integer (?i) placeholder expects numeric value, ".gettype($value)." given", true);
681-
die();
681+
return '';
682682
}
683683
if (is_float($value))
684684
{
@@ -745,7 +745,7 @@ protected function createSET($data):string
745745
foreach ($data as $key => $value)
746746
{
747747
// Check if value is DB::pure object
748-
if (is_numeric($value))
748+
if (is_int($value) || is_float($value))
749749
{
750750
$str = $this->escapeInt($value);
751751
} elseif (is_object($value) && isset($value->sql)) {

0 commit comments

Comments
 (0)