Пример #1
0
 function testToAndFromXML()
 {
     $first_string = $this->db->toXML();
     $db_obj =& DB_Table_Database::fromXML($first_string, $this->conn);
     $second_string = $db_obj->toXML();
     $this->assertEquals($second_string, $first_string);
 }
Пример #2
0
$PersonPhone = new DB_Table($conn, 'PersonPhone');
$PersonPhone->col['PersonID'] = array('type' => 'integer', 'default' => 75);
$PersonPhone->col['PhoneID'] = array('type' => 'integer', 'require' => true);
$PersonPhone->idx['PersonID'] = array('cols' => 'PersonID', 'type' => 'normal');
$PersonPhone->idx['PhoneID'] = array('cols' => 'PhoneID', 'type' => 'normal');
*/
require_once dirname(__FILE__) . '/PersonPhone_Table.php';
$PersonPhone = new PersonPhone_Table($conn, 'PersonPhone');
$Street = new DB_Table($conn, 'Street');
$Street->col['Street'] = array('type' => 'char', 'size' => 64);
$Street->col['City'] = array('type' => 'char', 'size' => 64);
$Street->col['StateAbb'] = array('type' => 'char', 'size' => 2);
$Street->col['Sunny'] = array('type' => 'boolean');
$Street->idx['Street'] = array('cols' => array('Street', 'City', 'StateAbb'), 'type' => 'primary');
// Instantiate new DB_Table_Database object
$db = new DB_Table_Database($conn, $db_name);
// Add all tables to it
$db->addTable($Person);
$db->addTable($Address);
$db->addTable($Phone);
$db->addTable($PersonAddress);
$db->addTable($PersonPhone);
$db->addTable($Street);
// Add references to DB_Table_Database object
$db->addRef('PersonAddress', 'PersonID2', 'Person', null, 'cascade', 'cascade');
$db->addRef('PersonAddress', 'AddressID', 'Address', null, 'cascade', 'cascade');
$db->addRef('PersonPhone', 'PersonID', 'Person', null, 'cascade', 'cascade');
$db->addRef('PersonPhone', 'PhoneID', 'Phone', null, 'cascade', 'cascade');
$db->addRef('Address', array('Street', 'City', 'StateAbb'), 'Street', array('Street', 'City', 'StateAbb'), 'cascade', 'cascade');
// Add links PersonPhone and PersonAddress
$db->addAllLinks();
Пример #3
0
 /**
  * Returns a DB_Table_Database object constructed from an XML string
  *
  * Uses the MDB2 XML schema for a database element, including a new
  * syntax for foreign key indices. 
  *
  * NOTE: This function requires PHP 5. It throws an error if used
  * with PHP 4. 
  *
  * @param  string XML string representation
  * @return object DB_Table_Database object on success (PEAR_Error on failure)
  *
  * @throws PEAR_Error if:
  *    - PHP version is not >= 5.0.0 (...DATABASE_ERR_PHP_VERSION )
  *    - Parsing by simpleXML fails (...DATABASE_ERR_XML_PARSE )
  *
  * @access public
  */
 function fromXML($xml_string, $conn)
 {
     // Check PHP version. Throw error if not >= PHP 5.0.0
     $version = phpversion();
     if (version_compare($version, '5.0.0', "<")) {
         return $this->throwError(DB_TABLE_DATABASE_ERR_PHP_VERSION, $version);
     }
     $xml = simplexml_load_string($xml_string);
     if ($xml == false) {
         return $this->throwError(DB_TABLE_DATABASE_ERR_XML_PARSE);
     }
     // Instantiate database object
     $database_name = (string) $xml->name;
     $database_obj = new DB_Table_Database($conn, $database_name);
     // Create array of foreign key references
     $ref = array();
     // Loop over tables
     foreach ($xml->table as $table) {
         $table_name = (string) $table->name;
         // Instantiate table object
         $table_obj = new DB_Table($conn, $table_name);
         // Add columns to table object
         $declaration = $table->declaration;
         foreach ($declaration->field as $field) {
             $col_name = (string) $field->name;
             $type = (string) $field->type;
             $def = array('type' => $type);
             if (isset($field->length)) {
                 $def['size'] = (int) $field->length;
             }
             if (isset($field->notnull)) {
                 if ($field->notnull) {
                     $def['require'] = true;
                 } else {
                     $def['require'] = false;
                 }
             }
             if (isset($field->default)) {
                 $def['default'] = $field->default;
             }
             if (isset($field->autoincrement)) {
                 if (is_null($table_obj->auto_inc_col)) {
                     $table_obj->auto_inc_col = $col_name;
                 } else {
                     return $this->throwError(DB_TABLE_DATABASE_ERR_XML_MULT_AUTO_INC);
                 }
             }
             $table_obj->col[$col_name] = $def;
         }
         // Add indices
         foreach ($declaration->index as $index) {
             if (isset($index->name)) {
                 $name = (string) $index->name;
             } else {
                 $name = null;
             }
             $def = array();
             if (isset($index->primary)) {
                 $def['type'] = 'primary';
             } elseif (isset($index->unique)) {
                 $def['type'] = 'unique';
             } else {
                 $def['type'] = 'normal';
             }
             foreach ($index->field as $field) {
                 $def['cols'][] = (string) $field;
             }
             if ($name) {
                 $table_obj->idx[$name] = $def;
             } else {
                 $table_obj->idx[] = $def;
             }
         }
         // Add table object to database object
         $database_obj->addTable($table_obj);
         // Foreign key references
         foreach ($declaration->foreign as $foreign) {
             if (isset($foreign->name)) {
                 $name = (string) $foreign->name;
             } else {
                 $name = null;
             }
             $fkey = array();
             foreach ($foreign->field as $field) {
                 $fkey[] = (string) $field;
             }
             if (count($fkey) == 1) {
                 $fkey = $fkey[0];
             }
             $rtable = (string) $foreign->references->table;
             if (isset($foreign->references->field)) {
                 $rkey = array();
                 foreach ($foreign->references->field as $field) {
                     $rkey[] = (string) $field;
                 }
                 if (count($rkey) == 1) {
                     $rkey = $rkey[0];
                 }
             } else {
                 $rkey = null;
             }
             if (isset($foreign->ondelete)) {
                 $on_delete = (string) $foreign->ondelete;
             } else {
                 $on_delete = null;
             }
             if (isset($foreign->onupdate)) {
                 $on_update = (string) $foreign->onupdate;
             } else {
                 $on_update = null;
             }
             // Add reference definition to $ref array
             $def = array();
             $def['fkey'] = $fkey;
             $def['rkey'] = $rkey;
             $def['on_delete'] = $on_delete;
             $def['on_update'] = $on_update;
             if (!isset($ref[$table_name])) {
                 $ref[$table_name] = array();
             }
             $ref[$table_name][$rtable] = $def;
         }
         // Release variable $table_obj to refer to another table
         unset($table_obj);
     }
     // Add all references to database object
     foreach ($ref as $ftable => $list) {
         foreach ($list as $rtable => $def) {
             $fkey = $def['fkey'];
             $rkey = $def['rkey'];
             $on_delete = $def['on_delete'];
             $on_update = $def['on_update'];
             $database_obj->addRef($ftable, $fkey, $rtable, $rkey, $on_delete, $on_update);
         }
     }
     return $database_obj;
 }