/** * get the stucture of a field into an array * * @param object $db database object that is extended by this class * @param string $table name of table that should be used in method * @param string $field_name name of field that should be used in method * @return mixed data array on success, a MDB error on failure * @access public */ function getTableFieldDefinition(&$db, $table, $field_name) { if ($field_name == $db->dummy_primary_key) { return $db->raiseError(MDB_ERROR_MANAGER, NULL, NULL, 'Get table field definiton: ' . $db->dummy_primary_key . ' is an hidden column'); } $result = $db->query("SHOW COLUMNS FROM {$table}"); if (MDB::isError($result)) { return $result; } $columns = $db->getColumnNames($result); if (MDB::isError($columns)) { $db->freeResult($columns); return $columns; } if ($db->options['optimize'] != 'portability') { $columns = array_change_key_case($columns); } if (!isset($columns[$column = 'field']) || !isset($columns[$column = 'type'])) { $db->freeResult($result); return $db->raiseError(MDB_ERROR_MANAGER, NULL, NULL, 'Get table field definition: show columns does not return the column ' . $column); } $field_column = $columns['field']; $type_column = $columns['type']; while (is_array($row = $db->fetchInto($result))) { if ($field_name == $row[$field_column]) { $db_type = strtolower($row[$type_column]); $db_type = strtok($db_type, '(), '); if ($db_type == 'national') { $db_type = strtok('(), '); } $length = strtok('(), '); $decimal = strtok('(), '); $type = array(); switch ($db_type) { case 'tinyint': case 'smallint': case 'mediumint': case 'int': case 'integer': case 'bigint': $type[0] = 'integer'; if ($length == '1') { $type[1] = 'boolean'; if (preg_match('/^[is|has]/', $field_name)) { $type = array_reverse($type); } } break; case 'tinytext': case 'mediumtext': case 'longtext': case 'text': case 'char': case 'varchar': $type[0] = 'text'; if ($decimal == 'binary') { $type[1] = 'blob'; } elseif ($length == '1') { $type[1] = 'boolean'; if (preg_match('/[is|has]/', $field_name)) { $type = array_reverse($type); } } elseif (strstr($db_type, 'text')) { $type[1] = 'clob'; } break; case 'enum': preg_match_all('/\'.+\'/U', $row[$type_column], $matches); $length = 0; if (is_array($matches)) { foreach ($matches[0] as $value) { $length = max($length, strlen($value) - 2); } } unset($decimal); case 'set': $type[0] = 'text'; $type[1] = 'integer'; break; case 'date': $type[0] = 'date'; break; case 'datetime': case 'timestamp': $type[0] = 'timestamp'; break; case 'time': $type[0] = 'time'; break; case 'float': case 'double': case 'real': $type[0] = 'float'; break; case 'decimal': case 'numeric': $type[0] = 'decimal'; break; case 'tinyblob': case 'mediumblob': case 'longblob': case 'blob': $type[0] = 'blob'; $type[1] = 'text'; break; case 'year': $type[0] = 'integer'; $type[1] = 'date'; break; default: return $db->raiseError(MDB_ERROR_MANAGER, NULL, NULL, 'List table fields: unknown database attribute type'); } unset($notnull); if (isset($columns['null']) && $row[$columns['null']] != 'YES') { $notnull = 1; } unset($default); if (isset($columns['default']) && isset($row[$columns['default']])) { $default = $row[$columns['default']]; } $definition = $field_choices = array(); for ($datatype = 0; $datatype < count($type); $datatype++) { $field_choices[$datatype] = array('type' => $type[$datatype]); if (isset($notnull)) { $field_choices[$datatype]['notnull'] = 1; } if (isset($default)) { $field_choices[$datatype]['default'] = $default; } if ($type[$datatype] != 'boolean' && $type[$datatype] != 'time' && $type[$datatype] != 'date' && $type[$datatype] != 'timestamp') { if (strlen($length)) { $field_choices[$datatype]['length'] = $length; } } } $definition[0] = $field_choices; if (isset($columns['extra']) && isset($row[$columns['extra']]) && $row[$columns['extra']] == 'auto_increment') { $implicit_sequence = array(); $implicit_sequence['on'] = array(); $implicit_sequence['on']['table'] = $table; $implicit_sequence['on']['field'] = $field_name; $definition[1]['name'] = $table . '_' . $field_name; $definition[1]['definition'] = $implicit_sequence; } if (isset($columns['key']) && isset($row[$columns['key']]) && $row[$columns['key']] == 'PRI') { // check that its not just a unique field $query = "SHOW INDEX FROM {$table}"; $indexes = $db->queryAll($query, NULL, MDB_FETCHMODE_ASSOC); if (MDB::isError($indexes)) { return $indexes; } $is_primary = FALSE; foreach ($indexes as $index) { if ($db->options['optimize'] != 'portability') { array_change_key_case($index); } if ($index['key_name'] == 'PRIMARY' && $index['column_name'] == $field_name) { $is_primary = TRUE; break; } } if ($is_primary) { $implicit_index = array(); $implicit_index['unique'] = 1; $implicit_index['FIELDS'][$field_name] = ''; $definition[2]['name'] = $field_name; $definition[2]['definition'] = $implicit_index; } } $db->freeResult($result); return $definition; } } if (!$db->options['autofree']) { $db->freeResult($result); } if (MDB::isError($row)) { return $row; } return $db->raiseError(MDB_ERROR_MANAGER, NULL, NULL, 'Get table field definition: it was not specified an existing table column'); }
/** * list all fields in a tables in the current database * * @param object $db database object that is extended by this class * @param string $table name of table that should be used in method * @return mixed data array on success, a MDB error on failure * @access public */ function listTableFields(&$db, $table) { $result = $db->query("SELECT * FROM {$table}"); if (MDB::isError($result)) { return $result; } $columns = $db->getColumnNames($result); if (MDB::isError($columns)) { $db->freeResult($columns); } return array_flip($columns); }
/** * list all fields in a tables in the current database * * @param object $db database object that is extended by this class * @param string $table name of table that should be used in method * @return mixed data array on success, a MDB error on failure * @access public */ function listTableFields(&$db, $table) { $result = $db->query("SELECT RDB{$FIELD_SOURCE} FROM RDB{$RELATION_FIELDS} WHERE RDB{$RELATION_NAME}='{$table}'"); if (MDB::isError($result)) { return $result; } $columns = $db->getColumnNames($result); if (MDB::isError($columns)) { $db->freeResult($columns); } return $columns; }