001package bradleyross.library.database;
002import java.io.StringWriter;
003import java.io.PrintWriter;
004import java.util.Hashtable;
005import java.util.Enumeration;
006import bradleyross.library.database.ExtrasRegistration;
007
008/**
009 * Template for database specific methods.
010 * 
011 * <p>The goal would be to have a separate class for each type of database.
012 *    These classes would then be registered with the ExtrasRegistration class
013 *    and the DatabaseProperties subclass would then use the appropriate
014 *    DatabaseExtras class to obtain information that varies between databases.</p>
015 * 
016 * @author Bradley Ross
017 *
018 */
019abstract class DatabaseExtras 
020{
021        /**
022         * Returns database code
023         * @return Identifying code for database type
024         */
025        public abstract String getDbms();
026        /**
027         * The objects automatically register themselves when instantiated.
028         */
029        public DatabaseExtras() 
030        {
031                try
032                {
033                        ExtrasRegistration.register(this);
034                        setup();
035                }
036                catch (Exception e)
037                {
038                        System.out.println(e.getClass().getName() + " " + e.getMessage());
039                }
040        }
041        /**
042         * Sets up database specific objects.
043         */
044        protected abstract void setup();
045        /**
046         * Array containing valid codes for database specific terms.
047         * <table border="1">
048
049         * <tr><td><code>CURRENTTIME</code></td><td>Database specific term for obtaining current
050         *     date and time to be used as the value of a column.</td></tr>
051         * <tr><td><code>CURRENTDATE</code></td><td>Database specific term for obtaining current
052         *     date to be used as the value of a column.  (Hour, minutes, seconds and 
053         *     fractional seconds set to zero)</td></tr>
054         * <tr><td><code>DATE</code></td><td>Data type to be used for representing dates
055         *     (Hours, minutes, seconds, and fractional seconds set to zero)</td></tr>
056         * <tr><td><code>DATETIME</code></td><td>Data type to be used for date and time.</td></tr>
057         * <tr><td><code>DOUBLE</code></td><td>Data type to be used for double
058         *     precision floating point values.</tr>
059         * <tr><td><code>FLOAT</code></td><td>Data type to be used for single
060         *     precision floating point values.</td></tr>
061         * <tr><td><code>LONGBLOB</code></td><td>Data type for very long binary objects.</td></tr>
062         * <tr><td><code>LONGCLOB</code></td><td>Data type for very long character objects.</td></tr>
063         * 
064         * 
065         * </table>
066         */
067        protected String[] validTerms = { "CURRENTDATE", "CURRENTTIME", 
068                        "DATE", "DATETIME",
069                        "DOUBLE", "FLOAT", "LONGBLOB", "LONGCLOB" };
070        /**
071         * Contains mapping of database specific terms to their code values.
072         */
073        Hashtable<String, String> terms = new Hashtable<String,String>();
074        /**
075         * Add a database specific term to the list.
076         * @param key Code for database specific term
077         * @param value Value of database specific term
078         */
079        protected void addTerm(String key, String value)
080        {
081                terms.put(key, value);
082        }
083        /**
084         * Returns a database specific term for a function.
085         * @param value Identifier for term
086         * @return Database specific term
087         */
088        public String getTerm(String value)
089        {
090                return (String) terms.get(value.trim().toUpperCase());
091        }
092        public String listAllTerms()
093        {
094                StringWriter writer = new StringWriter();
095                PrintWriter printer = new PrintWriter(writer);
096                Enumeration<String> keys = terms.keys();
097                printer.println("Listing terms for " + getDbms());
098                while (keys.hasMoreElements())
099                {
100                        String keyValue = keys.nextElement();
101                        String term = terms.get(keyValue);
102                        printer.println(keyValue+"="+term);
103                }
104                return writer.toString();
105        }
106}