Completed
Branch master (024767)
by Henry Stivens
01:51
created

DbPdoInformix::create_table()   F

Complexity

Conditions 20
Paths > 20000

Size

Total Lines 66
Code Lines 45

Duplication

Lines 66
Ratio 100 %

Importance

Changes 0
Metric Value
cc 20
eloc 45
nc 23345
nop 3
dl 66
loc 66
rs 2.774
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * KumbiaPHP web & app Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.txt.
9
 * It is also available through the world-wide-web at this URL:
10
 * http://wiki.kumbiaphp.com/Licencia
11
 * If you did not receive a copy of the license and are unable to
12
 * obtain it through the world-wide-web, please send an email
13
 * to [email protected] so we can send you a copy immediately.
14
 *
15
 * @category   Kumbia
16
 * @package    Db
17
 * @subpackage Adapters
18
 * @copyright  Copyright (c) 2005 - 2017 Kumbia Team (http://www.kumbiaphp.com)
19
 * @license    http://wiki.kumbiaphp.com/Licencia     New BSD License
20
 */
21
/**
22
 * @see DbPdo Padre de Drivers Pdo
23
 */
24
require_once CORE_PATH . 'libs/db/adapters/pdo.php';
25
26
/**
27
 * PDO Informix Database Support
28
 *
29
 * @category   Kumbia
30
 * @package    Db
31
 * @subpackage Adapters
32
 */
33
class DbPdoInformix extends DbPDO
34
{
35
36
    /**
37
     * Nombre de RBDM
38
     */
39
    protected $db_rbdm = "informix";
40
41
    /**
42
     * Tipo de Dato Integer
43
     *
44
     */
45
    const TYPE_INTEGER = "INTEGER";
46
47
    /**
48
     * Tipo de Dato Date
49
     *
50
     */
51
    const TYPE_DATE = "DATE";
52
53
    /**
54
     * Tipo de Dato Varchar
55
     *
56
     */
57
    const TYPE_VARCHAR = "VARCHAR";
58
59
    /**
60
     * Tipo de Dato Decimal
61
     *
62
     */
63
    const TYPE_DECIMAL = "DECIMAL";
64
65
    /**
66
     * Tipo de Dato Datetime
67
     *
68
     */
69
    const TYPE_DATETIME = "DATETIME";
70
71
    /**
72
     * Tipo de Dato Char
73
     *
74
     */
75
    const TYPE_CHAR = "CHAR";
76
77
    /**
78
     * Ejecuta acciones de incializacion del driver
79
     *
80
     */
81
    public function initialize()
82
    {
83
84
    }
85
86
    /**
87
     * Verifica si una tabla existe o no
88
     *
89
     * @param string $table
90
     * @return boolean
91
     */
92
    public function table_exists($table, $schema='')
93
    {
94
        /**
95
         * Informix no soporta schemas
96
         */
97
        $table = addslashes("$table");
98
        $num = $this->fetch_one("SELECT COUNT(*) FROM systables WHERE tabname = '$table'");
99
        return (int) $num[0];
100
    }
101
102
    /**
103
     * Devuelve un LIMIT valido para un SELECT del RBDM
104
     *
105
     * @param string $sql
106
     * @return string
107
     */
108
    public function limit($sql)
109
    {
110
        $params = Util::getParams(func_get_args());
111
        
112
        $limit ='';
113
        if(isset($params['offset'])){
114
            $limit .= " SKIP $params[offset]";
115
        }
116
        if(isset($params['limit'])){
117
            $limit .= " FIRST $params[limit]";
118
        }
119
120
        return str_ireplace("SELECT ", "SELECT $limit ", $sql);
121
    }
122
123
    /**
124
     * Borra una tabla de la base de datos
125
     *
126
     * @param string $table
127
     * @return boolean
128
     */
129
    public function drop_table($table, $if_exists=true)
130
    {
131
        if ($if_exists) {
132
            if ($this->table_exists($table)) {
133
                return $this->query("DROP TABLE $table");
134
            } else {
135
                return true;
136
            }
137
        } else {
138
            //$this->set_return_rows(false);
139
            return $this->query("DROP TABLE $table");
140
        }
141
    }
142
143
    /**
144
     * Crea una tabla utilizando SQL nativo del RDBM
145
     *
146
     * TODO:
147
     * - Falta que el parametro index funcione. Este debe listar indices compuestos multipes y unicos
148
     * - Agregar el tipo de tabla que debe usarse (Informix)
149
     * - Soporte para campos autonumericos
150
     * - Soporte para llaves foraneas
151
     *
152
     * @param string $table
153
     * @param array $definition
154
     * @return boolean
155
     */
156
    public function create_table($table, $definition, $index=array())
157
    {
158
        $create_sql = "CREATE TABLE $table (";
159
        if (!is_array($definition)) {
160
            throw new KumbiaException("Definición inválida para crear la tabla '$table'");
161
        }
162
        $create_lines = array();
163
        $index = array();
164
        $unique_index = array();
165
        $primary = array();
166
        //$not_null = "";
167
        //$size = "";
168
        foreach ($definition as $field => $field_def) {
169
            if (isset($field_def['not_null'])) {
170
                $not_null = $field_def['not_null'] ? 'NOT NULL' : '';
171
            } else {
172
                $not_null = "";
173
            }
174
            if (isset($field_def['size'])) {
175
                $size = $field_def['size'] ? '(' . $field_def['size'] . ')' : '';
176
            } else {
177
                $size = "";
178
            }
179
            if (isset($field_def['index'])) {
180
                if ($field_def['index']) {
181
                    $index[] = "INDEX($field)";
182
                }
183
            }
184
            if (isset($field_def['unique_index'])) {
185
                if ($field_def['unique_index']) {
186
                    $index[] = "UNIQUE($field)";
187
                }
188
            }
189
            if (isset($field_def['primary'])) {
190
                if ($field_def['primary']) {
191
                    $primary[] = "$field";
192
                }
193
            }
194
            if (isset($field_def['auto'])) {
195
                if ($field_def['auto']) {
196
                    $field_def['type'] = "SERIAL";
197
                }
198
            }
199
            if (isset($field_def['extra'])) {
200
                $extra = $field_def['extra'];
201
            } else {
202
                $extra = "";
203
            }
204
            $create_lines[] = "$field " . $field_def['type'] . $size . ' ' . $not_null . ' ' . $extra;
205
        }
206
        $create_sql.= join(',', $create_lines);
207
        $last_lines = array();
208
        if (count($primary)) {
209
            $last_lines[] = 'PRIMARY KEY(' . join(",", $primary) . ')';
210
        }
211
        if (count($index)) {
212
            $last_lines[] = join(',', $index);
213
        }
214
        if (count($unique_index)) {
215
            $last_lines[] = join(',', $unique_index);
216
        }
217
        if (count($last_lines)) {
218
            $create_sql.= ',' . join(',', $last_lines) . ')';
219
        }
220
        return $this->query($create_sql);
221
    }
222
223
    /**
224
     * Listar las tablas en la base de datos
225
     *
226
     * @return array
227
     */
228
    public function list_tables()
229
    {
230
        return $this->fetch_all("SELECT tabname FROM systables WHERE tabtype = 'T' AND version <> 65537");
231
    }
232
233
    /**
234
     * Listar los campos de una tabla
235
     *
236
     * @param string $table
237
     * @return array
238
     */
239
    public function describe_table($table, $schema='')
240
    {
241
        /**
242
         * Informix no soporta schemas
243
         * TODO: No hay un metodo identificable para obtener llaves primarias
244
         * no nulos y tamaños reales de campos
245
         * Primary Key, Null?
246
         */
247
        $describe = $this->fetch_all("SELECT c.colname AS Field, c.coltype AS Type,
248
                'YES' AS NULL, c.collength as Length
249
                 FROM systables t, syscolumns c WHERE
250
                c.tabid = t.tabid AND t.tabname = '$table' ORDER BY c.colno");
251
        $final_describe = array();
252
        foreach ($describe as $field) {
253
            //Serial
254
            if ($field['field'] == 'id') {
255
                $field["key"] = 'PRI';
256
                $field["null"] = 'NO';
257
            } else {
258
                $field["key"] = '';
259
            }
260
            if (substr($field['field'], -3) == '_id') {
261
                $field["null"] = 'NO';
262
            }
263
            if ($field['type'] == 262) {
264
                $field['type'] = "integer";
265
            }
266
            if ($field['type'] == 13) {
267
                $field['type'] = "varchar(" . $field['length'] . ")";
268
            }
269
            if ($field['type'] == 2) {
270
                $field['type'] = "int(" . $field['length'] . ")";
271
            }
272
            if ($field['type'] == 7) {
273
                $field['type'] = "date";
274
            }
275
            $final_describe[] = array(
276
                "Field" => $field["field"],
277
                "Type" => $field["type"],
278
                "Null" => $field["null"],
279
                "Key" => $field["key"]
280
            );
281
        }
282
        return $final_describe;
283
    }
284
285
}
286