Subversion-Projekte lars-tiefland.ci

Revision

Revision 2257 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
68 lars 1
<?php
2
/**
3
 * CodeIgniter
4
 *
5
 * An open source application development framework for PHP
6
 *
7
 * This content is released under the MIT License (MIT)
8
 *
2414 lars 9
 * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
68 lars 10
 *
11
 * Permission is hereby granted, free of charge, to any person obtaining a copy
12
 * of this software and associated documentation files (the "Software"), to deal
13
 * in the Software without restriction, including without limitation the rights
14
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
 * copies of the Software, and to permit persons to whom the Software is
16
 * furnished to do so, subject to the following conditions:
17
 *
18
 * The above copyright notice and this permission notice shall be included in
19
 * all copies or substantial portions of the Software.
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
 * THE SOFTWARE.
28
 *
29
 * @package	CodeIgniter
30
 * @author	EllisLab Dev Team
31
 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
2414 lars 32
 * @copyright	Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33
 * @license	https://opensource.org/licenses/MIT	MIT License
68 lars 34
 * @link	https://codeigniter.com
35
 * @since	Version 1.3.0
36
 * @filesource
37
 */
38
defined('BASEPATH') OR exit('No direct script access allowed');
39
 
40
/**
41
 * Postgre Forge Class
42
 *
43
 * @package		CodeIgniter
44
 * @subpackage	Drivers
45
 * @category	Database
46
 * @author		EllisLab Dev Team
47
 * @link		https://codeigniter.com/user_guide/database/
48
 */
49
class CI_DB_postgre_forge extends CI_DB_forge {
50
 
51
	/**
52
	 * UNSIGNED support
53
	 *
54
	 * @var	array
55
	 */
56
	protected $_unsigned		= array(
57
		'INT2'		=> 'INTEGER',
58
		'SMALLINT'	=> 'INTEGER',
59
		'INT'		=> 'BIGINT',
60
		'INT4'		=> 'BIGINT',
61
		'INTEGER'	=> 'BIGINT',
62
		'INT8'		=> 'NUMERIC',
63
		'BIGINT'	=> 'NUMERIC',
64
		'REAL'		=> 'DOUBLE PRECISION',
65
		'FLOAT'		=> 'DOUBLE PRECISION'
66
	);
67
 
68
	/**
69
	 * NULL value representation in CREATE/ALTER TABLE statements
70
	 *
71
	 * @var	string
72
	 */
73
	protected $_null = 'NULL';
74
 
75
	// --------------------------------------------------------------------
76
 
77
	/**
78
	 * Class constructor
79
	 *
80
	 * @param	object	&$db	Database object
81
	 * @return	void
82
	 */
83
	public function __construct(&$db)
84
	{
85
		parent::__construct($db);
86
 
87
		if (version_compare($this->db->version(), '9.0', '>'))
88
		{
89
			$this->create_table_if = 'CREATE TABLE IF NOT EXISTS';
90
		}
91
	}
92
 
93
	// --------------------------------------------------------------------
94
 
95
	/**
96
	 * ALTER TABLE
97
	 *
98
	 * @param	string	$alter_type	ALTER type
99
	 * @param	string	$table		Table name
100
	 * @param	mixed	$field		Column definition
101
	 * @return	string|string[]
102
	 */
103
	protected function _alter_table($alter_type, $table, $field)
104
 	{
105
		if (in_array($alter_type, array('DROP', 'ADD'), TRUE))
106
		{
107
			return parent::_alter_table($alter_type, $table, $field);
108
		}
109
 
110
		$sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
111
		$sqls = array();
112
		for ($i = 0, $c = count($field); $i < $c; $i++)
113
		{
114
			if ($field[$i]['_literal'] !== FALSE)
115
			{
116
				return FALSE;
117
			}
118
 
119
			if (version_compare($this->db->version(), '8', '>=') && isset($field[$i]['type']))
120
			{
121
				$sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
122
					.' TYPE '.$field[$i]['type'].$field[$i]['length'];
123
			}
124
 
125
			if ( ! empty($field[$i]['default']))
126
			{
127
				$sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
2414 lars 128
					.' SET '.$field[$i]['default'];
68 lars 129
			}
130
 
131
			if (isset($field[$i]['null']))
132
			{
133
				$sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
2414 lars 134
					.(trim($field[$i]['null']) === $this->_null ? ' DROP NOT NULL' : ' SET NOT NULL');
68 lars 135
			}
136
 
137
			if ( ! empty($field[$i]['new_name']))
138
			{
139
				$sqls[] = $sql.' RENAME COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
140
					.' TO '.$this->db->escape_identifiers($field[$i]['new_name']);
141
			}
142
 
143
			if ( ! empty($field[$i]['comment']))
144
			{
145
				$sqls[] = 'COMMENT ON COLUMN '
146
					.$this->db->escape_identifiers($table).'.'.$this->db->escape_identifiers($field[$i]['name'])
147
					.' IS '.$field[$i]['comment'];
148
			}
149
		}
150
 
151
		return $sqls;
152
 	}
153
 
154
	// --------------------------------------------------------------------
155
 
156
	/**
157
	 * Field attribute TYPE
158
	 *
159
	 * Performs a data type mapping between different databases.
160
	 *
161
	 * @param	array	&$attributes
162
	 * @return	void
163
	 */
164
	protected function _attr_type(&$attributes)
165
	{
2107 lars 166
		// Reset field lengths for data types that don't support it
68 lars 167
		if (isset($attributes['CONSTRAINT']) && stripos($attributes['TYPE'], 'int') !== FALSE)
168
		{
169
			$attributes['CONSTRAINT'] = NULL;
170
		}
171
 
172
		switch (strtoupper($attributes['TYPE']))
173
		{
174
			case 'TINYINT':
175
				$attributes['TYPE'] = 'SMALLINT';
176
				$attributes['UNSIGNED'] = FALSE;
177
				return;
178
			case 'MEDIUMINT':
179
				$attributes['TYPE'] = 'INTEGER';
180
				$attributes['UNSIGNED'] = FALSE;
181
				return;
182
			default: return;
183
		}
184
	}
185
 
186
	// --------------------------------------------------------------------
187
 
188
	/**
189
	 * Field attribute AUTO_INCREMENT
190
	 *
191
	 * @param	array	&$attributes
192
	 * @param	array	&$field
193
	 * @return	void
194
	 */
195
	protected function _attr_auto_increment(&$attributes, &$field)
196
	{
197
		if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE)
198
		{
199
			$field['type'] = ($field['type'] === 'NUMERIC')
200
				? 'BIGSERIAL'
201
				: 'SERIAL';
202
		}
203
	}
204
 
205
}