Beispiel #1
0
Datei: CRUD.php Projekt: jasny/Q
 /**
  * Add criteria to statement for each filter from array $filters
  * 
  * @param $statement
  * @param array $filters
  */
 protected function applyFilters($statement, $filters)
 {
     $db = $this->table->getConnection();
     foreach ($filters as $key => $value) {
         if (empty($value)) {
             continue;
         }
         list($field, $operator) = explode(' ', $key) + array(1 => null);
         $statement->addCriteria(strpos('.', $field) === false ? $db->makeIdentifier($this->table, $field) : DB::i()->quoteField($field), $value, $operator);
     }
 }
Beispiel #2
0
Datei: data.php Projekt: jasny/Q
 /**
  * Output list of records.
  */
 public function overview()
 {
     $show = !empty($_GET['show']) ? $_GET['show'] : 'current';
     $page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
     $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : $_ENV['cfg']['cms']['limit_overview'];
     $filters = !empty($_GET['filter']) ? $_GET['filter'] : array();
     $order = isset($_GET['order']) ? $_GET['order'] : null;
     $statement = $this->td->getStatement('overview.xml');
     $statement->addToPart(DB::COMMAND_PART, 'SQL_CALC_FOUND_ROWS');
     foreach ($filters as $key => $value) {
         if (empty($value)) {
             continue;
         }
         list($field, $operator) = explode(' ', $key . ' ');
         $statement->addCriteria(strpos('.', $field) === false ? DB::i()->makeIdentifier($this->table, $field) : DB::i()->quoteIdentifier($field), $value, $operator);
     }
     if (isset($limit)) {
         $statement->setLimit($limit, DB::arg('page', $page));
     }
     if (isset($order)) {
         list($field, $sort) = explode(" ", $order . ' ASC');
         $statement->addOrderBy((strpos('.', $field) === false ? DB::i()->makeIdentifier($this->table, $field) : DB::i()->quoteIdentifier($field)) . ' ' . $sort);
     }
     $xml = join("\n", (array) $statement->execute(DB::arg('state', $state))->getColumn());
     $rowcount = DB::i()->query("SELECT FOUND_ROWS()")->fetchValue();
     $xml = '<overview table="' . htmlspecialchars($this->table, ENT_COMPAT, 'UTF-8') . '" page="' . $page . '" count="' . $rowcount . '"' . (isset($limit) ? ' limit="' . $limit . '"' : '') . ' descfield="' . htmlspecialchars($this->td->getFieldProperty('#role:description', 'name'), ENT_COMPAT, 'UTF-8') . '">' . $xml . '</overview>';
     self::outputXML($xml);
 }
Beispiel #3
0
 /**
  * Get value(s) of primary key(s)
  * 
  * @param  mixed $table  Table index or name
  * @return mixed
  */
 public function getId($table = null)
 {
     if (!isset($table)) {
         $table = isset($this->_baseTable) ? $this->_baseTable->getTablename() : 0;
     }
     if (is_int($table) && !empty($this->_tablerefs)) {
         $tables = array_keys($this->_tablerefs);
         if (!isset($tables[$table])) {
             throw new DB_Exception("Unable to get id of table number {$table}: Record only holds fields of " . count($tables) . " tables.");
         }
         $table = $tables[$table];
     }
     if (!isset($this->_primaryKeyFields)) {
         $this->indexPrimairyKeyFields();
     }
     if (!isset($this->_primaryKeyFields[$table])) {
         throw new DB_Exception("Unable to get id of table '{$table}': Record does not hold the fields for the primary key(s).");
     }
     if (count($this->_primaryKeyFields[$table]) === 1) {
         return $this->_primaryKeyFields[$table][0]->getValue();
     }
     foreach ($this->_primaryKeyFields[$table] as $field) {
         $ids[] = $field->getValue();
     }
     return $ids;
 }
Beispiel #4
0
 public function checkAccount()
 {
     global $_F;
     $auth_raw = $_COOKIE['auth'];
     if ($auth_raw) {
         list($uid, $auth_str) = explode('_', $auth_raw);
         $table = new DB_Table('members');
         $user = $table->find('uid=' . $uid);
         if ($user) {
             if (md5($user['username'] . '_' . $user['password']) == $auth_str) {
                 $_F['uid'] = $uid;
                 $_F['username'] = $user['username'];
                 $_F['real_name'] = $user['real_name'];
                 return true;
             }
         }
     }
     return false;
 }
Beispiel #5
0
Datei: Field.php Projekt: jasny/Q
 /**
  * Return the name of a field.
  *
  * @param int $flags  A DB::FIELDNAME_% constant and DB::WITH_ALIAS and DB::QUOTE_% options as binary set 
  * @return string
  */
 public function getName($flags = DB::FIELDNAME_FULL)
 {
     switch ($flags & 0xf) {
         case DB::FIELDNAME_NAME:
             return $flags & QUOTE_LOOSE || $flags & QUOTE_STRICT ? $this->parent->getConnection()->quoteIdentifier($this['name'], $flags) : $this['name'];
         case DB::FIELDNAME_FULL:
             return $flags & QUOTE_LOOSE || $flags & QUOTE_STRICT ? $this->parent->getConnection()->makeIdentifier($this['table'], $this['name'], $flags) : (isset($this['table']) ? $this['table'] . '.' . $this['name'] : $this['name']);
         case DB::FIELDNAME_COLUMN:
             return $this->parent->getConnection()->makeIdentifier($this['table'], $this['name_db'], $flags & DB::WITH_ALIAS && $this['name'] != $this['name_db'] ? $this['name'] : null, $flags);
         case DB::FIELDNAME_DB:
             return $this->parent->getConnection()->makeIdentifier($this['table_db'], $this['name_db'], $flags & DB::WITH_ALIAS && $this['name'] != $this['name_db'] ? $this['name'] : null);
     }
 }
Beispiel #6
0
Datei: DB.php Projekt: jasny/Q
 /**
  * Log a message.
  *
  * @param string|array $message  Message or associated array with info
  * @param string       $type
  */
 public function log($message, $type = null)
 {
     if (!isset($type) && is_array($message) && isset($message['type'])) {
         $type = $message['type'];
     }
     if (isset($this->alias[$type])) {
         $type = $this->alias[$type];
     }
     if (!$this->shouldLog($type)) {
         return;
     }
     $values = array_merge($this->_eventValues->getAll(), isset($type) ? array('type' => $type) : array(), is_array($message) ? $message : array('message' => $message));
     $store = null;
     if (!isset($this->fields)) {
         $store =& $values;
     } else {
         foreach ($this->fields as $key => $field) {
             $store[$field] = isset($values[$key]) ? $values[$key] : null;
         }
     }
     $conn = $this->table instanceof DB_Table ? $this->table->getConnection() : DB::i();
     $conn->store($this->table, $values);
 }
Beispiel #7
0
Datei: MySQL.php Projekt: jasny/Q
 /**
  * Resolve semantic mapping for fields in criteria.
  * 
  * @param DB_Table $table
  * @param array    $criteria
  * @param boolean  $keyvalue  Assume key/value pairs
  */
 protected function resolveCriteria(DB_Table $table, &$criteria, $keyvalue = false)
 {
     if (!isset($criteria)) {
         return;
     }
     if (!$keyvalue && (!is_array($criteria) || !is_string(key($criteria)))) {
         $pk = $table->getPrimaryKey();
         if (empty($pk)) {
             throw new Exception("Unable to select record for {$table}: Unable to determine a WHERE statement. The table might have no primary key.");
         }
         $criteria = (array) $criteria;
         if (count($pk) != count($criteria)) {
             throw new Exception("Unable to select record for {$table}: " . count($criteria) . " values specified, while primary key from table consists of " . count($pk) . " fields (" . implode(', ', $pk) . ").");
         }
         $criteria = array_combine((array) $pk->getName(DB::FIELDNAME_TABLE | DB::QUOTE), $criteria);
     } else {
         foreach (array_keys($criteria) as $i => $field) {
             if ($field[0] === '#') {
                 $keys[$i] = (string) $table->{$field};
             }
         }
         // Most cases this is not needed, so merge afterwards
         if (isset($keys)) {
             $keys = +array_keys($criteria);
             sort($keys);
             $criteria = array_combine($keys, $criteria);
         }
     }
 }
Beispiel #8
0
 /**
  * 
  * Check whether an index has the right type and has all specified columns.
  * 
  * @access private
  * 
  * @param string $idxname The index name.
  * 
  * @param string $newIdxName The prefixed and suffixed index name.
  * 
  * @param string $type The index type.
  * 
  * @param mixed $cols The column names for the index.
  * 
  * @param mixed $table_indexes Array with all indexes of the table.
  * 
  * @param string $mode The name of the calling function, this can be either
  * 'verify' or 'alter'.
  * 
  * @return bool|object Boolean true if the index has the right type and all
  * specified columns. Otherwise, either boolean false (case 'alter') or a
  * PEAR_Error (case 'verify').
  * 
  */
 function _checkIndex($idxname, $newIdxName, $type, $cols, &$table_indexes, $mode)
 {
     $index_found = false;
     foreach ($table_indexes[$type] as $index_name => $index_fields) {
         if (strtolower($index_name) == strtolower($newIdxName)) {
             $index_found = true;
             array_walk($cols, create_function('&$value,$key', '$value = trim(strtolower($value));'));
             array_walk($index_fields, create_function('&$value,$key', '$value = trim(strtolower($value));'));
             foreach ($index_fields as $index_field) {
                 if (($key = array_search($index_field, $cols)) !== false) {
                     unset($cols[$key]);
                 }
             }
             break;
         }
     }
     if (!$index_found) {
         return $mode == 'alter' ? false : DB_Table::throwError(DB_TABLE_ERR_VER_IDX_MISSING, "'{$idxname}' ('{$newIdxName}')");
     }
     if (count($cols) > 0) {
         // string of column names
         $colstring = implode(', ', $cols);
         return $mode == 'alter' ? false : DB_Table::throwError(DB_TABLE_ERR_VER_IDX_COL_MISSING, "'{$idxname}' ({$colstring})");
     }
     return true;
 }
Beispiel #9
0
 /**
  * alter an existing table
  *
  * @param string $name         name of the table that is intended to be changed.
  * @param array $changes     associative array that contains the details of each type
  *                             of change that is intended to be performed. The types of
  *                             changes that are currently supported are defined as follows:
  *
  *                             name
  *
  *                                New name for the table.
  *
  *                            add
  *
  *                                Associative array with the names of fields to be added as
  *                                 indexes of the array. The value of each entry of the array
  *                                 should be set to another associative array with the properties
  *                                 of the fields to be added. The properties of the fields should
  *                                 be the same as defined by the Metabase parser.
  *
  *
  *                            remove
  *
  *                                Associative array with the names of fields to be removed as indexes
  *                                 of the array. Currently the values assigned to each entry are ignored.
  *                                 An empty array should be used for future compatibility.
  *
  *                            rename
  *
  *                                Associative array with the names of fields to be renamed as indexes
  *                                 of the array. The value of each entry of the array should be set to
  *                                 another associative array with the entry named name with the new
  *                                 field name and the entry named Declaration that is expected to contain
  *                                 the portion of the field declaration already in DBMS specific SQL code
  *                                 as it is used in the CREATE TABLE statement.
  *
  *                            change
  *
  *                                Associative array with the names of the fields to be changed as indexes
  *                                 of the array. Keep in mind that if it is intended to change either the
  *                                 name of a field and any other properties, the change array entries
  *                                 should have the new names of the fields as array indexes.
  *
  *                                The value of each entry of the array should be set to another associative
  *                                 array with the properties of the fields to that are meant to be changed as
  *                                 array entries. These entries should be assigned to the new values of the
  *                                 respective properties. The properties of the fields should be the same
  *                                 as defined by the Metabase parser.
  *
  *                            Example
  *                                array(
  *                                    'name' => 'userlist',
  *                                    'add' => array(
  *                                        'quota' => array(
  *                                            'type' => 'integer',
  *                                            'unsigned' => 1
  *                                        )
  *                                    ),
  *                                    'remove' => array(
  *                                        'file_limit' => array(),
  *                                        'time_limit' => array()
  *                                    ),
  *                                    'change' => array(
  *                                        'name' => array(
  *                                            'length' => '20',
  *                                            'definition' => array(
  *                                                'type' => 'text',
  *                                                'length' => 20,
  *                                            ),
  *                                        )
  *                                    ),
  *                                    'rename' => array(
  *                                        'sex' => array(
  *                                            'name' => 'gender',
  *                                            'definition' => array(
  *                                                'type' => 'text',
  *                                                'length' => 1,
  *                                                'default' => 'M',
  *                                            ),
  *                                        )
  *                                    )
  *                                )
  *
  * @param boolean $check     (ignored in DB_Table)
  * @access public
  *
  * @return mixed DB_OK on success, a PEAR error on failure
  */
 function alterTable($name, $changes, $check)
 {
     $version = $this->_getServerVersion();
     foreach ($changes as $change_name => $change) {
         switch ($change_name) {
             case 'add':
                 if ($version['major'] >= 3 && $version['minor'] >= 1) {
                     break;
                 }
             case 'name':
                 if ($version['major'] >= 3 && $version['minor'] >= 1) {
                     break;
                 }
             case 'remove':
             case 'change':
             case 'rename':
             default:
                 return DB_Table::throwError(DB_TABLE_ERR_ALTER_TABLE_IMPOS);
         }
     }
     $query = '';
     if (array_key_exists('name', $changes)) {
         $change_name = $this->_db->quoteIdentifier($changes['name']);
         $query .= 'RENAME TO ' . $change_name;
     }
     if (array_key_exists('add', $changes)) {
         foreach ($changes['add'] as $field_name => $field) {
             if ($query) {
                 $query .= ', ';
             }
             $query .= 'ADD COLUMN ' . $field_name . ' ' . $field;
         }
     }
     if (!$query) {
         return DB_OK;
     }
     $name = $this->_db->quoteIdentifier($name);
     return $this->_db->query("ALTER TABLE {$name} {$query}");
 }
Beispiel #10
0
 /**
  * alter an existing table
  *
  * @param string $name         name of the table that is intended to be changed.
  * @param array $changes     associative array that contains the details of each type
  *                             of change that is intended to be performed. The types of
  *                             changes that are currently supported are defined as follows:
  *
  *                             name
  *
  *                                New name for the table.
  *
  *                            add
  *
  *                                Associative array with the names of fields to be added as
  *                                 indexes of the array. The value of each entry of the array
  *                                 should be set to another associative array with the properties
  *                                 of the fields to be added. The properties of the fields should
  *                                 be the same as defined by the Metabase parser.
  *
  *
  *                            remove
  *
  *                                Associative array with the names of fields to be removed as indexes
  *                                 of the array. Currently the values assigned to each entry are ignored.
  *                                 An empty array should be used for future compatibility.
  *
  *                            rename
  *
  *                                Associative array with the names of fields to be renamed as indexes
  *                                 of the array. The value of each entry of the array should be set to
  *                                 another associative array with the entry named name with the new
  *                                 field name and the entry named Declaration that is expected to contain
  *                                 the portion of the field declaration already in DBMS specific SQL code
  *                                 as it is used in the CREATE TABLE statement.
  *
  *                            change
  *
  *                                Associative array with the names of the fields to be changed as indexes
  *                                 of the array. Keep in mind that if it is intended to change either the
  *                                 name of a field and any other properties, the change array entries
  *                                 should have the new names of the fields as array indexes.
  *
  *                                The value of each entry of the array should be set to another associative
  *                                 array with the properties of the fields to that are meant to be changed as
  *                                 array entries. These entries should be assigned to the new values of the
  *                                 respective properties. The properties of the fields should be the same
  *                                 as defined by the Metabase parser.
  *
  *                            Example
  *                                array(
  *                                    'name' => 'userlist',
  *                                    'add' => array(
  *                                        'quota' => array(
  *                                            'type' => 'integer',
  *                                            'unsigned' => 1
  *                                        )
  *                                    ),
  *                                    'remove' => array(
  *                                        'file_limit' => array(),
  *                                        'time_limit' => array()
  *                                    ),
  *                                    'change' => array(
  *                                        'name' => array(
  *                                            'length' => '20',
  *                                            'definition' => array(
  *                                                'type' => 'text',
  *                                                'length' => 20,
  *                                            ),
  *                                        )
  *                                    ),
  *                                    'rename' => array(
  *                                        'sex' => array(
  *                                            'name' => 'gender',
  *                                            'definition' => array(
  *                                                'type' => 'text',
  *                                                'length' => 1,
  *                                                'default' => 'M',
  *                                            ),
  *                                        )
  *                                    )
  *                                )
  *
  * @param boolean $check     (ignored in DB_Table)
  * @access public
  *
  * @return mixed DB_OK on success, a PEAR error on failure
  */
 function alterTable($name, $changes, $check)
 {
     foreach ($changes as $change_name => $change) {
         switch ($change_name) {
             case 'add':
             case 'remove':
             case 'change':
             case 'rename':
             case 'name':
                 break;
             default:
                 return DB_Table::throwError(DB_TABLE_ERR_ALTER_TABLE_IMPOS);
         }
     }
     $query = '';
     if (array_key_exists('name', $changes)) {
         $change_name = $this->_db->quoteIdentifier($changes['name']);
         $query .= 'RENAME TO ' . $change_name;
     }
     if (array_key_exists('add', $changes)) {
         foreach ($changes['add'] as $field_name => $field) {
             if ($query) {
                 $query .= ', ';
             }
             $query .= 'ADD ' . $field_name . ' ' . $field;
         }
     }
     if (array_key_exists('remove', $changes)) {
         foreach ($changes['remove'] as $field_name => $field) {
             if ($query) {
                 $query .= ', ';
             }
             $field_name = $this->_db->quoteIdentifier($field_name);
             $query .= 'DROP ' . $field_name;
         }
     }
     $rename = array();
     if (array_key_exists('rename', $changes)) {
         foreach ($changes['rename'] as $field_name => $field) {
             $rename[$field['name']] = $field_name;
         }
     }
     if (array_key_exists('change', $changes)) {
         foreach ($changes['change'] as $field_name => $field) {
             if ($query) {
                 $query .= ', ';
             }
             if (isset($rename[$field_name])) {
                 $old_field_name = $rename[$field_name];
                 unset($rename[$field_name]);
             } else {
                 $old_field_name = $field_name;
             }
             $old_field_name = $this->_db->quoteIdentifier($old_field_name);
             $query .= "CHANGE {$old_field_name} " . $field_name . ' ' . $field['definition'];
         }
     }
     if (!empty($rename)) {
         foreach ($rename as $rename_name => $renamed_field) {
             if ($query) {
                 $query .= ', ';
             }
             $field = $changes['rename'][$renamed_field];
             $renamed_field = $this->_db->quoteIdentifier($renamed_field);
             $query .= 'CHANGE ' . $renamed_field . ' ' . $field['name'] . ' ' . $renamed_field['definition'];
         }
     }
     if (!$query) {
         return DB_OK;
     }
     $name = $this->_db->quoteIdentifier($name);
     return $this->_db->query("ALTER TABLE {$name} {$query}");
 }
Beispiel #11
0
 /**
  * alter an existing table
  *
  * @param string $name         name of the table that is intended to be changed.
  * @param array $changes     associative array that contains the details of each type
  *                             of change that is intended to be performed. The types of
  *                             changes that are currently supported are defined as follows:
  *
  *                             name
  *
  *                                New name for the table.
  *
  *                            add
  *
  *                                Associative array with the names of fields to be added as
  *                                 indexes of the array. The value of each entry of the array
  *                                 should be set to another associative array with the properties
  *                                 of the fields to be added. The properties of the fields should
  *                                 be the same as defined by the Metabase parser.
  *
  *
  *                            remove
  *
  *                                Associative array with the names of fields to be removed as indexes
  *                                 of the array. Currently the values assigned to each entry are ignored.
  *                                 An empty array should be used for future compatibility.
  *
  *                            rename
  *
  *                                Associative array with the names of fields to be renamed as indexes
  *                                 of the array. The value of each entry of the array should be set to
  *                                 another associative array with the entry named name with the new
  *                                 field name and the entry named Declaration that is expected to contain
  *                                 the portion of the field declaration already in DBMS specific SQL code
  *                                 as it is used in the CREATE TABLE statement.
  *
  *                            change
  *
  *                                Associative array with the names of the fields to be changed as indexes
  *                                 of the array. Keep in mind that if it is intended to change either the
  *                                 name of a field and any other properties, the change array entries
  *                                 should have the new names of the fields as array indexes.
  *
  *                                The value of each entry of the array should be set to another associative
  *                                 array with the properties of the fields to that are meant to be changed as
  *                                 array entries. These entries should be assigned to the new values of the
  *                                 respective properties. The properties of the fields should be the same
  *                                 as defined by the Metabase parser.
  *
  *                            Example
  *                                array(
  *                                    'name' => 'userlist',
  *                                    'add' => array(
  *                                        'quota' => array(
  *                                            'type' => 'integer',
  *                                            'unsigned' => 1
  *                                        )
  *                                    ),
  *                                    'remove' => array(
  *                                        'file_limit' => array(),
  *                                        'time_limit' => array()
  *                                    ),
  *                                    'change' => array(
  *                                        'name' => array(
  *                                            'length' => '20',
  *                                            'definition' => array(
  *                                                'type' => 'text',
  *                                                'length' => 20,
  *                                            ),
  *                                        )
  *                                    ),
  *                                    'rename' => array(
  *                                        'sex' => array(
  *                                            'name' => 'gender',
  *                                            'definition' => array(
  *                                                'type' => 'text',
  *                                                'length' => 1,
  *                                                'default' => 'M',
  *                                            ),
  *                                        )
  *                                    )
  *                                )
  *
  * @param boolean $check     (ignored in DB_Table)
  * @access public
  *
  * @return mixed DB_OK on success, a PEAR error on failure
  */
 function alterTable($name, $changes, $check)
 {
     foreach ($changes as $change_name => $change) {
         switch ($change_name) {
             case 'add':
             case 'remove':
             case 'change':
             case 'name':
             case 'rename':
                 break;
             default:
                 return DB_Table::throwError(DB_TABLE_ERR_ALTER_TABLE_IMPOS);
         }
     }
     if (array_key_exists('add', $changes)) {
         foreach ($changes['add'] as $field_name => $field) {
             $query = 'ADD ' . $field_name . ' ' . $field;
             $result = $this->_db->query("ALTER TABLE {$name} {$query}");
             if (PEAR::isError($result)) {
                 return $result;
             }
         }
     }
     if (array_key_exists('remove', $changes)) {
         foreach ($changes['remove'] as $field_name => $field) {
             $field_name = $this->_db->quoteIdentifier($field_name);
             $query = 'DROP ' . $field_name;
             $result = $this->_db->query("ALTER TABLE {$name} {$query}");
             if (PEAR::isError($result)) {
                 return $result;
             }
         }
     }
     if (array_key_exists('change', $changes)) {
         foreach ($changes['change'] as $field_name => $field) {
             $field_name = $this->_db->quoteIdentifier($field_name);
             if (array_key_exists('type', $field)) {
                 $query = "ALTER {$field_name} TYPE " . $field['definition'];
                 $result = $this->_db->query("ALTER TABLE {$name} {$query}");
                 if (PEAR::isError($result)) {
                     return $result;
                 }
             }
             /* default / notnull changes not (yet) supported in DB_Table                
                             if (array_key_exists('default', $field)) {
                                 $query = "ALTER $field_name SET DEFAULT ".$db->quote($field['definition']['default'], $field['definition']['type']);
                                 $result = $db->exec("ALTER TABLE $name $query");
                                 if (PEAR::isError($result)) {
                                     return $result;
                                 }
                             }
                             if (array_key_exists('notnull', $field)) {
                                 $query.= "ALTER $field_name ".($field['definition']['notnull'] ? "SET" : "DROP").' NOT NULL';
                                 $result = $db->exec("ALTER TABLE $name $query");
                                 if (PEAR::isError($result)) {
                                     return $result;
                                 }
                             }
             */
         }
     }
     if (array_key_exists('rename', $changes)) {
         foreach ($changes['rename'] as $field_name => $field) {
             $field_name = $this->_db->quoteIdentifier($field_name);
             $result = $this->_db->query("ALTER TABLE {$name} RENAME COLUMN {$field_name} TO " . $this->_db->quoteIdentifier($field['name']));
             if (PEAR::isError($result)) {
                 return $result;
             }
         }
     }
     $name = $this->_db->quoteIdentifier($name);
     if (array_key_exists('name', $changes)) {
         $change_name = $this->_db->quoteIdentifier($changes['name']);
         $result = $this->_db->query("ALTER TABLE {$name} RENAME TO " . $change_name);
         if (PEAR::isError($result)) {
             return $result;
         }
     }
     return DB_OK;
 }
Beispiel #12
0
 /**
  *
  * Generates a sequence value; sequence name defaults to the table name.
  * 
  * @access public
  * 
  * @param string $seq_name The sequence name; defaults to table_id.
  * 
  * @return integer The next value in the sequence.
  *
  * @see DB::nextID()
  * 
  * @see MDB2::nextID()
  *
  */
 function nextID($seq_name = null)
 {
     if (is_null($seq_name)) {
         $seq_name = "{$this->table}";
     } else {
         $seq_name = "{$this->table}_{$seq_name}";
     }
     // the maximum length is 30, but PEAR DB/MDB2 will add "_seq" to the
     // name, so the max length here is less 4 chars. we have to
     // check here because the sequence will be created automatically
     // by PEAR DB/MDB2, which will not check for length on its own.
     if (strlen($seq_name) > 26) {
         return DB_Table::throwError(DB_TABLE_ERR_SEQ_STRLEN, " ('{$seq_name}')");
     }
     return $this->db->nextId($seq_name);
 }
Beispiel #13
0
 /**
  * alter an existing table
  *
  * @param string $name         name of the table that is intended to be changed.
  * @param array $changes     associative array that contains the details of each type
  *                             of change that is intended to be performed. The types of
  *                             changes that are currently supported are defined as follows:
  *
  *                             name
  *
  *                                New name for the table.
  *
  *                            add
  *
  *                                Associative array with the names of fields to be added as
  *                                 indexes of the array. The value of each entry of the array
  *                                 should be set to another associative array with the properties
  *                                 of the fields to be added. The properties of the fields should
  *                                 be the same as defined by the Metabase parser.
  *
  *
  *                            remove
  *
  *                                Associative array with the names of fields to be removed as indexes
  *                                 of the array. Currently the values assigned to each entry are ignored.
  *                                 An empty array should be used for future compatibility.
  *
  *                            rename
  *
  *                                Associative array with the names of fields to be renamed as indexes
  *                                 of the array. The value of each entry of the array should be set to
  *                                 another associative array with the entry named name with the new
  *                                 field name and the entry named Declaration that is expected to contain
  *                                 the portion of the field declaration already in DBMS specific SQL code
  *                                 as it is used in the CREATE TABLE statement.
  *
  *                            change
  *
  *                                Associative array with the names of the fields to be changed as indexes
  *                                 of the array. Keep in mind that if it is intended to change either the
  *                                 name of a field and any other properties, the change array entries
  *                                 should have the new names of the fields as array indexes.
  *
  *                                The value of each entry of the array should be set to another associative
  *                                 array with the properties of the fields to that are meant to be changed as
  *                                 array entries. These entries should be assigned to the new values of the
  *                                 respective properties. The properties of the fields should be the same
  *                                 as defined by the Metabase parser.
  *
  *                            Example
  *                                array(
  *                                    'name' => 'userlist',
  *                                    'add' => array(
  *                                        'quota' => array(
  *                                            'type' => 'integer',
  *                                            'unsigned' => 1
  *                                        )
  *                                    ),
  *                                    'remove' => array(
  *                                        'file_limit' => array(),
  *                                        'time_limit' => array()
  *                                    ),
  *                                    'change' => array(
  *                                        'name' => array(
  *                                            'length' => '20',
  *                                            'definition' => array(
  *                                                'type' => 'text',
  *                                                'length' => 20,
  *                                            ),
  *                                        )
  *                                    ),
  *                                    'rename' => array(
  *                                        'sex' => array(
  *                                            'name' => 'gender',
  *                                            'definition' => array(
  *                                                'type' => 'text',
  *                                                'length' => 1,
  *                                                'default' => 'M',
  *                                            ),
  *                                        )
  *                                    )
  *                                )
  *
  * @param boolean $check     (ignored in DB_Table)
  * @access public
  *
  * @return mixed DB_OK on success, a PEAR error on failure
  */
 function alterTable($name, $changes, $check)
 {
     foreach ($changes as $change_name => $change) {
         switch ($change_name) {
             case 'add':
             case 'remove':
             case 'rename':
             case 'change':
                 break;
             default:
                 return DB_Table::throwError(DB_TABLE_ERR_ALTER_TABLE_IMPOS);
         }
     }
     $query = '';
     if (array_key_exists('add', $changes)) {
         foreach ($changes['add'] as $field_name => $field) {
             if ($query) {
                 $query .= ', ';
             }
             $query .= 'ADD ' . $field_name . ' ' . $field;
         }
     }
     if (array_key_exists('remove', $changes)) {
         foreach ($changes['remove'] as $field_name => $field) {
             if ($query) {
                 $query .= ', ';
             }
             $field_name = $this->_db->quoteIdentifier($field_name);
             $query .= 'DROP ' . $field_name;
         }
     }
     if (array_key_exists('rename', $changes)) {
         foreach ($changes['rename'] as $field_name => $field) {
             if ($query) {
                 $query .= ', ';
             }
             $field_name = $this->_db->quoteIdentifier($field_name);
             $query .= 'ALTER ' . $field_name . ' TO ' . $this->_db->quoteIdentifier($field['name']);
         }
     }
     if (array_key_exists('change', $changes)) {
         // missing support to change DEFAULT and NULLability
         foreach ($changes['change'] as $field_name => $field) {
             if ($query) {
                 $query .= ', ';
             }
             $field_name = $this->_db->quoteIdentifier($field_name);
             $query .= 'ALTER ' . $field_name . ' TYPE ' . $field['definition'];
         }
     }
     if (!strlen($query)) {
         return DB_OK;
     }
     $name = $this->_db->quoteIdentifier($name);
     $result = $this->_db->query("ALTER TABLE {$name} {$query}");
     return $result;
 }
Beispiel #14
0
 /**
  * alter an existing table
  *
  * @param string $name         name of the table that is intended to be changed.
  * @param array $changes     associative array that contains the details of each type
  *                             of change that is intended to be performed. The types of
  *                             changes that are currently supported are defined as follows:
  *
  *                             name
  *
  *                                New name for the table.
  *
  *                            add
  *
  *                                Associative array with the names of fields to be added as
  *                                 indexes of the array. The value of each entry of the array
  *                                 should be set to another associative array with the properties
  *                                 of the fields to be added. The properties of the fields should
  *                                 be the same as defined by the Metabase parser.
  *
  *
  *                            remove
  *
  *                                Associative array with the names of fields to be removed as indexes
  *                                 of the array. Currently the values assigned to each entry are ignored.
  *                                 An empty array should be used for future compatibility.
  *
  *                            rename
  *
  *                                Associative array with the names of fields to be renamed as indexes
  *                                 of the array. The value of each entry of the array should be set to
  *                                 another associative array with the entry named name with the new
  *                                 field name and the entry named Declaration that is expected to contain
  *                                 the portion of the field declaration already in DBMS specific SQL code
  *                                 as it is used in the CREATE TABLE statement.
  *
  *                            change
  *
  *                                Associative array with the names of the fields to be changed as indexes
  *                                 of the array. Keep in mind that if it is intended to change either the
  *                                 name of a field and any other properties, the change array entries
  *                                 should have the new names of the fields as array indexes.
  *
  *                                The value of each entry of the array should be set to another associative
  *                                 array with the properties of the fields to that are meant to be changed as
  *                                 array entries. These entries should be assigned to the new values of the
  *                                 respective properties. The properties of the fields should be the same
  *                                 as defined by the Metabase parser.
  *
  *                            Example
  *                                array(
  *                                    'name' => 'userlist',
  *                                    'add' => array(
  *                                        'quota' => array(
  *                                            'type' => 'integer',
  *                                            'unsigned' => 1
  *                                        )
  *                                    ),
  *                                    'remove' => array(
  *                                        'file_limit' => array(),
  *                                        'time_limit' => array()
  *                                    ),
  *                                    'change' => array(
  *                                        'name' => array(
  *                                            'length' => '20',
  *                                            'definition' => array(
  *                                                'type' => 'text',
  *                                                'length' => 20,
  *                                            ),
  *                                        )
  *                                    ),
  *                                    'rename' => array(
  *                                        'sex' => array(
  *                                            'name' => 'gender',
  *                                            'definition' => array(
  *                                                'type' => 'text',
  *                                                'length' => 1,
  *                                                'default' => 'M',
  *                                            ),
  *                                        )
  *                                    )
  *                                )
  *
  * @param boolean $check     (ignored in DB_Table)
  * @access public
  *
  * @return mixed DB_OK on success, a PEAR error on failure
  */
 function alterTable($name, $changes, $check)
 {
     foreach ($changes as $change_name => $change) {
         switch ($change_name) {
             case 'add':
             case 'remove':
             case 'change':
             case 'name':
             case 'rename':
                 break;
             default:
                 return DB_Table::throwError(DB_TABLE_ERR_ALTER_TABLE_IMPOS);
         }
     }
     $name = $this->_db->quoteIdentifier($name);
     if (array_key_exists('add', $changes)) {
         $fields = array();
         foreach ($changes['add'] as $field_name => $field) {
             $fields[] = $field_name . ' ' . $field;
         }
         $result = $this->_db->query("ALTER TABLE {$name} ADD (" . implode(', ', $fields) . ')');
         if (PEAR::isError($result)) {
             return $result;
         }
     }
     if (array_key_exists('change', $changes)) {
         $fields = array();
         foreach ($changes['change'] as $field_name => $field) {
             $fields[] = $field_name . ' ' . $field['definition'];
         }
         $result = $this->_db->query("ALTER TABLE {$name} MODIFY (" . implode(', ', $fields) . ')');
         if (PEAR::isError($result)) {
             return $result;
         }
     }
     if (array_key_exists('rename', $changes)) {
         foreach ($changes['rename'] as $field_name => $field) {
             $field_name = $this->_db->quoteIdentifier($field_name);
             $query = "ALTER TABLE {$name} RENAME COLUMN {$field_name} TO " . $this->_db->quoteIdentifier($field['name']);
             $result = $this->_db->query($query);
             if (PEAR::isError($result)) {
                 return $result;
             }
         }
     }
     if (array_key_exists('remove', $changes)) {
         $fields = array();
         foreach ($changes['remove'] as $field_name => $field) {
             $fields[] = $this->_db->quoteIdentifier($field_name);
         }
         $result = $this->_db->query("ALTER TABLE {$name} DROP COLUMN " . implode(', ', $fields));
         if (PEAR::isError($result)) {
             return $result;
         }
     }
     if (array_key_exists('name', $changes)) {
         $change_name = $this->_db->quoteIdentifier($changes['name']);
         $result = $this->_db->query("ALTER TABLE {$name} RENAME TO " . $change_name);
         if (PEAR::isError($result)) {
             return $result;
         }
     }
     return DB_OK;
 }
Beispiel #15
0
 /**
  * 
  * Get the column declaration string for a DB_Table column.
  * 
  * @static
  * 
  * @access public
  * 
  * @param string $phptype The DB phptype key.
  * 
  * @param string $coltype The DB_Table column type.
  * 
  * @param int $size The size for the column (needed for string and
  * decimal).
  * 
  * @param int $scope The scope for the column (needed for decimal).
  * 
  * @param bool $require True if the column should be NOT NULL, false
  * allowed to be NULL.
  * 
  * @param string $default The SQL calculation for a default value.
  * 
  * @return string|object A declaration string on success, or a
  * PEAR_Error on failure.
  * 
  */
 function getDeclare($phptype, $coltype, $size = null, $scope = null, $require = null, $default = null)
 {
     // validate char and varchar: does it have a size?
     if (($coltype == 'char' || $coltype == 'varchar') && ($size < 1 || $size > 255)) {
         return DB_Table::throwError(DB_TABLE_ERR_DECLARE_STRING, "(size='{$size}')");
     }
     // validate decimal: does it have a size and scope?
     if ($coltype == 'decimal' && ($size < 1 || $size > 255 || $scope < 0 || $scope > $size)) {
         return DB_Table::throwError(DB_TABLE_ERR_DECLARE_DECIMAL, "(size='{$size}' scope='{$scope}')");
     }
     // map of column types and declarations for this RDBMS
     $map = $GLOBALS['_DB_TABLE']['type'][$phptype];
     // is it a recognized column type?
     $types = array_keys($map);
     if (!in_array($coltype, $types)) {
         return DB_Table::throwError(DB_TABLE_ERR_DECLARE_TYPE, "('{$coltype}')");
     }
     // basic declaration
     switch ($coltype) {
         case 'char':
         case 'varchar':
             $declare = $map[$coltype] . "({$size})";
             break;
         case 'decimal':
             $declare = $map[$coltype] . "({$size},{$scope})";
             break;
         default:
             $declare = $map[$coltype];
             break;
     }
     // set the "NULL"/"NOT NULL" portion
     $declare .= $require ? ' NOT NULL' : ' NULL';
     // set the "DEFAULT" portion
     $declare .= $default ? " DEFAULT {$default}" : '';
     // done
     return $declare;
 }