kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | c52f9bfb9370b44fa8e541949cf86f212b0ab6ce1453763736f603b9c9c94a9f |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2016-05-31 17:32 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-07-22 09:47 -07:00 |
| Parents | 42b5c0e4eb36 |
1 file changed
modified driver-mapping/src/main/java/com/datastax/driver/mapping/QueryType.java +65 −151
| @@ -19,171 +19,85 @@ | ||
| 19 | 19 | import com.datastax.driver.core.querybuilder.Delete; |
| 20 | 20 | import com.datastax.driver.core.querybuilder.Insert; |
| 21 | 21 | import com.datastax.driver.core.querybuilder.Select; |
| 22 | -import com.google.common.base.Objects; | |
| 23 | 22 | |
| 24 | -import java.util.ArrayList; | |
| 25 | 23 | import java.util.Collection; |
| 26 | -import java.util.List; | |
| 27 | 24 | import java.util.Set; |
| 28 | 25 | |
| 29 | 26 | import static com.datastax.driver.core.querybuilder.QueryBuilder.*; |
| 30 | 27 | |
| 31 | -class QueryType { | |
| 32 | - | |
| 33 | - private enum Kind {SAVE, GET, DEL, SLICE, REVERSED_SLICE} | |
| 34 | - | |
| 35 | - private final Kind kind; | |
| 36 | - | |
| 37 | - // For slices | |
| 38 | - private final int startBoundSize; | |
| 39 | - private final boolean startInclusive; | |
| 40 | - private final int endBoundSize; | |
| 41 | - private final boolean endInclusive; | |
| 42 | - | |
| 43 | - static final QueryType SAVE = new QueryType(Kind.SAVE); | |
| 44 | - static final QueryType DEL = new QueryType(Kind.DEL); | |
| 45 | - static final QueryType GET = new QueryType(Kind.GET); | |
| 46 | - | |
| 47 | - private QueryType(Kind kind) { | |
| 48 | - this(kind, 0, false, 0, false); | |
| 49 | - } | |
| 50 | - | |
| 51 | - private QueryType(Kind kind, int startBoundSize, boolean startInclusive, int endBoundSize, boolean endInclusive) { | |
| 52 | - this.kind = kind; | |
| 53 | - this.startBoundSize = startBoundSize; | |
| 54 | - this.startInclusive = startInclusive; | |
| 55 | - this.endBoundSize = endBoundSize; | |
| 56 | - this.endInclusive = endInclusive; | |
| 57 | - } | |
| 58 | - | |
| 59 | - String makePreparedQueryString(TableMetadata table, EntityMapper<?> mapper, MappingManager manager, Set<PropertyMapper> columns, Collection<Mapper.Option> options) { | |
| 60 | - switch (kind) { | |
| 61 | - case SAVE: { | |
| 62 | - Insert insert = table == null | |
| 63 | - ? insertInto(mapper.getKeyspace(), mapper.getTable()) | |
| 64 | - : insertInto(table); | |
| 65 | - for (PropertyMapper col : columns) | |
| 66 | - if (!col.isComputed()) | |
| 67 | - insert.value(col.columnName, bindMarker()); | |
| 68 | - | |
| 69 | - Insert.Options usings = insert.using(); | |
| 70 | - for (Mapper.Option opt : options) { | |
| 71 | - opt.checkValidFor(QueryType.SAVE, manager); | |
| 72 | - if (opt.isIncludedInQuery()) | |
| 73 | - opt.appendTo(usings); | |
| 74 | - } | |
| 75 | - return insert.toString(); | |
| 28 | +enum QueryType { | |
| 29 | + | |
| 30 | + SAVE { | |
| 31 | + @Override | |
| 32 | + String makePreparedQueryString(TableMetadata table, EntityMapper<?> mapper, MappingManager manager, Set<PropertyMapper> columns, Collection<Mapper.Option> options) { | |
| 33 | + Insert insert = table == null | |
| 34 | + ? insertInto(mapper.getKeyspace(), mapper.getTable()) | |
| 35 | + : insertInto(table); | |
| 36 | + for (PropertyMapper col : columns) | |
| 37 | + if (!col.isComputed()) | |
| 38 | + insert.value(col.columnName, bindMarker()); | |
| 39 | + | |
| 40 | + Insert.Options usings = insert.using(); | |
| 41 | + for (Mapper.Option opt : options) { | |
| 42 | + opt.checkValidFor(QueryType.SAVE, manager); | |
| 43 | + if (opt.isIncludedInQuery()) | |
| 44 | + opt.appendTo(usings); | |
| 76 | 45 | } |
| 77 | - case GET: { | |
| 78 | - Select.Selection selection = select(); | |
| 79 | - for (PropertyMapper col : mapper.allColumns) { | |
| 80 | - Select.SelectionOrAlias column = col.isComputed() | |
| 81 | - ? selection.raw(col.columnName) | |
| 82 | - : selection.column(col.columnName); | |
| 46 | + return insert.toString(); | |
| 47 | + } | |
| 83 | 48 | |
| 84 | - if (col.alias == null) { | |
| 85 | - selection = column; | |
| 86 | - } else { | |
| 87 | - selection = column.as(col.alias); | |
| 88 | - } | |
| 89 | - } | |
| 90 | - Select select; | |
| 91 | - if (table == null) { | |
| 92 | - select = selection.from(mapper.getKeyspace(), mapper.getTable()); | |
| 49 | + }, | |
| 50 | + | |
| 51 | + GET { | |
| 52 | + @Override | |
| 53 | + String makePreparedQueryString(TableMetadata table, EntityMapper<?> mapper, MappingManager manager, Set<PropertyMapper> columns, Collection<Mapper.Option> options) { | |
| 54 | + Select.Selection selection = select(); | |
| 55 | + for (PropertyMapper col : mapper.allColumns) { | |
| 56 | + Select.SelectionOrAlias column = col.isComputed() | |
| 57 | + ? selection.raw(col.columnName) | |
| 58 | + : selection.column(col.columnName); | |
| 59 | + | |
| 60 | + if (col.alias == null) { | |
| 61 | + selection = column; | |
| 93 | 62 | } else { |
| 94 | - select = selection.from(table); | |
| 63 | + selection = column.as(col.alias); | |
| 95 | 64 | } |
| 96 | - Select.Where where = select.where(); | |
| 97 | - for (int i = 0; i < mapper.primaryKeySize(); i++) | |
| 98 | - where.and(eq(mapper.getPrimaryKeyColumn(i).columnName, bindMarker())); | |
| 99 | - | |
| 100 | - for (Mapper.Option opt : options) | |
| 101 | - opt.checkValidFor(QueryType.GET, manager); | |
| 102 | - return select.toString(); | |
| 103 | 65 | } |
| 104 | - case DEL: { | |
| 105 | - Delete delete = table == null | |
| 106 | - ? delete().all().from(mapper.getKeyspace(), mapper.getTable()) | |
| 107 | - : delete().all().from(table); | |
| 108 | - Delete.Where where = delete.where(); | |
| 109 | - for (int i = 0; i < mapper.primaryKeySize(); i++) | |
| 110 | - where.and(eq(mapper.getPrimaryKeyColumn(i).columnName, bindMarker())); | |
| 111 | - Delete.Options usings = delete.using(); | |
| 112 | - for (Mapper.Option opt : options) { | |
| 113 | - opt.checkValidFor(QueryType.DEL, manager); | |
| 114 | - if (opt.isIncludedInQuery()) | |
| 115 | - opt.appendTo(usings); | |
| 116 | - } | |
| 117 | - return delete.toString(); | |
| 66 | + Select select; | |
| 67 | + if (table == null) { | |
| 68 | + select = selection.from(mapper.getKeyspace(), mapper.getTable()); | |
| 69 | + } else { | |
| 70 | + select = selection.from(table); | |
| 118 | 71 | } |
| 119 | - case SLICE: | |
| 120 | - case REVERSED_SLICE: { | |
| 121 | - Select select = table == null | |
| 122 | - ? select().all().from(mapper.getKeyspace(), mapper.getTable()) | |
| 123 | - : select().all().from(table); | |
| 124 | - Select.Where where = select.where(); | |
| 125 | - for (int i = 0; i < mapper.partitionKeys.size(); i++) | |
| 126 | - where.and(eq(mapper.partitionKeys.get(i).columnName, bindMarker())); | |
| 127 | - | |
| 128 | - if (startBoundSize > 0) { | |
| 129 | - if (startBoundSize == 1) { | |
| 130 | - String name = mapper.clusteringColumns.get(0).columnName; | |
| 131 | - where.and(startInclusive ? gte(name, bindMarker()) : gt(name, bindMarker())); | |
| 132 | - } else { | |
| 133 | - List<String> names = new ArrayList<String>(startBoundSize); | |
| 134 | - List<Object> values = new ArrayList<Object>(startBoundSize); | |
| 135 | - for (int i = 0; i < startBoundSize; i++) { | |
| 136 | - names.add(mapper.clusteringColumns.get(i).columnName); | |
| 137 | - values.add(bindMarker()); | |
| 138 | - } | |
| 139 | - where.and(startInclusive ? gte(names, values) : gt(names, values)); | |
| 140 | - } | |
| 141 | - } | |
| 142 | - | |
| 143 | - if (endBoundSize > 0) { | |
| 144 | - if (endBoundSize == 1) { | |
| 145 | - String name = mapper.clusteringColumns.get(0).columnName; | |
| 146 | - where.and(endInclusive ? gte(name, bindMarker()) : gt(name, bindMarker())); | |
| 147 | - } else { | |
| 148 | - List<String> names = new ArrayList<String>(endBoundSize); | |
| 149 | - List<Object> values = new ArrayList<Object>(endBoundSize); | |
| 150 | - for (int i = 0; i < endBoundSize; i++) { | |
| 151 | - names.add(mapper.clusteringColumns.get(i).columnName); | |
| 152 | - values.add(bindMarker()); | |
| 153 | - } | |
| 154 | - where.and(endInclusive ? lte(names, values) : lt(names, values)); | |
| 72 | + Select.Where where = select.where(); | |
| 73 | + for (int i = 0; i < mapper.primaryKeySize(); i++) | |
| 74 | + where.and(eq(mapper.getPrimaryKeyColumn(i).columnName, bindMarker())); | |
| 75 | + | |
| 76 | + for (Mapper.Option opt : options) | |
| 77 | + opt.checkValidFor(QueryType.GET, manager); | |
| 78 | + return select.toString(); | |
| 79 | + } | |
| 80 | + }, | |
| 81 | + | |
| 82 | + DEL { | |
| 83 | + @Override | |
| 84 | + String makePreparedQueryString(TableMetadata table, EntityMapper<?> mapper, MappingManager manager, Set<PropertyMapper> columns, Collection<Mapper.Option> options) { | |
| 85 | + Delete delete = table == null | |
| 86 | + ? delete().all().from(mapper.getKeyspace(), mapper.getTable()) | |
| 87 | + : delete().all().from(table); | |
| 88 | + Delete.Where where = delete.where(); | |
| 89 | + for (int i = 0; i < mapper.primaryKeySize(); i++) | |
| 90 | + where.and(eq(mapper.getPrimaryKeyColumn(i).columnName, bindMarker())); | |
| 91 | + Delete.Options usings = delete.using(); | |
| 92 | + for (Mapper.Option opt : options) { | |
| 93 | + opt.checkValidFor(QueryType.DEL, manager); | |
| 94 | + if (opt.isIncludedInQuery()) | |
| 95 | + opt.appendTo(usings); | |
| 155 | 96 | } |
| 156 | - } | |
| 157 | - | |
| 158 | - select = select.limit(bindMarker()); | |
| 159 | - | |
| 160 | - if (kind == Kind.REVERSED_SLICE) | |
| 161 | - select = select.orderBy(desc(mapper.clusteringColumns.get(0).columnName)); | |
| 162 | - | |
| 163 | - return select.toString(); | |
| 164 | - } | |
| 97 | + return delete.toString(); | |
| 165 | 98 | } |
| 166 | - throw new AssertionError(); | |
| 167 | - } | |
| 168 | - | |
| 169 | - @Override | |
| 170 | - public boolean equals(Object obj) { | |
| 171 | - if (this == obj) | |
| 172 | - return true; | |
| 173 | - if (obj == null || this.getClass() != obj.getClass()) | |
| 174 | - return false; | |
| 175 | - | |
| 176 | - QueryType that = (QueryType) obj; | |
| 177 | - return kind == that.kind | |
| 178 | - && startBoundSize == that.startBoundSize | |
| 179 | - && startInclusive == that.startInclusive | |
| 180 | - && endBoundSize == that.endBoundSize | |
| 181 | - && endInclusive == that.endInclusive; | |
| 182 | - } | |
| 99 | + }; | |
| 183 | 100 | |
| 184 | - @Override | |
| 185 | - public int hashCode() { | |
| 186 | - return Objects.hashCode(kind, startBoundSize, startInclusive, endBoundSize, endInclusive); | |
| 187 | - } | |
| 101 | + abstract String makePreparedQueryString(TableMetadata table, EntityMapper<?> mapper, MappingManager manager, Set<PropertyMapper> columns, Collection<Mapper.Option> options); | |
| 188 | 102 | |
| 189 | 103 | } |