1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vectorface\MySQLite; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use PDO; |
7
|
|
|
use ReflectionClass; |
8
|
|
|
use ReflectionMethod; |
9
|
|
|
|
10
|
|
|
use Vectorface\MySQLite\MySQL\Aggregate; |
11
|
|
|
use Vectorface\MySQLite\MySQL\Comparison; |
12
|
|
|
use Vectorface\MySQLite\MySQL\DateTime; |
13
|
|
|
use Vectorface\MySQLite\MySQL\Flow; |
14
|
|
|
use Vectorface\MySQLite\MySQL\Numeric; |
15
|
|
|
use Vectorface\MySQLite\MySQL\StringFunctions; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Provides some compatibility functions, allowing SQLite to mimic some MySQL functions. |
19
|
|
|
* |
20
|
|
|
* All public methods starting with a mysql_ will be registered by the addFunctions method. |
21
|
|
|
*/ |
22
|
|
|
class MySQLite |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Individual traits group functions into MySQL's documented function categories. |
26
|
|
|
*/ |
27
|
|
|
use Aggregate; |
28
|
|
|
use Comparison; |
29
|
|
|
use DateTime; |
30
|
|
|
use Flow; |
31
|
|
|
use Numeric; |
32
|
|
|
use StringFunctions; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get a list of MySQL compatibility functions currently provided by this class. |
36
|
|
|
* |
37
|
|
|
* @return string[] An array of function names, normalized to lower case. |
38
|
|
|
*/ |
39
|
1 |
|
public static function getFunctionList() |
40
|
|
|
{ |
41
|
1 |
|
return array_map( |
42
|
1 |
|
function ($f) { |
43
|
1 |
|
return substr($f, 6); |
44
|
1 |
|
}, |
45
|
1 |
|
array_keys(static::getPublicMethodData()) |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Add MySQLite compatibility functions to a PDO object. |
51
|
|
|
* |
52
|
|
|
* @param PDO &$pdo A PDO instance to which the MySQLite compatibility functions should be added. |
53
|
|
|
* @param string[] $fnList A list of functions to create on the SQLite database. (Omit to create all.) |
54
|
|
|
* @return PDO Returns a reference to the PDO instance passed in to the function. |
55
|
|
|
*/ |
56
|
5 |
|
public static function &createFunctions(PDO &$pdo, ?array $fnList = null) |
57
|
|
|
{ |
58
|
5 |
|
if ($pdo->getAttribute(PDO::ATTR_DRIVER_NAME) !== 'sqlite') { |
59
|
1 |
|
throw new InvalidArgumentException('Expecting a PDO instance using the SQLite driver'); |
60
|
|
|
} |
61
|
|
|
|
62
|
5 |
|
foreach (static::getPublicMethodData() as $method => $paramCount) { |
63
|
5 |
|
static::registerMethod($pdo, $method, $paramCount, $fnList); |
64
|
|
|
} |
65
|
|
|
|
66
|
5 |
|
return $pdo; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get information about functions that are meant to be exposed by this class. |
71
|
|
|
* |
72
|
|
|
* @return int[] An associative array composed of function names mapping to accepted parameter counts. |
73
|
|
|
*/ |
74
|
6 |
|
protected static function getPublicMethodData() |
75
|
|
|
{ |
76
|
6 |
|
$data = []; |
77
|
|
|
|
78
|
6 |
|
$ref = new ReflectionClass(__CLASS__); |
79
|
6 |
|
$methods = $ref->getMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_STATIC); |
80
|
6 |
|
foreach ($methods as $method) { |
81
|
6 |
|
if (strpos($method->name, 'mysql_') !== 0) { |
82
|
6 |
|
continue; |
83
|
|
|
} |
84
|
|
|
|
85
|
6 |
|
$data[$method->name] = $method->getNumberOfRequiredParameters(); |
86
|
|
|
} |
87
|
|
|
|
88
|
6 |
|
return $data; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Register a method as an SQLite funtion |
93
|
|
|
* |
94
|
|
|
* @param PDO &$pdo A PDO instance to which the MySQLite compatibility functions should be added. |
95
|
|
|
* @param string $method The internal method name. |
96
|
|
|
* @param int $paramCount The suggested parameter count. |
97
|
|
|
* @param string[] $fnList A list of functions to create on the SQLite database, or empty for all. |
98
|
|
|
* @return bool Returns true if the method was registed. False otherwise. |
99
|
|
|
*/ |
100
|
5 |
|
protected static function registerMethod(PDO &$pdo, $method, $paramCount, ?array $fnList = null) |
101
|
|
|
{ |
102
|
5 |
|
$function = substr($method, 6); /* Strip 'mysql_' prefix to get the function name. */ |
103
|
|
|
|
104
|
|
|
/* Skip functions not in the list. */ |
105
|
5 |
|
if (!empty($fnList) && !in_array($function, $fnList)) { |
106
|
1 |
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
5 |
|
if ($paramCount) { |
110
|
4 |
|
return $pdo->sqliteCreateFunction($function, [__CLASS__, $method], $paramCount); |
111
|
|
|
} |
112
|
5 |
|
return $pdo->sqliteCreateFunction($function, [__CLASS__, $method]); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|