| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
abstract class DBAdapter { |
|---|
| 45 |
|
|---|
| 46 |
const ID_METHOD_NONE = 0; |
|---|
| 47 |
const ID_METHOD_AUTOINCREMENT = 1; |
|---|
| 48 |
const ID_METHOD_SEQUENCE = 2; |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
* Creole driver to Propel adapter map. |
|---|
| 52 |
* @var array |
|---|
| 53 |
*/ |
|---|
| 54 |
private static $adapters = array( |
|---|
| 55 |
'mysql' => 'DBMySQL', |
|---|
| 56 |
'mysqli' => 'DBMySQLi', |
|---|
| 57 |
'mssql' => 'DBMSSQL', |
|---|
| 58 |
'sybase' => 'DBSybase', |
|---|
| 59 |
'oracle' => 'DBOracle', |
|---|
| 60 |
'pgsql' => 'DBPostgres', |
|---|
| 61 |
'sqlite' => 'DBSQLite', |
|---|
| 62 |
'' => 'DBNone', |
|---|
| 63 |
); |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
* Creates a new instance of the database adapter associated |
|---|
| 67 |
* with the specified Creole driver. |
|---|
| 68 |
* |
|---|
| 69 |
* @param string $driver The name of the Propel/Creole driver to |
|---|
| 70 |
* create a new adapter instance for or a shorter form adapter key. |
|---|
| 71 |
* @return DBAdapter An instance of a Propel database adapter. |
|---|
| 72 |
* @throws PropelException if the adapter could not be instantiated. |
|---|
| 73 |
*/ |
|---|
| 74 |
public static function factory($driver) { |
|---|
| 75 |
$adapterClass = isset(self::$adapters[$driver]) ? self::$adapters[$driver] : null; |
|---|
| 76 |
if ($adapterClass !== null) { |
|---|
| 77 |
$a = new $adapterClass(); |
|---|
| 78 |
return $a; |
|---|
| 79 |
} else { |
|---|
| 80 |
throw new PropelException("Unsupported Propel driver: " . $driver . ": Check your configuration file"); |
|---|
| 81 |
} |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
* This method is called after a connection was created to run necessary |
|---|
| 86 |
* post-initialization queries or code. |
|---|
| 87 |
* |
|---|
| 88 |
* If a charset was specified, this will be set before any other queries |
|---|
| 89 |
* are executed. |
|---|
| 90 |
* |
|---|
| 91 |
* This base method runs queries specified using the "query" setting. |
|---|
| 92 |
* |
|---|
| 93 |
* @param PDO A PDO connection instance. |
|---|
| 94 |
* @param array An array of settings. |
|---|
| 95 |
* @see setCharset() |
|---|
| 96 |
*/ |
|---|
| 97 |
public function initConnection(PDO $con, array $settings) |
|---|
| 98 |
{ |
|---|
| 99 |
if (isset($settings['charset']['value'])) { |
|---|
| 100 |
$this->setCharset($con, $settings['charset']['value']); |
|---|
| 101 |
} |
|---|
| 102 |
if (isset($settings['queries']) && is_array($settings['queries'])) { |
|---|
| 103 |
foreach ($settings['queries'] as $queries) { |
|---|
| 104 |
foreach ((array)$queries as $query) { |
|---|
| 105 |
$con->exec($query); |
|---|
| 106 |
} |
|---|
| 107 |
} |
|---|
| 108 |
} |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
* Sets the character encoding using SQL standard SET NAMES statement. |
|---|
| 113 |
* |
|---|
| 114 |
* This method is invoked from the default initConnection() method and must |
|---|
| 115 |
* be overridden for an RDMBS which does _not_ support this SQL standard. |
|---|
| 116 |
* |
|---|
| 117 |
* @param PDO A PDO connection instance. |
|---|
| 118 |
* @param string The charset encoding. |
|---|
| 119 |
* @see initConnection() |
|---|
| 120 |
*/ |
|---|
| 121 |
public function setCharset(PDO $con, $charset) |
|---|
| 122 |
{ |
|---|
| 123 |
$con->exec("SET NAMES '" . $charset . "'"); |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
* This method is used to ignore case. |
|---|
| 128 |
* |
|---|
| 129 |
* @param string The string to transform to upper case. |
|---|
| 130 |
* @return string The upper case string. |
|---|
| 131 |
*/ |
|---|
| 132 |
public abstract function toUpperCase($in); |
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
* Returns the character used to indicate the beginning and end of |
|---|
| 136 |
* a piece of text used in a SQL statement (generally a single |
|---|
| 137 |
* quote). |
|---|
| 138 |
* |
|---|
| 139 |
* @return string The text delimeter. |
|---|
| 140 |
*/ |
|---|
| 141 |
public function getStringDelimiter() |
|---|
| 142 |
{ |
|---|
| 143 |
return '\''; |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
* This method is used to ignore case. |
|---|
| 148 |
* |
|---|
| 149 |
* @param string $in The string whose case to ignore. |
|---|
| 150 |
* @return string The string in a case that can be ignored. |
|---|
| 151 |
*/ |
|---|
| 152 |
public abstract function ignoreCase($in); |
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
* This method is used to ignore case in an ORDER BY clause. |
|---|
| 156 |
* Usually it is the same as ignoreCase, but some databases |
|---|
| 157 |
* (Interbase for example) does not use the same SQL in ORDER BY |
|---|
| 158 |
* and other clauses. |
|---|
| 159 |
* |
|---|
| 160 |
* @param string $in The string whose case to ignore. |
|---|
| 161 |
* @return string The string in a case that can be ignored. |
|---|
| 162 |
*/ |
|---|
| 163 |
public function ignoreCaseInOrderBy($in) |
|---|
| 164 |
{ |
|---|
| 165 |
return $this->ignoreCase($in); |
|---|
| 166 |
} |
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
* Returns SQL which concatenates the second string to the first. |
|---|
| 170 |
* |
|---|
| 171 |
* @param string String to concatenate. |
|---|
| 172 |
* @param string String to append. |
|---|
| 173 |
* @return string |
|---|
| 174 |
*/ |
|---|
| 175 |
public abstract function concatString($s1, $s2); |
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
* Returns SQL which extracts a substring. |
|---|
| 179 |
* |
|---|
| 180 |
* @param string String to extract from. |
|---|
| 181 |
* @param int Offset to start from. |
|---|
| 182 |
* @param int Number of characters to extract. |
|---|
| 183 |
* @return string |
|---|
| 184 |
*/ |
|---|
| 185 |
public abstract function subString($s, $pos, $len); |
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
* Returns SQL which calculates the length (in chars) of a string. |
|---|
| 189 |
* |
|---|
| 190 |
* @param string String to calculate length of. |
|---|
| 191 |
* @return string |
|---|
| 192 |
*/ |
|---|
| 193 |
public abstract function strLength($s); |
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
* Quotes database objec identifiers (table names, col names, sequences, etc.). |
|---|
| 198 |
* @param string $text The identifier to quote. |
|---|
| 199 |
* @return string The quoted identifier. |
|---|
| 200 |
*/ |
|---|
| 201 |
public function quoteIdentifier($text) |
|---|
| 202 |
{ |
|---|
| 203 |
return '"' . $text . '"'; |
|---|
| 204 |
} |
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
* Quotes a database table which could have space seperating it from an alias, both should be identified seperately |
|---|
| 208 |
* @param string $table The table name to quo |
|---|
| 209 |
* @return string The quoted table name |
|---|
| 210 |
**/ |
|---|
| 211 |
public function quoteIdentifierTable($table) { |
|---|
| 212 |
return implode(" ", array_map(array($this, "quoteIdentifier"), explode(" ", $table) ) ); |
|---|
| 213 |
} |
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
* Returns the native ID method for this RDBMS. |
|---|
| 217 |
* @return int one of DBAdapter:ID_METHOD_SEQUENCE, DBAdapter::ID_METHOD_AUTOINCREMENT. |
|---|
| 218 |
*/ |
|---|
| 219 |
protected function getIdMethod() |
|---|
| 220 |
{ |
|---|
| 221 |
return DBAdapter::ID_METHOD_AUTOINCREMENT; |
|---|
| 222 |
} |
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
* Whether this adapter uses an ID generation system that requires getting ID _before_ performing INSERT. |
|---|
| 226 |
* @return boolean |
|---|
| 227 |
*/ |
|---|
| 228 |
public function isGetIdBeforeInsert() |
|---|
| 229 |
{ |
|---|
| 230 |
return ($this->getIdMethod() === DBAdapter::ID_METHOD_SEQUENCE); |
|---|
| 231 |
} |
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
* Whether this adapter uses an ID generation system that requires getting ID _before_ performing INSERT. |
|---|
| 235 |
* @return boolean |
|---|
| 236 |
*/ |
|---|
| 237 |
public function isGetIdAfterInsert() |
|---|
| 238 |
{ |
|---|
| 239 |
return ($this->getIdMethod() === DBAdapter::ID_METHOD_AUTOINCREMENT); |
|---|
| 240 |
} |
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
* Gets the generated ID (either last ID for autoincrement or next sequence ID). |
|---|
| 244 |
* @return mixed |
|---|
| 245 |
*/ |
|---|
| 246 |
public function getId(PDO $con, $name = null) |
|---|
| 247 |
{ |
|---|
| 248 |
return $con->lastInsertId($name); |
|---|
| 249 |
} |
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
* Returns timestamp formatter string for use in date() function. |
|---|
| 253 |
* @return string |
|---|
| 254 |
*/ |
|---|
| 255 |
public function getTimestampFormatter() |
|---|
| 256 |
{ |
|---|
| 257 |
return "Y-m-d H:i:s"; |
|---|
| 258 |
} |
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
* Returns date formatter string for use in date() function. |
|---|
| 262 |
* @return string |
|---|
| 263 |
*/ |
|---|
| 264 |
public function getDateFormatter() |
|---|
| 265 |
{ |
|---|
| 266 |
return "Y-m-d"; |
|---|
| 267 |
} |
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
* Returns time formatter string for use in date() function. |
|---|
| 271 |
* @return string |
|---|
| 272 |
*/ |
|---|
| 273 |
public function getTimeFormatter() |
|---|
| 274 |
{ |
|---|
| 275 |
return "H:i:s"; |
|---|
| 276 |
} |
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
* Should Column-Names get identifiers for inserts or updates. |
|---|
| 280 |
* By default false is returned -> backwards compability. |
|---|
| 281 |
* |
|---|
| 282 |
* it`s a workaround...!!! |
|---|
| 283 |
* |
|---|
| 284 |
* @todo should be abstract |
|---|
| 285 |
* @return boolean |
|---|
| 286 |
* @deprecated |
|---|
| 287 |
*/ |
|---|
| 288 |
public function useQuoteIdentifier() |
|---|
| 289 |
{ |
|---|
| 290 |
return false; |
|---|
| 291 |
} |
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
* Modifies the passed-in SQL to add LIMIT and/or OFFSET. |
|---|
| 295 |
*/ |
|---|
| 296 |
public abstract function applyLimit(&$sql, $offset, $limit); |
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 |
* Gets the SQL string that this adapter uses for getting a random number. |
|---|
| 300 |
* |
|---|
| 301 |
* @param mixed $seed (optional) seed value for databases that support this |
|---|
| 302 |
*/ |
|---|
| 303 |
public abstract function random($seed = null); |
|---|
| 304 |
|
|---|
| 305 |
} |
|---|
| 306 |
|
|---|