-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_monetdb.php
537 lines (453 loc) · 14.1 KB
/
php_monetdb.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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
<?php
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright 2008-2015 MonetDB B.V.
?>
<?php
/**
* php_monetdb.php
* Implementation of the driver API.
*
* This library relies on the native PHP mapi implementation.
*
* This file should be included by all pages that want to make use of a
* database connection.
*
* Synopsis of the provided functions:
*
* function monetdb_connect() *
* function monetdb_disconnect() *
* function monetdb_connected() *
* function monetdb_query($query) *
* function monetdb_fetch_assoc($hdl) *
* function monetdb_fetch_object($hdl)
* function monetdb_num_rows($hdl) *
* function monetdb_affected_rows($hdl) *
* function monetdb_last_error() *
* function monetdb_insert_id($seq) *
* function monetdb_quote_ident($str) *
* function monetdb_escape_string($str) *
**/
/**
* php_mapi.inc is a native (socket based) php implementation of the MAPI communication protocol.
*/
require 'php_mapi.inc';
/**
* register a 'monetdb' extension to retain compatibility with wht Cimpl based scripts.
*/
/**
* Opens a connection to a MonetDB server.
*
* @param string language to be used (sql)
* @param string hostname to connect to (default is localhost)
* @param int port to use (default is 50000)
* @param string username (default is monetdb)
* @param string password (default is monetdb)
* @param string database to use (default is demo)
* @param string hash function to use during authentication (defaults to SHA1)
* @return bool TRUE on success or FALSE on failure
*/
function monetdb_connect($lang = "sql", $host = "127.0.0.1", $port = 50000, $username = "monetdb", $password = "monetdb", $database = "demo", $hashfunc = "") {
$options["host"] = $host;
$options["port"] = $port;
$options["username"] = $username;
$options["password"] = $password;
$options["database"] = $database;
$options["persistent"] = FALSE;
if ($hashfunc == "") {
$hashfunc = "sha1";
}
if ($lang == "") {
$lang = "sql";
}
$options["hashfunc"] = $hashfunc;
$options["lang"] = $lang;
return mapi_connect_proxy($options);
}
/**
* Opens a persistent connection to a MonetDB server.
* First, when connecting, the function would first try to find a (persistent) link that's already open with the same host,
* username and password. If one is found, an identifier for it will be returned instead of opening a new connection.
*
* Second, the connection to the SQL server will not be closed when the execution of the script ends.
* Instead, the link will remain open for future use (monetdb_close() will not close links established by monetdb_pconnect()).
*
* This type of link is therefore called 'persistent'.
*
* @param string language to be used (sql)
* @param string hostname to connect to (default is localhost)
* @param int port to use (default is 50000)
* @param string username (default is monetdb)
* @param string password (default is monetdb)
* @param string database to use (default is demo)
* @param string hash function to use during authentication (defaults to SHA1)
* @return bool TRUE on success or FALSE on failure
*/
function monetdb_pconnect($lang = "sql", $host = "127.0.0.1", $port = 500000, $username = "monetdb", $password = "monetdb", $database = "demo", $hashfunc = "") {
$options["host"] = $host;
$options["port"] = $port;
$options["username"] = $username;
$options["password"] = $password;
$options["database"] = $database;
$options["persistent"] = TRUE;
if ($hashfunc == "") {
$hashfunc = "sha1";
}
if ($lang == "") {
$lang = "sql";
} else if (strstr($lang, "sql") == $lang) {
$lang = "sql";
}
$options["hashfunc"] = $hashfunc;
$options["lang"] = $lang;
return mapi_connect_proxy($options);
}
/**
* Disconnects the connection to the database.
*
* @param resource connection instance
*/
function monetdb_disconnect($conn=NULL) {
$num_args = func_num_args();
if ($num_args == 0) {
$conn = mapi_get_current_conn();
mapi_close(NULL);
} else {
mapi_close($conn);
}
}
/**
* Returns whether a connection to the database has been made, and has
* not been closed yet. Note that this function doesn't guarantee that
* the connection is alive or usable.
*
* @param resource connection instance
* @return bool TRUE if there is a connection, FALSE otherwise
*
*/
function monetdb_connected($connection=NULL) {
$num_args = func_num_args();
if ($num_args == 0){
return mapi_connected(mapi_get_current_conn());
}
return mapi_connected($connection);
}
/**
* Executes the given query on the database.
*
* @param resource connection instance
* @param string the SQL query to execute
* @return resource a query handle or FALSE on failure
*/
function monetdb_query($connection=NULL, $query="") {
$num_args = func_num_args();
if ($num_args == 1){
$arg = func_get_arg(0);
if (is_string($arg)) {
$conn = mapi_get_current_conn();
if ($conn != NULL) {
return mapi_execute($conn, $arg);
}
}
} else {
return mapi_execute($connection, $query);
}
return FALSE;
}
/**
* Returns the number of rows in the query result.
*
* @param resouce the query resource
* @return int the number of rows in the result; FALSE if the query did not return any result set
*/
function monetdb_num_rows($hdl) {
if ($hdl["operation"] == Q_TABLE || $hdl["operation"] == Q_BLOCK ) {
return $hdl["query"]["rows"];
} else {
return FALSE;
}
}
/**
* Returns the number of fields in the query result.
*
* @param resouce the query resource
* @return int the number of fields in the result; FALSE if the query did not return any result set
*/
function monetdb_num_fields($hdl) {
if ($hdl["operation"] == Q_TABLE || $hdl["operation"] == Q_BLOCK ) {
return $hdl["query"]["fields"];
} else {
return FALSE;
}
}
/**
* Returns an array containing column values as value.
* For efficiency reasons the array pointer is not reset when calling monetdb_fetch_row
* specifying a row value.
*
* @param resource the query handle
* @param int the position of the row to retrieve
* @return array the next row in the query result as associative array or
* FALSE if no more rows exist
*/
function monetdb_fetch_row(&$hdl, $row=-1) {
global $last_error;
if ($hdl["operation"] != Q_TABLE && $hdl["operation"] != Q_BLOCK ) {
return FALSE;
}
if ($row == -1){
// Parse the tuple and present it to the user
$entry = current($hdl["record_set"]);
//advance the array of one position
next($hdl["record_set"]);
} else {
if ($row < $hdl["query"]["rows"]) {
/* Parse the tuple and present it to the user*/
$entry = $hdl["record_set"][$row-1];
}
else {
$last_error = "Index out of bound\n";
return FALSE;
}
}
if ($entry) {
return php_parse_row($entry);
}
return $entry;
}
/**
* Returns an associative array containing the column names as keys, and
* column values as value.
*
* @param resource the query handle
* @param int the position of the row to retrieve
* @return array the next row in the query result as associative array or
* FALSE if no more rows exist
*/
function monetdb_fetch_assoc(&$hdl, $row=-1) {
if ($hdl["operation"] != Q_TABLE && $hdl["operation"] != Q_BLOCK ) {
return FALSE;
}
// first retrieve the row as an array
$fetched_row = monetdb_fetch_row($hdl, $row);
if ($fetched_row == FALSE) {
return FALSE;
}
// now hash the array by field name
$hashed = array();
$i = 0;
foreach ($hdl["header"]["fields"] as $field) {
$field = str_replace(" ", "", $field);
$hashed[$field] = $fetched_row[$i];
$i++;
}
return $hashed;
}
/**
* Returns the result in the given query resource as object one row at a time. Column
* names become members of the object through which the column values
* can be retrieved.
*
* @param resource the query handle
* @param int the position of the row to retrieve
* @return the query result as object or FALSE if there are no more rows
*/
function monetdb_fetch_object(&$hdl, $row=-1) {
if ($hdl["operation"] != Q_TABLE && $hdl["operation"] != Q_BLOCK ) {
return FALSE;
}
if (($row_array = monetdb_fetch_assoc($hdl, $row)) == FALSE) {
return FALSE;
}
$row_object = new stdClass();
if (is_array($row_array) && count($row_array) > 0) {
foreach ($row_array as $name=>$value) {
$name = strtolower(trim($name));
if (!empty($name)) {
$row_object->$name = $value;
}
}
}
return $row_object;
}
/**
* Returns the name of the field at position $field
*
* @param resource the query handle
* @param int field number
* @return string the field name, FALSE is an error occured.
*/
function monetdb_field_name(&$hdl, $field) {
if (is_array($hdl) && $field >= 0) {
if ($hdl["operation"] == Q_TABLE || $hdl["operation"] == Q_BLOCK ) {
if ($hdl["header"]["fields"] != "" ) {
return $hdl["header"]["fields"][$field];
}
}
}
return FALSE;
}
/**
* Returns the number of affected rows for an UPDATE, INSERT or DELETE
* query. The number of affected rows typically is 1 for INSERT
* queries.
*
* @param resource the query handle
* @return int the number of affected rows, FALSE if the last executed query did not affect any row.
*/
function monetdb_affected_rows($hdl) {
if ($hdl["operation"] != Q_UPDATE) {
return FALSE;
}
return $hdl["query"]["affected"];
}
/**
* Check if the query handle contains a result set. Note: the result set may be empty.
*
* @param resource the query handle
* @return bool TRUE if the query contains a result set, FALSE otherwise.
*/
function monetdb_has_result($hdl) {
if (($hdl["operation"] == Q_TABLE) || ($hdl["operation"] == Q_BLOCK)) {
return TRUE;
}
return FALSE;
}
/**
* Returns the last error reported by the database.
*
* @return string the last error reported
*/
function monetdb_last_error() {
global $last_error;
return $last_error;
}
/**
* Generates the next id in the sequence $seq
*
* @param resource connection instance
* @param seq sequence whose next
* value we want to retrieve
* @return string the ID of the last tuple inserted. FALSE if an error occurs
*/
function monetdb_insert_id($connection = NULL, $seq) {
$num_args = func_num_args();
if ($num_args == 1) {
$connection = mapi_get_current_conn();
$seq = func_get_arg(0);
}
if (is_string($seq)) {
$query = "SELECT NEXT VALUE FOR ".monetdb_quote_ident($seq)."";
$res = monetdb_query($connection, $query);
$row = monetdb_fetch_assoc($result);
return($row[$seq]);
}
return FALSE;
}
/**
* Returns a 'quoted identifier' suitable for MonetDB.
* This utility function can be used in queries to for instance quote
* names of tables of columns that otherwise would be a mistaken for a
* keyword.
* NOTE: the given string is currently not checked for validity, hence
* the output of this function may be an invalid identifier.
*
* @param string the identifier to quote
* @return string the quoted identifier
*
*/
function monetdb_quote_ident($str) {
return('"'. $str .'"');
}
/**
* Returns an 'escaped' string that can be used for instance within
* single quotes to represent a CHARACTER VARYING object in SQL.
*
* @param string the string to escape
* @return string the escaped string
*/
function monetdb_escape_string($str) {
return mapi_quote($str, strlen($str));
}
/**
* Free the result set memory.
*
* @param resource the query handle.
* @return bool returns TRUE on success or FALSE on failure.
*
*/
function monetdb_free_result(&$hdl) {
$conn_id = $hdl["conn"];
$res_id = $hdl["query"]["id"];
/* Release the result set on server */
mapi_free_result($conn_id, $res_id);
if (isset($hdl) && is_array($hdl)) {
foreach($hdl as $field) {
if (isset($field)) {
unset($field);
}
}
unset($hdl);
return TRUE;
}
return FALSE;
}
/*
* These functions are not present in the original Cimpl implementation
*/
/**
* Create a new savepoint ID
* @param resource connection instance
* @return bool TRUE if the ID has been correctly generated, FALSE otherwise.
*/
function create_savepoint(&$conn) {
if ($conn != NULL) {
$index = count($conn["transactions"]);
$id = "monetdbsp" . $index;
array_push($conn["transactions"], $id);
return TRUE;
}
return FALSE;
}
/**
* Release a savepoint ID.
* @param resource connection instance
* @return bool TRUE if the ID has been correctly released, FALSE otherwise.
*/
function release_savepoint(&$conn) {
if ($conn != NULL) {
array_pop($conn["transactions"]);
return TRUE;
}
return FALSE;
}
/**
* Return the current (last generated) savepoint ID.
* @param resource connection instance
* @return string savepoint ID. I no savepoints are available, FALSE is returned.
*/
function get_savepoint(&$conn) {
if (count($conn["transactions"]) == 0) {
return FALSE;
}
// return the last element in the array
return $conn["transactions"][count($conn["transactions"])-1];
}
/**
* Sets auto commit mode on/off
* @param resource connection instance
* @param bool TRUE to turn auto commit on, FALSE to turn it off.
* @return bool TRUE is auto commit mode was correctly set. FALSE otherwise.
*/
function auto_commit($conn, $flag=TRUE) {
if ($conn["socket"] != NULL) {
$cmd = "auto_commit " . (int) $flag;
mapi_write($conn["socket"], format_command($cmd));
mapi_read($conn["socket"]);
return TRUE;
}
return FALSE;
}
?>