001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2007 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc;
022:
023: import java.sql.DatabaseMetaData;
024: import java.sql.SQLException;
025: import java.util.Collection;
026: import java.util.List;
027: import java.util.regex.Pattern;
028:
029: /**
030: * Encapsulates database vendor specific SQL syntax.
031: *
032: * @author Paul Ferraro
033: * @since 1.1
034: */
035: public interface Dialect {
036: /**
037: * Returns a simple SQL statement used to validate whether a database is alive or not.
038: * @return a SQL statement
039: * @throws SQLException
040: */
041: public String getSimpleSQL() throws SQLException;
042:
043: /**
044: * Returns a SQL statement used to truncate a table.
045: * @param properties table meta data
046: * @return a SQL statement
047: * @throws SQLException if there was an error fetching meta data.
048: */
049: public String getTruncateTableSQL(TableProperties properties)
050: throws SQLException;
051:
052: /**
053: * Returns a SQL statement used to create a foreign key constraint.
054: * @param constraint foreign key constraint meta data
055: * @return a SQL statement
056: * @throws SQLException if there was an error fetching meta data.
057: */
058: public String getCreateForeignKeyConstraintSQL(
059: ForeignKeyConstraint constraint) throws SQLException;
060:
061: /**
062: * Returns a SQL statement used to drop a foreign key constraint.
063: * @param constraint foreign key constraint meta data
064: * @return a SQL statement
065: * @throws SQLException if there was an error fetching meta data.
066: */
067: public String getDropForeignKeyConstraintSQL(
068: ForeignKeyConstraint constraint) throws SQLException;
069:
070: /**
071: * Returns a SQL statement used to create a unique constraint.
072: * @param constraint unique constraint meta data
073: * @return a SQL statement
074: * @throws SQLException if there was an error fetching meta data.
075: */
076: public String getCreateUniqueConstraintSQL(
077: UniqueConstraint constraint) throws SQLException;
078:
079: /**
080: * Returns a SQL statement used to drop a unique constraint.
081: * @param constraint unique constraint meta data
082: * @return a SQL statement
083: * @throws SQLException if there was an error fetching meta data.
084: */
085: public String getDropUniqueConstraintSQL(UniqueConstraint constraint)
086: throws SQLException;
087:
088: /**
089: * Determines whether the specified SQL is a SELECT ... FOR UPDATE statement
090: * @param sql a SQL statement
091: * @return true if this is a SELECT ... FOR UPDATE statement, false if it is not
092: * @throws SQLException if there was an error fetching meta data.
093: */
094: public boolean isSelectForUpdate(String sql) throws SQLException;
095:
096: /**
097: * Returns the data type of the specified column of the specified schema and table.
098: * This method is intended to correct JDBC driver type mapping flaws.
099: * @param properties table column meta data
100: * @return the JDBC data type of this column
101: * @throws SQLException
102: */
103: public int getColumnType(ColumnProperties properties)
104: throws SQLException;
105:
106: /**
107: * Parses a table name from the specified INSERT SQL statement that may contain identity columns.
108: * @param sql a SQL statement
109: * @return the name of a table, or null if this SQL statement is not an INSERT statement or this dialect does not support identity columns
110: * @throws SQLException
111: * @since 2.0
112: */
113: public String parseInsertTable(String sql) throws SQLException;
114:
115: /**
116: * Indicates whether or not the specified column is an identity column.
117: * @param properties a table column
118: * @return true, if this column is an identity column, false otherwise
119: * @throws SQLException
120: * @since 2.0
121: */
122: public boolean isIdentity(ColumnProperties properties)
123: throws SQLException;
124:
125: /**
126: * Parses a sequence name from the specified SQL statement.
127: * @param sql a SQL statement
128: * @return the name of a sequence, or null if this SQL statement does not reference a sequence or this dialect does not support sequences
129: * @throws SQLException
130: * @since 2.0
131: */
132: public String parseSequence(String sql) throws SQLException;
133:
134: /**
135: * Returns a collection of all sequences in this database.
136: * @param metaData database meta data
137: * @return a collection of sequence names
138: * @throws SQLException
139: * @since 2.0
140: */
141: public Collection<QualifiedName> getSequences(
142: DatabaseMetaData metaData) throws SQLException;
143:
144: /**
145: * Returns a SQL statement for obtaining the next value the specified sequence
146: * @param sequence a sequence name
147: * @return a SQL statement
148: * @throws SQLException
149: * @since 2.0
150: */
151: public String getNextSequenceValueSQL(SequenceProperties sequence)
152: throws SQLException;
153:
154: /**
155: * Returns a SQL statement used reset the current value of a sequence.
156: * @param sequence a sequence name
157: * @param value a sequence value
158: * @return a SQL statement
159: * @throws SQLException
160: * @since 2.0
161: */
162: public String getAlterSequenceSQL(SequenceProperties sequence,
163: long value) throws SQLException;
164:
165: /**
166: * Returns a SQL statement used reset the current value of an identity column.
167: * @param table a sequence name
168: * @param column a sequence name
169: * @param value a sequence value
170: * @return a SQL statement
171: * @throws SQLException
172: * @since 2.0.2
173: */
174: public String getAlterIdentityColumnSQL(TableProperties table,
175: ColumnProperties column, long value) throws SQLException;
176:
177: /**
178: * Returns a search path of schemas
179: * @param metaData
180: * @return a list of schema names
181: * @throws SQLException
182: * @since 2.0
183: */
184: public List<String> getDefaultSchemas(DatabaseMetaData metaData)
185: throws SQLException;
186:
187: /**
188: * Returns a pattern for identifiers that do not require quoting
189: * @param metaData
190: * @return a regular expression pattern
191: * @throws SQLException
192: * @since 2.0.2
193: */
194: public Pattern getIdentifierPattern(DatabaseMetaData metaData)
195: throws SQLException;
196:
197: /**
198: * Replaces non-deterministic CURRENT_DATE functions with deterministic static values.
199: * @param sql an SQL statement
200: * @param date the replacement date
201: * @return an equivalent deterministic SQL statement
202: * @throws SQLException
203: * @since 2.0.2
204: */
205: public String evaluateCurrentDate(String sql, java.sql.Date date)
206: throws SQLException;
207:
208: /**
209: * Replaces non-deterministic CURRENT_TIME functions with deterministic static values.
210: * @param sql an SQL statement
211: * @param time the replacement time
212: * @return an equivalent deterministic SQL statement
213: * @throws SQLException
214: * @since 2.0.2
215: */
216: public String evaluateCurrentTime(String sql, java.sql.Time time)
217: throws SQLException;
218:
219: /**
220: * Replaces non-deterministic CURRENT_TIMESTAMP functions with deterministic static values.
221: * @param sql an SQL statement
222: * @param timestamp the replacement timestamp
223: * @return an equivalent deterministic SQL statement
224: * @throws SQLException
225: * @since 2.0.2
226: */
227: public String evaluateCurrentTimestamp(String sql,
228: java.sql.Timestamp timestamp) throws SQLException;
229:
230: /**
231: * Replaces non-deterministic RAND() functions with deterministic static values.
232: * @param sql an SQL statement
233: * @return an equivalent deterministic SQL statement
234: * @throws SQLException
235: * @since 2.0.2
236: */
237: public String evaluateRand(String sql) throws SQLException;
238: }
|