11<?php
22namespace Mhorninger \MySQLite ;
33
4+ use \PDO ;
5+ use \Mhorninger \MySQLite \MySQL \DateTimeExtended ;
6+ use \Vectorface \MySQLite \MySQL \Aggregate ;
7+ use \Vectorface \MySQLite \MySQL \Comparison ;
8+ use \Vectorface \MySQLite \MySQL \DateTime ;
9+ use \Vectorface \MySQLite \MySQL \Flow ;
10+ use \Vectorface \MySQLite \MySQL \Numeric ;
11+ use \Vectorface \MySQLite \MySQL \StringFn ;
12+
13+ use ReflectionClass ;
14+ use ReflectionMethod ;
15+
16+ /**
17+ * MySQLite is the extension Vectorface's MySQLite extension.
18+ * @see \Vectorface\MySQLite\MySQLite
19+ */
420class MySQLite extends \Vectorface \MySQLite \MySQLite
521{
22+ use DateTimeExtended;
23+
24+ /**
25+ * Get information about functions that are meant to be exposed by this class.
26+ *
27+ * @return int[] An associative array composed of function names mapping to accepted parameter counts.
28+ */
29+ protected static function getPublicMethodData ()
30+ {
31+ $ data = [];
32+
33+ $ ref = new ReflectionClass (__CLASS__ );
34+ $ methods = $ ref ->getMethods (ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_STATIC );
35+ foreach ($ methods as $ method ) {
36+ if (strpos ($ method ->name , 'mysql_ ' ) !== 0 ) {
37+ continue ;
38+ }
39+
40+ $ data [$ method ->name ] = $ method ->getNumberOfRequiredParameters ();
41+ }
42+
43+ return $ data ;
44+ }
45+
46+ /**
47+ * Add MySQLite compatibility functions to a PDO object.
48+ *
49+ * @param \PDO &$pdo A PDO instance to which the MySQLite compatibility functions should be added.
50+ * @param string[] $fnList A list of functions to create on the SQLite database. (Omit to create all.)
51+ * @return \PDO Returns a reference to the PDO instance passed in to the function.
52+ */
53+ public static function &createFunctions (\PDO &$ pdo , array $ fnList = null )
54+ {
55+ if ($ pdo ->getAttribute (PDO ::ATTR_DRIVER_NAME ) !== 'sqlite ' ) {
56+ throw new InvalidArgumentException ('Expecting a PDO instance using the SQLite driver ' );
57+ }
58+
59+ foreach (static ::getPublicMethodData () as $ method => $ paramCount ) {
60+ static ::registerMethod ($ pdo , $ method , $ paramCount , $ fnList );
61+ }
62+
63+ return $ pdo ;
64+ }
65+
66+ /**
67+ * Register a method as an SQLite funtion
68+ *
69+ * @param PDO &$pdo A PDO instance to which the MySQLite compatibility functions should be added.
70+ * @param string $method The internal method name.
71+ * @param int $paramCount The suggested parameter count.
72+ * @param string[] $fnList A list of functions to create on the SQLite database, or empty for all.
73+ * @return bool Returns true if the method was registed. False otherwise.
74+ */
75+ protected static function registerMethod (\PDO &$ pdo , $ method , $ paramCount , array $ fnList = null )
76+ {
77+ $ function = substr ($ method , 6 ); /* Strip 'mysql_' prefix to get the function name. */
78+
79+ /* Skip functions not in the list. */
80+ if (!empty ($ fnList ) && !in_array ($ function , $ fnList )) {
81+ return false ;
82+ }
683
7- }
84+ if ($ paramCount ) {
85+ return $ pdo ->sqliteCreateFunction ($ function , [__CLASS__ , $ method ], $ paramCount );
86+ }
87+ return $ pdo ->sqliteCreateFunction ($ function , [__CLASS__ , $ method ]);
88+ }
89+ }
0 commit comments