Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 1
<?php
2
/**
3
 * MS SQL Server layer for DBO
4
 *
5
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://cakephp.org CakePHP(tm) Project
14
 * @package       Cake.Model.Datasource.Database
15
 * @since         CakePHP(tm) v 0.10.5.1790
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('DboSource', 'Model/Datasource');
20
 
21
/**
22
 * Dbo layer for Microsoft's official SQLServer driver
23
 *
24
 * A Dbo layer for MS SQL Server 2005 and higher. Requires the
25
 * `pdo_sqlsrv` extension to be enabled.
26
 *
27
 * @link http://www.php.net/manual/en/ref.pdo-sqlsrv.php
28
 *
29
 * @package       Cake.Model.Datasource.Database
30
 */
31
class Sqlserver extends DboSource {
32
 
33
/**
34
 * Driver description
35
 *
36
 * @var string
37
 */
38
	public $description = "SQL Server DBO Driver";
39
 
40
/**
41
 * Starting quote character for quoted identifiers
42
 *
43
 * @var string
44
 */
45
	public $startQuote = "[";
46
 
47
/**
48
 * Ending quote character for quoted identifiers
49
 *
50
 * @var string
51
 */
52
	public $endQuote = "]";
53
 
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
	protected $_fieldMappings = array();
61
 
62
/**
63
 * Storing the last affected value
64
 *
65
 * @var mixed
66
 */
67
	protected $_lastAffected = false;
68
 
69
/**
70
 * Base configuration settings for MS SQL driver
71
 *
72
 * @var array
73
 */
74
	protected $_baseConfig = array(
75
		'persistent' => true,
76
		'host' => 'localhost\SQLEXPRESS',
77
		'login' => '',
78
		'password' => '',
79
		'database' => 'cake',
80
		'schema' => '',
81
		'flags' => array()
82
	);
83
 
84
/**
85
 * MS SQL column definition
86
 *
87
 * @var array
88
 */
89
	public $columns = array(
90
		'primary_key' => array('name' => 'IDENTITY (1, 1) NOT NULL'),
91
		'string' => array('name' => 'nvarchar', 'limit' => '255'),
92
		'text' => array('name' => 'nvarchar', 'limit' => 'MAX'),
93
		'integer' => array('name' => 'int', 'formatter' => 'intval'),
94
		'biginteger' => array('name' => 'bigint'),
95
		'numeric' => array('name' => 'decimal', 'formatter' => 'floatval'),
96
		'decimal' => array('name' => 'decimal', 'formatter' => 'floatval'),
97
		'float' => array('name' => 'float', 'formatter' => 'floatval'),
98
		'real' => array('name' => 'float', 'formatter' => 'floatval'),
99
		'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
100
		'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
101
		'time' => array('name' => 'datetime', 'format' => 'H:i:s', 'formatter' => 'date'),
102
		'date' => array('name' => 'datetime', 'format' => 'Y-m-d', 'formatter' => 'date'),
103
		'binary' => array('name' => 'varbinary'),
104
		'boolean' => array('name' => 'bit')
105
	);
106
 
107
/**
108
 * Magic column name used to provide pagination support for SQLServer 2008
109
 * which lacks proper limit/offset support.
110
 *
111
 * @var string
112
 */
113
	const ROW_COUNTER = '_cake_page_rownum_';
114
 
115
/**
116
 * Connects to the database using options in the given configuration array.
117
 *
118
 * @return bool True if the database could be connected, else false
119
 * @throws MissingConnectionException
120
 */
121
	public function connect() {
122
		$config = $this->config;
123
		$this->connected = false;
124
 
125
		$flags = $config['flags'] + array(
126
			PDO::ATTR_PERSISTENT => $config['persistent'],
127
			PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
128
		);
129
 
130
		if (!empty($config['encoding'])) {
131
			$flags[PDO::SQLSRV_ATTR_ENCODING] = $config['encoding'];
132
		}
133
 
134
		try {
135
			$this->_connection = new PDO(
136
				"sqlsrv:server={$config['host']};Database={$config['database']}",
137
				$config['login'],
138
				$config['password'],
139
				$flags
140
			);
141
			$this->connected = true;
142
			if (!empty($config['settings'])) {
143
				foreach ($config['settings'] as $key => $value) {
144
					$this->_execute("SET $key $value");
145
				}
146
			}
147
		} catch (PDOException $e) {
148
			throw new MissingConnectionException(array(
149
				'class' => get_class($this),
150
				'message' => $e->getMessage()
151
			));
152
		}
153
 
154
		return $this->connected;
155
	}
156
 
157
/**
158
 * Check that PDO SQL Server is installed/loaded
159
 *
160
 * @return bool
161
 */
162
	public function enabled() {
163
		return in_array('sqlsrv', PDO::getAvailableDrivers());
164
	}
165
 
166
/**
167
 * Returns an array of sources (tables) in the database.
168
 *
169
 * @param mixed $data The names
170
 * @return array Array of table names in the database
171
 */
172
	public function listSources($data = null) {
173
		$cache = parent::listSources();
174
		if ($cache !== null) {
175
			return $cache;
176
		}
177
		$result = $this->_execute("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES");
178
 
179
		if (!$result) {
180
			$result->closeCursor();
181
			return array();
182
		}
183
		$tables = array();
184
 
185
		while ($line = $result->fetch(PDO::FETCH_NUM)) {
186
			$tables[] = $line[0];
187
		}
188
 
189
		$result->closeCursor();
190
		parent::listSources($tables);
191
		return $tables;
192
	}
193
 
194
/**
195
 * Returns an array of the fields in given table name.
196
 *
197
 * @param Model|string $model Model object to describe, or a string table name.
198
 * @return array Fields in table. Keys are name and type
199
 * @throws CakeException
200
 */
201
	public function describe($model) {
202
		$table = $this->fullTableName($model, false, false);
203
		$fulltable = $this->fullTableName($model, false, true);
204
 
205
		$cache = parent::describe($fulltable);
206
		if ($cache) {
207
			return $cache;
208
		}
209
 
210
		$fields = array();
211
		$schema = is_object($model) ? $model->schemaName : false;
212
 
213
		$cols = $this->_execute(
214
			"SELECT
215
				COLUMN_NAME as Field,
216
				DATA_TYPE as Type,
217
				COL_LENGTH('" . ($schema ? $fulltable : $table) . "', COLUMN_NAME) as Length,
218
				IS_NULLABLE As [Null],
219
				COLUMN_DEFAULT as [Default],
220
				COLUMNPROPERTY(OBJECT_ID('" . ($schema ? $fulltable : $table) . "'), COLUMN_NAME, 'IsIdentity') as [Key],
221
				NUMERIC_SCALE as Size
222
			FROM INFORMATION_SCHEMA.COLUMNS
223
			WHERE TABLE_NAME = '" . $table . "'" . ($schema ? " AND TABLE_SCHEMA = '" . $schema . "'" : '')
224
		);
225
 
226
		if (!$cols) {
227
			throw new CakeException(__d('cake_dev', 'Could not describe table for %s', $table));
228
		}
229
 
230
		while ($column = $cols->fetch(PDO::FETCH_OBJ)) {
231
			$field = $column->Field;
232
			$fields[$field] = array(
233
				'type' => $this->column($column),
234
				'null' => ($column->Null === 'YES' ? true : false),
235
				'default' => $column->Default,
236
				'length' => $this->length($column),
237
				'key' => ($column->Key == '1') ? 'primary' : false
238
			);
239
 
240
			if ($fields[$field]['default'] === 'null') {
241
				$fields[$field]['default'] = null;
242
			}
243
			if ($fields[$field]['default'] !== null) {
244
				$fields[$field]['default'] = preg_replace(
245
					"/^[(]{1,2}'?([^')]*)?'?[)]{1,2}$/",
246
					"$1",
247
					$fields[$field]['default']
248
				);
249
				$this->value($fields[$field]['default'], $fields[$field]['type']);
250
			}
251
 
252
			if ($fields[$field]['key'] !== false && $fields[$field]['type'] === 'integer') {
253
				$fields[$field]['length'] = 11;
254
			} elseif ($fields[$field]['key'] === false) {
255
				unset($fields[$field]['key']);
256
			}
257
			if (in_array($fields[$field]['type'], array('date', 'time', 'datetime', 'timestamp'))) {
258
				$fields[$field]['length'] = null;
259
			}
260
			if ($fields[$field]['type'] === 'float' && !empty($column->Size)) {
261
				$fields[$field]['length'] = $fields[$field]['length'] . ',' . $column->Size;
262
			}
263
		}
264
		$this->_cacheDescription($table, $fields);
265
		$cols->closeCursor();
266
		return $fields;
267
	}
268
 
269
/**
270
 * Generates the fields list of an SQL query.
271
 *
272
 * @param Model $model The model to get fields for.
273
 * @param string $alias Alias table name
274
 * @param array $fields The fields so far.
275
 * @param bool $quote Whether or not to quote identfiers.
276
 * @return array
277
 */
278
	public function fields(Model $model, $alias = null, $fields = array(), $quote = true) {
279
		if (empty($alias)) {
280
			$alias = $model->alias;
281
		}
282
		$fields = parent::fields($model, $alias, $fields, false);
283
		$count = count($fields);
284
 
285
		if ($count >= 1 && strpos($fields[0], 'COUNT(*)') === false) {
286
			$result = array();
287
			for ($i = 0; $i < $count; $i++) {
288
				$prepend = '';
289
 
290
				if (strpos($fields[$i], 'DISTINCT') !== false && strpos($fields[$i], 'COUNT') === false) {
291
					$prepend = 'DISTINCT ';
292
					$fields[$i] = trim(str_replace('DISTINCT', '', $fields[$i]));
293
				}
294
 
295
				if (!preg_match('/\s+AS\s+/i', $fields[$i])) {
296
					if (substr($fields[$i], -1) === '*') {
297
						if (strpos($fields[$i], '.') !== false && $fields[$i] != $alias . '.*') {
298
							$build = explode('.', $fields[$i]);
299
							$AssociatedModel = $model->{$build[0]};
300
						} else {
301
							$AssociatedModel = $model;
302
						}
303
 
304
						$_fields = $this->fields($AssociatedModel, $AssociatedModel->alias, array_keys($AssociatedModel->schema()));
305
						$result = array_merge($result, $_fields);
306
						continue;
307
					}
308
 
309
					if (strpos($fields[$i], '.') === false) {
310
						$this->_fieldMappings[$alias . '__' . $fields[$i]] = $alias . '.' . $fields[$i];
311
						$fieldName = $this->name($alias . '.' . $fields[$i]);
312
						$fieldAlias = $this->name($alias . '__' . $fields[$i]);
313
					} else {
314
						$build = explode('.', $fields[$i]);
315
						$build[0] = trim($build[0], '[]');
316
						$build[1] = trim($build[1], '[]');
317
						$name = $build[0] . '.' . $build[1];
318
						$alias = $build[0] . '__' . $build[1];
319
 
320
						$this->_fieldMappings[$alias] = $name;
321
						$fieldName = $this->name($name);
322
						$fieldAlias = $this->name($alias);
323
					}
324
					if ($model->getColumnType($fields[$i]) === 'datetime') {
325
						$fieldName = "CONVERT(VARCHAR(20), {$fieldName}, 20)";
326
					}
327
					$fields[$i] = "{$fieldName} AS {$fieldAlias}";
328
				}
329
				$result[] = $prepend . $fields[$i];
330
			}
331
			return $result;
332
		}
333
		return $fields;
334
	}
335
 
336
/**
337
 * Generates and executes an SQL INSERT statement for given model, fields, and values.
338
 * Removes Identity (primary key) column from update data before returning to parent, if
339
 * value is empty.
340
 *
341
 * @param Model $model The model to insert into.
342
 * @param array $fields The fields to set.
343
 * @param array $values The values to set.
344
 * @return array
345
 */
346
	public function create(Model $model, $fields = null, $values = null) {
347
		if (!empty($values)) {
348
			$fields = array_combine($fields, $values);
349
		}
350
		$primaryKey = $this->_getPrimaryKey($model);
351
 
352
		if (array_key_exists($primaryKey, $fields)) {
353
			if (empty($fields[$primaryKey])) {
354
				unset($fields[$primaryKey]);
355
			} else {
356
				$this->_execute('SET IDENTITY_INSERT ' . $this->fullTableName($model) . ' ON');
357
			}
358
		}
359
		$result = parent::create($model, array_keys($fields), array_values($fields));
360
		if (array_key_exists($primaryKey, $fields) && !empty($fields[$primaryKey])) {
361
			$this->_execute('SET IDENTITY_INSERT ' . $this->fullTableName($model) . ' OFF');
362
		}
363
		return $result;
364
	}
365
 
366
/**
367
 * Generates and executes an SQL UPDATE statement for given model, fields, and values.
368
 * Removes Identity (primary key) column from update data before returning to parent.
369
 *
370
 * @param Model $model The model to update.
371
 * @param array $fields The fields to set.
372
 * @param array $values The values to set.
373
 * @param mixed $conditions The conditions to use.
374
 * @return array
375
 */
376
	public function update(Model $model, $fields = array(), $values = null, $conditions = null) {
377
		if (!empty($values)) {
378
			$fields = array_combine($fields, $values);
379
		}
380
		if (isset($fields[$model->primaryKey])) {
381
			unset($fields[$model->primaryKey]);
382
		}
383
		if (empty($fields)) {
384
			return true;
385
		}
386
		return parent::update($model, array_keys($fields), array_values($fields), $conditions);
387
	}
388
 
389
/**
390
 * Returns a limit statement in the correct format for the particular database.
391
 *
392
 * @param int $limit Limit of results returned
393
 * @param int $offset Offset from which to start results
394
 * @return string SQL limit/offset statement
395
 */
396
	public function limit($limit, $offset = null) {
397
		if ($limit) {
398
			$rt = '';
399
			if (!strpos(strtolower($limit), 'top') || strpos(strtolower($limit), 'top') === 0) {
400
				$rt = ' TOP';
401
			}
402
			$rt .= sprintf(' %u', $limit);
403
			if (is_int($offset) && $offset > 0) {
404
				$rt = sprintf(' OFFSET %u ROWS FETCH FIRST %u ROWS ONLY', $offset, $limit);
405
			}
406
			return $rt;
407
		}
408
		return null;
409
	}
410
 
411
/**
412
 * Converts database-layer column types to basic types
413
 *
414
 * @param mixed $real Either the string value of the fields type.
415
 *    or the Result object from Sqlserver::describe()
416
 * @return string Abstract column type (i.e. "string")
417
 */
418
	public function column($real) {
419
		$limit = null;
420
		$col = $real;
421
		if (is_object($real) && isset($real->Field)) {
422
			$limit = $real->Length;
423
			$col = $real->Type;
424
		}
425
 
426
		if ($col === 'datetime2') {
427
			return 'datetime';
428
		}
429
		if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) {
430
			return $col;
431
		}
432
		if ($col === 'bit') {
433
			return 'boolean';
434
		}
435
		if (strpos($col, 'bigint') !== false) {
436
			return 'biginteger';
437
		}
438
		if (strpos($col, 'int') !== false) {
439
			return 'integer';
440
		}
441
		if (strpos($col, 'char') !== false && $limit == -1) {
442
			return 'text';
443
		}
444
		if (strpos($col, 'char') !== false) {
445
			return 'string';
446
		}
447
		if (strpos($col, 'text') !== false) {
448
			return 'text';
449
		}
450
		if (strpos($col, 'binary') !== false || $col === 'image') {
451
			return 'binary';
452
		}
453
		if (in_array($col, array('float', 'real'))) {
454
			return 'float';
455
		}
456
		if (in_array($col, array('decimal', 'numeric'))) {
457
			return 'decimal';
458
		}
459
		return 'text';
460
	}
461
 
462
/**
463
 * Handle SQLServer specific length properties.
464
 * SQLServer handles text types as nvarchar/varchar with a length of -1.
465
 *
466
 * @param mixed $length Either the length as a string, or a Column descriptor object.
467
 * @return mixed null|integer with length of column.
468
 */
469
	public function length($length) {
470
		if (is_object($length) && isset($length->Length)) {
471
			if ($length->Length == -1 && strpos($length->Type, 'char') !== false) {
472
				return null;
473
			}
474
			if (in_array($length->Type, array('nchar', 'nvarchar'))) {
475
				return floor($length->Length / 2);
476
			}
477
			if ($length->Type === 'text') {
478
				return null;
479
			}
480
			return $length->Length;
481
		}
482
		return parent::length($length);
483
	}
484
 
485
/**
486
 * Builds a map of the columns contained in a result
487
 *
488
 * @param PDOStatement $results The result to modify.
489
 * @return void
490
 */
491
	public function resultSet($results) {
492
		$this->map = array();
493
		$numFields = $results->columnCount();
494
		$index = 0;
495
 
496
		while ($numFields-- > 0) {
497
			$column = $results->getColumnMeta($index);
498
			$name = $column['name'];
499
 
500
			if (strpos($name, '__')) {
501
				if (isset($this->_fieldMappings[$name]) && strpos($this->_fieldMappings[$name], '.')) {
502
					$map = explode('.', $this->_fieldMappings[$name]);
503
				} elseif (isset($this->_fieldMappings[$name])) {
504
					$map = array(0, $this->_fieldMappings[$name]);
505
				} else {
506
					$map = array(0, $name);
507
				}
508
			} else {
509
				$map = array(0, $name);
510
			}
511
			$map[] = ($column['sqlsrv:decl_type'] === 'bit') ? 'boolean' : $column['native_type'];
512
			$this->map[$index++] = $map;
513
		}
514
	}
515
 
516
/**
517
 * Builds final SQL statement
518
 *
519
 * @param string $type Query type
520
 * @param array $data Query data
521
 * @return string
522
 */
523
	public function renderStatement($type, $data) {
524
		switch (strtolower($type)) {
525
			case 'select':
526
				extract($data);
527
				$fields = trim($fields);
528
 
529
				if (strpos($limit, 'TOP') !== false && strpos($fields, 'DISTINCT ') === 0) {
530
					$limit = 'DISTINCT ' . trim($limit);
531
					$fields = substr($fields, 9);
532
				}
533
 
534
				// hack order as SQLServer requires an order if there is a limit.
535
				if ($limit && !$order) {
536
					$order = 'ORDER BY (SELECT NULL)';
537
				}
538
 
539
				// For older versions use the subquery version of pagination.
540
				if (version_compare($this->getVersion(), '11', '<') && preg_match('/FETCH\sFIRST\s+([0-9]+)/i', $limit, $offset)) {
541
					preg_match('/OFFSET\s*(\d+)\s*.*?(\d+)\s*ROWS/', $limit, $limitOffset);
542
 
543
					$limit = 'TOP ' . (int)$limitOffset[2];
544
					$page = (int)($limitOffset[1] / $limitOffset[2]);
545
					$offset = (int)($limitOffset[2] * $page);
546
 
547
					$rowCounter = self::ROW_COUNTER;
548
					$sql = "SELECT {$limit} * FROM (
549
							SELECT {$fields}, ROW_NUMBER() OVER ({$order}) AS {$rowCounter}
550
							FROM {$table} {$alias} {$joins} {$conditions} {$group}
551
						) AS _cake_paging_
552
						WHERE _cake_paging_.{$rowCounter} > {$offset}
553
						ORDER BY _cake_paging_.{$rowCounter}
554
					";
555
					return trim($sql);
556
				}
557
				if (strpos($limit, 'FETCH') !== false) {
558
					return trim("SELECT {$fields} FROM {$table} {$alias} {$joins} {$conditions} {$group} {$order} {$limit}");
559
				}
560
				return trim("SELECT {$limit} {$fields} FROM {$table} {$alias} {$joins} {$conditions} {$group} {$order}");
561
			case "schema":
562
				extract($data);
563
 
564
				foreach ($indexes as $i => $index) {
565
					if (preg_match('/PRIMARY KEY/', $index)) {
566
						unset($indexes[$i]);
567
						break;
568
					}
569
				}
570
 
571
				foreach (array('columns', 'indexes') as $var) {
572
					if (is_array(${$var})) {
573
						${$var} = "\t" . implode(",\n\t", array_filter(${$var}));
574
					}
575
				}
576
				return trim("CREATE TABLE {$table} (\n{$columns});\n{$indexes}");
577
			default:
578
				return parent::renderStatement($type, $data);
579
		}
580
	}
581
 
582
/**
583
 * Returns a quoted and escaped string of $data for use in an SQL statement.
584
 *
585
 * @param string $data String to be prepared for use in an SQL statement
586
 * @param string $column The column into which this data will be inserted
587
 * @return string Quoted and escaped data
588
 */
589
	public function value($data, $column = null) {
590
		if ($data === null || is_array($data) || is_object($data)) {
591
			return parent::value($data, $column);
592
		}
593
		if (in_array($data, array('{$__cakeID__$}', '{$__cakeForeignKey__$}'), true)) {
594
			return $data;
595
		}
596
 
597
		if (empty($column)) {
598
			$column = $this->introspectType($data);
599
		}
600
 
601
		switch ($column) {
602
			case 'string':
603
			case 'text':
604
				return 'N' . $this->_connection->quote($data, PDO::PARAM_STR);
605
			default:
606
				return parent::value($data, $column);
607
		}
608
	}
609
 
610
/**
611
 * Returns an array of all result rows for a given SQL query.
612
 * Returns false if no rows matched.
613
 *
614
 * @param Model $model The model to read from
615
 * @param array $queryData The query data
616
 * @param int $recursive How many layers to go.
617
 * @return array|false Array of resultset rows, or false if no rows matched
618
 */
619
	public function read(Model $model, $queryData = array(), $recursive = null) {
620
		$results = parent::read($model, $queryData, $recursive);
621
		$this->_fieldMappings = array();
622
		return $results;
623
	}
624
 
625
/**
626
 * Fetches the next row from the current result set.
627
 * Eats the magic ROW_COUNTER variable.
628
 *
629
 * @return mixed
630
 */
631
	public function fetchResult() {
632
		if ($row = $this->_result->fetch(PDO::FETCH_NUM)) {
633
			$resultRow = array();
634
			foreach ($this->map as $col => $meta) {
635
				list($table, $column, $type) = $meta;
636
				if ($table === 0 && $column === self::ROW_COUNTER) {
637
					continue;
638
				}
639
				$resultRow[$table][$column] = $row[$col];
640
				if ($type === 'boolean' && $row[$col] !== null) {
641
					$resultRow[$table][$column] = $this->boolean($resultRow[$table][$column]);
642
				}
643
			}
644
			return $resultRow;
645
		}
646
		$this->_result->closeCursor();
647
		return false;
648
	}
649
 
650
/**
651
 * Inserts multiple values into a table
652
 *
653
 * @param string $table The table to insert into.
654
 * @param string $fields The fields to set.
655
 * @param array $values The values to set.
656
 * @return void
657
 */
658
	public function insertMulti($table, $fields, $values) {
659
		$primaryKey = $this->_getPrimaryKey($table);
660
		$hasPrimaryKey = $primaryKey && (
661
			(is_array($fields) && in_array($primaryKey, $fields)
662
			|| (is_string($fields) && strpos($fields, $this->startQuote . $primaryKey . $this->endQuote) !== false))
663
		);
664
 
665
		if ($hasPrimaryKey) {
666
			$this->_execute('SET IDENTITY_INSERT ' . $this->fullTableName($table) . ' ON');
667
		}
668
 
669
		parent::insertMulti($table, $fields, $values);
670
 
671
		if ($hasPrimaryKey) {
672
			$this->_execute('SET IDENTITY_INSERT ' . $this->fullTableName($table) . ' OFF');
673
		}
674
	}
675
 
676
/**
677
 * Generate a database-native column schema string
678
 *
679
 * @param array $column An array structured like the
680
 *   following: array('name'=>'value', 'type'=>'value'[, options]),
681
 *   where options can be 'default', 'length', or 'key'.
682
 * @return string
683
 */
684
	public function buildColumn($column) {
685
		$result = parent::buildColumn($column);
686
		$result = preg_replace('/(bigint|int|integer)\([0-9]+\)/i', '$1', $result);
687
		$result = preg_replace('/(bit)\([0-9]+\)/i', '$1', $result);
688
		if (strpos($result, 'DEFAULT NULL') !== false) {
689
			if (isset($column['default']) && $column['default'] === '') {
690
				$result = str_replace('DEFAULT NULL', "DEFAULT ''", $result);
691
			} else {
692
				$result = str_replace('DEFAULT NULL', 'NULL', $result);
693
			}
694
		} elseif (array_keys($column) === array('type', 'name')) {
695
			$result .= ' NULL';
696
		} elseif (strpos($result, "DEFAULT N'")) {
697
			$result = str_replace("DEFAULT N'", "DEFAULT '", $result);
698
		}
699
		return $result;
700
	}
701
 
702
/**
703
 * Format indexes for create table
704
 *
705
 * @param array $indexes The indexes to build
706
 * @param string $table The table to make indexes for.
707
 * @return string
708
 */
709
	public function buildIndex($indexes, $table = null) {
710
		$join = array();
711
 
712
		foreach ($indexes as $name => $value) {
713
			if ($name === 'PRIMARY') {
714
				$join[] = 'PRIMARY KEY (' . $this->name($value['column']) . ')';
715
			} elseif (isset($value['unique']) && $value['unique']) {
716
				$out = "ALTER TABLE {$table} ADD CONSTRAINT {$name} UNIQUE";
717
 
718
				if (is_array($value['column'])) {
719
					$value['column'] = implode(', ', array_map(array(&$this, 'name'), $value['column']));
720
				} else {
721
					$value['column'] = $this->name($value['column']);
722
				}
723
				$out .= "({$value['column']});";
724
				$join[] = $out;
725
			}
726
		}
727
		return $join;
728
	}
729
 
730
/**
731
 * Makes sure it will return the primary key
732
 *
733
 * @param Model|string $model Model instance of table name
734
 * @return string
735
 */
736
	protected function _getPrimaryKey($model) {
737
		$schema = $this->describe($model);
738
		foreach ($schema as $field => $props) {
739
			if (isset($props['key']) && $props['key'] === 'primary') {
740
				return $field;
741
			}
742
		}
743
		return null;
744
	}
745
 
746
/**
747
 * Returns number of affected rows in previous database operation. If no previous operation exists,
748
 * this returns false.
749
 *
750
 * @param mixed $source Unused
751
 * @return int Number of affected rows
752
 */
753
	public function lastAffected($source = null) {
754
		$affected = parent::lastAffected();
755
		if ($affected === null && $this->_lastAffected !== false) {
756
			return $this->_lastAffected;
757
		}
758
		return $affected;
759
	}
760
 
761
/**
762
 * Executes given SQL statement.
763
 *
764
 * @param string $sql SQL statement
765
 * @param array $params list of params to be bound to query (supported only in select)
766
 * @param array $prepareOptions Options to be used in the prepare statement
767
 * @return mixed PDOStatement if query executes with no problem, true as the result of a successful, false on error
768
 * query returning no rows, such as a CREATE statement, false otherwise
769
 * @throws PDOException
770
 */
771
	protected function _execute($sql, $params = array(), $prepareOptions = array()) {
772
		$this->_lastAffected = false;
773
		$sql = trim($sql);
774
		if (strncasecmp($sql, 'SELECT', 6) === 0 || preg_match('/^EXEC(?:UTE)?\s/mi', $sql) > 0) {
775
			$prepareOptions += array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL);
776
			return parent::_execute($sql, $params, $prepareOptions);
777
		}
778
		try {
779
			$this->_lastAffected = $this->_connection->exec($sql);
780
			if ($this->_lastAffected === false) {
781
				$this->_results = null;
782
				$error = $this->_connection->errorInfo();
783
				$this->error = $error[2];
784
				return false;
785
			}
786
			return true;
787
		} catch (PDOException $e) {
788
			if (isset($query->queryString)) {
789
				$e->queryString = $query->queryString;
790
			} else {
791
				$e->queryString = $sql;
792
			}
793
			throw $e;
794
		}
795
	}
796
 
797
/**
798
 * Generate a "drop table" statement for the given table
799
 *
800
 * @param type $table Name of the table to drop
801
 * @return string Drop table SQL statement
802
 */
803
	protected function _dropTable($table) {
804
		return "IF OBJECT_ID('" . $this->fullTableName($table, false) . "', 'U') IS NOT NULL DROP TABLE " . $this->fullTableName($table) . ";";
805
	}
806
 
807
/**
808
 * Gets the schema name
809
 *
810
 * @return string The schema name
811
 */
812
	public function getSchemaName() {
813
		return $this->config['schema'];
814
	}
815
 
816
}