blob: 34a6ee44ee6fc19527547b8b9b560d2bfd5abc43 [file] [log] [blame]
Andrey Andreev9fd79f52012-03-20 23:04:13 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Timothy Warren80ab8162011-08-22 18:26:12 -04002/**
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 Warren80ab8162011-08-22 18:26:12 -04006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev9fd79f52012-03-20 23:04:13 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev9fd79f52012-03-20 23:04:13 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -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 *
Timothy Warren80ab8162011-08-22 18:26:12 -040019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Timothy Warren80ab8162011-08-22 18:26:12 -040023 * @link http://codeigniter.com
Timothy Warren018af7a2011-09-07 12:07:35 -040024 * @since Version 2.1.0
Timothy Warren80ab8162011-08-22 18:26:12 -040025 * @filesource
26 */
27
Timothy Warren80ab8162011-08-22 18:26:12 -040028/**
29 * PDO Forge Class
30 *
31 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050032 * @author EllisLab Dev Team
Timothy Warren80ab8162011-08-22 18:26:12 -040033 * @link http://codeigniter.com/database/
34 */
35class CI_DB_pdo_forge extends CI_DB_forge {
36
Andrey Andreevd947eba2012-04-09 14:58:28 +030037 protected $_drop_table = 'DROP TABLE %s';
Timothy Warren80ab8162011-08-22 18:26:12 -040038
39 /**
40 * Create Table
41 *
Timothy Warren80ab8162011-08-22 18:26:12 -040042 * @param string the table name
43 * @param array the fields
44 * @param mixed primary key(s)
45 * @param mixed key(s)
Andrey Andreev9fd79f52012-03-20 23:04:13 +020046 * @param bool should 'IF NOT EXISTS' be added to the SQL
Timothy Warren80ab8162011-08-22 18:26:12 -040047 * @return bool
48 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +030049 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Timothy Warren80ab8162011-08-22 18:26:12 -040050 {
51 $sql = 'CREATE TABLE ';
52
53 if ($if_not_exists === TRUE)
54 {
55 $sql .= 'IF NOT EXISTS ';
56 }
57
Andrey Andreevea09a8a2012-04-06 20:50:07 +030058 $sql .= $this->db->escape_identifiers($table).' (';
Timothy Warren80ab8162011-08-22 18:26:12 -040059 $current_field_count = 0;
60
Andrey Andreevea09a8a2012-04-06 20:50:07 +030061 foreach ($fields as $field => $attributes)
Timothy Warren80ab8162011-08-22 18:26:12 -040062 {
63 // Numeric field names aren't allowed in databases, so if the key is
64 // numeric, we know it was assigned by PHP and the developer manually
65 // entered the field information, so we'll simply add it to the list
66 if (is_numeric($field))
67 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +030068 $sql .= "\n\t".$attributes;
Timothy Warren80ab8162011-08-22 18:26:12 -040069 }
70 else
71 {
72 $attributes = array_change_key_case($attributes, CASE_UPPER);
Andrey Andreev5ef194d2012-06-08 01:12:54 +030073 $numeric = array('SERIAL', 'INTEGER');
Timothy Warren80ab8162011-08-22 18:26:12 -040074
Andrey Andreev5ef194d2012-06-08 01:12:54 +030075 $sql .= "\n\t".$this->db->escape_identifiers($field).' '.$attributes['TYPE'];
Timothy Warren80ab8162011-08-22 18:26:12 -040076
Andrey Andreev5ef194d2012-06-08 01:12:54 +030077 if ( ! empty($attributes['CONSTRAINT']))
Timothy Warren80ab8162011-08-22 18:26:12 -040078 {
Taufan Aditya18209332012-02-09 16:07:27 +070079 // Exception for Postgre numeric which not too happy with constraint within those type
Andrey Andreevfbba54e2012-06-23 20:26:31 +030080 if ( ! ($this->db->subdriver === 'pgsql' && in_array($attributes['TYPE'], $numeric)))
Taufan Aditya18209332012-02-09 16:07:27 +070081 {
82 $sql .= '('.$attributes['CONSTRAINT'].')';
83 }
Timothy Warren80ab8162011-08-22 18:26:12 -040084 }
85
Andrey Andreev5ef194d2012-06-08 01:12:54 +030086 if ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE)
Timothy Warren80ab8162011-08-22 18:26:12 -040087 {
88 $sql .= ' UNSIGNED';
89 }
90
Andrey Andreev5ef194d2012-06-08 01:12:54 +030091 if (isset($attributes['DEFAULT']))
Timothy Warren80ab8162011-08-22 18:26:12 -040092 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +030093 $sql .= " DEFAULT '".$attributes['DEFAULT']."'";
Timothy Warren80ab8162011-08-22 18:26:12 -040094 }
95
Andrey Andreev5ef194d2012-06-08 01:12:54 +030096 $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE)
97 ? ' NULL' : ' NOT NULL';
Timothy Warren80ab8162011-08-22 18:26:12 -040098
Andrey Andreev5ef194d2012-06-08 01:12:54 +030099 if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400100 {
101 $sql .= ' AUTO_INCREMENT';
102 }
103 }
104
105 // don't add a comma on the end of the last field
106 if (++$current_field_count < count($fields))
107 {
108 $sql .= ',';
109 }
110 }
111
112 if (count($primary_keys) > 0)
113 {
Andrey Andreev9637b402012-06-08 15:39:24 +0300114 $sql .= ",\n\tPRIMARY KEY (".implode(', ', $this->db->escape_identifiers($primary_keys)).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400115 }
116
117 if (is_array($keys) && count($keys) > 0)
118 {
119 foreach ($keys as $key)
120 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300121 $key = is_array($key)
Andrey Andreev9637b402012-06-08 15:39:24 +0300122 ? $this->db->escape_identifiers($key)
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300123 : array($this->db->escape_identifiers($key));
Timothy Warren80ab8162011-08-22 18:26:12 -0400124
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300125 $sql .= ",\n\tFOREIGN KEY (".implode(', ', $key).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400126 }
127 }
128
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300129 return $sql."\n)";
Timothy Warren80ab8162011-08-22 18:26:12 -0400130 }
131
132 // --------------------------------------------------------------------
133
134 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400135 * Alter table query
136 *
137 * Generates a platform-specific query so that a table can be altered
138 * Called by add_column(), drop_column(), and column_alter(),
139 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400140 * @param string the ALTER type (ADD, DROP, CHANGE)
141 * @param string the column name
142 * @param string the table name
143 * @param string the column definition
144 * @param string the default value
Andrey Andreev9fd79f52012-03-20 23:04:13 +0200145 * @param bool should 'NOT NULL' be added
Timothy Warren80ab8162011-08-22 18:26:12 -0400146 * @param string the field after which we should add the new field
Andrey Andreev9fd79f52012-03-20 23:04:13 +0200147 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400148 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300149 protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400150 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300151 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '.$this->db->escape_identifiers($column_name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400152
153 // DROP has everything it needs now.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100154 if ($alter_type === 'DROP')
Timothy Warren80ab8162011-08-22 18:26:12 -0400155 {
156 return $sql;
157 }
158
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300159 return $sql .' '.$column_definition
160 .($default_value !== '' ? " DEFAULT '".$default_value."'" : '')
161 .($null === NULL ? ' NULL' : ' NOT NULL')
162 .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : '');
Timothy Warren80ab8162011-08-22 18:26:12 -0400163 }
164
Timothy Warren80ab8162011-08-22 18:26:12 -0400165}
166
167/* End of file pdo_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400168/* Location: ./system/database/drivers/pdo/pdo_forge.php */