2006-10-26 15:24:35 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.phpdoctrine.com>.
|
|
|
|
*/
|
2007-07-01 15:27:45 +04:00
|
|
|
Doctrine::autoload('Doctrine_Expression_Driver');
|
2006-10-26 15:24:35 +04:00
|
|
|
/**
|
|
|
|
* Doctrine_Expression_Sqlite
|
|
|
|
*
|
2006-11-16 16:19:45 +03:00
|
|
|
* @package Doctrine
|
2007-10-04 01:43:22 +04:00
|
|
|
* @subpackage Expression
|
2006-11-16 16:19:45 +03:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision$
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
*/
|
2007-07-01 15:27:45 +04:00
|
|
|
class Doctrine_Expression_Sqlite extends Doctrine_Expression_Driver
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2006-11-02 16:34:33 +03:00
|
|
|
/**
|
|
|
|
* Returns the md5 sum of the data that SQLite's md5() function receives.
|
|
|
|
*
|
2006-11-15 18:19:55 +03:00
|
|
|
* @param mixed $data
|
2006-11-02 16:34:33 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public static function md5Impl($data)
|
|
|
|
{
|
2006-11-02 16:34:33 +03:00
|
|
|
return md5($data);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-11-02 16:34:33 +03:00
|
|
|
/**
|
|
|
|
* Returns the modules of the data that SQLite's mod() function receives.
|
|
|
|
*
|
2006-11-15 18:19:55 +03:00
|
|
|
* @param integer $dividend
|
|
|
|
* @param integer $divisor
|
2006-11-02 16:34:33 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public static function modImpl($dividend, $divisor)
|
|
|
|
{
|
2006-11-02 16:34:33 +03:00
|
|
|
return $dividend % $divisor;
|
|
|
|
}
|
|
|
|
|
2006-11-15 18:19:55 +03:00
|
|
|
/**
|
|
|
|
* locate
|
|
|
|
* returns the position of the first occurrence of substring $substr in string $str that
|
|
|
|
* SQLite's locate() function receives
|
|
|
|
*
|
|
|
|
* @param string $substr literal string to find
|
|
|
|
* @param string $str literal string
|
|
|
|
* @return string
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public static function locateImpl($substr, $str)
|
|
|
|
{
|
2006-11-15 18:19:55 +03:00
|
|
|
return strpos($str, $substr);
|
|
|
|
}
|
2006-12-29 17:40:47 +03:00
|
|
|
public static function sha1Impl($str)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
return sha1($str);
|
2006-11-15 18:19:55 +03:00
|
|
|
}
|
2006-12-29 17:40:47 +03:00
|
|
|
public static function ltrimImpl($str)
|
|
|
|
{
|
2006-11-15 18:19:55 +03:00
|
|
|
return ltrim($str);
|
|
|
|
}
|
2006-12-29 17:40:47 +03:00
|
|
|
public static function rtrimImpl($str)
|
|
|
|
{
|
2006-11-15 18:19:55 +03:00
|
|
|
return rtrim($str);
|
|
|
|
}
|
2006-12-29 17:40:47 +03:00
|
|
|
public static function trimImpl($str)
|
|
|
|
{
|
2006-11-15 18:19:55 +03:00
|
|
|
return trim($str);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-10-26 15:24:35 +04:00
|
|
|
/**
|
2006-12-29 17:01:31 +03:00
|
|
|
* returns the regular expression operator
|
2006-10-26 15:24:35 +04:00
|
|
|
*
|
2006-10-31 02:27:26 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function regexp()
|
|
|
|
{
|
2006-10-31 02:27:26 +03:00
|
|
|
return 'RLIKE';
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-11-15 18:19:55 +03:00
|
|
|
/**
|
|
|
|
* soundex
|
2006-12-29 17:01:31 +03:00
|
|
|
* Returns a string to call a function to compute the
|
2006-11-15 18:19:55 +03:00
|
|
|
* soundex encoding of a string
|
|
|
|
*
|
|
|
|
* The string "?000" is returned if the argument is NULL.
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @return string SQL soundex function with given parameter
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function soundex($value)
|
|
|
|
{
|
2006-11-15 18:19:55 +03:00
|
|
|
return 'SOUNDEX(' . $value . ')';
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-10-31 02:27:26 +03:00
|
|
|
/**
|
|
|
|
* Return string to call a variable with the current timestamp inside an SQL statement
|
|
|
|
* There are three special variables for current date and time.
|
|
|
|
*
|
|
|
|
* @return string sqlite function as string
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function now($type = 'timestamp')
|
|
|
|
{
|
2006-10-31 02:27:26 +03:00
|
|
|
switch ($type) {
|
2006-12-30 00:30:37 +03:00
|
|
|
case 'time':
|
|
|
|
return 'time(\'now\')';
|
|
|
|
case 'date':
|
|
|
|
return 'date(\'now\')';
|
|
|
|
case 'timestamp':
|
|
|
|
default:
|
|
|
|
return 'datetime(\'now\')';
|
2006-10-31 02:27:26 +03:00
|
|
|
}
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-10-31 02:27:26 +03:00
|
|
|
/**
|
|
|
|
* return string to call a function to get random value inside an SQL statement
|
|
|
|
*
|
|
|
|
* @return string to generate float between 0 and 1
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function random()
|
|
|
|
{
|
2006-10-31 02:27:26 +03:00
|
|
|
return '((RANDOM() + 2147483648) / 4294967296)';
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-10-31 02:27:26 +03:00
|
|
|
/**
|
|
|
|
* return string to call a function to get a substring inside an SQL statement
|
|
|
|
*
|
2006-12-29 17:01:31 +03:00
|
|
|
* Note: Not SQL92, but common functionality.
|
|
|
|
*
|
2006-10-31 02:27:26 +03:00
|
|
|
* SQLite only supports the 2 parameter variant of this function
|
2006-10-26 15:24:35 +04:00
|
|
|
*
|
2006-10-31 02:27:26 +03:00
|
|
|
* @param string $value an sql string literal or column name/alias
|
|
|
|
* @param integer $position where to start the substring portion
|
|
|
|
* @param integer $length the substring portion length
|
|
|
|
* @return string SQL substring function with given parameters
|
2006-10-26 15:24:35 +04:00
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function substring($value, $position, $length = null)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
if ($length !== null) {
|
2006-10-31 02:27:26 +03:00
|
|
|
return 'SUBSTR(' . $value . ', ' . $position . ', ' . $length . ')';
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-10-31 02:27:26 +03:00
|
|
|
return 'SUBSTR(' . $value . ', ' . $position . ', LENGTH(' . $value . '))';
|
2006-10-26 15:24:35 +04:00
|
|
|
}
|
2007-10-04 01:43:22 +04:00
|
|
|
}
|