2 * Created on 12.08.2004
5 package com.quantum.util.sql;
7 import java.sql.SQLException;
9 import com.quantum.adapters.DatabaseAdapter;
10 import com.quantum.model.Bookmark;
11 import com.quantum.model.Column;
12 import com.quantum.model.Entity;
13 import com.quantum.model.NotConnectedException;
14 import com.quantum.util.StringMatrix;
17 * Functions to build SQL instructions adapted to the particular database
21 public class SQLInstructionBuilder {
24 * Generates an Insert SQL instruction for each row of data in an StrigMatrix
25 * @param entity The entity to generate the instruction for
26 * @param columns A StringMatrix holding the names and values of the columns to insert
27 * @return A String with the insert sentences generated
29 public static String buildInsert(Entity entity, StringMatrix columns)
31 if (entity == null || columns == null ) return "";
32 StringBuffer valuesClause = new StringBuffer();
33 StringBuffer namesClause = new StringBuffer();
34 String insertSentences = "";
36 // We generate an update sentence for each row in the StringMatrix
37 for (int iRow = 0; iRow < columns.size(); iRow++) {
38 for (int iCol = 0; iCol < columns.getNumColumns(); iCol++) {
40 namesClause.append(", "); //$NON-NLS-1$
41 valuesClause.append(", "); //$NON-NLS-1$
43 namesClause.append(columns.getHeaderColumn(iCol));
44 valuesClause.append(quoteValue( entity, columns.getHeaderColumn(iCol), columns.get(iCol, iRow)));
46 if (iRow > 0) insertSentences += ";\n";
47 insertSentences += "INSERT INTO " + entity.getQuotedTableName(); //$NON-NLS-1$
48 insertSentences += "(" + namesClause + " )"; //$NON-NLS-1$
49 insertSentences += " VALUES " + " ( " + valuesClause + " )" ; //$NON-NLS-1$
51 return insertSentences;
55 * Generates an UPDATE SQL instruction for each row of data in an StrigMatrix
56 * @param entity The entity to generate the instruction for
57 * @param columns A StringMatrix holding the names and values of the columns to insert
58 * @param key A StringMatrix holding the names and values of the columns of the key
59 * @return A String with the insert sentences generated
61 public static String buildUpdate(Entity entity, StringMatrix columns, StringMatrix key)
63 if (entity == null || columns == null ) return "";
64 StringBuffer setClause = new StringBuffer();
65 String whereClause = "";
66 String updateSentences = "";
68 // We generate an update sentence for each row in the StringMatrix
69 for (int iRow = 0; iRow < columns.size(); iRow++) {
70 for (int iCol = 0; iCol < columns.getNumColumns(); iCol++) {
71 if (iCol > 0) setClause.append(", "); //$NON-NLS-1$
72 setClause.append(columns.getHeaderColumn(iCol));
73 setClause.append(" = "); //$NON-NLS-1$
74 setClause.append(quoteValue( entity, columns.getHeaderColumn(iCol), columns.get(iCol, iRow)));
76 if (key != null && iRow < key.size()) {
77 whereClause = getWhereClause(entity, key, iRow);
79 if (iRow > 0) updateSentences += ";\n";
80 updateSentences += "UPDATE " + entity.getQuotedTableName(); //$NON-NLS-1$
81 updateSentences += " SET " + setClause.toString(); //$NON-NLS-1$
82 if (whereClause.length() > 0)
83 updateSentences += " WHERE " + whereClause; //$NON-NLS-1$
85 return updateSentences;
92 public static String buildDelete(Entity entity, StringMatrix key)
94 if (entity == null ) return "";
95 String deleteSentences = "";
96 String whereClause = "";
98 // We generate an update sentence for each row in the StringMatrix
99 if (key == null) return "DELETE FROM " + entity.getQuotedTableName(); //$NON-NLS-1$
101 for (int iRow = 0; iRow < key.size(); iRow++) {
102 if (key != null && iRow < key.size()) {
103 whereClause = getWhereClause(entity, key, iRow);
105 if (iRow > 0) deleteSentences += ";\n";
106 deleteSentences += "DELETE FROM " + entity.getQuotedTableName(); //$NON-NLS-1$
107 if (whereClause.length() > 0)
108 deleteSentences += " WHERE " + whereClause; //$NON-NLS-1$
110 return deleteSentences;
113 * Builds a Select query with all columns and no rows (useful for structure querying)
117 public static String buildSelectAllColumnsNoRows(Entity entity) {
118 return "SELECT * FROM " + entity.getQuotedTableName() + " WHERE (1 = 0)"; //$NON-NLS-1$ //$NON-NLS-2$
121 * Builds a Select query with all columns and no rows (useful for structure querying)
125 public static String buildSelectAllColumnsAllRows(Entity entity) {
126 return "SELECT * FROM " + entity.getQuotedTableName() ; //$NON-NLS-1$ //$NON-NLS-2$
129 * Builds a Select query with the selected columns and the selected rows (useful for structure querying)
131 * @param columns Selected columns.
132 * @param key Selected key and values, in row 0. Only 1 select query will be generated.
135 public static String buildSelect(Entity entity, Column[] columns, StringMatrix key) {
136 if (entity == null || columns == null ) return "";
137 StringBuffer columnsList = new StringBuffer();
138 String whereClause = "";
139 String selectQuery = "";
141 for (int iCol = 0; iCol < columns.length; iCol++) {
142 if (iCol > 0) columnsList.append(", "); //$NON-NLS-1$
143 columnsList.append(columns[iCol].getName());
146 whereClause = getWhereClause(entity, key, 0);
148 selectQuery += "SELECT " + columnsList; //$NON-NLS-1$
149 selectQuery += " FROM " + entity.getQuotedTableName(); //$NON-NLS-1$
150 if (whereClause.length() > 0)
151 selectQuery += " WHERE " + whereClause; //$NON-NLS-1$
158 * @param entity The entity to get the where clause for
159 * @param key A StringMatrix with the colums that form the key and rows with the values
160 * @param iRow The key to the row that contains the values we are interested in
161 * @return A String with where clause (without the 'WHERE' reserved word), adapted to the database of the entity
163 public static String getWhereClause(Entity entity, StringMatrix key, int iRow) {
164 StringBuffer whereClause = new StringBuffer();
165 for (int iKey = 0; iKey < key.getNumColumns(); iKey++) {
166 if (iKey > 0) whereClause.append(" AND "); //$NON-NLS-1$
167 whereClause.append("("); //$NON-NLS-1$
168 whereClause.append(key.getHeaderColumn(iKey));
169 whereClause.append(" = "); //$NON-NLS-1$
170 whereClause.append(quoteValue( entity, key.getHeaderColumn(iKey), key.get(iKey, iRow)));
171 whereClause.append(")"); //$NON-NLS-1$
173 return whereClause.toString();
176 * Default-value function.
177 * Generates a whereClause adapted to the entity´s database
178 * with the data of the first row of the StringMatrix (row 0).
179 * @see com.quantum.util.sql.SQLInstructionBuilder#getWhereClause
181 public static String getWhereClause(Entity entity, StringMatrix key) {
182 return getWhereClause(entity, key, 0);
186 * Quotes the 'value' according with the type of the column and the database
187 * @param entity Entity
188 * @param columnName Name of the column in the Entity
189 * @param value Value of the column, to be quoted
191 public static String quoteValue(Entity entity, String columnName, String value) {
192 Bookmark bookmark = entity.getBookmark();
193 DatabaseAdapter adapter = bookmark.getAdapter();
195 if (adapter != null && entity != null && getColumn(entity, columnName) != null) {
196 Column column = getColumn(entity, columnName);
197 return adapter.quote(value, column.getType(), column.getTypeName());
203 * Wrapper function to avoid exception handling
204 * @param entity The entity that has the column
205 * @param columnName The column name
206 * @return A Column object from that entity, with the given name. null if not found or not connected.
208 public static Column getColumn(Entity entity, String columnName) {
210 return entity == null ? null : entity.getColumn(columnName);
211 } catch (NotConnectedException e) {
213 } catch (SQLException e) {