Skip to content
This repository has been archived by the owner on Jun 16, 2020. It is now read-only.

Commit

Permalink
Removed whitespace changes caused by IDE
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimitriy Remerov committed Sep 8, 2016
1 parent 064d5e2 commit 4b5fa8a
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions src/main/java/org/wololo/jdbc/resources/RowsResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class RowsResource extends DataSourceResource {
@PathParam("databaseName") String databaseName;
@PathParam("schemaName") String schemaName;
@PathParam("tableName") String tableName;

static final Pattern wherePattern = Pattern.compile("(\\w+)([<>!=])(\\w+|'\\w+')");
static final Map<String, Comparator> comparatorMap = new HashMap<String, Comparator>();
static {
Expand All @@ -64,7 +64,7 @@ public class RowsResource extends DataSourceResource {
comparatorMap.put("=<", Comparator.LESS_OR_EQUAL);
comparatorMap.put("LIKE", Comparator.LIKE);
}

@GET
@Produces("application/json")
public StreamingOutput get(
Expand All @@ -73,22 +73,22 @@ public StreamingOutput get(
@DefaultValue("0") @QueryParam("limit") final String limit,
@DefaultValue("0") @QueryParam("offset") final String offset,
@DefaultValue("") @QueryParam("orderby") final String orderby) throws SQLException {

final List<Field<Object>> fields = parseSelectParam(select);

return new StreamingOutput() {
@Override
public void write(OutputStream output) throws IOException, WebApplicationException {
writeRows(output,
fields,
where,
parseNumericParam(limit),
parseNumericParam(offset),
writeRows(output,
fields,
where,
parseNumericParam(limit),
parseNumericParam(offset),
orderby);
}
};
}

@POST
public void post(Map<String, Object> row) throws SQLException {
InsertSetStep<Record> step = create.insertInto(table(tableName));
Expand All @@ -97,11 +97,11 @@ public void post(Map<String, Object> row) throws SQLException {
query = step.set(field(entry.getKey()), entry.getValue());
}
logger.debug(query.getSQL());

final int result = query.execute();
if (result != 1) throw new RuntimeException("Unexpected result " + result + " (expected 1)");
}

List<Field<Object>> parseSelectParam(final String select) throws SQLException {
if (select.equals("*")) {
return getFields();
Expand All @@ -114,7 +114,7 @@ List<Field<Object>> parseSelectParam(final String select) throws SQLException {
return fields;
}
};

@SuppressWarnings({ "unchecked", "rawtypes" })
SortField<Object>[] parseOrderbyParam(final String orderby) {
if (orderby.equals("")) {
Expand All @@ -133,27 +133,27 @@ SortField<Object>[] parseOrderbyParam(final String orderby) {
fields.add(field(subparts[0]).desc());
}
}

}
return fields.toArray(new SortField[] { });
}
};

int parseNumericParam(final String limit) {
if (limit.equals("0")) {
return 0;
} else {
return Integer.parseInt(limit);
}
}

Condition parseWhere(String where) {
Matcher matcher = wherePattern.matcher(where);
if (matcher.matches() != true) {
throw new RuntimeException("Unexpected where clause (must conform to a op b)");
}
int groups = matcher.groupCount();

if (groups == 3) {
String a = matcher.group(1);
String op = matcher.group(2);
Expand All @@ -163,36 +163,36 @@ Condition parseWhere(String where) {
throw new RuntimeException("Unexpected where clause (must conform to a op b)");
}
}

void writeRows(
final OutputStream output,
final List<Field<Object>> fields,
final String where,
final int limit,
final int offset,
final String orderby) throws IOException {

SelectJoinStep<Record> query = create.select(fields).from(schemaName + "." + tableName);

if (where.length()>0) {
query.where(parseWhere(where));
}

if (limit>0) {
SelectOffsetStep<Record> offsetStep = query.limit(limit);
if (offset>0) {
offsetStep.offset(offset);
}
}

if (orderby.length()>0) {
query.orderBy(parseOrderbyParam(orderby));
}

logger.debug(query.getSQL());
final JsonGenerator jsonGenerator = new JsonFactory().createGenerator(output, JsonEncoding.UTF8);
jsonGenerator.writeStartArray();

final Cursor<Record> cursor = query.fetchLazy();
try {
while (cursor.hasNext()) {
Expand All @@ -206,15 +206,15 @@ void writeRows(
jsonGenerator.close();
}
}

void writeRow(final JsonGenerator jsonGenerator, final Record record) throws SQLException, JsonGenerationException, IOException {
jsonGenerator.writeStartObject();
for (Map.Entry<String, Object> entry : record.intoMap().entrySet()) {
jsonGenerator.writeObjectField(entry.getKey(), entry.getValue());
}
jsonGenerator.writeEndObject();
}

List<Field<Object>> getFields() throws SQLException {
try (Connection connection = ds.getConnection()) {
DatabaseMetaData meta = connection.getMetaData();
Expand Down

0 comments on commit 4b5fa8a

Please sign in to comment.