| 1 | <?php |
| 2 | /* |
| 3 | * $Id: Oracle.php 3159 2007-11-15 05:16:31Z 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 | * @version $Revision: 3159 $ |
| 28 | * @link www.phpdoctrine.org |
| 29 | * @since 1.0 |
| 30 | */ |
| 31 | class Doctrine_DataDict_Oracle extends Doctrine_DataDict |
| 32 | { |
| 33 | /** |
| 34 | * Obtain DBMS specific SQL code portion needed to declare an text type |
| 35 | * field to be used in statements like CREATE TABLE. |
| 36 | * |
| 37 | * @param array $field associative array with the name of the properties |
| 38 | * of the field being declared as array indexes. Currently, the types |
| 39 | * of supported field properties are as follows: |
| 40 | * |
| 41 | * length |
| 42 | * Integer value that determines the maximum length of the text |
| 43 | * field. If this argument is missing the field should be |
| 44 | * declared to have the longest length allowed by the DBMS. |
| 45 | * |
| 46 | * default |
| 47 | * Text value to be used as default for this field. |
| 48 | * |
| 49 | * notnull |
| 50 | * Boolean flag that indicates whether this field is constrained |
| 51 | * to not be set to null. |
| 52 | * @return string DBMS specific SQL code portion that should be used to |
| 53 | * declare the specified field. |
| 54 | */ |
| 55 | public function getNativeDeclaration(array $field) |
| 56 | { |
| 57 | if ( ! isset($field['type'])) { |
| 58 | throw new Doctrine_DataDict_Exception('Missing column type.'); |
| 59 | } |
| 60 | switch ($field['type']) { |
| 61 | case 'string': |
| 62 | case 'array': |
| 63 | case 'object': |
| 64 | case 'gzip': |
| 65 | case 'char': |
| 66 | case 'varchar': |
| 67 | $length = !empty($field['length']) |
| 68 | ? $field['length'] : 16777215; // TODO: $this->conn->options['default_text_field_length']; |
| 69 | |
| 70 | $fixed = ((isset($field['fixed']) && $field['fixed']) || $field['type'] == 'char') ? true : false; |
| 71 | |
| 72 | return $fixed ? 'CHAR('.$length.')' : 'VARCHAR2('.$length.')'; |
| 73 | case 'clob': |
| 74 | return 'CLOB'; |
| 75 | case 'blob': |
| 76 | return 'BLOB'; |
| 77 | case 'integer': |
| 78 | case 'enum': |
| 79 | case 'int': |
| 80 | if ( ! empty($field['length'])) { |
| 81 | return 'NUMBER('.$field['length'].')'; |
| 82 | } |
| 83 | return 'INT'; |
| 84 | case 'boolean': |
| 85 | return 'NUMBER(1)'; |
| 86 | case 'date': |
| 87 | case 'time': |
| 88 | case 'timestamp': |
| 89 | return 'DATE'; |
| 90 | case 'float': |
| 91 | case 'double': |
| 92 | return 'NUMBER'; |
| 93 | case 'decimal': |
| 94 | $scale = !empty($field['scale']) ? $field['scale'] : $this->conn->getAttribute(Doctrine::ATTR_DECIMAL_PLACES); |
| 95 | return 'NUMBER(*,'.$scale.')'; |
| 96 | default: |
| 97 | } |
| 98 | throw new Doctrine_DataDict_Exception('Unknown field type \'' . $field['type'] . '\'.'); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Maps a native array description of a field to a doctrine datatype and length |
| 103 | * |
| 104 | * @param array $field native field description |
| 105 | * @return array containing the various possible types, length, sign, fixed |
| 106 | * @throws Doctrine_DataDict_Oracle_Exception |
| 107 | */ |
| 108 | public function getPortableDeclaration(array $field) |
| 109 | { |
| 110 | if ( ! isset($field['data_type'])) { |
| 111 | throw new Doctrine_DataDict_Exception('Native oracle definition must have a data_type key specified'); |
| 112 | } |
| 113 | |
| 114 | $dbType = strtolower($field['data_type']); |
| 115 | $type = array(); |
| 116 | $length = $unsigned = $fixed = null; |
| 117 | if ( ! empty($field['data_length'])) { |
| 118 | $length = $field['data_length']; |
| 119 | } |
| 120 | |
| 121 | if ( ! isset($field['column_name'])) { |
| 122 | $field['column_name'] = ''; |
| 123 | } |
| 124 | |
| 125 | switch ($dbType) { |
| 126 | case 'integer': |
| 127 | case 'pls_integer': |
| 128 | case 'binary_integer': |
| 129 | $type[] = 'integer'; |
| 130 | if ($length == '1') { |
| 131 | $type[] = 'boolean'; |
| 132 | if (preg_match('/^(is|has)/', $field['column_name'])) { |
| 133 | $type = array_reverse($type); |
| 134 | } |
| 135 | } |
| 136 | break; |
| 137 | case 'varchar': |
| 138 | case 'varchar2': |
| 139 | case 'nvarchar2': |
| 140 | $fixed = false; |
| 141 | case 'char': |
| 142 | case 'nchar': |
| 143 | $type[] = 'string'; |
| 144 | if ($length == '1') { |
| 145 | $type[] = 'boolean'; |
| 146 | if (preg_match('/^(is|has)/', $field['column_name'])) { |
| 147 | $type = array_reverse($type); |
| 148 | } |
| 149 | } |
| 150 | if ($fixed !== false) { |
| 151 | $fixed = true; |
| 152 | } |
| 153 | break; |
| 154 | case 'date': |
| 155 | case 'timestamp': |
| 156 | $type[] = 'timestamp'; |
| 157 | $length = null; |
| 158 | break; |
| 159 | case 'float': |
| 160 | $type[] = 'float'; |
| 161 | break; |
| 162 | case 'number': |
| 163 | if ( ! empty($field['data_scale'])) { |
| 164 | $type[] = 'decimal'; |
| 165 | } else { |
| 166 | $type[] = 'integer'; |
| 167 | if ($length == '1') { |
| 168 | $type[] = 'boolean'; |
| 169 | if (preg_match('/^(is|has)/', $field['column_name'])) { |
| 170 | $type = array_reverse($type); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | break; |
| 175 | case 'long': |
| 176 | $type[] = 'string'; |
| 177 | case 'clob': |
| 178 | case 'nclob': |
| 179 | $type[] = 'clob'; |
| 180 | break; |
| 181 | case 'blob': |
| 182 | case 'raw': |
| 183 | case 'long raw': |
| 184 | case 'bfile': |
| 185 | $type[] = 'blob'; |
| 186 | $length = null; |
| 187 | break; |
| 188 | case 'rowid': |
| 189 | case 'urowid': |
| 190 | default: |
| 191 | throw new Doctrine_DataDict_Exception('unknown database attribute type: ' . $dbType); |
| 192 | } |
| 193 | |
| 194 | return array('type' => $type, |
| 195 | 'length' => $length, |
| 196 | 'unsigned' => $unsigned, |
| 197 | 'fixed' => $fixed); |
| 198 | } |
| 199 | } |