PEAR and DB using php
<? Php
// + ------------------------------------------------------------------------ +
// | PHP version 4.0
| // + ------------------------------------------------------------------------ +
// | Copyright (c) 1997,199 8, 1999,200 0, 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
| // | Http://www.php.net/license/2_02.txt.
| // | If you did not receive a copy of the PHP license and are unable
| // | Obtain it through the world-wide-web, please send a note
| // License@php.net so we can mail you a copy immediately.
| // + ------------------------------------------------------------------------ +
// | Authors: Christian Stocker <chregu@phant.ch>
| // + ------------------------------------------------------------------------ +
//
// $ Id: sql2xml. php, v 1.59 2001/11/13 10:54:02 chregu Exp $
/**
* This class takes a PEAR: DB-Result Object, a SQL-query-string or an array
* And returns a xml-representation of it.
*
* TODO
*-Encoding etc, options for header
*-ERROR CHECKING
*
* Usage example
*
* Include_once ("DB. php ");
* Include_once ("XML/sql2xml. php ");
* $ Db = DB: connect ("mysql: // root @ localhost/xmltest ");
* $ Sql2xml = new xml_sql2xml ();
* // The next one is only needed, if you need others than the default
* $ Sql2xml-> setEncoding ("ISO-8859-1", "UTF-8 ");
* $ Result = $ db-> query ("select * from bands ");
* $ Xmlstring = $ sql2xml-> getXML ($ result );
*
* Or
*
* Include_once ("DB. php ");
* Include_once ("XML/sql2xml. php ");
* $ Sql2xml = new xml_sql2xml ("mysql: // root @ localhost/xmltest ");
* $ Sql2xml-> Add ("select * from bands ");
* $ Xmlstring = $ sql2xml-> getXML ();
*
* More documentation and a tutorial/how-to can be found
* Http://php.chregu. TV /sql2xml
*
* @ Author Christian Stocker <chregu@bitflux.ch>
* @ Version $ Id: sql2xml. php, v 1.59 2001/11/13 10:54:02 chregu Exp $
* @ Package XML
*/
Class XML_sql2xml {
/**
* If joined-tables shoshould be output nested.
* Means, if you have joined two or more queries, the later
* Specified tables will be nested within the result of the former
* Table.
* Works at the moment only with mysql automagically. For other RDBMS
* You have to provide your table-relations by hand (see user_tableinfo)
*
* @ Var boolean
* @ See $ user_tableinfo, doSql2Xml (), doArray2Xml ();
*/
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
* @ Access private
*/
Var $ db = Null;
/**
* Options to be used in extended Classes (for example in sql2xml_ext ).
* They are passed with SetOptions as an array (arrary ("user_options" = array ());
* And can then be accessed with $ this-> user_options ["bla"] from your
* Extended classes for additional features.
* This array is not used in this base class, it's only for passing easy parameters
* To extended classes.
*
* @ Var array
*/
Var $ user_options = array ();
/**
* The DomDocument Object to be used in the whole class
*
* @ Var object DomDocument
* @ Access private
*/
Var $ xmldoc;
/**
* The Root of the domxml object
* I'm not sure, if we need this as a class variable ....
* Cocould be replaced by domxml_root ($ this-> xmldoc );
*
* @ Var object DomNode
* @ Access private
*/
Var $ xmlroot;
/**
* This array is used to give the structure of your database to the class.
* It's especially useful, if you don't use mysql, since other RDBMS
* Mysql are not able at the moment to provide the right information about
* Your database structure within the query. And if you have more than 2
* Tables joined in the SQL it's also not possible for mysql to find out
* Your real relations.
* The parameters are the same as in fieldInfo from the PEAR: DB and some
* Additional ones. Here they come:
* From PEAR: DB-> fieldinfo:
*
* $ TableInfo [$ I] ["table"]: the table, which field # $ I belongs.
* For some rdbms/comples queries and with arrays, it's impossible
* To find out to which table the field actually belongs. You can
* Specify it here more accurate. Or if you want, that one fields
* Belongs to another table, than it actually says (yes, there's
* Use for that, see the upcoming tutorial ...)
*
* $ TableInfo [$ I] ["name"]: the name of field # $ I. if you want another
* Name for the tag, than the query or your array provides, assign
* It here.
*
* Additional info
* $ TableInfo ["parent_key"] [$ table]: index of the parent key for $ table.
* This is the field, where the programm looks for changes, if this
* Field changes, it assumes, that we need a new "rowset" in
* Parent table.
*
* $ TableInfo ["parent_table"] [$ table]: name of the parent table for $ table.
*
* @ Var array
* @ Access private
*/
Var $ user_tableInfo = array ();
/**
* The encoding type, the input from the db has
*/
Var $ encoding_from = "ISO-8859-1 ";
/**
* The encoding type, the output in the xml shocould have
* (Note that domxml at the moment only support UTF-8, or at least it looks like)
*/
Var $ encoding_to = "gb2312 ";
Var $ tagname = "tagname ";
/**
* Constructor
* The Constructor can take a Pear: DB "data source name" (eg.
* "Mysql: // user: passwd @ localhost/dbname") and will then connect
* To the DB, or a PEAR: DB object link, if you already connected
* The db before.
"If you provide nothing as $ dsn, you only can later add stuff
* A pear: db-resultset or as an array. providing SQL-strings will
* Not work.
* The $ root param is used, if you want to provide another name for your
* Root-tag than "root". if you give an empty string (""), there will be no
* Root element created here, but only when you add a resultset/array/SQL-string.
* And the first tag of this result is used as the root tag.
*
* @ Param mixed $ dsn PEAR: DB "data source name" or object DB object
* @ Param string $ root the name of the xml-doc root element.
* @ Access public
*/
Function XML_sql2xml ($ dsn = Null, $ root = "root "){
// If it's a string, then it must be a dsn-identifier;
If (is_string ($ dsn ))
{
Include_once ("DB. php ");
$ This-> db = DB: Connect ($ dsn );
If (DB: isError ($ this-> db ))
{
Print "The given dsn for XML_sql2xml was not valid in file". _ FILE _. "at line". _ LINE _. "<br> \ n ";
Return new DB_Error ($ this-> db-> code, PEAR_ERROR_DIE );
}
}
Elseif (is_object ($ dsn) & 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 ")
{
$ This-> db = $ dsn;
}
$ This-> xmldoc = domxml_new_xmldoc ('1. 0 ');
// Oehm, seems not to work, unfortunately... does anybody know a solution?
$ This-> xmldoc-> encoding = $ this-> encoding_to;
If ($ root ){
$ This-> xmlroot = $ this-> xmldoc-> add_root ($ root );
// PHP 4.0.6 had $ root-> name as tagname, check for that here...
If (! Isset ($ this-> xmlroot-> {$ this-> tagname }))
{
$ This-> tagname = "name ";
}
}
}
/**
* General method for adding new resultsets to the xml-object
* Give a SQL-query-string, a pear: db_result object or an array
* Input parameter, and the method callthe appropriate method for this
* Input and adds this to $ this-> xmldoc
*
* @ Param string SQL-string, or object db_result, or array
* @ Param mixed additional parameters for the following funwing
* @ Access public
* @ See addResult (), addSql (), addArray (), addXmlFile ()
*/
Function add ($ resultset, $ params = Null)
{
// If string, then it's a query, a xml-file or a xml-string...
If (is_string ($ resultset )){
If (preg_match ("/\. xml $/", $ resultset )){
$ This-> AddXmlFile ($ resultset, $ params );
}
Elseif (preg_match ("/. * select. * from. */I", $ resultset )){
$ This-> AddSql ($ resultset );
}
Else {
$ This-> AddXmlString ($ resultset );
}
}
// If array, then it's an array...
Elseif (is_array ($ resultset )){
$ This-> AddArray ($ resultset );
}
If (get_class ($ resultset) = "db_result "){
$ This-> AddResult ($ resultset );
}
}
/**
* Adds the content of a xml-file to $ this-> xmldoc, on the same level
* As a normal resultset (mostly just below <root>)
*
* @ Param string filename
* @ Param mixed xpath either a string with the xpath expression or an array with "xpath" => xpath expression and "root" = tag/subtag/etc, which are the tags to be inserted before the result
* @ Access public
* @ See doXmlString2Xml ()
*/
Function addXmlFile ($ file, $ xpath = Null)
{
$ Fd = fopen ($ file, "r ");
$ Content = fread ($ fd, filesize ($ file ));
Fclose ($ fd );
$ This-> doXmlString2Xml ($ content, $ xpath );
}
/**
* Adds the content of a xml-string to $ this-> xmldoc, on the same level
* As a normal resultset (mostly just below <root>)
*
* @ Param string xml
* @ Param mixed xpath either a string with the xpath expression or an array with "xpath" => xpath expression and "root" = tag/subtag/etc, which are the tags to be inserted before the result
* @ Access public
* @ See doXmlString2Xml ()
*/