View Javadoc
1 /* 2 * Created on 2003-6-10 15:28:13 by joel guo 3 * 4 * vTradEx Information Technology Inc. 5 */ 6 package com.cyclops.dbdigger.sqlcastor; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import org.apache.commons.lang.StringUtils; 11 12 import com.cyclops.dbdigger.schema.Column; 13 import com.cyclops.dbdigger.schema.Table; 14 import com.cyclops.dbdigger.sql.Combination; 15 import com.cyclops.dbdigger.sql.Comparison; 16 import com.cyclops.dbdigger.sql.Condition; 17 import com.cyclops.dbdigger.sql.Delete; 18 import com.cyclops.dbdigger.sql.Equal; 19 import com.cyclops.dbdigger.sql.Insert; 20 import com.cyclops.dbdigger.sql.OrderBy; 21 import com.cyclops.dbdigger.sql.SQL; 22 import com.cyclops.dbdigger.sql.Select; 23 import com.cyclops.dbdigger.sql.Update; 24 /*** Add description <font color="red">HERE</font>! 25 * 26 * @author <a href="mailto:joeblack.guo@vtradex.com">joel guo</a> 27 * @since 2003-6-10 15:28:13 28 */ 29 public class DefaultSQLCastor implements SQLCastor { 30 private static final String APOS = "'"; 31 private static final String BLANK_SPACE = " "; 32 private static final String COMMA = ", "; 33 private static final String FROM = "FROM"; 34 private static final String WHERE = "WHERE"; 35 private static final String ORDER_BY = "ORDER BY"; 36 /*** Override method cast() of super class 37 * @see com.cyclops.dbdigger.driver.SQLCastor#cast(com.cyclops.dbdigger.sql.Statement) 38 */ 39 public String cast(SQL sql) { 40 if (sql instanceof Select) { 41 return castSelect((Select) sql); 42 } else if (sql instanceof Update) { 43 return castUpdate((Update) sql); 44 } else if (sql instanceof Insert) { 45 return castInsert((Insert) sql); 46 } else if (sql instanceof Delete) { 47 return castDelete((Delete) sql); 48 } else { 49 throw new IllegalArgumentException(); 50 } 51 } 52 /*** Method castColumnName() 53 * @param column Column object 54 * @return Tablename.Columnname 55 */ 56 protected String castColumnName(Column column) { 57 return castTableName(column.getTable()) + '.' + column.getName(); 58 } 59 /*** Method castCombination() 60 * @param combination Combination object 61 * @return Casting result 62 */ 63 protected String castCombination(Combination combination) { 64 Condition[] cs = combination.getCriteria(); 65 List parts = new ArrayList(); 66 for (int i = 0; i < cs.length; i++) { 67 Condition c2 = cs[i]; 68 parts.add("(" + castCondition(c2) + ")"); 69 } 70 return StringUtils.join( 71 parts.iterator(), 72 BLANK_SPACE + combination.getCombinator() + BLANK_SPACE); 73 } 74 /*** Method castComparison() 75 * @param comparison Comparison criterion 76 * @return String expression 77 */ 78 protected String castComparison(Comparison comparison) { 79 StringBuffer sb = new StringBuffer(); 80 sb.append(castColumnName(comparison.getColumn())); 81 sb.append(BLANK_SPACE).append(comparison.getComparator()).append( 82 BLANK_SPACE); 83 sb.append(castValue(comparison.getColumn(), comparison.getValue())); 84 return sb.toString(); 85 } 86 /*** Cast Condition object to SQL expression 87 * @param criterion Condition object 88 * @return Casting result 89 */ 90 protected String castCondition(Condition criterion) { 91 if (criterion instanceof Combination) { 92 return castCombination((Combination) criterion); 93 } else if (criterion instanceof Comparison) { 94 return castComparison((Comparison) criterion); 95 } else { 96 return null; 97 } 98 } 99 /*** Cast delete sql 100 * @param delete Delete sql object 101 * @return Casting result 102 */ 103 protected String castDelete(Delete delete) { 104 StringBuffer sb = new StringBuffer(); 105 sb.append(delete.getOperation()).append(BLANK_SPACE); 106 sb.append(FROM).append(BLANK_SPACE).append( 107 castTableName(delete.getFrom())); 108 if (delete.getWhere() != null) { 109 sb.append(BLANK_SPACE).append(WHERE).append(BLANK_SPACE); 110 sb.append(castCondition(delete.getWhere())); 111 } 112 return sb.toString(); 113 } 114 /*** Method castInsert() 115 * @param insert Insert object 116 * @return Casting result 117 */ 118 protected String castInsert(Insert insert) { 119 return null; 120 } 121 /*** Method castSelect() 122 * @param select Select query object 123 * @return Casting result 124 */ 125 protected String castSelect(Select select) { 126 StringBuffer sb = new StringBuffer(select.getOperation()); 127 sb.append(BLANK_SPACE); 128 Column[] selectedColumns = select.getSeletedColumns(); 129 List selectedColumnNames = new ArrayList(); 130 for (int i = 0; i < selectedColumns.length; i++) { 131 Column selectedColumn = selectedColumns[i]; 132 selectedColumnNames.add(castColumnName(selectedColumn)); 133 } 134 sb.append(StringUtils.join(selectedColumnNames.iterator(), COMMA)); 135 sb.append(BLANK_SPACE).append(FROM).append(BLANK_SPACE); 136 Table[] fromTables = select.getFroms(); 137 List fromTableNames = new ArrayList(); 138 for (int i = 0; i < fromTables.length; i++) { 139 Table fromTable = fromTables[i]; 140 fromTableNames.add(castTableName(fromTable)); 141 } 142 sb.append(StringUtils.join(fromTableNames.iterator(), COMMA)); 143 if (select.getWhere() != null) { 144 sb.append(BLANK_SPACE).append(WHERE).append(BLANK_SPACE); 145 sb.append(castCondition(select.getWhere())); 146 } 147 sb.append(castOrderBys(select.getOrderBys())); 148 return sb.toString(); 149 } 150 /*** Cast name of the table 151 * @param table Table object 152 * @return Name of the table 153 */ 154 protected String castTableName(Table table) { 155 return table.getName(); 156 } 157 /*** Method castUpdate() 158 * @param aupdate Update query object 159 * @return Casting result 160 */ 161 protected String castUpdate(Update aupdate) { 162 StringBuffer sb = 163 new StringBuffer(aupdate.getOperation()).append(BLANK_SPACE); 164 Table[] tables = aupdate.getTables(); 165 List tableNames = new ArrayList(); 166 for (int i = 0; i < tables.length; i++) { 167 Table table = tables[i]; 168 tableNames.add(castTableName(table)); 169 } 170 sb.append(StringUtils.join(tableNames.iterator(), COMMA)); 171 sb.append(" SET "); 172 Equal[] sets = aupdate.getSets(); 173 List equalExpressions = new ArrayList(); 174 for (int i = 0; i < sets.length; i++) { 175 Equal equal = sets[i]; 176 equalExpressions.add(castCondition(equal)); 177 } 178 sb.append(StringUtils.join(equalExpressions.iterator(), COMMA)); 179 if (aupdate.getWhere() != null) { 180 sb.append(castCondition(aupdate.getWhere())); 181 } 182 return sb.toString(); 183 } 184 /*** Method castValue() 185 * @param column Column for this value 186 * @param value Value object, could be a Column 187 * @return Casting result 188 */ 189 protected String castValue(Column column, Object value) { 190 if (value instanceof Column) { 191 return castColumnName((Column) value); 192 } else if (column.getDBType().needQuotation()) { 193 return APOS + castValueExpression(column, value) + APOS; 194 } else { 195 return castValueExpression(column, value); 196 } 197 } 198 /*** Method castValueExpression() 199 * @param column Column for this value 200 * @param valueObject Value object, never to a Column 201 * @return Casting result 202 */ 203 protected String castValueExpression(Column column, Object valueObject) { 204 return valueObject.toString(); 205 } 206 /*** Method castOrderBys() 207 * @param orderBys Array of order by objects 208 * @return Order by sql string 209 */ 210 protected String castOrderBys(OrderBy[] orderBys) { 211 if (orderBys == null || orderBys.length == 0) { 212 return ""; 213 } 214 List parts = new ArrayList(); 215 for (int i = 0; i < orderBys.length; i++) { 216 OrderBy orderBy = orderBys[i]; 217 String part = castColumnName(orderBy.getColumn()); 218 if (orderBy.isDescendent()) { 219 part += " DESC"; 220 } 221 parts.add(part); 222 } 223 StringBuffer sb = new StringBuffer(BLANK_SPACE); 224 sb.append(ORDER_BY).append(BLANK_SPACE); 225 sb.append(StringUtils.join(parts.iterator(), COMMA)); 226 return sb.toString(); 227 } 228 }

This page was automatically generated by Maven