[geeklog-cvs] geeklog: Moved Randy's parse_csv_sql_string function from the MS...
geeklog-cvs at lists.geeklog.net
geeklog-cvs at lists.geeklog.net
Sun Mar 21 04:55:32 EDT 2010
changeset 7819:3676d32d9a6d
url: http://project.geeklog.net/cgi-bin/hgwebdir.cgi/geeklog/rev/3676d32d9a6d
user: Dirk Haun <dirk at haun-online.de>
date: Sun Mar 21 09:54:00 2010 +0100
description:
Moved Randy's parse_csv_sql_string function from the MS SQL driver to lib-datavase.php as DBINT_parseCsvSqlString for possible use in other DB drivers (e.g. Postgres)
diffstat:
system/databases/mssql.class.php | 99 +++++---------------------------
system/lib-database.php | 70 +++++++++++++++++++++++
2 files changed, 87 insertions(+), 82 deletions(-)
diffs (190 lines):
diff -r a6a301bada92 -r 3676d32d9a6d system/databases/mssql.class.php
--- a/system/databases/mssql.class.php Sat Mar 20 15:56:56 2010 -0400
+++ b/system/databases/mssql.class.php Sun Mar 21 09:54:00 2010 +0100
@@ -1081,91 +1081,26 @@
return $string;
}
-
-
-
-
-
-
- //since nothing can do this properly, i had to write it myself.
- //trick is that a string csv may have a comma within a delimited csv field which explode
- //cant handle
- function parse_csv_sql_string($csv){
- $len=strlen($csv);
- $mode=0; //mode=0 for non string, mode=1 for string
- $retArray=array();
- $thisValue='';
- for($x=0;$x<$len;$x++){
- //loop thru the string
- if($csv[$x]=="'"){
- if($x!=0){
- if($csv[$x-1]!="\\"){
- //this means that the preceeding char is not escape.. thus this is either the end of a mode 1 or the beginning of a mode 1
- if($mode==1){
- $mode=0;
- //this means that we are done this string value
- //dont add this character to the string
- }
- else{
- $mode=1;
- //dont add this character to the string....
- }
- }
- else{//this is a character to add.....
- $thisValue=$thisValue . $csv[$x];
- }
- }
- else{//x==0
- $mode=1;
- }
- }//end if csv
- elseif($csv[$x]==","){
- if($mode==1){
- //this means that the comma falls INSIDE of a string. its a keeper
- $thisValue=$thisValue . $csv[$x];
- }
- else{//this is the dilineation between fields.. pop this value
- array_push($retArray, $thisValue);
- $thisValue='';
- $mode=0;
- }
- }//end elseif
-
- else{
- //just add it!
- $thisValue=$thisValue . $csv[$x];
- }//end else
- }//end for
- array_push($retArray, $thisValue);
- return $retArray;
- }//end function
-
-
-
- //thanks to php.net for this
-function array_push_associative(&$arr) {
- $ret = 0;
- $args = func_get_args();
- foreach ($args as $arg) {
- if (is_array($arg)) {
- foreach ($arg as $key => $value) {
- @$arr[$key] = $value;
- $ret++;
- }
- }else{
- @$arr[$arg] = "";
- }
- }
- return $ret;
-}
-
+ // thanks to php.net for this
+ function array_push_associative(&$arr)
+ {
+ $ret = 0;
+ $args = func_get_args();
+ foreach ($args as $arg) {
+ if (is_array($arg)) {
+ foreach ($arg as $key => $value) {
+ @$arr[$key] = $value;
+ $ret++;
+ }
+ } else {
+ @$arr[$arg] = "";
+ }
+ }
-
-
-
-
+ return $ret;
+ }
/**
diff -r a6a301bada92 -r 3676d32d9a6d system/lib-database.php
--- a/system/lib-database.php Sat Mar 20 15:56:56 2010 -0400
+++ b/system/lib-database.php Sun Mar 21 09:54:00 2010 +0100
@@ -576,4 +576,74 @@
return $exists;
}
+/**
+* Parse a CSV-like SQL string, as used by DB_save
+*
+* This function will help parse the CVS-like strings that are used by DB_save.
+* Those are specific to MySQL and have to be handled separately by other DBs.
+*
+* Since nothing can do this properly, I had to write it myself.
+* Trick is that a string csv may have a comma within a delimited csv field
+* which explode can't handle.
+*
+* @param string $csv The string to parse
+* @return array parsed string contents
+* @author Randy Kolenko
+* @see DB_save
+* @internal to be used by the DB drivers only
+*
+*/
+function DBINT_parseCsvSqlString($csv)
+{
+ $len = strlen($csv);
+ $mode = 0; // mode=0 for non string, mode=1 for string
+ $retArray = array();
+ $thisValue = '';
+ for ($x = 0; $x < $len; $x++) {
+ // loop thru the string
+ if ($csv[$x] == "'") {
+ if ($x != 0) {
+ if ($csv[$x-1] != "\\") {
+ /**
+ * this means that the preceeding char is not escape..
+ * thus this is either the end of a mode 1 or the beginning
+ * of a mode 1
+ */
+ if ($mode == 1) {
+ $mode = 0;
+ // this means that we are done this string value
+ // don't add this character to the string
+ } else {
+ $mode = 1;
+ // don't add this character to the string....
+ }
+ } else {
+ //this is a character to add.....
+ $thisValue = $thisValue . $csv[$x];
+ }
+ } else {
+ // x==0
+ $mode = 1;
+ }
+ } elseif ($csv[$x] == ",") {
+ if ($mode == 1) {
+ // this means that the comma falls INSIDE of a string.
+ // its a keeper
+ $thisValue = $thisValue . $csv[$x];
+ } else {
+ // this is the dilineation between fields.. pop this value
+ array_push($retArray, $thisValue);
+ $thisValue = '';
+ $mode = 0;
+ }
+ } else {
+ // just add it!
+ $thisValue = $thisValue . $csv[$x];
+ }
+ }
+ array_push($retArray, $thisValue);
+
+ return $retArray;
+}
+
?>
More information about the geeklog-cvs
mailing list