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