54 lines
3.2 KiB
Java
54 lines
3.2 KiB
Java
import java.io.FileInputStream;
|
|
import java.sql.*;
|
|
import java.util.Properties;
|
|
|
|
public class SimplifyG3Soft {
|
|
static final String ALLOW = "ISNULL(CONVERT(int, SESSION_CONTEXT(N'allow_g3soft')), 0) = 1";
|
|
public static void main(String[] args) throws Exception {
|
|
Properties props = new Properties();
|
|
try (FileInputStream in = new FileInputStream(args[0])) { props.load(in); }
|
|
try (Connection conn = DriverManager.getConnection(props.getProperty("url"), props.getProperty("username"), props.getProperty("password"))) {
|
|
conn.setAutoCommit(false);
|
|
// 1. drop views that reference b_user (v_b_user, and v_s_log_audit depends on v_b_user)
|
|
run(conn, "IF OBJECT_ID('dbo.v_s_log_audit') IS NOT NULL DROP VIEW dbo.v_s_log_audit");
|
|
run(conn, "IF OBJECT_ID('dbo.v_b_user') IS NOT NULL DROP VIEW dbo.v_b_user");
|
|
// 2. drop b_is_system column
|
|
run(conn, "ALTER TABLE dbo.b_user DROP COLUMN b_is_system");
|
|
// 3. recreate v_b_user filtering by account name only
|
|
run(conn, "CREATE VIEW dbo.v_b_user AS SELECT * FROM dbo.b_user WHERE b_account <> N'g3soft' OR " + ALLOW);
|
|
// 4. recreate v_s_log_audit (depends on v_b_user)
|
|
run(conn, "CREATE VIEW dbo.v_s_log_audit AS SELECT * FROM dbo.s_log_audit WHERE b_operator_id IN (SELECT b_id FROM dbo.v_b_user) OR " + ALLOW);
|
|
conn.commit();
|
|
System.out.println("COMMITTED.");
|
|
|
|
System.out.println("\n== b_user columns ==");
|
|
dump(conn, "SELECT name FROM sys.columns WHERE object_id=OBJECT_ID('b_user') ORDER BY column_id");
|
|
System.out.println("\n== v_b_user WITHOUT ctx (expect 001,002 only) ==");
|
|
dump(conn, "SELECT b_id, b_account FROM v_b_user ORDER BY b_id");
|
|
try (Statement st = conn.createStatement()) { st.execute("EXEC sys.sp_set_session_context @key=N'allow_g3soft', @value=1"); }
|
|
System.out.println("== v_b_user WITH ctx=1 (expect all 3) ==");
|
|
dump(conn, "SELECT b_id, b_account FROM v_b_user ORDER BY b_id");
|
|
try (Statement st = conn.createStatement()) { st.execute("EXEC sys.sp_set_session_context @key=N'allow_g3soft', @value=NULL"); }
|
|
System.out.println("\n== views ==");
|
|
dump(conn, "SELECT name FROM sys.views WHERE name LIKE 'v_%' ORDER BY name");
|
|
}
|
|
}
|
|
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(95, sql.replaceAll("\\s+", " ").trim().length())));
|
|
}
|
|
static void dump(Connection conn, String sql) throws SQLException {
|
|
try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql)) {
|
|
ResultSetMetaData m = rs.getMetaData();
|
|
while (rs.next()) {
|
|
StringBuilder sb = new StringBuilder(" ");
|
|
for (int i = 1; i <= m.getColumnCount(); i++) {
|
|
if (i > 1) sb.append(" | ");
|
|
sb.append(m.getColumnName(i)).append("=").append(rs.getObject(i));
|
|
}
|
|
System.out.println(sb);
|
|
}
|
|
}
|
|
}
|
|
}
|