Date: 03/19/00
- Next message: ssilk: "[PHPLIB-DEV] cvs commit"
- Previous message: ssilk: "[PHPLIB-DEV] cvs commit"
- Next in thread: ssilk: "[PHPLIB-DEV] cvs commit"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
From: ssilk
Date: Mon Mar 20 02:44:13 2000
Modified files:
php-lib/php/query_sql.inc
Log message:
Here is the ulitmative "must-have" of Query. :-)
Sorry, I just imported the functions only from another project into here
- so I haven't testet it yet.
Index: php-lib/php/query_sql.inc
diff -u php-lib/php/query_sql.inc:1.12 php-lib/php/query_sql.inc:1.13
--- php-lib/php/query_sql.inc:1.12 Tue Mar 7 00:45:30 2000
+++ php-lib/php/query_sql.inc Mon Mar 20 02:43:43 2000
@@ -9,7 +9,7 @@
* is based in November 1997,
* it was a collection of functions for PHP/FI and mSQL.
*
- * $Id: query_sql.inc,v 1.12 2000/03/06 23:45:30 ssilk Exp $
+ * $Id: query_sql.inc,v 1.13 2000/03/20 01:43:43 ssilk Exp $
*
*/
@@ -38,7 +38,8 @@
## currently unused, this var is just an idea for later use.
var $Backslash_N_is_NULL = false;
- ## Metacache: see funtcion metadata_buffered()
+ ## Metacache: see function metadata_buffered()
+ ## Only needed if you are using ALTER TABLE during program runs
var $meta_cache_off = false;
## This is the char, which will be replaced by \char.
@@ -61,6 +62,15 @@
## needed by some applications (SAP for example)
var $NULLIsSpace = false;
+ ## Fireball-stuff
+ ## a regular expression that replaces chars that are
+ ## listed here with '_'; this replaces all punctuation-chars
+ ## (;.+-/!$&/(=#*~ etc.)
+ var $fireball_exclude_ereg = "[[:punct:]]";
+ ## and the char(s) that the found stuff will replaced with
+ ## another meaningful value could be '%'
+ var $fireball_replace_char = "_";
+
###########################
## _QDebug
function _QDebug ($str) {
@@ -427,8 +437,6 @@
-
-
##
## WHERE-PLAIN-CLAUSE
## Let you generate a WHERE-Clause with a Loop.
@@ -472,7 +480,8 @@
## It can handle NULL-Values (IS NULL) in a special manner:
## if a value of $fields is 'NULL', we are looking, if the
## operator is '='. In this case the operator is changed into "IS"
- ## in any other case it is changed into "IS NOT".
+ ## in any other case it is changed into "IS NOT". (won't match
+ ## 'like' etc. but this function is not thought for such operations!)
##
## $tables table
## $fields Assoc name=>value-fields
@@ -635,10 +644,10 @@
# uses always lowercase
$field_name = strtolower($key);
}
- if ($this->Q_Debug) echo "'$field_name' => '$val'<br>\n";
+ $this->_QDebug("'$field_name' => '$val'");
$GLOBALS[$field_name]=$val;
} else {
- if ($this->Q_Debug) echo "Not matching: '$key'<br>\n";
+ $this->_QDebug("Not matching: '$key'");
}
endfor;
}
@@ -656,21 +665,6 @@
}
-/*--------------------------------------------
- There is no sense in it!
- deleted this function!
- //--------------------------------------------------------------
- // 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 "copy" all the records of
@@ -679,7 +673,7 @@
// this_query_column=>my_tablename_column
// renames this_query_column into my_table_column
//--------------------------------------------------------------
- function insert_query($table,$translate='') {
+ function copy_result($table,$translate='') {
if(!is_array($translate)) $translate=Array();
while ($this->next_record()) {
$meta=$this->metadata_buffered($table);
@@ -691,14 +685,218 @@
$v[$j] = $this->Record[$j];
}
}
- $this->insert_plain_clause($table,$v,ARRAY());
+ $this->query($this->insert_plain_clause($table,$v));
}
}
// compatibility function to db_usql
function dump_query($tablename,$bla='') {
- $this->insert_query($tablename);
+ $this->copy_result($tablename);
+ }
+
+
+ //------------------------------------------------------------
+ // This is great: A function which generates a search query
+ // from a single input-field like Fireball does.
+ // (of course there are many other searching engines, which use
+ // this format, but I like the name :-)
+ // Documentation:
+ // $string: Is the searching string, it knows the following rules:
+ // 1. 'word1 word2' Result includes word1 OR word2
+ // 2. '+word1 +word2' Result includes word1 AND word2
+ // 3. '-word' Result dosn't include this word
+ // 4. '"fasel bla"'
+ // 'fasel-bla' Word is taken as a single word the
+ // punctuation is replaced with '_'
+ // (configurable!)
+ // ANY of the combinations of rule 1-4 and any number of words
+ // (see $maxwords) are VALID!
+ //
+ // $query: This is a part of a WHERE-Clause, that should be generated.
+ // It should look like the following:
+ // "( first_name like '%{s}%' or name like '%{s}%' or".
+ // " street like '%{s}%' or zipcode like '{s}%' or".
+ // " town like '%{s}%')"
+ // {s} is replaced by the current searching-word.
+ // The query has to be placed into brackets, cause otherwise
+ // the like operators will match false.
+ //
+ // $maxwords: Maximum number of searching words. Zero (default)
+ // will not check. Too much words returns ARRAY(false,1);
+ //
+ // RETURN-VALUES:
+ // We will currently return an array with two values:
+ // list($wherepart,$what) = fireball($x,$y);
+ // $wherepart: is the part of the query which could be included to
+ // the WHERE-Part of your Query.
+ // We advise you, that a user can easily create
+ // queries which are bigger than the maximum
+ // buffer-size of the underlaying database;
+ // this has to be checked by yourself!
+ // This result could be directly used as parameter $where
+ // within the function where_Clause() or related functions
+ // $what: Is a simplified form of the $part, which could be
+ // used for other things, like informing the user
+ // about what he is currently searching.
+ // It's on you to translate this into a more understandable form
+ // Example:
+ // list($wherepart,$what)=
+ // fireball_clause("hugo +bla -hugobla","(name='{s}')");
+ // echo "$wherepart<br>$what";
+ // Output:
+ // ( (name='hugo') OR (name='bla') ) AND (name='bla') AND NOT (name='hugobla')
+ // ( 'hugo' OR 'bla' ) AND 'bla' AND NOT 'hugobla'
+ //-------------------------------------------------------------
+ function fireball_Clause ($string,$query,$maxwords=0) {
+
+ ## Part 1: Normalisation of $string
+ ## replace any tab or other char with space and remove trailing
+ ## backspace from '"'-char.
+ $string=ereg_Replace("[[:space:]]"," ",
+ str_Replace('\\"','"',trim($string)));
+ ## It replaces '"hugo bla"' with 'hugo_bla'
+ ## The first version of this part worked with regular expressions
+ ## But this version seems to be faster and more stable
+ $tmp=' '; $mod=0;
+ for ($i=0 ; $i < strlen($string) ; $i++ ) {
+ $j=substr($string,$i,1);
+ if ( '"'==$j ) {
+ if (!$mod && ereg("[ +-]",$tmp) ) {
+ $mod=1; $merker=$i;
+ }
+ elseif ($mod &&
+ ( $i+1 >= strlen($string) || substr($string,$i+1,1)==' ')) {
+ $string=substr($string,0,$i) . substr($string,$i+1);
+ $string=substr($string,0,$merker) .
+ substr($string,$merker+1);
+ $mod=0; $i--;
+ }
+ }
+ elseif ($j==' ' && $mod) {
+ $string=substr($string,0,$i) .$this->fireball_replace_char.
+ substr($string,$i+1);
+ }
+ $tmp=$j;
+ }
+
+
+ ## Part 2: Tokenising
+ ## replace multiple spaces into one and split it up
+ $string=ereg_Replace(" +"," ",$string);
+ $s=split(" ",$string);
+
+ ## Part 2.a: Too much words?
+ if ($maxwords) {
+ if ($count($s) > $maxwords) {
+ Return(false,1);
+ }
+ }
+
+ ## Part 2.b: UNIMPLEMENTED - this is just a comment for later use
+ ## A check about words that already containing other words
+ ## but thats difficult, think about: '+cars car -"cable car"'
+ ## so I introduce it when I really need it :-)
+
+ ## Part 3: put the tokens into drawers and replace unwanted chars
+ for (reset($s) ; list($cnt,$val)=each($s) ; ) {
+ switch (substr($val,0,1)) {
+ case '+':
+ $t[$cnt]="+";
+ $s[$cnt]=ereg_Replace($this->fireball_exclude_ereg,
+ $this->fireball_replace_char,substr($val,1));
+ break;
+ case '-':
+ $t[$cnt]="-";
+ $s[$cnt]=ereg_Replace($this->fireball_exclude_ereg,
+ $this->fireball_replace_char,substr($val,1));
+ break;
+ default:
+ $t[$cnt]="1";
+ $s[$cnt]=ereg_Replace($this->fireball_exclude_ereg,
+ $this->fireball_replace_char,$val);
+ }
+ }
+
+ ## Part 4: Creating a pseudoquery
+ ## smarter persons than me can of course put part 3-5 into
+ ## one part, but this code wouldn't be understandable any more :)
+ for ( reset($s) ; list($cnt)=each($s) ; ) {
+ switch ($t[$cnt]) {
+ case '+':
+ $q[]=$cnt;
+ break;
+ case '-':
+ $q[]="-$cnt";
+ break;
+ case '1':
+ unset($tmp);
+ for ( $i=0 ; $i<Count($s) ; $i++ ) {
+ switch ($t[$i]) {
+ case "1":
+ $t[$i]=2;
+ case "+":
+ $tmp[]=$i;
+ }
+ }
+ $q[]="( ". join(" OR ",$tmp) ." )";
+ }
+ }
+ $p=split(" ",join(" AND ",$q)); # not bad, eh? :)
+ $m=$p; # to generate a understandable output
+
+ ## Part 5: reparse pseudoquery back to a query
+ for ( $cnt=0; $cnt<Count($p) ; $cnt++ ) {
+ $val=$p[$cnt];
+ if (ereg("(-?)([[:digit:]]+)",$val,$regs)) {
+ if ($regs[1]) {
+ $o=" NOT ";
+ } else {
+ $o="";
+ }
+ $tmp=$s[$regs[2]]; ## Takes string no. $regs[2] for replacement
+ $p[$cnt]=$o.
+ str_Replace("{s}",$tmp,$query);
+ $m[$cnt]="$o '$tmp'";
+ }
+ }
+
+ # Remove unused brakets
+ $l=ereg_Replace("^\((.*)\)$","\\1",join(" ",$m) );
+
+ $this->_QDebug("Where-Part: '".join(" ",$p)."'<br>What: $l");
+
+ return(Array(join(" ",$p),$l) );
+ }
+
+ //--------------------------------------------------------------
+ // this function can be used to export all the columns of
+ // an assoc array *global* variables.
+ // The names of the indizes are the name of the global var
+ // So it is nearly the same function as dispose_vars()
+ // and another oposite function to capture_vars()
+ //
+ // $param is an optional assoc-array-parameter:
+ // to_upper=>true - uppercases all vars, otherwise lowercase
+ //--------------------------------------------------------------
+ function dispose_asoc_array($array,$params='') {
+ 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]) {
+ $field_name = strtoupper($key);
+ } else {
+ # Default behavior, cause phplib
+ # uses always lowercase
+ $field_name = strtolower($key);
+ }
+ $this->_QDebug("'$field_name' => '$val'");
+ $GLOBALS[$field_name]=$val;
+ } else {
+ $this->_QDebug("Not matching or empty: '$key'");
+ }
+ endfor;
}
+
}
-
PHPLIB Developers Mailing List. Send messages to <phplib-dev <email protected>>.
To unsubscribe, send "unsubscribe" to <phplib-dev-request <email protected>> in
the body, not the subject, of your message.
- Next message: ssilk: "[PHPLIB-DEV] cvs commit"
- Previous message: ssilk: "[PHPLIB-DEV] cvs commit"
- Next in thread: ssilk: "[PHPLIB-DEV] cvs commit"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

