lihang
2023-04-25 3fade6d3b27f5666672bb3af610020367f790bda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.vci.ubcs.ddl.mapper;
 
import com.vci.ubcs.ddl.bo.DdlTableBO;
import com.vci.ubcs.ddl.bo.DdlTableInDataBaseBO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
 
import java.util.List;
 
/**
 * Description: 数据库操作
 *
 * @author LiHang
 * @date 2023/4/24
 */
public interface DdlMapper {
    /**
     * 统计表格中有多少数据
     * @param tableName 表格名称
     * @return 数据的总数
     */
    @Select( "select count(*) from ${tableName}")
    @ResultType(Integer.class)
    int countAll(@Param("tableName")String tableName);
 
    /**
     * 根据SQL语句创建视图
     * @param viewCreateSql
     * @return 影响行数
     */
    @Update("${viewCreateSql}")
    @ResultType(Integer.class)
    int createViewBySql(@Param("viewCreateSql") String viewCreateSql);
 
    /**
     * 根据sql语句插入数据
     * @param insertSql 要执行的sql语句
     * @return 影响的行数
     */
    @Insert("${insertSql}")
    @ResultType(Integer.class)
    int insertBySql(@Param("insertSql")String insertSql);
 
    /**
     * 创建数据库表
     * @param tableName 表格的名称
     * @param attributeSql 属性的sql
     * @return 影响的行数
     */
    @Update("create table ${tableName} ( ${attributeSql} )")
    @ResultType(Integer.class)
    int createTableBySql(@Param("tableName") String tableName, @Param("attributeSql") String attributeSql);
 
    /**
     * 为表格添加注释内容
     * @param tableName 表格的名称
     * @param comment 注释的内容
     * @return 受影响的行数
     */
    @Update("COMMENT ON TABLE ${tableName}  IS '${comment}' ")
    @ResultType(Integer.class)
    int commentTable(@Param("tableName") String tableName, @Param("comment") String comment);
 
    /**
     * 为表格的列添加注释内容
     * @param tableName 表格的名称
     * @param columnName 列
     * @param comment 注释的内容
     * @return 受影响的行数
     */
    @Update("COMMENT ON column ${tableName}.${columnName} IS '${comment}' ")
    @ResultType(Integer.class)
    int commentColumnTable(@Param("tableName") String tableName,@Param("columnName")String columnName, @Param("comment") String comment);
 
    /**
     * 校验数据库表或者视图是否存在
     * @param tableName 表格的名称,不区分大小写
     * @return 存在的个数
     */
    @Select( "select count(table_name) from user_tables where upper(table_name) = upper(#{tableName,jdbcType=VARCHAR})")
    @ResultType(Integer.class)
    int checkTableExist(@Param("tableName") String tableName);
 
    /**
     * 查询表格的字段信息
     * @param tableName 表格的名称
     * @return 数据库表的字段信息
     */
    @Results(id="ddlTableColumnInfo",value = {
        @Result(property = "id",column = "COLUMN_NAME",jdbcType = JdbcType.VARCHAR),
        @Result(property = "name",column = "COMMENTS",jdbcType = JdbcType.VARCHAR),
        @Result(property = "attrDataType",column = "DATA_TYPE",jdbcType = JdbcType.VARCHAR),
        @Result(property = "attributeLength",column = "DATA_LENGTH",jdbcType = JdbcType.DECIMAL),
        @Result(property = "nullableFlag",column = "NULLABLE",jdbcType = JdbcType.VARCHAR),
        @Result(property = "precisionLength",column = "DATA_PRECISION",jdbcType = JdbcType.DECIMAL),
        @Result(property = "scaleLength",column = "DATA_SCALE",jdbcType = JdbcType.DECIMAL)
    })
    @Select("SELECT t.column_name,t.data_type,t.data_length,t.nullable,t.data_precision,t.data_scale,c.comments FROM user_tab_columns t inner JOIN user_col_comments c on t.TABLE_NAME  = c.table_name and t.COLUMN_NAME = c.column_name where t.table_name = upper(#{tableName,jdbcType=VARCHAR}) order by t.column_name asc")
    List<DdlTableInDataBaseBO> selectTableColumnInfo(@Param("tableName") String tableName);
 
    /**
     * 获取所有的表格名称
     * @return 表格名称
     */
    @Select("select table_name from user_tables order by table_name asc")
    List<String> selectAllTableName();
 
    /**
     * 查询表格的中文名称
     * @param tableName 表格名称
     * @return 表格名称和表格中文名称
     */
    @Results(id="ddlTableInfo",value = {
        @Result(property = "tableName",column = "TABLE_NAME",jdbcType = JdbcType.VARCHAR),
        @Result(property = "tableDesc",column = "COMMENTS",jdbcType = JdbcType.VARCHAR)
    })
    @Select("select table_name,comments from user_tab_comments where table_name = upper(#{tableName,jdbcType=VARCHAR}) ")
    DdlTableBO selectTableComment(@Param("tableName")String tableName);
 
    int addColumn2TableBySql(String tableName, String attributeSql);
 
    int modifyTableBySql(String tableName, String attributeSql);
 
    int dropTable(String tableName);
 
    int dropTableColumn(String tableName, String columnName);
}