Source of file SqlitePlatform.php
Size: 13,624 Bytes - Last Modified: 2013-05-11T09:45:37+02:00
/home/theseer/Downloads/Symfony/vendor/doctrine/dbal/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531 | <?php /* * 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 MIT license. For more information, see * <http://www.doctrine-project.org>. */ namespace Doctrine\DBAL\Platforms; use Doctrine\DBAL\DBALException; /** * The SqlitePlatform class describes the specifics and dialects of the SQLite * database platform. * * @since 2.0 * @author Roman Borschel <roman@code-factory.org> * @author Benjamin Eberlei <kontakt@beberlei.de> * @todo Rename: SQLitePlatform */ class SqlitePlatform extends AbstractPlatform { /** * {@inheritDoc} */ public function getRegexpExpression() { return 'RLIKE'; } /** * {@inheritDoc} */ public function getNowExpression($type = 'timestamp') { switch ($type) { case 'time': return 'time(\'now\')'; case 'date': return 'date(\'now\')'; case 'timestamp': default: return 'datetime(\'now\')'; } } /** * {@inheritDoc} */ public function getTrimExpression($str, $pos = self::TRIM_UNSPECIFIED, $char = false) { $trimChar = ($char != false) ? (', ' . $char) : ''; switch ($pos) { case self::TRIM_LEADING: $trimFn = 'LTRIM'; break; case self::TRIM_TRAILING: $trimFn = 'RTRIM'; break; default: $trimFn = 'TRIM'; } return $trimFn . '(' . $str . $trimChar . ')'; } /** * {@inheritDoc} * * SQLite only supports the 2 parameter variant of this function */ public function getSubstringExpression($value, $position, $length = null) { if ($length !== null) { return 'SUBSTR(' . $value . ', ' . $position . ', ' . $length . ')'; } return 'SUBSTR(' . $value . ', ' . $position . ', LENGTH(' . $value . '))'; } /** * {@inheritDoc} */ public function getLocateExpression($str, $substr, $startPos = false) { if ($startPos == false) { return 'LOCATE('.$str.', '.$substr.')'; } return 'LOCATE('.$str.', '.$substr.', '.$startPos.')'; } /** * {@inheritDoc} */ public function getDateDiffExpression($date1, $date2) { return 'ROUND(JULIANDAY('.$date1 . ')-JULIANDAY('.$date2.'))'; } /** * {@inheritDoc} */ public function getDateAddDaysExpression($date, $days) { return "DATE(" . $date . ",'+". $days . " day')"; } /** * {@inheritDoc} */ public function getDateSubDaysExpression($date, $days) { return "DATE(" . $date . ",'-". $days . " day')"; } /** * {@inheritDoc} */ public function getDateAddMonthExpression($date, $months) { return "DATE(" . $date . ",'+". $months . " month')"; } /** * {@inheritDoc} */ public function getDateSubMonthExpression($date, $months) { return "DATE(" . $date . ",'-". $months . " month')"; } /** * {@inheritDoc} */ protected function _getTransactionIsolationLevelSQL($level) { switch ($level) { case \Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED: return 0; case \Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED: case \Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ: case \Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE: return 1; default: return parent::_getTransactionIsolationLevelSQL($level); } } /** * {@inheritDoc} */ public function getSetTransactionIsolationSQL($level) { return 'PRAGMA read_uncommitted = ' . $this->_getTransactionIsolationLevelSQL($level); } /** * {@inheritDoc} */ public function prefersIdentityColumns() { return true; } /** * {@inheritDoc} */ public function getBooleanTypeDeclarationSQL(array $field) { return 'BOOLEAN'; } /** * {@inheritDoc} */ public function getIntegerTypeDeclarationSQL(array $field) { return $this->_getCommonIntegerTypeDeclarationSQL($field); } /** * {@inheritDoc} */ public function getBigIntTypeDeclarationSQL(array $field) { return $this->_getCommonIntegerTypeDeclarationSQL($field); } /** * {@inheritDoc} */ public function getTinyIntTypeDeclarationSql(array $field) { return $this->_getCommonIntegerTypeDeclarationSQL($field); } /** * {@inheritDoc} */ public function getSmallIntTypeDeclarationSQL(array $field) { return $this->_getCommonIntegerTypeDeclarationSQL($field); } /** * {@inheritDoc} */ public function getMediumIntTypeDeclarationSql(array $field) { return $this->_getCommonIntegerTypeDeclarationSQL($field); } /** * {@inheritDoc} */ public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration) { return 'DATETIME'; } /** * {@inheritDoc} */ public function getDateTypeDeclarationSQL(array $fieldDeclaration) { return 'DATE'; } /** * {@inheritDoc} */ public function getTimeTypeDeclarationSQL(array $fieldDeclaration) { return 'TIME'; } /** * {@inheritDoc} */ protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef) { return 'INTEGER'; } /** * {@inheritDoc} */ protected function _getCreateTableSQL($name, array $columns, array $options = array()) { $name = str_replace(".", "__", $name); $queryFields = $this->getColumnDeclarationListSQL($columns); if (isset($options['primary']) && ! empty($options['primary'])) { $keyColumns = array_unique(array_values($options['primary'])); $queryFields.= ', PRIMARY KEY('.implode(', ', $keyColumns).')'; } $query[] = 'CREATE TABLE ' . $name . ' (' . $queryFields . ')'; if (isset($options['indexes']) && ! empty($options['indexes'])) { foreach ($options['indexes'] as $index => $indexDef) { $query[] = $this->getCreateIndexSQL($indexDef, $name); } } if (isset($options['unique']) && ! empty($options['unique'])) { foreach ($options['unique'] as $index => $indexDef) { $query[] = $this->getCreateIndexSQL($indexDef, $name); } } return $query; } /** * {@inheritDoc} */ protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed) { return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)') : ($length ? 'VARCHAR(' . $length . ')' : 'TEXT'); } /** * {@inheritDoc} */ public function getClobTypeDeclarationSQL(array $field) { return 'CLOB'; } public function getListTableConstraintsSQL($table) { $table = str_replace(".", "__", $table); return "SELECT sql FROM sqlite_master WHERE type='index' AND tbl_name = '$table' AND sql NOT NULL ORDER BY name"; } public function getListTableColumnsSQL($table, $currentDatabase = null) { $table = str_replace(".", "__", $table); return "PRAGMA table_info($table)"; } /** * {@inheritDoc} */ public function getListTableIndexesSQL($table, $currentDatabase = null) { $table = str_replace(".", "__", $table); return "PRAGMA index_list($table)"; } public function getListTablesSQL() { return "SELECT name FROM sqlite_master WHERE type = 'table' AND name != 'sqlite_sequence' AND name != 'geometry_columns' AND name != 'spatial_ref_sys' " . "UNION ALL SELECT name FROM sqlite_temp_master " . "WHERE type = 'table' ORDER BY name"; } /** * {@inheritDoc} */ public function getListViewsSQL($database) { return "SELECT name, sql FROM sqlite_master WHERE type='view' AND sql NOT NULL"; } public function getCreateViewSQL($name, $sql) { return 'CREATE VIEW ' . $name . ' AS ' . $sql; } public function getDropViewSQL($name) { return 'DROP VIEW '. $name; } /** * {@inheritDoc} * * SQLite does support foreign key constraints, but only in CREATE TABLE statements... * This really limits their usefulness and requires SQLite specific handling, so * we simply say that SQLite does NOT support foreign keys for now... */ public function supportsForeignKeyConstraints() { return false; } /** * {@inheritDoc} */ public function supportsAlterTable() { return false; } /** * {@inheritDoc} */ public function supportsIdentityColumns() { return true; } /** * {@inheritDoc} */ public function getName() { return 'sqlite'; } /** * {@inheritDoc} */ public function getTruncateTableSQL($tableName, $cascade = false) { $tableName = str_replace(".", "__", $tableName); return 'DELETE FROM '.$tableName; } /** * User-defined function for Sqlite that is used with PDO::sqliteCreateFunction() * * @param int|float $value * * @return float */ static public function udfSqrt($value) { return sqrt($value); } /** * User-defined function for Sqlite that implements MOD(a, b) * * @param integer $a * @param integer $b * * @return integer */ static public function udfMod($a, $b) { return ($a % $b); } /** * @param string $str * @param string $substr * @param integer $offset * * @return integer */ static public function udfLocate($str, $substr, $offset = 0) { $pos = strpos($str, $substr, $offset); if ($pos !== false) { return $pos+1; } return 0; } public function getForUpdateSql() { return ''; } /** * {@inheritDoc} */ protected function initializeDoctrineTypeMappings() { $this->doctrineTypeMapping = array( 'boolean' => 'boolean', 'tinyint' => 'boolean', 'smallint' => 'smallint', 'mediumint' => 'integer', 'int' => 'integer', 'integer' => 'integer', 'serial' => 'integer', 'bigint' => 'bigint', 'bigserial' => 'bigint', 'clob' => 'text', 'tinytext' => 'text', 'mediumtext' => 'text', 'longtext' => 'text', 'text' => 'text', 'varchar' => 'string', 'longvarchar' => 'string', 'varchar2' => 'string', 'nvarchar' => 'string', 'image' => 'string', 'ntext' => 'string', 'char' => 'string', 'date' => 'date', 'datetime' => 'datetime', 'timestamp' => 'datetime', 'time' => 'time', 'float' => 'float', 'double' => 'float', 'double precision' => 'float', 'real' => 'float', 'decimal' => 'decimal', 'numeric' => 'decimal', 'blob' => 'blob', 'integer unsigned' => 'integer', ); } /** * {@inheritDoc} */ protected function getReservedKeywordsClass() { return 'Doctrine\DBAL\Platforms\Keywords\SQLiteKeywords'; } /** * {@inheritDoc} */ public function getBlobTypeDeclarationSQL(array $field) { return 'BLOB'; } /** * {@inheritDoc} */ public function getTemporaryTableName($tableName) { $tableName = str_replace(".", "__", $tableName); return $tableName; } /** * {@inheritDoc} * * Sqlite Platform emulates schema by underscoring each dot and generating tables * into the default database. * * This hack is implemented to be able to use SQLite as testdriver when * using schema supporting databases. */ public function canEmulateSchemas() { return true; } } |