/[libdata]/trunk/include/xx_pear_db.php
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/include/xx_pear_db.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 52 - (show annotations)
Fri Mar 5 23:38:19 2004 UTC (20 years, 1 month ago) by dpavlin
Original Path: branches/pear-db/my2pg/xx_pear_db.php
File size: 3941 byte(s)
check for errors in xx_query

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 $GLOBALS["xx_element_nr $ret"] = 0;
126
127 $ret = $con->query($sql);
128
129 if ($db->isError($ret)) {
130 #die($db->getUserInfo());
131 die($db->DebugInfo());
132 }
133
134 # $GLOBALS["pg_last_oid"] = pg_last_oid($ret);
135 if ($ret) {
136 return $ret;
137 } else {
138 print "WARNING: error executing SQL query '$sql'.";
139 }
140 }
141
142 function xx_data_seek($rs, $element_nr) {
143 $GLOBALS["xx_element_nr"] = $element_nr;
144 }
145
146 function xx_error() {
147 global $db;
148
149 # return $db->getUserInfo();
150 return $db->DebugInfo();
151 }
152
153 ?>

  ViewVC Help
Powered by ViewVC 1.1.26