|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package org.httprpc.sql; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class for dynamically constructing a SQL query. |
| 19 | + */ |
| 20 | +public class QueryBuilder { |
| 21 | + private StringBuilder sqlBuilder = new StringBuilder(); |
| 22 | + |
| 23 | + private QueryBuilder(String operation) { |
| 24 | + sqlBuilder.append(operation); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * TODO |
| 29 | + * @param columns |
| 30 | + * @return |
| 31 | + */ |
| 32 | + public static QueryBuilder select(String... columns) { |
| 33 | + if (columns == null) { |
| 34 | + throw new IllegalArgumentException(); |
| 35 | + } |
| 36 | + |
| 37 | + return new QueryBuilder("select " + String.join(", ", columns)); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * TODO |
| 42 | + * @param table |
| 43 | + * @param columns |
| 44 | + * @return |
| 45 | + */ |
| 46 | + public static QueryBuilder insertInto(String table, String... columns) { |
| 47 | + if (table == null) { |
| 48 | + throw new IllegalArgumentException(); |
| 49 | + } |
| 50 | + |
| 51 | + if (columns == null) { |
| 52 | + throw new IllegalArgumentException(); |
| 53 | + } |
| 54 | + |
| 55 | + return new QueryBuilder("insert into " + table + " (" + String.join(", ", columns) + ")"); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * TODO |
| 60 | + * @param table |
| 61 | + * @return |
| 62 | + */ |
| 63 | + public static QueryBuilder update(String table) { |
| 64 | + if (table == null) { |
| 65 | + throw new IllegalArgumentException(); |
| 66 | + } |
| 67 | + |
| 68 | + return new QueryBuilder("update " + table); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * TODO |
| 73 | + * @param table |
| 74 | + * @return |
| 75 | + */ |
| 76 | + public static QueryBuilder deleteFrom(String table) { |
| 77 | + if (table == null) { |
| 78 | + throw new IllegalArgumentException(); |
| 79 | + } |
| 80 | + |
| 81 | + return new QueryBuilder("delete from " + table); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * TODO |
| 86 | + * @param tables |
| 87 | + * @return |
| 88 | + */ |
| 89 | + public QueryBuilder from(String... tables) { |
| 90 | + if (tables == null) { |
| 91 | + throw new IllegalArgumentException(); |
| 92 | + } |
| 93 | + |
| 94 | + sqlBuilder.append(" from "); |
| 95 | + sqlBuilder.append(String.join(", ", tables)); |
| 96 | + |
| 97 | + return this; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * TODO |
| 102 | + * @param table |
| 103 | + * @return |
| 104 | + */ |
| 105 | + public QueryBuilder join(String table) { |
| 106 | + if (table == null) { |
| 107 | + throw new IllegalArgumentException(); |
| 108 | + } |
| 109 | + |
| 110 | + sqlBuilder.append(" join "); |
| 111 | + sqlBuilder.append(table); |
| 112 | + |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * TODO |
| 118 | + * @param predicate |
| 119 | + * @return |
| 120 | + */ |
| 121 | + public QueryBuilder on(String predicate) { |
| 122 | + if (predicate == null) { |
| 123 | + throw new IllegalArgumentException(); |
| 124 | + } |
| 125 | + |
| 126 | + sqlBuilder.append(" on "); |
| 127 | + sqlBuilder.append(predicate); |
| 128 | + |
| 129 | + return this; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * TODO |
| 134 | + * @param predicate |
| 135 | + * @return |
| 136 | + */ |
| 137 | + public QueryBuilder where(String predicate) { |
| 138 | + if (predicate == null) { |
| 139 | + throw new IllegalArgumentException(); |
| 140 | + } |
| 141 | + |
| 142 | + sqlBuilder.append(" where "); |
| 143 | + sqlBuilder.append(predicate); |
| 144 | + |
| 145 | + return this; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * TODO |
| 150 | + * @param columns |
| 151 | + * @return |
| 152 | + */ |
| 153 | + public QueryBuilder orderBy(String... columns) { |
| 154 | + if (columns == null) { |
| 155 | + throw new IllegalArgumentException(); |
| 156 | + } |
| 157 | + |
| 158 | + sqlBuilder.append(" order by "); |
| 159 | + sqlBuilder.append(String.join(", ", columns)); |
| 160 | + |
| 161 | + return this; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * TODO |
| 166 | + * @param values |
| 167 | + * @return |
| 168 | + */ |
| 169 | + public QueryBuilder values(Object... values) { |
| 170 | + if (values == null) { |
| 171 | + throw new IllegalArgumentException(); |
| 172 | + } |
| 173 | + |
| 174 | + sqlBuilder.append(" values ("); |
| 175 | + |
| 176 | + for (int i = 0; i < values.length; i++) { |
| 177 | + if (i > 0) { |
| 178 | + sqlBuilder.append(", "); |
| 179 | + } |
| 180 | + |
| 181 | + sqlBuilder.append(values[i]); |
| 182 | + } |
| 183 | + |
| 184 | + sqlBuilder.append(")"); |
| 185 | + |
| 186 | + return this; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * TODO |
| 191 | + * @param column |
| 192 | + * @param value |
| 193 | + * @return |
| 194 | + */ |
| 195 | + public QueryBuilder set(String column, Object value) { |
| 196 | + sqlBuilder.append(" set "); |
| 197 | + sqlBuilder.append(column); |
| 198 | + sqlBuilder.append(" = "); |
| 199 | + sqlBuilder.append(value); |
| 200 | + |
| 201 | + return this; |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public String toString() { |
| 206 | + return sqlBuilder.toString(); |
| 207 | + } |
| 208 | +} |
0 commit comments