public function getTableInfo($tableName = '', $fetch = '')
{
if (!$tableName) {
$tableName = $this->getTable();
}
if (is_array($tableName)) {
$tableName = key($tableName) ?: current($tableName);
}
if (strpos($tableName, ',')) {
// 多表不获取字段信息
return false;
} else {
$tableName = $this->parseSqlTable($tableName);
}
list($guid) = explode(' ', $tableName);
if (!isset(self::$info[$guid])) {
if (!strpos($guid, '.')) {
$schema = $this->getConfig('database') . '.' . $guid;
} else {
$schema = $guid;
}
// 读取缓存
if (is_file(RUNTIME_PATH . 'schema/' . $schema . '.php')) {
$info = (include RUNTIME_PATH . 'schema/' . $schema . '.php');
} else {
$info = $this->connection->getFields($guid);
}
$fields = array_keys($info);
$bind = $type = [];
foreach ($info as $key => $val) {
// 记录字段类型
$type[$key] = $val['type'];
$bind[$key] = $this->getFieldBindType($val['type']);
if (!empty($val['primary'])) {
$pk[] = $key;
}
}
if (isset($pk)) {
// 设置主键
$pk = count($pk) > 1 ? $pk : $pk[0];
} else {
$pk = null;
}
self::$info[$guid] = ['fields' => $fields, 'type' => $type, 'bind' => $bind, 'pk' => $pk];
}
return $fetch ? self::$info[$guid][$fetch] : self::$info[$guid];
}