| 1 | <?php |
| 2 | /* |
| 3 | * $Id: Mssql.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 Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library) |
| 28 | * @author Frank M. Kromann <frank@kromann.info> (PEAR MDB2 Mssql driver) |
| 29 | * @author David Coallier <davidc@php.net> (PEAR MDB2 Mssql driver) |
| 30 | * @version $Revision: 2963 $ |
| 31 | * @link www.phpdoctrine.org |
| 32 | * @since 1.0 |
| 33 | */ |
| 34 | class Doctrine_DataDict_Mssql extends Doctrine_DataDict |
| 35 | { |
| 36 | /** |
| 37 | * Obtain DBMS specific SQL code portion needed to declare an text type |
| 38 | * field to be used in statements like CREATE TABLE. |
| 39 | * |
| 40 | * @param array $field associative array with the name of the properties |
| 41 | * of the field being declared as array indexes. Currently, the types |
| 42 | * of supported field properties are as follows: |
| 43 | * |
| 44 | * length |
| 45 | * Integer value that determines the maximum length of the text |
| 46 | * field. If this argument is missing the field should be |
| 47 | * declared to have the longest length allowed by the DBMS. |
| 48 | * |
| 49 | * default |
| 50 | * Text value to be used as default for this field. |
| 51 | * |
| 52 | * notnull |
| 53 | * Boolean flag that indicates whether this field is constrained |
| 54 | * to not be set to null. |
| 55 | * |
| 56 | * @return string DBMS specific SQL code portion that should be used to |
| 57 | * declare the specified field. |
| 58 | */ |
| 59 | public function getNativeDeclaration($field) |
| 60 | { |
| 61 | if ( ! isset($field['type'])) { |
| 62 | throw new Doctrine_DataDict_Exception('Missing column type.'); |
| 63 | } |
| 64 | switch ($field['type']) { |
| 65 | case 'array': |
| 66 | case 'object': |
| 67 | case 'text': |
| 68 | case 'char': |
| 69 | case 'varchar': |
| 70 | case 'string': |
| 71 | case 'gzip': |
| 72 | $length = !empty($field['length']) |
| 73 | ? $field['length'] : false; |
| 74 | |
| 75 | $fixed = ((isset($field['fixed']) && $field['fixed']) || $field['type'] == 'char') ? true : false; |
| 76 | |
| 77 | return $fixed ? ($length ? 'CHAR('.$length.')' : 'CHAR('.$this->conn->options['default_text_field_length'].')') |
| 78 | : ($length ? 'VARCHAR('.$length.')' : 'TEXT'); |
| 79 | case 'clob': |
| 80 | if ( ! empty($field['length'])) { |
| 81 | $length = $field['length']; |
| 82 | if ($length <= 8000) { |
| 83 | return 'VARCHAR('.$length.')'; |
| 84 | } |
| 85 | } |
| 86 | return 'TEXT'; |
| 87 | case 'blob': |
| 88 | if ( ! empty($field['length'])) { |
| 89 | $length = $field['length']; |
| 90 | if ($length <= 8000) { |
| 91 | return "VARBINARY($length)"; |
| 92 | } |
| 93 | } |
| 94 | return 'IMAGE'; |
| 95 | case 'integer': |
| 96 | case 'enum': |
| 97 | case 'int': |
| 98 | return 'INT'; |
| 99 | case 'boolean': |
| 100 | return 'BIT'; |
| 101 | case 'date': |
| 102 | return 'CHAR(' . strlen('YYYY-MM-DD') . ')'; |
| 103 | case 'time': |
| 104 | return 'CHAR(' . strlen('HH:MM:SS') . ')'; |
| 105 | case 'timestamp': |
| 106 | return 'CHAR(' . strlen('YYYY-MM-DD HH:MM:SS') . ')'; |
| 107 | case 'float': |
| 108 | return 'FLOAT'; |
| 109 | case 'decimal': |
| 110 | $length = !empty($field['length']) ? $field['length'] : 18; |
| 111 | $scale = !empty($field['scale']) ? $field['scale'] : $this->conn->getAttribute(Doctrine::ATTR_DECIMAL_PLACES); |
| 112 | return 'DECIMAL('.$length.','.$scale.')'; |
| 113 | } |
| 114 | |
| 115 | throw new Doctrine_DataDict_Exception('Unknown field type \'' . $field['type'] . '\'.'); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Maps a native array description of a field to a MDB2 datatype and length |
| 120 | * |
| 121 | * @param array $field native field description |
| 122 | * @return array containing the various possible types, length, sign, fixed |
| 123 | */ |
| 124 | public function getPortableDeclaration($field) |
| 125 | { |
| 126 | $db_type = preg_replace('/\d/','', strtolower($field['type']) ); |
| 127 | $length = (isset($field['length']) && $field['length'] > 0) ? $field['length'] : null; |
| 128 | |
| 129 | $type = array(); |
| 130 | // todo: unsigned handling seems to be missing |
| 131 | $unsigned = $fixed = null; |
| 132 | |
| 133 | if ( ! isset($field['name'])) |
| 134 | $field['name'] = ''; |
| 135 | |
| 136 | switch ($db_type) { |
| 137 | case 'bit': |
| 138 | $type[0] = 'boolean'; |
| 139 | break; |
| 140 | case 'int': |
| 141 | $type[0] = 'integer'; |
| 142 | if ($length == 1) { |
| 143 | $type[] = 'boolean'; |
| 144 | } |
| 145 | break; |
| 146 | case 'datetime': |
| 147 | $type[0] = 'timestamp'; |
| 148 | break; |
| 149 | case 'float': |
| 150 | case 'real': |
| 151 | case 'numeric': |
| 152 | $type[0] = 'float'; |
| 153 | break; |
| 154 | case 'decimal': |
| 155 | case 'money': |
| 156 | $type[0] = 'decimal'; |
| 157 | break; |
| 158 | case 'text': |
| 159 | case 'varchar': |
| 160 | $fixed = false; |
| 161 | case 'char': |
| 162 | $type[0] = 'string'; |
| 163 | if ($length == '1') { |
| 164 | $type[] = 'boolean'; |
| 165 | if (preg_match('/^[is|has]/', $field['name'])) { |
| 166 | $type = array_reverse($type); |
| 167 | } |
| 168 | } elseif (strstr($db_type, 'text')) { |
| 169 | $type[] = 'clob'; |
| 170 | } |
| 171 | if ($fixed !== false) { |
| 172 | $fixed = true; |
| 173 | } |
| 174 | break; |
| 175 | case 'image': |
| 176 | case 'varbinary': |
| 177 | $type[] = 'blob'; |
| 178 | $length = null; |
| 179 | break; |
| 180 | default: |
| 181 | throw new Doctrine_DataDict_Exception('unknown database attribute type: '.$db_type); |
| 182 | } |
| 183 | |
| 184 | return array('type' => $type, |
| 185 | 'length' => $length, |
| 186 | 'unsigned' => $unsigned, |
| 187 | 'fixed' => $fixed); |
| 188 | } |
| 189 | } |