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
      *
      * @param    Object result result from a DB-query
      * @param    array  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
      *
      * @param    Object  result result from a DB-query
      * @param    array   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
      *
      * @param    Object result result from a DB-query
      * @param    array  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 = False;
            $i = 0;

            while (list($key, $val) = each($res))
            {
                $tableInfo[$i][table]= "result";
                $tableInfo[$i][name] = $key;
                $resFirstRow[$i] = $val;
                $i++;
            }
            $res  = $resFirstRow;
            $FirstFetchDone = True;
            $fetchmode = DB_FETCHMODE_ORDERED;
        }
        else
        {
            $fetchmode = DB_FETCHMODE_ORDERED;
        }

        // initialize db hierarchy...
        $parenttable = "root";
        $tableInfo[parent_key][root] = 0;
        foreach ($tableInfo as $key => $value)
        {
            if (is_int($key))
            {
                if (is_null($tableInfo[parent_table][$value[table]]))
                {
                    $tableInfo[parent_key][$value[table]] = $key;
                    $tableInfo[parent_table][$value[table]] = $parenttable;
                    $parenttable = $value[table] ;
                }
            }
            //if you need more tableInfo for later use you can write a function 
add_tableInfo..
            $this->add_tableInfo($key, $value, &$tableInfo);
        }

        // end initialize
        $parent[root] = $this->insert_new_result(&$tableInfo);

        while ($FirstFetchDone || $res = $result->FetchRow($fetchmode))
        {
            //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)
                {
                    if ($tableInfo[parent_key][$tableInfo[$key][table]] == $key )
                    {
                        if ($this->nested || $key == 0)
                        {

                            $parent[$tableInfo[$key][table]] =  
$this->insert_new_row($parent[$tableInfo[parent_table][$tableInfo[$key][table]]],$res,$key,&$tableInfo);
                        }
                        else
                        {
                            $parent[$tableInfo[$key][table]]= 
$parent[$tableInfo[parent_table][$tableInfo[$key][table]]];
                        }

                        //set all children entries to somethin stupid
                        foreach($tableInfo[parent_table] as $pkey => $pvalue)
                        {
                            if ($pvalue == $tableInfo[$key][table])
                            {
                                $resold[$tableInfo[parent_key][$pkey]]= 
"ThisIsJustAPlaceHolder";
                            }
                        }

                    }

                    
$this->insert_new_element($parent[$tableInfo[$key][table]],$res,$key,&$tableInfo,&$subrow);


                }
            }

            $resold = $res;
            unset ($subrow);
        }
        return $this->xmldoc;
    }

    function setOption($option, $value)
    {
        if (isset($this->$option)) {
            $this->$option = $value;

            return True;
        }
        return False;
        //        return $this->raiseError("unknown option $option");
    }

// this are the functions, which are intended to be overriden in user classes

    function insert_new_result (&$metadata)
    {
        if ($this->xmlroot)
            return $this->xmlroot->new_child("result",Null);
        else
            return $this->xmldoc->add_root("result");
    }

    function insert_new_row ($parent_row, $res, $key, &$metadata)
    {
        return  $parent_row->new_child("row",Null);
    }

    function insert_new_element ($parent, $res, $key, &$metadata, &$subrow)
    {
        return  $parent->new_child($metadata[$key][name], utf8_encode($res[$key]));
    }

    function add_tableInfo ($key, $value, &$metadata) {

    }
}
?>

Index: php4/pear/Experimental/XML/sql2xml_ext.php
+++ php4/pear/Experimental/XML/sql2xml_ext.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_ext.php,v 1.1 2001/03/24 20:00:02 chregu Exp $


/**
 *  This class shows with one example, how the base sql2xml-class
 *   could be extended.
 *
 * Usage example
 *
 * include_once ("DB.php");
 * include_once("XML/sql2xml_ext.php");
 * $xml = new xml_sql2xml_ext;
 * $options= array( user_options => array (xml_seperator =>"_",
 *                                       element_id => "id"),
 * );
 * $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_ext.php,v 1.1 2001/03/24 20:00:02 chregu Exp $
 */

require_once ("XML/sql2xml.php");


class XML_sql2xml_ext extends XML_sql2xml {


    function insert_new_row ($parent_row,$res,$key,&$tableInfo)
    {

        $new_row= $parent_row->new_child("row",Null);
        /* make an unique ID attribute in the row element with tablename.id if there's 
an id
               otherwise just make an unique id with the php-function, just that 
there's a unique id for this row.
                CAUTION: This ID changes every time ;) (if no id from db-table)
               */
        if ($res[$tableInfo[id][$tableInfo[$key][table]]])
        {
            if ($res[$tableInfo[id][$tableInfo[$key][table]]] == 
$this->user_options[id])
            {
                $new_row->set_attribute("selected","selected");
            }
            
$new_row->set_attribute("ID",utf8_encode($tableInfo[$key][table].$res[$tableInfo[id][$tableInfo[$key][table]]]));
        }
        else
        {
            $new_row->set_attribute("ID", uniqid($tableInfo[$key][table]));
        }

        return $new_row;
    }


    function insert_new_result (&$tableInfo) {

    if ($this->xmlroot)
            $xmlroot=$this->xmlroot->new_child($tableInfo[0][table],Null);
        else
            $xmlroot= $this->xmldoc->add_root($tableInfo[0][table]);
        $xmlroot->set_attribute("type","Database");
        return $xmlroot;


    }

    function insert_new_element ($parent,$res,$key,&$tableInfo,&$subrow) {

        if ($this->user_options[xml_seperator])
        {
            //the preg should be only done once....
            $i=0;
            
preg_match_all("/([^".$this->user_options[xml_seperator]."]+)".$this->user_options[xml_seperator]."*/",$tableInfo[$key][name],$regs);
            $subrow[$regs[1][-1]]= $parent;

            // here we separate db fields to subtags.
            for ($i=0;$i<(count($regs[1])-1);$i++)
            {
                if ( ! $subrow[$regs[1][$i]]) {
                    $subrow[$regs[1][$i]]= 
$subrow[$regs[1][$i-1]]->new_child($regs[1][$i],Null);
                }
            }
            
$subrows=$subrow[$regs[1][$i-1]]->new_child($regs[1][$i],utf8_encode($res[$key]));
        }
        else
        {
            $regs[1][0] = $tableInfo[$key][name];
            $i=0;
            $subrow=$parent->new_child($regs[1][$i],utf8_encode($res[$key]));
        }

    }

    function add_tableInfo ($key,$value,&$tableInfo) {
        if (!$metdata[id][$value[table]] && $value[name] == 
$this->user_options[element_id] )
        {
            $tableInfo[id][$value[table]]= $key;
        }

    }
}
?>

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