/[libdata]/trunk/xx_pgsql.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/xx_pgsql.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 39 - (show annotations)
Thu Mar 4 21:42:17 2004 UTC (20 years ago) by dpavlin
File size: 3721 byte(s)
wrapper fuction from xx_ -> mysql_, and with more logic for PostgreSQL

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

  ViewVC Help
Powered by ViewVC 1.1.26