chregu          Fri Apr  6 03:13:13 2001 EDT

  Modified files:              
    /php4/pear/Experimental/XML sql2xml.php 
  Log:
  - Row and result Tag names are now configurable
  - some errorchecking
  
  
Index: php4/pear/Experimental/XML/sql2xml.php
diff -u php4/pear/Experimental/XML/sql2xml.php:1.6 
php4/pear/Experimental/XML/sql2xml.php:1.7
--- php4/pear/Experimental/XML/sql2xml.php:1.6  Wed Apr  4 02:15:57 2001
+++ php4/pear/Experimental/XML/sql2xml.php      Fri Apr  6 03:13:13 2001
@@ -15,19 +15,16 @@
 // | Authors: Christian Stocker <[EMAIL PROTECTED]>                         |
 // +----------------------------------------------------------------------+
 //
-// $Id: sql2xml.php,v 1.6 2001/04/04 09:15:57 chregu Exp $
+// $Id: sql2xml.php,v 1.7 2001/04/06 10:13:13 chregu Exp $
 
-require_once("PEAR.php");
-
 /**
 * This class takes a PEAR::DB-Result Object, a sql-query-string or an array
 *  and returns a xml-representation of it.
 *
-* More docs will follow
-*
 * TODO
 *   -encoding etc, options for header
-*   - ERROR CHECKING
+*   -ERROR CHECKING
+*
 * Usage example
 *
 * include_once ("DB.php");
@@ -39,16 +36,17 @@
 *
 * or
 *
+* include_once ("DB.php");
+* include_once("XML/sql2xml.php");
 * $sql2xml = new xml_sql2xml("mysql://root@localhost/xmltest");
-* $sql2xml->AddSql("select * from bands");
+* $sql2xml->Add("select * from bands");
 * $xmlstring = $sql2xml->getXML();
 *
-* more examples and outputs on
+* More documentation and a tutorial/how-to can be found at
 *   http://www.nomad.ch/php/sql2xml
-*   for the time being
 *
 * @author   Christian Stocker <[EMAIL PROTECTED]>
-* @version  $Id: sql2xml.php,v 1.6 2001/04/04 09:15:57 chregu Exp $
+* @version  $Id: sql2xml.php,v 1.7 2001/04/06 10:13:13 chregu Exp $
 * @package  XML
 */
 class XML_sql2xml {
@@ -67,6 +65,22 @@
     var $nested = True;
 
     /**
+    * Name of the tag element for resultsets
+    *
+    * @var  string
+    * @see  insertNewResult()
+    */
+    var $tagNameResult = "result";
+
+    /**
+    * Name of the tag element for rows
+    *
+    * @var  string
+    * @see  insertNewRow()
+    */
+    var $tagNameRow = "row";
+
+    /**
     *
     * @var   object PEAR::DB
     * @acces private
@@ -159,15 +173,12 @@
     * @param  $dsn string with PEAR::DB "data source name" or object DB object
     * @param  $root string of the name of the xml-doc root element.
     */
-    function XML_sql2xml ($dsn=False,$root = "root") {
+    function XML_sql2xml ($dsn=Null,$root = "root") {
 
-        if (DB::isError($dsn)) {
-            print "The given param for XML_sql2xml was not valid in file ".__FILE__." 
at line ".__LINE__."<br>\n";
-            return new DB_Error($dsn->code,PEAR_ERROR_DIE);
-        }
-
         // if it's a string, then it must be a dsn-identifier;
-        if (is_string($dsn)) {
+        if (is_string($dsn))
+        {
+            include_once ("DB.php");
             $this->db = DB::Connect($dsn);
             if (DB::isError($this->db))
             {
@@ -176,6 +187,13 @@
             }
 
         }
+
+        elseif (DB::isError($dsn))
+        {
+            print "The given param for XML_sql2xml was not valid in file ".__FILE__." 
+at line ".__LINE__."<br>\n";
+            return new DB_Error($dsn->code,PEAR_ERROR_DIE);
+        }
+
         // if parent class is db_common, then it's already a connected identifier
         elseif (get_parent_class($dsn) == "db_common")
         {
@@ -306,9 +324,12 @@
             new DB_Error($result->code,PEAR_ERROR_DIE);
         }
 
-        //user should be able to give his own tableInfo-array, but not implemented yet
+        // the method_exists is here, cause tableInfo is only in the cvs at the moment
+        // (should be in 4.0.6)
+        // BE CAREFUL: if you have fields with the same name in different tables, you 
+will get errors
+        // later, since DB_FETCHMODE_ASSOC doesn't differentiate that stuff.
 
-        if (! ($tableInfo = $result->tableInfo(False)))
+        if (!method_exists($result,"tableInfo") || ! ($tableInfo = 
+$result->tableInfo(False)))
         {
             //emulate tableInfo. this can go away, if every db supports tableInfo
             $fetchmode = DB_FETCHMODE_ASSOC;
@@ -318,7 +339,7 @@
 
             while (list($key, $val) = each($res))
             {
-                $tableInfo[$i]["table"]= "result";
+                $tableInfo[$i]["table"]= $this->tagNameResult;
                 $tableInfo[$i]["name"] = $key;
                 $resFirstRow[$i] = $val;
                 $i++;
@@ -363,7 +384,6 @@
         {
             //FirstFetchDone is only for emulating tableInfo, as long as not all dbs 
support tableInfo. can go away later
             $FirstFetchDone = False;
-
             while (list($key, $val) = each($res))
             {
                 if ($resold[$tableInfo["parent_key"][$tableInfo[$key]["table"]]] != 
$res[$tableInfo["parent_key"][$tableInfo[$key]["table"]]] || !$this->nested)
@@ -415,7 +435,7 @@
     function DoArray2Xml ($array, $parent) {
         while (list($key, $val) = each($array))
             {
-                $tableInfo[$key]["table"]= "result";
+                $tableInfo[$key]["table"]= $this->tagNameResult;
                 $tableInfo[$key]["name"] = $key;
             }
         if ($this->user_tableInfo)
@@ -485,9 +505,9 @@
     function insertNewResult(&$metadata)
     {
         if ($this->xmlroot)
-            return $this->xmlroot->new_child("result", NULL);
+            return $this->xmlroot->new_child($this->tagNameResult, NULL);
         else
-            return $this->xmldoc->add_root("result");
+            return $this->xmldoc->add_root($this->tagNameResult);
     }
 
 
@@ -503,7 +523,7 @@
     */
     function insertNewRow($parent_row, $res, $key, &$metadata)
     {
-        return  $parent_row->new_child("row", NULL);
+        return  $parent_row->new_child($this->tagNameRow, NULL);
     }
 
 



-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to