-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlServerProcessor.php
More file actions
135 lines (117 loc) · 4.27 KB
/
SqlServerProcessor.php
File metadata and controls
135 lines (117 loc) · 4.27 KB
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
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\Sqlsrv\Query\Processors;
use Exception;
use Hyperf\Database\Connection;
use Hyperf\Database\Query\Builder;
use Hyperf\Database\Query\Processors\Processor;
class SqlServerProcessor extends Processor
{
/**
* Process an "insert get ID" query.
*
* @param string $sql
* @param array $values
* @param null|string $sequence
* @throws Exception
*/
public function processInsertGetId(Builder $query, $sql, $values, $sequence = null): int
{
/** @var Connection $connection */
$connection = $query->getConnection();
$connection->insert($sql, $values);
if ($connection->getConfig('odbc') === true) {
$id = $this->processInsertGetIdForOdbc($connection);
} else {
$id = $connection->getPdo()->lastInsertId();
}
return is_numeric($id) ? (int) $id : $id;
}
/**
* Process the results of a columns query.
*/
public function processColumns(array $results): array
{
return array_map(function ($result) {
$result = (object) $result;
$type = match ($typeName = $result->type_name) {
'binary', 'varbinary', 'char', 'varchar', 'nchar', 'nvarchar' => $result->length == -1 ? $typeName . '(max)' : $typeName . "({$result->length})",
'decimal', 'numeric' => $typeName . "({$result->precision},{$result->places})",
'float', 'datetime2', 'datetimeoffset', 'time' => $typeName . "({$result->precision})",
default => $typeName,
};
return [
'name' => $result->name,
'type_name' => $result->type_name,
'type' => $type,
'collation' => $result->collation,
'nullable' => (bool) $result->nullable,
'default' => $result->default,
'auto_increment' => (bool) $result->autoincrement,
'comment' => $result->comment,
'generation' => $result->expression ? [
'type' => $result->persisted ? 'stored' : 'virtual',
'expression' => $result->expression,
] : null,
];
}, $results);
}
/**
* Process the results of an indexes query.
*/
public function processIndexes(array $results): array
{
return array_map(function ($result) {
$result = (object) $result;
return [
'name' => strtolower($result->name),
'columns' => explode(',', $result->columns),
'type' => strtolower($result->type),
'unique' => (bool) $result->unique,
'primary' => (bool) $result->primary,
];
}, $results);
}
/**
* Process the results of a foreign keys query.
*/
public function processForeignKeys(array $results): array
{
return array_map(function ($result) {
$result = (object) $result;
return [
'name' => $result->name,
'columns' => explode(',', $result->columns),
'foreign_schema' => $result->foreign_schema,
'foreign_table' => $result->foreign_table,
'foreign_columns' => explode(',', $result->foreign_columns),
'on_update' => strtolower(str_replace('_', ' ', $result->on_update)),
'on_delete' => strtolower(str_replace('_', ' ', $result->on_delete)),
];
}, $results);
}
/**
* Process an "insert get ID" query for ODBC.
*
* @throws Exception
*/
protected function processInsertGetIdForOdbc(Connection $connection): int
{
$result = $connection->selectFromWriteConnection(
'SELECT CAST(COALESCE(SCOPE_IDENTITY(), @@IDENTITY) AS int) AS insertid'
);
if (! $result) {
throw new Exception('Unable to retrieve lastInsertID for ODBC.');
}
$row = $result[0];
return is_object($row) ? $row->insertid : $row['insertid'];
}
}