Gets the database character set of the given table

@param   string       $table  The table for which to retrieve the character set
@param   string       $field  The field to check (optional)
@return  string|bool
@since   2.0.0
777450
public function getCharacterSet($table, $field = null)
	{
		$create = $this->getTableCreate($table);

		if (isset($field))
		{
			preg_match('/' . $this->quoteName($field) . ' [[:alnum:]\(\)]* CHARACTER SET ([[:alnum:]]*)/', $create[$table], $matches);
		}
		else
		{
			preg_match('/CHARSET=([[:alnum:]]*)/', $create[$table], $matches);
		}

		return (isset($matches[1])) ? $matches[1] : false;
	}
535098
public String getStr(String field, Charset charset) {
		final Object obj = get(field);
		if (obj instanceof Clob) {
			return SqlUtil.clobToStr((Clob) obj);
		} else if (obj instanceof Blob) {
			return SqlUtil.blobToStr((Blob) obj, charset);
		} else if (obj instanceof RowId) {
			final RowId rowId = (RowId) obj;
			return StrUtil.str(rowId.getBytes(), charset);
		}
		return super.getStr(field);
	}
341537
final public function fetchColumnDefs(string $table) {
        $tableKey = strtolower($table);

        if (!empty($this->tables[$tableKey]['columns'])) {
            $this->tables[$tableKey]['columns'];
        } elseif ($this->tableNames !== null && !isset($this->tableNames[$tableKey])) {
            return null;
        }

        $columnDefs = $this->fetchColumnDefsDb($table);
        if ($columnDefs !== null) {
            $this->tables[$tableKey]['columns'] = $columnDefs;
        }
        return $columnDefs;
    }
684982
public function getField(string $field)
    {
        if (isset($this->_record[$field]))
            return $this->_record[$field];
        return null;
    }
538381
public function isFieldExists($table, $field)
	{
		if (isset($this->tables[$table])) {
			$fields = $this->getFields($table);

			return $fields->where('name', $field)->count() > 0;
		}

		return false;
	}
499095
private function getFields($table)
    {
        $fields = $this->entity->getFieldNames();
        if (empty($this->with) && $this->alias === null) {
            return $fields;
        }

        $table = $this->getTableAlias($table);

        $prefixIfMissing = function ($field) use ($table) {
            if (strpos($field, '.') !== false) {
                return $field;
            }

            return $table . '.' . $field;
        };

        return array_map($prefixIfMissing, $fields);
    }
1032286
public function fieldExists($field, $table) {
        $fieldLists = $this->query("DESC " . $this->getPrefix() . $table);
        foreach ($fieldLists as $f) {
            if (strtolower($f['Field']) == strtolower($field)) {
                return TRUE;
            }
        }
        return FALSE;
    }
629136
public function getClobTypeDeclarationSQL(array $field)
    {
        if (! empty($field['length']) && is_numeric($field['length'])) {
            $length = $field['length'];

            if ($length <= static::LENGTH_LIMIT_TINYTEXT) {
                return 'TINYTEXT';
            }

            if ($length <= static::LENGTH_LIMIT_TEXT) {
                return 'TEXT';
            }

            if ($length <= static::LENGTH_LIMIT_MEDIUMTEXT) {
                return 'MEDIUMTEXT';
            }
        }

        return 'LONGTEXT';
    }
629351
protected function check($table) {
		if ($this->quoteCharacter && strpos($table, $this->quoteCharacter)!==false) {
		  throw new Redbean_Exception_Security('Illegal chars in table name');
	    }
		return $table;
	}
82451
public function list_fields($table)
	{
		// Is there a cached result?
		if (isset($this->data_cache['field_names'][$table]))
		{
			return $this->data_cache['field_names'][$table];
		}

		if (($result = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE)
		{
			return FALSE;
		}

		$this->data_cache['field_names'][$table] = array();
		foreach ($result->result_array() as $row)
		{
			$this->data_cache['field_names'][$table][] = $row['name'];
		}

		return $this->data_cache['field_names'][$table];
	}
917197
protected static function resolve_field($field, $table) {
        $prefix = constant(__CLASS__ . '::ALIAS_' . core_text::strtoupper($table));
        return sprintf('%s.%s', $prefix, $field);
    }
346326
public function FetchColumnSql($Table) {
      if ($Table[0] != '`' && !StringBeginsWith($Table, $this->Database->DatabasePrefix))
         $Table = $this->Database->DatabasePrefix.$Table;
      
      return "show columns from ".$this->FormatTableName($Table);
   }
609834
public function getColumns($table)
    {
        $table = str_replace("`", "", $table);

        $columns = Db::fetchAll("SHOW COLUMNS FROM `" . $table . "`");

        $columnNames = array();
        foreach ($columns as $column) {
            $columnNames[] = $column['Field'];
        }

        return $columnNames;
    }
1095254
final public function fetchTableDef(string $table) {
        $tableKey = strtolower($table);

        // First check the table cache.
        if (isset($this->tables[$tableKey])) {
            $tableDef = $this->tables[$tableKey];

            if (isset($tableDef['columns'], $tableDef['indexes'])) {
                return $tableDef;
            }
        } elseif ($this->tableNames !== null && !isset($this->tableNames[$tableKey])) {
            return null;
        }

        $tableDef = $this->fetchTableDefDb($table);
        if ($tableDef !== null) {
            $this->fixIndexes($tableDef['name'], $tableDef);
            $this->tables[$tableKey] = $tableDef;
        }

        return $tableDef;
    }
1185212
private function getFieldCreate($field)
    {
        $res = '';

        switch ($field['type']) {
            case self::FIELD_TYPE_VARCHAR_255:
                // Simple text input
                if (!isset($field['length'])) {
                    $field['length'] = '255';
                }
                $res = '`'. $field['name'] .'` varchar('. $field['length'] .') NULL '.(isset($field['default_value']) ? ' DEFAULT "' . $field['default_value'] . '"' : '').'';
                break;

            case self::FIELD_TYPE_CHAR:
                // Codes
                if (!isset($field['length'])) {
                    dump('Length for "'. $field['name'] .'" required');
                }
                $res = '`'. $field['name'] .'` char('. $field['length'] .') NULL';
                break;

            case self::FIELD_TYPE_TEXT:
                // Large textares
                $res = '`'. $field['name'] .'` text NULL';
                break;

            case 'mediumtext':
                // Large textares
                $res = '`'. $field['name'] .'` mediumtext NULL';
                break;

            case 'int':
                // Digit
                $unsigned = isset($field['unsigned']) && $field['unsigned'];
                if (!isset($field['length'])) {
                    $field['length'] = 11;
                    if ($unsigned) {
                        $field['length'] = 10;
                    }
                }
                $res = '`'. $field['name'] .'` int('. $field['length'] .') '. ($unsigned ? ' unsigned ' : '') . (isset($field['null']) && !$field['null'] ? ' NOT NULL' : ((isset($field['auto_increment']) ? '' : ' DEFAULT "'. (isset($field['default_value']) ? $field['default_value'] : '0') .'"') . ' NULL'));
                break;

            case self::FIELD_TYPE_UNSIGNED_INTEGER:
            case 'ts':
            case 'order':
                // Digit
                $res = '`'. $field['name'] .'` int(10) unsigned NULL DEFAULT "0"';
                break;

            case self::FIELD_TYPE_INDEX:
                $res = '`'. $field['name'] .'` int(10) unsigned DEFAULT "0"';

                // Add index if not exists
                if (!isset($this->table_structure['indexes'][$field['name']])) {
                    $this->table_structure['indexes'][$field['name']] = [
                        'type' => 'key',
                    ];
                }
                break;

            case self::FIELD_TYPE_TRANSLATION:
                // Int index with comment
                $res = '`'. $field['name'] .'` int(10) unsigned NULL DEFAULT "0"';
                $field['comment'] = self::FIELD_TYPE_TRANSLATION;
                break;

            case 'current_timestamp':
                // Dates
                $res = '`'. $field['name'] .'` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP';
                break;

            case 'date':
            case 'datetime':
                // Default : CURRENT_TIMESTAMP
                $res = '`'. $field['name'] .'` datetime NOT NULL';
                break;

            case 'checkbox':
            case 'active':
            case self::FIELD_TYPE_BOOL:
                // True or false, 0 | 1
                $res = '`'. $field['name'] .'` tinyint(1) unsigned NULL DEFAULT "'. (isset($field['default_value']) ? $field['default_value'] : '0') .'"';
                break;

            case self::FIELD_TYPE_FLOAT_DECIMAL:
            case 'decimal': // TODO REMOVE THIS 'decimal'
                // Decimal
                $unsigned = isset($field['unsigned']) && $field['unsigned'];
                if (!isset($field['length'])) {
                    $field['length'] = 8.2;
                }
                // Convert to db format
                $field['length'] = str_replace('.', ',', $field['length']);

                $res = '`'. $field['name'] .'` decimal('. $field['length'] .') '. ($unsigned ? ' unsigned ' : '') .' NULL DEFAULT "0"';
                break;

            case 'enum':
            case 'array':
                if (empty($field['options'])) {
                    dump('Param "options" must be set for field "enum" and have filled elements');
                }

                // If associative array supplied - we need only keys
                if (!isset($field['options'][0])) {
                    $field['options'] = array_keys($field['options']);
                }

                $res = '`'. $field['name'] .'` enum("'. implode('","', $field['options']) .'") NULL '. (isset($field['default_value']) ? ' DEFAULT "' . $field['default_value'] . '"' : '') .'';
                break;

            case 'json':

                $res = '`' . $field['name'] . '` JSON NULL ' . (isset($field['default_value']) ? ' DEFAULT "' . $field['default_value'] . '"' : '') . '';
                break;

            default:
                dump('Type "'. $field['type'] .'" not found in TableStructure');
        }

        if (isset($field['auto_increment'])) {
            $res .= ' AUTO_INCREMENT';
        }

        if (isset($field['comment'])) {
            $res .= ' COMMENT "'. $field['comment'] .'"';
        }

        return $res;
    }
691263
public function getTableKeys($table)
	{
		$this->connect();

		/** @var OracleQuery $query */
		$query = $this->getQuery(true);

		$fieldCasing = $this->getOption(\PDO::ATTR_CASE);

		$this->setOption(\PDO::ATTR_CASE, \PDO::CASE_UPPER);

		$table = strtoupper($table);
		$query->select('*');
		$query->from('ALL_CONSTRAINTS');
		$query->where('table_name = :tableName');

		$query->bind(':tableName', $table);

		$this->setQuery($query);
		$keys = $this->loadObjectList();

		$this->setOption(\PDO::ATTR_CASE, $fieldCasing);

		return $keys;
	}
537260
public function getSinglelangFields($table, $lang)
    {
        $langTable = getLangTableName($table, $lang);

        $baseFields = $this->getFields($table);
        $langFields = $this->getFields($langTable);

        //Some fields (for example OXID) must be taken from core table.
        $langFields = $this->filterCoreFields($langFields);

        $fields = array_merge($baseFields, $langFields);
        $singleLangFields = [];

        foreach ($fields as $fieldName => $field) {
            if (preg_match("/(({$table}|{$langTable})\.)?(?<field>.+)_(?<lang>[0-9]+)$/", $field, $matches)) {
                if ($matches['lang'] == $lang) {
                    $singleLangFields[$matches['field']] = $field;
                }
            } else {
                $singleLangFields[$fieldName] = $field;
            }
        }

        return $singleLangFields;
    }