[PHP-CVS] cvs: php4 /pear/Experimental/XML sql2xml.php

2001-04-06 Thread Christian Stocker

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 

[PHP-CVS] cvs: php4 /pear/Experimental/XML sql2xml.php

2001-03-30 Thread Christian Stocker

chregu  Fri Mar 30 02:36:42 2001 EDT

  Modified files:  
/php4/pear/Experimental/XML sql2xml.php 
  Log:
  - new user_tableInfo option for user-contributed xml-structure
  - Error checking in doSql2Xml
  - xml_encode function for correctly parsing of s.
  
  
Index: php4/pear/Experimental/XML/sql2xml.php
diff -u php4/pear/Experimental/XML/sql2xml.php:1.2 
php4/pear/Experimental/XML/sql2xml.php:1.3
--- php4/pear/Experimental/XML/sql2xml.php:1.2  Thu Mar 29 13:26:53 2001
+++ php4/pear/Experimental/XML/sql2xml.php  Fri Mar 30 02:36:42 2001
@@ -15,7 +15,7 @@
 // | Authors: Christian Stocker [EMAIL PROTECTED] |
 // +--+
 //
-// $Id: sql2xml.php,v 1.2 2001/03/29 21:26:53 uw Exp $
+// $Id: sql2xml.php,v 1.3 2001/03/30 10:36:42 chregu Exp $
 
 /**
 * This class takes a PEAR::DB-Result Object (or more than one)
@@ -37,7 +37,7 @@
 *   for the time being
 *
 * @author   Christian Stocker [EMAIL PROTECTED]
-* @version  $Id: sql2xml.php,v 1.2 2001/03/29 21:26:53 uw Exp $
+* @version  $Id: sql2xml.php,v 1.3 2001/03/30 10:36:42 chregu Exp $
 */
 class XML_sql2xml {
 
@@ -71,7 +71,14 @@
 
 /**
 *
+* @var  boolean
+*/
+var $user_tableInfo = False;
+
+
+/**
 *
+*
 * @param  string
 */
 function XML_sql2xml ($root = "root") {
@@ -139,6 +146,11 @@
 function doSql2Xml($result, $options = False)
 {
 
+if (DB::IsError($result)) {
+print "Error in file ".__FILE__." at line ".__LINE__."br\n";
+new DB_Error($result-code,PEAR_ERROR_DIE);
+}
+
 //set options
 if (is_array($options))
 {
@@ -191,6 +203,12 @@
 }
 
 // end initialize
+
+// if user made some own tableInfo data, merge them here.
+if ($this-user_tableInfo) 
+{
+$tableInfo = $this-array_merge_clobber($tableInfo,$this-user_tableInfo);
+}
 $parent[root] = $this-insertNewResult($tableInfo);
 
 while ($FirstFetchDone || $res = $result-FetchRow($fetchmode))
@@ -299,7 +317,7 @@
 */
 function insertNewElement($parent, $res, $key, $metadata, $subrow)
 {
-return  $parent-new_child($metadata[$key]["name"], utf8_encode($res[$key]));
+return  $parent-new_child($metadata[$key]["name"], 
+$this-xml_encode($res[$key]));
 }
 
 
@@ -313,5 +331,50 @@
 function addTableInfo($key, $value, $metadata) {
 
 }
+
+/**
+*
+* @param
+* @abstract
+*/
+function xml_encode ($text) {
+$text = utf8_encode(ereg_replace("","amp;",$text));
+return $text;
+}
+//taken from [EMAIL PROTECTED] at 
+http://www.php.net/manual/en/function.array-merge-recursive.php
+/**
+* There seemed to be no built in function that would merge two arrays recursively 
+and clobber
+*   any existing key/value pairs. Array_Merge() is not recursive, and 
+array_merge_recursive
+*   seemed to give unsatisfactory results... it would append duplicate key/values.
+*
+*   So here's a cross between array_merge and array_merge_recursive
+**/
+
+/**
+*
+* @param  
+* @param  
+* @abstract
+*/
+function array_merge_clobber($a1,$a2)
+{
+if(!is_array($a1) || !is_array($a2)) return false;
+$newarray = $a1;
+while (list($key, $val) = each($a2))
+{
+if (is_array($val)  is_array($newarray[$key]))
+{
+$newarray[$key] = $this-array_merge_clobber($newarray[$key], $val);
+}
+else
+{
+$newarray[$key] = $val;
+}
+}
+return $newarray;
+}
+
 }
 ?
+
+



-- 
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]




[PHP-CVS] cvs: php4 /pear/Experimental/XML sql2xml.php

2001-03-30 Thread Christian Stocker

chregu  Fri Mar 30 04:55:30 2001 EDT

  Modified files:  
/php4/pear/Experimental/XML sql2xml.php 
  Log:
  too much whitespaces after ?
  
  
Index: php4/pear/Experimental/XML/sql2xml.php
diff -u php4/pear/Experimental/XML/sql2xml.php:1.4 
php4/pear/Experimental/XML/sql2xml.php:1.5
--- php4/pear/Experimental/XML/sql2xml.php:1.4  Fri Mar 30 03:03:11 2001
+++ php4/pear/Experimental/XML/sql2xml.php  Fri Mar 30 04:55:30 2001
@@ -15,7 +15,7 @@
 // | Authors: Christian Stocker [EMAIL PROTECTED] |
 // +--+
 //
-// $Id: sql2xml.php,v 1.4 2001/03/30 11:03:11 chregu Exp $
+// $Id: sql2xml.php,v 1.5 2001/03/30 12:55:30 chregu Exp $
 
 /**
 * This class takes a PEAR::DB-Result Object (or more than one)
@@ -37,7 +37,7 @@
 *   for the time being
 *
 * @author   Christian Stocker [EMAIL PROTECTED]
-* @version  $Id: sql2xml.php,v 1.4 2001/03/30 11:03:11 chregu Exp $
+* @version  $Id: sql2xml.php,v 1.5 2001/03/30 12:55:30 chregu Exp $
 */
 class XML_sql2xml {
 
@@ -376,5 +376,3 @@
 
 }
 ?
-
-



-- 
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]




[PHP-CVS] cvs: php4 /pear/Experimental/XML sql2xml.php sql2xml_ext.php

2001-03-24 Thread Christian Stocker

chregu  Sat Mar 24 12:00:03 2001 EDT

  Added files: 
/php4/pear/Experimental/XML sql2xml.php sql2xml_ext.php 
  Log:
  First Commit of the sql2xml classes.
  
  This class takes a PEAR::DB-Result Object (or more than one) and returns a
  xml-representation of it.
  
  
  


Index: php4/pear/Experimental/XML/sql2xml.php
+++ php4/pear/Experimental/XML/sql2xml.php
?php
// +--+
// | PHP version 4.0  |
// +--+
// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group |
// +--+
// | This source file is subject to version 2.0 of the PHP license,   |
// | that is bundled with this package in the file LICENSE, and is|
// | available at through the world-wide-web at   |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to  |
// | [EMAIL PROTECTED] so we can mail you a copy immediately.   |
// +--+
// | Authors: Christian Stocker [EMAIL PROTECTED] |
// +--+
//
// $Id: sql2xml.php,v 1.1 2001/03/24 20:00:02 chregu Exp $

/**
* This class takes a PEAR::DB-Result Object (or more than one)
*  and returns a xml-representation of it.
*
* More docs will follow
*
* Usage example
*
* include_once ("DB.php");
* include_once("XML/sql2xml.php");
* $db = DB::connect("mysql://root@localhost/xmltest");
* $xml = new xml_sql2xml;
* $result = $db-query("select * from bands");
* $xmlstring = $xml-getxml($result,$options));
*
* more examples and outputs on
*   http://www.nomad.ch/php/sql2xml
*   for the time being
*
* @author   Christian Stocker [EMAIL PROTECTED]
* @version  $Id: sql2xml.php,v 1.1 2001/03/24 20:00:02 chregu Exp $
*/
class XML_sql2xml {

/**
*
* @var  boolean
*/
var $nested = True;


/**
*
* @var  boolean
*/
var $user_options = False;


/**
*
* @var  object  domxml
*/
var $xmldoc;


/**
*
* @var  string
*/
var $xmlroot = "";


/**
*
*
* @param  string
*/
function XML_sql2xml ($root = "root") {

$this-xmldoc = domxml_new_xmldoc('1.0');
if ($root) {
$this-xmlroot = $this-xmldoc-add_root($root);
}

}

/**
  * Adds an aditional resultset to the xml-document
  *
  * @paramObject result result from a DB-query
  * @paramarray  mixed   options to be passed (does it need that?)
  * @return   string  xml
  * @access   public
  */
function add_result($result, $options = False)
{
$this-do_sql2xml($result, $options);
}

/**
  * Returns an xml-string with a xml-representation of the resultsets.
  *
  * The resultset can be directly provided here, or if you need more than one
  * in your xml, then you have to provide each of them with add_result befor getxml
  *
  * @paramObject  result result from a DB-query
  * @paramarray   mixed   options to be passed (does it need that?)
  * @return   string  xml
  * @access   public
  */

function getxml($result = False, $options = False)
{
return domxml_dumpmem($this-getxmlObject($result, $options));
}

/**
  * Returns an xml DomDocument Object with a xml-representation of the resultsets.
  *
  * The resultset can be directly provided here, or if you need more than one
  * in your xml, then you have to provide each of them with add_result befor getxml
  *
  * @paramObject result result from a DB-query
  * @paramarray  mixed   options to be passed (does it need that?)
  * @return   Object DomDocument
  * @access   public
  */
function getxmlObject($result = False, $options = False) {
if ($result) {
$this-do_sql2xml($result, $options);
}
return $this-xmldoc;
}

function do_sql2xml($result, $options = False)
{

//set options
if (is_array($options))
{
foreach ($options as $option = $value)
{
$this-setOption($option, $value);
}
}
//user should be able to give his own tableInfo-array, but not implemented yet
if (! ($tableInfo = $result-tableInfo(False)))
{
//emulate tableInfo. this can go away, if every db supports tableInfo
$fetchmode = DB_FETCHMODE_ASSOC;
$res = $result-FetchRow($fetchmode);
$this-nested