| 1 | <?php |
| 2 | /* |
| 3 | * $Id: Sqlite.php 4106 2008-03-24 02:03:46Z jwage $ |
| 4 | * |
| 5 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 6 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 7 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 8 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 9 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 10 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 11 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 12 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 13 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 14 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 15 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 16 | * |
| 17 | * This software consists of voluntary contributions made by many individuals |
| 18 | * and is licensed under the LGPL. For more information, see |
| 19 | * <http://www.phpdoctrine.org>. |
| 20 | */ |
| 21 | Doctrine::autoload('Doctrine_DataDict'); |
| 22 | /** |
| 23 | * @package Doctrine |
| 24 | * @subpackage DataDict |
| 25 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
| 26 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi> |
| 27 | * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library) |
| 28 | * @version $Revision: 4106 $ |
| 29 | * @link www.phpdoctrine.org |
| 30 | * @since 1.0 |
| 31 | */ |
| 32 | class Doctrine_DataDict_Sqlite extends Doctrine_DataDict |
| 33 | { |
| 34 | /** |
| 35 | * Obtain DBMS specific SQL code portion needed to declare an text type |
| 36 | * field to be used in statements like CREATE TABLE. |
| 37 | * |
| 38 | * @param array $field associative array with the name of the properties |
| 39 | * of the field being declared as array indexes. Currently, the types |
| 40 | * of supported field properties are as follows: |
| 41 | * |
| 42 | * length |
| 43 | * Integer value that determines the maximum length of the text |
| 44 | * field. If this argument is missing the field should be |
| 45 | * declared to have the longest length allowed by the DBMS. |
| 46 | * |
| 47 | * default |
| 48 | * Text value to be used as default for this field. |
| 49 | * |
| 50 | * notnull |
| 51 | * Boolean flag that indicates whether this field is constrained |
| 52 | * to not be set to null. |
| 53 | * @author Lukas Smith (PEAR MDB2 library) |
| 54 | * @return string DBMS specific SQL code portion that should be used to |
| 55 | * declare the specified field. |
| 56 | */ |
| 57 | public function getNativeDeclaration(array $field) |
| 58 | { |
| 59 | if ( ! isset($field['type'])) { |
| 60 | throw new Doctrine_DataDict_Exception('Missing column type.'); |
| 61 | } |
| 62 | switch ($field['type']) { |
| 63 | case 'text': |
| 64 | case 'object': |
| 65 | case 'array': |
| 66 | case 'string': |
| 67 | case 'char': |
| 68 | case 'gzip': |
| 69 | case 'varchar': |
| 70 | $length = (isset($field['length']) && $field['length']) ? $field['length'] : null; |
| 71 | |
| 72 | $fixed = ((isset($field['fixed']) && $field['fixed']) || $field['type'] == 'char') ? true : false; |
| 73 | |
| 74 | return $fixed ? ($length ? 'CHAR('.$length.')' : 'CHAR('.$this->conn->getAttribute(Doctrine::ATTR_DEFAULT_TEXTFLD_LENGTH).')') |
| 75 | : ($length ? 'VARCHAR('.$length.')' : 'TEXT'); |
| 76 | case 'clob': |
| 77 | if ( ! empty($field['length'])) { |
| 78 | $length = $field['length']; |
| 79 | if ($length <= 255) { |
| 80 | return 'TINYTEXT'; |
| 81 | } elseif ($length <= 65535) { |
| 82 | return 'TEXT'; |
| 83 | } elseif ($length <= 16777215) { |
| 84 | return 'MEDIUMTEXT'; |
| 85 | } |
| 86 | } |
| 87 | return 'LONGTEXT'; |
| 88 | case 'blob': |
| 89 | if ( ! empty($field['length'])) { |
| 90 | $length = $field['length']; |
| 91 | if ($length <= 255) { |
| 92 | return 'TINYBLOB'; |
| 93 | } elseif ($length <= 65535) { |
| 94 | return 'BLOB'; |
| 95 | } elseif ($length <= 16777215) { |
| 96 | return 'MEDIUMBLOB'; |
| 97 | } |
| 98 | } |
| 99 | return 'LONGBLOB'; |
| 100 | case 'enum': |
| 101 | case 'integer': |
| 102 | case 'boolean': |
| 103 | case 'int': |
| 104 | return 'INTEGER'; |
| 105 | case 'date': |
| 106 | return 'DATE'; |
| 107 | case 'time': |
| 108 | return 'TIME'; |
| 109 | case 'timestamp': |
| 110 | return 'DATETIME'; |
| 111 | case 'float': |
| 112 | case 'double': |
| 113 | return 'DOUBLE';//($this->conn->options['fixed_float'] ? '('. |
| 114 | //($this->conn->options['fixed_float']+2).','.$this->conn->options['fixed_float'].')' : ''); |
| 115 | case 'decimal': |
| 116 | $length = !empty($field['length']) ? $field['length'] : 18; |
| 117 | $scale = !empty($field['scale']) ? $field['scale'] : $this->conn->getAttribute(Doctrine::ATTR_DECIMAL_PLACES); |
| 118 | return 'DECIMAL('.$length.','.$scale.')'; |
| 119 | } |
| 120 | throw new Doctrine_DataDict_Exception('Unknown field type \'' . $field['type'] . '\'.'); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Maps a native array description of a field to Doctrine datatype and length |
| 125 | * |
| 126 | * @param array $field native field description |
| 127 | * @return array containing the various possible types, length, sign, fixed |
| 128 | */ |
| 129 | public function getPortableDeclaration(array $field) |
| 130 | { |
| 131 | $e = explode('(', $field['type']); |
| 132 | $field['type'] = $e[0]; |
| 133 | if (isset($e[1])) { |
| 134 | $length = trim($e[1], ')'); |
| 135 | $field['length'] = $length; |
| 136 | } |
| 137 | |
| 138 | $dbType = strtolower($field['type']); |
| 139 | |
| 140 | if ( ! $dbType) { |
| 141 | throw new Doctrine_DataDict_Exception('Missing "type" from field definition'); |
| 142 | } |
| 143 | |
| 144 | $length = (isset($field['length'])) ? $field['length'] : null; |
| 145 | $unsigned = (isset($field['unsigned'])) ? $field['unsigned'] : null; |
| 146 | $fixed = null; |
| 147 | $type = array(); |
| 148 | |
| 149 | if ( ! isset($field['name'])) { |
| 150 | $field['name'] = ''; |
| 151 | } |
| 152 | |
| 153 | switch ($dbType) { |
| 154 | case 'boolean': |
| 155 | $type[] = 'boolean'; |
| 156 | break; |
| 157 | case 'tinyint': |
| 158 | $type[] = 'integer'; |
| 159 | $type[] = 'boolean'; |
| 160 | if (preg_match('/^(is|has)/', $field['name'])) { |
| 161 | $type = array_reverse($type); |
| 162 | } |
| 163 | $unsigned = preg_match('/ unsigned/i', $field['type']); |
| 164 | $length = 1; |
| 165 | break; |
| 166 | case 'smallint': |
| 167 | $type[] = 'integer'; |
| 168 | $unsigned = preg_match('/ unsigned/i', $field['type']); |
| 169 | $length = 2; |
| 170 | break; |
| 171 | case 'mediumint': |
| 172 | $type[] = 'integer'; |
| 173 | $unsigned = preg_match('/ unsigned/i', $field['type']); |
| 174 | $length = 3; |
| 175 | break; |
| 176 | case 'int': |
| 177 | case 'integer': |
| 178 | case 'serial': |
| 179 | $type[] = 'integer'; |
| 180 | $unsigned = preg_match('/ unsigned/i', $field['type']); |
| 181 | $length = 4; |
| 182 | break; |
| 183 | case 'bigint': |
| 184 | case 'bigserial': |
| 185 | $type[] = 'integer'; |
| 186 | $unsigned = preg_match('/ unsigned/i', $field['type']); |
| 187 | $length = 8; |
| 188 | break; |
| 189 | case 'clob': |
| 190 | case 'tinytext': |
| 191 | case 'mediumtext': |
| 192 | case 'longtext': |
| 193 | case 'text': |
| 194 | case 'varchar': |
| 195 | case 'varchar2': |
| 196 | $fixed = false; |
| 197 | case 'char': |
| 198 | $type[] = 'text'; |
| 199 | if ($length == '1') { |
| 200 | $type[] = 'boolean'; |
| 201 | if (preg_match('/^(is|has)/', $field['name'])) { |
| 202 | $type = array_reverse($type); |
| 203 | } |
| 204 | } elseif (strstr($dbType, 'text')) { |
| 205 | $type[] = 'clob'; |
| 206 | } |
| 207 | if ($fixed !== false) { |
| 208 | $fixed = true; |
| 209 | } |
| 210 | break; |
| 211 | case 'date': |
| 212 | $type[] = 'date'; |
| 213 | $length = null; |
| 214 | break; |
| 215 | case 'datetime': |
| 216 | case 'timestamp': |
| 217 | $type[] = 'timestamp'; |
| 218 | $length = null; |
| 219 | break; |
| 220 | case 'time': |
| 221 | $type[] = 'time'; |
| 222 | $length = null; |
| 223 | break; |
| 224 | case 'float': |
| 225 | case 'double': |
| 226 | case 'real': |
| 227 | $type[] = 'float'; |
| 228 | $length = null; |
| 229 | break; |
| 230 | case 'decimal': |
| 231 | case 'numeric': |
| 232 | $type[] = 'decimal'; |
| 233 | $length = null; |
| 234 | break; |
| 235 | case 'tinyblob': |
| 236 | case 'mediumblob': |
| 237 | case 'longblob': |
| 238 | case 'blob': |
| 239 | $type[] = 'blob'; |
| 240 | $length = null; |
| 241 | break; |
| 242 | case 'year': |
| 243 | $type[] = 'integer'; |
| 244 | $type[] = 'date'; |
| 245 | $length = null; |
| 246 | break; |
| 247 | default: |
| 248 | throw new Doctrine_DataDict_Exception('unknown database attribute type: '.$dbType); |
| 249 | } |
| 250 | |
| 251 | return array('type' => $type, |
| 252 | 'length' => $length, |
| 253 | 'unsigned' => $unsigned, |
| 254 | 'fixed' => $fixed); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Obtain DBMS specific SQL code portion needed to declare an integer type |
| 259 | * field to be used in statements like CREATE TABLE. |
| 260 | * |
| 261 | * @param string $name name the field to be declared. |
| 262 | * @param array $field associative array with the name of the properties |
| 263 | * of the field being declared as array indexes. |
| 264 | * Currently, the types of supported field |
| 265 | * properties are as follows: |
| 266 | * |
| 267 | * unsigned |
| 268 | * Boolean flag that indicates whether the field |
| 269 | * should be declared as unsigned integer if |
| 270 | * possible. |
| 271 | * |
| 272 | * default |
| 273 | * Integer value to be used as default for this |
| 274 | * field. |
| 275 | * |
| 276 | * notnull |
| 277 | * Boolean flag that indicates whether this field is |
| 278 | * constrained to not be set to null. |
| 279 | * @return string DBMS specific SQL code portion that should be used to |
| 280 | * declare the specified field. |
| 281 | * @access protected |
| 282 | */ |
| 283 | public function getIntegerDeclaration($name, array $field) |
| 284 | { |
| 285 | $default = $autoinc = ''; |
| 286 | $type = $this->getNativeDeclaration($field); |
| 287 | |
| 288 | $autoincrement = isset($field['autoincrement']) && $field['autoincrement']; |
| 289 | |
| 290 | if ($autoincrement) { |
| 291 | $autoinc = ' PRIMARY KEY AUTOINCREMENT'; |
| 292 | $type = 'INTEGER'; |
| 293 | } elseif (array_key_exists('default', $field)) { |
| 294 | if ($field['default'] === '') { |
| 295 | $field['default'] = empty($field['notnull']) ? null : 0; |
| 296 | } |
| 297 | $default = ' DEFAULT ' . $this->conn->quote($field['default'], $field['type']); |
| 298 | }/** |
| 299 | elseif (empty($field['notnull'])) { |
| 300 | $default = ' DEFAULT NULL'; |
| 301 | } |
| 302 | */ |
| 303 | |
| 304 | $notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : ''; |
| 305 | |
| 306 | // sqlite does not support unsigned attribute for autoinremented fields |
| 307 | $unsigned = (isset($field['unsigned']) && $field['unsigned'] && !$autoincrement) ? ' UNSIGNED' : ''; |
| 308 | |
| 309 | $name = $this->conn->quoteIdentifier($name, true); |
| 310 | return $name . ' ' . $type . $unsigned . $default . $notnull . $autoinc; |
| 311 | } |
| 312 | } |