";
- ora_commiton($this->Link_ID);
- }
- if($this->Debug) {
- printf("
connect() Obtained the Link_ID: $this->Link_ID
\n");
- }
- }
- }
-
- ## In order to increase the # of cursors per system/user go edit the
- ## init.ora file and increase the max_open_cursors parameter. Yours is on
- ## the default value, 100 per user.
- ## We tried to change the behaviour of query() in a way, that it tries
- ## to safe cursors, but on the other side be carefull with this, that you
- ## don't use an old result.
- ##
- ## You can also make extensive use of ->disconnect()!
- ## The unused QueryIDs will be recycled sometimes.
-
- function query($Query_String) {
-
- /* No empty queries, please, since PHP4 chokes on them. */
- if ($Query_String == "")
- /* The empty query string is passed on from the constructor,
- * when calling the class without a query, e.g. in situations
- * like these: '$db = new DB_Sql_Subclass;'
- */
- return 0;
-
- $this->connect();
- $this->lastQuery=$Query_String;
-
- if (!$this->Query_ID) {
- $this->Query_ID= ora_open($this->Link_ID);
- }
- if($this->Debug) {
- printf("Debug: query = %s
\n", $Query_String);
- printf("
Debug: Query_ID: %d
\n", $this->Query_ID);
- }
-
- if(!@ora_parse($this->Query_ID,$Query_String)) {
- $this->Errno=ora_errorcode($this->Query_ID);
- $this->Error=ora_error($this->Query_ID);
- $this->halt("
ora_parse() failed:
$Query_String
Snap & paste this to sqlplus!");
- } elseif (!@ora_exec($this->Query_ID)) {
- $this->Errno=ora_errorcode($this->Query_ID);
- $this->Error=ora_error($this->Query_ID);
- $this->halt("
\n$Query_String\n
Snap & paste this to sqlplus!");
- }
-
- $this->Row=0;
-
- if(!$this->Query_ID) {
- $this->halt("Invalid SQL: ".$Query_String);
- }
-
- return $this->Query_ID;
- }
-
- function next_record() {
- if (!$this->no_next_fetch &&
- 0 == ora_fetch($this->Query_ID)) {
- if ($this->Debug) {
- printf("
next_record(): ID: %d,Rows: %d
\n",
- $this->Query_ID,$this->num_rows());
- }
- $this->Row +=1;
-
- $errno=ora_errorcode($this->Query_ID);
- if(1403 == $errno) { # 1043 means no more records found
- $this->Errno=0;
- $this->Error="";
- $this->disconnect();
- $stat=0;
- } else {
- $this->Error=ora_error($this->Query_ID);
- $this->Errno=$errno;
- if($this->Debug) {
- printf("
%d Error: %s",
- $this->Errno,
- $this->Error);
- }
- $stat=0;
- }
- } else {
- $this->no_next_fetch=false;
- for($ix=0;$ix
\n";
- }
- $stat=1;
- }
-
- return $stat;
- }
-
- ## seek() works only for $pos - 1 and $pos
- ## Perhaps I make a own implementation, but my
- ## opinion is, that this should be done by PHP3
- function seek($pos) {
- if ($this->Row - 1 == $pos) {
- $this->no_next_fetch=true;
- } elseif ($this->Row == $pos ) {
- ## do nothing
- } else {
- $this->halt("Invalid seek(): Position is cannot be handled by API.
".
- "Difference too big. Wanted: $pos Current pos: $this->Row");
- }
- if ($Debug) echo "
Debug: seek = $pos
";
- $this->Row=$pos;
- }
-
- function lock($table, $mode = "write") {
- if ($mode == "write") {
- $result = ora_do($this->Link_ID, "lock table $table in row exclusive mode");
- } else {
- $result = 1;
- }
- return $result;
- }
-
- function unlock() {
- return ora_do($this->Link_ID, "commit");
- }
-
- function metadata($table,$full=false) {
- $count = 0;
- $id = 0;
- $res = array();
-
- /*
- * Due to compatibility problems with Table we changed the behavior
- * of metadata();
- * depending on $full, metadata returns the following values:
- *
- * - full is false (default):
- * $result[]:
- * [0]["table"] table name
- * [0]["name"] field name
- * [0]["type"] field type
- * [0]["len"] field length
- * [0]["flags"] field flags ("NOT NULL", "INDEX")
- * [0]["format"] precision and scale of number (eg. "10,2") or empty
- * [0]["index"] name of index (if has one)
- * [0]["chars"] number of chars (if any char-type)
- *
- * - full is true
- * $result[]:
- * ["num_fields"] number of metadata records
- * [0]["table"] table name
- * [0]["name"] field name
- * [0]["type"] field type
- * [0]["len"] field length
- * [0]["flags"] field flags ("NOT NULL", "INDEX")
- * [0]["format"] precision and scale of number (eg. "10,2") or empty
- * [0]["index"] name of index (if has one)
- * [0]["chars"] number of chars (if any char-type)
- * ["meta"][field name] index of field named "field name"
- * The last one is used, if you have a field name, but no index.
- * Test: if (isset($result['meta']['myfield'])) {} ...
- */
-
- $this->connect();
-
- ## This is a RIGHT OUTER JOIN: "(+)", if you want to see, what
- ## this query results try the following:
- ## $table = new Table; $db = new my_DB_Sql; # you have to make
- ## # your own class
- ## $table->show_results($db->query(see query vvvvvv))
- ##
- $this->query("SELECT T.table_name,T.column_name,T.data_type,".
- "T.data_length,T.data_precision,T.data_scale,T.nullable,".
- "T.char_col_decl_length,I.index_name".
- " FROM ALL_TAB_COLUMNS T,ALL_IND_COLUMNS I".
- " WHERE T.column_name=I.column_name (+)".
- " AND T.table_name=I.table_name (+)".
- " AND T.table_name=UPPER('$table') ORDER BY T.column_id");
-
- $i=0;
- while ($this->next_record()) {
- $res[$i]["table"] = $this->Record[table_name];
- $res[$i]["name"] = strtolower($this->Record[column_name]);
- $res[$i]["type"] = $this->Record[data_type];
- $res[$i]["len"] = $this->Record[data_length];
- if ($this->Record[index_name]) $res[$i]["flags"] = "INDEX ";
- $res[$i]["flags"] .= ( $this->Record[nullable] == 'N') ? '' : 'NOT NULL';
- $res[$i]["format"]= (int)$this->Record[data_precision].",".
- (int)$this->Record[data_scale];
- if ("0,0"==$res[$i]["format"]) $res[$i]["format"]='';
- $res[$i]["index"] = $this->Record[index_name];
- $res[$i]["chars"] = $this->Record[char_col_decl_length];
- if ($full) {
- $j=$res[$i]["name"];
- $res["meta"][$j] = $i;
- $res["meta"][strtoupper($j)] = $i;
- }
- if ($full) $res["meta"][$res[$i]["name"]] = $i;
- $i++;
- }
- if ($full) $res["num_fields"]=$i;
-# $this->disconnect();
- return $res;
- }
-
- ## THIS FUNCTION IS UNSTESTED!
- function affected_rows() {
- if ($Debug) echo "
Debug: affected_rows=". ora_numrows($this->Query_ID)."
";
- return ora_numrows($this->Query_ID);
- }
-
- ## Known bugs: It will not work for SELECT DISTINCT and any
- ## other constructs which are depending on the resulting rows.
- ## So you *really need* to check every query you make, if it
- ## will work with it.
- ##
- ## Also, for a qualified replacement you need to parse the
- ## selection, cause this will fail: "SELECT id, from FROM ...").
- ## "FROM" is - as far as I know a keyword in Oracle, so it can
- ## only be used in this way. But you have been warned.
- function num_rows() {
- $curs=ora_open($this->Link_ID);
-
- ## this is the important part and it is also the HACK!
- if (eregi("^[[:space:]]*SELECT[[:space:]]",$this->lastQuery) ) {
- $from_pos = strpos(strtoupper($this->lastQuery),"FROM");
- $q = "SELECT count(*) ". substr($this->lastQuery, $from_pos);
-
- ORA_parse($curs,$q);
- ORA_exec($curs);
- ORA_fetch($curs);
- if ($Debug) echo "
Debug: num_rows=". ORA_getcolumn($curs,0)."
";
- return(ORA_getcolumn($curs,0));
- } else {
- $this->halt("Last Query was not a SELECT: $this->lastQuery");
- }
- }
-
- function num_fields() {
- if ($Debug) echo "
Debug: num_fields=". ora_numcols($this->Query_ID) . "
";
- return ora_numcols($this->Query_ID);
- }
-
- function nf() {
- return $this->num_rows();
- }
-
- function np() {
- print $this->num_rows();
- }
-
- function f($Name) {
- return $this->Record[$Name];
- }
-
- function p($Name) {
- print $this->Record[$Name];
- }
-
- /* public: sequence number */
- function nextid($seq_name)
- {
- $this->connect();
-
- /* Independent Query_ID */
- $Query_ID = ora_open($this->Link_ID);
-
- if(!@ora_parse($Query_ID,"SELECT $seq_name.NEXTVAL FROM DUAL"))
- {
- // There is no such sequence yet, then create it
- if(!@ora_parse($Query_ID,"CREATE SEQUENCE $seq_name")
- ||
- !@ora_exec($Query_ID)
- )
- {
- $this->halt("
nextid() function - unable to create sequence");
- return 0;
- }
- @ora_parse($Query_ID,"SELECT $seq_name.NEXTVAL FROM DUAL");
- }
- if (!@ora_exec($Query_ID)) {
- $this->halt("
ora_exec() failed:
nextID function");
- }
- if (@ora_fetch($Query_ID) ) {
- $next_id = ora_getcolumn($Query_ID, 0);
- }
- else {
- $next_id = 0;
- }
- if ( Query_ID > 0 ) {
- ora_close(Query_ID);
- }
-
- return $next_id;
- }
-
- function disconnect() {
- if($this->Debug) {
- echo "Debug: Disconnecting $this->Query_ID...
\n";
- }
- if ( $this->Query_ID < 1 ) {
- echo "Warning: disconnect(): Cannot free ID $this->Query_ID\n";
-# return();
- }
- ora_close($this->Query_ID);
- $this->Query_ID=0;
- }
-
- /* private: error handling */
- function halt($msg) {
- if ($this->Halt_On_Error == "no")
- return;
-
- $this->haltmsg($msg);
-
- if ($this->Halt_On_Error != "report")
- die("Session halted.");
- }
-
- function haltmsg($msg) {
- printf("Database error: %s
\n", $msg);
- printf("Oracle Error: %s (%s)
\n",
- $this->Errno,
- $this->Error);
- }
-
- function table_names() {
- $this->connect();
- $this->query("
- SELECT table_name,tablespace_name
- FROM user_tables");
- $i=0;
- while ($this->next_record())
- {
- $info[$i]["table_name"] =$this->Record["table_name"];
- $info[$i]["tablespace_name"]=$this->Record["tablespace_name"];
- $i++;
- }
- return $info;
- }
-}
-?>
diff --git a/phplib/db_pgsql.inc b/phplib/db_pgsql.inc
deleted file mode 100755
index 039defa..0000000
--- a/phplib/db_pgsql.inc
+++ /dev/null
@@ -1,225 +0,0 @@
-query($query);
- }
-
- function connect() {
- if ( 0 == $this->Link_ID ) {
- $cstr = "dbname=".$this->Database.
- $this->ifadd($this->Host, "host=").
- $this->ifadd($this->Port, "port=").
- $this->ifadd($this->User, "user=").
- $this->ifadd($this->Password, "password=");
- $this->Link_ID=pg_pconnect($cstr);
- if (!$this->Link_ID) {
- $this->halt("Link-ID == false, pconnect failed");
- }
- }
- }
-
- function query($Query_String) {
- /* No empty queries, please, since PHP4 chokes on them. */
- if ($Query_String == "")
- /* The empty query string is passed on from the constructor,
- * when calling the class without a query, e.g. in situations
- * like these: '$db = new DB_Sql_Subclass;'
- */
- return 0;
-
- $this->connect();
-
-# printf("
Debug: query = %s
\n", $Query_String);
-
- $this->Query_ID = pg_Exec($this->Link_ID, $Query_String);
- $this->Row = 0;
-
- $this->Error = pg_ErrorMessage($this->Link_ID);
- $this->Errno = ($this->Error == "")?0:1;
- if (!$this->Query_ID) {
- $this->halt("Invalid SQL: ".$Query_String);
- }
-
- return $this->Query_ID;
- }
-
- function next_record() {
- $this->Record = @pg_fetch_array($this->Query_ID, $this->Row++);
-
- $this->Error = pg_ErrorMessage($this->Link_ID);
- $this->Errno = ($this->Error == "")?0:1;
-
- $stat = is_array($this->Record);
- if (!$stat && $this->Auto_Free) {
- pg_freeresult($this->Query_ID);
- $this->Query_ID = 0;
- }
- return $stat;
- }
-
- function seek($pos) {
- $this->Row = $pos;
- }
-
- function lock($table, $mode = "write") {
- if ($mode == "write") {
- $result = pg_Exec($this->Link_ID, "lock table $table");
- } else {
- $result = 1;
- }
- return $result;
- }
-
- function unlock() {
- return pg_Exec($this->Link_ID, "commit");
- }
-
-
- /* public: sequence numbers */
- function nextid($seq_name) {
- $this->connect();
-
- if ($this->lock($this->Seq_Table)) {
- /* get sequence number (locked) and increment */
- $q = sprintf("select nextid from %s where seq_name = '%s'",
- $this->Seq_Table,
- $seq_name);
- $id = @pg_Exec($this->Link_ID, $q);
- $res = @pg_Fetch_Array($id, 0);
-
- /* No current value, make one */
- if (!is_array($res)) {
- $currentid = 0;
- $q = sprintf("insert into %s values('%s', %s)",
- $this->Seq_Table,
- $seq_name,
- $currentid);
- $id = @pg_Exec($this->Link_ID, $q);
- } else {
- $currentid = $res["nextid"];
- }
- $nextid = $currentid + 1;
- $q = sprintf("update %s set nextid = '%s' where seq_name = '%s'",
- $this->Seq_Table,
- $nextid,
- $seq_name);
- $id = @pg_Exec($this->Link_ID, $q);
- $this->unlock();
- } else {
- $this->halt("cannot lock ".$this->Seq_Table." - has it been created?");
- return 0;
- }
- return $nextid;
- }
-
-
-
- function metadata($table) {
- $count = 0;
- $id = 0;
- $res = array();
-
- $this->connect();
- $id = pg_exec($this->Link_ID, "select * from $table");
- if ($id < 0) {
- $this->Error = pg_ErrorMessage($id);
- $this->Errno = 1;
- $this->halt("Metadata query failed.");
- }
- $count = pg_NumFields($id);
-
- for ($i=0; $i<$count; $i++) {
- $res[$i]["table"] = $table;
- $res[$i]["name"] = pg_FieldName ($id, $i);
- $res[$i]["type"] = pg_FieldType ($id, $i);
- $res[$i]["len"] = pg_FieldSize ($id, $i);
- $res[$i]["flags"] = "";
- }
-
- pg_FreeResult($id);
- return $res;
- }
-
- function affected_rows() {
- return pg_cmdtuples($this->Query_ID);
- }
-
- function num_rows() {
- return pg_numrows($this->Query_ID);
- }
-
- function num_fields() {
- return pg_numfields($this->Query_ID);
- }
-
- function nf() {
- return $this->num_rows();
- }
-
- function np() {
- print $this->num_rows();
- }
-
- function f($Name) {
- return $this->Record[$Name];
- }
-
- function p($Name) {
- print $this->Record[$Name];
- }
-
- function halt($msg) {
- printf("Database error: %s
\n", $msg);
- printf("PostgreSQL Error: %s (%s)
\n",
- $this->Errno,
- $this->Error);
- die("Session halted.");
- }
-
- function table_names() {
- $this->query("select relname from pg_class where relkind = 'r' and not relname like 'pg_%'");
- $i=0;
- while ($this->next_record())
- {
- $return[$i]["table_name"]= $this->f(0);
- $return[$i]["tablespace_name"]=$this->Database;
- $return[$i]["database"]=$this->Database;
- $i++;
- }
- return $return;
- }
-}
-?>
diff --git a/phplib/db_sybase.inc b/phplib/db_sybase.inc
deleted file mode 100755
index 63f9e15..0000000
--- a/phplib/db_sybase.inc
+++ /dev/null
@@ -1,148 +0,0 @@
-
- *
- * metadata() contributed by Adelino Monteiro
\n", $Query_String);
-
- $this->Query_ID = sybase_query($Query_String,$this->Link_ID);
- $this->Row = 0;
- if (!$this->Query_ID) {
- $this->halt("Invalid SQL: ".$Query_String);
- }
-
- return $this->Query_ID;
- }
-
- function next_record() {
- $this->Record = sybase_fetch_array($this->Query_ID);
- $this->Row += 1;
-
- $stat = is_array($this->Record);
- if (!$stat && $this->Auto_Free) {
- sybase_free_result($this->Query_ID);
- $this->Query_ID = 0;
- }
- return $stat;
- }
-
- function seek($pos) {
- $status = sybase_data_seek($this->Query_ID, $pos);
- if ($status)
- $this->Row = $pos;
- return;
- }
-
- function metadata($table) {
- $count = 0;
- $id = 0;
- $res = array();
-
- $this->connect();
- $result = $this->query("exec sp_columns $table");
- if ($result < 0) {
- $this->Errno = 1;
- $this->Error = "Metadata query failed";
- $this->halt("Metadata query failed.");
- }
- $count = sybase_num_rows($result);
-
- for ($i=0; $i<$count; $i++) {
- $res[$i]["table"] = $table ;
- $res[$i]["name"] = sybase_result ($result, $i, "COLUMN_NAME");
- $res[$i]["type"] = sybase_result ($result, $i, "TYPE_NAME");
- $res[$i]["len"] = sybase_result ($result, $i, "LENGTH");
- $res[$i]["position"] = sybase_result ($result, $i, "ORDINAL_POSITION");
- $res[$i]["flags"] = sybase_result ($result, $i, "REMARKS");
-
- }
- }
-
- function affected_rows() {
- return sybase_affected_rows($this->Query_ID);
- }
-
- function num_rows() {
- return sybase_num_rows($this->Query_ID);
- }
-
- function num_fields() {
- return sybase_num_fields($this->Query_ID);
- }
-
- function nf() {
- return $this->num_rows();
- }
-
- function np() {
- print $this->num_rows();
- }
-
- function f($Name) {
- return $this->Record[$Name];
- }
-
- function p($Name) {
- print $this->Record[$Name];
- }
-
- function halt($msg) {
- printf("Database error: %s
\n", $msg);
- printf("Sybase Error
\n");
- die("Session halted.");
- }
-}
-?>
diff --git a/phplib/db_usql.inc b/phplib/db_usql.inc
deleted file mode 100755
index 3bf5987..0000000
--- a/phplib/db_usql.inc
+++ /dev/null
@@ -1,76 +0,0 @@
-Record))
- if (ereg("[A-Za-z][A-Za-z0-9_]*", $key)) {
- $field_name = strtoupper($key);
- global $$field_name;
- $$field_name=$val;
- };
- }
-
-//--------------------------------------------------------------
-// this function can be used to export all the records of
-// a table on the output in the form of a call to the db_sql
-// query function with an insert statement.
-//--------------------------------------------------------------
- function dump_table($tablename, $filter="") {
- $this->query(sprintf("select * from %s", $tablename));
- while ($this->next_record()) {
- $this->dump_record($tablename, $filter);
- };
- }
-
-//--------------------------------------------------------------
-// this function can be used to export all the records of
-// a query on the output in the form of a call to the db_sql
-// query function with an insert statement.
-//--------------------------------------------------------------
- function dump_query($tablename, $filter="") {
- //$this->query(sprintf("select * from %s", $tablename));
- while ($this->next_record()) {
- $this->dump_record($tablename, $filter);
- };
- }
-
- function dump_record($tablename, $filter="") {
- $fields="";
- $values="";
- while (list($key, $val) = each($this->Record))
- if (ereg("[A-Za-z][A-Za-z0-9_]*", $key)) {
- $field_name = strtoupper($key);
- if (!empty($val))
- if (strstr( $filter, $field_name )=="") {
- $fields.="$field_name ,";
- $val=ereg_replace("'","''",$val);
- $val=ereg_replace("\"","\\\"",$val);
- //addslashes($val);
- $values.="'$val' ,";
- };
- }
- $fields=substr($fields, 0, strlen($fields)-1);
- $values=substr($values, 0, strlen($values)-1);
- $insert=sprintf("insert into %s(%s) values(%s)", $tablename, $fields, $values);
- echo "\$db->query(\"$insert\");\n";
- }
- };
-
-?>
diff --git a/phplib/query_sql.inc b/phplib/query_sql.inc
deleted file mode 100755
index d029e94..0000000
--- a/phplib/query_sql.inc
+++ /dev/null
@@ -1,742 +0,0 @@
-
- * I took many ideas from his SQL.inc, thanks! :-)
- * The idea is of this class is based in November 1997,
- * it was a collection of functions for PHP/FI and mSQL.
- *
- * $Id$
- *
- */
-
-
-/*
-The Query-class is an enhancement to the db_*-classes. Currently It supports
-mySQL an Oracle but it is easy expandable. See down.
-
-It always needs the class DB_Sql!
-
-Query is fully upward compatible with DB_Sql. Example:
-
-OLD: NEW:
-
-require("db_mysql.inc"); require("db_mysql.inc");
-class hugobla extends DB_sql {} require("query_sql.inc");
-$db = new hugobla; class hugobla extends Query {}
- $db = new hugobla;
-
-It just provides a number of new functions.
-
-The Query-class is inteded to help you with creating selects, inserts,
-updates, where-clauses etc. Not just *simple* selects, but longer ones. It
-is indeed a great help for tables with more than 10-20 columns. But it can
-also be used for simple and small selects. The inbuilt checks help you
-programming saver.
-
-Here is an example:
-
-file: insert.php3
-------------------
-
-## gets data from my form and inserts it into db
-
-require("prepend.inc"); ## here the classes are loaded and configured
-$db = new hugobla;
-$db->query($db->insert_plain_Clause("mytable",$db->capture_vars("mytable"),ARRAY()));
-echo "Values inserted";
-
-?>
-
-file: insert2.php3
--------------------
-
-## gets data from my form and inserts it into db with a new INDEX
-## myindex is defined as INT AUTO_INCREMENT PRIMARY KEY
-## (this is mysql, in oracle you have to define a trigger)
-## mytime is defined as DATETIME (DATE in oracle)
-
-require("prepend.inc"); ## here the classes are loaded and configured
-$db = new hugobla;
-$mytime="SYSDATE()";
-$db->query($db->insert_plain_Clause("mytable",$db->capture_vars("mytable"),
- ARRAY(myindex=>'NULL',mytime='func')));
-echo "Values inserted: "$db->last_insert_id();
-
-?>
-
-This example is nice, cause you see how easy it can be used. :-)
-
-The best thing is, that you don't have to care, if a field is a string or a
-number. The values are automatically converted into the right form. The type
-of the vars are read from the table. Stringvalues are encapsulated with '
-(configurable) and escaped (the code for this is currently not good - we are
-assuming, that PHP is configured not to encapsulate strings), int-
-and real-values are casted. It can handle "NULL"-values, function-statements
-or other values for insertion.
-
-You will make less errors.
-
-mySQL and most other DB's accept a a short form of insert-clause (INSERT
-INTO bla VALUES (...)). The Query-class will always make the longer form
-(INSERT INTO BLA (...) VALUES (...)). This makes it possible to use ALTER
-TABLE-commands without changing the program! E.g. changing a field in a
-table from NUMBER to VARCHAR(10) is fully encapsulated with this class.
-
-The class supports currently only mysql and oracle. I think the differences
-between the DBs are encapsulated enough in the db_* classes, so it is
-possible to handle the remaining small differences inside this class (this
-affects mainly the function sql2phptype() ) and it could be easiely extended
-(asuming, that the metadata()-function of the db_*-class works correctly).
-In this case it is important, that the $type-variable in the db_*.inc-class
-is correctly set.
-
-
-TODO-list:
-- A method to create querys like the LIMIT-clause in mySQL. For Oracle
- this works:
-
- select linenum, foo, bar
- from (select rownum as linenum, foo, bar from
- (select foo,bar from chaos order by bar) )
- where linenum between 11 and 20;
-
-- cleaner escaping, handling of \ and NUL (current code is bullshit)
- Some ideas?
-
-- Little Alta-Vista: add functions to create a where clause from a search
- string with rules to handle searching for more than one word.
- half automatic generating search patterns into a where-clause
- simple search engine support, simple support for semi full-text-search
-
-- automatic configurable manipulation of values, eg.
- triming of strings (delete whitespace at begin and end)
- also : TOUPPER, TOLOWER etc
-
-- SELECT-Clause (GROUP BY, HAVING, JOIN...)
-
-- make new functions insert_Clause() etc. which inserts only the
- fields they got from your call (the current will do "plain" insert)
-
-- where_Clause() - creating WHERE for select, update, exists etc.
-
-- serv all queries directly into db, return just the handle
- (hm, how to deal with the DB-handle?)
-
-- Return a 2-dimensional (Table-compatible) field from select (not so important)
-
-- The sql2phptype() can be used to help creating automatic input forms
- for a table
-
-DEPENDING:
-- db_mysql: new function metatabledata(), which returns the table-info from
- current selected table (will return multiple table-columns with a join)
-- db_mysql: perhaps the function sql2phptype() should be placed there?
-
-
-For developers of new db_*.inc: the function metadata() is very important
-for the correct work of this class. T
-
-*/
-
-class Query extends DB_Sql {
-
- ## DONT FORGET to set the variables from DB_Sql! See there!
-
- ## For debugging: if set to TRUE the Query is printed out,
- ## before executing or returning
- var $Q_Debug=false;
-
- ## set this to another value, if you want to hide it
- ## in your HTML-code
- ## example: var $Q_Debug_print="\n\n";
- var $Q_Debug_print="
QDebug:\n%s\n
\n";
-
- ## Set this to TRUE if you only want to test, which query
- ## will be created (ideal in combination with $Q_Debug)
- ## This depends only functions which will make changes
- var $No_Write=false;
-
- ## currently unused, this var is just an idea for later use.
- var $Backslash_N_is_NULL = false;
-
- ## Metacache: see funtcion metadata_buffered()
- var $meta_cache_off = false;
-
- ## This is the char, which will be replaced by \char.
- ## PHP3 seems to be NOT binary-safe, so not quoting
- ## for \0 (some ideas?)
- ## we use ereg_replace to do the replacements.
- ## with PHP3.0.6 you should replace this with str_replace()!
- ## Quoting = 1 -> normal quoting using AddSlashes
- ## 2 -> Replace \' to '' - needed eg. for sybase or oracle
- var $Quoting=1;
- var $Quotechar="'";
-
- var $StrLengTrunc = false;
- var $StrLengWarn = false;
-
- ###########################
- ## _QDebug
- function _QDebug ($str) {
- if ($this->Q_Debug) {
- printf($this->Q_Debug_print,$str);
- }
- }
-
- ###########################
- ## Set DB-Classname
- ## This is only a 3rd posibility for setting the classname
- ##
- function set_db_class ($db_class) {
- $this->Database=$db_class;
- }
-
-
- ###########################
- ## This function gets a datatype from the DB and returns an
- ## equivalent datatype for PHP
- ##
- ## It returns also a subtype for a string
- ##
- function sql2phptype ($type,$format='') {
- ## $this->type is currently either "mysql" or "oracle"
- switch ($this->type) {
- case "mysql":
- switch ($type) {
- case "var string":
- case "string" :
- case "char" :
- return(Array("string",""));
- break;
- case "timestamp" :
- case "datetime" :
- case "date" :
- case "time" :
- return(Array("string","date"));
- break;
- case "blob" :
- return(Array("string","blob"));
- break;
- case "real" :
- return(Array("double",""));
- break;
- case "long" :
- default :
- return(Array("int",""));
- break;
- }
- break;
- case "oracle":
- switch ($type) {
- case "VARCHAR2" :
- case "VARCHAR" :
- case "CHAR" :
- return(Array("string",""));
- break;
- case "DATE" :
- return(Array("string","date"));
- break;
- case "BLOB" :
- case "CLOB" :
- case "BFILE" :
- case "RAW" :
- case "LONG" :
- case "LONG RAW" :
- return(Array("string","blob"));
- break;
- case "NUMBER" :
- if ($format) {
- return(Array("double",""));
- } else {
- return(Array("int",""));
- }
- break;
- default :
- $this->halt("sql2phptype(): Type is not a valid value: '$type'");
- break;
- }
- break;
- default:
- $this->halt("sql2phptype(): DB-type is not a valid value: ".$this->type);
- break;
- }
- }
-
-
- #######################################
- ## This function returns a PHP-variable depending
- ## on type. E.g. a string is returned as 'string'
- ##
- ## The parameters mean
- ## $val - the value
- ## There is a special case: If value is "NULL" and
- ## the type is not "string" or subtype is empty, then
- ## a value "NULL" is inserted. This let you just spare
- ## a little bit work with $special
- ##
- ## $meta - the meta information for this field (that's what
- ## is returned by metadata() from DB_sql-class, but just one
- ## single row, e.g. $meta[2], not hole $meta).
- ##
- ## $special - Overwrites the type of the var if set. Some special
- ## meanings:
- ## "NULL" means, that this value must be set to "NULL"
- ## "func" means, that $val should be untouched -
- ## e.g. to insert the value of a SQL-function
- ## [ INSERT INTO bla VALUES ( time=NOW() ) ]
- ##
-
- function convert ($val,$meta,$special="") {
- list($type,$subtype)=$this->sql2phptype($meta["type"],$meta["format"]);
- if (($val == "NULL" &&
- ($type != "string" || $type[1] != "")) ||
- $special == "NULL") {
- $type="NULL";
- } else {
- if ($special) {
- $type=$special;
- if ($type!="func") {
- $val=$type;
- $type="func";
- }
- }
- }
- switch ($type) {
- case "string" :
- $val=(string)$val;
- if ($this->Quoting) {
- $val=AddSlashes($val);
- }
- if ($this->Quoting==2) {
- $val=str_replace("\\'","''",$val);
- }
- if ($subtype!='date' &&
- ( $this->StrLengTrunc || $this->StrLengWarn ) ) {
- if ( strlen($val) > $meta[len] ) {
- if ($this->StrLengWarn) {
- echo "
STRING TOO LONG: '$meta[name]'";
- if ($this->StrLengTrunc) {
- echo ", TRUNCATING!";
- }
- }
- if ($this->StrLengTrunc) {
- $val=substr($val,0,$meta[len]);
- }
- }
- }
- $val=$this->Quotechar . $val . $this->Quotechar;
- break;
- case "int" :
- $val=(int)$val;
- break;
- case "double" :
- $val=(double)$val;
- break;
- case "NULL" :
- $val="NULL";
- break;
- case "func" :
- $val=(string)$val;
- break;
- default :
- echo "UNKNOWN TYPE: $type
";
- }
- $this->_QDebug("Val: $meta[name] => $val
");
- return(Array($val,$meta["name"]));
- }
-
-
- ##
- ## Function to generate a plain INSERT-Clause
- ## ("plain" means, that every field in the table will
- ## be set to a value, default is '' or 0 if nothing said
- ## in $special)
- ##
- ## $fields is an assoc. Array consisting out of
- ## table_name => value-pairs
- ## $special is an assoc. field which will commit special
- ## handling to convert() (See there)
- ## $check could be "strong" or "soft".
- ## "soft" won't tell you if there were to less
- ## or too much fields (good for debuging)
- ##
- ## returns the insert clause. It's on you to modify it
- ## and send it to your DB
- ##
- function insert_plain_Clause ($table,$fields,$special,$check="soft") {
- $meta=$this->metadata_buffered($table);
-
- for ($i=0; $i < $meta["num_fields"]; $i++) {
- $j=$meta[$i]["name"];
- ## NOT IMPLEMENTED: SEARCHING FOR $fields[$i]
- list($val["val"][$i],$val["name"][$i])=
- $this->convert($fields[$j],$meta[$i],$special[$j]);
- }
- if (Count($fields)!=Count($val["name"]) && $check=="strong") {
- echo "WARNING: insert_plain_clause(): There are not the same number of".
- " fields as in table for INSERT
";
- }
- $q=sprintf("INSERT INTO %s (%s) VALUES (%s)",
- $table,join($val["name"],","),
- join($val["val"],","));
- $this->_QDebug($q);
- return($q);
- }
-
- # Replace, a special mySQL-function, same as INSERT
- function replace_plain_Clause ($table,$fields,$special,$check="soft") {
- $meta=$this->metadata_buffered($table);
-
- for ($i=0; $i < $meta["num_fields"]; $i++) {
- $j=$meta[$i]["name"];
- ## NOT IMPLEMENTED: SEARCHING FOR $fields[$i]
- list($val["val"][$i],$val["name"][$i])=
- $this->convert($fields[$j],$meta[$i],$special[$j]);
- }
- if (Count($fields)!=Count($val["name"]) && $check=="strong") {
- echo "WARNING: replace_plain_Clause(): There are not the same number of".
- " fields as in table for INSERT
";
- }
- $q=sprintf("REPLACE %s (%s) VALUES (%s)",
- $table,join($val["name"],","),
- join($val["val"],","));
- $this->_QDebug($q);
- return($q);
- }
-
- ##
- ## This function is nearly the same, as insert_plain_Clause,
- ## The where parameter is new and should be generated by yourself
- ## The check parameter knows 3 values: strong, soft and weak
- ## weak enables you to sent a query without $where (enables you
- ## to update the hole table)
- ##
- function update_plain_Clause ($table,$fields,$special,$where,$check="soft") {
- $meta=$this->metadata_buffered($table);
- if (!$where && $check!="weak") {
- echo "ERROR: update_plain_Clause(): Parameter \$where is empty!
";
- return(false);
- }
-
- for ($i=0; $i < $meta["num_fields"]; $i++) {
- $j=$meta[$i]["name"];
- ## NOT IMPLEMENTED: SEARCHING FOR $fields[$i]
- list($val["val"][$i],$val["name"][$i])=
- $this->convert($fields[$j],$meta[$i],$special[$j]);
-#echo "V: ".$val["name"][$i]." : ". $val["val"][$i]." - ".$fields[$j]."
";
- }
- if (Count($fields)!=Count($val["name"]) && $check=="strong") {
- echo "WARNING: update_plain_Clause(): There are not the same number of".
- " fields for INSERT
";
- }
- for ($i=0 ; $i < Count ($val["name"]); $i++ ) {
- $s[]=$val["name"][$i]."=".$val["val"][$i];
- }
- $q=sprintf("UPDATE %s SET %s",$table,join($s,","));
- if ($where) {
- if (!eregi("^[[:space:]]*WHERE",$where)) {
- ## insert "WHERE" if not set
- $where="WHERE $where";
- }
- $q.=" $where";
- }
- $this->_QDebug($q);
- return($q);
- }
-
-
- ##
- ## This function is nearly the same, as insert_Clause,
- ## The where parameter is new and should be generated by yourself
- ## The check parameter knows 3 values: strong, soft and weak
- ## weak enables you to sent a query without $where (enables you
- ## to update the hole table)
- ##
- function update_Clause ($table,$fields,$special,$where,$check="soft") {
- $meta=$this->metadata_buffered($table);
- if (!$where && $check!="weak") {
- echo "ERROR: update_Clause(): Parameter \$where is empty!
";
- return(false);
- }
-
- $i=0;
- for (reset($fields); list($key,$val)=each($fields); $i++) {
- if ( isset($meta[meta][$key]) ) {
- $j=$meta[meta][$key];
- list($v["val"][$i],$v["name"][$i])=
- $this->convert($val,$meta[$j],$special[$key]);
- }
- }
- for ($i=0 ; $i < Count ($v["name"]); $i++ ) {
- $s[]=$v["name"][$i]."=".$v["val"][$i];
- }
- if (Count($s)) {
- $q=sprintf("UPDATE %s SET %s",$table,join($s,","));
- if ($where) {
- if (!eregi("^[[:space:]]*WHERE",$where)) {
- ## insert "WHERE" if not set
- $where="WHERE $where";
- }
- $q.=" $where";
- }
- }
- $this->_QDebug($q);
- return($q);
- }
-
-
-
- ##
- ## DELETE
- ## deletes the selected Table
- ## $check can be "soft" and "weak". Weak let's you delete the
- ## hole table, if you want
- ##
- function delete_Clause ($table,$where,$check="soft") {
- if (!$where && $check!="weak") {
- echo "ERROR: delete_Clause(): Parameter \$where is empty!
";
- return(false);
- }
-
- $q=sprintf("DELETE FROM %s",$table);
- if ($where) {
- if (!eregi("^[[:space:]]*WHERE",$where)) {
- ## insert "WHERE" if not set
- $where="WHERE $where";
- }
- $q.=" $where";
- }
- $this->_QDebug($q);
- return($q);
- }
-
-
- ##
- ## This function checks wether in table $table a
- ## field $name is set with value $val
- ##
- ## it returns the number of found matches or zero
- ##
- function exists ($table,$name,$val) {
- $meta=$this->metadata_buffered($table);
- $j=$meta["meta"][$name];
- list($k)=$this->convert($val,$meta[$j]);
- $q=sprintf("SELECT COUNT(%s) as c FROM %s WHERE %s=%s",
- $name,$table,$name,$k);
- $this->_QDebugs($q);
- $this->query($q);
- $this->next_record();
- return($this->f("c"));
- }
-
- ##
- ## This function creates a query like exists, but returns
- ## an assoc array of the first found row, or false if nothing found
- ## field $name is set with value $val
- ##
- function getrow ($table,$name,$val) {
- $meta=$this->metadata_buffered($table);
- $j=$meta[meta][$name];
- list($k)=$this->convert($val,$meta[$j]);
- $q=sprintf("SELECT * FROM %s WHERE %s=%s",
- $table,$name,$k);
- $this->_QDebug($q);
- $this->query($q);
- if ($this->next_record()) {
- return($this->Record);
- } else {
- echo "
WARNING: getrow(): KEY: $name VAL: $val not found
";
- return(false);
- }
- }
-
-
-
- ##
- ## WHERE-PLAIN-CLAUSE
- ## Let you generate a WHERE-Clause with a Loop.
- ##
- ## Returns a where-clause beginning with " WHERE "
- ##
- ## This function generates a where-clause
- ## $mywhere An array of simple expressions, eg. "firstname='Alex'"
- ## $andor This string is printed bewtween the where-Array
- ## default is 'AND'. It will handle an existing
- ## $oldwhere correctly. You can set this to '', but then
- ## the correct operator must be set by you in the where
- ## $where an existing WHERE-clause. Default is empty.
- ## $check if 'strong', it will stop, if an empty where-clause
- ## will be returned, to avoid "full" selects. Default is soft
- ##
- function where_plain_Clause ($mywhere,$andor='AND',$where='',$check="soft") {
- $meta=$this->metadata_buffered($table);
- $q='';
-
- for ($i=0; $i
";
- }
- $q=ereg_Replace("^ $andor "," WHERE ",$q);
- $this->_QDebug("where_Clause(): $q");
- return($q);
- }
-
-
- ##
- ## capture-vars
- ##
- ## This function returns an assoc. Array consisting out of
- ## name=>value-pairs needed by all the other functions. It reads
- ## the name of the vars from the fields in $table and the values
- ## from the $GLOBALS-var-field.
- ## This has the sense, that you can name the variables in your
- ## Input-Form exactly like the names in your table. This again
- ## let make you less errors and less side effects.
- ##
- ## $table The name of the table
- ##
- function capture_vars ($table) {
- $meta=$this->metadata_buffered($table);
- $r=Array();
- for ($i=0; $i < $meta["num_fields"]; $i++) {
- $j=$meta[$i]["name"];
- if (isset($GLOBALS[$j])) {
- $r[$j] = $GLOBALS[$j];
- $this->_QDebug("Found $j: $r[$j]");
- }
- }
- return($r);
- }
-
- ##
- ## all_changed_vars
- ##
- ## This function returns an assoc. Array consisting out of
- ## name=>value-pairs which have a different value from the value
- ## currently existing in your table. This is needed by
- ## update_Clause(), cause with this, the update-query can be shortened
- ## to the maximum needed max. Can also be used for much other things,
- ## e.g. checking if something in your form has been changed (in this
- ## case it returns an empty array)
- ##
- ## $table The name of the table
- ## $fields Your assoc value field, which you want to check for
- ## $where The where-clause, which matches your row.
- ## This functions writes warnings, if your where-clause
- ## returns more than one row or nothing
- ##
- function all_changed_vars ($table,$fields,$where,$check='soft') {
- $meta=$this->metadata_buffered($table);
-
- $q1="SELECT * FROM $table $where";
- $this->query($q1);
- $r=Array();
- if ($this->next_record()) {
- for ($i=0; $i < $meta["num_fields"]; $i++) {
- $j=$meta[$i]["name"];
- if ($this->Record[$j]!=$fields[$j]) {
- $r[$j]=$fields[$j];
- $this->_QDebug("Changed $j: ".$fields[$j]." -> ".$this->Record[$j]);
- }
- }
- if ($this->next_record()) {
- echo "ERROR: all_changed_vars(): Found more than one row!
";
- }
- } elseif ($check!='soft') {
- echo "
WARNING: all_changed_vars(): No row found!
";
- }
- $this->_QDebug("WHERE: $where");
- return($r);
- }
-
- ##
- ## metadata_buffered (internal)
- ##
- ## This function calls metadata() if it won't find the buffer,
- ## this speeds the Query-class strongly up, cause it is needed in nearly
- ## every function
- ##
- ## $table the name of the table
- ##
- ## Returns the metadata-field
- ##
- function metadata_buffered($table) {
- if ( !is_Array($this->meta_buf[$table]) || $this->meta_cache_off) {
- return ($this->meta_buf[$table]=$this->metadata($table,true));
- } else {
- return ($this->meta_buf[$table]);
- }
- }
-
-
-}
-
-?>
diff --git a/phplib/sqlquery.inc b/phplib/sqlquery.inc
deleted file mode 100755
index cd47158..0000000
--- a/phplib/sqlquery.inc
+++ /dev/null
@@ -1,322 +0,0 @@
- array(
- "searchfor" => "Suchen nach:",
- "and" => "und",
- "or" => "oder",
- "like" => "enthält",
- "reset" => "Neu",
- "submit" => "Ausführen",
- "less" => "Weniger",
- "more" => "Mehr"
- ),
-
- "en" => array(
- "searchfor" => "Search for:",
- "and" => "and",
- "or" => "or",
- "like" => "contains",
- "reset" => "Reset Query",
- "submit" => "Submit Query",
- "less" => "Fewer",
- "more" => "More"
- )
- );
-
- ## SQL comparision dictionary
- var $compare = array(
- "like" => "like",
- ">" => ">",
- "<" => "<",
-
- ">=" => ">=",
- "<=" => "<=",
- "=" => "=",
- "<>" => "<>"
- );
-
-
- function start($class = "") {
- }
-
- ## selection:
- ##
- ## Create a