-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpudlSqlSrv.php
More file actions
171 lines (123 loc) · 4.9 KB
/
pudlSqlSrv.php
File metadata and controls
171 lines (123 loc) · 4.9 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
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
<?php
if (!class_exists('pudl',false)) require_once(__DIR__.'/../pudl.php');
require_once(is_owner(__DIR__.'/pudlMsShared.php'));
require_once(is_owner(__DIR__.'/pudlSqlSrvResult.php'));
class pudlSqlSrv
extends pudlMsShared {
////////////////////////////////////////////////////////////////////////////
// CONNECT TO THE MICROSOFT SQL SERVER
// http://php.net/manual/en/function.sqlsrv-connect.php
////////////////////////////////////////////////////////////////////////////
public function connect() {
$auth = $this->auth();
pudl_require_extension('sqlsrv');
//USE EXISTING CONNECTION IF AVAILABLE
if (is_resource($auth['server'])) {
$this->connection = $auth['server'];
//ATTEMPT TO CREATE A NEW CONNECTION OTHERWISE
} else {
$this->connection = @sqlsrv_connect(
$auth['server'],
[
'Database' => $auth['database'],
'UID' => $auth['username'],
'PWD' => $auth['password'],
'ConnectionPooling' => $auth['persistent'],
'LoginTimeout' => $auth['timeout'],
'APP' => 'PHP/PUDL',
'CharacterSet' => 'UTF-8',
]
);
}
if (!$this->connection) {
throw new pudlConnectionException($this,
'Unable to connect to Microsoft SQL Server ' .
'"' . $auth['server'] . '"' .
' with the username ' .
'"' . $auth['username'] . '"' .
"\nError " . $this->errno() .
': ' . $this->error()
);
}
// STORE WHICH SERVER WE'RE CONNECTED TO
if (!empty($auth['server'])) {
$this->connected = $auth['server'];
}
}
////////////////////////////////////////////////////////////////////////////
// CLOSE THE CONNECTION TO THE MICROSOFT SQL SERVER
// http://php.net/manual/en/function.sqlsrv-close.php
////////////////////////////////////////////////////////////////////////////
public function disconnect($trigger=true) {
parent::disconnect($trigger);
if ($this->connection) @sqlsrv_close($this->connection);
$this->connection = NULL;
}
////////////////////////////////////////////////////////////////////////////
// PROCESS THE SQL QUERY
// http://php.net/manual/en/function.sqlsrv-query.php
////////////////////////////////////////////////////////////////////////////
protected function process($query) {
if (!$this->connection) return new pudlSqlSrvResult($this);
$result = @sqlsrv_query(
$this->connection,
$query,
[],
['Scrollable' => SQLSRV_CURSOR_STATIC]
);
return new pudlSqlSrvResult($this, $result);
}
////////////////////////////////////////////////////////////////////////////
// GET THE MOST RECENT AUTO-INCREMENT ID
// https://docs.microsoft.com/en-us/sql/t-sql/functions/identity-transact-sql
////////////////////////////////////////////////////////////////////////////
public function insertId() {
if (!$this->connection) return false;
$result = @sqlsrv_query($this->connection, 'SELECT @@IDENTITY');
if ($result === false) return false;
$return = @sqlsrv_fetch_array($result, SQLSRV_FETCH_NUMERIC);
@sqlsrv_free_stmt($result);
return reset($return);
}
////////////////////////////////////////////////////////////////////////////
// GET THE NUMBER OF ROWS AFFECTED BY THE MOST RECENT SQL QUERY
// https://docs.microsoft.com/en-us/sql/t-sql/functions/rowcount-transact-sql
////////////////////////////////////////////////////////////////////////////
public function updated() {
if (!$this->connection) return false;
$result = @sqlsrv_query($this->connection, 'SELECT @@ROWCOUNT');
if ($result === false) return false;
$return = @sqlsrv_fetch_array($result, SQLSRV_FETCH_NUMERIC);
@sqlsrv_free_stmt($result);
return reset($return);
}
////////////////////////////////////////////////////////////////////////////
// GET THE VERSION INFO STRING FOR THE MICROSOFT SQL SERVER
// http://php.net/manual/en/function.sqlsrv-server-info.php
////////////////////////////////////////////////////////////////////////////
public function info() {
if (!$this->connection) return NULL;
$info = sqlsrv_server_info($this->connection);
return $info['SQLServerVersion'];
}
////////////////////////////////////////////////////////////////////////////
// RETURNS THE ERROR CODE FOR THE MOST RECENT FUNCTION CALL
// http://php.net/manual/en/function.sqlsrv-errors.php
////////////////////////////////////////////////////////////////////////////
public function errno() {
$errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
if (empty($errors)) return 0;
$error = end($errors);
return !empty($error['code']) ? $error['code'] : 0;
}
////////////////////////////////////////////////////////////////////////////
// RETURNS A STRING DESCRIPTION OF THE LAST ERROR
// http://php.net/manual/en/function.sqlsrv-errors.php
////////////////////////////////////////////////////////////////////////////
public function error() {
$errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
if (empty($errors)) return false;
$error = end($errors);
return !empty($error['message']) ? $error['message'] : false;
}
}