Project

General

Profile

Bug #23416 ยป adodb-511.diff

Administrator Admin, 2010-08-21 10:24

View differences:

adodb/adodb-php4.inc.php (revision 0)
<?php
/*
V5.11 5 May 2010 (c) 2000-2010 John Lim (jlim#natsoft.com). All rights reserved.
Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses,
the BSD license will take precedence.
Set tabs to 4.
*/
class ADODB_BASE_RS {
}
?>
adodb/toexport.inc.php (revision 0)
<?php
/**
* @version V4.93 10 Oct 2006 (c) 2000-2010 John Lim (jlim#natsoft.com). All rights reserved.
* Released under both BSD license and Lesser GPL library license.
* Whenever there is any discrepancy between the two licenses,
* the BSD license will take precedence.
*
* Code to export recordsets in several formats:
*
* AS VARIABLE
* $s = rs2csv($rs); # comma-separated values
* $s = rs2tab($rs); # tab delimited
*
* TO A FILE
* $f = fopen($path,'w');
* rs2csvfile($rs,$f);
* fclose($f);
*
* TO STDOUT
* rs2csvout($rs);
*/
// returns a recordset as a csv string
function rs2csv(&$rs,$addtitles=true)
{
return _adodb_export($rs,',',',',false,$addtitles);
}
// writes recordset to csv file
function rs2csvfile(&$rs,$fp,$addtitles=true)
{
_adodb_export($rs,',',',',$fp,$addtitles);
}
// write recordset as csv string to stdout
function rs2csvout(&$rs,$addtitles=true)
{
$fp = fopen('php://stdout','wb');
_adodb_export($rs,',',',',true,$addtitles);
fclose($fp);
}
function rs2tab(&$rs,$addtitles=true)
{
return _adodb_export($rs,"\t",',',false,$addtitles);
}
// to file pointer
function rs2tabfile(&$rs,$fp,$addtitles=true)
{
_adodb_export($rs,"\t",',',$fp,$addtitles);
}
// to stdout
function rs2tabout(&$rs,$addtitles=true)
{
$fp = fopen('php://stdout','wb');
_adodb_export($rs,"\t",' ',true,$addtitles);
if ($fp) fclose($fp);
}
function _adodb_export(&$rs,$sep,$sepreplace,$fp=false,$addtitles=true,$quote = '"',$escquote = '"',$replaceNewLine = ' ')
{
if (!$rs) return '';
//----------
// CONSTANTS
$NEWLINE = "\r\n";
$BUFLINES = 100;
$escquotequote = $escquote.$quote;
$s = '';
if ($addtitles) {
$fieldTypes = $rs->FieldTypesArray();
reset($fieldTypes);
$i = 0;
while(list(,$o) = each($fieldTypes)) {
$v = ($o) ? $o->name : 'Field'.($i++);
if ($escquote) $v = str_replace($quote,$escquotequote,$v);
$v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
$elements[] = $v;
}
$s .= implode($sep, $elements).$NEWLINE;
}
$hasNumIndex = isset($rs->fields[0]);
$line = 0;
$max = $rs->FieldCount();
while (!$rs->EOF) {
$elements = array();
$i = 0;
if ($hasNumIndex) {
for ($j=0; $j < $max; $j++) {
$v = $rs->fields[$j];
if (!is_object($v)) $v = trim($v);
else $v = 'Object';
if ($escquote) $v = str_replace($quote,$escquotequote,$v);
$v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
else $elements[] = $v;
}
} else { // ASSOCIATIVE ARRAY
foreach($rs->fields as $v) {
if ($escquote) $v = str_replace($quote,$escquotequote,trim($v));
$v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
else $elements[] = $v;
}
}
$s .= implode($sep, $elements).$NEWLINE;
$rs->MoveNext();
$line += 1;
if ($fp && ($line % $BUFLINES) == 0) {
if ($fp === true) echo $s;
else fwrite($fp,$s);
$s = '';
}
}
if ($fp) {
if ($fp === true) echo $s;
else fwrite($fp,$s);
$s = '';
}
return $s;
}
?>
adodb/adodb-xmlschema.inc.php (revision 0)
<?php
// Copyright (c) 2004 ars Cognita Inc., all rights reserved
/* ******************************************************************************
Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses,
the BSD license will take precedence.
*******************************************************************************/
/**
* xmlschema is a class that allows the user to quickly and easily
* build a database on any ADOdb-supported platform using a simple
* XML schema.
*
* Last Editor: $Author: jlim $
* @author Richard Tango-Lowy & Dan Cech
* @version $Revision: 1.12 $
*
* @package axmls
* @tutorial getting_started.pkg
*/
function _file_get_contents($file)
{
if (function_exists('file_get_contents')) return file_get_contents($file);
$f = fopen($file,'r');
if (!$f) return '';
$t = '';
while ($s = fread($f,100000)) $t .= $s;
fclose($f);
return $t;
}
/**
* Debug on or off
*/
if( !defined( 'XMLS_DEBUG' ) ) {
define( 'XMLS_DEBUG', FALSE );
}
/**
* Default prefix key
*/
if( !defined( 'XMLS_PREFIX' ) ) {
define( 'XMLS_PREFIX', '%%P' );
}
/**
* Maximum length allowed for object prefix
*/
if( !defined( 'XMLS_PREFIX_MAXLEN' ) ) {
define( 'XMLS_PREFIX_MAXLEN', 10 );
}
/**
* Execute SQL inline as it is generated
*/
if( !defined( 'XMLS_EXECUTE_INLINE' ) ) {
define( 'XMLS_EXECUTE_INLINE', FALSE );
}
/**
* Continue SQL Execution if an error occurs?
*/
if( !defined( 'XMLS_CONTINUE_ON_ERROR' ) ) {
define( 'XMLS_CONTINUE_ON_ERROR', FALSE );
}
/**
* Current Schema Version
*/
if( !defined( 'XMLS_SCHEMA_VERSION' ) ) {
define( 'XMLS_SCHEMA_VERSION', '0.2' );
}
/**
* Default Schema Version. Used for Schemas without an explicit version set.
*/
if( !defined( 'XMLS_DEFAULT_SCHEMA_VERSION' ) ) {
define( 'XMLS_DEFAULT_SCHEMA_VERSION', '0.1' );
}
/**
* Default Schema Version. Used for Schemas without an explicit version set.
*/
if( !defined( 'XMLS_DEFAULT_UPGRADE_METHOD' ) ) {
define( 'XMLS_DEFAULT_UPGRADE_METHOD', 'ALTER' );
}
/**
* Include the main ADODB library
*/
if( !defined( '_ADODB_LAYER' ) ) {
require( 'adodb.inc.php' );
require( 'adodb-datadict.inc.php' );
}
/**
* Abstract DB Object. This class provides basic methods for database objects, such
* as tables and indexes.
*
* @package axmls
* @access private
*/
class dbObject {
/**
* var object Parent
*/
var $parent;
/**
* var string current element
*/
var $currentElement;
/**
* NOP
*/
function dbObject( &$parent, $attributes = NULL ) {
$this->parent = $parent;
}
/**
* XML Callback to process start elements
*
* @access private
*/
function _tag_open( &$parser, $tag, $attributes ) {
}
/**
* XML Callback to process CDATA elements
*
* @access private
*/
function _tag_cdata( &$parser, $cdata ) {
}
/**
* XML Callback to process end elements
*
* @access private
*/
function _tag_close( &$parser, $tag ) {
}
function create(&$xmls) {
return array();
}
/**
* Destroys the object
*/
function destroy() {
unset( $this );
}
/**
* Checks whether the specified RDBMS is supported by the current
* database object or its ranking ancestor.
*
* @param string $platform RDBMS platform name (from ADODB platform list).
* @return boolean TRUE if RDBMS is supported; otherwise returns FALSE.
*/
function supportedPlatform( $platform = NULL ) {
return is_object( $this->parent ) ? $this->parent->supportedPlatform( $platform ) : TRUE;
}
/**
* Returns the prefix set by the ranking ancestor of the database object.
*
* @param string $name Prefix string.
* @return string Prefix.
*/
function prefix( $name = '' ) {
return is_object( $this->parent ) ? $this->parent->prefix( $name ) : $name;
}
/**
* Extracts a field ID from the specified field.
*
* @param string $field Field.
* @return string Field ID.
*/
function FieldID( $field ) {
return strtoupper( preg_replace( '/^`(.+)`$/', '$1', $field ) );
}
}
/**
* Creates a table object in ADOdb's datadict format
*
* This class stores information about a database table. As charactaristics
* of the table are loaded from the external source, methods and properties
* of this class are used to build up the table description in ADOdb's
* datadict format.
*
* @package axmls
* @access private
*/
class dbTable extends dbObject {
/**
* @var string Table name
*/
var $name;
/**
* @var array Field specifier: Meta-information about each field
*/
var $fields = array();
/**
* @var array List of table indexes.
*/
var $indexes = array();
/**
* @var array Table options: Table-level options
*/
var $opts = array();
/**
* @var string Field index: Keeps track of which field is currently being processed
*/
var $current_field;
/**
* @var boolean Mark table for destruction
* @access private
*/
var $drop_table;
/**
* @var boolean Mark field for destruction (not yet implemented)
* @access private
*/
var $drop_field = array();
/**
* Iniitializes a new table object.
*
* @param string $prefix DB Object prefix
* @param array $attributes Array of table attributes.
*/
function dbTable( &$parent, $attributes = NULL ) {
$this->parent = $parent;
$this->name = $this->prefix($attributes['NAME']);
}
/**
* XML Callback to process start elements. Elements currently
* processed are: INDEX, DROP, FIELD, KEY, NOTNULL, AUTOINCREMENT & DEFAULT.
*
* @access private
*/
function _tag_open( &$parser, $tag, $attributes ) {
$this->currentElement = strtoupper( $tag );
switch( $this->currentElement ) {
case 'INDEX':
if( !isset( $attributes['PLATFORM'] ) OR $this->supportedPlatform( $attributes['PLATFORM'] ) ) {
xml_set_object( $parser, $this->addIndex( $attributes ) );
}
break;
case 'DATA':
if( !isset( $attributes['PLATFORM'] ) OR $this->supportedPlatform( $attributes['PLATFORM'] ) ) {
xml_set_object( $parser, $this->addData( $attributes ) );
}
break;
case 'DROP':
$this->drop();
break;
case 'FIELD':
// Add a field
$fieldName = $attributes['NAME'];
$fieldType = $attributes['TYPE'];
$fieldSize = isset( $attributes['SIZE'] ) ? $attributes['SIZE'] : NULL;
$fieldOpts = isset( $attributes['OPTS'] ) ? $attributes['OPTS'] : NULL;
$this->addField( $fieldName, $fieldType, $fieldSize, $fieldOpts );
break;
case 'KEY':
case 'NOTNULL':
case 'AUTOINCREMENT':
// Add a field option
$this->addFieldOpt( $this->current_field, $this->currentElement );
break;
case 'DEFAULT':
// Add a field option to the table object
// Work around ADOdb datadict issue that misinterprets empty strings.
if( $attributes['VALUE'] == '' ) {
$attributes['VALUE'] = " '' ";
}
$this->addFieldOpt( $this->current_field, $this->currentElement, $attributes['VALUE'] );
break;
case 'DEFDATE':
case 'DEFTIMESTAMP':
// Add a field option to the table object
$this->addFieldOpt( $this->current_field, $this->currentElement );
break;
default:
// print_r( array( $tag, $attributes ) );
}
}
/**
* XML Callback to process CDATA elements
*
* @access private
*/
function _tag_cdata( &$parser, $cdata ) {
switch( $this->currentElement ) {
// Table constraint
case 'CONSTRAINT':
if( isset( $this->current_field ) ) {
$this->addFieldOpt( $this->current_field, $this->currentElement, $cdata );
} else {
$this->addTableOpt( $cdata );
}
break;
// Table option
case 'OPT':
$this->addTableOpt( $cdata );
break;
default:
}
}
/**
* XML Callback to process end elements
*
* @access private
*/
function _tag_close( &$parser, $tag ) {
$this->currentElement = '';
switch( strtoupper( $tag ) ) {
case 'TABLE':
$this->parent->addSQL( $this->create( $this->parent ) );
xml_set_object( $parser, $this->parent );
$this->destroy();
break;
case 'FIELD':
unset($this->current_field);
break;
}
}
/**
* Adds an index to a table object
*
* @param array $attributes Index attributes
* @return object dbIndex object
*/
function addIndex( $attributes ) {
$name = strtoupper( $attributes['NAME'] );
$this->indexes[$name] = new dbIndex( $this, $attributes );
return $this->indexes[$name];
}
/**
* Adds data to a table object
*
* @param array $attributes Data attributes
* @return object dbData object
*/
function addData( $attributes ) {
if( !isset( $this->data ) ) {
$this->data = new dbData( $this, $attributes );
}
return $this->data;
}
/**
* Adds a field to a table object
*
* $name is the name of the table to which the field should be added.
* $type is an ADODB datadict field type. The following field types
* are supported as of ADODB 3.40:
* - C: varchar
* - X: CLOB (character large object) or largest varchar size
* if CLOB is not supported
* - C2: Multibyte varchar
* - X2: Multibyte CLOB
* - B: BLOB (binary large object)
* - D: Date (some databases do not support this, and we return a datetime type)
* - T: Datetime or Timestamp
* - L: Integer field suitable for storing booleans (0 or 1)
* - I: Integer (mapped to I4)
* - I1: 1-byte integer
* - I2: 2-byte integer
* - I4: 4-byte integer
* - I8: 8-byte integer
* - F: Floating point number
* - N: Numeric or decimal number
*
* @param string $name Name of the table to which the field will be added.
* @param string $type ADODB datadict field type.
* @param string $size Field size
* @param array $opts Field options array
* @return array Field specifier array
*/
function addField( $name, $type, $size = NULL, $opts = NULL ) {
$field_id = $this->FieldID( $name );
// Set the field index so we know where we are
$this->current_field = $field_id;
// Set the field name (required)
$this->fields[$field_id]['NAME'] = $name;
// Set the field type (required)
$this->fields[$field_id]['TYPE'] = $type;
// Set the field size (optional)
if( isset( $size ) ) {
$this->fields[$field_id]['SIZE'] = $size;
}
// Set the field options
if( isset( $opts ) ) {
$this->fields[$field_id]['OPTS'][] = $opts;
}
}
/**
* Adds a field option to the current field specifier
*
* This method adds a field option allowed by the ADOdb datadict
* and appends it to the given field.
*
* @param string $field Field name
* @param string $opt ADOdb field option
* @param mixed $value Field option value
* @return array Field specifier array
*/
function addFieldOpt( $field, $opt, $value = NULL ) {
if( !isset( $value ) ) {
$this->fields[$this->FieldID( $field )]['OPTS'][] = $opt;
// Add the option and value
} else {
$this->fields[$this->FieldID( $field )]['OPTS'][] = array( $opt => $value );
}
}
/**
* Adds an option to the table
*
* This method takes a comma-separated list of table-level options
* and appends them to the table object.
*
* @param string $opt Table option
* @return array Options
*/
function addTableOpt( $opt ) {
if(isset($this->currentPlatform)) {
$this->opts[$this->parent->db->databaseType] = $opt;
}
return $this->opts;
}
/**
* Generates the SQL that will create the table in the database
*
* @param object $xmls adoSchema object
* @return array Array containing table creation SQL
*/
function create( &$xmls ) {
$sql = array();
// drop any existing indexes
if( is_array( $legacy_indexes = $xmls->dict->MetaIndexes( $this->name ) ) ) {
foreach( $legacy_indexes as $index => $index_details ) {
$sql[] = $xmls->dict->DropIndexSQL( $index, $this->name );
}
}
// remove fields to be dropped from table object
foreach( $this->drop_field as $field ) {
unset( $this->fields[$field] );
}
// if table exists
if( is_array( $legacy_fields = $xmls->dict->MetaColumns( $this->name ) ) ) {
// drop table
if( $this->drop_table ) {
$sql[] = $xmls->dict->DropTableSQL( $this->name );
return $sql;
}
// drop any existing fields not in schema
foreach( $legacy_fields as $field_id => $field ) {
if( !isset( $this->fields[$field_id] ) ) {
$sql[] = $xmls->dict->DropColumnSQL( $this->name, '`'.$field->name.'`' );
}
}
// if table doesn't exist
} else {
if( $this->drop_table ) {
return $sql;
}
$legacy_fields = array();
}
// Loop through the field specifier array, building the associative array for the field options
$fldarray = array();
foreach( $this->fields as $field_id => $finfo ) {
// Set an empty size if it isn't supplied
if( !isset( $finfo['SIZE'] ) ) {
$finfo['SIZE'] = '';
}
// Initialize the field array with the type and size
$fldarray[$field_id] = array(
'NAME' => $finfo['NAME'],
'TYPE' => $finfo['TYPE'],
'SIZE' => $finfo['SIZE']
);
// Loop through the options array and add the field options.
if( isset( $finfo['OPTS'] ) ) {
foreach( $finfo['OPTS'] as $opt ) {
// Option has an argument.
if( is_array( $opt ) ) {
$key = key( $opt );
$value = $opt[key( $opt )];
@$fldarray[$field_id][$key] .= $value;
// Option doesn't have arguments
} else {
$fldarray[$field_id][$opt] = $opt;
}
}
}
}
if( empty( $legacy_fields ) ) {
// Create the new table
$sql[] = $xmls->dict->CreateTableSQL( $this->name, $fldarray, $this->opts );
logMsg( end( $sql ), 'Generated CreateTableSQL' );
} else {
// Upgrade an existing table
logMsg( "Upgrading {$this->name} using '{$xmls->upgrade}'" );
switch( $xmls->upgrade ) {
// Use ChangeTableSQL
case 'ALTER':
logMsg( 'Generated ChangeTableSQL (ALTERing table)' );
$sql[] = $xmls->dict->ChangeTableSQL( $this->name, $fldarray, $this->opts );
break;
case 'REPLACE':
logMsg( 'Doing upgrade REPLACE (testing)' );
$sql[] = $xmls->dict->DropTableSQL( $this->name );
$sql[] = $xmls->dict->CreateTableSQL( $this->name, $fldarray, $this->opts );
break;
// ignore table
default:
return array();
}
}
foreach( $this->indexes as $index ) {
$sql[] = $index->create( $xmls );
}
if( isset( $this->data ) ) {
$sql[] = $this->data->create( $xmls );
}
return $sql;
}
/**
* Marks a field or table for destruction
*/
function drop() {
if( isset( $this->current_field ) ) {
// Drop the current field
logMsg( "Dropping field '{$this->current_field}' from table '{$this->name}'" );
// $this->drop_field[$this->current_field] = $xmls->dict->DropColumnSQL( $this->name, $this->current_field );
$this->drop_field[$this->current_field] = $this->current_field;
} else {
// Drop the current table
logMsg( "Dropping table '{$this->name}'" );
// $this->drop_table = $xmls->dict->DropTableSQL( $this->name );
$this->drop_table = TRUE;
}
}
}
/**
* Creates an index object in ADOdb's datadict format
*
* This class stores information about a database index. As charactaristics
* of the index are loaded from the external source, methods and properties
* of this class are used to build up the index description in ADOdb's
* datadict format.
*
* @package axmls
* @access private
*/
class dbIndex extends dbObject {
/**
* @var string Index name
*/
var $name;
/**
* @var array Index options: Index-level options
*/
var $opts = array();
/**
* @var array Indexed fields: Table columns included in this index
*/
var $columns = array();
/**
* @var boolean Mark index for destruction
* @access private
*/
var $drop = FALSE;
/**
* Initializes the new dbIndex object.
*
* @param object $parent Parent object
* @param array $attributes Attributes
*
* @internal
*/
function dbIndex( &$parent, $attributes = NULL ) {
$this->parent = $parent;
$this->name = $this->prefix ($attributes['NAME']);
}
/**
* XML Callback to process start elements
*
* Processes XML opening tags.
* Elements currently processed are: DROP, CLUSTERED, BITMAP, UNIQUE, FULLTEXT & HASH.
*
* @access private
*/
function _tag_open( &$parser, $tag, $attributes ) {
$this->currentElement = strtoupper( $tag );
switch( $this->currentElement ) {
case 'DROP':
$this->drop();
break;
case 'CLUSTERED':
case 'BITMAP':
case 'UNIQUE':
case 'FULLTEXT':
case 'HASH':
// Add index Option
$this->addIndexOpt( $this->currentElement );
break;
default:
// print_r( array( $tag, $attributes ) );
}
}
/**
* XML Callback to process CDATA elements
*
* Processes XML cdata.
*
* @access private
*/
function _tag_cdata( &$parser, $cdata ) {
switch( $this->currentElement ) {
// Index field name
case 'COL':
$this->addField( $cdata );
break;
default:
}
}
/**
* XML Callback to process end elements
*
* @access private
*/
function _tag_close( &$parser, $tag ) {
$this->currentElement = '';
switch( strtoupper( $tag ) ) {
case 'INDEX':
xml_set_object( $parser, $this->parent );
break;
}
}
/**
* Adds a field to the index
*
* @param string $name Field name
* @return string Field list
*/
function addField( $name ) {
$this->columns[$this->FieldID( $name )] = $name;
// Return the field list
return $this->columns;
}
/**
* Adds options to the index
*
* @param string $opt Comma-separated list of index options.
* @return string Option list
*/
function addIndexOpt( $opt ) {
$this->opts[] = $opt;
// Return the options list
return $this->opts;
}
/**
* Generates the SQL that will create the index in the database
*
* @param object $xmls adoSchema object
* @return array Array containing index creation SQL
*/
function create( &$xmls ) {
if( $this->drop ) {
return NULL;
}
// eliminate any columns that aren't in the table
foreach( $this->columns as $id => $col ) {
if( !isset( $this->parent->fields[$id] ) ) {
unset( $this->columns[$id] );
}
}
return $xmls->dict->CreateIndexSQL( $this->name, $this->parent->name, $this->columns, $this->opts );
}
/**
* Marks an index for destruction
*/
function drop() {
$this->drop = TRUE;
}
}
/**
* Creates a data object in ADOdb's datadict format
*
* This class stores information about table data.
*
* @package axmls
* @access private
*/
class dbData extends dbObject {
var $data = array();
var $row;
/**
* Initializes the new dbIndex object.
*
* @param object $parent Parent object
* @param array $attributes Attributes
*
* @internal
*/
function dbData( &$parent, $attributes = NULL ) {
$this->parent = $parent;
}
/**
* XML Callback to process start elements
*
* Processes XML opening tags.
* Elements currently processed are: DROP, CLUSTERED, BITMAP, UNIQUE, FULLTEXT & HASH.
*
* @access private
*/
function _tag_open( &$parser, $tag, $attributes ) {
$this->currentElement = strtoupper( $tag );
switch( $this->currentElement ) {
case 'ROW':
$this->row = count( $this->data );
$this->data[$this->row] = array();
break;
case 'F':
$this->addField($attributes);
default:
// print_r( array( $tag, $attributes ) );
}
}
/**
* XML Callback to process CDATA elements
*
* Processes XML cdata.
*
* @access private
*/
function _tag_cdata( &$parser, $cdata ) {
switch( $this->currentElement ) {
// Index field name
case 'F':
$this->addData( $cdata );
break;
default:
}
}
/**
* XML Callback to process end elements
*
* @access private
*/
function _tag_close( &$parser, $tag ) {
$this->currentElement = '';
switch( strtoupper( $tag ) ) {
case 'DATA':
xml_set_object( $parser, $this->parent );
break;
}
}
/**
* Adds a field to the index
*
* @param string $name Field name
* @return string Field list
*/
function addField( $attributes ) {
if( isset( $attributes['NAME'] ) ) {
$name = $attributes['NAME'];
} else {
$name = count($this->data[$this->row]);
}
// Set the field index so we know where we are
$this->current_field = $this->FieldID( $name );
}
/**
* Adds options to the index
*
* @param string $opt Comma-separated list of index options.
* @return string Option list
*/
function addData( $cdata ) {
if( !isset( $this->data[$this->row] ) ) {
$this->data[$this->row] = array();
}
if( !isset( $this->data[$this->row][$this->current_field] ) ) {
$this->data[$this->row][$this->current_field] = '';
}
$this->data[$this->row][$this->current_field] .= $cdata;
}
/**
* Generates the SQL that will create the index in the database
*
* @param object $xmls adoSchema object
* @return array Array containing index creation SQL
*/
function create( &$xmls ) {
$table = $xmls->dict->TableName($this->parent->name);
$table_field_count = count($this->parent->fields);
$sql = array();
// eliminate any columns that aren't in the table
foreach( $this->data as $row ) {
$table_fields = $this->parent->fields;
$fields = array();
foreach( $row as $field_id => $field_data ) {
if( !array_key_exists( $field_id, $table_fields ) ) {
if( is_numeric( $field_id ) ) {
$field_id = reset( array_keys( $table_fields ) );
} else {
continue;
}
}
$name = $table_fields[$field_id]['NAME'];
switch( $table_fields[$field_id]['TYPE'] ) {
case 'C':
case 'C2':
case 'X':
case 'X2':
$fields[$name] = $xmls->db->qstr( $field_data );
break;
case 'I':
case 'I1':
case 'I2':
case 'I4':
case 'I8':
$fields[$name] = intval($field_data);
break;
default:
$fields[$name] = $field_data;
}
unset($table_fields[$field_id]);
}
// check that at least 1 column is specified
if( empty( $fields ) ) {
continue;
}
// check that no required columns are missing
if( count( $fields ) < $table_field_count ) {
foreach( $table_fields as $field ) {
if (isset( $field['OPTS'] ))
if( ( in_array( 'NOTNULL', $field['OPTS'] ) || in_array( 'KEY', $field['OPTS'] ) ) && !in_array( 'AUTOINCREMENT', $field['OPTS'] ) ) {
continue(2);
}
}
}
$sql[] = 'INSERT INTO '. $table .' ('. implode( ',', array_keys( $fields ) ) .') VALUES ('. implode( ',', $fields ) .')';
}
return $sql;
}
}
/**
* Creates the SQL to execute a list of provided SQL queries
*
* @package axmls
* @access private
*/
class dbQuerySet extends dbObject {
/**
* @var array List of SQL queries
*/
var $queries = array();
/**
* @var string String used to build of a query line by line
*/
var $query;
/**
* @var string Query prefix key
*/
var $prefixKey = '';
/**
* @var boolean Auto prefix enable (TRUE)
*/
var $prefixMethod = 'AUTO';
/**
* Initializes the query set.
*
* @param object $parent Parent object
* @param array $attributes Attributes
*/
function dbQuerySet( &$parent, $attributes = NULL ) {
$this->parent = $parent;
// Overrides the manual prefix key
if( isset( $attributes['KEY'] ) ) {
$this->prefixKey = $attributes['KEY'];
}
$prefixMethod = isset( $attributes['PREFIXMETHOD'] ) ? strtoupper( trim( $attributes['PREFIXMETHOD'] ) ) : '';
// Enables or disables automatic prefix prepending
switch( $prefixMethod ) {
case 'AUTO':
$this->prefixMethod = 'AUTO';
break;
case 'MANUAL':
$this->prefixMethod = 'MANUAL';
break;
case 'NONE':
$this->prefixMethod = 'NONE';
break;
}
}
/**
* XML Callback to process start elements. Elements currently
* processed are: QUERY.
*
* @access private
*/
function _tag_open( &$parser, $tag, $attributes ) {
$this->currentElement = strtoupper( $tag );
switch( $this->currentElement ) {
case 'QUERY':
// Create a new query in a SQL queryset.
// Ignore this query set if a platform is specified and it's different than the
// current connection platform.
if( !isset( $attributes['PLATFORM'] ) OR $this->supportedPlatform( $attributes['PLATFORM'] ) ) {
$this->newQuery();
} else {
$this->discardQuery();
}
break;
default:
// print_r( array( $tag, $attributes ) );
}
}
/**
* XML Callback to process CDATA elements
*/
function _tag_cdata( &$parser, $cdata ) {
switch( $this->currentElement ) {
// Line of queryset SQL data
case 'QUERY':
$this->buildQuery( $cdata );
break;
default:
}
}
/**
* XML Callback to process end elements
*
* @access private
*/
function _tag_close( &$parser, $tag ) {
$this->currentElement = '';
switch( strtoupper( $tag ) ) {
case 'QUERY':
// Add the finished query to the open query set.
$this->addQuery();
break;
case 'SQL':
$this->parent->addSQL( $this->create( $this->parent ) );
xml_set_object( $parser, $this->parent );
$this->destroy();
break;
default:
}
}
/**
* Re-initializes the query.
*
* @return boolean TRUE
*/
function newQuery() {
$this->query = '';
return TRUE;
}
/**
* Discards the existing query.
*
* @return boolean TRUE
*/
function discardQuery() {
unset( $this->query );
return TRUE;
}
/**
* Appends a line to a query that is being built line by line
*
* @param string $data Line of SQL data or NULL to initialize a new query
* @return string SQL query string.
*/
function buildQuery( $sql = NULL ) {
if( !isset( $this->query ) OR empty( $sql ) ) {
return FALSE;
}
$this->query .= $sql;
return $this->query;
}
/**
* Adds a completed query to the query list
*
* @return string SQL of added query
*/
function addQuery() {
if( !isset( $this->query ) ) {
return FALSE;
}
$this->queries[] = $return = trim($this->query);
unset( $this->query );
return $return;
}
/**
* Creates and returns the current query set
*
* @param object $xmls adoSchema object
* @return array Query set
*/
function create( &$xmls ) {
foreach( $this->queries as $id => $query ) {
switch( $this->prefixMethod ) {
case 'AUTO':
// Enable auto prefix replacement
// Process object prefix.
// Evaluate SQL statements to prepend prefix to objects
$query = $this->prefixQuery( '/^\s*((?is)INSERT\s+(INTO\s+)?)((\w+\s*,?\s*)+)(\s.*$)/', $query, $xmls->objectPrefix );
$query = $this->prefixQuery( '/^\s*((?is)UPDATE\s+(FROM\s+)?)((\w+\s*,?\s*)+)(\s.*$)/', $query, $xmls->objectPrefix );
$query = $this->prefixQuery( '/^\s*((?is)DELETE\s+(FROM\s+)?)((\w+\s*,?\s*)+)(\s.*$)/', $query, $xmls->objectPrefix );
// SELECT statements aren't working yet
#$data = preg_replace( '/(?ias)(^\s*SELECT\s+.*\s+FROM)\s+(\W\s*,?\s*)+((?i)\s+WHERE.*$)/', "\1 $prefix\2 \3", $data );
case 'MANUAL':
// If prefixKey is set and has a value then we use it to override the default constant XMLS_PREFIX.
// If prefixKey is not set, we use the default constant XMLS_PREFIX
if( isset( $this->prefixKey ) AND( $this->prefixKey !== '' ) ) {
// Enable prefix override
$query = str_replace( $this->prefixKey, $xmls->objectPrefix, $query );
} else {
// Use default replacement
$query = str_replace( XMLS_PREFIX , $xmls->objectPrefix, $query );
}
}
$this->queries[$id] = trim( $query );
}
// Return the query set array
return $this->queries;
}
/**
* Rebuilds the query with the prefix attached to any objects
*
* @param string $regex Regex used to add prefix
* @param string $query SQL query string
* @param string $prefix Prefix to be appended to tables, indices, etc.
* @return string Prefixed SQL query string.
*/
function prefixQuery( $regex, $query, $prefix = NULL ) {
if( !isset( $prefix ) ) {
return $query;
}
if( preg_match( $regex, $query, $match ) ) {
$preamble = $match[1];
$postamble = $match[5];
$objectList = explode( ',', $match[3] );
// $prefix = $prefix . '_';
$prefixedList = '';
foreach( $objectList as $object ) {
if( $prefixedList !== '' ) {
$prefixedList .= ', ';
}
$prefixedList .= $prefix . trim( $object );
}
$query = $preamble . ' ' . $prefixedList . ' ' . $postamble;
}
return $query;
}
}
/**
* Loads and parses an XML file, creating an array of "ready-to-run" SQL statements
*
* This class is used to load and parse the XML file, to create an array of SQL statements
* that can be used to build a database, and to build the database using the SQL array.
*
* @tutorial getting_started.pkg
*
* @author Richard Tango-Lowy & Dan Cech
* @version $Revision: 1.12 $
*
* @package axmls
*/
class adoSchema {
/**
* @var array Array containing SQL queries to generate all objects
* @access private
*/
var $sqlArray;
/**
* @var object ADOdb connection object
* @access private
*/
var $db;
/**
* @var object ADOdb Data Dictionary
* @access private
*/
var $dict;
/**
* @var string Current XML element
* @access private
*/
var $currentElement = '';
/**
* @var string If set (to 'ALTER' or 'REPLACE'), upgrade an existing database
* @access private
*/
var $upgrade = '';
/**
* @var string Optional object prefix
* @access private
*/
var $objectPrefix = '';
/**
* @var long Original Magic Quotes Runtime value
* @access private
*/
var $mgq;
/**
* @var long System debug
* @access private
*/
var $debug;
/**
* @var string Regular expression to find schema version
* @access private
*/
var $versionRegex = '/<schema.*?( version="([^"]*)")?.*?>/';
/**
* @var string Current schema version
* @access private
*/
var $schemaVersion;
/**
* @var int Success of last Schema execution
*/
var $success;
/**
* @var bool Execute SQL inline as it is generated
*/
var $executeInline;
/**
* @var bool Continue SQL execution if errors occur
*/
var $continueOnError;
/**
* Creates an adoSchema object
*
* Creating an adoSchema object is the first step in processing an XML schema.
* The only parameter is an ADOdb database connection object, which must already
* have been created.
*
* @param object $db ADOdb database connection object.
*/
function adoSchema( $db ) {
// Initialize the environment
$this->mgq = get_magic_quotes_runtime();
ini_set("magic_quotes_runtime", 0);
#set_magic_quotes_runtime(0);
$this->db = $db;
$this->debug = $this->db->debug;
$this->dict = NewDataDictionary( $this->db );
$this->sqlArray = array();
$this->schemaVersion = XMLS_SCHEMA_VERSION;
$this->executeInline( XMLS_EXECUTE_INLINE );
$this->continueOnError( XMLS_CONTINUE_ON_ERROR );
$this->setUpgradeMethod();
}
/**
* Sets the method to be used for upgrading an existing database
*
... This diff was truncated because it exceeds the maximum size that can be displayed.
    (1-1/1)