Subversion-Projekte lars-tiefland.ci

Revision

Revision 2049 | Zur aktuellen Revision | Details | 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
 *
9
 * Copyright (c) 2014 - 2016, British Columbia Institute of Technology
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/)
32
 * @copyright	Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
33
 * @license	http://opensource.org/licenses/MIT	MIT License
34
 * @link	https://codeigniter.com
35
 * @since	Version 3.0.0
36
 * @filesource
37
 */
38
defined('BASEPATH') OR exit('No direct script access allowed');
39
 
40
/**
41
 * PDO CUBRID Database Adapter Class
42
 *
43
 * Note: _DB is an extender class that the app controller
44
 * creates dynamically based on whether the query builder
45
 * class is being used or not.
46
 *
47
 * @package		CodeIgniter
48
 * @subpackage	Drivers
49
 * @category	Database
50
 * @author		EllisLab Dev Team
51
 * @link		https://codeigniter.com/user_guide/database/
52
 */
53
class CI_DB_pdo_cubrid_driver extends CI_DB_pdo_driver {
54
 
55
	/**
56
	 * Sub-driver
57
	 *
58
	 * @var	string
59
	 */
60
	public $subdriver = 'cubrid';
61
 
62
	/**
63
	 * Identifier escape character
64
	 *
65
	 * @var	string
66
	 */
67
	protected $_escape_char = '`';
68
 
69
	/**
70
	 * ORDER BY random keyword
71
	 *
72
	 * @var array
73
	 */
74
	protected $_random_keyword = array('RANDOM()', 'RANDOM(%d)');
75
 
76
	// --------------------------------------------------------------------
77
 
78
	/**
79
	 * Class constructor
80
	 *
81
	 * Builds the DSN if not already set.
82
	 *
83
	 * @param	array	$params
84
	 * @return	void
85
	 */
86
	public function __construct($params)
87
	{
88
		parent::__construct($params);
89
 
90
		if (empty($this->dsn))
91
		{
92
			$this->dsn = 'cubrid:host='.(empty($this->hostname) ? '127.0.0.1' : $this->hostname);
93
 
94
			empty($this->port) OR $this->dsn .= ';port='.$this->port;
95
			empty($this->database) OR $this->dsn .= ';dbname='.$this->database;
96
			empty($this->char_set) OR $this->dsn .= ';charset='.$this->char_set;
97
		}
98
	}
99
 
100
	// --------------------------------------------------------------------
101
 
102
	/**
103
	 * Show table query
104
	 *
105
	 * Generates a platform-specific query string so that the table names can be fetched
106
	 *
107
	 * @param	bool	$prefix_limit
108
	 * @return	string
109
	 */
110
	protected function _list_tables($prefix_limit = FALSE)
111
	{
112
		$sql = 'SHOW TABLES';
113
 
114
		if ($prefix_limit === TRUE && $this->dbprefix !== '')
115
		{
116
			return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
117
		}
118
 
119
		return $sql;
120
	}
121
 
122
	// --------------------------------------------------------------------
123
 
124
	/**
125
	 * Show column query
126
	 *
127
	 * Generates a platform-specific query string so that the column names can be fetched
128
	 *
129
	 * @param	string	$table
130
	 * @return	string
131
	 */
132
	protected function _list_columns($table = '')
133
	{
134
		return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
135
	}
136
 
137
	// --------------------------------------------------------------------
138
 
139
	/**
140
	 * Returns an object with field data
141
	 *
142
	 * @param	string	$table
143
	 * @return	array
144
	 */
145
	public function field_data($table)
146
	{
147
		if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
148
		{
149
			return FALSE;
150
		}
151
		$query = $query->result_object();
152
 
153
		$retval = array();
154
		for ($i = 0, $c = count($query); $i < $c; $i++)
155
		{
156
			$retval[$i]			= new stdClass();
157
			$retval[$i]->name		= $query[$i]->Field;
158
 
159
			sscanf($query[$i]->Type, '%[a-z](%d)',
160
				$retval[$i]->type,
161
				$retval[$i]->max_length
162
			);
163
 
164
			$retval[$i]->default		= $query[$i]->Default;
165
			$retval[$i]->primary_key	= (int) ($query[$i]->Key === 'PRI');
166
		}
167
 
168
		return $retval;
169
	}
170
 
171
	// --------------------------------------------------------------------
172
 
173
	/**
174
	 * Update_Batch statement
175
	 *
176
	 * Generates a platform-specific batch update string from the supplied data
177
	 *
178
	 * @param	string	$table	Table name
179
	 * @param	array	$values	Update data
180
	 * @param	string	$index	WHERE key
181
	 * @return	string
182
	 */
183
	protected function _update_batch($table, $values, $index)
184
	{
185
		$ids = array();
186
		foreach ($values as $key => $val)
187
		{
188
			$ids[] = $val[$index];
189
 
190
			foreach (array_keys($val) as $field)
191
			{
192
				if ($field !== $index)
193
				{
194
					$final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
195
				}
196
			}
197
		}
198
 
199
		$cases = '';
200
		foreach ($final as $k => $v)
201
		{
202
			$cases .= $k." = CASE \n"
203
				.implode("\n", $v)."\n"
204
				.'ELSE '.$k.' END), ';
205
		}
206
 
207
		$this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
208
 
209
		return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
210
	}
211
 
212
	// --------------------------------------------------------------------
213
 
214
	/**
215
	 * Truncate statement
216
	 *
217
	 * Generates a platform-specific truncate string from the supplied data
218
	 *
219
	 * If the database does not support the TRUNCATE statement,
220
	 * then this method maps to 'DELETE FROM table'
221
	 *
222
	 * @param	string	$table
223
	 * @return	string
224
	 */
225
	protected function _truncate($table)
226
	{
227
		return 'TRUNCATE '.$table;
228
	}
229
 
230
	// --------------------------------------------------------------------
231
 
232
	/**
233
	 * FROM tables
234
	 *
235
	 * Groups tables in FROM clauses if needed, so there is no confusion
236
	 * about operator precedence.
237
	 *
238
	 * @return	string
239
	 */
240
	protected function _from_tables()
241
	{
242
		if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
243
		{
244
			return '('.implode(', ', $this->qb_from).')';
245
		}
246
 
247
		return implode(', ', $this->qb_from);
248
	}
249
 
250
}