Commit bff53284dbc3b979e0d12b2cc5ccc4af18990f69

Authored by Neil Blakey-Milner
1 parent e34485e1

Remove phplib.


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3028 c91229c3-7414-0410-bfa2-8a42b809f60b
phplib/be_sql.inc deleted
1   -<?php
2   -
3   -##
4   -## Copyright (c) 1999-2000 Internet Images srl
5   -## Massimiliano Masserelli <negro@interim.it>
6   -##
7   -## $Id$
8   -##
9   -## PHPLIB Blob Engine using plain sql
10   -## depends on db_sql
11   -##
12   -## This is a "reference" class to be used only as an example. This should
13   -## not be used in applications.
14   -##
15   -## It's also a good skeleton for writing a new Blob Engine
16   -##
17   -## The table used by this class must contain three fields:
18   -## be_bid 16 chars
19   -## be_seq 6 chars
20   -## be_data 4096 chars
21   -
22   -
23   -class BE_Sql {
24   - ##
25   - ## Define following parameters by overwriting or by deriving
26   - ## your own class (recommended)
27   - ##
28   - var $database_class = "DB_Sql"; ## access table via this class
29   - var $database_table = "be_table"; ## write data into this table
30   - var $split_length = 4096; ## maximum amount of data for each row
31   - ## this value should be safe enough
32   - ## end of configuration
33   -
34   - var $db; ## Internal, object used for db connection
35   -
36   - function start() {
37   - $name = $this->database_class;
38   - $this->db = new $name;
39   - }
40   -
41   - function blob_create() {
42   - if (! is_object($this->db)) {
43   - $this->start();
44   - }
45   - $bn = false;
46   - $count = 0;
47   - while (! $bn && ($count++ < 10)) {
48   - $bn = uniqid("");
49   - $this->db->query(sprintf("INSERT INTO %s".
50   - " (be_bid, be_seq, be_data) ".
51   - " VALUES ".
52   - " ('%s', '%06d', '')", $this->database_table, $bn, 0));
53   - if ($this->db->affected_rows() != 1) {
54   - $bn = "";
55   - }
56   - }
57   - return $bn;
58   - }
59   -
60   - function blob_open($id) {
61   - if (! is_object($this->db)) {
62   - $this->start();
63   - }
64   - $this->db->query("SELECT count(*) FROM ". $this->database_table.
65   - " WHERE be_bid = '$id'");
66   - if ($this->db->next_record()) {
67   - return ($this->db->f(0) > 0);
68   - } else {
69   - return false;
70   - }
71   - }
72   -
73   - function blob_close($id) {
74   - return true;
75   - }
76   -
77   - function blob_delete($id) {
78   - if (! is_object($this->db)) {
79   - $this->start();
80   - }
81   - $this->db->query("DELETE FROM ". $this->database_table.
82   - " WHERE be_bid = '$id'");
83   - return true;
84   - }
85   -
86   - function blob_read($id) {
87   - if (! is_object($this->db)) {
88   - $this->start();
89   - }
90   - $str = "";
91   - $this->db->query("SELECT be_data, be_seq FROM ". $this->database_table.
92   - " WHERE be_bid = '$id' ORDER BY be_seq");
93   - while ($this->db->next_record()) {
94   - $str .= $this->db->f(0);
95   - }
96   - return base64_decode($str);
97   - }
98   -
99   - function blob_write($id, $data) {
100   - if (! is_object($this->db)) {
101   - $this->start();
102   - }
103   - $this->db->query("BEGIN TRANSACTION");
104   - $this->db->query("DELETE FROM ". $this->database_table.
105   - " WHERE be_bid = '$id'");
106   - $str = base64_encode($data);
107   - $seq = 0;
108   - while ($part = substr($str, 0, $this->split_length)) {
109   - $this->db->query(sprintf("INSERT INTO %s ".
110   - "(be_bid, be_seq, be_data) ".
111   - " VALUES ".
112   - "('%s', '%06d', '%s')",
113   - $this->database_table,
114   - $id,
115   - $seq++,
116   - $part));
117   - $str = substr($str, $this->split_length);
118   - }
119   - $this->db->query("END TRANSACTION");
120   - return true;
121   - }
122   -
123   -
124   -## function halt($s) {
125   -## echo "<b>$s</b>";
126   -## exit;
127   -## }
128   -
129   -}
130   -?>
phplib/ct_split_sql.inc deleted
1   -<?php
2   -##
3   -## Copyright (c) 1999-2000 Internet Images srl
4   -## Massimiliano Masserelli
5   -##
6   -## $Id$
7   -##
8   -## PHPLIB Data Storage Container using a SQL database and multiple
9   -## rows for each element
10   -##
11   -## Every session-name pair will end up in one OR MORE table rows, thus
12   -## allowing serialization of huge quantities of data.
13   -##
14   -
15   -class CT_Split_Sql {
16   - ##
17   - ## Define these parameters by overwriting or by
18   - ## deriving your own class from it (recommened)
19   - ##
20   -
21   - var $database_table = "active_sessions_split";
22   - var $database_class = "";
23   - var $database_lock_semaphore = "";
24   - var $split_length = 4096; ## Split data every xxx bytes
25   -
26   - ## The only supported storage method is base64 encoding
27   - ## end of configuration
28   -
29   - var $db;
30   -
31   - function ac_start() {
32   - $name = $this->database_class;
33   - $this->db = new $name;
34   - }
35   -
36   - function ac_get_lock() {
37   - if ( "" != $this->database_lock_semaphore ) {
38   - while ( ! $this->db->query("SELECT get_lock('%s')",
39   - $this->database_lock_semaphore) ) {
40   - $t = 1 + time(); while ( $t > time() ) { ; }
41   - }
42   - }
43   - }
44   -
45   - function ac_release_lock() {
46   - if ( "" != $this->database_lock_semaphore ) {
47   - $this->db->query("SELECT release_lock('%s')",
48   - $this->database_lock_semaphore);
49   - }
50   - }
51   -
52   - function ac_gc($gc_time, $name) {
53   - $timeout = time();
54   - $sqldate = date("YmdHis", $timeout - ($gc_time * 60));
55   - $this->db->query(sprintf("DELETE FROM %s ".
56   - "WHERE ct_changed < '%s' AND ct_name = '%s'",
57   - $this->database_table,
58   - $sqldate,
59   - addslashes($name)));
60   - }
61   -
62   - function ac_store($id, $name, $str) {
63   - $ret = true;
64   - $str = base64_encode($str);
65   - $name = addslashes($name);
66   - $now = date("YmdHis", time());
67   - $this->db->query("BEGIN TRANSACTION");
68   - $this->db->query(sprintf("DELETE FROM %s WHERE ct_sid='%s' AND ct_name='%s'",
69   - $this->database_table,
70   - $id,
71   - $name
72   - ));
73   - $count = 0;
74   - while ($part = substr($str, 0, $this->split_length)) {
75   - $this->db->query(sprintf("INSERT INTO %s ".
76   - " (ct_sid, ct_name, ct_pos, ct_val, ct_changed) ".
77   - " VALUES ".
78   - " ('%s','%s','%06d','%s','%s')",
79   - $this->database_table,
80   - $id,
81   - $name,
82   - $count++,
83   - $part,
84   - $now
85   - ));
86   - $str = substr($str, $this->split_length);
87   - }
88   - $this->db->query("END TRANSACTION");
89   - return $ret;
90   - }
91   -
92   - function ac_delete($id, $name) {
93   - $this->db->query(sprintf("DELETE FROM %s ".
94   - "WHERE ct_name = '%s' AND ct_sid = '%s'",
95   - $this->database_table,
96   - addslashes($name),
97   - $id));
98   - }
99   -
100   - function ac_get_value($id, $name) {
101   - $this->db->query(sprintf("SELECT ct_val, ct_pos FROM %s ".
102   - "WHERE ct_sid = '%s' AND ct_name = '%s' ".
103   - "ORDER BY ct_pos",
104   - $this->database_table,
105   - $id,
106   - addslashes($name)));
107   - $str="";
108   - while ($this->db->next_record()) {
109   - $str .= $this->db->f("ct_val");
110   - }
111   - if (! empty($str)) {
112   - $str = base64_decode($str);
113   - };
114   -## DEB echo $str;
115   - return $str;
116   - }
117   -
118   - function ac_newid($str, $name) {
119   - return $str;
120   - }
121   -
122   - function ac_halt($s) {
123   - $this->db->halt($s);
124   - }
125   -}
126   -?>
phplib/ct_sql.inc deleted
1   -<?php
2   -
3   -##
4   -## Copyright (c) 1998-2000 NetUSE AG
5   -## Boris Erdmann, Kristian Koehntopp
6   -##
7   -## Copyright (c) 1998-2000 Sascha Schumann <sascha@schumann.cx>
8   -##
9   -## $Id$
10   -##
11   -## PHPLIB Data Storage Container using a SQL database
12   -##
13   -
14   -class CT_Sql {
15   - ##
16   - ## Define these parameters by overwriting or by
17   - ## deriving your own class from it (recommened)
18   - ##
19   -
20   - var $database_table = "active_sessions";
21   - var $database_class = "Owl_Sql";
22   - var $database_lock_semaphore = "";
23   -
24   - var $encoding_mode = "base64";
25   -
26   - ## end of configuration
27   -
28   - var $db;
29   -
30   - function ac_start() {
31   - $name = $this->database_class;
32   - $this->db = new $name;
33   - }
34   -
35   - function ac_get_lock() {
36   - if ( "" != $this->database_lock_semaphore ) {
37   - $query = sprintf("SELECT get_lock('%s')", $this->database_lock_semaphore);
38   - while ( ! $this->db->query($query)) {
39   - $t = 1 + time(); while ( $t > time() ) { ; }
40   - }
41   - }
42   - }
43   -
44   - function ac_release_lock() {
45   - if ( "" != $this->database_lock_semaphore ) {
46   - $query = sprintf("SELECT release_lock('%s')", $this->database_lock_semaphore);
47   - $this->db->query($query);
48   - }
49   - }
50   -
51   - function ac_gc($gc_time, $name) {
52   - $timeout = time();
53   - $sqldate = date("YmdHis", $timeout - ($gc_time * 60));
54   - $this->db->query(sprintf("DELETE FROM %s WHERE changed < '%s' AND name = '%s'",
55   - $this->database_table,
56   - $sqldate,
57   - addslashes($name)));
58   - }
59   -
60   - function ac_store($id, $name, $str) {
61   - $ret = true;
62   -
63   - switch ( $this->encoding_mode ) {
64   - case "slashes":
65   - $str = addslashes($name . ":" . $str);
66   - break;
67   -
68   - case "base64":
69   - default:
70   - $str = base64_encode($name . ":" . $str);
71   - };
72   -
73   - $name = addslashes($name);
74   -
75   - ## update duration of visit
76   - global $HTTP_REFERER, $HTTP_USER_AGENT, $REMOTE_ADDR;
77   -
78   - $now = date("YmdHis", time());
79   - $uquery = sprintf("update %s set val='%s', changed='%s' where sid='%s' and name='%s'",
80   - $this->database_table,
81   - $str,
82   - $now,
83   - $id,
84   - $name);
85   - $squery = sprintf("select count(*) from %s where val='%s' and changed='%s' and sid='%s' and name='%s'",
86   - $this->database_table,
87   - $str,
88   - $now,
89   - $id,
90   - $name);
91   - $iquery = sprintf("insert into %s ( sid, name, val, changed ) values ('%s', '%s', '%s', '%s')",
92   - $this->database_table,
93   - $id,
94   - $name,
95   - $str,
96   - $now);
97   -
98   - $this->db->query($uquery);
99   -
100   - # FIRST test to see if any rows were affected.
101   - # Zero rows affected could mean either there were no matching rows
102   - # whatsoever, OR that the update statement did match a row but made
103   - # no changes to the table data (i.e. UPDATE tbl SET col = 'x', when
104   - # "col" is _already_ set to 'x') so then,
105   - # SECOND, query(SELECT...) on the sid to determine if the row is in
106   - # fact there,
107   - # THIRD, verify that there is at least one row present, and if there
108   - # is not, then
109   - # FOURTH, insert the row as we've determined that it does not exist.
110   -
111   - if ( $this->db->affected_rows() == 0
112   - && $this->db->query($squery)
113   - && $this->db->f(1) == 0
114   - && !$this->db->query($iquery)) {
115   -
116   - $ret = false;
117   - }
118   - return $ret;
119   - }
120   -
121   - function ac_delete($id, $name) {
122   - $this->db->query(sprintf("delete from %s where name = '%s' and sid = '%s'",
123   - $this->database_table,
124   - addslashes($name),
125   - $id));
126   - }
127   -
128   - function ac_get_value($id, $name) {
129   - $this->db->query(sprintf("select val from %s where sid = '%s' and name = '%s'",
130   - $this->database_table,
131   - $id,
132   - addslashes($name)));
133   - if ($this->db->next_record()) {
134   - $str = $this->db->f("val");
135   - $str2 = base64_decode( $str );
136   -
137   - if ( ereg("^".$name.":.*", $str2) ) {
138   - $str = ereg_replace("^".$name.":", "", $str2 );
139   - } else {
140   -
141   - $str3 = stripslashes( $str );
142   -
143   - if ( ereg("^".$name.":.*", $str3) ) {
144   - $str = ereg_replace("^".$name.":", "", $str3 );
145   - } else {
146   -
147   - switch ( $this->encoding_mode ) {
148   - case "slashes":
149   - $str = stripslashes($str);
150   - break;
151   -
152   - case "base64":
153   - default:
154   - $str = base64_decode($str);
155   - }
156   - }
157   - };
158   - return $str;
159   - };
160   - return "";
161   - }
162   -
163   - function ac_newid($str, $name) {
164   - return $str;
165   - }
166   -
167   - function ac_halt($s) {
168   - $this->db->halt($s);
169   - }
170   -}
171   -?>
phplib/db_msql.inc deleted
1   -<?php
2   -/*
3   - * Session Management for PHP3
4   - *
5   - * Copyright (c) 1998-2000 NetUSE AG
6   - * Boris Erdmann, Kristian Koehntopp
7   - *
8   - * Derived from db_mysql.inc by Sascha Schumann <sascha@schumann.cx>
9   - *
10   - * $Id$
11   - *
12   - */
13   -
14   -class DB_Sql {
15   - var $Host = "";
16   - var $Database = "";
17   -
18   - var $Link_ID = 0;
19   - var $Query_ID = 0;
20   - var $Record = array();
21   - var $Row;
22   -
23   - var $Error = "";
24   -
25   - var $Auto_Free = 0; ## Set this to 1 for automatic msql_free_result()
26   -
27   - /* public: constructor */
28   - function DB_Sql($query = "") {
29   - $this->query($query);
30   - }
31   -
32   - function connect() {
33   - // Not connected? Then connect?
34   - if ( 0 == $this->Link_ID ) {
35   - // Check for local connect
36   - $this->Link_ID = empty($this->Host)?
37   - $this->Link_ID=msql_pconnect():
38   - $this->Link_ID=msql_pconnect($this->Host);
39   - }
40   -
41   - // Still not connected? Raise error.
42   - if ( 0 == $this->Link_ID ) {
43   - $this->halt("Link-ID == false, pconnect failed");
44   - }
45   -
46   - // Select current database
47   - if (!msql_select_db($this->Database, $this->Link_ID)) {
48   - $this->halt("cannot use database ".$this->Database);
49   - }
50   - }
51   -
52   - function query($Query_String) {
53   -
54   - /* No empty queries, please, since PHP4 chokes on them. */
55   - if ($Query_String == "")
56   - /* The empty query string is passed on from the constructor,
57   - * when calling the class without a query, e.g. in situations
58   - * like these: '$db = new DB_Sql_Subclass;'
59   - */
60   - return 0;
61   -
62   - $this->connect();
63   -
64   -# printf("Debug: query = %s<br>\n", $Query_String);
65   -
66   - $this->Query_ID = msql_query($Query_String,$this->Link_ID);
67   - $this->Row = 0;
68   - $this->Error = msql_error();
69   - if (!$this->Query_ID) {
70   - $this->halt("Invalid SQL: ".$Query_String);
71   - }
72   -
73   - return $this->Query_ID;
74   - }
75   -
76   - function next_record() {
77   - $this->Record = msql_fetch_array($this->Query_ID);
78   - $this->Row += 1;
79   - $this->Error = msql_error();
80   -
81   - $stat = is_array($this->Record);
82   - if (!$stat && $this->Auto_Free) {
83   - msql_free_result($this->Query_ID);
84   - $this->Query_ID = 0;
85   - }
86   - return $stat;
87   - }
88   -
89   - function seek($pos) {
90   - $status = msql_data_seek($this->Query_ID, $pos);
91   - if ($status)
92   - $this->Row = $pos;
93   - return;
94   - }
95   -
96   - function metadata($table) {
97   - $count = 0;
98   - $id = 0;
99   - $res = array();
100   -
101   - $this->connect();
102   - $id = @msql_list_fields($this->Database, $table);
103   - if ($id < 0) {
104   - $this->Error = msql_error();
105   - $this->halt("Metadata query failed.");
106   - }
107   - $count = msql_num_fields($id);
108   -
109   - for ($i=0; $i<$count; $i++) {
110   - $res[$i]["table"] = msql_fieldtable ($id, $i);
111   - $res[$i]["name"] = msql_fieldname ($id, $i);
112   - $res[$i]["type"] = msql_fieldtype ($id, $i);
113   - $res[$i]["len"] = msql_fieldlen ($id, $i);
114   - $res[$i]["flags"] = msql_fieldflags ($id, $i);
115   - $res["meta"][$res[$i]["name"]] = $i;
116   - $res["num_fields"]= $count;
117   - }
118   -
119   - msql_free_result($id);
120   - return $res;
121   - }
122   -
123   - function affected_rows() {
124   - return msql_affected_rows($this->Query_ID);
125   - }
126   -
127   - function num_rows() {
128   - return msql_num_rows($this->Query_ID);
129   - }
130   -
131   - function num_fields() {
132   - return msql_num_fields($this->Query_ID);
133   - }
134   -
135   - function nf() {
136   - return $this->num_rows();
137   - }
138   -
139   - function np() {
140   - print $this->num_rows();
141   - }
142   -
143   - function f($Name) {
144   - return $this->Record[$Name];
145   - }
146   -
147   - function p($Name) {
148   - print $this->Record[$Name];
149   - }
150   -
151   - function halt($msg) {
152   - printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
153   - printf("<b>MSQL Error</b>: %s<br>\n", $this->Error);
154   - die("Session halted.");
155   - }
156   -}
157   -?>
phplib/db_mssql.inc deleted
1   -<?php
2   -/*
3   - * Session Management for PHP3
4   - *
5   - * (C) Copyright 1998 Cameron Taggart (cameront@wolfenet.com)
6   - * Modified by Guarneri carmelo (carmelo@melting-soft.com)
7   - * Modified by Cameron Just (C.Just@its.uq.edu.au)
8   - *
9   - * $Id$
10   - */
11   -# echo "<BR>This is using the MSSQL class<BR>";
12   -
13   -class DB_Sql {
14   - var $Host = "";
15   - var $Database = "";
16   - var $User = "";
17   - var $Password = "";
18   -
19   - var $Link_ID = 0;
20   - var $Query_ID = 0;
21   - var $Record = array();
22   - var $Row = 0;
23   -
24   - var $Errno = 0;
25   - var $Error = "";
26   -
27   - var $Auto_Free = 0; ## set this to 1 to automatically free results
28   -
29   -
30   - /* public: constructor */
31   - function DB_Sql($query = "") {
32   - $this->query($query);
33   - }
34   -
35   - function connect() {
36   - if ( 0 == $this->Link_ID ) {
37   - $this->Link_ID=mssql_pconnect($this->Host, $this->User, $this->Password);
38   - if (!$this->Link_ID)
39   - $this->halt("Link-ID == false, mssql_pconnect failed");
40   - else
41   - mssql_select_db($this->Database, $this->Link_ID);
42   - }
43   - }
44   - function free_result(){
45   - mssql_free_result($this->Query_ID);
46   - $this->Query_ID = 0;
47   - }
48   -
49   - function query($Query_String)
50   - {
51   -
52   - /* No empty queries, please, since PHP4 chokes on them. */
53   - if ($Query_String == "")
54   - /* The empty query string is passed on from the constructor,
55   - * when calling the class without a query, e.g. in situations
56   - * like these: '$db = new DB_Sql_Subclass;'
57   - */
58   - return 0;
59   -
60   - if (!$this->Link_ID)
61   - $this->connect();
62   -
63   -# printf("<br>Debug: query = %s<br>\n", $Query_String);
64   -
65   - $this->Query_ID = mssql_query($Query_String, $this->Link_ID);
66   - $this->Row = 0;
67   - if (!$this->Query_ID) {
68   - $this->Errno = 1;
69   - $this->Error = "General Error (The MSSQL interface cannot return detailed error messages).";
70   - $this->halt("Invalid SQL: ".$Query_String);
71   - }
72   - return $this->Query_ID;
73   - }
74   -
75   - function next_record() {
76   -
77   - if ($this->Record = mssql_fetch_row($this->Query_ID)) {
78   - // add to Record[<key>]
79   - $count = mssql_num_fields($this->Query_ID);
80   - for ($i=0; $i<$count; $i++){
81   - $fieldinfo = mssql_fetch_field($this->Query_ID,$i);
82   - $this->Record[strtolower($fieldinfo->name)] = $this->Record[$i];
83   - }
84   - $this->Row += 1;
85   - $stat = 1;
86   - } else {
87   - if ($this->Auto_Free) {
88   - $this->free_result();
89   - }
90   - $stat = 0;
91   - }
92   - return $stat;
93   - }
94   -
95   - function seek($pos) {
96   - mssql_data_seek($this->Query_ID,$pos);
97   - $this->Row = $pos;
98   - }
99   -
100   - function metadata($table) {
101   - $count = 0;
102   - $id = 0;
103   - $res = array();
104   -
105   - $this->connect();
106   - $id = mssql_query("select * from $table", $this->Link_ID);
107   - if (!$id) {
108   - $this->Errno = 1;
109   - $this->Error = "General Error (The MSSQL interface cannot return detailed error messages).";
110   - $this->halt("Metadata query failed.");
111   - }
112   - $count = mssql_num_fields($id);
113   -
114   - for ($i=0; $i<$count; $i++) {
115   - $info = mssql_fetch_field($id, $i);
116   - $res[$i]["table"] = $table;
117   - $res[$i]["name"] = $info["name"];
118   - $res[$i]["len"] = $info["max_length"];
119   - $res[$i]["flags"] = $info["numeric"];
120   - }
121   - $this->free_result();
122   - return $res;
123   - }
124   -
125   - function affected_rows() {
126   - return mssql_affected_rows($this->Query_ID);
127   - }
128   -
129   - function num_rows() {
130   - return mssql_num_rows($this->Query_ID);
131   - }
132   -
133   - function num_fields() {
134   - return mssql_num_fields($this->Query_ID);
135   - }
136   -
137   - function nf() {
138   - return $this->num_rows();
139   - }
140   -
141   - function np() {
142   - print $this->num_rows();
143   - }
144   -
145   - function f($Field_Name) {
146   - return $this->Record[strtolower($Field_Name)];
147   - }
148   -
149   - function p($Field_Name) {
150   - print $this->f($Field_Name);
151   - }
152   -
153   - function halt($msg) {
154   - printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
155   - printf("<b>MSSQL Error</b>: %s (%s)<br>\n",
156   - $this->Errno,
157   - $this->Error);
158   - die("Session halted.");
159   - }
160   -}
161   -?>
phplib/db_mysql.inc deleted
1   -<?php
2   -/*
3   - * Session Management for PHP3
4   - *
5   - * Copyright (c) 1998-2000 NetUSE AG
6   - * Boris Erdmann, Kristian Koehntopp
7   - *
8   - * $Id$
9   - *
10   - */
11   -
12   -class DB_Sql {
13   -
14   - /* public: connection parameters */
15   - var $Host = "";
16   - var $Database = "";
17   - var $User = "";
18   - var $Password = "";
19   -
20   - /* public: configuration parameters */
21   - var $Auto_Free = 0; ## Set to 1 for automatic mysql_free_result()
22   - var $Debug = 0; ## Set to 1 for debugging messages.
23   - var $Halt_On_Error = "yes"; ## "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning)
24   - var $Seq_Table = "db_sequence";
25   -
26   - /* public: result array and current row number */
27   - var $Record = array();
28   - var $Row;
29   -
30   - /* public: current error number and error text */
31   - var $Errno = 0;
32   - var $Error = "";
33   -
34   - /* public: this is an api revision, not a CVS revision. */
35   - var $type = "mysql";
36   - var $revision = "1.2";
37   -
38   - /* private: link and query handles */
39   - var $Link_ID = 0;
40   - var $Query_ID = 0;
41   -
42   -
43   -
44   - /* public: constructor */
45   - function DB_Sql($query = "") {
46   - $this->query($query);
47   - }
48   -
49   - /* public: some trivial reporting */
50   - function link_id() {
51   - return $this->Link_ID;
52   - }
53   -
54   - function query_id() {
55   - return $this->Query_ID;
56   - }
57   -
58   - /* public: connection management */
59   - function connect($Database = "", $Host = "", $User = "", $Password = "") {
60   - /* Handle defaults */
61   - if ("" == $Database)
62   - $Database = $this->Database;
63   - if ("" == $Host)
64   - $Host = $this->Host;
65   - if ("" == $User)
66   - $User = $this->User;
67   - if ("" == $Password)
68   - $Password = $this->Password;
69   -
70   - /* establish connection, select database */
71   - if ( 0 == $this->Link_ID ) {
72   -
73   - $this->Link_ID=mysql_pconnect($Host, $User, $Password);
74   - if (!$this->Link_ID) {
75   - $this->halt("pconnect($Host, $User, \$Password) failed.");
76   - return 0;
77   - }
78   -
79   - if (!@mysql_select_db($Database,$this->Link_ID)) {
80   - $this->halt("cannot use database ".$this->Database);
81   - return 0;
82   - }
83   - }
84   -
85   - return $this->Link_ID;
86   - }
87   -
88   - /* public: discard the query result */
89   - function free() {
90   - @mysql_free_result($this->Query_ID);
91   - $this->Query_ID = 0;
92   - }
93   -
94   - /* public: perform a query */
95   - function query($Query_String) {
96   - global $default;
97   - // only log the queries if we're in debug mode
98   - if ($default->minimumLoggingLevel == DEBUG) {
99   - require_once("$default->fileSystemRoot/phpSniff/phpTimer.class.php");
100   - $timer = new phpTimer();
101   - $timer->start();
102   - }
103   -
104   - /* No empty queries, please, since PHP4 chokes on them. */
105   - if ($Query_String == "")
106   - /* The empty query string is passed on from the constructor,
107   - * when calling the class without a query, e.g. in situations
108   - * like these: '$db = new DB_Sql_Subclass;'
109   - */
110   - return 0;
111   -
112   - if (!$this->connect()) {
113   - return 0; /* we already complained in connect() about that. */
114   - };
115   -
116   - # New query, discard previous result.
117   - if ($this->Query_ID) {
118   - $this->free();
119   - }
120   -
121   - if ($this->Debug)
122   - printf("Debug: query = %s<br>\n", $Query_String);
123   -
124   - $this->Query_ID = @mysql_query($Query_String,$this->Link_ID);
125   - $this->Row = 0;
126   - $this->Errno = mysql_errno();
127   - $this->Error = mysql_error();
128   - if (!$this->Query_ID) {
129   - $this->halt("Invalid SQL: ".$Query_String);
130   - }
131   -
132   - # Will return nada if it fails. That's fine.
133   - if ($default->minimumLoggingLevel == DEBUG) {
134   - $timer->stop();
135   - if ($fp = fopen($default->fileSystemRoot . "/log/queryLog.txt", "a")) {
136   - fwrite($fp, $timer->get_current() . ": $Query_String\n");
137   - }
138   - fclose($fp);
139   - }
140   - return $this->Query_ID;
141   - }
142   -
143   - // BEGIN wes addition
144   - function insert_id() {
145   - return mysql_insert_id($this->Link_ID);
146   - }
147   - // END wes addition
148   -
149   - /* public: walk result set */
150   - function next_record() {
151   - if (!$this->Query_ID) {
152   - $this->halt("next_record called with no query pending.");
153   - return 0;
154   - }
155   -
156   - $this->Record = @mysql_fetch_array($this->Query_ID);
157   - $this->Row += 1;
158   - $this->Errno = mysql_errno();
159   - $this->Error = mysql_error();
160   -
161   - $stat = is_array($this->Record);
162   - if (!$stat && $this->Auto_Free) {
163   - $this->free();
164   - }
165   - return $stat;
166   - }
167   -
168   - /* public: position in result set */
169   - function seek($pos = 0) {
170   - $status = @mysql_data_seek($this->Query_ID, $pos);
171   - if ($status)
172   - $this->Row = $pos;
173   - else {
174   - $this->halt("seek($pos) failed: result has ".$this->num_rows()." rows");
175   -
176   - /* half assed attempt to save the day,
177   - * but do not consider this documented or even
178   - * desireable behaviour.
179   - */
180   - @mysql_data_seek($this->Query_ID, $this->num_rows());
181   - $this->Row = $this->num_rows;
182   - return 0;
183   - }
184   -
185   - return 1;
186   - }
187   -
188   - /* public: table locking */
189   - function lock($table, $mode="write") {
190   - $this->connect();
191   -
192   - $query="lock tables ";
193   - if (is_array($table)) {
194   - while (list($key,$value)=each($table)) {
195   - if ($key=="read" && $key!=0) {
196   - $query.="$value read, ";
197   - } else {
198   - $query.="$value $mode, ";
199   - }
200   - }
201   - $query=substr($query,0,-2);
202   - } else {
203   - $query.="$table $mode";
204   - }
205   - $res = @mysql_query($query, $this->Link_ID);
206   - if (!$res) {
207   - $this->halt("lock($table, $mode) failed.");
208   - return 0;
209   - }
210   - return $res;
211   - }
212   -
213   - function unlock() {
214   - $this->connect();
215   -
216   - $res = @mysql_query("unlock tables");
217   - if (!$res) {
218   - $this->halt("unlock() failed.");
219   - return 0;
220   - }
221   - return $res;
222   - }
223   -
224   -
225   - /* public: evaluate the result (size, width) */
226   - function affected_rows() {
227   - return @mysql_affected_rows($this->Link_ID);
228   - }
229   -
230   - function num_rows() {
231   - return @mysql_num_rows($this->Query_ID);
232   - }
233   -
234   - function num_fields() {
235   - return @mysql_num_fields($this->Query_ID);
236   - }
237   -
238   - /* public: shorthand notation */
239   - function nf() {
240   - return $this->num_rows();
241   - }
242   -
243   - function np() {
244   - print $this->num_rows();
245   - }
246   -
247   - function f($Name) {
248   - return $this->Record[$Name];
249   - }
250   -
251   - function p($Name) {
252   - print $this->Record[$Name];
253   - }
254   -
255   - /* public: sequence numbers */
256   - function nextid($seq_name) {
257   - $this->connect();
258   -
259   - if ($this->lock($this->Seq_Table)) {
260   - /* get sequence number (locked) and increment */
261   - $q = sprintf("select nextid from %s where seq_name = '%s'",
262   - $this->Seq_Table,
263   - $seq_name);
264   - $id = @mysql_query($q, $this->Link_ID);
265   - $res = @mysql_fetch_array($id);
266   -
267   - /* No current value, make one */
268   - if (!is_array($res)) {
269   - $currentid = 0;
270   - $q = sprintf("insert into %s values('%s', %s)",
271   - $this->Seq_Table,
272   - $seq_name,
273   - $currentid);
274   - $id = @mysql_query($q, $this->Link_ID);
275   - } else {
276   - $currentid = $res["nextid"];
277   - }
278   - $nextid = $currentid + 1;
279   - $q = sprintf("update %s set nextid = '%s' where seq_name = '%s'",
280   - $this->Seq_Table,
281   - $nextid,
282   - $seq_name);
283   - $id = @mysql_query($q, $this->Link_ID);
284   - $this->unlock();
285   - } else {
286   - $this->halt("cannot lock ".$this->Seq_Table." - has it been created?");
287   - return 0;
288   - }
289   - return $nextid;
290   - }
291   -
292   - /* public: return table metadata */
293   - function metadata($table='',$full=false) {
294   - $count = 0;
295   - $id = 0;
296   - $res = array();
297   -
298   - /*
299   - * Due to compatibility problems with Table we changed the behavior
300   - * of metadata();
301   - * depending on $full, metadata returns the following values:
302   - *
303   - * - full is false (default):
304   - * $result[]:
305   - * [0]["table"] table name
306   - * [0]["name"] field name
307   - * [0]["type"] field type
308   - * [0]["len"] field length
309   - * [0]["flags"] field flags
310   - *
311   - * - full is true
312   - * $result[]:
313   - * ["num_fields"] number of metadata records
314   - * [0]["table"] table name
315   - * [0]["name"] field name
316   - * [0]["type"] field type
317   - * [0]["len"] field length
318   - * [0]["flags"] field flags
319   - * ["meta"][field name] index of field named "field name"
320   - * The last one is used, if you have a field name, but no index.
321   - * Test: if (isset($result['meta']['myfield'])) { ...
322   - */
323   -
324   - // if no $table specified, assume that we are working with a query
325   - // result
326   - if ($table) {
327   - $this->connect();
328   - $id = @mysql_list_fields($this->Database, $table);
329   - if (!$id)
330   - $this->halt("Metadata query failed.");
331   - } else {
332   - $id = $this->Query_ID;
333   - if (!$id)
334   - $this->halt("No query specified.");
335   - }
336   -
337   - $count = @mysql_num_fields($id);
338   -
339   - // made this IF due to performance (one if is faster than $count if's)
340   - if (!$full) {
341   - for ($i=0; $i<$count; $i++) {
342   - $res[$i]["table"] = @mysql_field_table ($id, $i);
343   - $res[$i]["name"] = @mysql_field_name ($id, $i);
344   - $res[$i]["type"] = @mysql_field_type ($id, $i);
345   - $res[$i]["len"] = @mysql_field_len ($id, $i);
346   - $res[$i]["flags"] = @mysql_field_flags ($id, $i);
347   - }
348   - } else { // full
349   - $res["num_fields"]= $count;
350   -
351   - for ($i=0; $i<$count; $i++) {
352   - $res[$i]["table"] = @mysql_field_table ($id, $i);
353   - $res[$i]["name"] = @mysql_field_name ($id, $i);
354   - $res[$i]["type"] = @mysql_field_type ($id, $i);
355   - $res[$i]["len"] = @mysql_field_len ($id, $i);
356   - $res[$i]["flags"] = @mysql_field_flags ($id, $i);
357   - $res["meta"][$res[$i]["name"]] = $i;
358   - }
359   - }
360   -
361   - // free the result only if we were called on a table
362   - if ($table) @mysql_free_result($id);
363   - return $res;
364   - }
365   -
366   - /* private: error handling */
367   - function halt($msg) {
368   - $this->Error = @mysql_error($this->Link_ID);
369   - $this->Errno = @mysql_errno($this->Link_ID);
370   - if ($this->Halt_On_Error == "no")
371   - return;
372   -
373   - $this->haltmsg($msg);
374   -
375   - if ($this->Halt_On_Error != "report")
376   - die("Session halted.");
377   - }
378   -
379   - function haltmsg($msg) {
380   - printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
381   - printf("<b>MySQL Error</b>: %s (%s)<br>\n",
382   - $this->Errno,
383   - $this->Error);
384   - }
385   -
386   - function table_names() {
387   - $this->query("SHOW TABLES");
388   - $i=0;
389   - while ($info=mysql_fetch_row($this->Query_ID))
390   - {
391   - $return[$i]["table_name"]= $info[0];
392   - $return[$i]["tablespace_name"]=$this->Database;
393   - $return[$i]["database"]=$this->Database;
394   - $i++;
395   - }
396   - return $return;
397   - }
398   -}
399   -?>
phplib/db_oci8.inc deleted
1   -<?php
2   -
3   -/*
4   - * Oracle/OCI8 accessor based on Session Management for PHP3
5   - *
6   - * (C) Copyright 1999-2000 Stefan Sels phplib@sels.com
7   - *
8   - * based on db_oracle.inc by Luis Francisco Gonzalez Hernandez
9   - * contains metadata() from db_oracle.inc 1.10
10   - *
11   - * $Id$
12   - *
13   - */
14   -
15   -class DB_Sql {
16   - var $Debug = 0;
17   - var $sqoe = 1; // sqoe= show query on error
18   -
19   - var $Database = "";
20   - var $User = "";
21   - var $Password = "";
22   -
23   - var $Link_ID = 0;
24   - var $Record = array();
25   - var $Row;
26   - var $Parse;
27   - var $Error = "";
28   -
29   - /* public: constructor */
30   - function DB_Sql($query = "") {
31   - $this->query($query);
32   - }
33   -
34   - function connect() {
35   - if ( 0 == $this->Link_ID ) {
36   - if($this->Debug) {
37   - printf("<br>Connecting to $this->Database...<br>\n");
38   - }
39   - $this->Link_ID=OCIplogon
40   - ("$this->User","$this->Password","$this->Database");
41   -
42   - if (!$this->Link_ID) {
43   - $this->halt("Link-ID == false " .
44   - "($this->Link_ID), OCILogon failed");
45   - }
46   -
47   - if($this->Debug) {
48   - printf("<br>Obtained the Link_ID: $this->Link_ID<br>\n");
49   - }
50   - }
51   - }
52   -
53   - function query($Query_String) {
54   -
55   - /* No empty queries, please, since PHP4 chokes on them. */
56   - if ($Query_String == "")
57   - /* The empty query string is passed on from the constructor,
58   - * when calling the class without a query, e.g. in situations
59   - * like these: '$db = new DB_Sql_Subclass;'
60   - */
61   - return 0;
62   -
63   -
64   - $this->connect();
65   -
66   - $this->Parse=OCIParse($this->Link_ID,$Query_String);
67   - if(!$this->Parse) {
68   - $this->Error=OCIError($this->Parse);
69   - } else { OCIExecute($this->Parse);
70   - $this->Error=OCIError($this->Parse);
71   - }
72   -
73   - $this->Row=0;
74   -
75   - if($this->Debug) {
76   - printf("Debug: query = %s<br>\n", $Query_String);
77   - }
78   -
79   - if ($this->Error["code"]!=1403 && $this->Error["code"]!=0 && $this->sqoe)
80   - echo "<BR><FONT color=red><B>".$this->Error["message"]."<BR>Query :\"$Query_String\"</B></FONT>";
81   - return $this->Parse;
82   - }
83   -
84   - function next_record() {
85   - if(0 == OCIFetchInto($this->Parse,$result,OCI_ASSOC+OCI_RETURN_NULLS)) {
86   - if ($this->Debug) {
87   - printf("<br>ID: %d,Rows: %d<br>\n",
88   - $this->Link_ID,$this->num_rows());
89   - }
90   - $this->Row +=1;
91   -
92   - $errno=OCIError($this->Parse);
93   - if(1403 == $errno) { # 1043 means no more records found
94   - $this->Error="";
95   - $this->disconnect();
96   - $stat=0;
97   - } else {
98   - $this->Error=OCIError($this->Parse);
99   - if($this->Debug) {
100   - printf("<br>Error: %s",
101   - $this->Error["message"]);
102   - }
103   - $stat=0;
104   - }
105   - } else {
106   - for($ix=1;$ix<=OCINumcols($this->Parse);$ix++) {
107   - $col=strtoupper(OCIColumnname($this->Parse,$ix));
108   - $colreturn=strtolower($col);
109   - $this->Record[ "$colreturn" ] = $result["$col"];
110   - if($this->Debug) echo"<b>[$col]</b>:".$result["$col"]."<br>\n";
111   - }
112   - $stat=1;
113   - }
114   -
115   - return $stat;
116   - }
117   -
118   - function seek($pos) {
119   - $this->Row=$pos;
120   - }
121   -
122   - function metadata($table,$full=false) {
123   - $count = 0;
124   - $id = 0;
125   - $res = array();
126   -
127   - /*
128   - * Due to compatibility problems with Table we changed the behavior
129   - * of metadata();
130   - * depending on $full, metadata returns the following values:
131   - *
132   - * - full is false (default):
133   - * $result[]:
134   - * [0]["table"] table name
135   - * [0]["name"] field name
136   - * [0]["type"] field type
137   - * [0]["len"] field length
138   - * [0]["flags"] field flags ("NOT NULL", "INDEX")
139   - * [0]["format"] precision and scale of number (eg. "10,2") or empty
140   - * [0]["index"] name of index (if has one)
141   - * [0]["chars"] number of chars (if any char-type)
142   - *
143   - * - full is true
144   - * $result[]:
145   - * ["num_fields"] number of metadata records
146   - * [0]["table"] table name
147   - * [0]["name"] field name
148   - * [0]["type"] field type
149   - * [0]["len"] field length
150   - * [0]["flags"] field flags ("NOT NULL", "INDEX")
151   - * [0]["format"] precision and scale of number (eg. "10,2") or empty
152   - * [0]["index"] name of index (if has one)
153   - * [0]["chars"] number of chars (if any char-type)
154   - * ["meta"][field name] index of field named "field name"
155   - * The last one is used, if you have a field name, but no index.
156   - * Test: if (isset($result['meta']['myfield'])) {} ...
157   - */
158   -
159   - $this->connect();
160   -
161   - ## This is a RIGHT OUTER JOIN: "(+)", if you want to see, what
162   - ## this query results try the following:
163   - ## $table = new Table; $db = new my_DB_Sql; # you have to make
164   - ## # your own class
165   - ## $table->show_results($db->query(see query vvvvvv))
166   - ##
167   - $this->query("SELECT T.table_name,T.column_name,T.data_type,".
168   - "T.data_length,T.data_precision,T.data_scale,T.nullable,".
169   - "T.char_col_decl_length,I.index_name".
170   - " FROM ALL_TAB_COLUMNS T,ALL_IND_COLUMNS I".
171   - " WHERE T.column_name=I.column_name (+)".
172   - " AND T.table_name=I.table_name (+)".
173   - " AND T.table_name=UPPER('$table') ORDER BY T.column_id");
174   -
175   - $i=0;
176   - while ($this->next_record()) {
177   - $res[$i]["table"] = $this->Record[table_name];
178   - $res[$i]["name"] = strtolower($this->Record[column_name]);
179   - $res[$i]["type"] = $this->Record[data_type];
180   - $res[$i]["len"] = $this->Record[data_length];
181   - if ($this->Record[index_name]) $res[$i]["flags"] = "INDEX ";
182   - $res[$i]["flags"] .= ( $this->Record[nullable] == 'N') ? '' : 'NOT NULL';
183   - $res[$i]["format"]= (int)$this->Record[data_precision].",".
184   - (int)$this->Record[data_scale];
185   - if ("0,0"==$res[$i]["format"]) $res[$i]["format"]='';
186   - $res[$i]["index"] = $this->Record[index_name];
187   - $res[$i]["chars"] = $this->Record[char_col_decl_length];
188   - if ($full) {
189   - $j=$res[$i]["name"];
190   - $res["meta"][$j] = $i;
191   - $res["meta"][strtoupper($j)] = $i;
192   - }
193   - if ($full) $res["meta"][$res[$i]["name"]] = $i;
194   - $i++;
195   - }
196   - if ($full) $res["num_fields"]=$i;
197   -# $this->disconnect();
198   - return $res;
199   - }
200   -
201   -
202   - function affected_rows() {
203   - return $this->num_rows();
204   - }
205   -
206   - function num_rows() {
207   - return OCIrowcount($this->Parse);
208   - }
209   -
210   - function num_fields() {
211   - return OCINumcols($this->Parse);
212   - }
213   -
214   - function nf() {
215   - return $this->num_rows();
216   - }
217   -
218   - function np() {
219   - print $this->num_rows();
220   - }
221   -
222   - function f($Name) {
223   - if (is_object($this->Record[$Name]))
224   - {
225   - return $this->Record[$Name]->load();
226   - } else
227   - {
228   - return $this->Record[$Name];
229   - }
230   - }
231   -
232   - function p($Name) {
233   - print $this->f($Name);
234   - }
235   -
236   - function nextid($seqname)
237   - {
238   - $this->connect();
239   -
240   - $Query_ID=@ociparse($this->Link_ID,"SELECT $seqname.NEXTVAL FROM DUAL");
241   -
242   - if(!@ociexecute($Query_ID))
243   - {
244   - $this->Error=@OCIError($Query_ID);
245   - if($this->Error["code"]==2289)
246   - {
247   - $Query_ID=ociparse($this->Link_ID,"CREATE SEQUENCE $seqname");
248   - if(!ociexecute($Query_ID))
249   - {
250   - $this->Error=OCIError($Query_ID);
251   - $this->halt("<BR> nextid() function - unable to create sequence<br>".$this->Error["message"]);
252   - } else
253   - {
254   - $Query_ID=ociparse($this->Link_ID,"SELECT $seqname.NEXTVAL FROM DUAL");
255   - ociexecute($Query_ID);
256   - }
257   - }
258   - }
259   -
260   - if (ocifetch($Query_ID))
261   - {
262   - $next_id = ociresult($Query_ID,"NEXTVAL");
263   - } else
264   - {
265   - $next_id = 0;
266   - }
267   - ocifreestatement($Query_ID);
268   - return $next_id;
269   - }
270   -
271   - function disconnect() {
272   - if($this->Debug) {
273   - printf("Disconnecting...<br>\n");
274   - }
275   - OCILogoff($this->Link_ID);
276   - }
277   -
278   - function halt($msg) {
279   - printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
280   - printf("<b>ORACLE Error</b>: %s<br>\n",
281   - $this->Error["message"]);
282   - die("Session halted.");
283   - }
284   -
285   - function lock($table, $mode = "write") {
286   - $this->connect();
287   - if ($mode == "write") {
288   - $Parse=OCIParse($this->Link_ID,"lock table $table in row exclusive mode");
289   - OCIExecute($Parse);
290   - } else {
291   - $result = 1;
292   - }
293   - return $result;
294   - }
295   -
296   - function unlock() {
297   - return $this->query("commit");
298   - }
299   -
300   - function table_names() {
301   - $this->connect();
302   - $this->query("
303   - SELECT table_name,tablespace_name
304   - FROM user_tables");
305   - $i=0;
306   - while ($this->next_record())
307   - {
308   - $info[$i]["table_name"] =$this->Record["table_name"];
309   - $info[$i]["tablespace_name"]=$this->Record["tablespace_name"];
310   - $i++;
311   - }
312   - return $info;
313   - }
314   -
315   - function add_specialcharacters($query)
316   - {
317   - return str_replace("'","''",$query);
318   - }
319   -
320   - function split_specialcharacters($query)
321   - {
322   - return str_replace("''","'",$query);
323   - }
324   -}
325   -?>
phplib/db_odbc.inc deleted
1   -<?php
2   -/*
3   - * Session Management for PHP3
4   - *
5   - * Copyright (c) 1998-2000 Cameron Taggart (cameront@wolfenet.com)
6   - * Modified by Guarneri carmelo (carmelo@melting-soft.com)
7   - *
8   - * $Id$
9   - */
10   -
11   -class DB_Sql {
12   - var $Host = "";
13   - var $Database = "";
14   - var $User = "";
15   - var $Password = "";
16   - var $UseODBCCursor = 0;
17   -
18   - var $Link_ID = 0;
19   - var $Query_ID = 0;
20   - var $Record = array();
21   - var $Row = 0;
22   -
23   - var $Errno = 0;
24   - var $Error = "";
25   -
26   - var $Auto_Free = 0; ## set this to 1 to automatically free results
27   -
28   - /* public: constructor */
29   - function DB_Sql($query = "") {
30   - $this->query($query);
31   - }
32   -
33   - function connect() {
34   - if ( 0 == $this->Link_ID ) {
35   - $this->Link_ID=odbc_pconnect($this->Database, $this->User, $this->Password, $this->UseODBCCursor);
36   - if (!$this->Link_ID) {
37   - $this->halt("Link-ID == false, odbc_pconnect failed");
38   - }
39   - }
40   - }
41   -
42   - function query($Query_String) {
43   -
44   - /* No empty queries, please, since PHP4 chokes on them. */
45   - if ($Query_String == "")
46   - /* The empty query string is passed on from the constructor,
47   - * when calling the class without a query, e.g. in situations
48   - * like these: '$db = new DB_Sql_Subclass;'
49   - */
50   - return 0;
51   -
52   - $this->connect();
53   -
54   -# printf("<br>Debug: query = %s<br>\n", $Query_String);
55   -
56   -# rei@netone.com.br suggested that we use this instead of the odbc_exec().
57   -# He is on NT, connecting to a Unix MySQL server with ODBC. -- KK
58   -# $this->Query_ID = odbc_prepare($this->Link_ID,$Query_String);
59   -# $this->Query_Ok = odbc_execute($this->Query_ID);
60   -
61   - $this->Query_ID = odbc_exec($this->Link_ID,$Query_String);
62   - $this->Row = 0;
63   - odbc_binmode($this->Query_ID, 1);
64   - odbc_longreadlen($this->Query_ID, 4096);
65   -
66   - if (!$this->Query_ID) {
67   - $this->Errno = 1;
68   - $this->Error = "General Error (The ODBC interface cannot return detailed error messages).";
69   - $this->halt("Invalid SQL: ".$Query_String);
70   - }
71   - return $this->Query_ID;
72   - }
73   -
74   - function next_record() {
75   - $this->Record = array();
76   - $stat = odbc_fetch_into($this->Query_ID, ++$this->Row, &$this->Record);
77   - if (!$stat) {
78   - if ($this->Auto_Free) {
79   - odbc_free_result($this->Query_ID);
80   - $this->Query_ID = 0;
81   - };
82   - } else {
83   - // add to Record[<key>]
84   - $count = odbc_num_fields($this->Query_ID);
85   - for ($i=1; $i<=$count; $i++)
86   - $this->Record[strtolower(odbc_field_name ($this->Query_ID, $i)) ] = $this->Record[ $i - 1 ];
87   - }
88   - return $stat;
89   - }
90   -
91   - function seek($pos) {
92   - $this->Row = $pos;
93   - }
94   -
95   - function metadata($table) {
96   - $count = 0;
97   - $id = 0;
98   - $res = array();
99   -
100   - $this->connect();
101   - $id = odbc_exec($this->Link_ID, "select * from $table");
102   - if (!$id) {
103   - $this->Errno = 1;
104   - $this->Error = "General Error (The ODBC interface cannot return detailed error messages).";
105   - $this->halt("Metadata query failed.");
106   - }
107   - $count = odbc_num_fields($id);
108   -
109   - for ($i=1; $i<=$count; $i++) {
110   - $res[$i]["table"] = $table;
111   - $name = odbc_field_name ($id, $i);
112   - $res[$i]["name"] = $name;
113   - $res[$i]["type"] = odbc_field_type ($id, $name);
114   - $res[$i]["len"] = 0; // can we determine the width of this column?
115   - $res[$i]["flags"] = ""; // any optional flags to report?
116   - }
117   -
118   - odbc_free_result($id);
119   - return $res;
120   - }
121   -
122   - function affected_rows() {
123   - return odbc_num_rows($this->Query_ID);
124   - }
125   -
126   - function num_rows() {
127   - # Many ODBC drivers don't support odbc_num_rows() on SELECT statements.
128   - $num_rows = odbc_num_rows($this->Query_ID);
129   - //printf ($num_rows."<br>");
130   -
131   - # This is a workaround. It is intended to be ugly.
132   - if ($num_rows < 0) {
133   - $i=10;
134   - while (odbc_fetch_row($this->Query_ID, $i))
135   - $i*=10;
136   -
137   - $j=0;
138   - while ($i!=$j) {
139   - $k= $j+intval(($i-$j)/2);
140   - if (odbc_fetch_row($this->Query_ID, $k))
141   - $j=$k;
142   - else
143   - $i=$k;
144   - if (($i-$j)==1) {
145   - if (odbc_fetch_row($this->Query_ID, $i))
146   - $j=$i;
147   - else
148   - $i=$j;
149   - };
150   - //printf("$i $j $k <br>");
151   - };
152   - $num_rows=$i;
153   - }
154   -
155   - return $num_rows;
156   - }
157   -
158   - function num_fields() {
159   - return count($this->Record)/2;
160   - }
161   -
162   - function nf() {
163   - return $this->num_rows();
164   - }
165   -
166   - function np() {
167   - print $this->num_rows();
168   - }
169   -
170   - function f($Field_Name) {
171   - return $this->Record[strtolower($Field_Name)];
172   - }
173   -
174   - function p($Field_Name) {
175   - print $this->f($Field_Name);
176   - }
177   -
178   - function halt($msg) {
179   - printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
180   - printf("<b>ODBC Error</b>: %s (%s)<br>\n",
181   - $this->Errno,
182   - $this->Error);
183   - die("Session halted.");
184   - }
185   -}
186   -?>
phplib/db_oracle.inc deleted
1   -<?php
2   -/*
3   - * Oracle accessor based on Session Management for PHP3
4   - *
5   - * Copyright (c) 1998-2000 Luis Francisco Gonzalez Hernandez
6   - *
7   - * $Id$
8   - *
9   - */
10   -
11   -class DB_Sql {
12   - var $Debug = false;
13   - var $Home = "/u01/app/oracle/product/8.0.4";
14   - var $Remote = 1;
15   - /* Due to a strange error with Oracle 8.0.5, Apache and PHP3.0.6
16   - you don't need to set the ENV - on my system Apache
17   - will change to a zombie, if I don't set this to FALSE!
18   - If unsure try it out, if it works. */
19   - var $OraPutEnv = true;
20   -
21   - var $Database = "";
22   - var $User = "";
23   - var $Password = "";
24   -
25   - var $Link_ID = 0;
26   - var $Query_ID = 0;
27   - var $Record = array();
28   - var $Row;
29   -
30   - var $Errno = 0;
31   - var $Error = "";
32   - var $ora_no_next_fetch=false;
33   -
34   -
35   - /* copied from db_mysql for completeness */
36   - /* public: identification constant. never change this. */
37   - var $type = "oracle";
38   - var $revision = "1.2";
39   -
40   - var $Halt_On_Error = "yes"; ## "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning)
41   -
42   - /* public: constructor */
43   - function DB_Sql($query = "") {
44   - $this->query($query);
45   - }
46   -
47   - /* public: some trivial reporting */
48   - function link_id() {
49   - return $this->Link_ID;
50   - }
51   -
52   - function query_id() {
53   - return $this->Query_ID;
54   - }
55   -
56   - function connect() {
57   - ## see above why we do this
58   - if ($this->OraPutEnv) {
59   - PutEnv("ORACLE_SID=$this->Database");
60   - PutEnv("ORACLE_HOME=$this->Home");
61   - }
62   - if ( 0 == $this->Link_ID ) {
63   - if($this->Debug) {
64   - printf("<br>Connect()ing to $this->Database...<br>\n");
65   - }
66   - if($this->Remote) {
67   - if($this->Debug) {
68   - printf("<br>connect() $this->User/******@$this->Database.world<br>\n");
69   - }
70   - $this->Link_ID=ora_plogon
71   - ("$this->User/$this->Password@$this->Database","");
72   - /************** (comment by SSilk)
73   - this dosn't work on my system:
74   - $this->Link_ID=ora_plogon
75   - ("$this->User@$this->Database.world","$this->Password");
76   - ***************/
77   - } else {
78   - if($this->Debug) {
79   - printf("<br>connect() $this->User, $this->Password <br>\n");
80   - }
81   - $this->Link_ID=ora_plogon("$this->User","$this->Password");
82   - /* (comment by SSilk: don't know how this could work, but I leave this untouched!) */
83   - }
84   - if($this->Debug) {
85   - printf("<br>connect() Link_ID: $this->Link_ID<br>\n");
86   - }
87   - if (!$this->Link_ID) {
88   - $this->halt("connect() Link-ID == false " .
89   - "($this->Link_ID), ora_plogon failed");
90   - } else {
91   - //echo "commit on<p>";
92   - ora_commiton($this->Link_ID);
93   - }
94   - if($this->Debug) {
95   - printf("<br>connect() Obtained the Link_ID: $this->Link_ID<br>\n");
96   - }
97   - }
98   - }
99   -
100   - ## In order to increase the # of cursors per system/user go edit the
101   - ## init.ora file and increase the max_open_cursors parameter. Yours is on
102   - ## the default value, 100 per user.
103   - ## We tried to change the behaviour of query() in a way, that it tries
104   - ## to safe cursors, but on the other side be carefull with this, that you
105   - ## don't use an old result.
106   - ##
107   - ## You can also make extensive use of ->disconnect()!
108   - ## The unused QueryIDs will be recycled sometimes.
109   -
110   - function query($Query_String) {
111   -
112   - /* No empty queries, please, since PHP4 chokes on them. */
113   - if ($Query_String == "")
114   - /* The empty query string is passed on from the constructor,
115   - * when calling the class without a query, e.g. in situations
116   - * like these: '$db = new DB_Sql_Subclass;'
117   - */
118   - return 0;
119   -
120   - $this->connect();
121   - $this->lastQuery=$Query_String;
122   -
123   - if (!$this->Query_ID) {
124   - $this->Query_ID= ora_open($this->Link_ID);
125   - }
126   - if($this->Debug) {
127   - printf("Debug: query = %s<br>\n", $Query_String);
128   - printf("<br>Debug: Query_ID: %d<br>\n", $this->Query_ID);
129   - }
130   -
131   - if(!@ora_parse($this->Query_ID,$Query_String)) {
132   - $this->Errno=ora_errorcode($this->Query_ID);
133   - $this->Error=ora_error($this->Query_ID);
134   - $this->halt("<BR>ora_parse() failed:<BR>$Query_String<BR><small>Snap & paste this to sqlplus!</SMALL>");
135   - } elseif (!@ora_exec($this->Query_ID)) {
136   - $this->Errno=ora_errorcode($this->Query_ID);
137   - $this->Error=ora_error($this->Query_ID);
138   - $this->halt("<BR>\n$Query_String\n<BR><small>Snap & paste this to sqlplus!</SMALL>");
139   - }
140   -
141   - $this->Row=0;
142   -
143   - if(!$this->Query_ID) {
144   - $this->halt("Invalid SQL: ".$Query_String);
145   - }
146   -
147   - return $this->Query_ID;
148   - }
149   -
150   - function next_record() {
151   - if (!$this->no_next_fetch &&
152   - 0 == ora_fetch($this->Query_ID)) {
153   - if ($this->Debug) {
154   - printf("<br>next_record(): ID: %d,Rows: %d<br>\n",
155   - $this->Query_ID,$this->num_rows());
156   - }
157   - $this->Row +=1;
158   -
159   - $errno=ora_errorcode($this->Query_ID);
160   - if(1403 == $errno) { # 1043 means no more records found
161   - $this->Errno=0;
162   - $this->Error="";
163   - $this->disconnect();
164   - $stat=0;
165   - } else {
166   - $this->Error=ora_error($this->Query_ID);
167   - $this->Errno=$errno;
168   - if($this->Debug) {
169   - printf("<br>%d Error: %s",
170   - $this->Errno,
171   - $this->Error);
172   - }
173   - $stat=0;
174   - }
175   - } else {
176   - $this->no_next_fetch=false;
177   - for($ix=0;$ix<ora_numcols($this->Query_ID);$ix++) {
178   - $col=strtolower(ora_columnname($this->Query_ID,$ix));
179   - $value=ora_getcolumn($this->Query_ID,$ix);
180   - $this->Record[ "$col" ] = $value;
181   -# echo"<b>[$col]</b>: $value <br>\n";
182   - }
183   - $stat=1;
184   - }
185   -
186   - return $stat;
187   - }
188   -
189   - ## seek() works only for $pos - 1 and $pos
190   - ## Perhaps I make a own implementation, but my
191   - ## opinion is, that this should be done by PHP3
192   - function seek($pos) {
193   - if ($this->Row - 1 == $pos) {
194   - $this->no_next_fetch=true;
195   - } elseif ($this->Row == $pos ) {
196   - ## do nothing
197   - } else {
198   - $this->halt("Invalid seek(): Position is cannot be handled by API.<BR>".
199   - "Difference too big. Wanted: $pos Current pos: $this->Row");
200   - }
201   - if ($Debug) echo "<BR>Debug: seek = $pos<BR>";
202   - $this->Row=$pos;
203   - }
204   -
205   - function lock($table, $mode = "write") {
206   - if ($mode == "write") {
207   - $result = ora_do($this->Link_ID, "lock table $table in row exclusive mode");
208   - } else {
209   - $result = 1;
210   - }
211   - return $result;
212   - }
213   -
214   - function unlock() {
215   - return ora_do($this->Link_ID, "commit");
216   - }
217   -
218   - function metadata($table,$full=false) {
219   - $count = 0;
220   - $id = 0;
221   - $res = array();
222   -
223   - /*
224   - * Due to compatibility problems with Table we changed the behavior
225   - * of metadata();
226   - * depending on $full, metadata returns the following values:
227   - *
228   - * - full is false (default):
229   - * $result[]:
230   - * [0]["table"] table name
231   - * [0]["name"] field name
232   - * [0]["type"] field type
233   - * [0]["len"] field length
234   - * [0]["flags"] field flags ("NOT NULL", "INDEX")
235   - * [0]["format"] precision and scale of number (eg. "10,2") or empty
236   - * [0]["index"] name of index (if has one)
237   - * [0]["chars"] number of chars (if any char-type)
238   - *
239   - * - full is true
240   - * $result[]:
241   - * ["num_fields"] number of metadata records
242   - * [0]["table"] table name
243   - * [0]["name"] field name
244   - * [0]["type"] field type
245   - * [0]["len"] field length
246   - * [0]["flags"] field flags ("NOT NULL", "INDEX")
247   - * [0]["format"] precision and scale of number (eg. "10,2") or empty
248   - * [0]["index"] name of index (if has one)
249   - * [0]["chars"] number of chars (if any char-type)
250   - * ["meta"][field name] index of field named "field name"
251   - * The last one is used, if you have a field name, but no index.
252   - * Test: if (isset($result['meta']['myfield'])) {} ...
253   - */
254   -
255   - $this->connect();
256   -
257   - ## This is a RIGHT OUTER JOIN: "(+)", if you want to see, what
258   - ## this query results try the following:
259   - ## $table = new Table; $db = new my_DB_Sql; # you have to make
260   - ## # your own class
261   - ## $table->show_results($db->query(see query vvvvvv))
262   - ##
263   - $this->query("SELECT T.table_name,T.column_name,T.data_type,".
264   - "T.data_length,T.data_precision,T.data_scale,T.nullable,".
265   - "T.char_col_decl_length,I.index_name".
266   - " FROM ALL_TAB_COLUMNS T,ALL_IND_COLUMNS I".
267   - " WHERE T.column_name=I.column_name (+)".
268   - " AND T.table_name=I.table_name (+)".
269   - " AND T.table_name=UPPER('$table') ORDER BY T.column_id");
270   -
271   - $i=0;
272   - while ($this->next_record()) {
273   - $res[$i]["table"] = $this->Record[table_name];
274   - $res[$i]["name"] = strtolower($this->Record[column_name]);
275   - $res[$i]["type"] = $this->Record[data_type];
276   - $res[$i]["len"] = $this->Record[data_length];
277   - if ($this->Record[index_name]) $res[$i]["flags"] = "INDEX ";
278   - $res[$i]["flags"] .= ( $this->Record[nullable] == 'N') ? '' : 'NOT NULL';
279   - $res[$i]["format"]= (int)$this->Record[data_precision].",".
280   - (int)$this->Record[data_scale];
281   - if ("0,0"==$res[$i]["format"]) $res[$i]["format"]='';
282   - $res[$i]["index"] = $this->Record[index_name];
283   - $res[$i]["chars"] = $this->Record[char_col_decl_length];
284   - if ($full) {
285   - $j=$res[$i]["name"];
286   - $res["meta"][$j] = $i;
287   - $res["meta"][strtoupper($j)] = $i;
288   - }
289   - if ($full) $res["meta"][$res[$i]["name"]] = $i;
290   - $i++;
291   - }
292   - if ($full) $res["num_fields"]=$i;
293   -# $this->disconnect();
294   - return $res;
295   - }
296   -
297   - ## THIS FUNCTION IS UNSTESTED!
298   - function affected_rows() {
299   - if ($Debug) echo "<BR>Debug: affected_rows=". ora_numrows($this->Query_ID)."<BR>";
300   - return ora_numrows($this->Query_ID);
301   - }
302   -
303   - ## Known bugs: It will not work for SELECT DISTINCT and any
304   - ## other constructs which are depending on the resulting rows.
305   - ## So you *really need* to check every query you make, if it
306   - ## will work with it.
307   - ##
308   - ## Also, for a qualified replacement you need to parse the
309   - ## selection, cause this will fail: "SELECT id, from FROM ...").
310   - ## "FROM" is - as far as I know a keyword in Oracle, so it can
311   - ## only be used in this way. But you have been warned.
312   - function num_rows() {
313   - $curs=ora_open($this->Link_ID);
314   -
315   - ## this is the important part and it is also the HACK!
316   - if (eregi("^[[:space:]]*SELECT[[:space:]]",$this->lastQuery) ) {
317   - $from_pos = strpos(strtoupper($this->lastQuery),"FROM");
318   - $q = "SELECT count(*) ". substr($this->lastQuery, $from_pos);
319   -
320   - ORA_parse($curs,$q);
321   - ORA_exec($curs);
322   - ORA_fetch($curs);
323   - if ($Debug) echo "<BR>Debug: num_rows=". ORA_getcolumn($curs,0)."<BR>";
324   - return(ORA_getcolumn($curs,0));
325   - } else {
326   - $this->halt("Last Query was not a SELECT: $this->lastQuery");
327   - }
328   - }
329   -
330   - function num_fields() {
331   - if ($Debug) echo "<BR>Debug: num_fields=". ora_numcols($this->Query_ID) . "<BR>";
332   - return ora_numcols($this->Query_ID);
333   - }
334   -
335   - function nf() {
336   - return $this->num_rows();
337   - }
338   -
339   - function np() {
340   - print $this->num_rows();
341   - }
342   -
343   - function f($Name) {
344   - return $this->Record[$Name];
345   - }
346   -
347   - function p($Name) {
348   - print $this->Record[$Name];
349   - }
350   -
351   - /* public: sequence number */
352   - function nextid($seq_name)
353   - {
354   - $this->connect();
355   -
356   - /* Independent Query_ID */
357   - $Query_ID = ora_open($this->Link_ID);
358   -
359   - if(!@ora_parse($Query_ID,"SELECT $seq_name.NEXTVAL FROM DUAL"))
360   - {
361   - // There is no such sequence yet, then create it
362   - if(!@ora_parse($Query_ID,"CREATE SEQUENCE $seq_name")
363   - ||
364   - !@ora_exec($Query_ID)
365   - )
366   - {
367   - $this->halt("<BR> nextid() function - unable to create sequence");
368   - return 0;
369   - }
370   - @ora_parse($Query_ID,"SELECT $seq_name.NEXTVAL FROM DUAL");
371   - }
372   - if (!@ora_exec($Query_ID)) {
373   - $this->halt("<BR>ora_exec() failed:<BR>nextID function");
374   - }
375   - if (@ora_fetch($Query_ID) ) {
376   - $next_id = ora_getcolumn($Query_ID, 0);
377   - }
378   - else {
379   - $next_id = 0;
380   - }
381   - if ( Query_ID > 0 ) {
382   - ora_close(Query_ID);
383   - }
384   -
385   - return $next_id;
386   - }
387   -
388   - function disconnect() {
389   - if($this->Debug) {
390   - echo "Debug: Disconnecting $this->Query_ID...<br>\n";
391   - }
392   - if ( $this->Query_ID < 1 ) {
393   - echo "<B>Warning</B>: disconnect(): Cannot free ID $this->Query_ID\n";
394   -# return();
395   - }
396   - ora_close($this->Query_ID);
397   - $this->Query_ID=0;
398   - }
399   -
400   - /* private: error handling */
401   - function halt($msg) {
402   - if ($this->Halt_On_Error == "no")
403   - return;
404   -
405   - $this->haltmsg($msg);
406   -
407   - if ($this->Halt_On_Error != "report")
408   - die("Session halted.");
409   - }
410   -
411   - function haltmsg($msg) {
412   - printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
413   - printf("<b>Oracle Error</b>: %s (%s)<br>\n",
414   - $this->Errno,
415   - $this->Error);
416   - }
417   -
418   - function table_names() {
419   - $this->connect();
420   - $this->query("
421   - SELECT table_name,tablespace_name
422   - FROM user_tables");
423   - $i=0;
424   - while ($this->next_record())
425   - {
426   - $info[$i]["table_name"] =$this->Record["table_name"];
427   - $info[$i]["tablespace_name"]=$this->Record["tablespace_name"];
428   - $i++;
429   - }
430   - return $info;
431   - }
432   -}
433   -?>
phplib/db_pgsql.inc deleted
1   -<?php
2   -/*
3   - * Session Management for PHP3
4   - *
5   - * Copyright (c) 1998-2000 NetUSE AG
6   - * Boris Erdmann, Kristian Koehntopp
7   - *
8   - * $Id$
9   - *
10   - */
11   -
12   -class DB_Sql {
13   - var $Host = "";
14   - var $Database = "";
15   - var $User = "";
16   - var $Password = "";
17   -
18   - var $Link_ID = 0;
19   - var $Query_ID = 0;
20   - var $Record = array();
21   - var $Row = 0;
22   -
23   - var $Seq_Table = "db_sequence";
24   -
25   - var $Errno = 0;
26   - var $Error = "";
27   -
28   - var $Auto_Free = 0; # Set this to 1 for automatic pg_freeresult on
29   - # last record.
30   -
31   - function ifadd($add, $me) {
32   - if("" != $add) return " ".$me.$add;
33   - }
34   -
35   - /* public: constructor */
36   - function DB_Sql($query = "") {
37   - $this->query($query);
38   - }
39   -
40   - function connect() {
41   - if ( 0 == $this->Link_ID ) {
42   - $cstr = "dbname=".$this->Database.
43   - $this->ifadd($this->Host, "host=").
44   - $this->ifadd($this->Port, "port=").
45   - $this->ifadd($this->User, "user=").
46   - $this->ifadd($this->Password, "password=");
47   - $this->Link_ID=pg_pconnect($cstr);
48   - if (!$this->Link_ID) {
49   - $this->halt("Link-ID == false, pconnect failed");
50   - }
51   - }
52   - }
53   -
54   - function query($Query_String) {
55   - /* No empty queries, please, since PHP4 chokes on them. */
56   - if ($Query_String == "")
57   - /* The empty query string is passed on from the constructor,
58   - * when calling the class without a query, e.g. in situations
59   - * like these: '$db = new DB_Sql_Subclass;'
60   - */
61   - return 0;
62   -
63   - $this->connect();
64   -
65   -# printf("<br>Debug: query = %s<br>\n", $Query_String);
66   -
67   - $this->Query_ID = pg_Exec($this->Link_ID, $Query_String);
68   - $this->Row = 0;
69   -
70   - $this->Error = pg_ErrorMessage($this->Link_ID);
71   - $this->Errno = ($this->Error == "")?0:1;
72   - if (!$this->Query_ID) {
73   - $this->halt("Invalid SQL: ".$Query_String);
74   - }
75   -
76   - return $this->Query_ID;
77   - }
78   -
79   - function next_record() {
80   - $this->Record = @pg_fetch_array($this->Query_ID, $this->Row++);
81   -
82   - $this->Error = pg_ErrorMessage($this->Link_ID);
83   - $this->Errno = ($this->Error == "")?0:1;
84   -
85   - $stat = is_array($this->Record);
86   - if (!$stat && $this->Auto_Free) {
87   - pg_freeresult($this->Query_ID);
88   - $this->Query_ID = 0;
89   - }
90   - return $stat;
91   - }
92   -
93   - function seek($pos) {
94   - $this->Row = $pos;
95   - }
96   -
97   - function lock($table, $mode = "write") {
98   - if ($mode == "write") {
99   - $result = pg_Exec($this->Link_ID, "lock table $table");
100   - } else {
101   - $result = 1;
102   - }
103   - return $result;
104   - }
105   -
106   - function unlock() {
107   - return pg_Exec($this->Link_ID, "commit");
108   - }
109   -
110   -
111   - /* public: sequence numbers */
112   - function nextid($seq_name) {
113   - $this->connect();
114   -
115   - if ($this->lock($this->Seq_Table)) {
116   - /* get sequence number (locked) and increment */
117   - $q = sprintf("select nextid from %s where seq_name = '%s'",
118   - $this->Seq_Table,
119   - $seq_name);
120   - $id = @pg_Exec($this->Link_ID, $q);
121   - $res = @pg_Fetch_Array($id, 0);
122   -
123   - /* No current value, make one */
124   - if (!is_array($res)) {
125   - $currentid = 0;
126   - $q = sprintf("insert into %s values('%s', %s)",
127   - $this->Seq_Table,
128   - $seq_name,
129   - $currentid);
130   - $id = @pg_Exec($this->Link_ID, $q);
131   - } else {
132   - $currentid = $res["nextid"];
133   - }
134   - $nextid = $currentid + 1;
135   - $q = sprintf("update %s set nextid = '%s' where seq_name = '%s'",
136   - $this->Seq_Table,
137   - $nextid,
138   - $seq_name);
139   - $id = @pg_Exec($this->Link_ID, $q);
140   - $this->unlock();
141   - } else {
142   - $this->halt("cannot lock ".$this->Seq_Table." - has it been created?");
143   - return 0;
144   - }
145   - return $nextid;
146   - }
147   -
148   -
149   -
150   - function metadata($table) {
151   - $count = 0;
152   - $id = 0;
153   - $res = array();
154   -
155   - $this->connect();
156   - $id = pg_exec($this->Link_ID, "select * from $table");
157   - if ($id < 0) {
158   - $this->Error = pg_ErrorMessage($id);
159   - $this->Errno = 1;
160   - $this->halt("Metadata query failed.");
161   - }
162   - $count = pg_NumFields($id);
163   -
164   - for ($i=0; $i<$count; $i++) {
165   - $res[$i]["table"] = $table;
166   - $res[$i]["name"] = pg_FieldName ($id, $i);
167   - $res[$i]["type"] = pg_FieldType ($id, $i);
168   - $res[$i]["len"] = pg_FieldSize ($id, $i);
169   - $res[$i]["flags"] = "";
170   - }
171   -
172   - pg_FreeResult($id);
173   - return $res;
174   - }
175   -
176   - function affected_rows() {
177   - return pg_cmdtuples($this->Query_ID);
178   - }
179   -
180   - function num_rows() {
181   - return pg_numrows($this->Query_ID);
182   - }
183   -
184   - function num_fields() {
185   - return pg_numfields($this->Query_ID);
186   - }
187   -
188   - function nf() {
189   - return $this->num_rows();
190   - }
191   -
192   - function np() {
193   - print $this->num_rows();
194   - }
195   -
196   - function f($Name) {
197   - return $this->Record[$Name];
198   - }
199   -
200   - function p($Name) {
201   - print $this->Record[$Name];
202   - }
203   -
204   - function halt($msg) {
205   - printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
206   - printf("<b>PostgreSQL Error</b>: %s (%s)<br>\n",
207   - $this->Errno,
208   - $this->Error);
209   - die("Session halted.");
210   - }
211   -
212   - function table_names() {
213   - $this->query("select relname from pg_class where relkind = 'r' and not relname like 'pg_%'");
214   - $i=0;
215   - while ($this->next_record())
216   - {
217   - $return[$i]["table_name"]= $this->f(0);
218   - $return[$i]["tablespace_name"]=$this->Database;
219   - $return[$i]["database"]=$this->Database;
220   - $i++;
221   - }
222   - return $return;
223   - }
224   -}
225   -?>
phplib/db_sybase.inc deleted
1   -<?php
2   -/*
3   - * Session Management for PHP3
4   - *
5   - * Copyright (c) 1998-2000 NetUSE AG
6   - * Boris Erdmann, Kristian Koehntopp
7   - *
8   - * Adapted from db_mysql.inc by Sascha Schumann <sascha@schumann.cx>
9   - *
10   - * metadata() contributed by Adelino Monteiro <adelino@infologia.pt>
11   - *
12   - * $Id$
13   - *
14   - */
15   -
16   -class DB_Sql {
17   - var $Host = "";
18   - var $Database = "";
19   - var $User = "";
20   - var $Password = "";
21   -
22   - var $Link_ID = 0;
23   - var $Query_ID = 0;
24   - var $Record = array();
25   - var $Row;
26   -
27   - var $Auto_Free = 0; ## Set this to 1 for automatic sybase_free_result()
28   -
29   - /* public: constructor */
30   - function DB_Sql($query = "") {
31   - $this->query($query);
32   - }
33   -
34   - function connect() {
35   - if ( 0 == $this->Link_ID ) {
36   - $this->Link_ID=sybase_pconnect($this->Host,$this->User,$this->Password);
37   - if (!$this->Link_ID) {
38   - $this->halt("Link-ID == false, pconnect failed");
39   - }
40   - if(!sybase_select_db($this->Database, $this->Link_ID)) {
41   - $this->halt("cannot use database ".$this->Database);
42   - }
43   - }
44   - }
45   -
46   - function query($Query_String) {
47   -
48   -
49   - /* No empty queries, please, since PHP4 chokes on them. */
50   - if ($Query_String == "")
51   - /* The empty query string is passed on from the constructor,
52   - * when calling the class without a query, e.g. in situations
53   - * like these: '$db = new DB_Sql_Subclass;'
54   - */
55   - return 0;
56   -
57   - $this->connect();
58   -
59   -# printf("Debug: query = %s<br>\n", $Query_String);
60   -
61   - $this->Query_ID = sybase_query($Query_String,$this->Link_ID);
62   - $this->Row = 0;
63   - if (!$this->Query_ID) {
64   - $this->halt("Invalid SQL: ".$Query_String);
65   - }
66   -
67   - return $this->Query_ID;
68   - }
69   -
70   - function next_record() {
71   - $this->Record = sybase_fetch_array($this->Query_ID);
72   - $this->Row += 1;
73   -
74   - $stat = is_array($this->Record);
75   - if (!$stat && $this->Auto_Free) {
76   - sybase_free_result($this->Query_ID);
77   - $this->Query_ID = 0;
78   - }
79   - return $stat;
80   - }
81   -
82   - function seek($pos) {
83   - $status = sybase_data_seek($this->Query_ID, $pos);
84   - if ($status)
85   - $this->Row = $pos;
86   - return;
87   - }
88   -
89   - function metadata($table) {
90   - $count = 0;
91   - $id = 0;
92   - $res = array();
93   -
94   - $this->connect();
95   - $result = $this->query("exec sp_columns $table");
96   - if ($result < 0) {
97   - $this->Errno = 1;
98   - $this->Error = "Metadata query failed";
99   - $this->halt("Metadata query failed.");
100   - }
101   - $count = sybase_num_rows($result);
102   -
103   - for ($i=0; $i<$count; $i++) {
104   - $res[$i]["table"] = $table ;
105   - $res[$i]["name"] = sybase_result ($result, $i, "COLUMN_NAME");
106   - $res[$i]["type"] = sybase_result ($result, $i, "TYPE_NAME");
107   - $res[$i]["len"] = sybase_result ($result, $i, "LENGTH");
108   - $res[$i]["position"] = sybase_result ($result, $i, "ORDINAL_POSITION");
109   - $res[$i]["flags"] = sybase_result ($result, $i, "REMARKS");
110   -
111   - }
112   - }
113   -
114   - function affected_rows() {
115   - return sybase_affected_rows($this->Query_ID);
116   - }
117   -
118   - function num_rows() {
119   - return sybase_num_rows($this->Query_ID);
120   - }
121   -
122   - function num_fields() {
123   - return sybase_num_fields($this->Query_ID);
124   - }
125   -
126   - function nf() {
127   - return $this->num_rows();
128   - }
129   -
130   - function np() {
131   - print $this->num_rows();
132   - }
133   -
134   - function f($Name) {
135   - return $this->Record[$Name];
136   - }
137   -
138   - function p($Name) {
139   - print $this->Record[$Name];
140   - }
141   -
142   - function halt($msg) {
143   - printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
144   - printf("<b>Sybase Error</b><br>\n");
145   - die("Session halted.");
146   - }
147   -}
148   -?>
phplib/db_usql.inc deleted
1   -<?php
2   -/*
3   - * PHP Base Library
4   - *
5   - * general utilities for db_sql
6   - *
7   - * (c) 1999-2000 Carmelo Guarneri
8   - *
9   - * $Id$
10   - *
11   - */
12   -
13   -
14   -class DB_USql extends DB_Sql {
15   -
16   -//--------------------------------------------------------------
17   -// this function can be used to export all the columns of
18   -// a record into global variables.
19   -// It should be used after a call to next_record().
20   -//--------------------------------------------------------------
21   - function import_record_vars() {
22   - while (list($key, $val) = each($this->Record))
23   - if (ereg("[A-Za-z][A-Za-z0-9_]*", $key)) {
24   - $field_name = strtoupper($key);
25   - global $$field_name;
26   - $$field_name=$val;
27   - };
28   - }
29   -
30   -//--------------------------------------------------------------
31   -// this function can be used to export all the records of
32   -// a table on the output in the form of a call to the db_sql
33   -// query function with an insert statement.
34   -//--------------------------------------------------------------
35   - function dump_table($tablename, $filter="") {
36   - $this->query(sprintf("select * from %s", $tablename));
37   - while ($this->next_record()) {
38   - $this->dump_record($tablename, $filter);
39   - };
40   - }
41   -
42   -//--------------------------------------------------------------
43   -// this function can be used to export all the records of
44   -// a query on the output in the form of a call to the db_sql
45   -// query function with an insert statement.
46   -//--------------------------------------------------------------
47   - function dump_query($tablename, $filter="") {
48   - //$this->query(sprintf("select * from %s", $tablename));
49   - while ($this->next_record()) {
50   - $this->dump_record($tablename, $filter);
51   - };
52   - }
53   -
54   - function dump_record($tablename, $filter="") {
55   - $fields="";
56   - $values="";
57   - while (list($key, $val) = each($this->Record))
58   - if (ereg("[A-Za-z][A-Za-z0-9_]*", $key)) {
59   - $field_name = strtoupper($key);
60   - if (!empty($val))
61   - if (strstr( $filter, $field_name )=="") {
62   - $fields.="$field_name ,";
63   - $val=ereg_replace("'","''",$val);
64   - $val=ereg_replace("\"","\\\"",$val);
65   - //addslashes($val);
66   - $values.="'$val' ,";
67   - };
68   - }
69   - $fields=substr($fields, 0, strlen($fields)-1);
70   - $values=substr($values, 0, strlen($values)-1);
71   - $insert=sprintf("insert into %s(%s) values(%s)", $tablename, $fields, $values);
72   - echo "\$db->query(\"$insert\");\n";
73   - }
74   - };
75   -
76   -?>
phplib/query_sql.inc deleted
1   -<?php
2   -/*
3   - * Query generation for PHP3
4   - *
5   - * (C) Copyright 1998 Alex Aulbach
6   - * Credits: Gerard Hickey <Gerard.Hickey@nsc.com>
7   - * I took many ideas from his SQL.inc, thanks! :-)
8   - * The idea is of this class is based in November 1997,
9   - * it was a collection of functions for PHP/FI and mSQL.
10   - *
11   - * $Id$
12   - *
13   - */
14   -
15   -
16   -/*
17   -The Query-class is an enhancement to the db_*-classes. Currently It supports
18   -mySQL an Oracle but it is easy expandable. See down.
19   -
20   -It always needs the class DB_Sql!
21   -
22   -Query is fully upward compatible with DB_Sql. Example:
23   -
24   -OLD: NEW:
25   -
26   -require("db_mysql.inc"); require("db_mysql.inc");
27   -class hugobla extends DB_sql {} require("query_sql.inc");
28   -$db = new hugobla; class hugobla extends Query {}
29   - $db = new hugobla;
30   -
31   -It just provides a number of new functions.
32   -
33   -The Query-class is inteded to help you with creating selects, inserts,
34   -updates, where-clauses etc. Not just *simple* selects, but longer ones. It
35   -is indeed a great help for tables with more than 10-20 columns. But it can
36   -also be used for simple and small selects. The inbuilt checks help you
37   -programming saver.
38   -
39   -Here is an example:
40   -
41   -file: insert.php3
42   -------------------
43   -<?
44   -## gets data from my form and inserts it into db
45   -
46   -require("prepend.inc"); ## here the classes are loaded and configured
47   -$db = new hugobla;
48   -$db->query($db->insert_plain_Clause("mytable",$db->capture_vars("mytable"),ARRAY()));
49   -echo "Values inserted";
50   -
51   -?>
52   -
53   -file: insert2.php3
54   --------------------
55   -<?
56   -## gets data from my form and inserts it into db with a new INDEX
57   -## myindex is defined as INT AUTO_INCREMENT PRIMARY KEY
58   -## (this is mysql, in oracle you have to define a trigger)
59   -## mytime is defined as DATETIME (DATE in oracle)
60   -
61   -require("prepend.inc"); ## here the classes are loaded and configured
62   -$db = new hugobla;
63   -$mytime="SYSDATE()";
64   -$db->query($db->insert_plain_Clause("mytable",$db->capture_vars("mytable"),
65   - ARRAY(myindex=>'NULL',mytime='func')));
66   -echo "Values inserted: "$db->last_insert_id();
67   -
68   -?>
69   -
70   -This example is nice, cause you see how easy it can be used. :-)
71   -
72   -The best thing is, that you don't have to care, if a field is a string or a
73   -number. The values are automatically converted into the right form. The type
74   -of the vars are read from the table. Stringvalues are encapsulated with '
75   -(configurable) and escaped (the code for this is currently not good - we are
76   -assuming, that PHP is configured not to encapsulate strings), int-
77   -and real-values are casted. It can handle "NULL"-values, function-statements
78   -or other values for insertion.
79   -
80   -You will make less errors.
81   -
82   -mySQL and most other DB's accept a a short form of insert-clause (INSERT
83   -INTO bla VALUES (...)). The Query-class will always make the longer form
84   -(INSERT INTO BLA (...) VALUES (...)). This makes it possible to use ALTER
85   -TABLE-commands without changing the program! E.g. changing a field in a
86   -table from NUMBER to VARCHAR(10) is fully encapsulated with this class.
87   -
88   -The class supports currently only mysql and oracle. I think the differences
89   -between the DBs are encapsulated enough in the db_* classes, so it is
90   -possible to handle the remaining small differences inside this class (this
91   -affects mainly the function sql2phptype() ) and it could be easiely extended
92   -(asuming, that the metadata()-function of the db_*-class works correctly).
93   -In this case it is important, that the $type-variable in the db_*.inc-class
94   -is correctly set.
95   -
96   -
97   -TODO-list:
98   -- A method to create querys like the LIMIT-clause in mySQL. For Oracle
99   - this works:
100   -
101   - select linenum, foo, bar
102   - from (select rownum as linenum, foo, bar from
103   - (select foo,bar from chaos order by bar) )
104   - where linenum between 11 and 20;
105   -
106   -- cleaner escaping, handling of \ and NUL (current code is bullshit)
107   - Some ideas?
108   -
109   -- Little Alta-Vista: add functions to create a where clause from a search
110   - string with rules to handle searching for more than one word.
111   - half automatic generating search patterns into a where-clause
112   - simple search engine support, simple support for semi full-text-search
113   -
114   -- automatic configurable manipulation of values, eg.
115   - triming of strings (delete whitespace at begin and end)
116   - also : TOUPPER, TOLOWER etc
117   -
118   -- SELECT-Clause (GROUP BY, HAVING, JOIN...)
119   -
120   -- make new functions insert_Clause() etc. which inserts only the
121   - fields they got from your call (the current will do "plain" insert)
122   -
123   -- where_Clause() - creating WHERE for select, update, exists etc.
124   -
125   -- serv all queries directly into db, return just the handle
126   - (hm, how to deal with the DB-handle?)
127   -
128   -- Return a 2-dimensional (Table-compatible) field from select (not so important)
129   -
130   -- The sql2phptype() can be used to help creating automatic input forms
131   - for a table
132   -
133   -DEPENDING:
134   -- db_mysql: new function metatabledata(), which returns the table-info from
135   - current selected table (will return multiple table-columns with a join)
136   -- db_mysql: perhaps the function sql2phptype() should be placed there?
137   -
138   -
139   -For developers of new db_*.inc: the function metadata() is very important
140   -for the correct work of this class. T
141   -
142   -*/
143   -
144   -class Query extends DB_Sql {
145   -
146   - ## DONT FORGET to set the variables from DB_Sql! See there!
147   -
148   - ## For debugging: if set to TRUE the Query is printed out,
149   - ## before executing or returning
150   - var $Q_Debug=false;
151   -
152   - ## set this to another value, if you want to hide it
153   - ## in your HTML-code
154   - ## example: var $Q_Debug_print="\n<!-- QDebug: %s -->\n";
155   - var $Q_Debug_print="<BR><B>QDebug:</B>\n%s\n<BR>\n";
156   -
157   - ## Set this to TRUE if you only want to test, which query
158   - ## will be created (ideal in combination with $Q_Debug)
159   - ## This depends only functions which will make changes
160   - var $No_Write=false;
161   -
162   - ## currently unused, this var is just an idea for later use.
163   - var $Backslash_N_is_NULL = false;
164   -
165   - ## Metacache: see funtcion metadata_buffered()
166   - var $meta_cache_off = false;
167   -
168   - ## This is the char, which will be replaced by \char.
169   - ## PHP3 seems to be NOT binary-safe, so not quoting
170   - ## for \0 (some ideas?)
171   - ## we use ereg_replace to do the replacements.
172   - ## with PHP3.0.6 you should replace this with str_replace()!
173   - ## Quoting = 1 -> normal quoting using AddSlashes
174   - ## 2 -> Replace \' to '' - needed eg. for sybase or oracle
175   - var $Quoting=1;
176   - var $Quotechar="'";
177   -
178   - var $StrLengTrunc = false;
179   - var $StrLengWarn = false;
180   -
181   - ###########################
182   - ## _QDebug
183   - function _QDebug ($str) {
184   - if ($this->Q_Debug) {
185   - printf($this->Q_Debug_print,$str);
186   - }
187   - }
188   -
189   - ###########################
190   - ## Set DB-Classname
191   - ## This is only a 3rd posibility for setting the classname
192   - ##
193   - function set_db_class ($db_class) {
194   - $this->Database=$db_class;
195   - }
196   -
197   -
198   - ###########################
199   - ## This function gets a datatype from the DB and returns an
200   - ## equivalent datatype for PHP
201   - ##
202   - ## It returns also a subtype for a string
203   - ##
204   - function sql2phptype ($type,$format='') {
205   - ## $this->type is currently either "mysql" or "oracle"
206   - switch ($this->type) {
207   - case "mysql":
208   - switch ($type) {
209   - case "var string":
210   - case "string" :
211   - case "char" :
212   - return(Array("string",""));
213   - break;
214   - case "timestamp" :
215   - case "datetime" :
216   - case "date" :
217   - case "time" :
218   - return(Array("string","date"));
219   - break;
220   - case "blob" :
221   - return(Array("string","blob"));
222   - break;
223   - case "real" :
224   - return(Array("double",""));
225   - break;
226   - case "long" :
227   - default :
228   - return(Array("int",""));
229   - break;
230   - }
231   - break;
232   - case "oracle":
233   - switch ($type) {
234   - case "VARCHAR2" :
235   - case "VARCHAR" :
236   - case "CHAR" :
237   - return(Array("string",""));
238   - break;
239   - case "DATE" :
240   - return(Array("string","date"));
241   - break;
242   - case "BLOB" :
243   - case "CLOB" :
244   - case "BFILE" :
245   - case "RAW" :
246   - case "LONG" :
247   - case "LONG RAW" :
248   - return(Array("string","blob"));
249   - break;
250   - case "NUMBER" :
251   - if ($format) {
252   - return(Array("double",""));
253   - } else {
254   - return(Array("int",""));
255   - }
256   - break;
257   - default :
258   - $this->halt("sql2phptype(): Type is not a valid value: '$type'");
259   - break;
260   - }
261   - break;
262   - default:
263   - $this->halt("sql2phptype(): DB-type is not a valid value: ".$this->type);
264   - break;
265   - }
266   - }
267   -
268   -
269   - #######################################
270   - ## This function returns a PHP-variable depending
271   - ## on type. E.g. a string is returned as 'string'
272   - ##
273   - ## The parameters mean
274   - ## $val - the value
275   - ## There is a special case: If value is "NULL" and
276   - ## the type is not "string" or subtype is empty, then
277   - ## a value "NULL" is inserted. This let you just spare
278   - ## a little bit work with $special
279   - ##
280   - ## $meta - the meta information for this field (that's what
281   - ## is returned by metadata() from DB_sql-class, but just one
282   - ## single row, e.g. $meta[2], not hole $meta).
283   - ##
284   - ## $special - Overwrites the type of the var if set. Some special
285   - ## meanings:
286   - ## "NULL" means, that this value must be set to "NULL"
287   - ## "func" means, that $val should be untouched -
288   - ## e.g. to insert the value of a SQL-function
289   - ## [ INSERT INTO bla VALUES ( time=NOW() ) ]
290   - ##
291   -
292   - function convert ($val,$meta,$special="") {
293   - list($type,$subtype)=$this->sql2phptype($meta["type"],$meta["format"]);
294   - if (($val == "NULL" &&
295   - ($type != "string" || $type[1] != "")) ||
296   - $special == "NULL") {
297   - $type="NULL";
298   - } else {
299   - if ($special) {
300   - $type=$special;
301   - if ($type!="func") {
302   - $val=$type;
303   - $type="func";
304   - }
305   - }
306   - }
307   - switch ($type) {
308   - case "string" :
309   - $val=(string)$val;
310   - if ($this->Quoting) {
311   - $val=AddSlashes($val);
312   - }
313   - if ($this->Quoting==2) {
314   - $val=str_replace("\\'","''",$val);
315   - }
316   - if ($subtype!='date' &&
317   - ( $this->StrLengTrunc || $this->StrLengWarn ) ) {
318   - if ( strlen($val) > $meta[len] ) {
319   - if ($this->StrLengWarn) {
320   - echo "<BR>STRING TOO LONG: '$meta[name]'";
321   - if ($this->StrLengTrunc) {
322   - echo ", TRUNCATING!";
323   - }
324   - }
325   - if ($this->StrLengTrunc) {
326   - $val=substr($val,0,$meta[len]);
327   - }
328   - }
329   - }
330   - $val=$this->Quotechar . $val . $this->Quotechar;
331   - break;
332   - case "int" :
333   - $val=(int)$val;
334   - break;
335   - case "double" :
336   - $val=(double)$val;
337   - break;
338   - case "NULL" :
339   - $val="NULL";
340   - break;
341   - case "func" :
342   - $val=(string)$val;
343   - break;
344   - default :
345   - echo "UNKNOWN TYPE: $type<BR>";
346   - }
347   - $this->_QDebug("Val: $meta[name] => $val<BR>");
348   - return(Array($val,$meta["name"]));
349   - }
350   -
351   -
352   - ##
353   - ## Function to generate a plain INSERT-Clause
354   - ## ("plain" means, that every field in the table will
355   - ## be set to a value, default is '' or 0 if nothing said
356   - ## in $special)
357   - ##
358   - ## $fields is an assoc. Array consisting out of
359   - ## table_name => value-pairs
360   - ## $special is an assoc. field which will commit special
361   - ## handling to convert() (See there)
362   - ## $check could be "strong" or "soft".
363   - ## "soft" won't tell you if there were to less
364   - ## or too much fields (good for debuging)
365   - ##
366   - ## returns the insert clause. It's on you to modify it
367   - ## and send it to your DB
368   - ##
369   - function insert_plain_Clause ($table,$fields,$special,$check="soft") {
370   - $meta=$this->metadata_buffered($table);
371   -
372   - for ($i=0; $i < $meta["num_fields"]; $i++) {
373   - $j=$meta[$i]["name"];
374   - ## NOT IMPLEMENTED: SEARCHING FOR $fields[$i]
375   - list($val["val"][$i],$val["name"][$i])=
376   - $this->convert($fields[$j],$meta[$i],$special[$j]);
377   - }
378   - if (Count($fields)!=Count($val["name"]) && $check=="strong") {
379   - echo "WARNING: insert_plain_clause(): There are not the same number of".
380   - " fields as in table for INSERT<BR>";
381   - }
382   - $q=sprintf("INSERT INTO %s (%s) VALUES (%s)",
383   - $table,join($val["name"],","),
384   - join($val["val"],","));
385   - $this->_QDebug($q);
386   - return($q);
387   - }
388   -
389   - # Replace, a special mySQL-function, same as INSERT
390   - function replace_plain_Clause ($table,$fields,$special,$check="soft") {
391   - $meta=$this->metadata_buffered($table);
392   -
393   - for ($i=0; $i < $meta["num_fields"]; $i++) {
394   - $j=$meta[$i]["name"];
395   - ## NOT IMPLEMENTED: SEARCHING FOR $fields[$i]
396   - list($val["val"][$i],$val["name"][$i])=
397   - $this->convert($fields[$j],$meta[$i],$special[$j]);
398   - }
399   - if (Count($fields)!=Count($val["name"]) && $check=="strong") {
400   - echo "WARNING: replace_plain_Clause(): There are not the same number of".
401   - " fields as in table for INSERT<BR>";
402   - }
403   - $q=sprintf("REPLACE %s (%s) VALUES (%s)",
404   - $table,join($val["name"],","),
405   - join($val["val"],","));
406   - $this->_QDebug($q);
407   - return($q);
408   - }
409   -
410   - ##
411   - ## This function is nearly the same, as insert_plain_Clause,
412   - ## The where parameter is new and should be generated by yourself
413   - ## The check parameter knows 3 values: strong, soft and weak
414   - ## weak enables you to sent a query without $where (enables you
415   - ## to update the hole table)
416   - ##
417   - function update_plain_Clause ($table,$fields,$special,$where,$check="soft") {
418   - $meta=$this->metadata_buffered($table);
419   - if (!$where && $check!="weak") {
420   - echo "ERROR: update_plain_Clause(): Parameter \$where is empty!<BR>";
421   - return(false);
422   - }
423   -
424   - for ($i=0; $i < $meta["num_fields"]; $i++) {
425   - $j=$meta[$i]["name"];
426   - ## NOT IMPLEMENTED: SEARCHING FOR $fields[$i]
427   - list($val["val"][$i],$val["name"][$i])=
428   - $this->convert($fields[$j],$meta[$i],$special[$j]);
429   -#echo "V: ".$val["name"][$i]." : ". $val["val"][$i]." - ".$fields[$j]."<BR>";
430   - }
431   - if (Count($fields)!=Count($val["name"]) && $check=="strong") {
432   - echo "WARNING: update_plain_Clause(): There are not the same number of".
433   - " fields for INSERT<BR>";
434   - }
435   - for ($i=0 ; $i < Count ($val["name"]); $i++ ) {
436   - $s[]=$val["name"][$i]."=".$val["val"][$i];
437   - }
438   - $q=sprintf("UPDATE %s SET %s",$table,join($s,","));
439   - if ($where) {
440   - if (!eregi("^[[:space:]]*WHERE",$where)) {
441   - ## insert "WHERE" if not set
442   - $where="WHERE $where";
443   - }
444   - $q.=" $where";
445   - }
446   - $this->_QDebug($q);
447   - return($q);
448   - }
449   -
450   -
451   - ##
452   - ## This function is nearly the same, as insert_Clause,
453   - ## The where parameter is new and should be generated by yourself
454   - ## The check parameter knows 3 values: strong, soft and weak
455   - ## weak enables you to sent a query without $where (enables you
456   - ## to update the hole table)
457   - ##
458   - function update_Clause ($table,$fields,$special,$where,$check="soft") {
459   - $meta=$this->metadata_buffered($table);
460   - if (!$where && $check!="weak") {
461   - echo "ERROR: update_Clause(): Parameter \$where is empty!<BR>";
462   - return(false);
463   - }
464   -
465   - $i=0;
466   - for (reset($fields); list($key,$val)=each($fields); $i++) {
467   - if ( isset($meta[meta][$key]) ) {
468   - $j=$meta[meta][$key];
469   - list($v["val"][$i],$v["name"][$i])=
470   - $this->convert($val,$meta[$j],$special[$key]);
471   - }
472   - }
473   - for ($i=0 ; $i < Count ($v["name"]); $i++ ) {
474   - $s[]=$v["name"][$i]."=".$v["val"][$i];
475   - }
476   - if (Count($s)) {
477   - $q=sprintf("UPDATE %s SET %s",$table,join($s,","));
478   - if ($where) {
479   - if (!eregi("^[[:space:]]*WHERE",$where)) {
480   - ## insert "WHERE" if not set
481   - $where="WHERE $where";
482   - }
483   - $q.=" $where";
484   - }
485   - }
486   - $this->_QDebug($q);
487   - return($q);
488   - }
489   -
490   -
491   -
492   - ##
493   - ## DELETE
494   - ## deletes the selected Table
495   - ## $check can be "soft" and "weak". Weak let's you delete the
496   - ## hole table, if you want
497   - ##
498   - function delete_Clause ($table,$where,$check="soft") {
499   - if (!$where && $check!="weak") {
500   - echo "ERROR: delete_Clause(): Parameter \$where is empty!<BR>";
501   - return(false);
502   - }
503   -
504   - $q=sprintf("DELETE FROM %s",$table);
505   - if ($where) {
506   - if (!eregi("^[[:space:]]*WHERE",$where)) {
507   - ## insert "WHERE" if not set
508   - $where="WHERE $where";
509   - }
510   - $q.=" $where";
511   - }
512   - $this->_QDebug($q);
513   - return($q);
514   - }
515   -
516   -
517   - ##
518   - ## This function checks wether in table $table a
519   - ## field $name is set with value $val
520   - ##
521   - ## it returns the number of found matches or zero
522   - ##
523   - function exists ($table,$name,$val) {
524   - $meta=$this->metadata_buffered($table);
525   - $j=$meta["meta"][$name];
526   - list($k)=$this->convert($val,$meta[$j]);
527   - $q=sprintf("SELECT COUNT(%s) as c FROM %s WHERE %s=%s",
528   - $name,$table,$name,$k);
529   - $this->_QDebugs($q);
530   - $this->query($q);
531   - $this->next_record();
532   - return($this->f("c"));
533   - }
534   -
535   - ##
536   - ## This function creates a query like exists, but returns
537   - ## an assoc array of the first found row, or false if nothing found
538   - ## field $name is set with value $val
539   - ##
540   - function getrow ($table,$name,$val) {
541   - $meta=$this->metadata_buffered($table);
542   - $j=$meta[meta][$name];
543   - list($k)=$this->convert($val,$meta[$j]);
544   - $q=sprintf("SELECT * FROM %s WHERE %s=%s",
545   - $table,$name,$k);
546   - $this->_QDebug($q);
547   - $this->query($q);
548   - if ($this->next_record()) {
549   - return($this->Record);
550   - } else {
551   - echo "<BR><B>WARNING:</B> getrow(): KEY: $name VAL: $val not found<BR>";
552   - return(false);
553   - }
554   - }
555   -
556   -
557   -
558   - ##
559   - ## WHERE-PLAIN-CLAUSE
560   - ## Let you generate a WHERE-Clause with a Loop.
561   - ##
562   - ## Returns a where-clause beginning with " WHERE "
563   - ##
564   - ## This function generates a where-clause
565   - ## $mywhere An array of simple expressions, eg. "firstname='Alex'"
566   - ## $andor This string is printed bewtween the where-Array
567   - ## default is 'AND'. It will handle an existing
568   - ## $oldwhere correctly. You can set this to '', but then
569   - ## the correct operator must be set by you in the where
570   - ## $where an existing WHERE-clause. Default is empty.
571   - ## $check if 'strong', it will stop, if an empty where-clause
572   - ## will be returned, to avoid "full" selects. Default is soft
573   - ##
574   - function where_plain_Clause ($mywhere,$andor='AND',$where='',$check="soft") {
575   - $meta=$this->metadata_buffered($table);
576   - $q='';
577   -
578   - for ($i=0; $i<Count($mywhere); $i++ ) {
579   - $q.=" $andor ".$mywhere[$i];
580   - }
581   - if ($where) {
582   - $where=eregi_Replace("^[[:space:]]*WHERE","",$where);
583   - $q.=" $andor $where";
584   - }
585   - if (!$q && $ckeck=='full') {
586   - echo "WARNING: where_plain_Clause(): WHERE-clause is empty!<BR>";
587   - }
588   - $q=ereg_Replace("^ $andor "," WHERE ",$q);
589   - $this->_QDebug("where_plain_Clause(): $q");
590   - return($q);
591   - }
592   -
593   - ##
594   - ## ANOTHER-WHERE-CLAUSE
595   - ##
596   - ## This function generates a where-clause beginning with " WHERE "
597   - ## Different form where_plain_Clause() this function is fully automated
598   - ## It can handle NULL-Values (IS NULL) in a special manner:
599   - ## if a value of $fields is 'NULL', we are looking, if the
600   - ## operator is '='. In this case the operator is changed into "IS"
601   - ## in any other case it is changed into "IS NOT".
602   - ##
603   - ## $tables table
604   - ## $fields Assoc name=>value-fields
605   - ## $op Assoc name=>operator. If empty, '=' is taken. it is printed
606   - ## *between* the name/value pairs.
607   - ## if $op is 'func' the name is taken as function name,
608   - ## inside the brakets is the value.
609   - ## $special Affects the calculation of value.
610   - ## See INSERT_CLAUSE() for more about this.
611   - ## $andor This string is printed bewtween the name/value-pairs,
612   - ## default is 'AND'. If $where is set, it prints
613   - ## it directly at the end before concatenating
614   - ## $where an existing WHERE-clause. Default is empty.
615   - ## $check if 'strong', it will stop, if an empty where-clause
616   - ## will be returned, to avoid "full" selects. Default is soft
617   - ##
618   - ## Returns a where-clause beginning with " WHERE "
619   - ##
620   - function where_Clause ($table,$fields,$op='',$special='',
621   - $andor='AND',$where='',$check="soft") {
622   - $meta=$this->metadata_buffered($table);
623   - $q='';
624   -
625   - if (!is_Array($op)) $op=ARRAY();
626   - if (!is_Array($special)) $op=ARRAY();
627   - if (!$andor) $andor='AND';
628   -
629   - $i=0;
630   - for (reset($fields); list($key,$val)=each($fields); $i++) {
631   - list($k[val],$k[name])=
632   - $this->convert($fields[$key],$meta[$meta[meta][$key]],$special[$key]);
633   - if (!$op[$key]) $o='='; else $o=$op[$key];
634   - if ('NULL'==strval($k[val])) {
635   - if ($o=='=' || strtoupper($o)=='IS') $o = 'IS';
636   - else $o = 'IS NOT';
637   - }
638   - $q.=" $andor $k[name] $o $k[val]";
639   - }
640   - if ($where) {
641   - $where=eregi_Replace("^[[:space:]]*WHERE","",$where);
642   - $q.=" $andor $where";
643   - }
644   - if (!$q && $ckeck=='full') {
645   - echo "WARNING: where_Clause(): WHERE-clause is empty!<BR>";
646   - }
647   - $q=ereg_Replace("^ $andor "," WHERE ",$q);
648   - $this->_QDebug("where_Clause(): $q");
649   - return($q);
650   - }
651   -
652   -
653   - ##
654   - ## capture-vars
655   - ##
656   - ## This function returns an assoc. Array consisting out of
657   - ## name=>value-pairs needed by all the other functions. It reads
658   - ## the name of the vars from the fields in $table and the values
659   - ## from the $GLOBALS-var-field.
660   - ## This has the sense, that you can name the variables in your
661   - ## Input-Form exactly like the names in your table. This again
662   - ## let make you less errors and less side effects.
663   - ##
664   - ## $table The name of the table
665   - ##
666   - function capture_vars ($table) {
667   - $meta=$this->metadata_buffered($table);
668   - $r=Array();
669   - for ($i=0; $i < $meta["num_fields"]; $i++) {
670   - $j=$meta[$i]["name"];
671   - if (isset($GLOBALS[$j])) {
672   - $r[$j] = $GLOBALS[$j];
673   - $this->_QDebug("Found $j: $r[$j]");
674   - }
675   - }
676   - return($r);
677   - }
678   -
679   - ##
680   - ## all_changed_vars
681   - ##
682   - ## This function returns an assoc. Array consisting out of
683   - ## name=>value-pairs which have a different value from the value
684   - ## currently existing in your table. This is needed by
685   - ## update_Clause(), cause with this, the update-query can be shortened
686   - ## to the maximum needed max. Can also be used for much other things,
687   - ## e.g. checking if something in your form has been changed (in this
688   - ## case it returns an empty array)
689   - ##
690   - ## $table The name of the table
691   - ## $fields Your assoc value field, which you want to check for
692   - ## $where The where-clause, which matches your row.
693   - ## This functions writes warnings, if your where-clause
694   - ## returns more than one row or nothing
695   - ##
696   - function all_changed_vars ($table,$fields,$where,$check='soft') {
697   - $meta=$this->metadata_buffered($table);
698   -
699   - $q1="SELECT * FROM $table $where";
700   - $this->query($q1);
701   - $r=Array();
702   - if ($this->next_record()) {
703   - for ($i=0; $i < $meta["num_fields"]; $i++) {
704   - $j=$meta[$i]["name"];
705   - if ($this->Record[$j]!=$fields[$j]) {
706   - $r[$j]=$fields[$j];
707   - $this->_QDebug("Changed $j: ".$fields[$j]." -> ".$this->Record[$j]);
708   - }
709   - }
710   - if ($this->next_record()) {
711   - echo "ERROR: all_changed_vars(): Found more than one row!<BR>";
712   - }
713   - } elseif ($check!='soft') {
714   - echo "<BR><B>WARNING:</B> all_changed_vars(): No row found!<BR>";
715   - }
716   - $this->_QDebug("WHERE: $where");
717   - return($r);
718   - }
719   -
720   - ##
721   - ## metadata_buffered (internal)
722   - ##
723   - ## This function calls metadata() if it won't find the buffer,
724   - ## this speeds the Query-class strongly up, cause it is needed in nearly
725   - ## every function
726   - ##
727   - ## $table the name of the table
728   - ##
729   - ## Returns the metadata-field
730   - ##
731   - function metadata_buffered($table) {
732   - if ( !is_Array($this->meta_buf[$table]) || $this->meta_cache_off) {
733   - return ($this->meta_buf[$table]=$this->metadata($table,true));
734   - } else {
735   - return ($this->meta_buf[$table]);
736   - }
737   - }
738   -
739   -
740   -}
741   -
742   -?>
phplib/sqlquery.inc deleted
1   -<?php
2   -/*
3   - * PHP Base Library
4   - *
5   - * Copyright (c) 1998-2000 NetUSE AG
6   - * Boris Erdmann, Kristian Koehntopp
7   - *
8   - * $Id$
9   - *
10   - */
11   -
12   -class Sql_Query {
13   - var $classname = "Sql_Query"; ## Persistence Support
14   - var $persistent_slots = array(
15   - "conditions", "input_size", "input_max", "method", "lang", "translate", "container", "variable"
16   - );
17   -
18   - var $conditions = 1; ## Allow for that many Query Conditions
19   - var $input_size = 20; ## Used in text input field creation
20   - var $input_max = 80;
21   -
22   - var $method = "post"; ## Generate get or post form...
23   - var $lang = "en"; ## HTML Widget language
24   -
25   - var $translate = "on"; ## If set, translate column names
26   - var $container = "on"; ## If set, create a container table
27   - var $variable = "on"; ## if set, create variable size buttons
28   -
29   - ## HTML Widget dictionary
30   - var $dict = array(
31   - "de" => array(
32   - "searchfor" => "Suchen nach:",
33   - "and" => "und",
34   - "or" => "oder",
35   - "like" => "enthรคlt",
36   - "reset" => "Neu",
37   - "submit" => "Ausfรผhren",
38   - "less" => "Weniger",
39   - "more" => "Mehr"
40   - ),
41   -
42   - "en" => array(
43   - "searchfor" => "Search for:",
44   - "and" => "and",
45   - "or" => "or",
46   - "like" => "contains",
47   - "reset" => "Reset Query",
48   - "submit" => "Submit Query",
49   - "less" => "Fewer",
50   - "more" => "More"
51   - )
52   - );
53   -
54   - ## SQL comparision dictionary
55   - var $compare = array(
56   - "like" => "like",
57   - ">" => ">",
58   - "<" => "<",
59   -
60   - ">=" => ">=",
61   - "<=" => "<=",
62   - "=" => "=",
63   - "<>" => "<>"
64   - );
65   -
66   -
67   - function start($class = "") {
68   - }
69   -
70   - ## selection:
71   - ##
72   - ## Create a <select> tag of the class $class with the name $name.
73   - ## The tag contains the options named in array $option. If $trans
74   - ## is true, $option is exspected to be a hash of
75   - ## "long name " => "sqlname" pairs. The option matching $old
76   - ## is created with the attribute "selected".
77   - ##
78   - function selection($name, $option, $old = "", $trans = "", $class = "") {
79   - $res = "";
80   - $res .= sprintf("<select%s name=\"%s\">\n",
81   - ($class)?" class=$class":"",
82   - $name);
83   - reset($option);
84   - while(list($k, $v) = each($option)) {
85   - if (($trans == "" && $old == $v)
86   - || ($trans != "" && $old == $k)) {
87   - $selected = " selected";
88   - } else {
89   - $selected = "";
90   - }
91   -
92   - $res .= sprintf("<option value=\"%s\"%s%s>%s\n",
93   - ($trans)?$k:$v,
94   - ($class)?" class=$class":"",
95   - ($selected)?" selected":"",
96   - $v);
97   - }
98   - $res .= sprintf(" </select>");
99   -
100   - return $res;
101   - }
102   -
103   - ## fieldname:
104   - ##
105   - ## Given a basename $base, and attribute name $att and an attribute
106   - ## number $num, this functions returns an input field name
107   - ## $base[$name_$num].
108   - ##
109   - ## This construct can be imported into a function namespace with a
110   - ## single global instruction and the field name can be easily
111   - ## exploded into component names and numbers.
112   -
113   - function makename($base, $att, $num) {
114   - return sprintf("%s[%s_%d]", $base, $att, $num);
115   - }
116   -
117   - ## form:
118   - ##
119   - ## Draw SQL Query selection form.
120   - ##
121   - function form($base, $option, $class = "", $target = "") {
122   - global $sess;
123   -
124   - ##
125   - ## load the HTML results of this function into $res.
126   - ##
127   - $res = "";
128   -
129   - ## A hack. We cannot do language dependent initialisation of
130   - ## static values.
131   - if (isset($this->compare["like"])) {
132   - $this->compare["like"] = $this->dict[$this->lang]["like"];
133   - }
134   -
135   - ## Prepare a self-directed container form
136   - if ($this->container) {
137   - $res .= sprintf("<table border=1%s><tr%s><td>\n",
138   - ($class)?" class=$class":"",
139   - ($class)?" class=$class":"",
140   - ($class)?" class=$class":"");
141   - }
142   - $res .= sprintf("<form method=\"%s\" action=\"%s\">\n",
143   - $this->method,
144   - ($target)?$target:$sess->self_url());
145   -
146   - ## Prepare the inner table, laying out the selection elements
147   - $res .= sprintf("<table%s>\n", ($class)?" class=$class":"");
148   -
149   - ## Build $this->conditions many selection elements
150   - for ($i=1; $i<= $this->conditions; $i++) {
151   - $res .= sprintf(" <tr%s>\n", ($class)?" class=$class":"");
152   -
153   - ## Build conjunction (first row does not have a conjunction)
154   - if ($i == 1) {
155   - $res .= sprintf(" <td%s>%s</td>\n",
156   - ($class)?" class=$class":"",
157   - $this->dict[$this->lang]["searchfor"]);
158   - } else {
159   - $res .= sprintf(" <td%s>%s</td>\n",
160   - ($class)?" class=$class":"",
161   - $this->selection($this->makename($base, "conj", $i),
162   - array("and" => $this->dict[$this->lang]["and"], "or" => $this->dict[$this->lang]["or"]),
163   - $GLOBALS[$base]["conj_".$i],
164   - "on",
165   - $class));
166   - }
167   -
168   - ## Build field selection
169   - $res .= sprintf(" <td%s>%s</td>\n",
170   - ($class)?" class=$class":"",
171   - $this->selection(
172   - $this->makename($base, "sel", $i),
173   - $option,
174   - $GLOBALS[$base]["sel_".$i],
175   - $this->translate,
176   - $class));
177   -
178   - ## Build comparison selection
179   - $res .= sprintf(" <td%s>%s</td>\n",
180   - ($class)?" class=$class":"",
181   - $this->selection(
182   - $this->makename($base, "comp", $i),
183   - $this->compare,
184   - $GLOBALS[$base]["comp_".$i],
185   - "on",
186   - $class));
187   - ## Create text input field.
188   - $res .= sprintf(" <td%s><input type=\"text\" name=\"%s\" value=\"%s\" size=%d maxlength=%d%s></td>\n",
189   - ($class)?" class=$class":"",
190   - $this->makename($base, "input", $i),
191   - $GLOBALS[$base]["input_".$i],
192   - $this->input_size,
193   - $this->input_max,
194   - ($class)?" class=$class":"");
195   -
196   - $res .= sprintf(" </tr>\n");
197   - }
198   -
199   - ## Create variable size buttons
200   - $res .= sprintf(" <tr%s>\n", ($class)?" class=$class":"");
201   - $res .= sprintf(" <td%s>&nbsp;</td>\n", ($class)?" class=$class":"");
202   -
203   - if ($this->variable) {
204   - $res .= sprintf(" <td%s><input type=\"submit\" name=\"%s\" value=\"%s\">&nbsp;",
205   - ($class)?" class=$class":"",
206   - $this->makename($base, "more", 0),
207   - $this->dict[$this->lang]["more"]);
208   - $res .= sprintf("<input type=\"submit\" name=\"%s\"value=\"%s\"></td>\n",
209   - $this->makename($base, "less", 0),
210   - $this->dict[$this->lang]["less"]);
211   - } else {
212   - $res .= sprintf(" <td%s>&nbsp;</td>\n", ($class)?" class=$class":"");
213   - }
214   -
215   - $res .= sprintf(" <td%s>&nbsp;</td>\n", ($class)?" class=$class":"");
216   - $res .= sprintf(" <td%s><input type=\"reset\" value=\"%s\">&nbsp;",
217   - ($class)?" class=$class":"",
218   - $this->dict[$this->lang]["reset"]);
219   - $res .= sprintf("<input type=\"submit\" name=\"%s\"value=\"%s\"></td>\n",
220   - $this->makename($base, "submit", 0),
221   - $this->dict[$this->lang]["submit"]);
222   -
223   - $res .= sprintf(" </tr>\n");
224   - $res .= sprintf("</table>\n");
225   -
226   - $res .= sprintf("</form>\n");
227   - if ($this->container) {
228   - $res .= sprintf("</td></tr></table>\n");
229   - }
230   - $res .= sprintf("<!-- End %s generated query form -->\n", $this->classname);
231   -
232   - return $res;
233   - }
234   -
235   - ## plain_where:
236   - ##
237   - ## Given a base variable name, creates a condition suitable for
238   - ## the where clause of a SQL query.
239   - ##
240   - function plain_where($base) {
241   - for($i=1; $i<=$this->conditions; $i++) {
242   - ## Only create conditions for used input fields
243   - if ($GLOBALS[$base]["input_".$i] == "")
244   - continue;
245   -
246   - ## If necessary, add conjunction
247   - if ($q != "")
248   - $q .= sprintf(" %s ", $GLOBALS[$base]["conj_".$i]);
249   -
250   - ## Handle "like"
251   - if ($GLOBALS[$base]["comp_".$i] == "like")
252   - $v = "%".$GLOBALS[$base]["input_".$i]."%";
253   - else
254   - $v = $GLOBALS[$base]["input_".$i];
255   -
256   - ## Create subcondition
257   - $q .= sprintf("%s %s '%s'",
258   - $GLOBALS[$base]["sel_".$i],
259   - $GLOBALS[$base]["comp_".$i],
260   - $v);
261   - }
262   -
263   - if (!$q) {
264   - $q = "1=0";
265   - }
266   -
267   - return "( $q )";
268   - }
269   -
270   - ## translated_plain_where:
271   - ##
272   - ## Given a base variable name, creates a translated version of
273   - ## the where clause of a SQL query.
274   - ##
275   - function translated_plain_where($base, $field) {
276   - for($i=1; $i<=$this->conditions; $i++) {
277   - ## Only create conditions for used input fields
278   - if ($GLOBALS[$base]["input_".$i] == "")
279   - continue;
280   -
281   - ## If necessary, add conjunction
282   - if ($q != "")
283   - $q .= sprintf(" %s ", $this->dict[$this->lang][$GLOBALS[$base]["conj_".$i]]);
284   -
285   - ## Handle "like"
286   - if ($GLOBALS[$base]["comp_".$i] == "like")
287   - $c = $this->dict[$this->lang][$GLOBALS[$base]["comp_".$i]];
288   - else
289   - $c = $this->compare[$GLOBALS[$base]["comp_".$i]];
290   -
291   - ## Create subcondition
292   - $q .= sprintf("%s %s '%s'",
293   - $field[$GLOBALS[$base]["sel_".$i]],
294   - $c,
295   - $GLOBALS[$base]["input_".$i]);
296   - }
297   -
298   - if (!$q) {
299   - $q = "1=0";
300   - }
301   -
302   - return "( $q )";
303   - }
304   -
305   - ## where:
306   - ##
307   - ## Same as plain_where(), but also inspects the submit button
308   - ## used to submit the query. Changes $this->conditions appropriately.
309   - function where($base, $incr = 1) {
310   - if (isset($GLOBALS[$base]["less_0"]))
311   - $this->conditions -= $incr;
312   -
313   - if (isset($GLOBALS[$base]["more_0"]))
314   - $this->conditions += $incr;
315   -
316   - if ($this->conditions < 1)
317   - $this->conditions = 1;
318   -
319   - return $this->plain_where($base);
320   - }
321   -}
322   -?>