| 1 | <?php |
| 2 | /* |
| 3 | * $Id: Firebird.php 2963 2007-10-21 06:23:59Z Jonathan.Wage $ |
| 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 Lorenzo Alberton <l.alberton@quipo.it> (PEAR MDB2 Interbase driver) |
| 28 | * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library) |
| 29 | * @version $Revision: 2963 $ |
| 30 | * @link www.phpdoctrine.org |
| 31 | * @since 1.0 |
| 32 | */ |
| 33 | class Doctrine_DataDict_Firebird extends Doctrine_DataDict |
| 34 | { |
| 35 | /** |
| 36 | * Obtain DBMS specific SQL code portion needed to declare an text type |
| 37 | * field to be used in statements like CREATE TABLE. |
| 38 | * |
| 39 | * @param array $field associative array with the name of the properties |
| 40 | * of the field being declared as array indexes. Currently, the types |
| 41 | * of supported field properties are as follows: |
| 42 | * |
| 43 | * length |
| 44 | * Integer value that determines the maximum length of the text |
| 45 | * field. If this argument is missing the field should be |
| 46 | * declared to have the longest length allowed by the DBMS. |
| 47 | * |
| 48 | * default |
| 49 | * Text value to be used as default for this field. |
| 50 | * |
| 51 | * notnull |
| 52 | * Boolean flag that indicates whether this field is constrained |
| 53 | * to not be set to null. |
| 54 | * @return string DBMS specific SQL code portion that should be used to |
| 55 | * declare the specified field. |
| 56 | */ |
| 57 | public function getNativeDeclaration($field) |
| 58 | { |
| 59 | if ( ! isset($field['type'])) { |
| 60 | throw new Doctrine_DataDict_Exception('Missing column type.'); |
| 61 | } |
| 62 | switch ($field['type']) { |
| 63 | case 'varchar': |
| 64 | case 'string': |
| 65 | case 'array': |
| 66 | case 'object': |
| 67 | case 'char': |
| 68 | case 'text': |
| 69 | case 'gzip': |
| 70 | $length = !empty($field['length']) |
| 71 | ? $field['length'] : 16777215; // TODO: $this->conn->options['default_text_field_length']; |
| 72 | |
| 73 | $fixed = ((isset($field['fixed']) && $field['fixed']) || $field['type'] == 'char') ? true : false; |
| 74 | |
| 75 | return $fixed ? 'CHAR('.$length.')' : 'VARCHAR('.$length.')'; |
| 76 | case 'clob': |
| 77 | return 'BLOB SUB_TYPE 1'; |
| 78 | case 'blob': |
| 79 | return 'BLOB SUB_TYPE 0'; |
| 80 | case 'integer': |
| 81 | case 'enum': |
| 82 | case 'int': |
| 83 | return 'INT'; |
| 84 | case 'boolean': |
| 85 | return 'SMALLINT'; |
| 86 | case 'date': |
| 87 | return 'DATE'; |
| 88 | case 'time': |
| 89 | return 'TIME'; |
| 90 | case 'timestamp': |
| 91 | return 'TIMESTAMP'; |
| 92 | case 'float': |
| 93 | return 'DOUBLE PRECISION'; |
| 94 | case 'decimal': |
| 95 | $length = !empty($field['length']) ? $field['length'] : 18; |
| 96 | $scale = !empty($field['scale']) ? $field['scale'] : $this->conn->getAttribute(Doctrine::ATTR_DECIMAL_PLACES); |
| 97 | return 'DECIMAL('.$length.','.$scale.')'; |
| 98 | } |
| 99 | |
| 100 | throw new Doctrine_DataDict_Exception('Unknown field type \'' . $field['type'] . '\'.'); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Maps a native array description of a field to a Doctrine datatype and length |
| 105 | * |
| 106 | * @param array $field native field description |
| 107 | * @return array containing the various possible types, length, sign, fixed |
| 108 | */ |
| 109 | public function getPortableDeclaration($field) |
| 110 | { |
| 111 | $length = (isset($field['length']) && $field['length'] > 0) ? $field['length'] : null; |
| 112 | |
| 113 | $type = array(); |
| 114 | $unsigned = $fixed = null; |
| 115 | $dbType = strtolower($field['type']); |
| 116 | $field['field_sub_type'] = !empty($field['field_sub_type']) |
| 117 | ? strtolower($field['field_sub_type']) : null; |
| 118 | |
| 119 | if ( ! isset($field['name'])) { |
| 120 | $field['name'] = ''; |
| 121 | } |
| 122 | |
| 123 | switch ($dbType) { |
| 124 | case 'smallint': |
| 125 | case 'integer': |
| 126 | case 'int64': |
| 127 | //these may be 'numeric' or 'decimal' |
| 128 | if (isset($field['field_sub_type'])) { |
| 129 | $field['type'] = $field['field_sub_type']; |
| 130 | return $this->getPortableDeclaration($field); |
| 131 | } |
| 132 | case 'bigint': |
| 133 | case 'quad': |
| 134 | $type[] = 'integer'; |
| 135 | if ($length == '1') { |
| 136 | $type[] = 'boolean'; |
| 137 | if (preg_match('/^(is|has)/', $field['name'])) { |
| 138 | $type = array_reverse($type); |
| 139 | } |
| 140 | } |
| 141 | break; |
| 142 | case 'varchar': |
| 143 | $fixed = false; |
| 144 | case 'char': |
| 145 | case 'cstring': |
| 146 | $type[] = 'string'; |
| 147 | if ($length == '1') { |
| 148 | $type[] = 'boolean'; |
| 149 | if (preg_match('/^(is|has)/', $field['name'])) { |
| 150 | $type = array_reverse($type); |
| 151 | } |
| 152 | } |
| 153 | if ($fixed !== false) { |
| 154 | $fixed = true; |
| 155 | } |
| 156 | break; |
| 157 | case 'date': |
| 158 | $type[] = 'date'; |
| 159 | $length = null; |
| 160 | break; |
| 161 | case 'timestamp': |
| 162 | $type[] = 'timestamp'; |
| 163 | $length = null; |
| 164 | break; |
| 165 | case 'time': |
| 166 | $type[] = 'time'; |
| 167 | $length = null; |
| 168 | break; |
| 169 | case 'float': |
| 170 | case 'double': |
| 171 | case 'double precision': |
| 172 | case 'd_float': |
| 173 | $type[] = 'float'; |
| 174 | break; |
| 175 | case 'decimal': |
| 176 | case 'numeric': |
| 177 | $type[] = 'decimal'; |
| 178 | break; |
| 179 | case 'blob': |
| 180 | $type[] = ($field['field_sub_type'] == 'text') ? 'clob' : 'blob'; |
| 181 | $length = null; |
| 182 | break; |
| 183 | default: |
| 184 | throw new Doctrine_DataDict_Exception('unknown database attribute type: '.$dbType); |
| 185 | } |
| 186 | |
| 187 | return array('type' => $type, |
| 188 | 'length' => $length, |
| 189 | 'unsigned' => $unsigned, |
| 190 | 'fixed' => $fixed); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Obtain DBMS specific SQL code portion needed to set the CHARACTER SET |
| 195 | * of a field declaration to be used in statements like CREATE TABLE. |
| 196 | * |
| 197 | * @param string $charset name of the charset |
| 198 | * @return string DBMS specific SQL code portion needed to set the CHARACTER SET |
| 199 | * of a field declaration. |
| 200 | */ |
| 201 | public function getCharsetFieldDeclaration($charset) |
| 202 | { |
| 203 | return 'CHARACTER SET ' . $charset; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Obtain DBMS specific SQL code portion needed to set the COLLATION |
| 208 | * of a field declaration to be used in statements like CREATE TABLE. |
| 209 | * |
| 210 | * @param string $collation name of the collation |
| 211 | * @return string DBMS specific SQL code portion needed to set the COLLATION |
| 212 | * of a field declaration. |
| 213 | */ |
| 214 | public function getCollationFieldDeclaration($collation) |
| 215 | { |
| 216 | return 'COLLATE ' . $collation; |
| 217 | } |
| 218 | } |