PostgresDialect.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2005-2008 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00019     final class PostgresDialect extends Dialect
00020     {
00021         private static $tsConfiguration = 'utf8_russian';
00022         private static $rankFunction = 'rank';
00023         
00027         public static function me()
00028         {
00029             return Singleton::getInstance(__CLASS__);
00030         }
00031         
00032         public static function getTsConfiguration()
00033         {
00034             return self::$tsConfiguration;
00035         }
00036         
00037         public static function setTsConfiguration($configuration)
00038         {
00039             self::$tsConfiguration = $configuration;
00040         }
00041         
00042         public static function setRankFunction($rank)
00043         {
00044             self::$rankFunction = $rank;
00045         }
00046         
00047         public static function quoteValue($value)
00048         {
00049             return "'".pg_escape_string($value)."'";
00050         }
00051         
00052         public static function toCasted($field, $type)
00053         {
00054             return "{$field}::{$type}";
00055         }
00056         
00057         public static function prepareFullText($words, $logic)
00058         {
00059             Assert::isArray($words);
00060             
00061             $glue = ($logic == DB::FULL_TEXT_AND) ? ' & ' : ' | ';
00062             
00063             return
00064                 strtolower(
00065                     implode(
00066                         $glue,
00067                         array_map(
00068                             array('PostgresDialect', 'quoteValue'),
00069                             $words
00070                         )
00071                     )
00072                 );
00073         }
00074         
00075         public function quoteBinary($data)
00076         {
00077             return pg_escape_bytea($data);
00078         }
00079         
00080         public function unquoteBinary($data)
00081         {
00082             return pg_unescape_bytea($data);
00083         }
00084         
00085         public function typeToString(DataType $type)
00086         {
00087             if ($type->getId() == DataType::BINARY)
00088                 return 'BYTEA';
00089             
00090             return parent::typeToString($type);
00091         }
00092         
00093         public function hasTruncate()
00094         {
00095             return true;
00096         }
00097         
00098         public function hasMultipleTruncate()
00099         {
00100             return true;
00101         }
00102         
00103         public function hasReturning()
00104         {
00105             return true;
00106         }
00107         
00108         public function fullTextSearch($field, $words, $logic)
00109         {
00110             $searchString = self::prepareFullText($words, $logic);
00111             $field = $this->fieldToString($field);
00112             
00113             return
00114                 "({$field} @@ to_tsquery('".self::$tsConfiguration."', ".
00115                 self::quoteValue($searchString)."))";
00116         }
00117         
00118         public function fullTextRank($field, $words, $logic)
00119         {
00120             $searchString = self::prepareFullText($words, $logic);
00121             $field = $this->fieldToString($field);
00122             
00123             return
00124                 self::$rankFunction."({$field}, to_tsquery('".self::$tsConfiguration."', ".
00125                 self::quoteValue($searchString)."))";
00126         }
00127         
00128         public function preAutoincrement(DBColumn $column)
00129         {
00130             self::checkColumn($column);
00131             
00132             return
00133                 'CREATE SEQUENCE "'
00134                 .$this->makeSequenceName($column).'";';
00135         }
00136         
00137         public function postAutoincrement(DBColumn $column)
00138         {
00139             self::checkColumn($column);
00140             
00141             return
00142                 'default nextval(\''
00143                 .$this->makeSequenceName($column).'\')';
00144         }
00145         
00146         protected function makeSequenceName(DBColumn $column)
00147         {
00148             return $column->getTable()->getName().'_'.$column->getName();
00149         }
00150         
00151         private static function checkColumn(DBColumn $column)
00152         {
00153             Assert::isTrue(
00154                 ($column->getTable() !== null)
00155                 && ($column->getDefault() === null)
00156             );
00157         }
00158     }
00159 ?>