蓄电池监控管理平台数据库初始化程序
whycrzg
2021-11-11 4fe9eb1f4a7990c284e6383650f59d6d05cd6a5a
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
package com.database_util;
 
import java.sql.ResultSet;
import java.sql.SQLException;
 
import com.sql.MysqlConnPool;
import com.sql.Sql_Mysql;
 
public class DB_Pwrdev_Alarm {
    public static void init(MysqlConnPool pool, boolean recreate) {
        createDB_Pwrdev_Alarm (pool);
        
        createPwrdev_Alarm_Table(pool, recreate);
        
        createPwrdev_Alarm_History_Table(pool, recreate);
        
        createPwrdev_Alarm_Param_Table(pool, recreate);
        
    }
 
    private static void createPwrdev_Alarm_Param_Table(MysqlConnPool pool, boolean recreate) {
        String sql_str01 = " DROP TABLE IF EXISTS "+Sql_Mysql.Pwrdev_Alarm_Param_Table;
        String sql_str02 = "CREATE TABLE IF NOT EXISTS " + Sql_Mysql.Pwrdev_Alarm_Param_Table + " (" + 
                "  `num` bigint(20) NOT NULL AUTO_INCREMENT," +
                "  `alarm_id` bigint(20) NOT NULL DEFAULT '0'," +
                "  `alarm_limitH` float NOT NULL DEFAULT '0'," +
                "  `alarm_limitL` float NOT NULL DEFAULT '0'," +
                "  `alarm_limitEn` int(11) NOT NULL DEFAULT '0' COMMENT 'ÊÇ·ñ¿ÉÒÔÉèÖÃÉÏÏÂÏÞ'," +
                "  `UserAlarm_EN` int(11) NOT NULL DEFAULT '1' COMMENT '¸æ¾¯¹æÔòÊÇ·ñÆô¶¯'," +
                "  `alarm_level` int(11) NOT NULL DEFAULT '0'," +
                "  `alarm_devtype` int(11) NOT NULL DEFAULT '0'," +
                "  `alarm_name` varchar(100) NOT NULL DEFAULT '0'," +
                "  PRIMARY KEY (`num`)," +
                "  UNIQUE KEY `unique_alarm_id` (`alarm_id`)," +
                "  KEY `index_alarm_id` (`alarm_id`)" +
                ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        try {
            if(true == recreate) {            
                sql.sqlMysqlExecute(sql_str01);
            }
            sql.sqlMysqlExecute(sql_str02);
 
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
 
    /**
     *     ´´½¨     db_pwrdev_alarm Êý¾Ý¿â
     * @param pool
     */
    private static void createDB_Pwrdev_Alarm(MysqlConnPool pool) {
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        try {
            sql.sqlMysqlExecute("CREATE DATABASE IF NOT EXISTS " + Sql_Mysql.DB_PWRDEV_ALARM);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
    
    /**
     *     ´´½¨   tb_pwrdev_alarm ±í
     * @param pool
     * @param recreate
     */
    public static void createPwrdev_Alarm_Table(MysqlConnPool pool, boolean recreate) {
        String sql_str01 = " DROP TABLE IF EXISTS "+Sql_Mysql.Pwrdev_Alarm_Table;
        String sql_str02 = "CREATE TABLE IF NOT EXISTS " + Sql_Mysql.Pwrdev_Alarm_Table + " (" + 
                "  `num` bigint(20) NOT NULL AUTO_INCREMENT," + 
                "  `record_id` bigint(20) NOT NULL DEFAULT '0'," + 
                "  `PowerDeviceId` bigint(20) NOT NULL DEFAULT '0'," + 
                "  `alm_type` int(11) NOT NULL DEFAULT '0'," + 
                "  `alm_level` int(11) NOT NULL DEFAULT '0'," + 
                "  `alm_source` int(11) NOT NULL DEFAULT '0'," +
                "  `alm_index` int(11) NOT NULL DEFAULT '0',"+
                "  `alm_start_time` datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," + 
                "  `alm_end_time` datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," + 
                "  `alm_value` float NOT NULL DEFAULT '0'," + 
                "  `alm_is_confirmed` tinyint(1) NOT NULL DEFAULT '0'," + 
                "  `alm_confirmed_time` datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," + 
                "  `alm_cleared_type` int(11) NOT NULL DEFAULT '0'," + 
                "  `usr_Id` int(11) NOT NULL DEFAULT '0'," + 
                "  PRIMARY KEY (`num`)," + 
                "  KEY `index_record_id` (`record_id`)," + 
                "  KEY `index_pwrdev_id` (`PowerDeviceId`)," + 
                "  KEY `index_alm_type` (`alm_type`)," + 
                "  KEY `index_alm_start_time` (`alm_start_time`)," + 
                "  KEY `index_alm_cleared_type` (`alm_cleared_type`)" + 
                ") ENGINE=InnoDB AUTO_INCREMENT=44 DEFAULT CHARSET=utf8;";
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        ResultSet res = null;
        try {
            if(true == recreate) {            
                sql.sqlMysqlExecute(sql_str01);
            }
            sql.sqlMysqlExecute(sql_str02);
 
            
            //Ìí¼Ó alm_source
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_pwrdev_alarm'"
                    + " AND table_name='tb_pwrdev_alarm'"
                    + " AND column_name='alm_source'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Pwrdev_Alarm_Table 
                                + " ADD COLUMN `alm_source` int(11) NOT NULL DEFAULT '0'");
            }
            
            //Ìí¼Ó alm_index
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_pwrdev_alarm'"
                    + " AND table_name='tb_pwrdev_alarm'"
                    + " AND column_name='alm_index'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Pwrdev_Alarm_Table 
                                + " ADD COLUMN `alm_index` int(11) NOT NULL DEFAULT '0'");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
    
    /**
     *     ´´½¨   tb_pwrdev_alarm_history ±í
     * @param pool
     * @param recreate
     */
    public static void createPwrdev_Alarm_History_Table(MysqlConnPool pool, boolean recreate) {
        String sql_str01 = " DROP TABLE IF EXISTS "+Sql_Mysql.Pwrdev_Alarm_History_Table;
        String sql_str02 = "CREATE TABLE IF NOT EXISTS " + Sql_Mysql.Pwrdev_Alarm_History_Table + " (" + 
                "  `num` bigint(20) NOT NULL AUTO_INCREMENT," + 
                "  `record_id` bigint(20) NOT NULL DEFAULT '0'," + 
                "  `PowerDeviceId` bigint(20) NOT NULL DEFAULT '0'," + 
                "  `alm_type` int(11) NOT NULL DEFAULT '0'," + 
                "  `alm_level` int(11) NOT NULL DEFAULT '0'," +
                "  `alm_source` int(11) NOT NULL DEFAULT '0'," +
                "  `alm_index` int(11) NOT NULL DEFAULT '0',"+ 
                "  `alm_start_time` datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," + 
                "  `alm_end_time` datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," + 
                "  `alm_value` float NOT NULL DEFAULT '0'," + 
                "  `alm_is_confirmed` tinyint(1) NOT NULL DEFAULT '0'," + 
                "  `alm_confirmed_time` datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," + 
                "  `alm_cleared_type` int(11) NOT NULL DEFAULT '0'," + 
                "  `usr_Id` int(11) NOT NULL DEFAULT '0'," + 
                "  PRIMARY KEY (`num`)," + 
                "  KEY `index_record_id` (`record_id`)," + 
                "  KEY `index_pwrdev_id` (`PowerDeviceId`)," + 
                "  KEY `index_alm_type` (`alm_type`)," + 
                "  KEY `index_alm_start_time` (`alm_start_time`)," + 
                "  KEY `index_alm_cleared_type` (`alm_cleared_type`)" + 
                ") ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=utf8;";
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        ResultSet res = null;
        try {
            if(true == recreate) {            
                sql.sqlMysqlExecute(sql_str01);
            }
            sql.sqlMysqlExecute(sql_str02);
 
            
            //Ìí¼Ó alm_source
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_pwrdev_alarm'"
                    + " AND table_name='tb_pwrdev_alarm_history'"
                    + " AND column_name='alm_source'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Pwrdev_Alarm_History_Table 
                                + " ADD COLUMN `alm_source` int(11) NOT NULL DEFAULT '0'");
            }
            
            //Ìí¼Ó alm_index
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_pwrdev_alarm'"
                    + " AND table_name='tb_pwrdev_alarm_history'"
                    + " AND column_name='alm_index'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Pwrdev_Alarm_History_Table 
                                + " ADD COLUMN `alm_index` int(11) NOT NULL DEFAULT '0'");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
}