96 lines
5.3 KiB
Java
96 lines
5.3 KiB
Java
import java.io.FileInputStream;
|
|
import java.sql.*;
|
|
import java.util.Properties;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class BUserRebuild {
|
|
public static void main(String[] args) throws Exception {
|
|
Properties props = new Properties();
|
|
try (FileInputStream in = new FileInputStream(args[0])) { props.load(in); }
|
|
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
|
|
try (Connection conn = DriverManager.getConnection(props.getProperty("url"), props.getProperty("username"), props.getProperty("password"))) {
|
|
conn.setAutoCommit(false);
|
|
|
|
// 1. stage 001/002 into temp table
|
|
run(conn, "IF OBJECT_ID('dbo.b_user_keep') IS NOT NULL DROP TABLE dbo.b_user_keep");
|
|
run(conn, "SELECT * INTO dbo.b_user_keep FROM dbo.b_user WHERE b_account IN ('001','002')");
|
|
|
|
// 2. drop + recreate with b_id first
|
|
run(conn, "DROP TABLE dbo.b_user");
|
|
String create = "CREATE TABLE dbo.b_user (\n" +
|
|
" b_id bigint identity(1,1) NOT NULL,\n" +
|
|
" b_account nvarchar(50) NOT NULL,\n" +
|
|
" b_name nvarchar(50) NULL,\n" +
|
|
" b_department nvarchar(50) NULL,\n" +
|
|
" b_subcode nvarchar(50) NULL,\n" +
|
|
" b_machineid nvarchar(50) NULL,\n" +
|
|
" b_bz nvarchar(200) NULL,\n" +
|
|
" b_password nvarchar(255) NOT NULL,\n" +
|
|
" b_canuse tinyint NOT NULL DEFAULT ((1)),\n" +
|
|
" b_othermachinelogin tinyint NOT NULL DEFAULT ((0)),\n" +
|
|
" b_salesman tinyint NOT NULL DEFAULT ((0)),\n" +
|
|
" b_docman tinyint NOT NULL DEFAULT ((0)),\n" +
|
|
" b_dealman tinyint NOT NULL DEFAULT ((0)),\n" +
|
|
" b_receivelevel tinyint NULL,\n" +
|
|
" b_paylevel tinyint NULL,\n" +
|
|
" b_visible_customer tinyint NOT NULL DEFAULT ((0)),\n" +
|
|
" b_visible_supply tinyint NOT NULL DEFAULT ((0)),\n" +
|
|
" b_levelvisible tinyint NOT NULL DEFAULT ((0)),\n" +
|
|
" b_fromcustomer tinyint NOT NULL DEFAULT ((0)),\n" +
|
|
" b_logintime tinyint NOT NULL DEFAULT ((0)),\n" +
|
|
" b_checkxy tinyint NOT NULL DEFAULT ((0)),\n" +
|
|
" b_loginfee tinyint NOT NULL DEFAULT ((0)),\n" +
|
|
" b_phone nvarchar(50) NULL,\n" +
|
|
" CONSTRAINT [PK_b_user] PRIMARY KEY (b_id),\n" +
|
|
" CONSTRAINT [UQ_b_user_account] UNIQUE (b_account)\n" +
|
|
");";
|
|
run(conn, create);
|
|
|
|
// 3. copy back 001/002 (without b_id, identity assigns 1,2)
|
|
String cols = getColsExcept(conn, "b_user_keep", "b_id");
|
|
run(conn, "INSERT INTO dbo.b_user (" + cols + ") SELECT " + cols + " FROM dbo.b_user_keep ORDER BY b_id");
|
|
|
|
// 4. drop temp
|
|
run(conn, "DROP TABLE dbo.b_user_keep");
|
|
|
|
conn.commit();
|
|
System.out.println("COMMITTED.");
|
|
|
|
// verify
|
|
try (Statement st = conn.createStatement();
|
|
ResultSet rs = st.executeQuery("SELECT c.column_id, c.name, c.is_identity FROM sys.columns c WHERE c.object_id=OBJECT_ID('b_user') ORDER BY c.column_id")) {
|
|
System.out.println("== column order ==");
|
|
while (rs.next()) System.out.printf(" [%d] %-22s identity=%s%n", rs.getInt("column_id"), rs.getString("name"), rs.getBoolean("is_identity"));
|
|
}
|
|
try (Statement st = conn.createStatement();
|
|
ResultSet rs = st.executeQuery("SELECT b_id, b_account, b_name, b_password, b_canuse FROM b_user ORDER BY b_id")) {
|
|
System.out.println("== data ==");
|
|
while (rs.next()) System.out.printf(" b_id=%s b_account=%s b_name=%s b_password=%s b_canuse=%s%n",
|
|
rs.getString("b_id"), rs.getString("b_account"), rs.getString("b_name"), rs.getString("b_password"), rs.getString("b_canuse"));
|
|
}
|
|
try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(
|
|
"SELECT name FROM sys.key_constraints WHERE parent_object_id=OBJECT_ID('b_user') ORDER BY name")) {
|
|
System.out.println("== constraints ==");
|
|
while (rs.next()) System.out.println(" " + rs.getString("name"));
|
|
}
|
|
} catch (SQLException e) {
|
|
System.err.println("ERROR: " + e.getMessage());
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
static void run(Connection conn, String sql) throws SQLException {
|
|
try (Statement st = conn.createStatement()) { st.execute(sql); }
|
|
System.out.println("OK: " + sql.replaceAll("\\s+", " ").trim().substring(0, Math.min(90, sql.replaceAll("\\s+", " ").trim().length())));
|
|
}
|
|
|
|
static String getColsExcept(Connection conn, String table, String exclude) throws SQLException {
|
|
try (Statement st = conn.createStatement();
|
|
ResultSet rs = st.executeQuery("SELECT name FROM sys.columns WHERE object_id=OBJECT_ID('" + table + "') AND name <> '" + exclude + "' ORDER BY column_id")) {
|
|
java.util.List<String> cols = new java.util.ArrayList<>();
|
|
while (rs.next()) cols.add("[" + rs.getString("name") + "]");
|
|
return String.join(", ", cols);
|
|
}
|
|
}
|
|
}
|