Date: 07/23/00
- Next message: ssilk: "[phplib-dev] cvs commit"
- Previous message: Kristian Köhntopp: "[phplib-dev] Summer holiday starting in Seattle..."
- Next in thread: ssilk: "[phplib-dev] cvs commit"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
From: ssilk
Date: Mon Jul 24 03:18:20 2000
Modified files:
php-lib/php/query_sql.inc
Log message:
- Made query_sql compatible with all warnings turned on, found many places,
where I assume an unset value or other things like that
- tested with PHP4
- new feature: Adding a dot "." to the table-name or $special[tableprefix]
is set will make query_sql to add the table name to every column name
(table.column). This feature is included in every function, but
makes sense only one some functions (where etc).
- New function table_quote() which is an abreviation for you to generate
very special query-parts (e.g. WHERE a BETWEEN 1 and 2)
- New function uniform_add_where() which will remve the "WHERE" from
a where-clause
- Add new feature that will enable you to create simp_where_clause() either
with a string or via array.
- Other small API changes ($special parameter added for exists etc)
Index: php-lib/php/query_sql.inc
diff -u php-lib/php/query_sql.inc:1.19 php-lib/php/query_sql.inc:1.20
--- php-lib/php/query_sql.inc:1.19 Wed Jul 12 01:20:50 2000
+++ php-lib/php/query_sql.inc Mon Jul 24 03:17:50 2000
@@ -18,7 +18,7 @@
* is based in November 1997,
* it was a collection of functions for PHP/FI and mSQL.
*
- * $Id: query_sql.inc,v 1.19 2000/07/11 23:20:50 ssilk Exp $
+ * $Id: query_sql.inc,v 1.20 2000/07/24 01:17:50 ssilk Exp $
*
*/
@@ -82,6 +82,8 @@
var $fireball_replace_char = "_";
+ var $meta_buf=array();
+
## (MORE OR LESS) PRIVATE FUNCTIONS ##
@@ -107,9 +109,9 @@
## (array $meta)
## : array( php_type , php_subtype )
function sql2phptype ($meta) {
- if (!$meta[php_type])
+ if (!$meta['php_type'])
$this->halt("sql2phptype(): php_type is empty!");
- return(Array($meta[php_type],$meta[php_subtype]));
+ return(Array($meta['php_type'],$meta['php_subtype']));
}
@@ -161,7 +163,7 @@
}
if ($subtype!='date' &&
( $this->StrLengTrunc || $this->StrLengWarn ) ) {
- if ( strlen($val) > $meta[len] ) {
+ if ( strlen($val) > $meta['len'] ) {
if ($this->StrLengWarn) {
echo "<BR>STRING TOO LONG: '$meta[name]'";
if ($this->StrLengTrunc) {
@@ -169,7 +171,7 @@
}
}
if ($this->StrLengTrunc && (string)$subtype!='NULL') {
- $val=substr($val,0,$meta[len]);
+ $val=substr($val,0,$meta['len']);
}
}
}
@@ -191,9 +193,27 @@
break;
default :
echo "UNKNOWN TYPE: $type<BR>";
+ }
+ if (!empty($meta['tableprefix'])) {
+ $this->_QDebug("Val: $meta[tableprefix]$meta[name] => $val<BR>");
+ return(Array($val,$meta['tableprefix'].$meta['name']));
+ } else {
+ $this->_QDebug("Val: $meta[name] => $val<BR>");
+ return(Array($val,$meta['name']));
}
- $this->_QDebug("Val: $meta[name] => $val<BR>");
- return(Array($val,$meta["name"]));
+ }
+
+ ##
+ ## table_quote - converts a value of given table into a
+ ## corresponding correctly escaped value. Indeed it is only
+ ## some better type of interface for convert()
+ ## This is for example to create a more complex-where-clause
+ ## (e.g. 'date BETWEEN ... AND ...')
+ ## without leaving query-class.
+ ##
+ function table_quote($table,$name,$value,$spec='') {
+ $meta=$this->metadata_buffered($table);
+ return($this->convert($value,$meta[$meta['meta'][$name]],special));
}
##
@@ -204,9 +224,9 @@
function chkprms ($params) {
if (!is_Array($params)) {
$check=strtolower($params);
- $params=ARRAY(check=>$check);
+ $params=ARRAY('check'=>$check);
} else {
- $check=strtolower($params[check]);
+ $check=strtolower($params['check']);
}
if (empty($check)) $check='soft';
if (! ereg("strong|soft|weak",$check) ) {
@@ -269,6 +289,9 @@
}
return($where);
}
+ function uniform_add_where ($where) {
+ return(eregi_Replace("^[[:space:]]*WHERE","",trim($where)));
+ }
##
## uniforms $fields with $special
@@ -278,10 +301,11 @@
## : array (array ($vals) , array($names) )
function uniform_vars (&$meta,&$fields,&$special) {
for (reset($fields); list($key,$val)=each($fields); ) {
- if ( isset($meta[meta][$key]) ) {
- $j=$meta[meta][$key];
+ if ( isset($meta['meta'][$key]) ) {
+ $j=$meta['meta'][$key];
list($vals[],$names[])=
- $this->convert($val,$meta[$j],$special[$key]);
+ $this->convert($val,$meta[$j],
+ (isset($special[$key])) ? $special[$key] : '');
}
}
# add information for $special-fields which are not used
@@ -301,12 +325,40 @@
## This function calls metadata() if it won't find the buffer
## (string $table)
## : array ($meta)
- function metadata_buffered($table) {
- if ( !is_Array($this->meta_buf[$table]) || $this->meta_cache_off) {
- return ($this->meta_buf[$table]=$this->metadata($table,true));
+ function metadata_buffered($table,$prefix=false) {
+ if (!$prefix) {
+ if ( !isset($this->meta_buf[$table]) or
+ !is_Array($this->meta_buf[$table]) or $this->meta_cache_off) {
+ return ($this->meta_buf[$table]=$this->metadata($table,true));
+ } else {
+ return ($this->meta_buf[$table]);
+ }
} else {
- return ($this->meta_buf[$table]);
+ $table=substr($table,0,strlen($table)-1);
+ if ( !isset($this->meta_buf[$table]) or
+ !is_Array($this->meta_buf[$table]) or $this->meta_cache_off) {
+ $this->meta_buf[$table]=$this->metadata($table,true);
+ }
+ for (reset($this->meta_buf[$table]) ; list($k,$v)=each($this->meta_buf[$table]) ; ) {
+ if (isset($v['table']))
+ $this->meta_buf[$table][$k]['tableprefix']="$v[table].";
+ }
+ return($this->meta_buf[$table]);
+ }
+ }
+
+ ##
+ ## istableprefix($table,$special)
+ ## returns (list($tablename,$prefix))
+ ##
+ function istableprefix($table,$special) {
+ $tableprefix=false;
+ if (substr($table,-1,1)=='.') {
+ $tableprefix=true;
+ $table=substr($table,0,strlen($table));
}
+ if (!empty($special['tableprefix'])) $tableprefix=true;
+ return(array($table,$tableprefix));
}
@@ -330,7 +382,8 @@
if (empty($special)) $special=ARRAY();
list($params,$check)=$this->chkprms($params);
- $meta=$this->metadata_buffered($table);
+ list($table,$prefix)=$this->istableprefix($table,$special);
+ $meta=$this->metadata_buffered($table,$prefix);
list($vals,$names)=$this->uniform_vars($meta,$fields,$special);
@@ -359,17 +412,18 @@
if (empty($special)) $special=ARRAY();
list($params,$check)=$this->chkprms($params);
- $meta=$this->metadata_buffered($table);
+ list($table,$prefix)=$this->istableprefix($table,$special);
+ $meta=$this->metadata_buffered($table,$prefix);
- if (Count($fields)!=Count($val["name"]) && $check=="strong") {
+ if (Count($fields)!=Count($val['name']) && $check=='strong') {
$this->halt("WARNING: insert_plain_clause(): There are not the same number of".
" fields as in table for INSERT");
}
- for ($i=0; $i < $meta["num_fields"]; $i++) {
- $j=$meta[$i]["name"];
+ for ($i=0; $i < $meta['num_fields']; $i++) {
+ $j=$meta[$i]['name'];
if (!isset($fields[$j])) {
- if ($params[nullisnull]) {
+ if ($params['nullisnull']) {
$special[$j]='NULL';
} else {
$fields[$j]='';
@@ -400,7 +454,8 @@
list($special,$where)=$this->special_or_where($where_or_special,$special_or_where);
list($params,$check)=$this->chkprms($params);
- $meta=$this->metadata_buffered($table);
+ list($table,$prefix)=$this->istableprefix($table,$special);
+ $meta=$this->metadata_buffered($table,$prefix);
if (empty($where) && $check=="soft") {
$where=$this->unique_where_Clause($table,$fields);
@@ -413,7 +468,7 @@
list($vals,$names)=$this->uniform_vars($meta,$fields,$special);
for ($i=0 ; $i < Count ($names); $i++ ) {
- $s[]=$names[$i]."=".$vals[$i];
+ $s[]=$names[$i].'='.$vals[$i];
}
if (Count($s)) {
$q=sprintf("UPDATE %s SET %s%s",$table,join($s,", "),
@@ -445,16 +500,17 @@
list($special,$where)=$this->special_or_where($where_or_special,$special_or_where);
list($params,$check)=$this->chkprms($params);
- $meta=$this->metadata_buffered($table);
- if ((Count($fields)!=$meta["num_fields"]) && $check=="strong") {
+ list($table,$prefix)=$this->istableprefix($table,$special);
+ $meta=$this->metadata_buffered($table,$prefix);
+ if ((Count($fields)!=$meta['num_fields']) && $check=="strong") {
echo "WARNING: update_plain_Clause(): This is not the correct number of".
" fields for UPDATE<BR>";
}
- for ($i=0; $i < $meta["num_fields"]; $i++) {
- $j=$meta[$i]["name"];
+ for ($i=0; $i < $meta['num_fields']; $i++) {
+ $j=$meta[$i]['name'];
if (!isset($fields[$j])) {
- if ($params[nullisnull]) {
+ if ($params['nullisnull']) {
$special[$j]='NULL';
} else {
$fields[$j]='';
@@ -491,8 +547,9 @@
if (empty($where)) {
## then fields must hold some values
- $meta=$this->metadata_buffered($table);
- $uniq=split(" ",$meta[unique]);
+ list($table,$prefix)=$this->istableprefix($table,$special);
+ $meta=$this->metadata_buffered($table,$prefix);
+ $uniq=split(" ",$meta['unique']);
$where=ARRAY();
for ( reset($uniq) ; list(,$name)=each($uniq) ; ) {
if (isset($fields[$name]))
@@ -534,7 +591,7 @@
## (
## string $table
## array $fields Assoc name=>value-fields
- ## array $op Assoc name=>operator. If empty, '=' is taken. it is printed
+ ## string or array $op Assoc name=>operator. If empty, '=' is taken. it is printed
## *between* the name/value pairs.
## if $op is 'func' the name is taken as function name,
## inside the brakets is the value.
@@ -552,18 +609,22 @@
function where_Clause ($table,$fields,$op='',$special='',
$andor='AND',$where='',$params="soft") {
list($params,$check)=$this->chkprms($params);
- if (!is_Array($op)) $op=ARRAY();
if (!is_Array($special)) $special=ARRAY();
if (!$andor) $andor='AND';
if (!is_Array($fields)) $fields=ARRAY();
if (!is_string($where)) echo "WARNING: WHERE is not string\n";
$q='';
- $meta=$this->metadata_buffered($table);
+ list($table,$prefix)=$this->istableprefix($table,$special);
+ $meta=$this->metadata_buffered($table,$prefix);
list($vals,$names)=$this->uniform_vars($meta,$fields,$special);
for ($i=0; $i < Count($names) ; $i++) {
- if (empty($op[$names[$i]])) $o='='; else $o=$op[$names[$i]];
+ if (is_array($op)) {
+ if (empty($op[$names[$i]])) $o='='; else $o=$op[$names[$i]];
+ } else {
+ if (empty($op)) $o='='; else $o=$op;
+ }
if ((string)$vals[$i]=='NULL') {
if ($o=='=' || strtoupper($o)=='IS') $o = 'IS';
else $o = 'IS NOT';
@@ -571,8 +632,8 @@
$q.=" $andor $names[$i] $o $vals[$i]";
}
if ($where) {
- $where=eregi_Replace("^[[:space:]]*WHERE","",$where);
- $q.=" $andor $where";
+ $where=$this->uniform_add_where($where);
+ if ($where) $q.=" $andor $where";
}
if (!$q && $ckeck=='strong') {
echo "WARNING: where_Clause(): WHERE-clause is empty!<BR>";
@@ -599,21 +660,22 @@
if (!$andor) $andor='AND';
if (!is_Array($fields)) $fields=ARRAY();
- $meta=$this->metadata_buffered($table);
- if ( empty($meta[unique]) ) {
+ list($table,$prefix)=$this->istableprefix($table,$special);
+ $meta=$this->metadata_buffered($table,$prefix);
+ if ( empty($meta['unique']) ) {
echo "ERROR: unique_where_Clause(): Table '$table' has no unique".
" identifier or primary key!<br>";
return(false);
}
- $uniq=split(" ",$meta[unique]);
+ $uniq=split(" ",$meta['unique']);
$wfields=ARRAY();
for ( reset($uniq) ; list(,$name)=each($uniq) ; ) {
- if (isset($meta[meta][$name])) {
+ if (isset($meta['meta'][$name])) {
$tmp= $this->convert($fields[$name],
- $meta[$meta[meta][$name]],
+ $meta[$meta['meta'][$name]],
$special[$name]);
if ( (string)$tmp[0] !='NULL' &&
- empty($special[removenull]) ) {
+ empty($special['removenull']) ) {
$wfields[$name]=$fields[$name];
}
} else {
@@ -651,28 +713,26 @@
## will be returned, to avoid "full" selects. Default is soft
##
function simp_where_Clause ($mywhere,$andor='AND',$where='',$check="soft") {
- $meta=$this->metadata_buffered($table);
- $q='';
+ $q=array();
for ($i=0; $i<Count($mywhere); $i++ ) {
- $q.=" $andor ".$mywhere[$i];
+ $q2=$this->uniform_add_where($mywhere[$i]);
+ if ($q2) $q[]=$q2;
}
if ($where) {
- $where=eregi_Replace("^[[:space:]]*WHERE","",$where);
- $q.=" $andor $where";
+ $where=$this->uniform_add_where($where);
+ if ($where) $q[]=$where;
}
if (!$q && $check=='strong') {
$this->halt("where_plain_Clause(): WHERE-clause is empty!");
}
- $q=$this->uniform_where(ereg_Replace("^ $andor ","",$q));
- $this->_QDebug("where_plain_Clause(): $q");
- return($q);
+ $q2=$this->uniform_where(join(" $andor ",$q));
+ $this->_QDebug("where_plain_Clause(): $q2");
+ return($q2);
}
-
-
##
## IU - INSERT or update UPDATE query for
## an unique row in a table
@@ -695,7 +755,7 @@
list($params,$check)=$this->chkprms($params);
if (!$whereclause=
- $this->unique_where_Clause($table,$fields,$special,$params) ) {
+ $this->unique_where_Clause($table,$fields,'',$special,$params) ) {
echo "ERROR: IU_Clause(): Where-Clause could't generated";
return(0);
}
@@ -707,7 +767,7 @@
## goes wrong with generating the where-clause?
echo "ERROR: IU_Clause(): This index isn't unique: '$whereclause'<BR>";
return(false);
- } elseif (empty($params[forceinsert]) && $this->Record[0] == 1) {
+ } elseif (empty($params['forceinsert']) && $this->Record[0] == 1) {
return(
$this->update_Clause($table,$fields,$special,$whereclause,$check) );
} else {
@@ -725,14 +785,15 @@
##
## it returns the number of found matches or zero
##
- function exists ($table,$name_or_fields,$val='') {
- $meta=$this->metadata_buffered($table);
+ function exists ($table,$name_or_fields,$val='',$special='') {
+ list($table,$prefix)=$this->istableprefix($table,$special);
+ $meta=$this->metadata_buffered($table,$prefix);
list($fields,$name)=$this->special_or_where($name_or_fields);
if (empty($name)) {
- $where=$this->where_Clause($table,$fields,'strong');
- } elseif ($name && !emtpy($val) ) {
- $where=$this->where_Clause($table,ARRAY($name=>$val),'strong');
+ $where=$this->where_Clause($table,$fields,'',$special,'','','strong');
+ } elseif ($name && !empty($val) ) {
+ $where=$this->where_Clause($table,ARRAY($name=>$val),'',$special,'','','strong');
} else {
$this->halt("exists(): Val was not set");
}
@@ -747,7 +808,7 @@
## an assoc array of the first found row, or false if nothing found
## field $name is set with value $val
##
- function getrow ($table,$name_or_fields,$val='') {
+ function getrow ($table,$name_or_fields,$val='',$special='') {
if (!$res=$this->getexrow(&$table,&$name_or_fields,&$val)) {
echo "<BR><B>WARNING:</B> getrow(): '$where' not found<BR>";
return(false);
@@ -758,16 +819,17 @@
## an assoc array of the first found row, or false if nothing found
## field $name is set with value $val
##
- function getexrow ($table,$name_or_fields,$val='') {
- $meta=$this->metadata_buffered($table);
+ function getexrow ($table,$name_or_fields,$val='',$special='') {
+ list($table,$prefix)=$this->istableprefix($table,$special);
+ $meta=$this->metadata_buffered($table,$prefix);
list($fields,$name)=$this->special_or_where($name_or_fields);
if (empty($name)) {
- $where=$this->where_Clause($table,$fields,'strong');
+ $where=$this->where_Clause($table,$fields,'',$special,'','','strong');
} elseif ($name && !empty($val) ) {
- $where=$this->where_Clause($table,ARRAY($name=>$val),'strong');
+ $where=$this->where_Clause($table,ARRAY($name=>$val),'',$special,'','','strong');
} else {
- $this->halt("exists(): Val was not set");
+ $this->halt("getexrow(): Val was not set");
}
$q=sprintf("SELECT * FROM %s%s",
$table,$where);
@@ -815,10 +877,11 @@
## $table The name of the table
##
function capture_vars ($table) {
- $meta=$this->metadata_buffered($table);
+ list($table,$prefix)=$this->istableprefix($table,$special);
+ $meta=$this->metadata_buffered($table,$prefix);
$r=Array();
- for ($i=0; $i < $meta["num_fields"]; $i++) {
- $j=$meta[$i]["name"];
+ 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]");
@@ -834,10 +897,11 @@
## pairs wich are in the given table
##
function capture_table_array ($table,$arr) {
- $meta=$this->metadata_buffered($table);
+ list($table,$prefix)=$this->istableprefix($table,$special);
+ $meta=$this->metadata_buffered($table,$prefix);
$r=Array();
- for ($i=0; $i < $meta["num_fields"]; $i++) {
- $j=$meta[$i]["name"];
+ for ($i=0; $i < $meta['num_fields']; $i++) {
+ $j=$meta[$i]['name'];
if (isset($arr[$j])) {
$r[$j] = $arr[$j];
$this->_QDebug("Found $j: $r[$j]");
@@ -880,7 +944,7 @@
// Alias to dispose_vars for compatibility to db_usql
function import_record_vars($params='') {
- $params[to_upper]=true;
+ $params['to_upper']=true;
$this->dispose_vars($params);
}
@@ -903,7 +967,7 @@
if (!is_array($params)) $params=ARRAY();
for ( reset($array); list($key,$val)=each($array); ) :
if (ereg("[A-Za-z_][A-Za-z0-9_]*", $key)) {
- if ($params[to_upper]) {
+ if ($params['to_upper']) {
$field_name = strtoupper($key);
} else {
# Default behavior, cause phplib
@@ -936,15 +1000,16 @@
## returns more than one row or nothing
##
function all_changed_vars ($table,$fields,$where='',$check='soft') {
- $meta=$this->metadata_buffered($table);
+ list($table,$prefix)=$this->istableprefix($table,$special);
+ $meta=$this->metadata_buffered($table,$prefix);
$q1="SELECT * FROM $table $where";
$this->query($q1);
$r=Array();
## TODO: uniform_vars missing here
if ($this->next_record()) {
- for ($i=0; $i < $meta["num_fields"]; $i++) {
- $j=$meta[$i]["name"];
+ 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]);
@@ -974,9 +1039,10 @@
function copy_result($table,$translate='') {
if(!is_array($translate)) $translate=Array();
while ($this->next_record()) {
- $meta=$this->metadata_buffered($table);
- for ( $i=0; $i < $meta[num_fields]; $i++) {
- $j=$meta[$i][name];
+ list($table,$prefix)=$this->istableprefix($table,$special);
+ $meta=$this->metadata_buffered($table,$prefix);
+ for ( $i=0; $i < $meta['num_fields']; $i++) {
+ $j=$meta[$i]['name'];
if ($translate[$j]) {
$v[$translate[$j]] = $this->Record[$j];
} else {
---------------------------------------------------------------------
To unsubscribe, e-mail: phplib-dev-unsubscribe <email protected>
For additional commands, e-mail: phplib-dev-help <email protected>
- Next message: ssilk: "[phplib-dev] cvs commit"
- Previous message: Kristian Köhntopp: "[phplib-dev] Summer holiday starting in Seattle..."
- Next in thread: ssilk: "[phplib-dev] cvs commit"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

