Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: Oracle.php 7490 2010-03-29 19:53:27Z 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.doctrine-project.org>.
20
 */
21
 
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: 7490 $
28
 * @link        www.doctrine-project.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 'enum':
62
                $field['length'] = isset($field['length']) && $field['length'] ? $field['length']:255;
63
            case 'string':
64
            case 'array':
65
            case 'object':
66
            case 'gzip':
67
            case 'char':
68
            case 'varchar':
69
                $length = !empty($field['length']) ? $field['length'] : false;
70
 
71
                $fixed  = ((isset($field['fixed']) && $field['fixed']) || $field['type'] == 'char') ? true : false;
72
 
73
                $unit = $this->conn->getParam('char_unit');
74
                $unit = ! is_null($unit) ? ' '.$unit : '';
75
 
76
                if ($length && $length <= $this->conn->getParam('varchar2_max_length')) {
77
                    return $fixed ? 'CHAR('.$length.$unit.')' : 'VARCHAR2('.$length.$unit.')';
78
                }
79
            case 'clob':
80
                return 'CLOB';
81
            case 'blob':
82
                return 'BLOB';
83
            case 'integer':
84
            case 'int':
85
            	$length = (!empty($field['length'])) ? $field['length'] : false;
86
            	if ( $length && $length <= $this->conn->number_max_precision)  {
87
            		if ($length <= 1) {
88
            			return 'NUMBER(3)'; // TINYINT, unsigned max. 256
89
            		} elseif ($length == 2) {
90
            			return 'NUMBER(5)'; // SMALLINT, unsigend max. 65.536
91
            		} elseif ($length == 3) {
92
            			return 'NUMBER(8)'; // MEDIUMINT, unsigned max. 16.777.216
93
            		} elseif ($length == 4) {
94
            			return 'NUMBER(10)'; // INTEGER, unsigend max. 4.294.967.296
95
            		} elseif ($length <= 8) {
96
            			return 'NUMBER(20)'; // BIGINT, unsigend max. 18.446.744.073.709.551.616
97
            		} else {
98
            			return 'INTEGER';
99
            		}
100
            	}
101
                return 'INTEGER';
102
            case 'boolean':
103
                return 'NUMBER(1)';
104
            case 'date':
105
            case 'time':
106
            case 'timestamp':
107
                return 'DATE';
108
            case 'float':
109
            case 'double':
110
                return 'NUMBER';
111
            case 'decimal':
112
                $scale = !empty($field['scale']) ? $field['scale'] : $this->conn->getAttribute(Doctrine_Core::ATTR_DECIMAL_PLACES);
113
                return 'NUMBER(*,'.$scale.')';
114
            default:
115
        }
116
        return $field['type'] . (isset($field['length']) ? '('.$field['length'].')':null);
117
    }
118
 
119
    /**
120
     * Maps a native array description of a field to a doctrine datatype and length
121
     *
122
     * @param array  $field native field description
123
     * @return array containing the various possible types, length, sign, fixed
124
     * @throws Doctrine_DataDict_Oracle_Exception
125
     */
126
    public function getPortableDeclaration(array $field)
127
    {
128
        if ( ! isset($field['data_type'])) {
129
            throw new Doctrine_DataDict_Exception('Native oracle definition must have a data_type key specified');
130
        }
131
 
132
        $dbType = strtolower($field['data_type']);
133
        $type = array();
134
        $length = $unsigned = $fixed = null;
135
        if ( ! empty($field['data_length'])) {
136
            $length = (int)$field['data_length'];
137
        }
138
 
139
        if ( ! isset($field['column_name'])) {
140
            $field['column_name'] = '';
141
        }
142
 
143
        switch ($dbType) {
144
            case 'integer':
145
            case 'pls_integer':
146
            case 'binary_integer':
147
                $type[] = 'integer';
148
                if ($length == '1') {
149
                    $type[] = 'boolean';
150
                    if (preg_match('/^(is|has)/i', $field['column_name'])) {
151
                        $type = array_reverse($type);
152
                    }
153
                }
154
                break;
155
            case 'varchar':
156
            case 'varchar2':
157
            case 'nvarchar2':
158
                $fixed = false;
159
            case 'char':
160
            case 'nchar':
161
                $type[] = 'string';
162
                if ($length == '1') {
163
                    $type[] = 'boolean';
164
                    if (preg_match('/^(is|has)/i', $field['column_name'])) {
165
                        $type = array_reverse($type);
166
                    }
167
                }
168
                if ($fixed !== false) {
169
                    $fixed = true;
170
                }
171
                break;
172
            case 'date':
173
            case 'timestamp':
174
                $type[] = 'timestamp';
175
                $length = null;
176
                break;
177
            case 'float':
178
                $type[] = 'float';
179
                break;
180
            case 'number':
181
                if ( ! empty($field['data_scale'])) {
182
                    $type[] = 'decimal';
183
                } else {
184
                    $type[] = 'integer';
185
                    if ((int)$length == '1') {
186
                        $type[] = 'boolean';
187
                        if (preg_match('/^(is|has)/i', $field['column_name'])) {
188
                            $type = array_reverse($type);
189
                        } else {
190
                            $length = 1; //TINYINT
191
                        }
192
                    } elseif ( ! is_null($length) && (int)$length <= 3) { // TINYINT
193
                        $length = 1;
194
                    } elseif ( ! is_null($length) && (int)$length <= 5) { // SMALLINT
195
                        $length = 2;
196
                    } elseif ( ! is_null($length) && (int)$length <= 8) { // MEDIUMINT
197
                        $length = 3;
198
                    } elseif ( ! is_null($length) && (int)$length <= 10) { // INT
199
                        $length = 4;
200
                    } elseif ( ! is_null($length) && (int)$length <= 20) { //BIGINT
201
                        $length = 8;
202
                    }
203
                }
204
                break;
205
            case 'long':
206
                $type[] = 'string';
207
            case 'clob':
208
            case 'nclob':
209
                $type[] = 'clob';
210
                break;
211
            case 'blob':
212
            case 'raw':
213
            case 'long raw':
214
            case 'bfile':
215
                $type[] = 'blob';
216
                $length = null;
217
            break;
218
            case 'rowid':
219
            case 'urowid':
220
            default:
221
                $type[] = $field['type'];
222
                $length = isset($field['length']) ? $field['length']:null;
223
        }
224
 
225
        return array('type'     => $type,
226
                     'length'   => $length,
227
                     'unsigned' => $unsigned,
228
                     'fixed'    => $fixed);
229
    }
230
}