Skip to content

Commit

Permalink
#master: Conditions and Attributes understand numeric arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Bugayenko committed May 11, 2014
1 parent 4e5364c commit 976ba6a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
8 changes: 7 additions & 1 deletion src/main/java/com/jcabi/dynamo/Attributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,13 @@ public Map<String, ExpectedAttributeValue> asKeys() {
public Attributes with(
@NotNull(message = "attribute name can't be NULL") final String name,
@NotNull(message = "value can't be NULL") final Object value) {
return this.with(name, new AttributeValue(value.toString()));
final AttributeValue attr;
if (value instanceof Long || value instanceof Integer) {
attr = new AttributeValue().withN(value.toString());
} else {
attr = new AttributeValue(value.toString());
}
return this.with(name, attr);
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/jcabi/dynamo/Conditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ public Conditions(@NotNull final Map<String, Condition> map) {
*/
@NotNull
public static Condition equalTo(@NotNull final Object value) {
return Conditions.equalTo(new AttributeValue(value.toString()));
final AttributeValue attr;
if (value instanceof Long || value instanceof Integer) {
attr = new AttributeValue().withN(value.toString());
} else {
attr = new AttributeValue().withS(value.toString());
}
return Conditions.equalTo(attr);
}

/**
Expand Down
27 changes: 23 additions & 4 deletions src/main/java/com/jcabi/dynamo/mock/H2Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
@ToString
@Loggable(Loggable.DEBUG)
@EqualsAndHashCode(of = "jdbc")
@SuppressWarnings("PMD.TooManyMethods")
public final class H2Data implements MkData {

/**
Expand Down Expand Up @@ -196,7 +197,7 @@ public Iterable<Attributes> iterate(final String table,
);
}
final AttributeValue val = cond.getAttributeValueList().get(0);
session = session.set(val.getS());
session = session.set(H2Data.value(val));
}
return session.select(H2Data.OUTCOME);
} catch (final SQLException ex) {
Expand All @@ -209,7 +210,7 @@ public void put(final String table, final Attributes attrs)
try {
JdbcSession session = new JdbcSession(this.connection());
for (final AttributeValue value : attrs.values()) {
session = session.set(value.getS());
session = session.set(H2Data.value(value));
}
session.sql(
String.format(
Expand All @@ -232,10 +233,10 @@ public void update(final String table, final Attributes keys,
try {
JdbcSession session = new JdbcSession(this.connection());
for (final AttributeValueUpdate value : attrs.values()) {
session = session.set(value.getValue().getS());
session = session.set(H2Data.value(value.getValue()));
}
for (final AttributeValue value : keys.values()) {
session = session.set(value.getS());
session = session.set(H2Data.value(value));
}
session.sql(
String.format(
Expand Down Expand Up @@ -300,4 +301,22 @@ private Connection connection() throws SQLException {
return new Driver().connect(this.jdbc, new Properties());
}

/**
* Get value from attribute.
* @param attr Attribute value
* @return Text format
*/
private static String value(final AttributeValue attr) {
String val = attr.getS();
if (val == null) {
val = attr.getN();
}
if (val == null) {
throw new IllegalArgumentException(
"we support only N and S at the moment"
);
}
return val;
}

}
10 changes: 5 additions & 5 deletions src/test/java/com/jcabi/dynamo/RegionITCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void worksWithAmazon() throws Exception {
MatcherAssert.assertThat(frame, Matchers.hasSize(Tv.FIVE));
final Iterator<Item> items = frame.iterator();
final Item item = items.next();
final String range = item.get(RegionITCase.RANGE).getS();
final int range = Integer.parseInt(item.get(RegionITCase.RANGE).getN());
MatcherAssert.assertThat(
item.get(attr).getS(),
Matchers.equalTo(value)
Expand All @@ -122,7 +122,7 @@ public void worksWithAmazon() throws Exception {
MatcherAssert.assertThat(
tbl.frame()
.where(RegionITCase.HASH, hash)
.where(RegionITCase.RANGE, range)
.where(RegionITCase.RANGE, Conditions.equalTo(range))
.through(new ScanValve())
.iterator().next()
.get(attr).getS(),
Expand All @@ -139,7 +139,7 @@ public void worksWithAmazon() throws Exception {
public void retrievesAttributesFromDynamo() throws Exception {
final String name = RandomStringUtils.randomAlphabetic(Tv.EIGHT);
final Table tbl = this.region(name).table(name);
final String idx = "2f7whf";
final int idx = Tv.TEN;
final String hash = "7afe5efa";
final String attr = "some-attribute";
tbl.put(
Expand All @@ -151,7 +151,7 @@ public void retrievesAttributesFromDynamo() throws Exception {
MatcherAssert.assertThat(
tbl.frame()
.where(RegionITCase.HASH, hash)
.where(RegionITCase.RANGE, idx)
.where(RegionITCase.RANGE, Conditions.equalTo(idx))
.through(
new QueryValve()
.withAttributeToGet(attr)
Expand Down Expand Up @@ -189,7 +189,7 @@ private Region region(final String table) throws Exception {
.withAttributeType(ScalarAttributeType.S),
new AttributeDefinition()
.withAttributeName(RegionITCase.RANGE)
.withAttributeType(ScalarAttributeType.S)
.withAttributeType(ScalarAttributeType.N)
)
.withKeySchema(
new KeySchemaElement()
Expand Down

0 comments on commit 976ba6a

Please sign in to comment.