| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* SVN FILE: $Id: dbo_mssql.php 7945 2008-12-19 02:16:01Z gwoo $ */
|
|
|
3 |
/**
|
|
|
4 |
* MS SQL layer for DBO
|
|
|
5 |
*
|
|
|
6 |
* Long description for file
|
|
|
7 |
*
|
|
|
8 |
* PHP versions 4 and 5
|
|
|
9 |
*
|
|
|
10 |
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
|
|
11 |
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
12 |
*
|
|
|
13 |
* Licensed under The MIT License
|
|
|
14 |
* Redistributions of files must retain the above copyright notice.
|
|
|
15 |
*
|
|
|
16 |
* @filesource
|
|
|
17 |
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
18 |
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
19 |
* @package cake
|
|
|
20 |
* @subpackage cake.cake.libs.model.datasources.dbo
|
|
|
21 |
* @since CakePHP(tm) v 0.10.5.1790
|
|
|
22 |
* @version $Revision: 7945 $
|
|
|
23 |
* @modifiedby $LastChangedBy: gwoo $
|
|
|
24 |
* @lastmodified $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
|
|
|
25 |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
26 |
*/
|
|
|
27 |
/**
|
|
|
28 |
* Short description for class.
|
|
|
29 |
*
|
|
|
30 |
* Long description for class
|
|
|
31 |
*
|
|
|
32 |
* @package cake
|
|
|
33 |
* @subpackage cake.cake.libs.model.datasources.dbo
|
|
|
34 |
*/
|
|
|
35 |
class DboMssql extends DboSource {
|
|
|
36 |
/**
|
|
|
37 |
* Driver description
|
|
|
38 |
*
|
|
|
39 |
* @var string
|
|
|
40 |
*/
|
|
|
41 |
var $description = "MS SQL DBO Driver";
|
|
|
42 |
/**
|
|
|
43 |
* Starting quote character for quoted identifiers
|
|
|
44 |
*
|
|
|
45 |
* @var string
|
|
|
46 |
*/
|
|
|
47 |
var $startQuote = "[";
|
|
|
48 |
/**
|
|
|
49 |
* Ending quote character for quoted identifiers
|
|
|
50 |
*
|
|
|
51 |
* @var string
|
|
|
52 |
*/
|
|
|
53 |
var $endQuote = "]";
|
|
|
54 |
/**
|
|
|
55 |
* Creates a map between field aliases and numeric indexes. Workaround for the
|
|
|
56 |
* SQL Server driver's 30-character column name limitation.
|
|
|
57 |
*
|
|
|
58 |
* @var array
|
|
|
59 |
*/
|
|
|
60 |
var $__fieldMappings = array();
|
|
|
61 |
/**
|
|
|
62 |
* Base configuration settings for MS SQL driver
|
|
|
63 |
*
|
|
|
64 |
* @var array
|
|
|
65 |
*/
|
|
|
66 |
var $_baseConfig = array(
|
|
|
67 |
'persistent' => true,
|
|
|
68 |
'host' => 'localhost',
|
|
|
69 |
'login' => 'root',
|
|
|
70 |
'password' => '',
|
|
|
71 |
'database' => 'cake',
|
|
|
72 |
'port' => '1433',
|
|
|
73 |
);
|
|
|
74 |
/**
|
|
|
75 |
* MS SQL column definition
|
|
|
76 |
*
|
|
|
77 |
* @var array
|
|
|
78 |
*/
|
|
|
79 |
var $columns = array(
|
|
|
80 |
'primary_key' => array('name' => 'IDENTITY (1, 1) NOT NULL'),
|
|
|
81 |
'string' => array('name' => 'varchar', 'limit' => '255'),
|
|
|
82 |
'text' => array('name' => 'text'),
|
|
|
83 |
'integer' => array('name' => 'int', 'formatter' => 'intval'),
|
|
|
84 |
'float' => array('name' => 'numeric', 'formatter' => 'floatval'),
|
|
|
85 |
'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
|
|
|
86 |
'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
|
|
|
87 |
'time' => array('name' => 'datetime', 'format' => 'H:i:s', 'formatter' => 'date'),
|
|
|
88 |
'date' => array('name' => 'datetime', 'format' => 'Y-m-d', 'formatter' => 'date'),
|
|
|
89 |
'binary' => array('name' => 'image'),
|
|
|
90 |
'boolean' => array('name' => 'bit')
|
|
|
91 |
);
|
|
|
92 |
/**
|
|
|
93 |
* Index of basic SQL commands
|
|
|
94 |
*
|
|
|
95 |
* @var array
|
|
|
96 |
* @access protected
|
|
|
97 |
*/
|
|
|
98 |
var $_commands = array(
|
|
|
99 |
'begin' => 'BEGIN TRANSACTION',
|
|
|
100 |
'commit' => 'COMMIT',
|
|
|
101 |
'rollback' => 'ROLLBACK'
|
|
|
102 |
);
|
|
|
103 |
/**
|
|
|
104 |
* MS SQL DBO driver constructor; sets SQL Server error reporting defaults
|
|
|
105 |
*
|
|
|
106 |
* @param array $config Configuration data from app/config/databases.php
|
|
|
107 |
* @return boolean True if connected successfully, false on error
|
|
|
108 |
*/
|
|
|
109 |
function __construct($config, $autoConnect = true) {
|
|
|
110 |
if ($autoConnect) {
|
|
|
111 |
if (!function_exists('mssql_min_message_severity')) {
|
|
|
112 |
trigger_error("PHP SQL Server interface is not installed, cannot continue. For troubleshooting information, see http://php.net/mssql/", E_USER_WARNING);
|
|
|
113 |
}
|
|
|
114 |
mssql_min_message_severity(15);
|
|
|
115 |
mssql_min_error_severity(2);
|
|
|
116 |
}
|
|
|
117 |
return parent::__construct($config, $autoConnect);
|
|
|
118 |
}
|
|
|
119 |
/**
|
|
|
120 |
* Connects to the database using options in the given configuration array.
|
|
|
121 |
*
|
|
|
122 |
* @return boolean True if the database could be connected, else false
|
|
|
123 |
*/
|
|
|
124 |
function connect() {
|
|
|
125 |
$config = $this->config;
|
|
|
126 |
|
|
|
127 |
$os = env('OS');
|
|
|
128 |
if (!empty($os) && strpos($os, 'Windows') !== false) {
|
|
|
129 |
$sep = ',';
|
|
|
130 |
} else {
|
|
|
131 |
$sep = ':';
|
|
|
132 |
}
|
|
|
133 |
$this->connected = false;
|
|
|
134 |
|
|
|
135 |
if (is_numeric($config['port'])) {
|
|
|
136 |
$port = $sep . $config['port']; // Port number
|
|
|
137 |
} elseif ($config['port'] === null) {
|
|
|
138 |
$port = ''; // No port - SQL Server 2005
|
|
|
139 |
} else {
|
|
|
140 |
$port = '\\' . $config['port']; // Named pipe
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
if (!$config['persistent']) {
|
|
|
144 |
$this->connection = mssql_connect($config['host'] . $port, $config['login'], $config['password'], true);
|
|
|
145 |
} else {
|
|
|
146 |
$this->connection = mssql_pconnect($config['host'] . $port, $config['login'], $config['password']);
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
if (mssql_select_db($config['database'], $this->connection)) {
|
|
|
150 |
$this->_execute("SET DATEFORMAT ymd");
|
|
|
151 |
$this->connected = true;
|
|
|
152 |
}
|
|
|
153 |
return $this->connected;
|
|
|
154 |
}
|
|
|
155 |
/**
|
|
|
156 |
* Disconnects from database.
|
|
|
157 |
*
|
|
|
158 |
* @return boolean True if the database could be disconnected, else false
|
|
|
159 |
*/
|
|
|
160 |
function disconnect() {
|
|
|
161 |
@mssql_free_result($this->results);
|
|
|
162 |
$this->connected = !@mssql_close($this->connection);
|
|
|
163 |
return !$this->connected;
|
|
|
164 |
}
|
|
|
165 |
/**
|
|
|
166 |
* Executes given SQL statement.
|
|
|
167 |
*
|
|
|
168 |
* @param string $sql SQL statement
|
|
|
169 |
* @return resource Result resource identifier
|
|
|
170 |
* @access protected
|
|
|
171 |
*/
|
|
|
172 |
function _execute($sql) {
|
|
|
173 |
return mssql_query($sql, $this->connection);
|
|
|
174 |
}
|
|
|
175 |
/**
|
|
|
176 |
* Returns an array of sources (tables) in the database.
|
|
|
177 |
*
|
|
|
178 |
* @return array Array of tablenames in the database
|
|
|
179 |
*/
|
|
|
180 |
function listSources() {
|
|
|
181 |
$cache = parent::listSources();
|
|
|
182 |
|
|
|
183 |
if ($cache != null) {
|
|
|
184 |
return $cache;
|
|
|
185 |
}
|
|
|
186 |
$result = $this->fetchAll('SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES', false);
|
|
|
187 |
|
|
|
188 |
if (!$result || empty($result)) {
|
|
|
189 |
return array();
|
|
|
190 |
} else {
|
|
|
191 |
$tables = array();
|
|
|
192 |
|
|
|
193 |
foreach ($result as $table) {
|
|
|
194 |
$tables[] = $table[0]['TABLE_NAME'];
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
parent::listSources($tables);
|
|
|
198 |
return $tables;
|
|
|
199 |
}
|
|
|
200 |
}
|
|
|
201 |
/**
|
|
|
202 |
* Returns an array of the fields in given table name.
|
|
|
203 |
*
|
|
|
204 |
* @param Model $model Model object to describe
|
|
|
205 |
* @return array Fields in table. Keys are name and type
|
|
|
206 |
*/
|
|
|
207 |
function describe(&$model) {
|
|
|
208 |
$cache = parent::describe($model);
|
|
|
209 |
|
|
|
210 |
if ($cache != null) {
|
|
|
211 |
return $cache;
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
$fields = false;
|
|
|
215 |
$cols = $this->fetchAll("SELECT COLUMN_NAME as Field, DATA_TYPE as Type, COL_LENGTH('" . $this->fullTableName($model, false) . "', COLUMN_NAME) as Length, IS_NULLABLE As [Null], COLUMN_DEFAULT as [Default], COLUMNPROPERTY(OBJECT_ID('" . $this->fullTableName($model, false) . "'), COLUMN_NAME, 'IsIdentity') as [Key], NUMERIC_SCALE as Size FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" . $this->fullTableName($model, false) . "'", false);
|
|
|
216 |
|
|
|
217 |
foreach ($cols as $column) {
|
|
|
218 |
$field = $column[0]['Field'];
|
|
|
219 |
$fields[$field] = array(
|
|
|
220 |
'type' => $this->column($column[0]['Type']),
|
|
|
221 |
'null' => (strtoupper($column[0]['Null']) == 'YES'),
|
|
|
222 |
'default' => preg_replace("/^[(]{1,2}'?([^')]*)?'?[)]{1,2}$/", "$1", $column[0]['Default']),
|
|
|
223 |
'length' => intval($column[0]['Length']),
|
|
|
224 |
'key' => ($column[0]['Key'] == '1')
|
|
|
225 |
);
|
|
|
226 |
if ($fields[$field]['default'] === 'null') {
|
|
|
227 |
$fields[$field]['default'] = null;
|
|
|
228 |
} else {
|
|
|
229 |
$this->value($fields[$field]['default'], $fields[$field]['type']);
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
if ($fields[$field]['key'] && $fields[$field]['type'] == 'integer') {
|
|
|
233 |
$fields[$field]['length'] = 11;
|
|
|
234 |
} elseif (!$fields[$field]['key']) {
|
|
|
235 |
unset($fields[$field]['key']);
|
|
|
236 |
}
|
|
|
237 |
if (in_array($fields[$field]['type'], array('date', 'time', 'datetime', 'timestamp'))) {
|
|
|
238 |
$fields[$field]['length'] = null;
|
|
|
239 |
}
|
|
|
240 |
}
|
|
|
241 |
$this->__cacheDescription($this->fullTableName($model, false), $fields);
|
|
|
242 |
return $fields;
|
|
|
243 |
}
|
|
|
244 |
/**
|
|
|
245 |
* Returns a quoted and escaped string of $data for use in an SQL statement.
|
|
|
246 |
*
|
|
|
247 |
* @param string $data String to be prepared for use in an SQL statement
|
|
|
248 |
* @param string $column The column into which this data will be inserted
|
|
|
249 |
* @param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided
|
|
|
250 |
* @return string Quoted and escaped data
|
|
|
251 |
*/
|
|
|
252 |
function value($data, $column = null, $safe = false) {
|
|
|
253 |
$parent = parent::value($data, $column, $safe);
|
|
|
254 |
|
|
|
255 |
if ($parent != null) {
|
|
|
256 |
return $parent;
|
|
|
257 |
}
|
|
|
258 |
if ($data === null) {
|
|
|
259 |
return 'NULL';
|
|
|
260 |
}
|
|
|
261 |
if ($data === '') {
|
|
|
262 |
return "''";
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
switch ($column) {
|
|
|
266 |
case 'boolean':
|
|
|
267 |
$data = $this->boolean((bool)$data);
|
|
|
268 |
break;
|
|
|
269 |
default:
|
|
|
270 |
if (get_magic_quotes_gpc()) {
|
|
|
271 |
$data = stripslashes(str_replace("'", "''", $data));
|
|
|
272 |
} else {
|
|
|
273 |
$data = str_replace("'", "''", $data);
|
|
|
274 |
}
|
|
|
275 |
break;
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
if (in_array($column, array('integer', 'float', 'binary')) && is_numeric($data)) {
|
|
|
279 |
return $data;
|
|
|
280 |
}
|
|
|
281 |
return "'" . $data . "'";
|
|
|
282 |
}
|
|
|
283 |
/**
|
|
|
284 |
* Generates the fields list of an SQL query.
|
|
|
285 |
*
|
|
|
286 |
* @param Model $model
|
|
|
287 |
* @param string $alias Alias tablename
|
|
|
288 |
* @param mixed $fields
|
|
|
289 |
* @return array
|
|
|
290 |
*/
|
|
|
291 |
function fields(&$model, $alias = null, $fields = array(), $quote = true) {
|
|
|
292 |
if (empty($alias)) {
|
|
|
293 |
$alias = $model->alias;
|
|
|
294 |
}
|
|
|
295 |
$fields = parent::fields($model, $alias, $fields, false);
|
|
|
296 |
$count = count($fields);
|
|
|
297 |
|
|
|
298 |
if ($count >= 1 && $fields[0] != '*' && strpos($fields[0], 'COUNT(*)') === false) {
|
|
|
299 |
for ($i = 0; $i < $count; $i++) {
|
|
|
300 |
$prepend = '';
|
|
|
301 |
|
|
|
302 |
if (strpos($fields[$i], 'DISTINCT') !== false) {
|
|
|
303 |
$prepend = 'DISTINCT ';
|
|
|
304 |
$fields[$i] = trim(str_replace('DISTINCT', '', $fields[$i]));
|
|
|
305 |
}
|
|
|
306 |
$fieldAlias = count($this->__fieldMappings);
|
|
|
307 |
|
|
|
308 |
if (!preg_match('/\s+AS\s+/i', $fields[$i])) {
|
|
|
309 |
if (strpos($fields[$i], '.') === false) {
|
|
|
310 |
$this->__fieldMappings[$alias . '__' . $fieldAlias] = $alias . '.' . $fields[$i];
|
|
|
311 |
$fieldName = $this->name($alias . '.' . $fields[$i]);
|
|
|
312 |
$fieldAlias = $this->name($alias . '__' . $fieldAlias);
|
|
|
313 |
} else {
|
|
|
314 |
$build = explode('.', $fields[$i]);
|
|
|
315 |
$this->__fieldMappings[$build[0] . '__' . $fieldAlias] = $fields[$i];
|
|
|
316 |
$fieldName = $this->name($build[0] . '.' . $build[1]);
|
|
|
317 |
$fieldAlias = $this->name(preg_replace("/^\[(.+)\]$/", "$1", $build[0]) . '__' . $fieldAlias);
|
|
|
318 |
}
|
|
|
319 |
if ($model->getColumnType($fields[$i]) == 'datetime') {
|
|
|
320 |
$fieldName = "CONVERT(VARCHAR(20), {$fieldName}, 20)";
|
|
|
321 |
}
|
|
|
322 |
$fields[$i] = "{$fieldName} AS {$fieldAlias}";
|
|
|
323 |
}
|
|
|
324 |
$fields[$i] = $prepend . $fields[$i];
|
|
|
325 |
}
|
|
|
326 |
}
|
|
|
327 |
return $fields;
|
|
|
328 |
}
|
|
|
329 |
/**
|
|
|
330 |
* Generates and executes an SQL INSERT statement for given model, fields, and values.
|
|
|
331 |
* Removes Identity (primary key) column from update data before returning to parent, if
|
|
|
332 |
* value is empty.
|
|
|
333 |
*
|
|
|
334 |
* @param Model $model
|
|
|
335 |
* @param array $fields
|
|
|
336 |
* @param array $values
|
|
|
337 |
* @param mixed $conditions
|
|
|
338 |
* @return array
|
|
|
339 |
*/
|
|
|
340 |
function create(&$model, $fields = null, $values = null) {
|
|
|
341 |
if (!empty($values)) {
|
|
|
342 |
$fields = array_combine($fields, $values);
|
|
|
343 |
}
|
|
|
344 |
|
|
|
345 |
if (array_key_exists($model->primaryKey, $fields)) {
|
|
|
346 |
if (empty($fields[$model->primaryKey])) {
|
|
|
347 |
unset($fields[$model->primaryKey]);
|
|
|
348 |
} else {
|
|
|
349 |
$this->_execute("SET IDENTITY_INSERT " . $this->fullTableName($model) . " ON");
|
|
|
350 |
}
|
|
|
351 |
}
|
|
|
352 |
$result = parent::create($model, array_keys($fields), array_values($fields));
|
|
|
353 |
if (array_key_exists($model->primaryKey, $fields) && !empty($fields[$model->primaryKey])) {
|
|
|
354 |
$this->_execute("SET IDENTITY_INSERT " . $this->fullTableName($model) . " OFF");
|
|
|
355 |
}
|
|
|
356 |
return $result;
|
|
|
357 |
}
|
|
|
358 |
/**
|
|
|
359 |
* Generates and executes an SQL UPDATE statement for given model, fields, and values.
|
|
|
360 |
* Removes Identity (primary key) column from update data before returning to parent.
|
|
|
361 |
*
|
|
|
362 |
* @param Model $model
|
|
|
363 |
* @param array $fields
|
|
|
364 |
* @param array $values
|
|
|
365 |
* @param mixed $conditions
|
|
|
366 |
* @return array
|
|
|
367 |
*/
|
|
|
368 |
function update(&$model, $fields = array(), $values = null, $conditions = null) {
|
|
|
369 |
if (!empty($values)) {
|
|
|
370 |
$fields = array_combine($fields, $values);
|
|
|
371 |
}
|
|
|
372 |
if (isset($fields[$model->primaryKey])) {
|
|
|
373 |
unset($fields[$model->primaryKey]);
|
|
|
374 |
}
|
|
|
375 |
return parent::update($model, array_keys($fields), array_values($fields), $conditions);
|
|
|
376 |
}
|
|
|
377 |
/**
|
|
|
378 |
* Returns a formatted error message from previous database operation.
|
|
|
379 |
*
|
|
|
380 |
* @return string Error message with error number
|
|
|
381 |
*/
|
|
|
382 |
function lastError() {
|
|
|
383 |
$error = mssql_get_last_message($this->connection);
|
|
|
384 |
|
|
|
385 |
if ($error) {
|
|
|
386 |
if (!preg_match('/contexto de la base de datos a|contesto di database|changed database/i', $error)) {
|
|
|
387 |
return $error;
|
|
|
388 |
}
|
|
|
389 |
}
|
|
|
390 |
return null;
|
|
|
391 |
}
|
|
|
392 |
/**
|
|
|
393 |
* Returns number of affected rows in previous database operation. If no previous operation exists,
|
|
|
394 |
* this returns false.
|
|
|
395 |
*
|
|
|
396 |
* @return integer Number of affected rows
|
|
|
397 |
*/
|
|
|
398 |
function lastAffected() {
|
|
|
399 |
if ($this->_result) {
|
|
|
400 |
return mssql_rows_affected($this->connection);
|
|
|
401 |
}
|
|
|
402 |
return null;
|
|
|
403 |
}
|
|
|
404 |
/**
|
|
|
405 |
* Returns number of rows in previous resultset. If no previous resultset exists,
|
|
|
406 |
* this returns false.
|
|
|
407 |
*
|
|
|
408 |
* @return integer Number of rows in resultset
|
|
|
409 |
*/
|
|
|
410 |
function lastNumRows() {
|
|
|
411 |
if ($this->_result) {
|
|
|
412 |
return @mssql_num_rows($this->_result);
|
|
|
413 |
}
|
|
|
414 |
return null;
|
|
|
415 |
}
|
|
|
416 |
/**
|
|
|
417 |
* Returns the ID generated from the previous INSERT operation.
|
|
|
418 |
*
|
|
|
419 |
* @param unknown_type $source
|
|
|
420 |
* @return in
|
|
|
421 |
*/
|
|
|
422 |
function lastInsertId($source = null) {
|
|
|
423 |
$id = $this->fetchRow('SELECT SCOPE_IDENTITY() AS insertID', false);
|
|
|
424 |
return $id[0]['insertID'];
|
|
|
425 |
}
|
|
|
426 |
/**
|
|
|
427 |
* Returns a limit statement in the correct format for the particular database.
|
|
|
428 |
*
|
|
|
429 |
* @param integer $limit Limit of results returned
|
|
|
430 |
* @param integer $offset Offset from which to start results
|
|
|
431 |
* @return string SQL limit/offset statement
|
|
|
432 |
*/
|
|
|
433 |
function limit($limit, $offset = null) {
|
|
|
434 |
if ($limit) {
|
|
|
435 |
$rt = '';
|
|
|
436 |
if (!strpos(strtolower($limit), 'top') || strpos(strtolower($limit), 'top') === 0) {
|
|
|
437 |
$rt = ' TOP';
|
|
|
438 |
}
|
|
|
439 |
$rt .= ' ' . $limit;
|
|
|
440 |
if (is_int($offset) && $offset > 0) {
|
|
|
441 |
$rt .= ' OFFSET ' . $offset;
|
|
|
442 |
}
|
|
|
443 |
return $rt;
|
|
|
444 |
}
|
|
|
445 |
return null;
|
|
|
446 |
}
|
|
|
447 |
/**
|
|
|
448 |
* Converts database-layer column types to basic types
|
|
|
449 |
*
|
|
|
450 |
* @param string $real Real database-layer column type (i.e. "varchar(255)")
|
|
|
451 |
* @return string Abstract column type (i.e. "string")
|
|
|
452 |
*/
|
|
|
453 |
function column($real) {
|
|
|
454 |
if (is_array($real)) {
|
|
|
455 |
$col = $real['name'];
|
|
|
456 |
|
|
|
457 |
if (isset($real['limit'])) {
|
|
|
458 |
$col .= '(' . $real['limit'] . ')';
|
|
|
459 |
}
|
|
|
460 |
return $col;
|
|
|
461 |
}
|
|
|
462 |
$col = str_replace(')', '', $real);
|
|
|
463 |
$limit = null;
|
|
|
464 |
if (strpos($col, '(') !== false) {
|
|
|
465 |
list($col, $limit) = explode('(', $col);
|
|
|
466 |
}
|
|
|
467 |
|
|
|
468 |
if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) {
|
|
|
469 |
return $col;
|
|
|
470 |
}
|
|
|
471 |
if ($col == 'bit') {
|
|
|
472 |
return 'boolean';
|
|
|
473 |
}
|
|
|
474 |
if (strpos($col, 'int') !== false) {
|
|
|
475 |
return 'integer';
|
|
|
476 |
}
|
|
|
477 |
if (strpos($col, 'char') !== false) {
|
|
|
478 |
return 'string';
|
|
|
479 |
}
|
|
|
480 |
if (strpos($col, 'text') !== false) {
|
|
|
481 |
return 'text';
|
|
|
482 |
}
|
|
|
483 |
if (strpos($col, 'binary') !== false || $col == 'image') {
|
|
|
484 |
return 'binary';
|
|
|
485 |
}
|
|
|
486 |
if (in_array($col, array('float', 'real', 'decimal', 'numeric'))) {
|
|
|
487 |
return 'float';
|
|
|
488 |
}
|
|
|
489 |
return 'text';
|
|
|
490 |
}
|
|
|
491 |
/**
|
|
|
492 |
* Enter description here...
|
|
|
493 |
*
|
|
|
494 |
* @param unknown_type $results
|
|
|
495 |
*/
|
|
|
496 |
function resultSet(&$results) {
|
|
|
497 |
$this->results =& $results;
|
|
|
498 |
$this->map = array();
|
|
|
499 |
$numFields = mssql_num_fields($results);
|
|
|
500 |
$index = 0;
|
|
|
501 |
$j = 0;
|
|
|
502 |
|
|
|
503 |
while ($j < $numFields) {
|
|
|
504 |
$column = mssql_field_name($results, $j);
|
|
|
505 |
|
|
|
506 |
if (strpos($column, '__')) {
|
|
|
507 |
if (isset($this->__fieldMappings[$column]) && strpos($this->__fieldMappings[$column], '.')) {
|
|
|
508 |
$map = explode('.', $this->__fieldMappings[$column]);
|
|
|
509 |
} elseif (isset($this->__fieldMappings[$column])) {
|
|
|
510 |
$map = array(0, $this->__fieldMappings[$column]);
|
|
|
511 |
} else {
|
|
|
512 |
$map = array(0, $column);
|
|
|
513 |
}
|
|
|
514 |
$this->map[$index++] = $map;
|
|
|
515 |
} else {
|
|
|
516 |
$this->map[$index++] = array(0, $column);
|
|
|
517 |
}
|
|
|
518 |
$j++;
|
|
|
519 |
}
|
|
|
520 |
}
|
|
|
521 |
/**
|
|
|
522 |
* Builds final SQL statement
|
|
|
523 |
*
|
|
|
524 |
* @param string $type Query type
|
|
|
525 |
* @param array $data Query data
|
|
|
526 |
* @return string
|
|
|
527 |
*/
|
|
|
528 |
function renderStatement($type, $data) {
|
|
|
529 |
switch (strtolower($type)) {
|
|
|
530 |
case 'select':
|
|
|
531 |
extract($data);
|
|
|
532 |
$fields = trim($fields);
|
|
|
533 |
|
|
|
534 |
if (strpos($limit, 'TOP') !== false && strpos($fields, 'DISTINCT ') === 0) {
|
|
|
535 |
$limit = 'DISTINCT ' . trim($limit);
|
|
|
536 |
$fields = substr($fields, 9);
|
|
|
537 |
}
|
|
|
538 |
|
|
|
539 |
if (preg_match('/offset\s+([0-9]+)/i', $limit, $offset)) {
|
|
|
540 |
$limit = preg_replace('/\s*offset.*$/i', '', $limit);
|
|
|
541 |
preg_match('/top\s+([0-9]+)/i', $limit, $limitVal);
|
|
|
542 |
$offset = intval($offset[1]) + intval($limitVal[1]);
|
|
|
543 |
$rOrder = $this->__switchSort($order);
|
|
|
544 |
list($order2, $rOrder) = array($this->__mapFields($order), $this->__mapFields($rOrder));
|
|
|
545 |
return "SELECT * FROM (SELECT {$limit} * FROM (SELECT TOP {$offset} {$fields} FROM {$table} {$alias} {$joins} {$conditions} {$group} {$order}) AS Set1 {$rOrder}) AS Set2 {$order2}";
|
|
|
546 |
} else {
|
|
|
547 |
return "SELECT {$limit} {$fields} FROM {$table} {$alias} {$joins} {$conditions} {$group} {$order}";
|
|
|
548 |
}
|
|
|
549 |
break;
|
|
|
550 |
case "schema":
|
|
|
551 |
extract($data);
|
|
|
552 |
|
|
|
553 |
foreach ($indexes as $i => $index) {
|
|
|
554 |
if (preg_match('/PRIMARY KEY/', $index)) {
|
|
|
555 |
unset($indexes[$i]);
|
|
|
556 |
break;
|
|
|
557 |
}
|
|
|
558 |
}
|
|
|
559 |
|
|
|
560 |
foreach (array('columns', 'indexes') as $var) {
|
|
|
561 |
if (is_array(${$var})) {
|
|
|
562 |
${$var} = "\t" . join(",\n\t", array_filter(${$var}));
|
|
|
563 |
}
|
|
|
564 |
}
|
|
|
565 |
return "CREATE TABLE {$table} (\n{$columns});\n{$indexes}";
|
|
|
566 |
break;
|
|
|
567 |
default:
|
|
|
568 |
return parent::renderStatement($type, $data);
|
|
|
569 |
break;
|
|
|
570 |
}
|
|
|
571 |
}
|
|
|
572 |
/**
|
|
|
573 |
* Reverses the sort direction of ORDER statements to get paging offsets to work correctly
|
|
|
574 |
*
|
|
|
575 |
* @param string $order
|
|
|
576 |
* @return string
|
|
|
577 |
* @access private
|
|
|
578 |
*/
|
|
|
579 |
function __switchSort($order) {
|
|
|
580 |
$order = preg_replace('/\s+ASC/i', '__tmp_asc__', $order);
|
|
|
581 |
$order = preg_replace('/\s+DESC/i', ' ASC', $order);
|
|
|
582 |
return preg_replace('/__tmp_asc__/', ' DESC', $order);
|
|
|
583 |
}
|
|
|
584 |
/**
|
|
|
585 |
* Translates field names used for filtering and sorting to shortened names using the field map
|
|
|
586 |
*
|
|
|
587 |
* @param string $sql A snippet of SQL representing an ORDER or WHERE statement
|
|
|
588 |
* @return string The value of $sql with field names replaced
|
|
|
589 |
* @access private
|
|
|
590 |
*/
|
|
|
591 |
function __mapFields($sql) {
|
|
|
592 |
if (empty($sql) || empty($this->__fieldMappings)) {
|
|
|
593 |
return $sql;
|
|
|
594 |
}
|
|
|
595 |
foreach ($this->__fieldMappings as $key => $val) {
|
|
|
596 |
$sql = preg_replace('/' . preg_quote($val) . '/', $this->name($key), $sql);
|
|
|
597 |
$sql = preg_replace('/' . preg_quote($this->name($val)) . '/', $this->name($key), $sql);
|
|
|
598 |
}
|
|
|
599 |
return $sql;
|
|
|
600 |
}
|
|
|
601 |
/**
|
|
|
602 |
* Returns an array of all result rows for a given SQL query.
|
|
|
603 |
* Returns false if no rows matched.
|
|
|
604 |
*
|
|
|
605 |
* @param string $sql SQL statement
|
|
|
606 |
* @param boolean $cache Enables returning/storing cached query results
|
|
|
607 |
* @return array Array of resultset rows, or false if no rows matched
|
|
|
608 |
*/
|
|
|
609 |
function read(&$model, $queryData = array(), $recursive = null) {
|
|
|
610 |
$results = parent::read($model, $queryData, $recursive);
|
|
|
611 |
$this->__fieldMappings = array();
|
|
|
612 |
return $results;
|
|
|
613 |
}
|
|
|
614 |
/**
|
|
|
615 |
* Fetches the next row from the current result set
|
|
|
616 |
*
|
|
|
617 |
* @return unknown
|
|
|
618 |
*/
|
|
|
619 |
function fetchResult() {
|
|
|
620 |
if ($row = mssql_fetch_row($this->results)) {
|
|
|
621 |
$resultRow = array();
|
|
|
622 |
$i = 0;
|
|
|
623 |
|
|
|
624 |
foreach ($row as $index => $field) {
|
|
|
625 |
list($table, $column) = $this->map[$index];
|
|
|
626 |
$resultRow[$table][$column] = $row[$index];
|
|
|
627 |
$i++;
|
|
|
628 |
}
|
|
|
629 |
return $resultRow;
|
|
|
630 |
} else {
|
|
|
631 |
return false;
|
|
|
632 |
}
|
|
|
633 |
}
|
|
|
634 |
/**
|
|
|
635 |
* Generate a database-native column schema string
|
|
|
636 |
*
|
|
|
637 |
* @param array $column An array structured like the following: array('name'=>'value', 'type'=>'value'[, options]),
|
|
|
638 |
* where options can be 'default', 'length', or 'key'.
|
|
|
639 |
* @return string
|
|
|
640 |
*/
|
|
|
641 |
function buildColumn($column) {
|
|
|
642 |
$result = preg_replace('/(int|integer)\([0-9]+\)/i', '$1', parent::buildColumn($column));
|
|
|
643 |
$null = (
|
|
|
644 |
(isset($column['null']) && $column['null'] == true) ||
|
|
|
645 |
(array_key_exists('default', $column) && $column['default'] === null) ||
|
|
|
646 |
(array_keys($column) == array('type', 'name'))
|
|
|
647 |
);
|
|
|
648 |
$primaryKey = (isset($column['key']) && $column['key'] == 'primary');
|
|
|
649 |
$stringKey = ($primaryKey && $column['type'] != 'integer');
|
|
|
650 |
|
|
|
651 |
if ($null && !$primaryKey) {
|
|
|
652 |
$result .= " NULL";
|
|
|
653 |
}
|
|
|
654 |
return $result;
|
|
|
655 |
}
|
|
|
656 |
/**
|
|
|
657 |
* Format indexes for create table
|
|
|
658 |
*
|
|
|
659 |
* @param array $indexes
|
|
|
660 |
* @param string $table
|
|
|
661 |
* @return string
|
|
|
662 |
*/
|
|
|
663 |
function buildIndex($indexes, $table = null) {
|
|
|
664 |
$join = array();
|
|
|
665 |
|
|
|
666 |
foreach ($indexes as $name => $value) {
|
|
|
667 |
if ($name == 'PRIMARY') {
|
|
|
668 |
$out = 'PRIMARY KEY (' . $this->name($value['column']) . ')';
|
|
|
669 |
} else {
|
|
|
670 |
$out = "ALTER TABLE {$table} ADD CONSTRAINT {$name} UNIQUE";
|
|
|
671 |
|
|
|
672 |
if (is_array($value['column'])) {
|
|
|
673 |
$value['column'] = join(', ', array_map(array(&$this, 'name'), $value['column']));
|
|
|
674 |
} else {
|
|
|
675 |
$value['column'] = $this->name($value['column']);
|
|
|
676 |
}
|
|
|
677 |
$out .= "({$value['column']});";
|
|
|
678 |
}
|
|
|
679 |
$join[] = $out;
|
|
|
680 |
}
|
|
|
681 |
return $join;
|
|
|
682 |
}
|
|
|
683 |
}
|
|
|
684 |
?>
|