1 |
<?php |
2 |
|
3 |
# convert calls to xx_ SQL to Pear::DB |
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 |
# That *WILL* break some aplication - it should LibData also, since it uses |
12 |
# three databases. However, LibData is using SQL syntax with dots in it, |
13 |
# so behaviour can be emulated (at SQL level) using PostgreSQL schema |
14 |
|
15 |
require_once 'DB.php'; |
16 |
|
17 |
|
18 |
function XXX_debug ($string = "XXX_debug called with empty string") { |
19 |
error_log($string, 0); |
20 |
} |
21 |
|
22 |
function xx_connect ( $db_srv, $db_usr, $db_pwd ) { |
23 |
global $db; |
24 |
|
25 |
$db =& DB::connect($GLOBALS['dsn'], $GLOBALS['dsn_options']); |
26 |
if (DB::isError($db)) { |
27 |
XXX_debug($db->getMessage() ." / ". |
28 |
$db->getUserInfo() . " / ". |
29 |
$db->getDebugInfo()); |
30 |
# XXX remove this! |
31 |
print $db->getDebugInfo(); |
32 |
|
33 |
die($db->getMessage()); |
34 |
} |
35 |
|
36 |
} |
37 |
|
38 |
function xx_errno() { |
39 |
global $db; |
40 |
|
41 |
die ("xx_errno called"); |
42 |
|
43 |
# XXX hmmm... |
44 |
# check last object for error! |
45 |
return 0; |
46 |
} |
47 |
|
48 |
function xx_fetch_array ($rs) { |
49 |
if ($GLOBALS["xx_element_nr $rs"] > $rs->numRows()) { |
50 |
return; |
51 |
} |
52 |
|
53 |
XXX_debug("xx_fetch_array ($rs) row: ".$GLOBALS["xx_element_nr $rs"]); |
54 |
|
55 |
$arr = $rs->fetchRow(DB_FETCHMODE_ASSOC, $GLOBALS["xx_element_nr $rs"]); |
56 |
$GLOBALS["xx_element_nr $rs"]++; |
57 |
return $arr; |
58 |
} |
59 |
|
60 |
|
61 |
function xx_insert_id($con = 0) { |
62 |
|
63 |
die ("xx_insert_id called"); |
64 |
|
65 |
if ($con == 0) { |
66 |
$con = $GLOBALS['xx_dbh']; |
67 |
} |
68 |
# this depends on premise that autoincrement field in tables |
69 |
# is always first one. If would be better to search for field |
70 |
# with sequence and return that, but it's too much work, and |
71 |
# this seem to work most of the time |
72 |
$oid = $GLOBALS["pg_last_oid"]; |
73 |
preg_match('/\s*insert\s+into\s+(\S+)\s+/is', $GLOBALS["xx_last_sql"], $table); |
74 |
if ($table) { |
75 |
$result = pg_query($con, "SELECT * FROM $table[1] where oid=$oid"); |
76 |
$arr = pg_fetch_array($result, 0, PGSQL_NUM); |
77 |
XXX_debug("xx_insert_id (table: $table, oid: $oid): ".$arr[0]); |
78 |
return $arr[0]; |
79 |
} else { |
80 |
print "WARNING: xx_insert_id emulation failed! Last SQL query was '".$GLOBALS["xx_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!"; |
81 |
exit; |
82 |
} |
83 |
} |
84 |
|
85 |
function xx_num_rows($rs) { |
86 |
$num = $rs->numRows(); |
87 |
XXX_debug("xx_num_rows: $num"); |
88 |
return $num; |
89 |
} |
90 |
|
91 |
function xx_query($sql, $con = 0) { |
92 |
global $db; |
93 |
|
94 |
if ($con == 0) { |
95 |
$con = $db; |
96 |
XXX_debug("xx_query (fixed conn): $sql"); |
97 |
} else { |
98 |
XXX_debug("xx_query (conn: $con): $sql"); |
99 |
} |
100 |
|
101 |
# PostgreSQL MVC obsoletes usage of lock/unlock in mysql terms |
102 |
if (preg_match('/^\s*lock/is', $sql) || preg_match('/^\s*unlock/is', $sql)) { |
103 |
return 1; |
104 |
} |
105 |
|
106 |
# fix Mysql contact function into something SQL92 compliant |
107 |
$sql = preg_replace('/concat\(([^\,]+)\s*,\s*([^\)]+)\)/is', '\\1 || \\2', $sql); |
108 |
|
109 |
# convert Mysql password function to md5 |
110 |
$sql = preg_replace('/password\(([^\)]+)\)/is', 'md5(\\1)', $sql); |
111 |
|
112 |
# fix update statements to remove table name (which would be understood as |
113 |
# schama in PostgreSQL |
114 |
preg_match('/^\s*update\s+(\S+)\s+set/is', $sql, $table); |
115 |
if ($table) { |
116 |
$sql = preg_replace('/\s+'.$table[1].'\.(\w+)/is', ' \\1', $sql); |
117 |
} |
118 |
|
119 |
$sql = preg_replace('/password\(([^\)]+)\)/is', 'md5(\\1)', $sql); |
120 |
|
121 |
$GLOBALS["xx_last_sql"] = $sql; |
122 |
|
123 |
XXX_debug("xx_query [transformed]: $sql"); |
124 |
|
125 |
$ret = $con->query($sql); |
126 |
|
127 |
$GLOBALS["xx_element_nr $ret"] = 0; |
128 |
|
129 |
# $GLOBALS["pg_last_oid"] = pg_last_oid($ret); |
130 |
if ($ret) { |
131 |
return $ret; |
132 |
} else { |
133 |
print "WARNING: error executing SQL query '$sql'."; |
134 |
} |
135 |
} |
136 |
|
137 |
function xx_data_seek($rs, $element_nr) { |
138 |
$GLOBALS["xx_element_nr"] = $element_nr; |
139 |
} |
140 |
|
141 |
function xx_error() { |
142 |
global $db; |
143 |
|
144 |
# return $db->getUserInfo(); |
145 |
return $db->DebugInfo(); |
146 |
} |
147 |
|
148 |
?> |