Overview

Namespaces

  • Alchemy
    • core
      • query
      • schema
    • dialect
    • engine
    • orm
    • tests
    • util
      • promise
  • PHP

Classes

  • Column
  • Foreign
  • ForeignKey
  • Index
  • Table
  • TableElement
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace Alchemy\core\schema;
  4: use Alchemy\core\query\ColumnRef;
  5: use Alchemy\core\query\TableRef;
  6: use Alchemy\util\promise\IPromisable;
  7: 
  8: 
  9: /**
 10:  * Abstract base class for representing a column in SQL
 11:  */
 12: class Column extends TableElement implements IPromisable {
 13: 
 14:     /**
 15:      * Retrieve the column for a reference like 'Table.Column',
 16:      * or 'self.Column' if a $self table is provided.
 17:      *
 18:      * @param  string $column column reference
 19:      * @param  Table  $self   table to use for relative references (optional)
 20:      * @return Column
 21:      */
 22:     public static function find($column, $self = null) {
 23:         list($ref, $col) = explode('.', $column) + array('', '');
 24: 
 25:         $table = ($ref == 'self' || $ref == '') ? $self : Table::find($ref);
 26:         if (!$table) {
 27:             throw new \Exception("Cannot find Table '{$ref}'.");
 28:         }
 29: 
 30:         return $table->getColumn($col);
 31:     }
 32: 
 33: 
 34:     public static function list_promisable_methods() {
 35:         return array(
 36:             'copy'     => "Alchemy\core\schema\Column",
 37:             'getTable' => "Alchemy\core\schema\Table");
 38:     }
 39: 
 40: 
 41:     /**
 42:      * Decode a value from the RDBMS into a PHP value
 43:      *
 44:      * @param mixed $value
 45:      * @return string
 46:      */
 47:     public function decode($value) {
 48:         $def = self::get_definition($this->type);
 49:         return $def['decode']($this, $value);
 50:     }
 51: 
 52: 
 53:     /**
 54:      * Encode a PHP value into something usable for the RDBMS.
 55:      *
 56:      * @param mixed $value
 57:      * @return Scalar
 58:      */
 59:     public function encode($value) {
 60:         $def = self::get_definition($this->type);
 61:         return $def['encode']($this, $value);
 62:     }
 63: 
 64: 
 65:     /**
 66:      * Get the ForeignKey constraint, if applicable, on this Column.
 67:      *
 68:      * @return ForeignKey
 69:      */
 70:     public function getForeignKey() {
 71:         if ($this->args['foreign_key']) {
 72:             return Index::ForeignKey(array(array($this), array($this->args['foreign_key'])), $this->table, $this->name);
 73:         }
 74:     }
 75: 
 76: 
 77:     /**
 78:      * Get the Index, if applicable, on this Column.
 79:      *
 80:      * @return Index
 81:      */
 82:     public function getIndex() {
 83:         if ($this->args['unique']) {
 84:             return Index::UniqueKey($this, $this->table, $this->name);
 85:         } elseif ($this->args['index']) {
 86:             return Index::Index($this, $this->table, $this->name);
 87:         }
 88:     }
 89: 
 90: 
 91:     public function getArg($arg) {
 92:         return array_key_exists($arg, $this->args) ? $this->args[$arg] : null;
 93:     }
 94: 
 95: 
 96:     /**
 97:      * Return a reference to this column
 98:      *
 99:      * @param  TableRef  $table TableRef to build on, else create a new one
100:      * @return ColumnRef
101:      */
102:     public function getRef(TableRef $table = null) {
103:         return new ColumnRef($this, $table ?: $this->getTable()->getRef());
104:     }
105: 
106: 
107:     /**
108:      * Return true if this column can be null
109:      *
110:      * @return bool
111:      */
112:     public function isNullable() {
113:         return $this->args['null'];
114:     }
115: 
116: 
117:     /**
118:      * Return true if this column is part of the primary key
119:      *
120:      * @return bool
121:      */
122:     public function isPrimaryKeyPart() {
123:         return $this->args['primary_key'];
124:     }
125: }
126: 
API documentation generated by ApiGen 2.8.0