2013-07-07 06:49:38 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* This file contains the class that defines user defined functions for PDO library.
|
|
|
|
*
|
|
|
|
* This file is for PHP 5.2.x or lesser. For PHP 5.3.x or later, functions.php is
|
|
|
|
* loaded. PHP version is checked in the db.php and that will load the apropriate
|
|
|
|
* one.
|
|
|
|
*
|
2013-07-07 06:49:38 +00:00
|
|
|
* @package SQLite Integration
|
2014-01-29 09:35:06 +00:00
|
|
|
* @author Kojima Toshiyasu
|
2013-07-07 06:49:38 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* This class defines user defined functions(UDFs) for PDO library.
|
|
|
|
*
|
|
|
|
* These functions replace those used in the SQL statement with the PHP functions.
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* new PDOSQLiteUDFS(ref_to_pdo_obj);
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* This automatically enables ref_to_pdo_obj to replace the function in the SQL statement
|
|
|
|
* to the ones defined here.
|
2013-07-07 06:49:38 +00:00
|
|
|
*/
|
|
|
|
class PDOSQLiteUDFS {
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* Initialize the user defined functions to the PDO object with PDO::sqliteCreateFunction().
|
|
|
|
*
|
|
|
|
* @param reference to PDO object $pdo
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function __construct(&$pdo){
|
|
|
|
foreach ($this->functions as $f=>$t) {
|
|
|
|
$pdo->sqliteCreateFunction($f, array($this, $t));
|
|
|
|
}
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Array to associate MySQL function to the substituted one.
|
|
|
|
*
|
|
|
|
* @var associative array
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
private $functions = array(
|
|
|
|
'month' => 'month',
|
|
|
|
'year' => 'year',
|
|
|
|
'day' => 'day',
|
|
|
|
'unix_timestamp' => 'unix_timestamp',
|
|
|
|
'now' => 'now',
|
|
|
|
'char_length' => 'char_length',
|
|
|
|
'md5' => 'md5',
|
|
|
|
'curdate' => 'curdate',
|
|
|
|
'rand' => 'rand',
|
|
|
|
'substring' => 'substring',
|
|
|
|
'dayofmonth' => 'day',
|
|
|
|
'second' => 'second',
|
|
|
|
'minute' => 'minute',
|
|
|
|
'hour' => 'hour',
|
|
|
|
'date_format' => 'dateformat',
|
|
|
|
'from_unixtime' => 'from_unixtime',
|
|
|
|
'date_add' => 'date_add',
|
|
|
|
'date_sub' => 'date_sub',
|
|
|
|
'adddate' => 'date_add',
|
|
|
|
'subdate' => 'date_sub',
|
|
|
|
'localtime' => 'now',
|
|
|
|
'localtimestamp' => 'now',
|
|
|
|
'isnull' => 'isnull',
|
|
|
|
'if' => '_if',
|
|
|
|
'regexpp' => 'regexp',
|
|
|
|
'concat' => 'concat',
|
|
|
|
'field' => 'field',
|
|
|
|
'log' => 'log',
|
|
|
|
'least' => 'least',
|
2014-01-29 09:35:06 +00:00
|
|
|
'greatest' => 'greatest',
|
2013-07-07 06:49:38 +00:00
|
|
|
'get_lock' => 'get_lock',
|
|
|
|
'release_lock' => 'release_lock',
|
|
|
|
'ucase' => 'ucase',
|
|
|
|
'lcase' => 'lcase',
|
|
|
|
'inet_ntoa' => 'inet_ntoa',
|
|
|
|
'inet_aton' => 'inet_aton',
|
2013-07-24 06:47:44 +00:00
|
|
|
'datediff' => 'datediff',
|
2013-09-15 21:04:53 +00:00
|
|
|
'locate' => 'locate',
|
|
|
|
'version' => 'version'
|
2013-07-07 06:49:38 +00:00
|
|
|
);
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to extract the month value from the date.
|
|
|
|
*
|
|
|
|
* @param string representing the date formated as 0000-00-00.
|
|
|
|
* @return string representing the number of the month between 1 and 12.
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function month($field){
|
|
|
|
$t = strtotime($field);
|
|
|
|
return date('n', $t);
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to extract the year value from the date.
|
|
|
|
*
|
|
|
|
* @param string representing the date formated as 0000-00-00.
|
|
|
|
* @return string representing the number of the year.
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function year($field){
|
|
|
|
$t = strtotime($field);
|
|
|
|
return date('Y', $t);
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to extract the day value from the date.
|
|
|
|
*
|
|
|
|
* @param string representing the date formated as 0000-00-00.
|
|
|
|
* @return string representing the number of the day of the month from 1 and 31.
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function day($field){
|
|
|
|
$t = strtotime($field);
|
|
|
|
return date('j', $t);
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to return the unix timestamp.
|
|
|
|
*
|
|
|
|
* Used without an argument, it returns PHP time() function (total seconds passed
|
|
|
|
* from '1970-01-01 00:00:00' GMT). Used with the argument, it changes the value
|
|
|
|
* to the timestamp.
|
|
|
|
*
|
|
|
|
* @param string representing the date formated as '0000-00-00 00:00:00'.
|
|
|
|
* @return number of unsigned integer
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function unix_timestamp($field = null){
|
|
|
|
return is_null($field) ? time() : strtotime($field);
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL SECOND() function.
|
|
|
|
*
|
|
|
|
* @param string representing the time formated as '00:00:00'.
|
|
|
|
* @return number of unsigned integer
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function second($field){
|
|
|
|
$t = strtotime($field);
|
|
|
|
return intval( date("s", $t) );
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL MINUTE() function.
|
|
|
|
*
|
|
|
|
* @param string representing the time formated as '00:00:00'.
|
|
|
|
* @return number of unsigned integer
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function minute($field){
|
|
|
|
$t = strtotime($field);
|
|
|
|
return intval(date("i", $t));
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL HOUR() function.
|
|
|
|
*
|
|
|
|
* @param string representing the time formated as '00:00:00'.
|
|
|
|
* @return number
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function hour($time){
|
|
|
|
list($hours, $minutes, $seconds) = explode(":", $time);
|
|
|
|
return intval($hours);
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL FROM_UNIXTIME() function.
|
|
|
|
*
|
|
|
|
* @param integer of unix timestamp
|
|
|
|
* @param string to indicate the way of formatting(optional)
|
|
|
|
* @return string formatted as '0000-00-00 00:00:00'.
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function from_unixtime($field, $format=null){
|
|
|
|
// $field is a timestamp
|
|
|
|
//convert to ISO time
|
|
|
|
$date = date("Y-m-d H:i:s", $field);
|
|
|
|
//now submit to dateformat
|
|
|
|
|
|
|
|
return is_null($format) ? $date : $self->dateformat($date, $format);
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL NOW() function.
|
|
|
|
*
|
|
|
|
* @return string representing current time formatted as '0000-00-00 00:00:00'.
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function now(){
|
|
|
|
return date("Y-m-d H:i:s");
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL CURDATE() function.
|
|
|
|
*
|
|
|
|
* @return string representing current time formatted as '0000-00-00'.
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function curdate() {
|
|
|
|
return date("Y-m-d");
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL CHAR_LENGTH() function.
|
|
|
|
*
|
|
|
|
* @param string
|
|
|
|
* @return unsigned integer for the length of the argument.
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function char_length($field){
|
|
|
|
return strlen($field);
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL MD5() function.
|
|
|
|
*
|
|
|
|
* @param string
|
|
|
|
* @return string of the md5 hash value of the argument.
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function md5($field){
|
|
|
|
return md5($field);
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL RAND() function.
|
|
|
|
*
|
|
|
|
* SQLite does have a random generator, but it is called RANDOM() and returns random
|
|
|
|
* number between -9223372036854775808 and +9223372036854775807. So we substitute it
|
|
|
|
* with PHP random generator.
|
|
|
|
*
|
|
|
|
* This function uses mt_rand() which is four times faster than rand() and returns
|
|
|
|
* the random number between 0 and 1.
|
|
|
|
*
|
|
|
|
* @return unsigned integer
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function rand(){
|
2014-01-29 09:35:06 +00:00
|
|
|
return mt_rand(0,1);
|
2013-07-07 06:49:38 +00:00
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL SUBSTRING() function.
|
|
|
|
*
|
|
|
|
* This function rewrites the function name to SQLite compatible substr().
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @param integer $pos representing the start point.
|
|
|
|
* @param integer $len representing the length of the substring(optional).
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function substring($text, $pos, $len=null){
|
2014-01-29 09:35:06 +00:00
|
|
|
return "substr($text, $pos, $len)";
|
2013-07-07 06:49:38 +00:00
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL DATEFORMAT() function.
|
|
|
|
*
|
|
|
|
* @param string date formatted as '0000-00-00' or datetime as '0000-00-00 00:00:00'.
|
|
|
|
* @param string $format
|
|
|
|
* @return string formatted according to $format
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function dateformat($date, $format){
|
|
|
|
$mysql_php_dateformats = array ( '%a' => 'D', '%b' => 'M', '%c' => 'n', '%D' => 'jS', '%d' => 'd', '%e' => 'j', '%H' => 'H', '%h' => 'h', '%I' => 'h', '%i' => 'i', '%j' => 'z', '%k' => 'G', '%l' => 'g', '%M' => 'F', '%m' => 'm', '%p' => 'A', '%r' => 'h:i:s A', '%S' => 's', '%s' => 's', '%T' => 'H:i:s', '%U' => 'W', '%u' => 'W', '%V' => 'W', '%v' => 'W', '%W' => 'l', '%w' => 'w', '%X' => 'Y', '%x' => 'o', '%Y' => 'Y', '%y' => 'y', );
|
|
|
|
$t = strtotime($date);
|
|
|
|
$format = strtr($format, $mysql_php_dateformats);
|
|
|
|
$output = date($format, $t);
|
|
|
|
return $output;
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL DATE_ADD() function.
|
|
|
|
*
|
|
|
|
* This function adds the time value of $interval expression to $date.
|
|
|
|
* $interval is a single quoted strings rewritten by SQLiteQueryDriver::rewrite_query().
|
|
|
|
* It is calculated in the private function deriveInterval().
|
|
|
|
*
|
|
|
|
* @param string $date representing the start date.
|
|
|
|
* @param string $interval representing the expression of the time to add.
|
|
|
|
* @return string date formated as '0000-00-00 00:00:00'.
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function date_add($date, $interval) {
|
|
|
|
$interval = $this->deriveInterval($interval);
|
|
|
|
switch (strtolower($date)) {
|
|
|
|
case "curdate()":
|
|
|
|
$objDate = new Datetime($this->curdate());
|
|
|
|
$objDate->add(new DateInterval($interval));
|
|
|
|
$returnval = $objDate->format("Y-m-d");
|
|
|
|
break;
|
|
|
|
case "now()":
|
|
|
|
$objDate = new Datetime($this->now());
|
|
|
|
$objDate->add(new DateInterval($interval));
|
|
|
|
$returnval = $objDate->format("Y-m-d H:i:s");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$objDate = new Datetime($date);
|
|
|
|
$objDate->add(new DateInterval($interval));
|
|
|
|
$returnval = $objDate->format("Y-m-d H:i:s");
|
|
|
|
}
|
|
|
|
return $returnval;
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL DATE_SUB() function.
|
|
|
|
*
|
|
|
|
* This function substracts the time value of $interval expression from $date.
|
|
|
|
* $interval is a single quoted strings rewritten by SQLiteQueryDriver::rewrite_query().
|
|
|
|
* It is calculated in the private function deriveInterval().
|
|
|
|
*
|
|
|
|
* @param string $date representing the start date.
|
|
|
|
* @param string $interval representing the expression of the time to substract.
|
|
|
|
* @return string date formated as '0000-00-00 00:00:00'.
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function date_sub($date, $interval) {
|
|
|
|
$interval = $this->deriveInterval($interval);
|
|
|
|
switch (strtolower($date)) {
|
|
|
|
case "curdate()":
|
|
|
|
$objDate = new Datetime($this->curdate());
|
|
|
|
$objDate->sub(new DateInterval($interval));
|
|
|
|
$returnval = $objDate->format("Y-m-d");
|
|
|
|
break;
|
|
|
|
case "now()":
|
|
|
|
$objDate = new Datetime($this->now());
|
|
|
|
$objDate->sub(new DateInterval($interval));
|
|
|
|
$returnval = $objDate->format("Y-m-d H:i:s");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$objDate = new Datetime($date);
|
|
|
|
$objDate->sub(new DateInterval($interval));
|
|
|
|
$returnval = $objDate->format("Y-m-d H:i:s");
|
|
|
|
}
|
|
|
|
return $returnval;
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to calculate the interval time between two dates value.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param string $interval white space separated expression.
|
|
|
|
* @return string representing the time to add or substract.
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
private function deriveInterval($interval){
|
|
|
|
$interval = trim(substr(trim($interval), 8));
|
|
|
|
$parts = explode(' ', $interval);
|
|
|
|
foreach($parts as $part){
|
|
|
|
if (!empty($part)){
|
|
|
|
$_parts[] = $part;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$type = strtolower(end($_parts));
|
|
|
|
switch ($type){
|
|
|
|
case "second":
|
|
|
|
case "minute":
|
|
|
|
case "hour":
|
|
|
|
case "day":
|
|
|
|
case "week":
|
|
|
|
case "month":
|
|
|
|
case "year":
|
|
|
|
if (intval($_parts[0]) > 1){
|
|
|
|
$type .= 's';
|
|
|
|
}
|
|
|
|
return "$_parts[0] $_parts[1]";
|
|
|
|
break;
|
|
|
|
case "minute_second":
|
|
|
|
list($minutes, $seconds) = explode (':', $_parts[0]);
|
|
|
|
$minutes = intval($minutes);
|
|
|
|
$seconds = intval($seconds);
|
|
|
|
$minutes = ($minutes > 1) ? "$minutes minutes" : "$minutes minute";
|
|
|
|
$seconds = ($seconds > 1) ? "$seconds seconds" : "$seconds second";
|
|
|
|
return "$minutes $seconds";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "hour_second":
|
|
|
|
list($hours, $minutes, $seconds) = explode (':', $_parts[0]);
|
|
|
|
$hours = intval($hours);
|
|
|
|
$minutes = intval($minutes);
|
|
|
|
$seconds = intval($seconds);
|
|
|
|
$hours = ($hours > 1) ? "$hours hours" : "$hours hour";
|
|
|
|
$minutes = ($minutes > 1) ? "$minutes minutes" : "$minutes minute";
|
|
|
|
$seconds = ($seconds > 1) ? "$seconds seconds" : "$seconds second";
|
|
|
|
return "$hours $minutes $seconds";
|
|
|
|
break;
|
|
|
|
case "hour_minute":
|
|
|
|
list($hours, $minutes) = explode (':', $_parts[0]);
|
|
|
|
$hours = intval($hours);
|
|
|
|
$minutes = intval($minutes);
|
|
|
|
$hours = ($hours > 1) ? "$hours hours" : "$hours hour";
|
|
|
|
$minutes = ($minutes > 1) ? "$minutes minutes" : "$minutes minute";
|
|
|
|
return "$hours $minutes";
|
|
|
|
break;
|
|
|
|
case "day_second":
|
|
|
|
$days = intval($_parts[0]);
|
|
|
|
list($hours, $minutes, $seconds) = explode (':', $_parts[1]);
|
|
|
|
$hours = intval($hours);
|
|
|
|
$minutes = intval($minutes);
|
|
|
|
$seconds = intval($seconds);
|
|
|
|
$days = $days > 1 ? "$days days" : "$days day";
|
|
|
|
$hours = ($hours > 1) ? "$hours hours" : "$hours hour";
|
|
|
|
$minutes = ($minutes > 1) ? "$minutes minutes" : "$minutes minute";
|
|
|
|
$seconds = ($seconds > 1) ? "$seconds seconds" : "$seconds second";
|
|
|
|
return "$days $hours $minutes $seconds";
|
|
|
|
break;
|
|
|
|
case "day_minute":
|
|
|
|
$days = intval($_parts[0]);
|
|
|
|
list($hours, $minutes) = explode (':', $_parts[1]);
|
|
|
|
$hours = intval($hours);
|
|
|
|
$minutes = intval($minutes);
|
|
|
|
$days = $days > 1 ? "$days days" : "$days day";
|
|
|
|
$hours = ($hours > 1) ? "$hours hours" : "$hours hour";
|
|
|
|
$minutes = ($minutes > 1) ? "$minutes minutes" : "$minutes minute";
|
|
|
|
return "$days $hours $minutes";
|
|
|
|
break;
|
|
|
|
case "day_hour":
|
|
|
|
$days = intval($_parts[0]);
|
|
|
|
$hours = intval($_parts[1]);
|
|
|
|
$days = $days > 1 ? "$days days" : "$days day";
|
|
|
|
$hours = ($hours > 1) ? "$hours hours" : "$hours hour";
|
|
|
|
return "$days $hours";
|
|
|
|
break;
|
|
|
|
case "year_month":
|
|
|
|
list($years, $months) = explode ('-', $_parts[0]);
|
|
|
|
$years = intval($years);
|
|
|
|
$months = intval($months);
|
|
|
|
$years = ($years > 1) ? "$years years" : "$years year";
|
|
|
|
$months = ($months > 1) ? "$months months": "$months month";
|
|
|
|
return "$years $months";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL DATE() function.
|
|
|
|
*
|
|
|
|
* @param string $date formatted as unix time.
|
|
|
|
* @return string formatted as '0000-00-00'.
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function date($date){
|
|
|
|
return date("Y-m-d", strtotime($date));
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL ISNULL() function.
|
|
|
|
*
|
|
|
|
* This function returns true if the argument is null, and true if not.
|
|
|
|
*
|
|
|
|
* @param various types $field
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function isnull($field){
|
|
|
|
return is_null($field);
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL IF() function.
|
|
|
|
*
|
|
|
|
* As 'IF' is a reserved word for PHP, function name must be changed.
|
|
|
|
*
|
|
|
|
* @param unknonw $expression the statement to be evaluated as true or false.
|
|
|
|
* @param unknown $true statement or value returned if $expression is true.
|
|
|
|
* @param unknown $false statement or value returned if $expression is false.
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function _if($expression, $true, $false){
|
|
|
|
return ($expression == true) ? $true : $false;
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL REGEXP() function.
|
|
|
|
*
|
|
|
|
* @param string $field haystack
|
|
|
|
* @param string $pattern: regular expression to match.
|
|
|
|
* @return integer 1 if matched, 0 if not matched.
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function regexp($field, $pattern){
|
|
|
|
$pattern = str_replace('/', '\/', $pattern);
|
|
|
|
$pattern = "/" . $pattern ."/i";
|
|
|
|
return preg_match ($pattern, $field);
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL CONCAT() function.
|
|
|
|
*
|
|
|
|
* SQLite does have CONCAT() function, but it has a different syntax from MySQL.
|
|
|
|
* So this function must be manipulated here.
|
|
|
|
*
|
|
|
|
* @param string
|
|
|
|
* @return NULL if the argument is null | string conatenated if the argument is given.
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function concat() {
|
|
|
|
$returnValue = "";
|
|
|
|
$argsNum = func_num_args();
|
|
|
|
$argsList = func_get_args();
|
|
|
|
for ($i = 0; $i < $argsNum; $i++) {
|
|
|
|
if (is_null($argsList[$i])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$returnValue .= $argsList[$i];
|
|
|
|
}
|
|
|
|
return $returnValue;
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL FIELD() function.
|
|
|
|
*
|
|
|
|
* This function gets the list argument and compares the first item to all the others.
|
|
|
|
* If the same value is found, it returns the position of that value. If not, it
|
|
|
|
* returns 0.
|
|
|
|
*
|
|
|
|
* @param variable number of string, integer or double
|
|
|
|
* @return unsigned integer
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function field() {
|
|
|
|
$numArgs = func_num_args();
|
|
|
|
if ($numArgs < 2 or is_null(func_get_arg(0))) {
|
2014-01-29 09:35:06 +00:00
|
|
|
return 0;
|
2013-07-07 06:49:38 +00:00
|
|
|
}
|
|
|
|
$arr = func_get_args();
|
|
|
|
$searchString = strtolower(array_shift($arr));
|
|
|
|
for ($i = 0; $i < $numArgs-1; $i++) {
|
|
|
|
if ($searchString === strtolower($arr[$i])) return $i + 1;
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
return 0;
|
2013-07-07 06:49:38 +00:00
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL LOG() function.
|
|
|
|
*
|
|
|
|
* Used with one argument, it returns the natural logarithm of X.
|
|
|
|
* <code>
|
|
|
|
* LOG(X)
|
|
|
|
* </code>
|
|
|
|
* Used with two arguments, it returns the natural logarithm of X base B.
|
|
|
|
* <code>
|
|
|
|
* LOG(B, X)
|
|
|
|
* </code>
|
|
|
|
* In this case, it returns the value of log(X) / log(B).
|
|
|
|
*
|
|
|
|
* Used without an argument, it returns false. This returned value will be
|
|
|
|
* rewritten to 0, because SQLite doesn't understand true/false value.
|
|
|
|
*
|
|
|
|
* @param integer representing the base of the logarithm, which is optional.
|
|
|
|
* @param double value to turn into logarithm.
|
|
|
|
* @return double | NULL
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function log() {
|
|
|
|
$numArgs = func_num_args();
|
|
|
|
if ($numArgs == 1) {
|
|
|
|
$arg1 = func_get_arg(0);
|
|
|
|
return log($arg1);
|
|
|
|
} else if ($numArgs == 2) {
|
|
|
|
$arg1 = func_get_arg(0);
|
|
|
|
$arg2 = func_get_arg(1);
|
|
|
|
return log($arg1)/log($arg2);
|
|
|
|
} else {
|
2014-01-29 09:35:06 +00:00
|
|
|
return null;
|
2013-07-07 06:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL LEAST() function.
|
|
|
|
*
|
|
|
|
* This function rewrites the function name to SQLite compatible function name.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function least() {
|
2014-01-29 09:35:06 +00:00
|
|
|
$arg_list = func_get_args();
|
|
|
|
return "min($arg_list)";
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Method to emulate MySQL GREATEST() function.
|
|
|
|
*
|
|
|
|
* This function rewrites the function name to SQLite compatible function name.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function greatest() {
|
|
|
|
$arg_list = func_get_args();
|
|
|
|
return "max($arg_list)";
|
2013-07-07 06:49:38 +00:00
|
|
|
}
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Method to dummy out MySQL GET_LOCK() function.
|
|
|
|
*
|
|
|
|
* This function is meaningless in SQLite, so we do nothing.
|
|
|
|
*
|
2013-07-07 06:49:38 +00:00
|
|
|
* @param string $name
|
|
|
|
* @param integer $timeout
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_lock($name, $timeout) {
|
|
|
|
return '1=1';
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to dummy out MySQL RELEASE_LOCK() function.
|
|
|
|
*
|
|
|
|
* This function is meaningless in SQLite, so we do nothing.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function release_lock($name) {
|
|
|
|
return '1=1';
|
|
|
|
}
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Method to emulate MySQL UCASE() function.
|
|
|
|
*
|
|
|
|
* This is MySQL alias for upper() function. This function rewrites it
|
|
|
|
* to SQLite compatible name upper().
|
|
|
|
*
|
|
|
|
* @param string
|
|
|
|
* @return string SQLite compatible function name.
|
2013-07-07 06:49:38 +00:00
|
|
|
*/
|
|
|
|
public function ucase($string) {
|
|
|
|
return "upper($string)";
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL LCASE() function.
|
|
|
|
*
|
|
|
|
* This is MySQL alias for lower() function. This function rewrites it
|
|
|
|
* to SQLite compatible name lower().
|
|
|
|
*
|
|
|
|
* @param string
|
|
|
|
* @return string SQLite compatible function name.
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function lcase($string) {
|
|
|
|
return "lower($string)";
|
|
|
|
}
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Method to emulate MySQL INET_NTOA() function.
|
|
|
|
*
|
|
|
|
* This function gets 4 or 8 bytes integer and turn it into the network address.
|
|
|
|
*
|
|
|
|
* @param unsigned long integer
|
|
|
|
* @return string
|
2013-07-07 06:49:38 +00:00
|
|
|
*/
|
|
|
|
public function inet_ntoa($num) {
|
|
|
|
return long2ip($num);
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Method to emulate MySQL INET_ATON() function.
|
|
|
|
*
|
|
|
|
* This function gets the network address and turns it into integer.
|
|
|
|
*
|
|
|
|
* @param string
|
|
|
|
* @return unsigned long integer
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
public function inet_aton($addr) {
|
|
|
|
$int_data = ip2long($addr);
|
|
|
|
$unsigned_int_data = sprintf('%u', $address);
|
|
|
|
return $unsigned_int_data;
|
|
|
|
}
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Method to emulate MySQL DATEDIFF() function.
|
|
|
|
*
|
|
|
|
* This function compares two dates value and returns the difference.
|
|
|
|
*
|
|
|
|
* PHP 5.3.2 has a serious bug in DateTime::diff(). So if users' PHP is that version,
|
|
|
|
* we don't use that function. See https://bugs.php.net/bug.php?id=51184.
|
|
|
|
*
|
|
|
|
* @param string start
|
|
|
|
* @param string end
|
2013-07-07 06:49:38 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function datediff($start, $end) {
|
|
|
|
$start_date = strtotime($start);
|
|
|
|
$end_date = strtotime($end);
|
|
|
|
$interval = floor(($end_date - $start_date)/(3600*24));
|
|
|
|
return $interval;
|
|
|
|
}
|
2013-07-24 06:47:44 +00:00
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Method to emulate MySQL LOCATE() function.
|
|
|
|
*
|
|
|
|
* This function returns the position if $substr is found in $str. If not,
|
|
|
|
* it returns 0. If mbstring extension is loaded, mb_strpos() function is
|
|
|
|
* used.
|
|
|
|
*
|
|
|
|
* @param string needle
|
|
|
|
* @param string haystack
|
|
|
|
* @param integer position
|
|
|
|
* @return integer
|
2013-07-24 06:47:44 +00:00
|
|
|
*/
|
|
|
|
public function locate($substr, $str, $pos = 0) {
|
|
|
|
if (!extension_loaded('mbstring')) {
|
|
|
|
if (($val = stros($str, $substr, $pos)) !== false) {
|
|
|
|
return $val + 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (($val = mb_strpos($str, $substr, $pos)) !== false) {
|
|
|
|
return $val + 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-15 21:04:53 +00:00
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Method to return MySQL version.
|
|
|
|
*
|
|
|
|
* This function only returns WordPress $required_mysql_version, because it is
|
|
|
|
* meaningless for SQLite database.
|
2013-09-15 21:04:53 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* @param none
|
|
|
|
* @return string representing the version number
|
2013-09-15 21:04:53 +00:00
|
|
|
*/
|
|
|
|
public function version() {
|
|
|
|
global $required_mysql_version;
|
|
|
|
return $required_mysql_version;
|
|
|
|
}
|
2013-07-07 06:49:38 +00:00
|
|
|
}
|
|
|
|
?>
|