41 lines
2.9 KiB
Java
41 lines
2.9 KiB
Java
import java.io.FileInputStream;
|
|
import java.sql.*;
|
|
import java.util.Properties;
|
|
public class LogStruct {
|
|
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"))) {
|
|
for (String t : new String[]{"s_log_login", "s_log_technical", "s_log_audit", "s_log_audit_field"}) {
|
|
System.out.println("==== " + t + " ====");
|
|
try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(
|
|
"SELECT c.name, ty.name AS type, c.max_length, c.precision, c.scale, c.is_nullable, c.is_identity, dc.definition AS def " +
|
|
"FROM sys.columns c JOIN sys.types ty ON ty.user_type_id = c.user_type_id " +
|
|
"LEFT JOIN sys.default_constraints dc ON dc.parent_object_id = c.object_id AND dc.parent_column_id = c.column_id " +
|
|
"WHERE c.object_id = OBJECT_ID('" + t + "') ORDER BY c.column_id")) {
|
|
while (rs.next()) {
|
|
String type = rs.getString("type"); int ml = rs.getInt("max_length");
|
|
String ts;
|
|
if (type.equals("nvarchar")||type.equals("varchar")||type.equals("nchar")||type.equals("char"))
|
|
ts = type + (ml == -1 ? "(max)" : "(" + (ml / (type.startsWith("n")?2:1)) + ")");
|
|
else if (type.equals("decimal")||type.equals("numeric"))
|
|
ts = type + "(" + rs.getInt("precision") + "," + rs.getInt("scale") + ")";
|
|
else ts = type;
|
|
System.out.printf(" %-24s %-15s null=%-5s id=%-5s def=%s%n",
|
|
rs.getString("name"), ts, rs.getBoolean("is_nullable"), rs.getBoolean("is_identity"), rs.getString("def"));
|
|
}
|
|
}
|
|
System.out.println(" -- indexes --");
|
|
try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(
|
|
"SELECT i.name, i.is_unique, i.is_primary_key, " +
|
|
"STUFF((SELECT ',' + c2.name FROM sys.index_columns ic2 JOIN sys.columns c2 ON c2.object_id=ic2.object_id AND c2.column_id=ic2.column_id " +
|
|
"WHERE ic2.object_id=i.object_id AND ic2.index_id=i.index_id ORDER BY ic2.key_ordinal FOR XML PATH('')),1,1,'') AS cols " +
|
|
"FROM sys.indexes i WHERE i.object_id=OBJECT_ID('" + t + "') AND i.name IS NOT NULL")) {
|
|
while (rs.next()) System.out.printf(" idx: %-28s unique=%-5s pk=%-5s cols=%s%n",
|
|
rs.getString("name"), rs.getBoolean("is_unique"), rs.getBoolean("is_primary_key"), rs.getString("cols"));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|