ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:dictionary:structure:sample_usage

Sample Usage

This page shows the usage of the metaObjectStructure in various situations.


Complex Example

The following complex example shows the additional functionality of updating indexes, as well as chaining methods

$dict = NewDataDictionary($db);
 
$t = new metaObjectStructure($dict,'test');
$t->addAttribute('ENGINE INNODB','mysql');
$t->addColumnObject('COL1','I');
$t->addColumnObject('COL1','I')
                   ->addAttribute('NOTNULL');
 
$c = $t->addColumnObject('COL2','C(64)');
$c->addAttribute('NOTNULL');
 
$i = $t->addIndexObject('CIDX');
$ii = $i->addIndexItemObject('COL1');
 
$t->addColumnObject('COL1','C(32)')
                ->addAttribute('NOTNULL')
                ->addAttribute(array('DEFAULT'=>'abc'));
 
$t->addColumnObject('COL2','I')
               ->addAttribute('NOTNULL')
               ->addAttribute('PRIMARY')
               ->addAttribute('AUTO');
$t->addColumnObject('COL3','N(12.2)');
$t->addColumnObject('COL4','C(64)')
              ->addAttribute(array('CHARACTER SET'=>'"ascii"'))
              ->addAttribute(array('COLLATE'=>'"latin1_swedish_ci"'),'mysql');
 
$t->addColumnObject('COL5',"ENUM('cats','dogs','fish')");
$t->addColumnObject('COL6','T')
              ->addAttribute('DEFTIMESTAMP');
 
$t->addColumnObject('COL7','D')
              ->addAttribute('DEFDATE');
 
$t->addIndexObject('COL4-INDEX')
              ->addIndexItem('COL4')
              ->addAttribute('ASC');
$t->addIndexObject('COL4-INDEX')
              ->addIndexItem('COL5');
 
$sql = $dict->createTableSql($tabledef);
 
print_r($sql);

Returns:

Array
(
[0] => CREATE TABLE test (
        COL1 C(32)  NOT NULL  DEFAULT 'abc',
        COL2 I  NOT NULL    AUTO-INCREMENT,
        COL3 N(12.2),
        COL4 C(64)  CHARACTER SET "ascii"  COLLATE "latin1_swedish_ci",
        COL5 ENUM('cats','dogs','fish'),
        COL6 T  DEFAULT NOW(),
        COL7 D  DEFAULT CURDATE(),
        PRIMARY KEY (COL2)
        ) ENGINE INNODB
[1] => ALTER TABLE test ADD  INDEX CIDX  (COL1)
[2] => ALTER TABLE test ADD  INDEX `COL4-INDEX`  (`COL4  ASC`, COL5)
)

The code above identifies a bug in addIndexSql where the column is incorrectly quoted if an attribute is added

Data Dictionary Objects

The object can be passed to the following ADOdb methods

createIndexSql

Note that multiple indexes can be created in the same command when used this way

Usage

$t = new metaObjectStructure($dict,'employees');
$t->addIndexObject('bd-idx')
    ->addIndexItemObject('birth_date')->addAttribute('ASC');
 
$sqlarray = $dict->createIndexSql($t);

dropIndexSql

Usage

$t = new metaObjectStructure($dict,'employees');
$t->addIndeObject('bd-idx');
 
$sqlarray = $dict->dropIndexSql($t);

addColumnSql

Usage

$t = new metaObjectStructure($dict,'employees');
$t->addColumnObject('COL','C(32)');
 
$sqlarray = $dict->addColumnSql($t);

alterColumnSql

Usage

$t = new metaObjectStructure($dict,'employees');
$t->addColumnObject('COL','C(32)');
 
$sqlarray = $dict->alterColumnSql($t);

dropColumnSql

Usage

$t = new metaObjectStructure($dict,'employees');
$t->addColumnObject('COL','C(32)');
 
$sqlarray = $dict->dropColumnSql($t);

dropTableSql

Usage

$t = new metaObjectStructure($dict,'employees');
$sqlarray = $dict->dropTableSql($t);

renameTableSql

Usage

$t = new metaObjectStructure($dict,'employees');
$t->setNewName('new-column');
$sqlarray = $dict->renameTableSql($t);
v5/dictionary/structure/sample_usage.txt · Last modified: 2016/04/09 21:46 by mnewnham