Commit 0d75147c authored by zYne's avatar zYne

Doctrine_Expression classes added, fixes #195, #196, #197, #198

Ticket: 195
parent ff85f8c6
......@@ -30,7 +30,7 @@ class Doctrine_DataDict {
protected $dbh;
public function __construct(PDO $dbh) {
public function __construct($dbh = null) {
$file = Doctrine::getPath().DIRECTORY_SEPARATOR."Doctrine".DIRECTORY_SEPARATOR."adodb-hack".DIRECTORY_SEPARATOR."adodb.inc.php";
if( ! file_exists($file))
......@@ -39,6 +39,7 @@ class Doctrine_DataDict {
require_once($file);
$this->dbh = $dbh;
if($dbh)
$this->dict = NewDataDictionary($dbh);
}
/**
......
......@@ -29,7 +29,7 @@
* @version $Id$
*/
class Doctrine_DataDict_Mysql extends Doctrine_DataDict {
class Doctrine_DataDict_Pgsql extends Doctrine_DataDict {
/**
* Obtain DBMS specific SQL code portion needed to declare an text type
* field to be used in statements like CREATE TABLE.
......@@ -53,7 +53,7 @@ class Doctrine_DataDict_Mysql extends Doctrine_DataDict {
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
*/
public function getTypeDeclaration(array $field) {
public function getNativeDeclaration(array $field) {
switch ($field['type']) {
case 'string':
case 'array':
......@@ -115,8 +115,8 @@ class Doctrine_DataDict_Mysql extends Doctrine_DataDict {
* @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
* @return array containing the various possible types, length, sign, fixed
*/
public function mapNativeDatatype($field) {
$db_type = preg_replace('/\d/','', strtolower($field['type']) );
public function getDoctrineDeclaration(array $field) {
$length = $field['length'];
if ($length == '-1' && !empty($field['atttypmod'])) {
$length = $field['atttypmod'] - 4;
......@@ -126,7 +126,7 @@ class Doctrine_DataDict_Mysql extends Doctrine_DataDict {
}
$type = array();
$unsigned = $fixed = null;
switch ($db_type) {
switch ($field['type']) {
case 'smallint':
case 'int2':
$type[] = 'integer';
......@@ -159,7 +159,7 @@ class Doctrine_DataDict_Mysql extends Doctrine_DataDict {
case 'bool':
case 'boolean':
$type[] = 'boolean';
$length = null;
$length = 1;
break;
case 'text':
case 'varchar':
......
<?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>.
*/
/**
* Doctrine_Expression
*
* @package Doctrine ORM
* @url www.phpdoctrine.com
* @license LGPL
*/
class Doctrine_Expression {
/**
* @var Doctrine_Connection $connection
*/
protected $conn;
/**
* @param Doctrine_Connection $conn
*/
public function __construct(Doctrine_Connection $conn) {
$this->conn = $conn;
}
/**
* Returns the average value of a column
*
* @param string $column the column to use
* @return string generated sql including an AVG aggregate function
*/
public function avg($column) {
$column = $this->getIdentifier($column);
return 'AVG(' . $column . ')';
}
/**
* Returns the number of rows (without a NULL value) of a column
*
* If a '*' is used instead of a column the number of selected rows
* is returned.
*
* @param string|integer $column the column to use
* @return string generated sql including a COUNT aggregate function
*/
public function count($column) {
$column = $this->getIdentifier($column);
return 'COUNT(' . $column . ')';
}
/**
* Returns the highest value of a column
*
* @param string $column the column to use
* @return string generated sql including a MAX aggregate function
*/
public function max($column) {
$column = $this->getIdentifier($column);
return 'MAX(' . $column . ')';
}
/**
* Returns the lowest value of a column
*
* @param string $column the column to use
* @return string
*/
public function min($column) {
$column = $this->getIdentifier($column);
return 'MIN(' . $column . ')';
}
/**
* Returns the total sum of a column
*
* @param string $column the column to use
* @return string
*/
public function sum($column) {
$column = $this->getIdentifier($column);
return 'SUM(' . $column . ')';
}
// scalar functions
/**
* Returns the md5 sum of a field.
*
* Note: Not SQL92, but common functionality
*
* @return string
*/
public function md5($column) {
$column = $this->getIdentifier($column);
return 'MD5(' . $column . ')';
}
/**
* Returns the length of a text field.
*
* @param string $expression1
* @param string $expression2
* @return string
*/
public function length($column) {
$column = $this->getIdentifier($column);
return 'LENGTH(' . $column . ')';
}
/**
* Rounds a numeric field to the number of decimals specified.
*
* @param string $expression1
* @param string $expression2
* @return string
*/
public function round($column, $decimals) {
$column = $this->getIdentifier($column);
return 'ROUND(' . $column . ', ' . $decimals . ')';
}
/**
* Returns the remainder of the division operation
* $expression1 / $expression2.
*
* @param string $expression1
* @param string $expression2
* @return string
*/
public function mod($expression1, $expression2) {
$expression1 = $this->getIdentifier($expression1);
$expression2 = $this->getIdentifier($expression2);
return 'MOD(' . $expression1 . ', ' . $expression2 . ')';
}
/**
* ltrim
* returns the string $str with leading space characters removed
*
* @param string $str literal string or column name
* @return string
*/
public function ltrim($str) {
return 'LTRIM(' . $str . ')';
}
/**
* upper
* Returns the string $str with all characters changed to uppercase according to the current character set mapping.
*
* @param string $str literal string or column name
* @return string
*/
public function upper($str) {
return 'UPPER(' . $str . ')';
}
/**
* lower
* Returns the string $str with all characters changed to lowercase according to the current character set mapping.
*
* @param string $str literal string or column name
* @return string
*/
public function lower($str) {
return 'LOWER(' . $str . ')';
}
/**
* locate
* returns the position of the first occurrence of substring $substr in string $str
*
* @param string $substr literal string
* @param string $str literal string
* @return string
*/
public function locate($substr, $str) {
return 'LOCATE(' . $str . ', ' . $substr . ')';
}
/**
* Returns the current system date.
*
* @return string
*/
public function now() {
return 'NOW()';
}
/**
* Returns part of a string.
*
* Note: Not SQL92, but common functionality.
*
* @param string $value the target $value the string or the string column.
* @param int $from extract from this characeter.
* @param int $len extract this amount of characters.
* @return string sql that extracts part of a string.
*/
public function subString($value, $from, $len = null) {
$value = $this->getIdentifier($value);
if ($len === null)
return 'SUBSTRING(' . $value . ' FROM ' . $from . ')';
else {
$len = $this->getIdentifier($len);
return 'SUBSTRING(' . $value . ' FROM ' . $from . ' FOR ' . $len . ')';
}
}
/**
* Returns a series of strings concatinated
*
* concat() accepts an arbitrary number of parameters. Each parameter
* must contain an expression or an array with expressions.
*
* @param string|array(string) strings that will be concatinated.
*/
public function concat($arg1, $arg2) {
$args = func_get_args();
$cols = $this->getIdentifiers($cols);
return 'CONCAT(' . join(', ', $cols) . ')';
}
}
<?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>.
*/
Doctrine::autoload('Doctrine_Expression');
/**
* Doctrine_Expression_Sqlite
*
* @package Doctrine ORM
* @url www.phpdoctrine.com
* @license LGPL
*/
class Doctrine_Expression_Oracle extends Doctrine_Expression {
/**
* Returns a series of strings concatinated
*
* concat() accepts an arbitrary number of parameters. Each parameter
* must contain an expression
*
* @param string $arg1, $arg2 ... $argN strings that will be concatinated.
* @return string
*/
public function concat($arg1, $arg2) {
$args = func_get_args();
$cols = $this->getIdentifiers( $args );
return join( ' || ' , $cols );
}
}
/*
* $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>.
*/
Doctrine::autoload('Doctrine_Expression');
/**
* Doctrine_Expression_Pgsql
*
* @package Doctrine ORM
* @url www.phpdoctrine.com
* @license LGPL
*/
class Doctrine_Expression_Pgsql extends Doctrine_Expression {
/**
* Returns the md5 sum of a field.
*
* Note: Not SQL92, but common functionality
*
* md5() works with the default PostgreSQL 8 versions.
*
* If you are using PostgreSQL 7.x or older you need
* to make sure that the digest procedure.
* If you use RPMS (Redhat and Mandrake) install the postgresql-contrib
* package. You must then install the procedure by running this shell command:
* <code>
* psql [dbname] < /usr/share/pgsql/contrib/pgcrypto.sql
* </code>
* You should make sure you run this as the postgres user.
*
* @return string
*/
public function md5($column) {
$column = $this->getIdentifier($column);
if ($this->version > 7)
return 'MD5(' . $column . ')';
else
return 'encode(digest(' . $column .', md5), hex)';
}
/**
* Returns part of a string.
*
* Note: Not SQL92, but common functionality.
*
* @param string $value the target $value the string or the string column.
* @param int $from extract from this characeter.
* @param int $len extract this amount of characters.
* @return string sql that extracts part of a string.
*/
public function subString($value, $from, $len = null) {
$value = $this->getIdentifier($value);
if ($len === null) {
$len = $this->getIdentifier($len);
return 'SUBSTR(' . $value . ', ' . $from . ')';
} else
return 'SUBSTR(' . $value . ', ' . $from . ', ' . $len . ')';
}
/**
* Returns a series of strings concatinated
*
* concat() accepts an arbitrary number of parameters. Each parameter
* must contain an expression or an array with expressions.
*
* @param string|array(string) strings that will be concatinated.
* @return string
*/
public function concat($arg1, $arg2) {
$args = func_get_args();
$cols = $this->getIdentifiers($cols);
return join(' || ' , $cols);
}
}
<?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>.
*/
Doctrine::autoload('Doctrine_Expression');
/**
* Doctrine_Expression_Sqlite
*
* @package Doctrine ORM
* @url www.phpdoctrine.com
* @license LGPL
*/
class Doctrine_Expression_Sqlite extends Doctrine_Expression {
/**
* Returns part of a string.
*
* Note: Not SQL92, but common functionality. SQLite only supports the 3
* parameter variant of this function, so we are using 2^30-1 as
* artificial length in that case.
*
* @param string $value the target $value the string or the string column.
* @param int $from extract from this characeter.
* @param int $len extract this amount of characters.
* @return string sql that extracts part of a string.
*/
public function subString($value, $from, $len = null) {
$value = $this->getIdentifier( $value );
if ( $len === null )
$len = 1073741823;
return 'SUBSTR(' . $value . ', ' . $from . ', ' . $len . ')';
}
}
<?php
class Doctrine_Filter {
private $name;
public function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment