@@ -30,14 +30,30 @@ class WP_SQLite_PDO_User_Defined_Functions {
3030 */
3131 public static function register_for ( $ pdo ): self {
3232 $ instance = new self ();
33+ $ register = static function ( $ function_name , $ callback , $ arity ) use ( $ pdo ) {
34+ if ( $ pdo instanceof PDO \SQLite ) {
35+ $ pdo ->createFunction ( $ function_name , $ callback , $ arity );
36+ } else {
37+ $ pdo ->sqliteCreateFunction ( $ function_name , $ callback , $ arity );
38+ }
39+ };
40+
3341 foreach ( $ instance ->functions as $ f => $ t ) {
34- $ arities = $ instance ->function_arities [ $ f ] ?? array ( -1 );
42+ foreach ( $ instance ->function_arities [ $ f ] as $ arity ) {
43+ $ register ( $ f , array ( $ instance , $ t ), $ arity );
44+ }
45+ }
46+ foreach ( $ instance ->unsupported_function_arities as $ f => $ arities ) {
47+ $ reject = static function () use ( $ f ) {
48+ throw new Exception (
49+ sprintf (
50+ "Incorrect parameter count in the call to native function '%s' " ,
51+ strtoupper ( $ f )
52+ )
53+ );
54+ };
3555 foreach ( $ arities as $ arity ) {
36- if ( $ pdo instanceof PDO \SQLite ) {
37- $ pdo ->createFunction ( $ f , array ( $ instance , $ t ), $ arity );
38- } else {
39- $ pdo ->sqliteCreateFunction ( $ f , array ( $ instance , $ t ), $ arity );
40- }
56+ $ register ( $ f , $ reject , $ arity );
4157 }
4258 }
4359 return $ instance ;
@@ -104,17 +120,75 @@ public static function register_for( $pdo ): self {
104120 );
105121
106122 /**
107- * Exact argument counts for functions with optional arguments .
123+ * Supported argument counts for each registered function .
108124 *
109- * Functions absent from this array are registered as variadic.
125+ * An argument count of -1 registers a variadic function .
110126 *
111127 * @var array
112128 */
113129 private $ function_arities = array (
114- 'regexp_like ' => array ( 2 , 3 ),
115- 'regexp_replace ' => array ( 3 , 4 , 5 , 6 ),
116- 'regexp_substr ' => array ( 2 , 3 , 4 , 5 ),
117- 'regexp_instr ' => array ( 2 , 3 , 4 , 5 , 6 ),
130+ 'throw ' => array ( 1 ),
131+ 'month ' => array ( 1 ),
132+ 'monthnum ' => array ( 1 ),
133+ 'year ' => array ( 1 ),
134+ 'day ' => array ( 1 ),
135+ 'hour ' => array ( 1 ),
136+ 'minute ' => array ( 1 ),
137+ 'second ' => array ( 1 ),
138+ 'week ' => array ( 1 , 2 ),
139+ 'weekday ' => array ( 1 ),
140+ 'dayofweek ' => array ( 1 ),
141+ 'dayofmonth ' => array ( 1 ),
142+ 'unix_timestamp ' => array ( 0 , 1 ),
143+ 'now ' => array ( 0 , 1 ),
144+ 'md5 ' => array ( 1 ),
145+ 'curdate ' => array ( 0 ),
146+ 'rand ' => array ( 1 ),
147+ 'from_unixtime ' => array ( 1 , 2 ),
148+ 'localtime ' => array ( 0 , 1 ),
149+ 'localtimestamp ' => array ( 0 , 1 ),
150+ 'isnull ' => array ( 1 ),
151+ 'if ' => array ( 3 ),
152+ 'regexp ' => array ( 2 ),
153+ 'regexp_like ' => array ( 2 , 3 ),
154+ 'regexp_replace ' => array ( 3 , 4 , 5 , 6 ),
155+ 'regexp_substr ' => array ( 2 , 3 , 4 , 5 ),
156+ 'regexp_instr ' => array ( 2 , 3 , 4 , 5 , 6 ),
157+ 'field ' => array ( -1 ),
158+ 'log ' => array ( 1 , 2 ),
159+ 'least ' => array ( -1 ),
160+ 'greatest ' => array ( -1 ),
161+ 'get_lock ' => array ( 2 ),
162+ 'release_lock ' => array ( 1 ),
163+ 'ucase ' => array ( 1 ),
164+ 'lcase ' => array ( 1 ),
165+ 'unhex ' => array ( 1 ),
166+ 'from_base64 ' => array ( 1 ),
167+ 'to_base64 ' => array ( 1 ),
168+ 'inet_ntoa ' => array ( 1 ),
169+ 'inet_aton ' => array ( 1 ),
170+ 'datediff ' => array ( 2 ),
171+ 'locate ' => array ( 2 , 3 ),
172+ 'utc_date ' => array ( 0 ),
173+ 'utc_time ' => array ( 0 , 1 ),
174+ 'utc_timestamp ' => array ( 0 , 1 ),
175+ 'version ' => array ( 0 ),
176+ 'reverse ' => array ( 1 ),
177+ '_helper_like_to_glob_pattern ' => array ( 1 ),
178+ );
179+
180+ /**
181+ * Variadic registrations that shadow incompatible SQLite built-in overloads.
182+ *
183+ * SQLite has variadic IF() and two-argument UNHEX() implementations. These
184+ * catch-all registrations prevent unsupported MySQL calls from falling
185+ * through to those built-ins.
186+ *
187+ * @var array
188+ */
189+ private $ unsupported_function_arities = array (
190+ 'if ' => array ( -1 ),
191+ 'unhex ' => array ( -1 ),
118192 );
119193
120194 /** @var string|null Last validated regex pattern. */
@@ -510,7 +584,7 @@ public function hour( $time ) {
510584 * @param string $field Representing the date.
511585 * @param int $mode The mode argument.
512586 */
513- public function week ( $ field , $ mode ) {
587+ public function week ( $ field , $ mode = 1 ) {
514588 /*
515589 * From https://www.php.net/manual/en/datetime.format.php:
516590 *
@@ -917,7 +991,8 @@ public function regexp_instr( $expr, $pattern, $pos = 1, $occurrence = 1, $retur
917991 */
918992 public function field () {
919993 $ num_args = func_num_args ();
920- if ( $ num_args < 2 || is_null ( func_get_arg ( 0 ) ) ) {
994+ $ this ->require_minimum_argument_count ( 'FIELD ' , $ num_args , 2 );
995+ if ( is_null ( func_get_arg ( 0 ) ) ) {
921996 return 0 ;
922997 }
923998 $ arg_list = func_get_args ();
@@ -974,6 +1049,7 @@ public function log() {
9741049 * @return mixed
9751050 */
9761051 public function least () {
1052+ $ this ->require_minimum_argument_count ( 'LEAST ' , func_num_args (), 2 );
9771053 $ arg_list = func_get_args ();
9781054
9791055 return min ( $ arg_list );
@@ -987,6 +1063,7 @@ public function least() {
9871063 * @return mixed
9881064 */
9891065 public function greatest () {
1066+ $ this ->require_minimum_argument_count ( 'GREATEST ' , func_num_args (), 2 );
9901067 $ arg_list = func_get_args ();
9911068
9921069 return max ( $ arg_list );
@@ -1283,6 +1360,27 @@ public function _helper_like_to_glob_pattern( $pattern ) {
12831360 return $ pattern ;
12841361 }
12851362
1363+ /**
1364+ * Reject a variadic function call with too few arguments.
1365+ *
1366+ * @param string $function_name Function name used in the error message.
1367+ * @param int $actual Actual argument count.
1368+ * @param int $minimum Minimum supported argument count.
1369+ *
1370+ * @throws Exception If the argument count is too low.
1371+ * @return void
1372+ */
1373+ private function require_minimum_argument_count ( $ function_name , $ actual , $ minimum ): void {
1374+ if ( $ actual < $ minimum ) {
1375+ throw new Exception (
1376+ sprintf (
1377+ "Incorrect parameter count in the call to native function '%s' " ,
1378+ $ function_name
1379+ )
1380+ );
1381+ }
1382+ }
1383+
12861384 /**
12871385 * Compile a MySQL-style regex into a PCRE pattern string.
12881386 *
0 commit comments