createTable() public méthode

The $cols parameter should be in this format ... {{code: php $cols = array( 'col_1' => array( 'type' => (string) bool, char, int, ... 'size' => (int) total length for char|varchar|numeric 'scope' => (int) decimal places for numeric 'default' => (bool) the default value, if any 'require' => (bool) is the value required to be NOT NULL? 'primary' => (bool) is this a primary key column? 'autoinc' => (bool) is this an auto-increment column? ), 'col_2' => array(...) ); }} For available field types, see Solar_Sql_Adapter::$_native.
public createTable ( string $table, array $cols ) : string
$table string The name of the table to create.
$cols array Array of columns to create.
Résultat string An SQL string.
 /**
  * 
  * Creates the table and indexes in the database using $this->_table_cols
  * and $this->_index.
  * 
  * @return void
  * 
  */
 protected function _createTableAndIndexes()
 {
     /**
      * Create the table.
      */
     $this->_sql->createTable($this->_table_name, $this->_table_cols);
     /**
      * Create the indexes.
      */
     foreach ($this->_index as $name => $info) {
         try {
             // create this index
             $this->_sql->createIndex($this->_table_name, $info['name'], $info['type'] == 'unique', $info['cols']);
         } catch (Exception $e) {
             // cancel the whole deal.
             $this->_sql->dropTable($this->_table_name);
             throw $e;
         }
     }
 }
Exemple #2
0
 /**
  * 
  * Overrides the adapter's create Table to manage Oracle's specific needs
  * for table creation. Creates a portable table.
  * 
  * The $cols parameter should be in this format ...
  * 
  * {{code: php
  *     $cols = array(
  *       'col_1' => array(
  *         'type'    => (string) bool, char, int, ...
  *         'size'    => (int) total length for char|varchar|numeric
  *         'scope'   => (int) decimal places for numeric
  *         'default' => (bool) the default value, if any
  *         'require' => (bool) is the value required to be NOT NULL?
  *         'primary' => (bool) is this a primary key column?
  *         'autoinc' => (bool) is this an auto-increment column?
  *       ),
  *       'col_2' => array(...)
  *     );
  * }}
  * 
  * For available field types, see Solar_Sql_Adapter::$_native.
  * 
  * @param string $table The name of the table to create.
  * 
  * @param array $cols Array of columns to create.
  * 
  * @return string An SQL string.
  * 
  * @todo Instead of stacking errors, stack info, then throw in exception.
  * 
  */
 public function createTable($table, $cols)
 {
     $table_name = strtoupper($table);
     // main creation routine
     parent::createTable($table, $cols);
     // create auto-increment triggers
     foreach ($cols as $col_name => $info) {
         $name = strtoupper($col_name);
         if (!empty($info['autoinc'])) {
             // create a sequence for the auto-increment
             $this->_createSequence("{$name}_SEQ", 1);
             // create a trigger for the auto-increment.
             // Do NOT reformat to have line breaks.
             // Oracle throws a fit if you do.
             $stmt = "CREATE OR REPLACE TRIGGER \"IN_{$name}\" " . "BEFORE INSERT ON {$table_name} " . "REFERENCING NEW AS NEW " . "FOR EACH ROW BEGIN " . "SELECT {$name}_SEQ.NEXTVAL INTO :NEW.{$name} FROM DUAL; " . "END;";
             $this->query($stmt);
         }
     }
 }