/
engine.inc.php
320 lines (287 loc) · 10.7 KB
/
engine.inc.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
<?php
/**************************************************************************
* This file is part of the WebIssues Server program
* Copyright (C) 2006 Michał Męciński
* Copyright (C) 2007-2020 WebIssues Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/
if ( !defined( 'WI_VERSION' ) ) die( -1 );
/**
* Back-end engine for Microsoft SQL Server database.
*
* This engine only works on Windows and uses ADODB to connect to the database.
*/
class System_Db_Mssql_Engine implements System_Db_IEngine
{
const adStateOpen = 0x01;
const adCmdText = 0x01;
const adExecuteNoRecords = 0x80;
const adParamInput = 1;
const adInteger = 3;
const adDouble = 5;
const adVarChar = 202;
const adVarBinary = 204;
const adTypeBinary = 1;
const adSaveCreateOverWrite = 2;
const adXactReadUncommitted = 256;
const adXactReadCommitted = 4096;
const adXactRepeatableRead = 65536;
const adXactSerializable = 1048576;
private $connection = null;
private $affectedRows = 0;
/**
* Constructor.
*/
public function __construct()
{
}
public function open( $host, $database, $user, $password )
{
$string = "Provider=sqloledb; Data Source=$host; Initial Catalog=$database";
if ( $user != '' )
$string .= "; User ID=$user; Password=$password";
else
$string .= "; Trusted_Connection=yes";
try {
$this->connection = new COM( 'ADODB.Connection', null, CP_UTF8 );
$this->connection->Open( $string );
} catch ( com_exception $ex ) {
throw new System_Db_Exception( null, $ex );
}
}
public function close()
{
if ( $this->connection != null ) {
if ( $this->connection->State & self::adStateOpen )
$this->connection->Close();
$this->connection = null;
}
}
public function execute( $query, $params )
{
try {
if ( empty( $params ) ) {
$this->connection->Execute( $query, $this->affectedRows, self::adCmdText | self::adExecuteNoRecords );
} else {
$command = $this->createCommand( $query, $params );
$command->Execute( $this->affectedRows, $params, self::adCmdText | self::adExecuteNoRecords );
}
} catch ( com_exception $ex ) {
$this->handleError( $ex );
}
}
public function query( $query, $params )
{
try {
if ( empty( $params ) ) {
$rs = $this->connection->Execute( $query, $this->affectedRows, self::adCmdText );
} else {
$command = $this->createCommand( $query, $params );
$rs = $command->Execute( $this->affectedRows, $params, self::adCmdText );
}
} catch ( com_exception $ex ) {
$this->handleError( $ex );
}
return new System_Db_Mssql_Result( $rs );
}
public function executeRaw( $query )
{
$this->execute( $query, array() );
}
private function createCommand( $query, $params )
{
$command = new COM( 'ADODB.Command', null, CP_UTF8 );
$command->ActiveConnection = $this->connection;
$command->CommandText = $query;
$command->CreateParameter( '', self::adInteger, self::adParamInput, -1, 0 );
foreach ( $params as $value ) {
if ( is_int( $value ) )
$param = $command->CreateParameter( '', self::adInteger, self::adParamInput, -1 );
else if ( is_float( $value ) )
$param = $command->CreateParameter( '', self::adDouble, self::adParamInput, -1 );
else if ( is_string( $value ) )
$param = $command->CreateParameter( '', self::adVarChar, self::adParamInput, max( mb_strlen( $value ), 1 ) );
else
$param = $command->CreateParameter( '', self::adVarBinary, self::adParamInput, max( count( $value ), 1 ) );
$command->Parameters->Append( $param );
}
return $command;
}
public function escapeArgument( $arg, $type, &$params )
{
switch( $type ) {
case 'd':
$params[] = (int)$arg;
return '?';
case 'f':
$params[] = (float)$arg;
return '?';
case 's':
case 't':
$params[] = (string)$arg;
return '?';
case 'b':
$stream = new COM( 'ADODB.Stream', null, CP_UTF8 );
$stream->Type = self::adTypeBinary;
$stream->Open();
if ( $arg->getPath() != null ) {
$stream->LoadFromFile( $arg->getPath() );
} else {
$path = tempnam( sys_get_temp_dir(), 'wi_' );
$arg->saveAs( $path );
$stream->LoadFromFile( $path );
unlink( $path );
}
$stream->Position = 0;
$params[] = $stream->Read();
$stream->Close();
return '?';
}
}
public function getPagedQuery( $query, $orderBy, $limit, $offset )
{
if ( substr( $query, 0, 7 ) != 'SELECT ' )
throw new System_Db_Exception( 'Not a select query' );
$query = substr( $query, 7 );
if ( $offset == 0 )
return "SELECT TOP $limit $query ORDER BY $orderBy";
$pos = strpos( $query, ' FROM ' );
if ( $pos === false )
throw new System_Db_Exception( 'No from clause found in query' );
$columns = explode( ',', substr( $query, 0, $pos ) );
foreach ( $columns as &$column ) {
$column = rtrim( $column , ' ' );
$pos = strrpos( $column, ' ' );
if ( $pos !== false )
$column = substr( $column, $pos + 1 );
$pos = strrpos( $column, '.' );
if ( $pos !== false )
$column = substr( $column, $pos + 1 );
$column = "t.$column";
}
$select = implode( ', ', $columns );
$top = $limit + $offset;
return "SELECT $select FROM ( SELECT TOP $top ROW_NUMBER() OVER (ORDER BY $orderBy) AS row_num, $query ) AS t WHERE t.row_num > $offset";
}
public function getLocaleCollation()
{
return '';
}
public function createAttachment( $data, $size, $fileName )
{
$path = tempnam( sys_get_temp_dir(), 'wi_' );
$stream = new COM( 'ADODB.Stream', null, CP_UTF8 );
$stream->Type = self::adTypeBinary;
$stream->Open();
$stream->Write( $data );
$stream->Position = 0;
$stream->SaveToFile( $path, self::adSaveCreateOverWrite );
$stream->Close();
return System_Core_Attachment::fromFile( $path, $size, $fileName, System_Core_Attachment::TemporaryFile );
}
public function getAffectedRows()
{
return $this->affectedRows;
}
public function getInsertId( $table, $column )
{
$query = 'SELECT @@IDENTITY';
$rs = $this->connection->Execute( $query, $this->affectedRows, self::adCmdText );
$insertId = $rs->Fields[ 0 ]->Value;
$rs->close();
// NOTE: $insertId is a VARIANT of type VT_DECIMAL so it must be converted to int
return (int)$insertId;
}
public function castExpression( $expression, $type )
{
switch ( $type ) {
case 'd':
return "CAST( $expression AS int )";
case 'f':
return "CAST( $expression AS decimal(14,6) )";
case 's':
return "CAST( $expression AS nvarchar(max) )";
case 't':
return "CAST( $expression AS datetime )";
default:
throw new System_Db_Exception( 'Invalid type' );
}
}
public function checkTableExists( $table )
{
$query = "SELECT OBJECT_ID('$table', 'U')";
$rs = $this->connection->Execute( $query, $this->affectedRows, self::adCmdText );
$objectId = $rs->Fields[ 0 ]->Value;
$rs->close();
return $objectId != null;
}
public function getParameter( $key )
{
switch ( $key ) {
case 'server':
return $this->connection->Properties[ 'DBMS Name' ];
case 'version':
return $this->connection->Properties[ 'DBMS Version' ];
default:
return null;
}
}
public function beginTransaction( $level, $table )
{
switch ( $level ) {
case System_Db_Transaction::ReadUncommitted:
$isoLevel = self::adXactReadUncommitted;
break;
case System_Db_Transaction::ReadCommitted:
$isoLevel = self::adXactReadCommitted;
break;
case System_Db_Transaction::RepeatableRead:
$isoLevel = self::adXactRepeatableRead;
break;
case System_Db_Transaction::Serializable:
$isoLevel = self::adXactSerializable;
break;
default:
throw new System_Db_Exception( 'Unsupported isolation level' );
}
try {
$this->connection->IsolationLevel = $isoLevel;
$this->connection->BeginTrans();
} catch ( com_exception $ex ) {
$this->handleError( $ex );
}
}
public function endTransaction( $commit )
{
try {
if ( $commit )
$this->connection->CommitTrans();
else
$this->connection->RollbackTrans();
} catch ( com_exception $ex ) {
$this->handleError( $ex );
}
}
private function handleError( $ex )
{
foreach ( $this->connection->Errors as $error ) {
if ( $error->NativeError == 1205 )
throw new System_Api_Error( System_Api_Error::TransactionDeadlock, $ex );
if ( $error->NativeError == 547 )
throw new System_Api_Error( System_Api_Error::ConstraintConflict, $ex );
}
throw new System_Db_Exception( null, $ex );
}
}