Add "Describe" command, with support for describing both (a) specific table(s) and...
[squelch.git] / src / main / java / net / jaekl / squelch / stmt / TabularColumnInfo.java
1 package net.jaekl.squelch.stmt;
2
3 import java.sql.ResultSet;
4 import java.sql.SQLException;
5
6 import net.jaekl.squelch.sql.Column;
7 import net.jaekl.squelch.sql.Row;
8
9 public class TabularColumnInfo extends Tabular {
10         private static final Column[] COLUMNS = {
11                 new Column("Column", String.class, 32),
12                 new Column("Type", String.class, 16),
13                 new Column("Modifiers", String.class, 16)
14         };
15         private ResultSet m_rs;
16         
17         public TabularColumnInfo(ResultSet rs) {
18                 m_rs = rs;
19         }
20         
21         @Override
22         Column[] getCols() throws SQLException {
23                 return COLUMNS;
24         }
25
26         @Override
27         Row getNext() throws SQLException {
28                 if (!m_rs.next()) {
29                         return null;
30                 }
31                 
32                 Row row = new Row(COLUMNS.length);
33                 
34                 String columnName = m_rs.getString(4);
35                 
36                 String typeName = m_rs.getString(6);
37                 int columnSize = m_rs.getInt(7);
38                 if (columnSize > 0) {
39                         int decimalDigits = m_rs.getInt(8);     // digits after the decimal point
40                         if (decimalDigits > 0) {
41                                 typeName += "(" + columnSize + "." + decimalDigits + ")";
42                         }
43                         else {
44                                 typeName += "(" + columnSize + ")";
45                         }
46                 }
47                 
48                 String remarks = m_rs.getString(12);
49                 String isNullable = m_rs.getString(18);
50                 String isAutoIncrement = m_rs.getString(23);
51                 String notes = "";
52                 if ("YES".equals(isNullable)) {
53                         notes += "NULL";
54                 }
55                 else if ("NO".equals(isNullable)) {
56                         notes += "NOT NULL";
57                 }
58                 if ("YES".equals(isAutoIncrement)) {
59                         if (notes.length() > 0) {
60                                 notes += " ";
61                         }
62                         notes += "AUTOINCREMENT";
63                 }
64                 if ((null != remarks) && (remarks.length() > 0)) {
65                         if (notes.length() > 0) {
66                                 notes += " ";
67                         }
68                         notes += "(" + remarks + ")";
69                 }
70                 
71                 row.setValue(1, columnName);
72                 row.setValue(2, typeName);
73                 row.setValue(3, notes);
74                 
75                 return row;
76         }
77
78 }