DB\SQL::schema PHP Method

schema() public method

Retrieve schema of SQL table
public schema ( $table, $fields = NULL, $ttl ) : array | FALSE
$table string
$fields array|string
$ttl int|array
return array | FALSE
    function schema($table, $fields = NULL, $ttl = 0)
    {
        if (strpos($table, '.')) {
            list($schema, $table) = explode('.', $table);
        }
        // Supported engines
        $cmd = ['sqlite2?' => ['PRAGMA table_info("' . $table . '")', 'name', 'type', 'dflt_value', 'notnull', 0, 'pk', TRUE], 'mysql' => ['SHOW columns FROM `' . $this->dbname . '`.`' . $table . '`', 'Field', 'Type', 'Default', 'Null', 'YES', 'Key', 'PRI'], 'mssql|sqlsrv|sybase|dblib|pgsql|odbc' => ['SELECT ' . 'c.column_name AS field,' . 'c.data_type AS type,' . 'c.column_default AS defval,' . 'c.is_nullable AS nullable,' . 't.constraint_type AS pkey ' . 'FROM information_schema.columns AS c ' . 'LEFT OUTER JOIN ' . 'information_schema.key_column_usage AS k ' . 'ON ' . 'c.table_name=k.table_name AND ' . 'c.column_name=k.column_name AND ' . 'c.table_schema=k.table_schema ' . ($this->dbname ? 'AND c.table_catalog=k.table_catalog ' : '') . 'LEFT OUTER JOIN ' . 'information_schema.table_constraints AS t ON ' . 'k.table_name=t.table_name AND ' . 'k.constraint_name=t.constraint_name AND ' . 'k.table_schema=t.table_schema ' . ($this->dbname ? 'AND k.table_catalog=t.table_catalog ' : '') . 'WHERE ' . 'c.table_name=' . $this->quote($table) . ($this->dbname ? ' AND c.table_catalog=' . $this->quote($this->dbname) : ''), 'field', 'type', 'defval', 'nullable', 'YES', 'pkey', 'PRIMARY KEY'], 'oci' => ['SELECT c.column_name AS field, ' . 'c.data_type AS type, ' . 'c.data_default AS defval, ' . 'c.nullable AS nullable, ' . '(SELECT t.constraint_type ' . 'FROM all_cons_columns acc ' . 'LEFT OUTER JOIN all_constraints t ' . 'ON acc.constraint_name=t.constraint_name ' . 'WHERE acc.table_name=' . $this->quote($table) . ' ' . 'AND acc.column_name=c.column_name ' . 'AND constraint_type=' . $this->quote('P') . ') AS pkey ' . 'FROM all_tab_cols c ' . 'WHERE c.table_name=' . $this->quote($table), 'FIELD', 'TYPE', 'DEFVAL', 'NULLABLE', 'Y', 'PKEY', 'P']];
        if (is_string($fields)) {
            $fields = \Base::instance()->split($fields);
        }
        foreach ($cmd as $key => $val) {
            if (preg_match('/' . $key . '/', $this->engine)) {
                $rows = [];
                foreach ($this->exec($val[0], NULL, $ttl) as $row) {
                    if (!$fields || in_array($row[$val[1]], $fields)) {
                        $rows[$row[$val[1]]] = ['type' => $row[$val[2]], 'pdo_type' => preg_match('/int\\b|integer/i', $row[$val[2]]) ? \PDO::PARAM_INT : (preg_match('/bool/i', $row[$val[2]]) ? \PDO::PARAM_BOOL : (preg_match('/blob|bytea|image|binary/i', $row[$val[2]]) ? \PDO::PARAM_LOB : (preg_match('/float|decimal|real|numeric|double/i', $row[$val[2]]) ? self::PARAM_FLOAT : \PDO::PARAM_STR))), 'default' => is_string($row[$val[3]]) ? preg_replace('/^\\s*([\'"])(.*)\\1\\s*/', '\\2', $row[$val[3]]) : $row[$val[3]], 'nullable' => $row[$val[4]] == $val[5], 'pkey' => $row[$val[6]] == $val[7]];
                    }
                }
                return $rows;
            }
        }
        user_error(sprintf(self::E_PKey, $table), E_USER_ERROR);
        return FALSE;
    }

Usage Example

Beispiel #1
0
 /**
  *	Instantiate class
  *	@param $db object
  *	@param $table string
  *	@param $fields array|string
  *	@param $ttl int
  **/
 function __construct(\DB\SQL $db, $table, $fields = NULL, $ttl = 60)
 {
     $this->db = $db;
     $this->engine = $db->driver();
     if ($this->engine == 'oci') {
         $table = strtoupper($table);
     }
     $this->source = $table;
     $this->table = $this->db->quotekey($table);
     $this->fields = $db->schema($table, $fields, $ttl);
     $this->reset();
 }
All Usage Examples Of DB\SQL::schema