00001 <?php
00002
00016
00017
00018
00019 import('xml.XMLParser');
00020
00021 class DBDataXMLParser {
00022
00024 var $parser;
00025
00027 var $dbconn;
00028
00030 var $sql;
00031
00035 function DBDataXMLParser() {
00036 $this->parser = &new XMLParser();
00037 $this->sql = array();
00038 }
00039
00045 function setDBConn(&$dbconn) {
00046 $this->dbconn = &$dbconn;
00047 }
00048
00054 function parseData($file) {
00055 $this->sql = array();
00056 $tree = $this->parser->parse($file);
00057 if ($tree !== false) {
00058 foreach ($tree->getChildren() as $table) {
00059 if ($table->getName() == 'table') {
00060 $fieldDefaultValues = array();
00061
00062
00063 foreach ($table->getChildren() as $row) {
00064 if ($row->getName() == 'field_default') {
00065
00066 $fieldName = $row->getAttribute('name');
00067 $value = $row->getValue();
00068 if ($value === null || $row->getAttribute('null') == 1) {
00069 $value = 'NULL';
00070 } else if (!is_numeric($value)) {
00071 $value = $this->quoteString($value);
00072 }
00073 $fieldDefaultValues[$fieldName] = $value;
00074
00075 } else if ($row->getName() == 'row') {
00076
00077 $fieldValues = array();
00078
00079 foreach ($row->getChildren() as $field) {
00080
00081 $fieldName = $field->getAttribute('name');
00082 $value = $field->getValue();
00083 if ($value === null || $field->getAttribute('null') == 1) {
00084 $value = 'NULL';
00085 } else if (!is_numeric($value)) {
00086 $value = $this->quoteString($value);
00087 }
00088 $fieldValues[$fieldName] = $value;
00089 }
00090
00091 $fieldValues = array_merge($fieldDefaultValues, $fieldValues);
00092
00093 if (count($fieldValues) > 0) {
00094 $this->sql[] = sprintf(
00095 'INSERT INTO %s (%s) VALUES (%s)',
00096 $table->getAttribute('name'),
00097 join(', ', array_keys($fieldValues)),
00098 join(', ', array_values($fieldValues))
00099 );
00100 }
00101 }
00102 }
00103
00104 } else if ($table->getName() == 'sql') {
00105
00106 foreach ($table->getChildren() as $query) {
00107
00108 if ($query->getName() == 'drop') {
00109 if (!isset($dbdict)) {
00110 $dbdict = @NewDataDictionary($this->dbconn);
00111 }
00112 $table = $query->getAttribute('table');
00113 $column = $query->getAttribute('column');
00114 if ($column) {
00115
00116 $this->sql[] = $dbdict->DropColumnSql($table, $column);
00117 } else {
00118 $this->sql[] = $dbdict->DropTableSQL($table);
00119 }
00120
00121 } else if ($query->getName() == 'rename') {
00122 if (!isset($dbdict)) {
00123 $dbdict = @NewDataDictionary($this->dbconn);
00124 }
00125 $table = $query->getAttribute('table');
00126 $column = $query->getAttribute('column');
00127 $to = $query->getAttribute('to');
00128 if ($column) {
00129 $columns = &$this->dbconn->MetaColumns($table, true);
00130 $colId = strtoupper($column);
00131 $flds = '';
00132 if (isset($columns[$colId])) {
00133 $col = $columns[$colId];
00134 $fld = array('NAME' => $col->name, 'TYPE' => $dbdict->MetaType($col), 'SIZE' => $col->max_length);
00135 if ($col->primary_key) $fld['KEY'] = 'KEY';
00136 if ($col->auto_increment) $fld['AUTOINCREMENT'] = 'AUTOINCREMENT';
00137 if ($col->not_null) $fld['NOTNULL'] = 'NOTNULL';
00138 if ($col->has_default) $fld['DEFAULT'] = $col->default_value;
00139 $flds = array($colId => $fld);
00140 }
00141 $this->sql[] = $dbdict->RenameColumnSQL($table, $column, $to, $flds);
00142 } else {
00143 $this->sql[] = $dbdict->RenameTableSQL($table, $to);
00144 }
00145 } else {
00146 $driver = $query->getAttribute('driver');
00147 if (empty($driver) || $this->dbconn->databaseType === $driver) {
00148 $this->sql[] = $query->getValue();
00149 }
00150 }
00151 }
00152 }
00153 }
00154 }
00155 return $this->sql;
00156 }
00157
00163 function executeData($continueOnError = false) {
00164 $this->errorMsg = null;
00165 $dbconn = $this->dbconn == null ? DBConnection::getConn() : $this->dbconn;
00166 foreach ($this->sql as $stmt) {
00167 $dbconn->execute($stmt);
00168 if (!$continueOnError && $dbconn->errorNo() != 0) {
00169 return false;
00170 }
00171 }
00172 return true;
00173 }
00174
00179 function extractData() {
00180 }
00181
00186 function getSQL() {
00187 return $this->sql;
00188 }
00189
00195 function quoteString($str) {
00196 return '\'' . str_replace('\'', '\\\'', str_replace('\\', '\\\\', $str)) . '\'';
00197 }
00198
00202 function destroy() {
00203 $this->parser->destroy();
00204 unset($this);
00205 }
00206 }
00207
00208 ?>