Example #1
0
 /**
  *
  * @param <type> $info
  */
 public function __construct(Webdb_DBMS_Table $table, $info)
 {
     $info = array_combine(array_map('strtolower', array_keys($info)), array_values($info));
     // Table object
     $this->_table = $table;
     // Name
     $this->_name = $info['field'];
     // Type
     $this->parse_type($info['type']);
     // Default
     $this->_default = $info['default'];
     // Primary key
     if (strtoupper($info['key']) == 'PRI') {
         $this->_isPK = true;
         if ($info['extra'] == 'auto_increment') {
             $this->_isAutoIncrement = true;
         }
     }
     // Unique key
     if (strtoupper($info['key']) == 'UNI') {
         $this->_isUnique = true;
     }
     // Comment
     $this->_comment = $info['comment'];
     // Collation
     $this->_collation = $info['collation'];
     // NULL?
     if ($info['null'] == 'NO') {
         $this->_required = TRUE;
     }
     // Is this a foreign key?
     if (in_array($this->_name, $table->get_foreign_key_names())) {
         $referencedTables = $table->get_referenced_tables();
         $this->_references = $referencedTables[$this->_name];
     }
     // DB user privileges
     $this->_db_user_privileges = $info['privileges'];
 }
Example #2
0
 /**
  * Get a table object.
  *
  * @param string $tablename
  * @return Webdb_DBMS_Database
  */
 public function get_table($tablename = FALSE)
 {
     if (!$tablename) {
         $tablename = Request::current()->param('tablename', FALSE);
         if ($tablename) {
             return $this->get_table($tablename);
         } else {
             return FALSE;
         }
     }
     if (!in_array($tablename, $this->list_tables())) {
         throw new Exception("The table '{$tablename}' could not be found.");
     }
     if (!isset($this->_tables[$tablename])) {
         $table = new Webdb_DBMS_Table($this, $tablename);
         if ($table->can('select')) {
             $this->_tables[$tablename] = $table;
         } else {
             return FALSE;
         }
     }
     return $this->_tables[$tablename];
 }
Example #3
0
 /**
  * Rename all keys in all data rows to match DB column names, and normalize
  * all values to be valid for the `$table`.
  *
  * If a _value_ in the array matches a lowercased DB column header, the _key_
  * of that value is the DB column name to which that header has been matched.
  *
  * @param Webdb_DBMS_Table $table
  * @param array $columns
  * @return array Array of error messages.
  */
 public function match_fields($table, $column_map)
 {
     // First get the indexes of the headers
     $heads = $this->remap($column_map);
     $errors = array();
     for ($row_num = 1; $row_num <= $this->row_count(); $row_num++) {
         foreach ($this->data[$row_num] as $col_num => $value) {
             if (!isset($heads[$col_num])) {
                 continue;
             }
             $col_errors = array();
             $db_column_name = $heads[$col_num];
             $column = $table->get_column($db_column_name);
             // Required, has no default, and is empty
             if ($column->is_required() and !$column->has_default() and empty($value)) {
                 $col_errors[] = 'Required but empty';
             }
             // Already exists
             if ($column->is_unique_key()) {
                 // @TODO
             }
             // Too long (if the column has a size and the value is greater than this)
             if (!$column->is_foreign_key() and !$column->is_boolean() and $column->get_size() > 0 and strlen($value) > $column->get_size()) {
                 $col_errors[] = 'Value (' . $value . ') too long (maximum length of ' . $column->get_size() . ')';
             }
             // Invalid foreign key value
             if (!empty($value) and $column->is_foreign_key()) {
                 $err = $this->validate_foreign_key($column, $col_num, $row_num, $value);
                 if ($err) {
                     $col_errors[] = $err;
                 }
             }
             // Dates
             if ($column->get_type() == 'date' and !empty($value) and preg_match('/\\d{4}-\\d{2}-\\d{2}/', $value) !== 1) {
                 $col_errors[] = 'Value (' . $value . ') not in date format';
             }
             if (count($col_errors) > 0) {
                 // Construct error details array
                 $errors[] = array('column_name' => $this->headers[$col_num], 'column_number' => $col_num, 'field_name' => $column->get_name(), 'row_number' => $row_num, 'messages' => $col_errors);
             }
         }
     }
     return $errors;
 }
Example #4
0
 /**
  * Rename all keys in all data rows to match DB column names, and normalize
  * all values to be valid for the `$table`.
  *
  * If a _value_ in the array matches a lowercased DB column header, the _key_
  * of that value is the DB column name to which that header has been matched.
  *
  * @param Webdb_DBMS_Table $table
  * @param array $array
  * @return void
  */
 public function match_fields($table, $array)
 {
     // First get the indexes of the headers
     foreach ($array as $key => $val) {
         foreach ($this->headers as $head_num => $head_name) {
             if (strtolower($head_name) == $val) {
                 $heads[$head_num] = $key;
             }
         }
     }
     // Now rename the keys in all the data rows, and get IDs for foreign keys.
     foreach ($this->data as &$row) {
         $new_row = array();
         foreach ($row as $cell_num => $value) {
             if (isset($heads[$cell_num])) {
                 $db_column_name = $heads[$cell_num];
                 if (!empty($value) && $table->get_column($db_column_name)->is_foreign_key()) {
                     $foreign_table = $table->get_column($db_column_name)->get_referenced_table();
                     $foreign_table->reset_filters();
                     $foreign_table->add_filter($foreign_table->get_title_column()->get_name(), '=', $value);
                     $value = $foreign_table->get_rows()->current();
                     $value = $value['id'];
                 }
                 $new_row[$db_column_name] = $value;
             }
         }
         $row = array_merge($table->get_default_row(), $new_row);
     }
 }