blob: 6ad583560a1f2c68c3c31d513db505b1b616ffa4 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevd9038782012-01-26 12:38:49 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevd9038782012-01-26 12:38:49 +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 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @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)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * SQLite Forge Class
31 *
32 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050033 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000034 * @link http://codeigniter.com/user_guide/database/
35 */
36class CI_DB_sqlite_forge extends CI_DB_forge {
37
38 /**
Andrey Andreeva287a342012-11-05 23:19:59 +020039 * CREATE TABLE IF statement
40 *
41 * @var string
42 */
43 protected $_create_table_if = FALSE;
44
45 /**
46 * UNSIGNED support
47 *
48 * @var bool|array
49 */
50 protected $_unsigned = FALSE;
51
52 /**
53 * NULL value representation in CREATE/ALTER TABLE statements
54 *
55 * @var string
56 */
57 protected $_null = 'NULL';
58
59 // --------------------------------------------------------------------
60
61 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000062 * Create database
63 *
Andrey Andreeva287a342012-11-05 23:19:59 +020064 * @param string $db_name (ignored)
Derek Allard2067d1a2008-11-13 22:59:24 +000065 * @return bool
66 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030067 public function create_database($db_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000068 {
69 // In SQLite, a database is created when you connect to the database.
70 // We'll return TRUE so that an error isn't generated
71 return TRUE;
72 }
73
74 // --------------------------------------------------------------------
75
76 /**
77 * Drop database
78 *
Andrey Andreeva287a342012-11-05 23:19:59 +020079 * @param string $db_name (ignored)
Derek Allard2067d1a2008-11-13 22:59:24 +000080 * @return bool
81 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030082 public function drop_database($db_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000083 {
84 if ( ! @file_exists($this->db->database) OR ! @unlink($this->db->database))
85 {
Andrey Andreevd9038782012-01-26 12:38:49 +020086 return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000087 }
Andrey Andreev5d281762012-06-11 22:05:40 +030088 elseif ( ! empty($this->db->data_cache['db_names']))
89 {
90 $key = array_search(strtolower($this->db->database), array_map('strtolower', $this->db->data_cache['db_names']), TRUE);
91 if ($key !== FALSE)
92 {
93 unset($this->db->data_cache['db_names'][$key]);
94 }
95 }
Andrey Andreevd9038782012-01-26 12:38:49 +020096
Derek Allard2067d1a2008-11-13 22:59:24 +000097 return TRUE;
98 }
Andrey Andreev8480f7c2012-03-20 16:55:05 +020099
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 // --------------------------------------------------------------------
101
102 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200103 * ALTER TABLE
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200105 * @todo implement drop_column(), modify_column()
106 * @param string $alter_type ALTER type
107 * @param string $table Table name
108 * @param mixed $field Column definition
109 * @return string|string[]
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200111 protected function _alter_table($alter_type, $table, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200113 if ($alter_type === 'DROP' OR $alter_type === 'CHANGE')
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200115 // drop_column():
116 // BEGIN TRANSACTION;
117 // CREATE TEMPORARY TABLE t1_backup(a,b);
118 // INSERT INTO t1_backup SELECT a,b FROM t1;
119 // DROP TABLE t1;
120 // CREATE TABLE t1(a,b);
121 // INSERT INTO t1 SELECT a,b FROM t1_backup;
122 // DROP TABLE t1_backup;
123 // COMMIT;
124
125 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 }
Barry Mienydd671972010-10-04 16:33:58 +0200127
Andrey Andreeva287a342012-11-05 23:19:59 +0200128 return parent::_alter_table($alter_type, $table, $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 }
130
131 // --------------------------------------------------------------------
132
133 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200134 * Process column
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200136 * @param array $field
Andrey Andreevd9038782012-01-26 12:38:49 +0200137 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200139 protected function _process_column($field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200141 return $this->db->escape_identifiers($field['name'])
142 .' '.$field['type']
143 .$field['auto_increment']
144 .$field['null']
145 .$field['unique']
146 .$field['default'];
147 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000148
Andrey Andreeva287a342012-11-05 23:19:59 +0200149 // --------------------------------------------------------------------
150
151 /**
152 * Field attribute TYPE
153 *
154 * Performs a data type mapping between different databases.
155 *
156 * @param array &$attributes
157 * @return void
158 */
159 protected function _attr_type(&$attributes)
160 {
161 switch (strtoupper($attributes['TYPE']))
162 {
163 case 'ENUM':
164 case 'SET':
165 $attributes['TYPE'] = 'TEXT';
166 return;
167 default: return;
168 }
169 }
170
171 // --------------------------------------------------------------------
172
173 /**
174 * Field attribute AUTO_INCREMENT
175 *
176 * @param array &$attributes
177 * @param array &$field
178 * @return void
179 */
180 protected function _attr_auto_increment(&$attributes, &$field)
181 {
182 if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE && stripos($field['type'], 'int') !== FALSE)
183 {
184 $field['type'] = 'INTEGER PRIMARY KEY';
185 $field['default'] = '';
186 $field['null'] = '';
187 $field['unique'] = '';
188 $field['auto_increment'] = ' AUTOINCREMENT';
189
190 $this->primary_keys = array();
191 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 }
193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194}
195
196/* End of file sqlite_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400197/* Location: ./system/database/drivers/sqlite/sqlite_forge.php */