-
Notifications
You must be signed in to change notification settings - Fork 1
/
SqlSrv.php
486 lines (443 loc) · 15.1 KB
/
SqlSrv.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2010, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/
namespace li3_sqlsrv\extensions\adapter\data\source\database;
use lithium\data\model\QueryException;
/**
* Extends the `Database` class to implement the necessary SQL-formatting and resultset-fetching
* features for working with SQL Server.
*
* For more information on configuring the database connection, see the `__construct()` method.
*
* @see li3_sqlsrv\data\source\SqlSrv::__construct()
*/
class SqlSrv extends \lithium\data\source\Database {
protected $_classes = array(
'entity' => 'lithium\data\entity\Record',
'set' => 'lithium\data\collection\RecordSet',
'relationship' => 'lithium\data\model\Relationship',
'result' => 'li3_sqlsrv\extensions\adapter\data\source\database\sql_srv\Result'
);
/**
* SqlSrv column type definitions.
*
* @var array
*/
protected $_columns = array(
'primary_key' => array('name' => 'IDENTITY (1, 1) NOT NULL'),
'string' => array('name' => 'varchar', 'length' => '255'),
'text' => array('name' => 'varchar', 'length' => 'max'),
'integer' => array('name' => 'integer', 'length' => 11, 'formatter' => 'intval'),
'float' => array('name' => 'float', 'formatter' => 'floatval'),
'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s.u', 'formatter' => 'date'),
'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
'time' => array('name' => 'datetime', 'format' => 'H:i:s', 'formatter' => 'date'),
'date' => array('name' => 'datetime', 'format' => 'Y-m-d', 'formatter' => 'date'),
'binary' => array('name' => 'varbinary', 'length' => 'max'),
'boolean' => array('name' => 'bit')
);
/**
* Strings used to render the given statement
*
* @see lithium\data\source\Database::renderCommand()
* @var array
*/
protected $_strings = array(
'read' => "SELECT {:limit} {:fields} From {:table}
{:joins} {:conditions} {:group} {:order} {:comment}",
'paged' => "
SELECT * From (
SELECT {:fields}, ROW_NUMBER() OVER({:order}) AS [__LI3_ROW_NUMBER__]
From {:table} {:joins} {:conditions} {:group}
) a {:limit} {:comment}",
'create' => "INSERT INTO {:table} ({:fields}) VALUES ({:values}) {:comment}",
'update' => "UPDATE {:table} SET {:fields} {:conditions} {:comment}",
'delete' => "DELETE {:flags} From {:table} {:aliases} {:conditions} {:comment}",
'schema' => "CREATE TABLE {:table} (\n{:columns}\n) {:indexes} {:comment}",
'join' => "{:type} JOIN {:table} {:constraint}"
);
/**
* Pair of opening and closing quote characters used for quoting identifiers in queries.
*
* @var array
*/
protected $_quotes = array('[', ']');
/**
* SQL Server-specific value denoting whether or not table aliases should be used in DELETE and
* UPDATE queries.
*
* @var boolean
*/
protected $_useAlias = true;
/**
* Constructs the SQL Server adapter and sets the default port to 1433.
*
* @see lithium\data\source\Database::__construct()
* @see lithium\data\Source::__construct()
* @see lithium\data\Connections::add()
* @param array $config Configuration options for this class. For additional configuration,
* see `lithium\data\source\Database` and `lithium\data\Source`. Available options
* defined by this class:
* - `'database'`: The name of the database to connect to. Defaults to 'lithium'.
* - `'host'`: The IP or machine name where SQL Server is running, followed by a colon,
* followed by a port number or socket. Defaults to `(local), 1433`.
* - `'persistent'`: If a persistent connection (connection pooling) should be made.
* Defaults to true.
* - `encoding`: One of `SQLSRV_ENC_CHAR`, `SQLSRV_ENC_BINARY` or `UTF-8`. Defaults
* to `SQLSRV_ENC_CHAR` (the SQL Server default).
* - `encrypted`: Specifies whether the communication with SQL Server is encrypted.
* - `replica`: Specifies the server and instance of the database's mirror
* (if enabled and configured) to use when the primary server is unavailable.
* - `password`: Specifies the password associated with the User ID to be used when
* connecting with SQL Server Authentication.
* - `UID`: Specifies the User ID to be used when connecting with
* SQL Server Authentication.
* - `APP`: Specifies the application name used in tracing.
* - `timeout`: Specifies the number of seconds to wait before failing the connection attempt.
*
* Typically, these parameters are set in `Connections::add()`, when adding the adapter to the
* list of active connections.
* @return The adapter instance.
*/
public function __construct(array $config = array()) {
$defaults = array(
'APP' => 'app',
'host' => '(local), 1433',
'UID' => null,
'password' => null,
'encoding' => SQLSRV_ENC_CHAR,
'persistent' => true,
'Encrypted' => false,
'replica' => null,
'timeout' => null,
);
parent::__construct($config + $defaults);
}
/**
* Check for required PHP extension, or supported database feature.
*
* @param string $feature Test for support for a specific feature, i.e. `"transactions"` or
* `"arrays"`.
* @return boolean Returns `true` if the particular feature (or if SQL Server) support is enabled,
* otherwise `false`.
*/
public static function enabled($feature = null) {
if (!$feature) {
return extension_loaded('sqlsrv');
}
$features = array(
'arrays' => false,
'transactions' => false,
'booleans' => true,
'relationships' => true,
);
return isset($features[$feature]) ? $features[$feature] : null;
}
/**
* Connects to the database using the options provided to the class constructor.
*
* @return boolean Returns `true` if a database connection could be established, otherwise
* `false`.
*/
public function connect() {
$options = array();
$config = $this->_config;
$this->_isConnected = false;
if (!$config['database']) {
return false;
}
$mapping = array(
'database' => 'Database',
'replica' => 'Failover_partner',
'password' => 'PWD',
'login' => 'UID',
'persistent' => 'ConnectionPooling',
'encoding' => 'CharacterSet',
'timeout' => 'LoginTimeout'
);
foreach ($mapping as $from => $to) {
if (isset($config[$from])) {
$options[$to] = $config[$from];
}
}
if (!$this->connection = sqlsrv_connect($config['host'], $options)) {
return false;
}
return $this->_isConnected = true;
}
/**
* Disconnects the adapter from the database.
*
* @return boolean True on success, else false.
*/
public function disconnect() {
if ($this->_isConnected) {
$this->_isConnected = !sqlsrv_close($this->connection);
return !$this->_isConnected;
}
return true;
}
/**
* Returns the list of tables in the currently-connected database.
*
* @param string $model The fully-name-spaced class name of the model object making the request.
* @return array Returns an array of objects to which models can connect.
* @filter This method can be filtered.
*/
public function entities($model = null) {
return $this->_filter(__METHOD__, compact('model'), function($self, $params) {
$query = "SELECT TABLE_NAME FROM [INFORMATION_SCHEMA].[TABLES]";
if (!$result = $self->invokeMethod('_execute', array($query))) {
return null;
}
$entities = array();
while ($data = $result->next()) {
list($entities[]) = $data;
}
return $entities;
});
}
/**
* Gets the column schema for a given SQL Server database table.
*
* @param mixed $entity Specifies the table name for which the schema should be returned, or
* the class name of the model object requesting the schema, in which case the model
* class will be queried for the correct table name.
* @param array $meta
* @return array Returns an associative array describing the given table's schema, where the
* array keys are the available fields, and the values are arrays describing each
* field, containing the following keys:
* - `'type'`: The field type name
* @filter This method can be filtered.
*/
public function describe($entity, array $meta = array()) {
$params = compact('entity', 'meta');
return $this->_filter(__METHOD__, $params, function($self, $params, $chain) {
extract($params);
$name = $self->invokeMethod('_entityName', array($entity));
$sql = "SELECT COLUMN_NAME as Field, DATA_TYPE as Type, "
. "COL_LENGTH('{$name}', COLUMN_NAME) as Length, IS_NULLABLE As [Null], "
. "COLUMN_DEFAULT as [Default], "
. "COLUMNPROPERTY(OBJECT_ID('{$name}'), COLUMN_NAME, 'IsIdentity') as [Key], "
. "NUMERIC_SCALE as Size FROM INFORMATION_SCHEMA.COLUMNS "
. "WHERE TABLE_NAME = '{$name}'";
$columns = $self->read($sql, array('return' => 'array'));
$fields = array();
foreach ($columns as $column) {
$match = $self->invokeMethod('_column', array($column['type']));
$fields[$column['field']] = $match + array(
'null' => ($column['null'] == 'YES' ? true : false),
'default' => $column['default'],
);
if ($fields[$column['Field']]['Length'] === -1) {
$fields[$column['Field']]['Length'] = 'max';
}
}
return $fields;
});
}
/**
* Converts a given value into the proper type based on a given schema definition.
*
* @see lithium\data\source\Database::schema()
* @param mixed $value The value to be converted. Arrays will be recursively converted.
* @param array $schema Formatted array from `lithium\data\source\Database::schema()`
* @return mixed Value with converted type.
*/
public function value($value, array $schema = array()) {
if (($result = parent::value($value, $schema)) !== null) {
return $result;
}
return "'" . $value . "'";
}
/**
* In cases where the query is a raw string (as opposed to a `Query` object), to database must
* determine the correct column names from the result resource.
*
* @param mixed $query
* @param resource $resource
* @param object $context
* @return array
*/
public function schema($query, $result = null, $context = null) {
if (is_object($query)) {
return parent::schema($query, $result, $context);
}
$fields = array();
$count = sqlsrv_num_fields($result->resource());
foreach (sqlsrv_field_metadata($result->resource()) as $name => $value) {
if ($name === 'Name') {
$fields[] = $value;
}
}
return $fields;
}
public function encoding($encoding = null) {
return true;
}
/**
* Retrieves database error message and error code.
*
* @return array
*/
public function error() {
if ($error = sqlsrv_errors(SQLSRV_ERR_ALL)) {
return array($error[0]['code'], $error[0]['message']);
}
return null;
}
public function alias($alias, $context) {
if ($context->type() == 'update' || $context->type() == 'delete') {
return;
}
return parent::alias($alias, $context);
}
/**
* @todo Eventually, this will need to rewrite aliases for DELETE and UPDATE queries, same with
* order().
* @param string $conditions
* @param string $context
* @param array $options
* @return void
*/
public function conditions($conditions, $context, array $options = array()) {
return parent::conditions($conditions, $context, $options);
}
/**
* Execute a given query.
*
* @see lithium\data\source\Database::renderCommand()
* @param string $sql The sql string to execute
* @param array $options
* @return resource Returns the result resource handle if the query is successful.
*/
protected function _execute($sql, array $options = array()) {
return $this->_filter(__METHOD__, compact('sql', 'options'), function($self, $params) {
$sql = $params['sql'];
$options = $params['options'];
$resource = sqlsrv_query($self->connection, $sql);
if ($resource === true) {
return true;
}
if (is_resource($resource)) {
return $self->invokeMethod('_instance', array('result', compact('resource')));
}
list($code, $error) = $self->error();
throw new QueryException("{$sql}: {$error}", $code);
});
}
/**
* Gets the last auto-generated ID from the query that inserted a new record.
*
* @param object $query The `Query` object associated with the query which generated
* @return mixed Returns the last inserted ID key for an auto-increment column or a column
* bound to a sequence.
*/
protected function _insertId($query) {
$resource = $this->_execute('SELECT @@identity as insertId');
$id = $this->result('next', $resource, null);
$id = $id['insertId'];
$this->result('close', $resource, null);
if (!empty($id) && $id !== '0') {
return $id;
}
}
/**
* Get a result.
*
* @param string $type
* @param object $resource
* @param unknown_type $context
* @return array|null
*/
public function result($type, $resource, $context) {
if (!is_resource($resource)) {
return null;
}
switch ($type) {
case 'next':
$result = sqlsrv_fetch_array($resource, SQLSRV_FETCH_ASSOC);
break;
case 'close':
sqlsrv_free_stmt($resource);
$result = null;
break;
default:
$result = parent::result($type, $resource, $context);
break;
}
return $result;
}
/**
* Converts database-layer column types to basic types.
*
* @param string $real Real database-layer column type (i.e. "varchar(255)")
* @return string Abstract column type (i.e. "string")
*/
protected function _column($real) {
if (is_array($real)) {
$length = '';
if (isset($real['length'])) {
$length = $real['length'];
if ($length === -1) {
$length = 'max';
}
$length = '(' . $length . ')';
}
return $real['type'] . $length;
}
if (!preg_match('/(?P<type>[^(]+)(?:\((?P<length>[^)]+)\))?/', $real, $column)) {
return $real;
}
$column = array_intersect_key($column, array('type' => null, 'length' => null));
switch (true) {
case $column['type'] === 'datetime':
break;
case ($column['type'] == 'tinyint' && $column['length'] == '1'):
case ($column['type'] == 'bit'):
$column = array('type' => 'boolean');
break;
case (strpos($column['type'], 'int') !== false):
$column['type'] = 'integer';
break;
case (strpos($column['type'], 'text') !== false):
$column['type'] = 'text';
break;
case strpos($column['type'], 'char') !== false:
if (isset($column['length']) && $column['length'] === 'max') {
$column['type'] = 'text';
unset($column['length']);
} else {
$column['type'] = 'string';
}
break;
case (strpos($column['type'], 'binary') !== false || $column['type'] == 'image'):
$column['type'] = 'binary';
break;
case preg_match('/float|double|decimal/', $column['type']):
$column['type'] = 'float';
break;
default:
$column['type'] = 'text';
break;
}
return $column;
}
/**
* Helper method that retrieves an entity's name via its metadata.
*
* @param string $entity Entity name.
* @return string Name.
*/
protected function _entityName($entity) {
if (class_exists($entity, false) && method_exists($entity, 'meta')) {
$entity = $entity::meta('name');
}
return $entity;
}
}
?>