[Phplib-commit] CVS: php-lib/php/db/mysql db_sql.inc,1.5,1.6
Brought to you by:
nhruby,
richardarcher
From: Richard A. <ric...@us...> - 2001-08-21 01:36:56
|
Update of /cvsroot/phplib/php-lib/php/db/mysql In directory usw-pr-cvs1:/tmp/cvs-serv13566 Modified Files: db_sql.inc Log Message: Reverting to version 1.1 of db_sql and re-applying the recent patches. The DB_Generic_Sql class concept is a good one. It moves a lot of the common code into a parent class so the DB-specific code is easier to maintain. But the code is incomplete, and reverting is the fastest way to get things working again. Index: db_sql.inc =================================================================== RCS file: /cvsroot/phplib/php-lib/php/db/mysql/db_sql.inc,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** db_sql.inc 2001/08/13 23:07:42 1.5 --- db_sql.inc 2001/08/21 01:36:54 1.6 *************** *** 3,8 **** * Session Management for PHP3 * ! * Copyright (c) 1998-2000 NetUSE GmbH ! * Boris Erdmann, Kristian Koehntopp * * $Id$ --- 3,8 ---- * Session Management for PHP3 * ! * Copyright (c) 1998,1999 NetUSE GmbH ! * Boris Erdmann, Kristian Koehntopp * * $Id$ *************** *** 10,39 **** */ ! class DB_MySQL_Sql extends DB_Generic_Sql { /* public: connection parameters */ ! var $Host = ''; ! var $Database = ''; ! var $User = ''; ! var $Password = ''; /* public: this is an api revision, not a CVS revision. */ ! var $type = 'mysql'; ! var $revision = '1.3'; /* public: constructor */ ! function DB_MySQL_Sql($query = '') { $this->query($query); } /* public: connection management */ ! function connect($Database = '', $Host = '', $User = '', $Password = '') { /* Handle defaults */ ! if ('' == $Database) $Database = $this->Database; ! if ('' == $Host) $Host = $this->Host; ! if ('' == $User) $User = $this->User; ! if ('' == $Password) $Password = $this->Password; --- 10,69 ---- */ ! class DB_Sql { ! /* public: connection parameters */ ! var $Host = ""; ! var $Database = ""; ! var $User = ""; ! var $Password = ""; + /* public: configuration parameters */ + var $Auto_Free = 0; ## Set to 1 for automatic mysql_free_result() + var $Debug = 0; ## Set to 1 for debugging messages. + var $Halt_On_Error = "yes"; ## "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning) + var $Seq_Table = "db_sequence"; + + /* public: result array and current row number */ + var $Record = array(); + var $Row; + + /* public: current error number and error text */ + var $Errno = 0; + var $Error = ""; + /* public: this is an api revision, not a CVS revision. */ ! var $type = "mysql"; ! var $revision = "1.2"; + /* private: link and query handles */ + var $Link_ID = 0; + var $Query_ID = 0; + + + /* public: constructor */ ! function DB_Sql($query = "") { $this->query($query); } + /* public: some trivial reporting */ + function link_id() { + return $this->Link_ID; + } + + function query_id() { + return $this->Query_ID; + } + /* public: connection management */ ! function connect($Database = "", $Host = "", $User = "", $Password = "") { /* Handle defaults */ ! if ("" == $Database) $Database = $this->Database; ! if ("" == $Host) $Host = $this->Host; ! if ("" == $User) $User = $this->User; ! if ("" == $Password) $Password = $this->Password; *************** *** 48,52 **** if (!@mysql_select_db($Database,$this->Link_ID)) { ! $this->halt('cannot use database '.$Database); return 0; } --- 78,82 ---- if (!@mysql_select_db($Database,$this->Link_ID)) { ! $this->halt("cannot use database ".$Database); return 0; } *************** *** 64,73 **** /* public: perform a query */ function query($Query_String) { ! if ($Query_String == '') return 0; - $this->Query_Str = $Query_String; if (!$this->connect()) { ! return false; /* we already complained in connect() about that. */ }; --- 94,107 ---- /* public: perform a query */ 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; if (!$this->connect()) { ! return 0; /* we already complained in connect() about that. */ }; *************** *** 78,88 **** if ($this->Debug) ! printf('Debug: query = %s<br>\n', $Query_String); $this->Query_ID = @mysql_query($Query_String,$this->Link_ID); $this->Row = 0; ! $this->check_error(); if (!$this->Query_ID) { ! $this->halt('Query failed.'); } --- 112,123 ---- if ($this->Debug) ! printf("Debug: query = %s<br>\n", $Query_String); $this->Query_ID = @mysql_query($Query_String,$this->Link_ID); $this->Row = 0; ! $this->Errno = mysql_errno(); ! $this->Error = mysql_error(); if (!$this->Query_ID) { ! $this->halt("Invalid SQL: ".$Query_String); } *************** *** 94,98 **** function next_record() { if (!$this->Query_ID) { ! $this->halt('next_record called with no query pending.'); return 0; } --- 129,133 ---- function next_record() { if (!$this->Query_ID) { ! $this->halt("next_record called with no query pending."); return 0; } *************** *** 100,104 **** $this->Record = @mysql_fetch_array($this->Query_ID); $this->Row += 1; ! $this->check_error(); $stat = is_array($this->Record); --- 135,140 ---- $this->Record = @mysql_fetch_array($this->Query_ID); $this->Row += 1; ! $this->Errno = mysql_errno(); ! $this->Error = mysql_error(); $stat = is_array($this->Record); *************** *** 115,119 **** $this->Row = $pos; else { ! $this->halt('seek($pos) failed: result has '.$this->num_rows().' rows.'); /* half assed attempt to save the day, --- 151,155 ---- $this->Row = $pos; else { ! $this->halt("seek($pos) failed: result has ".$this->num_rows()." rows."); /* half assed attempt to save the day, *************** *** 130,140 **** /* public: table locking */ ! function lock($table, $mode='write') { $this->connect(); ! $query='lock tables '; if (is_array($table)) { while (list($key,$value)=each($table)) { ! if ($key=='read' && $key!=0) { $query.="$value read, "; } else { --- 166,176 ---- /* public: table locking */ ! function lock($table, $mode="write") { $this->connect(); ! $query="lock tables "; if (is_array($table)) { while (list($key,$value)=each($table)) { ! if ($key=="read" && $key!=0) { $query.="$value read, "; } else { *************** *** 157,163 **** $this->connect(); ! $res = @mysql_query('unlock tables', $this->Link_ID); if (!$res) { ! $this->halt('unlock() failed.'); return 0; } --- 193,199 ---- $this->connect(); ! $res = @mysql_query("unlock tables", $this->Link_ID); if (!$res) { ! $this->halt("unlock() failed."); return 0; } *************** *** 178,181 **** --- 214,234 ---- } + /* public: shorthand notation */ + 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 numbers */ function nextid($seq_name) { *************** *** 184,188 **** if ($this->lock($this->Seq_Table)) { /* get sequence number (locked) and increment */ ! $q = sprintf('select p_nextid from %s where p_seq_name = '%s'', $this->Seq_Table, $seq_name); --- 237,241 ---- if ($this->lock($this->Seq_Table)) { /* get sequence number (locked) and increment */ ! $q = sprintf("select p_nextid from %s where p_seq_name = '%s'", $this->Seq_Table, $seq_name); *************** *** 193,197 **** if (!is_array($res)) { $currentid = 0; ! $q = sprintf('insert into %s ( p_seq_name, p_nextid ) values('%s', %s)', $this->Seq_Table, $seq_name, --- 246,250 ---- if (!is_array($res)) { $currentid = 0; ! $q = sprintf("insert into %s ( p_seq_name, p_nextid ) values('%s', %s)", $this->Seq_Table, $seq_name, *************** *** 199,206 **** $id = @mysql_query($q, $this->Link_ID); } else { ! $currentid = $res['p_nextid']; } $nextid = $currentid + 1; ! $q = sprintf('update %s set p_nextid = '%s' where p_seq_name = '%s'', $this->Seq_Table, $nextid, --- 252,259 ---- $id = @mysql_query($q, $this->Link_ID); } else { ! $currentid = $res["p_nextid"]; } $nextid = $currentid + 1; ! $q = sprintf("update %s set p_nextid = '%s' where p_seq_name = '%s'", $this->Seq_Table, $nextid, *************** *** 209,213 **** $this->unlock(); } else { ! $this->halt('cannot lock '.$this->Seq_Table.' - has it been created?'); return 0; } --- 262,266 ---- $this->unlock(); } else { ! $this->halt("cannot lock ".$this->Seq_Table." - has it been created?"); return 0; } *************** *** 216,220 **** /* public: return table metadata */ ! function metadata($table = '', $full = false) { $count = 0; $id = 0; --- 269,273 ---- /* public: return table metadata */ ! function metadata($table = "", $full = false) { $count = 0; $id = 0; *************** *** 228,248 **** * - 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 * * - full is true (was mainly introduced for the Query-class) * $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 ! * [0]['php_type'] the correspondig PHP-type ! * [0]['php_subtype'] the subtype of PHP-type ! * ['meta'][field name] index-num of field named 'field name' * This could used, if you have the name, but no index-num - very fast * [unique] = field names which have an unique key, separated by space --- 281,301 ---- * - 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 * * - full is true (was mainly introduced for the Query-class) * $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 ! * [0]["php_type"] the correspondig PHP-type ! * [0]["php_subtype"] the subtype of PHP-type ! * ["meta"][field name] index-num of field named "field name" * This could used, if you have the name, but no index-num - very fast * [unique] = field names which have an unique key, separated by space *************** *** 255,259 **** $id = @mysql_list_fields($this->Database, $table); if (!$id) { ! $this->halt('Metadata query failed.'); return false; } --- 308,312 ---- $id = @mysql_list_fields($this->Database, $table); if (!$id) { ! $this->halt("Metadata query failed."); return false; } *************** *** 261,265 **** $id = $this->Query_ID; if (!$id) { ! $this->halt('No query specified.'); return false; } --- 314,318 ---- $id = $this->Query_ID; if (!$id) { ! $this->halt("No query specified."); return false; } *************** *** 271,324 **** if (!$full) { for ($i=0; $i<$count; $i++) { ! $res[$i]['table'] = @mysql_field_table ($id, $i); ! $res[$i]['name'] = @mysql_field_name ($id, $i); ! $res[$i]['type'] = @mysql_field_type ($id, $i); ! $res[$i]['len'] = @mysql_field_len ($id, $i); ! $res[$i]['flags'] = @mysql_field_flags ($id, $i); } } else { // full $uniq=ARRAY(); ! $res['num_fields']= $count; for ($i=0; $i<$count; $i++) { ! $res[$i]['table'] = @mysql_field_table ($id, $i); ! $res[$i]['name'] = @mysql_field_name ($id, $i); ! $res[$i]['type'] = @mysql_field_type ($id, $i); ! $res[$i]['len'] = @mysql_field_len ($id, $i); ! $res[$i]['flags'] = @mysql_field_flags ($id, $i); ! $res['meta'][$res[$i]['name']] = $i; ! switch ($res[$i]['type']) { ! case 'var string': ! case 'string' : ! case 'char' : ! $res[$i]['php_type']='string'; ! $res[$i]['php_subtype']=''; break; ! case 'timestamp' : ! case 'datetime' : ! case 'date' : ! case 'time' : ! $res[$i]['php_type']='string'; ! $res[$i]['php_subtype']='date'; break; ! case 'blob' : ! $res[$i]['php_type']='string'; ! $res[$i]['php_subtype']='blob'; break; ! case 'real' : ! $res[$i]['php_type']='double'; ! $res[$i]['php_subtype']=''; break; ! case 'long' : default : ! $res[$i]['php_type']='int'; ! $res[$i]['php_subtype']=''; break; } ! if ( ereg('(unique_key|primary_key)',$res[$i]['flags']) ) { ! $uniq[]=$res[$i]['name']; } } ! $res['unique']=join(' ',$uniq); } --- 324,377 ---- if (!$full) { for ($i=0; $i<$count; $i++) { ! $res[$i]["table"] = @mysql_field_table ($id, $i); ! $res[$i]["name"] = @mysql_field_name ($id, $i); ! $res[$i]["type"] = @mysql_field_type ($id, $i); ! $res[$i]["len"] = @mysql_field_len ($id, $i); ! $res[$i]["flags"] = @mysql_field_flags ($id, $i); } } else { // full $uniq=ARRAY(); ! $res["num_fields"]= $count; for ($i=0; $i<$count; $i++) { ! $res[$i]["table"] = @mysql_field_table ($id, $i); ! $res[$i]["name"] = @mysql_field_name ($id, $i); ! $res[$i]["type"] = @mysql_field_type ($id, $i); ! $res[$i]["len"] = @mysql_field_len ($id, $i); ! $res[$i]["flags"] = @mysql_field_flags ($id, $i); ! $res["meta"][$res[$i]["name"]] = $i; ! switch ($res[$i]["type"]) { ! case "var string": ! case "string" : ! case "char" : ! $res[$i]["php_type"]="string"; ! $res[$i]["php_subtype"]=""; break; ! case "timestamp" : ! case "datetime" : ! case "date" : ! case "time" : ! $res[$i]["php_type"]="string"; ! $res[$i]["php_subtype"]="date"; break; ! case "blob" : ! $res[$i]["php_type"]="string"; ! $res[$i]["php_subtype"]="blob"; break; ! case "real" : ! $res[$i]["php_type"]="double"; ! $res[$i]["php_subtype"]=""; break; ! case "long" : default : ! $res[$i]["php_type"]="int"; ! $res[$i]["php_subtype"]=""; break; } ! if ( ereg("(unique_key|primary_key)",$res[$i]["flags"]) ) { ! $uniq[]=$res[$i]["name"]; } } ! $res["unique"]=join(" ",$uniq); } *************** *** 332,341 **** function table_names() { $this->connect(); ! $h = @mysql_query('show tables', $this->Link_ID); $i = 0; while ($info = @mysql_fetch_row($this->Query_ID)) { ! $return[$i]['table_name']= $info[0]; ! $return[$i]['tablespace_name']=$this->Database; ! $return[$i]['database']=$this->Database; $i++; } --- 385,394 ---- function table_names() { $this->connect(); ! $h = @mysql_query("show tables", $this->Link_ID); $i = 0; while ($info = @mysql_fetch_row($this->Query_ID)) { ! $return[$i]["table_name"]= $info[0]; ! $return[$i]["tablespace_name"]=$this->Database; ! $return[$i]["database"]=$this->Database; $i++; } *************** *** 346,353 **** /* private: error handling */ ! function check_error() { ! $this->Error = @mysql_error(); ! $this->Errno = @mysql_errno(); } } ?> --- 399,422 ---- /* private: error handling */ ! function halt($msg) { ! $this->Error = @mysql_error($this->Link_ID); ! $this->Errno = @mysql_errno($this->Link_ID); ! if ($this->Halt_On_Error == "no") ! return; ! ! $this->haltmsg($msg); ! ! if ($this->Halt_On_Error != "report") ! die("Session halted."); ! } ! ! function haltmsg($msg) { ! printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg); ! printf("<b>MySQL Error</b>: %s (%s)<br>\n", ! $this->Errno, ! $this->Error); } + } ?> + |