-
Notifications
You must be signed in to change notification settings - Fork 164
/
Dollar.java
166 lines (122 loc) · 4.26 KB
/
Dollar.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package org.rapidoid.app;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.concurrent.Callback;
import org.rapidoid.http.HTTP;
import org.rapidoid.http.HttpClient;
import org.rapidoid.http.HttpExchange;
import org.rapidoid.http.REST;
import org.rapidoid.http.RESTClient;
import org.rapidoid.plugins.Plugins;
import org.rapidoid.plugins.cache.CachePlugin;
import org.rapidoid.plugins.db.DBPlugin;
import org.rapidoid.plugins.email.EmailPlugin;
import org.rapidoid.plugins.entities.EntitiesPlugin;
import org.rapidoid.plugins.languages.LanguagesPlugin;
import org.rapidoid.plugins.sms.SMSPlugin;
import org.rapidoid.plugins.templates.TemplatesPlugin;
import org.rapidoid.plugins.users.UsersPlugin;
import org.rapidoid.sql.SQL;
import org.rapidoid.sql.SQLAPI;
import org.rapidoid.u.U;
import redis.clients.jedis.Jedis;
/*
* #%L
* rapidoid-app
* %%
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
@Authors("Nikolche Mihajlovski")
@Since("4.1.0")
public class Dollar {
/************ DATA CONTAINERS *************/
public final Map<?, ?> extra = U.map();
public final Map<Object, Map<Object, Object>> maps = U.mapOfMaps();
public final Map<Object, List<Object>> lists = U.mapOfLists();
public final Map<Object, Set<Object>> sets = U.mapOfSets();
/************ SERVICES *************/
public final HttpClient http = HTTP.DEFAULT_CLIENT;
public final RESTClient services = REST.DEFAULT_CLIENT;
public final LanguagesPlugin languages = Plugins.languages();
public final DBPlugin db = Plugins.db();
public final DBPlugin hibernate = Plugins.db("hibernate");
public final DBPlugin cassandra = Plugins.db("cassandra");
public final EntitiesPlugin entities = Plugins.entities();
public final UsersPlugin users = Plugins.users();
public final EmailPlugin email = Plugins.email();
public final SMSPlugin sms = Plugins.sms();
public final CachePlugin cache = Plugins.cache();
public final CachePlugin memcached = Plugins.cache("memcached");
public final TemplatesPlugin templates = Plugins.templates();
public final SQLAPI mysql = SQL.defaultInstance();
public final IOTool io = new IOToolImpl();
public final SQLAPI jdbc = SQL.defaultInstance();
public final HttpExchange req;
public final Map<String, Object> bindings;
private volatile boolean hasResult = false;
public Dollar(HttpExchange x, Map<String, Object> bindings) {
this.req = x;
this.bindings = bindings;
}
@Override
public String toString() {
return "$";
}
public List<Map<String, Object>> sql(String sql, Object[] args) {
if (sql.trim().toLowerCase().startsWith("select ")) {
return jdbc.query(sql, args);
} else {
jdbc.execute(sql, args);
return null;
}
}
public List<Map<String, Object>> sql(String sql) {
return sql(sql, new Object[0]);
}
public List<Map<String, Object>> cql(String cql, Object[] args) {
return cassandra.query(cql, args);
}
public void cql(String cql, Object[] args, Callback<List<Map<String, Object>>> callback) {
cassandra.queryAsync(cql, callback, args);
}
public List<Map<String, Object>> cql(String cql) {
return cql(cql, new Object[0]);
}
public DollarPage page(Object value, Map<String, Object> config) {
return new DollarPage(value, config);
}
@SuppressWarnings("unchecked")
public DollarPage page(Object value) {
return page(value, Collections.EMPTY_MAP);
}
public Jedis redis() {
return JedisTool.get();
}
public void result(Object result) {
hasResult = true;
Scripting.onScriptResult(req, result);
}
public boolean hasResult() {
return hasResult;
}
public String query() {
return req.dbQuery();
}
}