'启用',2=>'停用']; } public function getStatusTxtAttr($value,$row) { $statusList = self::statusList(); return $statusList[$value ?? $row['status']] ?? ''; } static public function engineList() { return ['MyISAM','InnoDB']; } public function fields() { return $this->hasMany(ModelFields::class,'name','model_name'); } /** * @title: 获取所有数据表 * @desc: 描述 * @return {*} * @author: Rock * @method: POST * @Date: 2023-07-26 10:20:58 */ static public function getTableList():array { $sql = "SHOW TABLE STATUS"; return Db::query($sql); } /** * @title: 检查表是否存在 * @desc: 描述 * @param {string} {name} {} {待检查的表名} * @return {boolean} {} {} {true存在,false不存在} * @author: Rock * @method: POST * @Date: 2023-04-17 08:16:41 */ static public function checkTable(string $name):bool { $database = config('database.connections.mysql.database'); $sql = "SELECT * FROM information_schema.TABLES WHERE TABLE_NAME = '$name' AND TABLE_SCHEMA='$database'"; $res = Db::query($sql); return $res && count($res) > 0; } /** * @title: 获取数据表主键 * @desc: 描述 * @param {string} {table} {} {表名} * @return {*} * @author: Rock * @method: POST * @Date: 2023-07-26 10:08:30 */ static public function pk(string $table):string { $fieldList = self::getFieldList($table); foreach($fieldList as $item){ if('PRI'==$item['Key']){ return $item['Field']; } } return ''; } /** * @title: 获取数据表注释 * @desc: 描述 * @param {string} {table} {} {表名} * @return {*} * @author: Rock * @method: POST * @Date: 2023-07-26 10:14:13 */ static public function getComment(string $table):string { $database = config('database.connections.mysql.database'); $sql = "SELECT TABLE_COMMENT FROM information_schema.TABLES WHERE table_name = '$table' AND TABLE_SCHEMA='$database'"; $res = Db::query($sql); return $res?$res[0]['TABLE_COMMENT']:''; } /** * @title: 获取数据表字段 * @desc: 描述 * @param {string} {table} {} {表名]} * @return {*} * @author: Rock * @method: POST * @Date: 2023-07-26 10:15:35 */ static public function getFieldList(string $table):array { $sql = "SHOW FULL COLUMNS FROM `$table`"; return Db::query($sql); } /** * @title: 创建表 * @desc: 描述 * @return {*} * @author: Rock * @method: POST * @Date: 2023-04-14 09:08:20 */ static public function createTable(int $id) { $info = self::find($id); $tableName = $info->getAttr('name'); $title = $info->title; $engine = $info->engine; $sql = <<getAttr('name'); $engine = $info->getAttr('engine'); $title = $info->getAttr('title'); $sql = "ALTER TABLE $oldname RENAME $name" ; $res = Db::execute($sql); $sql = "ALTER TABLE $name ENGINE = $engine COMMENT = '$title'"; $res = Db::execute($sql); return $res; } /** * @title: 删除表 * @desc: 描述 * @param {int} {id} {} {模型ID} * @return {*} * @author: Rock * @method: POST * @Date: 2023-04-14 09:08:47 */ static public function deleteTable(int $id) { $info = self::find($id); $name = $info->getAttr('name'); $sql = "DROP TABLE IF EXISTS $name"; $res = Db::execute($sql); return $res; } /** * @title: 复制表 * @desc: 描述 * @param {int} $id * @return {*} * @author: Rock * @method: POST * @Date: 2023-04-27 14:37:08 */ static public function copyTable(int $id) { $info = self::find($id); $name = $info->getAttr('name'); $title = $info->getAttr('title'); $sql = "SHOW CREATE TABLE $name"; $tableSql = Db::query($sql); if($tableSql){ $sql = $tableSql[0]['Create Table']; $sql = str_replace("TABLE `$name`","TABLE `{$name}_copy`",$sql); $sql = str_replace("COMMENT='$title'","COMMENT='{$title}副本'",$sql); Db::execute($sql); return true; } return false; } /** * @title: 获取英文的驼峰写法 * @desc: 描述 * @param {string} $name * @return {*} * @author: Rock * @method: POST * @Date: 2023-04-25 11:46:37 */ static public function getCamel(string $name) { $arr = explode('_',$name); $arr = array_filter($arr); $classname = ""; foreach($arr as $item){ $classname .= ucfirst($item); } return $classname; } }