blob: 7f3693dbdb22666289b756548556b26930b84b7c [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Timothy Warren76e04352012-02-14 11:55:17 -050028
Timothy Warren76e04352012-02-14 11:55:17 -050029/**
30 * Interbase/Firebird Forge Class
31 *
32 * @category Database
33 * @author EllisLab Dev Team
34 * @link http://codeigniter.com/user_guide/database/
35 */
Andrey Andreev26086872012-07-05 11:21:58 +030036class CI_DB_ibase_forge extends CI_DB_forge {
Timothy Warren76e04352012-02-14 11:55:17 -050037
Andrey Andreevd947eba2012-04-09 14:58:28 +030038 protected $_drop_table = 'DROP TABLE %s';
39
Timothy Warren76e04352012-02-14 11:55:17 -050040 /**
41 * Create database
42 *
Timothy Warren76e04352012-02-14 11:55:17 -050043 * @param string the database name
Timothy Warrenc2b712e2012-02-17 15:58:08 -050044 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -050045 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030046 public function create_database($db_name)
Timothy Warren76e04352012-02-14 11:55:17 -050047 {
Andrey Andreevd947eba2012-04-09 14:58:28 +030048 // Firebird databases are flat files, so a path is required
49
Timothy Warrenc2b712e2012-02-17 15:58:08 -050050 // Hostname is needed for remote access
Andrey Andreevd947eba2012-04-09 14:58:28 +030051 empty($this->db->hostname) OR $db_name = $this->hostname.':'.$db_name;
52
53 return parent::create_database('"'.$db_name.'"');
Timothy Warren76e04352012-02-14 11:55:17 -050054 }
55
56 // --------------------------------------------------------------------
57
58 /**
59 * Drop database
60 *
Andrey Andreevd947eba2012-04-09 14:58:28 +030061 * @param string the database name
62 * - not used in this driver, the current db is dropped
Timothy Warren76e04352012-02-14 11:55:17 -050063 * @return bool
64 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030065 public function drop_database($db_name = '')
Timothy Warren76e04352012-02-14 11:55:17 -050066 {
Andrey Andreevd947eba2012-04-09 14:58:28 +030067 if ( ! ibase_drop_db($this->conn_id))
68 {
69 return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE;
70 }
Andrey Andreev5d281762012-06-11 22:05:40 +030071 elseif ( ! empty($this->db->data_cache['db_names']))
72 {
73 $key = array_search(strtolower($this->db->database), array_map('strtolower', $this->db->data_cache['db_names']), TRUE);
74 if ($key !== FALSE)
75 {
76 unset($this->db->data_cache['db_names'][$key]);
77 }
78 }
Andrey Andreevd947eba2012-04-09 14:58:28 +030079
80 return TRUE;
Timothy Warren76e04352012-02-14 11:55:17 -050081 }
Andrey Andreevd947eba2012-04-09 14:58:28 +030082
Timothy Warren76e04352012-02-14 11:55:17 -050083 // --------------------------------------------------------------------
84
85 /**
86 * Create Table
87 *
Timothy Warren76e04352012-02-14 11:55:17 -050088 * @param string the table name
89 * @param array the fields
90 * @param mixed primary key(s)
91 * @param mixed key(s)
Andrey Andreevd947eba2012-04-09 14:58:28 +030092 * @param bool should 'IF NOT EXISTS' be added to the SQL
Timothy Warrenc2b712e2012-02-17 15:58:08 -050093 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -050094 */
Timothy Warren95562142012-02-20 17:37:21 -050095 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Timothy Warren76e04352012-02-14 11:55:17 -050096 {
97 $sql = 'CREATE TABLE ';
98
Andrey Andreev9637b402012-06-08 15:39:24 +030099 $sql .= $this->db->escape_identifiers($table).'(';
Timothy Warren76e04352012-02-14 11:55:17 -0500100 $current_field_count = 0;
101
Timothy Warren125fe732012-02-21 07:58:25 -0500102 foreach ($fields as $field => $attributes)
Timothy Warren76e04352012-02-14 11:55:17 -0500103 {
104 // Numeric field names aren't allowed in databases, so if the key is
105 // numeric, we know it was assigned by PHP and the developer manually
106 // entered the field information, so we'll simply add it to the list
107 if (is_numeric($field))
108 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300109 $sql .= "\n\t".$attributes;
Timothy Warren76e04352012-02-14 11:55:17 -0500110 }
111 else
112 {
113 $attributes = array_change_key_case($attributes, CASE_UPPER);
114
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300115 $sql .= "\n\t".$this->db->escape_identifiers($field).' '.$attributes['TYPE'];
Timothy Warren76e04352012-02-14 11:55:17 -0500116
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300117 empty($attributes['CONSTRAINT']) OR $sql .= '('.$attributes['CONSTRAINT'].')';
Timothy Warren76e04352012-02-14 11:55:17 -0500118
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300119 if ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE)
Timothy Warren76e04352012-02-14 11:55:17 -0500120 {
121 $sql .= ' UNSIGNED';
122 }
123
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300124 if (isset($attributes['DEFAULT']))
Timothy Warren76e04352012-02-14 11:55:17 -0500125 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300126 $sql .= " DEFAULT '".$attributes['DEFAULT']."'";
Timothy Warren76e04352012-02-14 11:55:17 -0500127 }
128
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300129 $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE)
130 ? ' NULL' : ' NOT NULL';
Timothy Warren76e04352012-02-14 11:55:17 -0500131
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300132 if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE)
Timothy Warren76e04352012-02-14 11:55:17 -0500133 {
134 $sql .= ' AUTO_INCREMENT';
135 }
136 }
137
138 // don't add a comma on the end of the last field
139 if (++$current_field_count < count($fields))
140 {
141 $sql .= ',';
142 }
143 }
144
145 if (count($primary_keys) > 0)
146 {
Andrey Andreev9637b402012-06-08 15:39:24 +0300147 $primary_keys = $this->db->escape_identifiers($primary_keys);
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300148 $sql .= ",\n\tPRIMARY KEY (".implode(', ', $primary_keys).')';
Timothy Warren76e04352012-02-14 11:55:17 -0500149 }
150
151 if (is_array($keys) && count($keys) > 0)
152 {
153 foreach ($keys as $key)
154 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300155 $key = is_array($key)
Andrey Andreev9637b402012-06-08 15:39:24 +0300156 ? $this->db->escape_identifiers($key)
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300157 : array($this->db->escape_identifiers($key));
Timothy Warren76e04352012-02-14 11:55:17 -0500158
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300159 $sql .= ",\n\tUNIQUE (".implode(', ', $key).')';
Timothy Warren76e04352012-02-14 11:55:17 -0500160 }
161 }
162
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300163 return $sql."\n)";
Timothy Warren76e04352012-02-14 11:55:17 -0500164 }
165
166 // --------------------------------------------------------------------
167
168 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500169 * Alter table query
170 *
171 * Generates a platform-specific query so that a table can be altered
172 * Called by add_column(), drop_column(), and column_alter(),
173 *
Timothy Warren76e04352012-02-14 11:55:17 -0500174 * @param string the ALTER type (ADD, DROP, CHANGE)
175 * @param string the column name
176 * @param string the table name
177 * @param string the column definition
178 * @param string the default value
Andrey Andreevd947eba2012-04-09 14:58:28 +0300179 * @param bool should 'NOT NULL' be added
Timothy Warren76e04352012-02-14 11:55:17 -0500180 * @param string the field after which we should add the new field
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500181 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500182 */
Timothy Warren95562142012-02-20 17:37:21 -0500183 protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500184 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300185 return 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '.$this->db->escape_identifiers($column_name)
186 .' '.$column_definition
187 .($default_value !== '' ? ' DEFAULT "'.$default_value.'"' : '')
188 .($null === NULL ? ' NULL' : ' NOT NULL')
189 .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : '');
Timothy Warren76e04352012-02-14 11:55:17 -0500190 }
191
Timothy Warren76e04352012-02-14 11:55:17 -0500192}
193
Andrey Andreev26086872012-07-05 11:21:58 +0300194/* End of file ibase_forge.php */
195/* Location: ./system/database/drivers/ibase/ibase_forge.php */