[Phplib-commit] CVS: php-lib/php/db/mysql db_sql.inc,1.8,1.9
Brought to you by:
nhruby,
richardarcher
From: Richard A. <ric...@us...> - 2001-08-27 08:23:56
|
Update of /cvsroot/phplib/php-lib/php/db/mysql In directory usw-pr-cvs1:/tmp/cvs-serv5781 Modified Files: db_sql.inc Log Message: Some changes to make it easier to keep the various db_sql classes in sync - remove mySQL references from comments - clean up comments and whitespace - add instance vars for the columns in the sequence table Change the behaviour of connect() so that the PHPLIB DB is always selected. Changed all the connect() calls to return if the connect failed. Make the p() function use f(). Streamline the metadata function -- may be a little slower but looks a lot nicer! Index: db_sql.inc =================================================================== RCS file: /cvsroot/phplib/php-lib/php/db/mysql/db_sql.inc,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** db_sql.inc 2001/08/21 02:20:52 1.8 --- db_sql.inc 2001/08/27 08:23:53 1.9 *************** *** 19,30 **** /* 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 */ --- 19,32 ---- /* public: configuration parameters */ ! var $Auto_Free = 0; ## Set to 1 to automatically free results ! 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"; ## Name of the sequence table ! var $Seq_ID_Col = "p_nextid"; ## Name of the Sequence ID column in $Seq_Table ! var $Seq_Name_Col = "p_seq_name"; ## Name of the Sequence Name column in $Seq_Table /* public: result array and current row number */ var $Record = array(); ! var $Row = 0; /* public: current error number and error text */ *************** *** 39,43 **** var $Link_ID = 0; var $Query_ID = 0; - --- 41,44 ---- *************** *** 70,74 **** /* establish connection, select database */ if ( 0 == $this->Link_ID ) { - $this->Link_ID=mysql_pconnect($Host, $User, $Password); if (!$this->Link_ID) { --- 71,74 ---- *************** *** 76,84 **** return 0; } ! if (!@mysql_select_db($Database,$this->Link_ID)) { ! $this->halt("cannot use database ".$Database); ! return 0; ! } } --- 76,84 ---- return 0; } + } ! if (!@mysql_select_db($Database,$this->Link_ID)) { ! $this->halt("cannot use database ".$Database); ! return 0; } *************** *** 104,108 **** if (!$this->connect()) { return 0; /* we already complained in connect() about that. */ ! }; # New query, discard previous result. --- 104,108 ---- if (!$this->connect()) { return 0; /* we already complained in connect() about that. */ ! } # New query, discard previous result. *************** *** 118,121 **** --- 118,122 ---- $this->Errno = mysql_errno(); $this->Error = mysql_error(); + if (!$this->Query_ID) { $this->halt("Invalid SQL: ".$Query_String); *************** *** 167,171 **** /* public: table locking */ function lock($table, $mode="write") { ! $this->connect(); $query="lock tables "; --- 168,174 ---- /* public: table locking */ function lock($table, $mode="write") { ! if (!$this->connect()) { ! return 0; /* we already complained in connect() about that. */ ! } $query="lock tables "; *************** *** 191,195 **** function unlock() { ! $this->connect(); $res = @mysql_query("unlock tables", $this->Link_ID); --- 194,200 ---- function unlock() { ! if (!$this->connect()) { ! return 0; /* we already complained in connect() about that. */ ! } $res = @mysql_query("unlock tables", $this->Link_ID); *************** *** 227,246 **** return $this->Record[$Name]; } } function p($Name) { ! if (isset($this->Record[$Name])) { ! print $this->Record[$Name]; ! } } /* public: sequence numbers */ function nextid($seq_name) { ! $this->connect(); 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); $id = @mysql_query($q, $this->Link_ID); --- 232,254 ---- return $this->Record[$Name]; } + return ""; } function p($Name) { ! print $this->f($Name); } /* public: sequence numbers */ function nextid($seq_name) { ! if (!$this->connect()) { ! return 0; /* we already complained in connect() about that. */ ! } if ($this->lock($this->Seq_Table)) { /* get sequence number (locked) and increment */ ! $q = sprintf("select %s from %s where %s = '%s'", ! $this->Seq_ID_Col, $this->Seq_Table, + $this->Seq_Name_Col, $seq_name); $id = @mysql_query($q, $this->Link_ID); *************** *** 250,266 **** if (!is_array($res)) { $currentid = 0; ! $q = sprintf("insert into %s ( p_seq_name, p_nextid ) values('%s', %s)", ! $this->Seq_Table, ! $seq_name, ! $currentid); $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, ! $seq_name); $id = @mysql_query($q, $this->Link_ID); $this->unlock(); --- 258,278 ---- if (!is_array($res)) { $currentid = 0; ! $q = sprintf("insert into %s ( %s, %s ) values('%s', %s)", ! $this->Seq_Table, ! $this->Seq_Name_Col, ! $this->Seq_ID_Col, ! $seq_name, ! $currentid); $id = @mysql_query($q, $this->Link_ID); } else { ! $currentid = $res[$this->Seq_ID_Col]; } $nextid = $currentid + 1; ! $q = sprintf("update %s set %s = '%s' where %s = '%s'", ! $this->Seq_Table, ! $this->Seq_ID_Col, ! $nextid, ! $this->Seq_Name_Col, ! $seq_name); $id = @mysql_query($q, $this->Link_ID); $this->unlock(); *************** *** 281,287 **** * 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 --- 293,299 ---- * Due to compatibility problems with Table we changed the behavior * of metadata(); ! * If $full is set, metadata returns additional information * ! * This information is always returned: * $result[]: * [0]["table"] table name *************** *** 291,314 **** * [0]["flags"] field flags * ! * - 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 ! * [0]["php_type"] the correspondig PHP-type * [0]["php_subtype"] the subtype of PHP-type ! * ["meta"][field name] index of field named "field name" ! * This last one could be used if you have a field name, but no index. * Test: if (isset($result['meta']['myfield'])) { ... * [unique] = field names which have an unique key, separated by space */ ! // if no $table specified, assume that we are working with a query ! // result if ($table) { ! $this->connect(); $id = @mysql_list_fields($this->Database, $table); if (!$id) { --- 303,322 ---- * [0]["flags"] field flags * ! * If $full is set this information is also returned: * $result[]: * ["num_fields"] number of metadata records ! * [0]["php_type"] the corresponding PHP-type * [0]["php_subtype"] the subtype of PHP-type ! * ["meta"][field name] index of field named "field name" ! * This one could be used if you have a field name, but no index. * Test: if (isset($result['meta']['myfield'])) { ... * [unique] = field names which have an unique key, separated by space */ ! // if no $table specified, assume that we are working with a query result if ($table) { ! if (!$this->connect()) { ! return 0; /* we already complained in connect() about that. */ ! } $id = @mysql_list_fields($this->Database, $table); if (!$id) { *************** *** 317,321 **** } } else { ! $id = $this->Query_ID; if (!$id) { $this->halt("No query specified."); --- 325,329 ---- } } else { ! $id = $this->Query_ID; if (!$id) { $this->halt("No query specified."); *************** *** 323,348 **** } } ! $count = @mysql_num_fields($id); ! // made this IF due to performance (one if is faster than $count if's) ! 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"]) { --- 331,350 ---- } } ! $count = @mysql_num_fields($id); ! 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); ! } ! ! if ($full) { ! $uniq = array(); ! $res["num_fields"] = $count; for ($i=0; $i<$count; $i++) { $res["meta"][$res[$i]["name"]] = $i; switch ($res[$i]["type"]) { *************** *** 390,394 **** /* public: find available table names */ function table_names() { ! $this->connect(); $h = @mysql_query("show tables", $this->Link_ID); $i = 0; --- 392,399 ---- /* public: find available table names */ function table_names() { ! if (!$this->connect()) { ! return 0; /* we already complained in connect() about that. */ ! } ! $h = @mysql_query("show tables", $this->Link_ID); $i = 0; |