Add "Describe" command, with support for describing both (a) specific table(s) and...
[squelch.git] / src / main / java / net / jaekl / squelch / stmt / TabularColumnInfo.java
diff --git a/src/main/java/net/jaekl/squelch/stmt/TabularColumnInfo.java b/src/main/java/net/jaekl/squelch/stmt/TabularColumnInfo.java
new file mode 100644 (file)
index 0000000..ff9fc62
--- /dev/null
@@ -0,0 +1,78 @@
+package net.jaekl.squelch.stmt;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import net.jaekl.squelch.sql.Column;
+import net.jaekl.squelch.sql.Row;
+
+public class TabularColumnInfo extends Tabular {
+       private static final Column[] COLUMNS = {
+               new Column("Column", String.class, 32),
+               new Column("Type", String.class, 16),
+               new Column("Modifiers", String.class, 16)
+       };
+       private ResultSet m_rs;
+       
+       public TabularColumnInfo(ResultSet rs) {
+               m_rs = rs;
+       }
+       
+       @Override
+       Column[] getCols() throws SQLException {
+               return COLUMNS;
+       }
+
+       @Override
+       Row getNext() throws SQLException {
+               if (!m_rs.next()) {
+                       return null;
+               }
+               
+               Row row = new Row(COLUMNS.length);
+               
+               String columnName = m_rs.getString(4);
+               
+               String typeName = m_rs.getString(6);
+               int columnSize = m_rs.getInt(7);
+               if (columnSize > 0) {
+                       int decimalDigits = m_rs.getInt(8);     // digits after the decimal point
+                       if (decimalDigits > 0) {
+                               typeName += "(" + columnSize + "." + decimalDigits + ")";
+                       }
+                       else {
+                               typeName += "(" + columnSize + ")";
+                       }
+               }
+               
+               String remarks = m_rs.getString(12);
+               String isNullable = m_rs.getString(18);
+               String isAutoIncrement = m_rs.getString(23);
+               String notes = "";
+               if ("YES".equals(isNullable)) {
+                       notes += "NULL";
+               }
+               else if ("NO".equals(isNullable)) {
+                       notes += "NOT NULL";
+               }
+               if ("YES".equals(isAutoIncrement)) {
+                       if (notes.length() > 0) {
+                               notes += " ";
+                       }
+                       notes += "AUTOINCREMENT";
+               }
+               if ((null != remarks) && (remarks.length() > 0)) {
+                       if (notes.length() > 0) {
+                               notes += " ";
+                       }
+                       notes += "(" + remarks + ")";
+               }
+               
+               row.setValue(1, columnName);
+               row.setValue(2, typeName);
+               row.setValue(3, notes);
+               
+               return row;
+       }
+
+}