1 |
dpavlin |
50 |
<?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 |
|
|
if ($con == 0) { |
63 |
|
|
$con = $GLOBALS['xx_dbh']; |
64 |
|
|
} |
65 |
|
|
# this depends on premise that autoincrement field in tables |
66 |
|
|
# is always first one. If would be better to search for field |
67 |
|
|
# with sequence and return that, but it's too much work, and |
68 |
|
|
# this seem to work most of the time |
69 |
|
|
$oid = $GLOBALS["pg_last_oid"]; |
70 |
|
|
preg_match('/\s*insert\s+into\s+(\S+)\s+/is', $GLOBALS["xx_last_sql"], $table); |
71 |
|
|
if ($table) { |
72 |
|
|
$result = pg_query($con, "SELECT * FROM $table[1] where oid=$oid"); |
73 |
|
|
$arr = pg_fetch_array($result, 0, PGSQL_NUM); |
74 |
|
|
XXX_debug("xx_insert_id (table: $table, oid: $oid): ".$arr[0]); |
75 |
|
|
return $arr[0]; |
76 |
|
|
} else { |
77 |
|
|
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!"; |
78 |
|
|
exit; |
79 |
|
|
} |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
function xx_num_rows($rs) { |
83 |
|
|
$num = $rs->numRows(); |
84 |
|
|
XXX_debug("xx_num_rows: $num"); |
85 |
|
|
return $num; |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
function xx_query($sql, $con = 0) { |
89 |
|
|
global $db; |
90 |
|
|
|
91 |
|
|
if ($con == 0) { |
92 |
|
|
$con = $db; |
93 |
|
|
XXX_debug("xx_query (fixed conn): $sql"); |
94 |
|
|
} else { |
95 |
|
|
XXX_debug("xx_query (conn: $con): $sql"); |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
# PostgreSQL MVC obsoletes usage of lock/unlock in mysql terms |
99 |
|
|
if (preg_match('/^\s*lock/is', $sql) || preg_match('/^\s*unlock/is', $sql)) { |
100 |
|
|
return 1; |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
# fix Mysql contact function into something SQL92 compliant |
104 |
|
|
$sql = preg_replace('/concat\(([^\,]+)\s*,\s*([^\)]+)\)/is', '\\1 || \\2', $sql); |
105 |
|
|
|
106 |
|
|
# convert Mysql password function to md5 |
107 |
|
|
$sql = preg_replace('/password\(([^\)]+)\)/is', 'md5(\\1)', $sql); |
108 |
|
|
|
109 |
|
|
# fix update statements to remove table name (which would be understood as |
110 |
|
|
# schama in PostgreSQL |
111 |
|
|
preg_match('/^\s*update\s+(\S+)\s+set/is', $sql, $table); |
112 |
|
|
if ($table) { |
113 |
|
|
$sql = preg_replace('/\s+'.$table[1].'\.(\w+)/is', ' \\1', $sql); |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
$sql = preg_replace('/password\(([^\)]+)\)/is', 'md5(\\1)', $sql); |
117 |
|
|
|
118 |
|
|
$GLOBALS["xx_last_sql"] = $sql; |
119 |
|
|
|
120 |
|
|
XXX_debug("xx_query [transformed]: $sql"); |
121 |
|
|
|
122 |
|
|
$ret = $con->query($sql); |
123 |
|
|
|
124 |
|
|
$GLOBALS["xx_element_nr $ret"] = 0; |
125 |
|
|
|
126 |
|
|
# $GLOBALS["pg_last_oid"] = pg_last_oid($ret); |
127 |
|
|
if ($ret) { |
128 |
|
|
return $ret; |
129 |
|
|
} else { |
130 |
|
|
print "WARNING: error executing SQL query '$sql'."; |
131 |
|
|
} |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
function xx_data_seek($rs, $element_nr) { |
135 |
|
|
$GLOBALS["xx_element_nr"] = $element_nr; |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
function xx_error() { |
139 |
|
|
global $db; |
140 |
|
|
|
141 |
|
|
# return $db->getUserInfo(); |
142 |
|
|
return $db->DebugInfo(); |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
?> |