Beispiel #1
0
 /**
  * Get string for add column
  * 
  * @param string $field_name
  * @param string $params
  * @param string $driver
  * @return string
  * @throws Kohana_Exception
  */
 protected function compile_column($field_name, $params, $driver)
 {
     if (empty($params)) {
         throw new Kohana_Exception('migrations.missing_argument');
     }
     $sql = $this->db->quote_column($field_name);
     $params = (array) $params;
     $query_params = array('type' => 'integer', 'null' => 'NULL', 'default' => '');
     $type = 'integer';
     $length = 0;
     foreach ($params as $key => $param) {
         if ($key === 0) {
             $query_params['type'] = $this->is_type($param, $driver);
             $type = $param;
             if (in_array($param, array('primary_key', 'big_primary_key'))) {
                 $query_params['null'] = '';
             }
         } else {
             switch ($key) {
                 case 'type':
                     $query_params['type'] = $this->is_type($param, $driver);
                     break;
                 case 'null':
                     if (!$param) {
                         $query_params['null'] = 'NOT NULL';
                     }
                     break;
                 case 'default':
                     if (is_string($param)) {
                         $param = $this->quote($param);
                     }
                     $query_params['default'] = $param;
                     break;
                 case 'length':
                     if ($param > 0 && $this->is_length($type)) {
                         $length = $param;
                     }
                     break;
             }
         }
     }
     foreach ($query_params as $key => $param) {
         if ($param !== '') {
             if ($key === 'default') {
                 $sql .= ' default';
             }
             $sql .= ' ' . $param;
             if ($key === 'type') {
                 if ($length == 0) {
                     $length = $this->getDefault($type);
                 }
                 if ($length > 0) {
                     $sql .= '(' . $length . ')';
                 }
             }
         }
     }
     return $sql;
 }