001/* 
002 * JKNIV, SQLegance keeping queries maintainable.
003 * 
004 * Copyright (C) 2017, the original author or authors.
005 *
006 * This library is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 2.1 of the License.
010 * 
011 * This library is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with this library; if not, write to the Free Software Foundation, Inc., 
018 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
019 */
020package net.sf.jkniv.sqlegance;
021
022import java.util.Date;
023
024import net.sf.jkniv.sqlegance.dialect.SqlDialect;
025import net.sf.jkniv.sqlegance.statement.ResultSetConcurrency;
026import net.sf.jkniv.sqlegance.statement.ResultSetHoldability;
027import net.sf.jkniv.sqlegance.statement.ResultSetType;
028import net.sf.jkniv.sqlegance.validation.ValidateType;
029import net.sf.jkniv.sqlegance.params.ParamParser;
030import net.sf.jkniv.sqlegance.transaction.Isolation;
031
032/**
033 * This interface represent Structured Query Language (SQL and some derivations like JPQL, HQL, JPQL, CQL...) 
034 * in XML file, each node at XML file is represented by this class. 
035 * <p>
036 * Those queries can be dynamic or static and to recover a dynamic query (built on-the-fly) is necessary to
037 * pass the parameters that can be mounted dynamically.
038 * 
039 * @author Alisson Gomes
040 * @since 0.6.0
041 */
042public interface Sql
043{
044    /**
045     * The name of query into XML file
046     * @return query name
047     */
048    String getName();
049    
050    /**
051     * Retrieve static SQL from a node, the dynamic parts is skipped.
052     * 
053     * @return SQL sentence
054     */
055    String getSql();
056    
057    /**
058     * Retrieve dynamic SQL from a node, the dynamic parts is evaluated at time
059     * to create the SQL.
060     * 
061     * @param params
062     *            parameters to evaluate dynamic SQL, can be a object like
063     *            Author, Book, etc or a java.util.Map where yours keys is like
064     *            the properties.
065     * @return Dynamic SQL sentence
066     */
067    String getSql(Object params);
068    
069    /**
070     * Retrieve node type: INSERT, UPDATE, DELETE, SELECT or PROCEDURE.
071     * 
072     * @return node type declared at XML file.
073     */
074    SqlType getSqlType();
075    
076    /**
077     * Verify if command is a SELECT
078     * @return {@code true} when is SELECT, {@code false} otherwise
079     */
080    boolean isSelectable();
081    
082    /**
083     * {@link Selectable} instance
084     * @return this instance
085     * @throws UnsupportedOperationException when this instance isn't {@link Selectable}
086     */
087    Selectable asSelectable();
088    
089    /**
090     * Verify if command is a INSERT
091     * @return {@code true} when is INSERT, {@code false} otherwise
092     */
093    boolean isInsertable();
094
095    /**
096     * {@link Insertable} instance
097     * @return this instance
098     * @throws UnsupportedOperationException when this instance isn't {@link Insertable}
099     */
100    Insertable asInsertable();
101    
102    /**
103     * Verify if command is a UPDATE
104     * @return {@code true} when is UPDATE, {@code false} otherwise
105     */
106    boolean isUpdateable();
107    
108    /**
109     * {@link Updateable} instance
110     * @return this instance
111     * @throws UnsupportedOperationException when this instance isn't {@link Updateable}
112     */
113    Updateable asUpdateable();
114    
115    /**
116     * Verify if command is a DELETE
117     * @return {@code true} when is DELETE, {@code false} otherwise
118     */
119    boolean isDeletable();
120    
121    /**
122     * {@link Deletable} instance
123     * @return this instance
124     * @throws UnsupportedOperationException when this instance isn't {@link Deletable}
125     */
126    Deletable asDeletable();
127
128    /**
129     * Verify if command is a STORED PROCEDURE
130     * @return {@code true} when is STORED PROCEDURE, {@code false} otherwise
131     */
132    boolean isStorable();
133
134    /**
135     * {@link Storable} instance
136     * @return this instance
137     * @throws UnsupportedOperationException when this instance isn't {@link Storable}
138     */
139    Storable asStorable();
140    
141    /**
142     * Retrieve the language type used at sql sentence.
143     * @return language type declared at xml file.
144     */
145    LanguageType getLanguageType();
146    
147    /*
148     * A SQL hint can be used on certain database platforms to define how the
149     * query uses indexes and other such low level usages. The SQL hint will be
150     * included in the SQL, after the SELECT/INSERT/UPDATE/DELETE command. - See
151     * more at:
152     * http://www.eclipse.org/eclipselink/documentation/2.5/jpa/extensions/q_sql_hint.htm
153     * 
154     * @return The full hint string, including the comment \ delimiters.
155     */
156    //String getHint();
157    
158    /**
159     * Retrieves the current transaction isolation level for the query.
160     * 
161     * @return the current transaction isolation level.
162     */
163    Isolation getIsolation();
164    
165    /**
166     * Retrieves the number of seconds the repository will wait for a Query
167     * object to execute. If the limit is exceeded, a RepositoryException is
168     * thrown.
169     * 
170     * @return the current query timeout limit in seconds; zero means there is
171     *         no limit
172     */
173    int getTimeout();
174    
175    /*
176     * Indicate if query is a batch of commands.
177     * @return true means is a batch command, false otherwise.
178     */
179    //boolean isBatch();
180
181    ResultSetType getResultSetType();
182    
183    ResultSetConcurrency getResultSetConcurrency();
184    
185    ResultSetHoldability getResultSetHoldability();
186    
187    String getReturnType();
188    
189    boolean hasReturnType();
190
191    Class<?> getReturnTypeAsClass(); // TODO test me, null when haven't return type
192
193    /**
194     * Timestamp when sql was read from xml
195     * @return when SQL was read from XML file
196     */
197    Date getTimestamp();
198    
199    /**
200     * The filename that SQL belong to
201     * @return return the filename, relative to absolute classpath, where the SQL was read.
202     */
203    String getResourceName();
204    
205    /**
206     * Expression XPATH to read SQL
207     * @return the XPATH expression to read the SQL at XML file.
208     */
209     String getXPath();
210     
211     /***
212      * strategy to parser the parameters from SQL.
213      * @return return the implementation of parser (colon, hash or question mark)
214      */
215     ParamParser getParamParser();
216     
217     /**
218      * Extract the name parameters from dynamic query 
219      * @param params parameters from query
220      * @return array of parameters names from query, array based-zero length when haven't param.
221      * Array of questions marks (?) is returned when the isn't name based.
222      */
223     String[] extractNames(Object params);
224     
225     /**
226      * Extract the name parameters from SQL
227      * @param sql sentence
228      * @return array of parameters names from query, array based-zero length when haven't param.
229      * Array of questions marks (?) is returned when the isn't name based.
230      */
231     String[] extractNames(String sql);
232     
233     
234     ValidateType getValidateType();
235     
236     void setValidateType(ValidateType validateType);
237
238     
239     void bind(SqlDialect sqlDialect);
240     
241     /**
242      * dialect for a specific database
243      * @return the dialect
244      */
245     SqlDialect getSqlDialect();
246     
247     /**
248      * name of package that this SQL belongs
249      * @return name of package
250      */
251     String getPackage();
252     
253     /**
254      * get statistical data from query execution 
255      * @return the statistical like min/max/avg times.
256      */
257     Statistical getStats();
258}