1 |
<?php |
2 |
|
3 |
# convert calls to xx_ SQL to PostgreSQL |
4 |
|
5 |
# this is ugly cludge. Since MySQL calls xx_connect and expects to |
6 |
# see database connection and after that calls xx_select_db to select |
7 |
# which database to use, we ignore that alltogether. Insted, we are using |
8 |
# well-known global variables to make correct connection to database, and |
9 |
# ignore xx_select_db. |
10 |
|
11 |
function XXX_debug ($string = "XXX_debug called with empty string") { |
12 |
error_log($string, 0); |
13 |
} |
14 |
|
15 |
function xx_connect ( $db_srv, $db_usr, $db_pwd ) { |
16 |
#$info = "host=$db_srv dbname=$db_nam user=$db_usr password=$db_pwd"; |
17 |
$info = "dbname=libdata user=dpavlin"; |
18 |
$GLOBALS['pg_dbh'] = pg_connect($info); |
19 |
return $GLOBALS['pg_dbh']; |
20 |
} |
21 |
|
22 |
function xx_select_db ( $db_nam, $con ) { |
23 |
return 1; |
24 |
} |
25 |
|
26 |
|
27 |
function xx_errno() { |
28 |
# return 0 on success |
29 |
$status = pg_result_status($GLOBALS['pg_dbh']); |
30 |
if ( |
31 |
$status == PGSQL_EMPTY_QUERY || |
32 |
$status == PGSQL_COMMAND_OK || |
33 |
$status == PGSQL_TUPLES_OK || |
34 |
$status == PGSQL_COPY_TO || |
35 |
$status == PGSQL_COPY_FROM || |
36 |
$status == PGSQL_NONFATAL_ERROR |
37 |
) { |
38 |
return 0; |
39 |
} else { |
40 |
return $status; |
41 |
} |
42 |
} |
43 |
|
44 |
function xx_fetch_array ($rs) { |
45 |
if ($GLOBALS["xx_element_nr $rs"] > pg_num_rows($rs)) { |
46 |
return; |
47 |
} |
48 |
XXX_debug("xx_fetch_array ($rs) row: ".$GLOBALS["xx_element_nr $rs"]); |
49 |
$arr = @pg_fetch_array($rs, $GLOBALS["xx_element_nr $rs"]); |
50 |
$GLOBALS["xx_element_nr $rs"]++; |
51 |
return $arr; |
52 |
} |
53 |
|
54 |
|
55 |
function xx_insert_id($con = 0) { |
56 |
if ($con == 0) { |
57 |
$con = $GLOBALS['pg_dbh']; |
58 |
} |
59 |
# this depends on premise that autoincrement field in tables |
60 |
# is always first one. If would be better to search for field |
61 |
# with sequence and return that, but it's too much work, and |
62 |
# this seem to work most of the time |
63 |
$oid = $GLOBALS["pg_last_oid"]; |
64 |
preg_match('/\s*insert\s+into\s+(\S+)\s+/is', $GLOBALS["pg_last_sql"], $table); |
65 |
if ($table) { |
66 |
$result = pg_query($con, "SELECT * FROM $table[1] where oid=$oid"); |
67 |
$arr = pg_fetch_array($result, 0, PGSQL_NUM); |
68 |
XXX_debug("xx_insert_id (table: $table, oid: $oid): ".$arr[0]); |
69 |
return $arr[0]; |
70 |
} else { |
71 |
print "WARNING: xx_insert_id emulation failed! Last SQL query was '".$GLOBALS["pg_last_sql"]."' and I can't extract table name from that query. If I could, I would try to find tuple with oid $oid in it!"; |
72 |
exit; |
73 |
} |
74 |
} |
75 |
|
76 |
function xx_num_rows($rs) { |
77 |
$num = pg_num_rows($rs); |
78 |
if (!isset($num)) { |
79 |
$num = pg_affected_rows($rs); |
80 |
} |
81 |
if (!isset($num)) { |
82 |
$num = 0; |
83 |
} |
84 |
XXX_debug("xx_num_rows: $num"); |
85 |
return $num; |
86 |
} |
87 |
|
88 |
function xx_query($sql, $con = 0) { |
89 |
|
90 |
if ($con == 0) { |
91 |
$con = $GLOBALS['pg_dbh']; |
92 |
XXX_debug("xx_query (fixed conn): $sql"); |
93 |
} else { |
94 |
XXX_debug("xx_query (conn: $con): $sql"); |
95 |
} |
96 |
|
97 |
# PostgreSQL MVC obsoletes usage of lock/unlock in mysql terms |
98 |
if (preg_match('/^\s*lock/is', $sql) || preg_match('/^\s*unlock/is', $sql)) { |
99 |
return 1; |
100 |
} |
101 |
|
102 |
# fix Mysql contact function into something SQL92 compliant |
103 |
$sql = preg_replace('/concat\(([^\,]+)\s*,\s*([^\)]+)\)/is', '\\1 || \\2', $sql); |
104 |
|
105 |
# convert Mysql password function to md5 |
106 |
$sql = preg_replace('/password\(([^\)]+)\)/is', 'md5(\\1)', $sql); |
107 |
|
108 |
# fix update statements to remove table name (which would be understood as |
109 |
# schama in PostgreSQL |
110 |
preg_match('/^\s*update\s+(\S+)\s+set/is', $sql, $table); |
111 |
if ($table) { |
112 |
$sql = preg_replace('/\s+'.$table[1].'\.(\w+)/is', ' \\1', $sql); |
113 |
} |
114 |
|
115 |
$sql = preg_replace('/password\(([^\)]+)\)/is', 'md5(\\1)', $sql); |
116 |
|
117 |
$GLOBALS["pg_last_sql"] = $sql; |
118 |
|
119 |
XXX_debug("xx_query [transformed]: $sql"); |
120 |
|
121 |
$ret = pg_query($con, $sql); |
122 |
|
123 |
$GLOBALS["xx_element_nr $ret"] = 0; |
124 |
|
125 |
$GLOBALS["pg_last_oid"] = pg_last_oid($ret); |
126 |
if ($ret) { |
127 |
return $ret; |
128 |
} else { |
129 |
print "WARNING: error executing SQL query '$sql'."; |
130 |
} |
131 |
} |
132 |
|
133 |
function xx_data_seek($rs, $element_nr) { |
134 |
$GLOBALS["xx_element_nr"] = $element_nr; |
135 |
} |
136 |
|
137 |
function xx_error() { |
138 |
return pg_result_error($GLOBALS['pg_dbh']); |
139 |
} |
140 |
|
141 |
?> |