summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-05-10 19:16:19 +0200
committerPeter Wu <peter@lekensteyn.nl>2014-05-10 19:16:19 +0200
commit05ae05165aae38047b56000ce3bf1d2f1d785621 (patch)
tree10c6a0a2d80f08575a22db0d0a7e7643cc140ee5
parent2f48d685150462c2c426edca019f4644f466ee25 (diff)
downloadDatafiller-05ae05165aae38047b56000ce3bf1d2f1d785621.tar.gz
Fix timestamp type
Without this cast, setTimestamp would complain that a timestamp is expected, but a text type is given.
-rw-r--r--src/database/NamedPreparedStatement.java4
-rw-r--r--src/database/QueryUtils.java13
2 files changed, 10 insertions, 7 deletions
diff --git a/src/database/NamedPreparedStatement.java b/src/database/NamedPreparedStatement.java
index 398ef60..089e2c7 100644
--- a/src/database/NamedPreparedStatement.java
+++ b/src/database/NamedPreparedStatement.java
@@ -25,12 +25,12 @@ public class NamedPreparedStatement {
public NamedPreparedStatement(Connection conn, String query) throws SQLException {
fields = new ArrayList<>();
- Pattern pattern = Pattern.compile(":(\\w+)");
+ Pattern pattern = Pattern.compile("(?<!:):(\\w+)(::\\w+)?");
Matcher matcher = pattern.matcher(query);
while (matcher.find()) {
fields.add(matcher.group(1));
}
- String sql = query.replaceAll(pattern.pattern(), "?");
+ String sql = query.replaceAll(pattern.pattern(), "?$2");
stmt = conn.prepareStatement(sql);
}
diff --git a/src/database/QueryUtils.java b/src/database/QueryUtils.java
index ff26ec4..eaf6a2d 100644
--- a/src/database/QueryUtils.java
+++ b/src/database/QueryUtils.java
@@ -14,12 +14,13 @@ public class QueryUtils {
public final static String insertProfile
= buildQuery("twitteruser", new String[]{"userid"},
"userid", "displayname", "timezone", "tweetcount", "followercount",
- "followedcount", "location", "tweetname", "createdat", "language");
+ "followedcount", "location", "tweetname", "createdat::timestamptz",
+ "language");
public final static String insertTweet
= buildQuery("tweet", new String[]{"tweetid"},
- "tweetid", "createdat", "favcount", "retweetcount", "text",
- "coordinates", "language", "retweetid", "replyid", "place",
+ "tweetid", "createdat::timestamptz", "favcount", "retweetcount",
+ "text", "coordinates", "language", "retweetid", "replyid", "place",
"userid");
public final static String insertHash
@@ -53,9 +54,10 @@ public class QueryUtils {
String values = ""; // :a, :b, :c
String set_values = ""; // a = nv.a, b = nv.b, c = nv.c
String pkey_matches = "";
- for (String key : keys) {
+ for (String field : keys) {
+ String key = field.replaceFirst("::\\w+", "");
fields += sep + key;
- values += sep + ":" + key;
+ values += sep + ":" + field;
// "u" is "table that gets updated", "nv" is "new values"
set_values += sep + key + " = nv." + key;
sep = ", ";
@@ -66,6 +68,7 @@ public class QueryUtils {
primaryKeys = keys;
}
for (String pkey : primaryKeys) {
+ assert !pkey.contains("::") : "Specify primary keys when type cast is given";
pkey_matches += sep + "u." + pkey + " = nv." + pkey;
sep = " AND ";
}