Location: PHPKode > projects > Phpwebpad > phpwebpad/bin/orm/TableDefinition.php
<?
/** -------------------------------------------------------------------------------------*
* Version: 1.0                                                                           *
* License: http://phpwebpad.hafij.com @copyright from 2010                               *
* ---------------------------------------------------------------------------------------*
* DEVELOPED BY                                                                           *
* Mohammad Hafijur Rahman (Badal)                                                        *
* hide@address.com, hide@address.com                                             *
* ------------------------------------------------------------------------------------ **/

/**
 * Define a table.
 */
class TableDefinition {
  private $table_name = null;
  private $columns = array();
  private $unique_keys = array();
  private $triggers = array();

  public function  __construct($table_name) {
    if(empty($table_name))
      throw new Exception('Table name can not be empty');

    $this->table_name = strtolower($table_name);
    $this->addNonNullColumn('created', 'DATETIME');
    $this->addNonNullColumn('modified', 'TIMESTAMP', 
            'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', null, false);
  }

  /**
   * @param string $column_name
   * @param string $type
   * @param mixed $default
   * @param string $comment
   * @param bool $wrap_default
   */
  public function addColumn($column_name, $type, $default = null, $comment = null,
                            $wrap_default = true) {
    if(empty($column_name) || empty($type))
      throw new Exception('Column name or type can not be empty');

    if($default === null) $default = "NULL";
    $this->columns[] = array('name'         => strtolower($column_name),
                             'type'         => $type,
                             'default'      => $default,
                             'comment'      => $comment,
                             'wrap_default' => $wrap_default,
                             'non_null'     => false
                        );
  }

  /**
   * @param string $column_name
   * @param string $type
   * @param mixed $default
   * @param string $comment
   * @param bool $wrap_default
   */
  public function addNonNullColumn($column_name, $type, $default = null, $comment = null,
                                   $wrap_default = true) {
    if(empty($column_name) || empty($type))
      throw new Exception('Column name or type can not be empty');
    
    $type = $type.' NOT NULL';
    $this->columns[] = array('name'         => strtolower($column_name),
                             'type'         => $type,
                             'default'      => $default,
                             'comment'      => $comment,
                             'wrap_default' => $wrap_default ,
                             'non_null'     => true
                       );
  }

  /**
   * @param string $name
   * @param array $columns
   */
  public function addUniqueKey($name, $columns) {
    if(empty($name))
      throw new Exception('Unique key name can not be empty');
    
    if(is_array($columns))
      $this->unique_keys[] = array('name' => strtolower($name), 'cols' => $columns);
    else
      throw new Exception('Columns must be an array');
  }

  /**
   * Add a trigger on this table.
   * @param string $name
   * @param bool $before
   * @param string $event
   * @param string $sql
   */
  public function addTrigger($name, $before = true, $event = "INSERT", $sql = null) {
    throw new Exception('not emplemented yet');
    
    if(empty($name))
      throw new Exception('Trigger name can not be empty');

    if(in_array($event, array('INSERT', 'UPDATE', 'DELETE')))
      throw new Exception('Event is not valid');

    $this->triggers[] = array('name'    => strtolower($name),
                              'before'  => $before,
                              'event'   => $event,
                              'sql'     => $sql
                        );
  }
  
  /**
   * Return the table name.
   * @return string
   */
  public function getTableName() {
    return $this->table_name;
  }

  /**
   * @return array
   */
  public function getColumns() {
    return $this->columns;
  }

  /**
   * @return array
   */
  public function getUniqueKeys() {
    return $this->unique_keys;
  }

  /**
   * @return array
   */
  public function getTriggers() {
    return $this->triggers;
  }
  
  /**
   * return the primary key name for this table definition.
   * @return string.
   */
  public function getPrimaryKeyName() {
    return $this->table_name.'_id';
  }
}
?>
Return current item: Phpwebpad