/
driver_clusterpoint_4.0.php
378 lines (329 loc) · 11.2 KB
/
driver_clusterpoint_4.0.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
<?php
/**
* Provides a driver for Clutserpoint V 4.0
*
* This file maps original mysql_* functions with clusterpoint equivalents
*
*/
require_once(CPS4WP_ROOT."/sql_parser/PHPSQLParser.php");
require_once(CPS4WP_ROOT."/sql_parser/PHPSQLCreator.php");
require_once(CPS4WP_ROOT."/SQLAbstract.php");
require_once(CPS4WP_ROOT."/SQLSelect.php");
require_once(CPS4WP_ROOT."/SQLInsert.php");
require_once(CPS4WP_ROOT."/SQLUpdate.php");
require_once(CPS4WP_ROOT."/SQLDelete.php");
// Initializing some variables
$GLOBALS['cps4wp_version'] = '0.1';
$GLOBALS['cps4wp_result'] = 0;
$GLOBALS['cps4wp_numrows_query'] = '';
$GLOBALS['cps4wp_ins_table'] = '';
$GLOBALS['cps4wp_ins_field'] = '';
$GLOBALS['cps4wp_last_insert'] = '';
$GLOBALS['cps4wp_connstr'] = '';
$GLOBALS['cps4wp_conn'] = false;
// CPS Specific implementations
function cps_escape_string($s){
return $s;
}
// wps functions
function wp_is_resource($result){
return true;
}
function wpsql_num_rows($result)
{ return count($result); }
function wpsql_numrows($result)
{ return count($result); }
function wpsql_num_fields($result)
{ return pg_num_fields($result); }
function wpsql_fetch_field($result)
{ return 'tablename'; }
function wpsql_fetch_object($result)
{
static $counter=false;
if($counter==false) $counter = 0;
if($counter < $result->body->found){
$counter++;
return $result->body->results[$counter-1];
}
$counter = false;
return false;
}
function wpsql_free_result($result)
{ return true;//pg_free_result($result);
}
function wpsql_affected_rows()
{
if( $GLOBALS['cps4wp_result'] === false)
return 0;
else
return $GLOBALS['cps4wp_result']->body->found;
}
function wpsql_fetch_row($result)
{ return pg_fetch_row($result); }
function wpsql_data_seek($result, $offset)
{ return pg_result_seek ( $result, $offset ); }
function wpsql_error()
{ return ''; if( $GLOBALS['cps4wp_conn']) return pg_last_error(); else return ''; }
function wpsql_fetch_assoc($result) { return pg_fetch_assoc($result); }
function wpsql_escape_string($s) { return pg_escape_string($s); }
function wpsql_real_escape_string($s,$c=NULL) { return cps_escape_string($s); }
function wpsql_get_server_info() { return '5.0.30'; } // Just want to fool wordpress ...
/**** Modified version of wpsql_result() is at the bottom of this file
function wpsql_result($result, $i, $fieldname)
{ return pg_fetch_result($result, $i, $fieldname); }
****/
// This is a fake connection except during installation
function wpsql_connect($dbserver, $dbuser, $dbpass)
{
$GLOBALS['cps4wp_connstr'] = '';
$hostport = explode(':', $dbserver);
if( !empty( $hostport[0]))
$GLOBALS['cps4wp_connstr'] .= ' host='.$hostport[0];
if( !empty( $hostport[1]))
$GLOBALS['cps4wp_connstr'] .= ' port='.$hostport[1];
if( !empty( $dbuser))
$GLOBALS['cps4wp_connstr'] .= ' user='.$dbuser;
if( !empty( $dbpass))
$GLOBALS['cps4wp_connstr'] .= ' password='.$dbpass;
elseif( !CPS4WP_INSECURE)
wp_die( 'Connecting to your Clutserpoint database without a password is not permitted.
<br />If you want to do it anyway, please set "CPS4WP_INSECURE" to true in your "db.php" file.' );
// // While installing, we test the connection to 'template1' (as we don't know the effective dbname yet)
if( defined('WP_INSTALLING') && WP_INSTALLING)
return wpsql_select_db( 'template1');
return 1;
}
// The effective connection happens here
function wpsql_select_db($dbname, $connection_id = 0)
{
// $pg_connstr = $GLOBALS['cps4wp_connstr'].' dbname='.$dbname;
// $GLOBALS['cps4wp_conn'] = pg_connect($pg_connstr);
// if( $GLOBALS['cps4wp_conn'])
// {
// $ver = pg_version($GLOBALS['cps4wp_conn']);
// if( isset($ver['server']))
// $GLOBALS['cps4wp_version'] = $ver['server'];
// }
// // Now we should be connected, we "forget" about the connection parameters (if this is not a "test" connection)
// if( !defined('WP_INSTALLING') || !WP_INSTALLING)
// $GLOBALS['cps4wp_connstr'] = '';
// // Execute early transmitted commands if needed
// if( isset($GLOBALS['cps4wp_pre_sql']) && !empty($GLOBALS['cps4wp_pre_sql']))
// foreach( $GLOBALS['cps4wp_pre_sql'] as $sql2run)
// wpsql_query( $sql2run);
// return $GLOBALS['cps4wp_conn'];
$GLOBALS['cps4wp_conn'] = $dbname;
return DB_NAME;
}
function wpsql_fetch_array($result)
{
$res = pg_fetch_array($result);
if( is_array($res) )
foreach($res as $v => $k )
$res[$v] = trim($k);
return $res;
}
function wpsql_query($sql, $dbh)
{
if( !$GLOBALS['cps4wp_conn'])
{
// Catch SQL to be executed as soon as connected
$GLOBALS['cps4wp_pre_sql'][] = $sql;
return true;
}
$initial = $sql;
if(!$sql) return;
return cps4wp_query( $sql);
}
function wpsql_insert_id($lnk = NULL)
{
global $wpdb;
$ins_field = $GLOBALS['cps4wp_ins_field'];
$table = $GLOBALS['cps4wp_ins_table'];
$lastq = $GLOBALS['cps4wp_last_insert'];
$seq = $table . '_seq';
// Table 'term_relationships' doesn't have a sequence
if( $table == $wpdb->term_relationships)
{
$sql = 'NO QUERY';
$data = 0;
}
// When using WP_Import plugin, ID is defined in the query
elseif('post_author' == $ins_field && false !== strpos($lastq,'ID'))
{
$sql = 'ID was in query ';
$pattern = '/.+\'(\d+).+$/';
preg_match($pattern, $lastq, $matches);
$data = $matches[1];
// We should update the sequence on the next non-INSERT query
$GLOBALS['cps4wp_queued_query'] = "SELECT SETVAL('$seq',(SELECT MAX(\"ID\") FROM $table)+1);";
}
else
{
return $GLOBALS['cps4wp_result']->body->
$sql = "SELECT CURRVAL('$seq')";
$res = pg_query($sql);
if( false !== $res)
$data = pg_fetch_result($res, 0, 0);
elseif( CPS4WP_DEBUG || CPS4WP_ERROR_LOG)
{
$log = '['.microtime(true)."] wpsql_insert_id() was called with '$table' and '$ins_field'".
" and generated an error. The latest INSERT query was :\n'$lastq'\n";
error_log( $log, 3, CPS4WP_LOG.'cps4wp_errors.log');
}
}
if( CPS4WP_DEBUG && $sql)
error_log( '['.microtime(true)."] Getting inserted ID for '$table' ('$ins_field') : $sql => $data\n", 3, CPS4WP_LOG.'cps4wp_insertid.log');
return $data;
}
function cps4wp_query( $sql)
{
global $wpdb;
// echo $sql;
$logto = 'queries';
// The end of the query may be protected against changes
$end = '';
// Remove unusefull spaces
$initial = $sql = trim($sql);
if( 0 === strpos($sql, 'SELECT'))
{
// clutserpoint doesnot support @ in queries
if(false !== strpos($sql, "@")) return false;
$logto = 'SELECT';
$s=new SQLSelect($sql);
return $GLOBALS['cps4wp_result'] = $s->toCps();
} // SELECT
elseif( 0 === strpos($sql, 'UPDATE'))
{
$logto = 'UPDATE';
$s=new SQLUpdate($sql);
return $GLOBALS['cps4wp_result'] = $s->toCps();
} // UPDATE
elseif( 0 === strpos($sql, 'INSERT'))
{
$logto = 'INSERT';
$s=new SQLInsert($sql);
return $GLOBALS['cps4wp_result'] = $s->toCps();
} // INSERT
elseif( 0 === strpos( $sql, 'DELETE' ))
{
$logto = 'DELETE';
$s=new SQLDelete($sql);
return $GLOBALS['cps4wp_result'] = $s->toCps();
}
// Fix tables listing
elseif( 0 === strpos($sql, 'SHOW TABLES'))
{
$logto = 'SHOWTABLES';
$o = new SQLAbstract();
return $o->execute('SELECT DISTINCT __table FROM '.DB_NAME);
}
// Rewriting optimize table
elseif( 0 === strpos($sql, 'OPTIMIZE TABLE'))
{
$logto = 'OPTIMIZE';
$sql = str_replace( 'OPTIMIZE TABLE', 'VACUUM', $sql);
}
// Handle 'SET NAMES ... COLLATE ...'
elseif( 0 === strpos($sql, 'SET NAMES') && false !== strpos($sql, 'COLLATE'))
{
$logto = 'SETNAMES';
$sql = "SET NAMES 'utf8'";
$sql= false; //cps don't need this now
}
// Load up upgrade and install functions as required
$begin = substr( $sql, 0, 3);
$search = array( 'SHO', 'ALT', 'DES', 'CRE', 'DRO');
if( in_array($begin, $search))
{
require_once( CPS4WP_ROOT.'/driver_pgsql_install.php');
$sql = cps4wp_installing( $sql, $logto);
}
// WP 2.9.1 uses a comparison where text data is not quoted
$pattern = '/AND meta_value = (-?\d+)/';
$sql = preg_replace( $pattern, 'AND meta_value = \'$1\'', $sql);
// Generic "INTERVAL xx YEAR|MONTH|DAY|HOUR|MINUTE|SECOND" handler
$pattern = '/INTERVAL[ ]+(\d+)[ ]+(YEAR|MONTH|DAY|HOUR|MINUTE|SECOND)/';
$sql = preg_replace( $pattern, "'\$1 \$2'::interval", $sql);
$pattern = '/DATE_SUB[ ]*\(([^,]+),([^\)]+)\)/';
$sql = preg_replace( $pattern, '($1::timestamp - $2)', $sql);
// Remove illegal characters
$sql = str_replace('`', '', $sql);
// Field names with CAPITALS need special handling
if( false !== strpos($sql, 'ID'))
{
$pattern = '/ID([^ ])/';
$sql = preg_replace($pattern, 'ID $1', $sql);
$pattern = '/ID$/';
$sql = preg_replace($pattern, 'ID ', $sql);
$pattern = '/\(ID/';
$sql = preg_replace($pattern, '( ID', $sql);
$pattern = '/,ID/';
$sql = preg_replace($pattern, ', ID', $sql);
$pattern = '/[0-9a-zA-Z_]+ID/';
$sql = preg_replace($pattern, '"$0"', $sql);
$pattern = '/\.ID/';
$sql = preg_replace($pattern, '."ID"', $sql);
$pattern = '/[\s]ID /';
$sql = preg_replace($pattern, ' "ID" ', $sql);
$pattern = '/"ID "/';
$sql = preg_replace($pattern, ' "ID" ', $sql);
} // CAPITALS
// Empty "IN" statements are erroneous
$sql = str_replace( 'IN (\'\')', 'IN (NULL)', $sql);
$sql = str_replace( 'IN ( \'\' )', 'IN (NULL)', $sql);
$sql = str_replace( 'IN ()', 'IN (NULL)', $sql);
// Put back the end of the query if it was separated
$sql .= $end;
// For insert ID catching
if( $logto == 'INSERT')
{
$pattern = '/INSERT INTO (\w+)\s+\([ a-zA-Z_"]+/';
preg_match($pattern, $sql, $matches);
$GLOBALS['cps4wp_ins_table'] = $matches[1];
$match_list = split(' ', $matches[0]);
if( $GLOBALS['cps4wp_ins_table'])
{
$GLOBALS['cps4wp_ins_field'] = trim($match_list[3],' () ');
if(! $GLOBALS['cps4wp_ins_field'])
$GLOBALS['cps4wp_ins_field'] = trim($match_list[4],' () ');
}
$GLOBALS['cps4wp_last_insert'] = $sql;
}
elseif( isset($GLOBALS['cps4wp_queued_query']))
{
pg_query($GLOBALS['cps4wp_queued_query']);
unset($GLOBALS['cps4wp_queued_query']);
}
// Correct quoting for PostgreSQL 9.1+ compatibility
$sql = str_replace( "\\'", "''", $sql);
$sql = str_replace( '\"', '"', $sql);
if( CPS4WP_DEBUG)
{
if( $initial != $sql)
error_log( '['.microtime(true)."] Converting :\n$initial\n---- to ----\n$sql\n---------------------\n", 3, CPS4WP_LOG.'cps4wp_'.$logto.'.log');
else
error_log( '['.microtime(true)."] $sql\n---------------------\n", 3, CPS4WP_LOG.'cps4wp_unmodified.log');
}
return $sql;
}
/*
Quick fix for wpsql_result() error and missing wpsql_errno() function
Source : http://vitoriodelage.wordpress.com/2014/06/06/add-missing-wpsql_errno-in-cps4wp-plugin/
*/
function wpsql_result($result, $i, $fieldname = null) {
if (is_resource($result)) {
if ($fieldname) {
return pg_fetch_result($result, $i, $fieldname);
} else {
return pg_fetch_result($result, $i);
}
}
}
function wpsql_errno( $connection) {
// throw new Exception("Error Processing Request", 1);
if($connection == 1) return false;
$result = pg_get_result($connection);
$result_status = pg_result_status($result);
return pg_result_error_field($result_status, PGSQL_DIAG_SQLSTATE);
}