wangting
2025-01-16 18c43123b51a1688ab4ae01fe3d171c7d92e619b
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package com.vci.server.omd.ddlTool;
 
import java.io.File;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
 
import org.hibernate.Session;
 
import com.vci.corba.common.VCIError;
import com.vci.corba.omd.atm.AttribItem;
import com.vci.server.base.persistence.dao.HibernateSessionFactory;
import com.vci.server.base.utility.AttributeHelper;
 
public final class DDLHelper {
    
    /**
     * 业务类型的系统属性
     */
    private static final String otherFieldBt = "\n\tOID VARCHAR2(36) not null," +
            "\n\tREVISIONOID VARCHAR2(36),\n\tNAMEOID VARCHAR2(36),\n\tBtmName VARCHAR2(36)," +
            "\n\tISLastR CHAR(1),\n\tISFirstR CHAR(1),\n\tISLastV CHAR(1),\n\tISFirstV CHAR(1)," +
            "\n\tCreator VARCHAR2(36)," + 
            "\n\tCreateTime TIMESTAMP,\n\tLastModifier VARCHAR2(36),\n\tLastModifyTime TIMESTAMP," + 
            "\n\tRevisionRule VARCHAR2(36),\n\tVersionRule VARCHAR2(36),\n\tRevisionSeq NUMBER," + 
            "\n\tRevisionValue VARCHAR2(10),\n\tVersionSeq NUMBER,\n\tVersionValue VARCHAR2(10)," + 
            "\n\tLCTID VARCHAR2(36),\n\tLCStatus VARCHAR2(36),\n\tTS TIMESTAMP," +
            "\n\tID VARCHAR2(36),\n\tNAME VARCHAR2(128),\n\tDESCRIPTION VARCHAR2(255)," +
            "\n\tOWNER VARCHAR2(36),\n\tCHECKINBY VARCHAR2(36),\n\tCHECKINTIME TIMESTAMP," +
            "\n\tCHECKOUTBY VARCHAR2(36),\n\tCHECKOUTTIME TIMESTAMP,\n\tCOPYFROMVERSION VARCHAR2(36),\n\t";
    
 
    private static final String otherFieldLt = "\n\tOID VARCHAR2(36) not null," + 
            "\n\tCreator VARCHAR2(36),\n\tCreateTime TIMESTAMP,\n\tLastModifier VARCHAR2(36)," +
            "\n\tLastModifyTime TIMESTAMP,\n\tF_OID VARCHAR2(36) not null,\n\tF_REVISIONOID VARCHAR2(36)," + 
            "\n\tF_NAMEOID VARCHAR2(36),\n\tF_BtwName VARCHAR2(36),\n\tT_OID VARCHAR2(36) not null,\n\tT_REVISIONOID VARCHAR2(36)," + 
            "\n\tT_NAMEOID VARCHAR2(36),\n\tT_BtwName VARCHAR2(36),\n\tTS TIMESTAMP,\n\t";
 
 
    
    public static boolean executeSql(String sql) throws Throwable{
        Session session = HibernateSessionFactory.getSession();
        try{
            session.createSQLQuery(sql).executeUpdate();
        }catch(Throwable e){
            throw e;
        }
        return true;
    }
 
    /**
     * 清空表中数据 truncate table platformlt_ebom
     */
 
    public static boolean truncateTable(String tableName) throws VCIError {
        Session session = HibernateSessionFactory.getSession();
        session.createSQLQuery("truncate table " + tableName).executeUpdate();
        return true;
    }
 
    /**
     * 清空表中数据 当表主键或唯一键被引用为外键时, 就算引用方没有数据, truncate还不是不能删除表中数据 而 delete可以 delete
     * table platformlt_ebom
     */
 
    public static boolean deleteTable(String tableName) throws VCIError {
        Session session = HibernateSessionFactory.getSession();
        session.createSQLQuery("delete from " + tableName).executeUpdate();
        return true;
    }
    
    public static boolean dropTable(String tableName) throws VCIError {
        if (!existsTable(tableName))
            return true;
        
        Session session = HibernateSessionFactory.getSession();
        session.createSQLQuery("drop table " + tableName).executeUpdate();
        return true;
    }    
    
    public static boolean existsTable(String tableName)  {
        int count = 0;
        try {
            Connection connection = HibernateSessionFactory.getSessionConnection();
            String sql = "SELECT COUNT(1) FROM User_Tables WHERE table_name = ?";
            PreparedStatement pst = connection.prepareStatement(sql);
            pst.setString(1, tableName);
            ResultSet rs = pst.executeQuery();
            
            if (rs.next()) {
                count = rs.getInt(0);
            }
        } catch (Exception e) {
            return false;
        }
        
        return count != 0;
    }
 
    /**
     * 
     * @param name
     * @return
     */
    public static boolean hasInstance(String name){
        boolean flag = false;
        Session session = HibernateSessionFactory.getSession();
        //判断表是否存在, 表不存在则该表无数据
        String sql_ = "select count(1) from user_tables where TABLE_NAME = '" + name.toUpperCase() +"'";
        List<Object> list_ = session.createSQLQuery(sql_).list();
        // 当list.get(i)中Object数量为1时, list.get(i)为Object
        // 当list.get(i)中Object数量 > 1时, list.get(i)为Object
        Object obj_ = list_.get(0);
        int count_ = ((BigDecimal) obj_).intValue();
        if (count_ < 1) {
            return false;
        }
        String sql = "select count(*) from " + name;
        List<Object> list = session.createSQLQuery(sql).list();
        //当list.get(i)中Object数量为1时, list.get(i)为Object
        //当list.get(i)中Object数量 > 1时, list.get(i)为Object
        Object obj = list.get(0);
        int count = ((BigDecimal)obj).intValue();
        if(count > 0){
            flag = true;
        }
        return flag;
    }
    
    
    /**
     * 获取属性字段的sql语句
     * @param array
     * @return
     */
    public static String getAbSql(AttribItem abItem){
        String sql = "";
        if(abItem == null){
            return sql;
        }
        String abName = abItem.name;
        String vtType = abItem.vtDataType;
        String other = abItem.other;
        String defValue = abItem.defValue;
        
        if(vtType.equals("VTString")){
            int length = 50;
            String lengthStr = AttributeHelper.getOtherValueByType(other, "length");
            if(lengthStr != null && !lengthStr.equals("")){
                length = Integer.valueOf(lengthStr);
            }
            sql += abName.toUpperCase() + " VARCHAR2(" + length + ")";
            if(!defValue.equals("")){
                sql += " default '" + defValue + "'";
            }
            sql += ",\n\t";
        }else if(vtType.equals("VTInteger") || vtType.equals("VTLong")){
            sql += abName.toUpperCase() + " NUMBER";
            if(!defValue.equals("")){
                sql += " default " + defValue;
            }
            sql += ",\n\t";
        }else if(vtType.equals("VTDouble")){
            int length = 20;
            String lengthStr = AttributeHelper.getOtherValueByType(other, "length");
            if(lengthStr != null && !lengthStr.equals("")){
                length = Integer.valueOf(lengthStr);
            }
            
            int accuracy = 2;
            String accuracyStr = AttributeHelper.getOtherValueByType(other, "accuracy");
            if(accuracyStr != null && !accuracyStr.equals("")){
                accuracy = Integer.valueOf(accuracyStr);
            }
            sql += abName.toUpperCase() + " NUMBER(" + length + ", " + accuracy +")";
            if(!defValue.equals("")){
                sql += " default " + defValue;
            }
            sql += ",\n\t";
        }else if(vtType.equals("VTBoolean")){
            sql += abName.toUpperCase() + " VARCHAR2(8)";
            if(!defValue.equals("")){
                sql += " default '" + defValue + "'";
            }
            sql += ",\n\t";
        }else if(vtType.equals("VTImage")){
            sql += abName.toUpperCase() + " VARCHAR2(255)";
            sql += ",\n\t";
        }else if(vtType.equals("VTDate")){
            sql += abName.toUpperCase() + " DATE";
            sql += ",\n\t";
        }else if(vtType.equals("VTTime")){
            sql += abName.toUpperCase() + " TIMESTAMP";
            sql += ",\n\t";
        }else if(vtType.equals("VTDateTime")){
            sql += abName.toUpperCase() + " TIMESTAMP";
            sql += ",\n\t";
        }else if(vtType.equals("VTNote")){
            sql += abName.toUpperCase() + " VARCHAR2(255)";
            sql += ",\n\t";
        }else if(vtType.equals("VTFilePath")){
            sql += abName.toUpperCase() + " VARCHAR2(255)";
            sql += ",\n\t";
        }else if(vtType.equals("VTClob")){
            sql += abName.toUpperCase() + " CLOB";
            sql += ",\n\t";
        }
        
        return sql;
    }
 
    public static String getBTSysFields() {
        return otherFieldBt;
    }
 
    public static String getLTSysFields() {
        return otherFieldLt;
    }
 
 
    /**
     * 获取属性other中type的值
     * @param other
     * @param type
     * @return
     */
//    static String getOtherValueByType(String other, String type){
//        String[] otherArray = other.split(";");
//        for(int i = 0; i < otherArray.length; i++){
//            String otherValue = otherArray[i];
//            if(otherValue.contains(type)){
//                return otherValue.substring(otherValue.indexOf("=") + 2, otherValue.length());
//            }
//        }
//        return null;
//        
//    }
}