1 10 package org.mmbase.storage.search.implementation.database; 11 12 import org.mmbase.storage.search.*; 13 import org.mmbase.util.logging.*; 14 15 24 public class HSqlSqlHandler extends BasicSqlHandler implements SqlHandler { 25 26 private static final Logger log = Logging.getLoggerInstance(HSqlSqlHandler.class); 27 28 31 public HSqlSqlHandler() { 32 super(); 33 } 34 35 public int getSupportLevel(int feature, SearchQuery query) throws SearchQueryException { 37 int result; 38 switch (feature) { 39 case SearchQueryHandler.FEATURE_MAX_NUMBER: 40 result = SearchQueryHandler.SUPPORT_OPTIMAL; 41 break; 42 43 case SearchQueryHandler.FEATURE_OFFSET: 44 result = SearchQueryHandler.SUPPORT_OPTIMAL; 45 break; 46 47 default: 48 result = super.getSupportLevel(feature, query); 49 } 50 return result; 51 } 52 53 56 protected void appendDateField(StringBuffer sb, Step step, String fieldName, boolean multipleSteps, int datePart) { 57 String datePartFunction = null; 58 switch (datePart) { 59 case FieldValueDateConstraint.CENTURY: 60 datePartFunction = "CENTURY"; 61 break; 62 case FieldValueDateConstraint.QUARTER: 63 datePartFunction = "QUARTER"; 64 break; 65 case FieldValueDateConstraint.WEEK: 66 datePartFunction = "WEEK"; 67 break; 68 case FieldValueDateConstraint.DAY_OF_YEAR: 69 datePartFunction = "DAYOFYEAR"; 70 break; 71 case FieldValueDateConstraint.DAY_OF_WEEK: 72 datePartFunction = "DAYOFWEEK"; 73 break; 74 default: 75 log.debug("Unknown datePart " + datePart); 76 } 77 if (datePartFunction != null) { 78 sb.append(datePartFunction); 79 sb.append("("); 80 appendField(sb, step, fieldName, multipleSteps); 81 sb.append(")"); 82 } else { 83 super.appendDateField(sb, step, fieldName, multipleSteps, datePart); 84 } 85 } 86 87 public String toSql(SearchQuery query, SqlHandler firstInChain) throws SearchQueryException { 89 91 if (query.getSteps().isEmpty()) { 93 throw new IllegalStateException ( "Searchquery has no step (at leas 1 step is required)."); 94 } 95 if (query.getFields().isEmpty()) { 96 throw new IllegalStateException ("Searchquery has no field (at least 1 field is required)."); 97 } 98 99 StringBuffer sbQuery = new StringBuffer ("SELECT "); 101 102 103 104 int offset = query.getOffset(); 106 int limit = query.getMaxNumber(); 107 if (offset != 0 || limit > -1) { 108 if (offset == 0) { 109 sbQuery.append("TOP ").append(limit).append(' '); 110 } else { 111 if (limit == -1) { 112 sbQuery.append("LIMIT ").append(offset).append(" 0 "); 113 } else { 114 sbQuery.append("LIMIT ").append(offset).append(' ').append(limit).append(' '); 115 } 118 } 119 120 } 121 if (query.isDistinct()) { 123 sbQuery.append("DISTINCT "); 124 } 125 126 firstInChain.appendQueryBodyToSql(sbQuery, query, firstInChain); 127 128 String strSQL = sbQuery.toString(); 129 if (log.isDebugEnabled()) { 130 log.debug("generated SQL: " + query + " -- >" + strSQL); 131 } 132 return strSQL; 133 } 134 } 135 | Popular Tags |