-
Notifications
You must be signed in to change notification settings - Fork 0
/
mssql.php
258 lines (225 loc) · 6.73 KB
/
mssql.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
<?php
/*
PHP REST SQL: A HTTP REST interface to relational databases
written in PHP
mssql.php :: Mssql database adapter
Copyright (C) 2008 Lee Smith <[email protected]>
based on MySQL adapter mysql.php by Paul James
Copyright (C) 2004 Paul James <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* $id$ */
/**
* PHP REST Mssql class
* Mssql connection class.
*/
class mssql {
/**
* @var int
*/
var $lastInsertPKeys;
/**
* @var resource
*/
var $lastQueryResultResource;
/**
* @var resource Database resource
*/
var $db;
/**
* Connect to the database.
* @param str[] config
*/
function connect($config) {
$connString = sprintf(
'host=%s dbname=%s user=%s password=%s',
$config['server'],
$config['database'],
$config['username'],
$config['password']
);
if ($this->db = mssql_pconnect($config['server'], $config['username'], $config['password'])) {
mssql_select_db($config['database']);
return TRUE;
}
return FALSE;
}
/**
* Close the database connection.
*/
function close() {
mssql_close($this->db);
}
/**
* Get the columns in a table.
* @param str table
* @return resource A resultset resource
*/
function getColumns($table) {
$qs = sprintf('SELECT * FROM information_schema.columns WHERE table_name =\'%s\'', $table);
return mssql_query($qs, $this->db);
}
/**
* Get a row from a table.
* @param str table
* @param str where
* @return resource A resultset resource
*/
function getRow($table, $where) {
$result = mssql_query(sprintf('SELECT * FROM %s WHERE %s', $table, $where));
if ($result) {
$this->lastQueryResultResource = $result;
}
return $result;
}
/**
* Get the rows in a table.
* @param str primary The names of the primary columns to return
* @param str table
* @return resource A resultset resource
*/
function getTable($primary, $table) {
$result = mssql_query(sprintf('SELECT %s FROM %s', $primary, $table));
if ($result) {
$this->lastQueryResultResource = $result;
}
return $result;
}
/**
* Get the tables in a database.
* @return resource A resultset resource
*/
function getDatabase() {
return mssql_query('SELECT table_name FROM information_schema.tables');
}
/**
* Get the primary keys for the request table.
* @return str[] The primary key field names
*/
function getPrimaryKeys($table) {
$primary = NULL;
$query = sprintf("SELECT [name]
FROM syscolumns
WHERE [id] IN (SELECT [id]
FROM sysobjects
WHERE [name] = '%s')
AND colid IN (SELECT SIK.colid
FROM sysindexkeys SIK
JOIN sysobjects SO ON SIK.[id] = SO.[id]
WHERE SIK.indid = 1
AND SO.[name] = '%s')", $table, $table);
$result = mssql_query($query);
// while($row = mssql_fetch_assoc($result))
$row = mssql_fetch_assoc($result);
{
if($row)
$primary[] = $row['name'];
}
return $primary;
}
/**
* Update a row.
* @param str table
* @param str values
* @param str where
* @return bool
*/
function updateRow($table, $values, $where) {
# translate from MySQL syntax :)
$values = preg_replace('/"/','\'',$values);
$values = preg_replace('/`/','"',$values);
$qs = sprintf('UPDATE %s SET %s WHERE %s', $table, $values, $where);
$result = mssql_query($qs);
if ($result) {
$this->lastQueryResultResource = $result;
}
return $result;
}
/**
* Insert a new row.
* @param str table
* @param str names
* @param str values
* @return bool
*/
function insertRow($table, $names, $values) {
# translate from MySQL syntax
$names = preg_replace('/`/', '"', $names); #backticks r so MySQL-ish! ;)
$values = preg_replace('/"/', '\'', $values);
$pkeys = join(', ', $this->getPrimaryKeys($table));
$qs = sprintf(
'INSERT INTO $table ("%s") VALUES ("%s") RETURNING (%s)',
$names,
$values,
$pkeys
);
$result = mssql_query($qs); #or die(pg_last_error());
$lastInsertPKeys = mssql_fetch_row($result);
$this->lastInsertPKeys = $lastInsertPKeys;
if ($result) {
$this->lastQueryResultResource = $result;
}
return $result;
}
/**
* Get the columns in a table.
* @param str table
* @return resource A resultset resource
*/
function deleteRow($table, $where) {
$result = mssql_query(sprintf('DELETE FROM %s WHERE %s', $table, $where));
if ($result) {
$this->lastQueryResultResource = $result;
}
return $result;
}
/**
* Escape a string to be part of the database query.
* @param str string The string to escape
* @return str The escaped string
*/
function escape($string) {
return mysql_escape_string($string);
}
/**
* Fetch a row from a query resultset.
* @param resource resource A resultset resource
* @return str[] An array of the fields and values from the next row in the resultset
*/
function row($resource) {
return mssql_fetch_assoc($resource);
}
/**
* The number of rows in a resultset.
* @param resource resource A resultset resource
* @return int The number of rows
*/
function numRows($resource) {
return mssql_num_rows($resource);
}
/**
* The number of rows affected by a query.
* @return int The number of rows
*/
function numAffected() {
return mssql_rows_affected($this->lastQueryResultResource);
}
/**
* Get the ID of the last inserted record.
* @return int The last insert ID ('a/b' in case of multi-field primary key)
*/
function lastInsertId() {
return join('/', $this->lastInsertPKeys);
}
}
?>