001package bradleyross.library.database;
002import java.util.Hashtable;
003import java.sql.SQLException;
004import java.sql.ResultSet;
005import java.sql.Types;
006import java.sql.DatabaseMetaData;
007import java.io.StringWriter;
008import java.io.PrintWriter;
009import java.util.Iterator;
010import java.util.Collection;
011import bradleyross.library.database.DatabaseProperties;
012/**
013 * Superclass for handling interactions with a database table.
014 * @author Bradley Ross
015 *
016 */
017public class TableHelper 
018{
019        /**
020         * Encapsulates information on a column in the table.
021         * @author Bradley Ross
022         *
023         */
024        class ColumnStatus
025        {
026                /**
027                 * Name of column.
028                 */
029                protected String name = null;
030                /**
031                 * Name of the java.sql or java.lang class that is
032                 * used to represent the information in this column.
033                 */
034                protected String dataclassName = null;
035                /**
036                 * True indicates that value of column has been changed.
037                 */
038                protected boolean isChanged = false;
039                /**
040                 * Data type as defined by java.sql.Types.
041                 * @see Types
042                 */
043                protected int type = -1;
044                /**
045                 * Code from java.sql.Types that is associated with this
046                 * column.
047                 */
048                protected Class<?> objectClass = null;
049                /**
050                 * True indicates that the value contains a null;
051                 */
052                boolean isNull = false;
053                /**
054                 * Value of the column.
055                 */
056                Object value = null;
057                public ColumnStatus(String nameValue, int typeValue)
058                {
059                        name = nameValue;
060                        type = typeValue;
061                        isChanged = false;
062                }
063                public String getName()
064                {
065                        return name;
066                }
067                public int getType()
068                {
069                        return type;
070                }
071                public void setName(String value)
072                {
073                        name = value;
074                }
075                public void setType(int value)
076                {
077                        type = value;
078                }
079        }
080        /**
081         * List of column names and the data types as defined in java.sql.Types.
082         */
083        protected Hashtable<String, ColumnStatus> columns = new Hashtable<String, ColumnStatus>();
084        /**
085         * Object containing information for connecting to database.
086         */
087        protected DatabaseProperties data = null;
088        public void setDatabase(DatabaseProperties dataValue)
089        {
090                data = dataValue;
091        }
092        public DatabaseProperties getDatabase()
093        {
094                return data;
095        }
096        public TableHelper(DatabaseProperties data, String catalog, String schema, String table)
097        throws SQLException
098        {
099                DatabaseMetaData meta = data.getConnection().getMetaData();
100                ResultSet rs = meta.getColumns(catalog, schema, table, null);
101                String columnName = null;
102                int dataType = -100000;
103                while (rs.next())
104                {
105                        columnName = rs.getString("COLUMN_NAME");
106                        dataType = rs.getInt("DATA_TYPE");
107                        if (rs.wasNull())
108                        {
109                                dataType = -100000;
110                        }
111                        ColumnStatus properties = new ColumnStatus(columnName, dataType);
112                        columns.put(columnName, properties);
113                }
114        }
115        /**
116         * Display contents of internal tables for diagnostic purposes.
117         * @return Information
118         */
119        public String displayEntries()
120        {
121                StringWriter writer = new StringWriter();
122                PrintWriter printer = new PrintWriter(writer);
123                Collection<ColumnStatus> collection = columns.values();
124                Iterator<ColumnStatus> iter = collection.iterator();
125                while (iter.hasNext())
126                {
127                        ColumnStatus item = iter.next();
128                        printer.println(item.getName() + " " + Integer.toString(item.getType()) + " " +
129                                        DatabaseProperties.decodeSqlType(item.getType()));
130                }
131                return writer.toString();
132        }
133        
134}