blob: 4e6a60c72760b46ba8187eee606b698e67b13aaa [file] [log] [blame]
Timothy Warren817af192012-02-16 08:28:00 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Timothy Warren76e04352012-02-14 11:55:17 -05002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Timothy Warren76e04352012-02-14 11:55:17 -05006 *
7 * NOTICE OF LICENSE
Timothy Warren817af192012-02-16 08:28:00 -05008 *
Timothy Warren76e04352012-02-14 11:55:17 -05009 * Licensed under the Open Software License version 3.0
Timothy Warren817af192012-02-16 08:28:00 -050010 *
Timothy Warren76e04352012-02-14 11:55:17 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
19 * @package CodeIgniter
20 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
23 * @link http://codeigniter.com
24 * @since Version 3.0
25 * @filesource
26 */
27
Timothy Warren76e04352012-02-14 11:55:17 -050028/**
29 * Interbase/Firebird Forge Class
30 *
31 * @category Database
32 * @author EllisLab Dev Team
33 * @link http://codeigniter.com/user_guide/database/
34 */
35class CI_DB_interbase_forge extends CI_DB_forge {
36
Andrey Andreevd947eba2012-04-09 14:58:28 +030037 protected $_drop_table = 'DROP TABLE %s';
38
Timothy Warren76e04352012-02-14 11:55:17 -050039 /**
40 * Create database
41 *
Timothy Warren76e04352012-02-14 11:55:17 -050042 * @param string the database name
Timothy Warrenc2b712e2012-02-17 15:58:08 -050043 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -050044 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030045 public function create_database($db_name)
Timothy Warren76e04352012-02-14 11:55:17 -050046 {
Andrey Andreevd947eba2012-04-09 14:58:28 +030047 // Firebird databases are flat files, so a path is required
48
Timothy Warrenc2b712e2012-02-17 15:58:08 -050049 // Hostname is needed for remote access
Andrey Andreevd947eba2012-04-09 14:58:28 +030050 empty($this->db->hostname) OR $db_name = $this->hostname.':'.$db_name;
51
52 return parent::create_database('"'.$db_name.'"');
Timothy Warren76e04352012-02-14 11:55:17 -050053 }
54
55 // --------------------------------------------------------------------
56
57 /**
58 * Drop database
59 *
Andrey Andreevd947eba2012-04-09 14:58:28 +030060 * @param string the database name
61 * - not used in this driver, the current db is dropped
Timothy Warren76e04352012-02-14 11:55:17 -050062 * @return bool
63 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030064 public function drop_database($db_name = '')
Timothy Warren76e04352012-02-14 11:55:17 -050065 {
Andrey Andreevd947eba2012-04-09 14:58:28 +030066 if ( ! ibase_drop_db($this->conn_id))
67 {
68 return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE;
69 }
70
71 return TRUE;
Timothy Warren76e04352012-02-14 11:55:17 -050072 }
Andrey Andreevd947eba2012-04-09 14:58:28 +030073
Timothy Warren76e04352012-02-14 11:55:17 -050074 // --------------------------------------------------------------------
75
76 /**
77 * Create Table
78 *
Timothy Warren76e04352012-02-14 11:55:17 -050079 * @param string the table name
80 * @param array the fields
81 * @param mixed primary key(s)
82 * @param mixed key(s)
Andrey Andreevd947eba2012-04-09 14:58:28 +030083 * @param bool should 'IF NOT EXISTS' be added to the SQL
Timothy Warrenc2b712e2012-02-17 15:58:08 -050084 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -050085 */
Timothy Warren95562142012-02-20 17:37:21 -050086 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Timothy Warren76e04352012-02-14 11:55:17 -050087 {
88 $sql = 'CREATE TABLE ';
89
Andrey Andreev5ef194d2012-06-08 01:12:54 +030090 $sql .= $this->db->protect_identifiers($table).'(';
Timothy Warren76e04352012-02-14 11:55:17 -050091 $current_field_count = 0;
92
Timothy Warren125fe732012-02-21 07:58:25 -050093 foreach ($fields as $field => $attributes)
Timothy Warren76e04352012-02-14 11:55:17 -050094 {
95 // Numeric field names aren't allowed in databases, so if the key is
96 // numeric, we know it was assigned by PHP and the developer manually
97 // entered the field information, so we'll simply add it to the list
98 if (is_numeric($field))
99 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300100 $sql .= "\n\t".$attributes;
Timothy Warren76e04352012-02-14 11:55:17 -0500101 }
102 else
103 {
104 $attributes = array_change_key_case($attributes, CASE_UPPER);
105
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300106 $sql .= "\n\t".$this->db->escape_identifiers($field).' '.$attributes['TYPE'];
Timothy Warren76e04352012-02-14 11:55:17 -0500107
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300108 empty($attributes['CONSTRAINT']) OR $sql .= '('.$attributes['CONSTRAINT'].')';
Timothy Warren76e04352012-02-14 11:55:17 -0500109
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300110 if ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE)
Timothy Warren76e04352012-02-14 11:55:17 -0500111 {
112 $sql .= ' UNSIGNED';
113 }
114
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300115 if (isset($attributes['DEFAULT']))
Timothy Warren76e04352012-02-14 11:55:17 -0500116 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300117 $sql .= " DEFAULT '".$attributes['DEFAULT']."'";
Timothy Warren76e04352012-02-14 11:55:17 -0500118 }
119
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300120 $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE)
121 ? ' NULL' : ' NOT NULL';
Timothy Warren76e04352012-02-14 11:55:17 -0500122
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300123 if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE)
Timothy Warren76e04352012-02-14 11:55:17 -0500124 {
125 $sql .= ' AUTO_INCREMENT';
126 }
127 }
128
129 // don't add a comma on the end of the last field
130 if (++$current_field_count < count($fields))
131 {
132 $sql .= ',';
133 }
134 }
135
136 if (count($primary_keys) > 0)
137 {
Andrey Andreevcaa04f12012-06-08 01:39:20 +0300138 $primary_keys = $this->db->protect_identifiers($primary_keys);
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300139 $sql .= ",\n\tPRIMARY KEY (".implode(', ', $primary_keys).')';
Timothy Warren76e04352012-02-14 11:55:17 -0500140 }
141
142 if (is_array($keys) && count($keys) > 0)
143 {
144 foreach ($keys as $key)
145 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300146 $key = is_array($key)
Andrey Andreevcaa04f12012-06-08 01:39:20 +0300147 ? $this->db->protect_identifiers($key)
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300148 : array($this->db->escape_identifiers($key));
Timothy Warren76e04352012-02-14 11:55:17 -0500149
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300150 $sql .= ",\n\tUNIQUE (".implode(', ', $key).')';
Timothy Warren76e04352012-02-14 11:55:17 -0500151 }
152 }
153
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300154 return $sql."\n)";
Timothy Warren76e04352012-02-14 11:55:17 -0500155 }
156
157 // --------------------------------------------------------------------
158
159 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500160 * Alter table query
161 *
162 * Generates a platform-specific query so that a table can be altered
163 * Called by add_column(), drop_column(), and column_alter(),
164 *
Timothy Warren76e04352012-02-14 11:55:17 -0500165 * @param string the ALTER type (ADD, DROP, CHANGE)
166 * @param string the column name
167 * @param string the table name
168 * @param string the column definition
169 * @param string the default value
Andrey Andreevd947eba2012-04-09 14:58:28 +0300170 * @param bool should 'NOT NULL' be added
Timothy Warren76e04352012-02-14 11:55:17 -0500171 * @param string the field after which we should add the new field
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500172 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500173 */
Timothy Warren95562142012-02-20 17:37:21 -0500174 protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500175 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300176 return 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '.$this->db->escape_identifiers($column_name)
177 .' '.$column_definition
178 .($default_value !== '' ? ' DEFAULT "'.$default_value.'"' : '')
179 .($null === NULL ? ' NULL' : ' NOT NULL')
180 .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : '');
Timothy Warren76e04352012-02-14 11:55:17 -0500181 }
182
Timothy Warren76e04352012-02-14 11:55:17 -0500183}
184
185/* End of file interbase_forge.php */
186/* Location: ./system/database/drivers/interbase/interbase_forge.php */