/[libdata]/branches/pear-db/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

Annotation of /branches/pear-db/include/xx_pear_db.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 53 - (hide annotations)
Sat Mar 6 00:33:16 2004 UTC (20 years, 1 month ago) by dpavlin
Original Path: branches/pear-db/my2pg/xx_pear_db.php
File size: 4461 byte(s)
session should now insert values correctly

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 dpavlin 53 $db->getDebugInfo());
30 dpavlin 50 # XXX remove this!
31     print $db->getDebugInfo();
32     die($db->getMessage());
33     }
34     }
35    
36     function xx_errno() {
37     global $db;
38    
39     die ("xx_errno called");
40    
41     # XXX hmmm...
42     # check last object for error!
43     return 0;
44     }
45    
46     function xx_fetch_array ($rs) {
47     if ($GLOBALS["xx_element_nr $rs"] > $rs->numRows()) {
48     return;
49     }
50    
51 dpavlin 53 $arr = $rs->fetchRow(DB_FETCHMODE_ASSOC, $GLOBALS["xx_element_nr $rs"]);
52    
53 dpavlin 50 XXX_debug("xx_fetch_array ($rs) row: ".$GLOBALS["xx_element_nr $rs"]);
54    
55     $GLOBALS["xx_element_nr $rs"]++;
56     return $arr;
57     }
58    
59    
60     function xx_insert_id($con = 0) {
61 dpavlin 51
62     die ("xx_insert_id called");
63    
64 dpavlin 50 if ($con == 0) {
65     $con = $GLOBALS['xx_dbh'];
66     }
67     # this depends on premise that autoincrement field in tables
68     # is always first one. If would be better to search for field
69     # with sequence and return that, but it's too much work, and
70     # this seem to work most of the time
71     $oid = $GLOBALS["pg_last_oid"];
72     preg_match('/\s*insert\s+into\s+(\S+)\s+/is', $GLOBALS["xx_last_sql"], $table);
73     if ($table) {
74     $result = pg_query($con, "SELECT * FROM $table[1] where oid=$oid");
75     $arr = pg_fetch_array($result, 0, PGSQL_NUM);
76     XXX_debug("xx_insert_id (table: $table, oid: $oid): ".$arr[0]);
77     return $arr[0];
78     } else {
79     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!";
80     exit;
81     }
82     }
83    
84     function xx_num_rows($rs) {
85     $num = $rs->numRows();
86     XXX_debug("xx_num_rows: $num");
87     return $num;
88     }
89    
90 dpavlin 53 function filter_sql($sql) {
91 dpavlin 50
92     # PostgreSQL MVC obsoletes usage of lock/unlock in mysql terms
93     if (preg_match('/^\s*lock/is', $sql) || preg_match('/^\s*unlock/is', $sql)) {
94 dpavlin 53 return;
95 dpavlin 50 }
96    
97     # fix Mysql contact function into something SQL92 compliant
98     $sql = preg_replace('/concat\(([^\,]+)\s*,\s*([^\)]+)\)/is', '\\1 || \\2', $sql);
99    
100     # convert Mysql password function to md5
101     $sql = preg_replace('/password\(([^\)]+)\)/is', 'md5(\\1)', $sql);
102    
103     # fix update statements to remove table name (which would be understood as
104     # schama in PostgreSQL
105     preg_match('/^\s*update\s+(\S+)\s+set/is', $sql, $table);
106     if ($table) {
107     $sql = preg_replace('/\s+'.$table[1].'\.(\w+)/is', ' \\1', $sql);
108     }
109    
110     $sql = preg_replace('/password\(([^\)]+)\)/is', 'md5(\\1)', $sql);
111    
112 dpavlin 53 return $sql;
113     }
114    
115     function xx_query($sql, $con = 0) {
116     global $db;
117    
118     if ($con == 0) {
119     $con = $db;
120     XXX_debug("xx_query (fixed conn): $sql");
121     } else {
122     XXX_debug("xx_query (conn: $con): $sql");
123     }
124    
125     $sql =filter_sql($sql);
126     if (! isset($sql)) {
127     return;
128     }
129    
130 dpavlin 50 $GLOBALS["xx_last_sql"] = $sql;
131    
132     XXX_debug("xx_query [transformed]: $sql");
133    
134 dpavlin 52 $ret = $con->query($sql);
135 dpavlin 53 xx_iserror($ret);
136 dpavlin 52
137 dpavlin 53 $GLOBALS["xx_element_nr $ret"] = 0;
138 dpavlin 52
139 dpavlin 50 # $GLOBALS["pg_last_oid"] = pg_last_oid($ret);
140     if ($ret) {
141     return $ret;
142     } else {
143     print "WARNING: error executing SQL query '$sql'.";
144     }
145     }
146    
147 dpavlin 53 # check for error, if so, die!
148     function xx_iserror($ret, $message = "") {
149     global $db;
150     if ($db->isError($ret)) {
151     die($message . $ret->getDebugInfo());
152     }
153     }
154    
155     # prepare and execute statement in one function
156     function xx_prepare_execute() {
157     global $db;
158    
159     $args = func_get_args();
160     $sql = array_shift($args);
161    
162     XXX_debug("xx_prepare_execute: $sql [".join("|",$args)."]");
163    
164     $sth = $db->prepare($sql);
165     xx_iserror($sth);
166     $ret = $db->execute($sth, $args);
167     xx_iserror($ret);
168     return $ret;
169     }
170    
171 dpavlin 50 function xx_data_seek($rs, $element_nr) {
172     $GLOBALS["xx_element_nr"] = $element_nr;
173     }
174    
175     function xx_error() {
176     global $db;
177    
178     # return $db->getUserInfo();
179     return $db->DebugInfo();
180     }
181    
182     ?>

  ViewVC Help
Powered by ViewVC 1.1.26