Permalink
Browse files

CQL3 stuff: checking for a cql row before inserting

  • Loading branch information...
1 parent a68dd39 commit 121cbe0a616e886cfd9ce4ffd7f1875ee528aa66 @easility easility committed Dec 15, 2013
Showing with 22 additions and 5 deletions.
  1. +22 −5 src/main/java/com/alvazan/orm/layer9z/spi/db/cassandracql3/CqlSession.java
@@ -101,13 +101,12 @@ private void persist(Persist action, MetaLookup ormSession) {
try {
PreparedStatement statement = session.prepare("INSERT INTO " + keys + "." + table + "(id, colname, colvalue) VALUES (?, ?, ?)");
BoundStatement boundStatement = new BoundStatement(statement);
+ String colName = StandardConverters.convertFromBytes(String.class, c.getName());
+ checkIfRowExsits(table, rowkey, colName);
if (c.getValue() != null && c.getValue().length != 0) {
- session.execute(boundStatement.bind(ByteBuffer.wrap(rowkey), StandardConverters.convertFromBytes(String.class, c.getName()),
- ByteBuffer.wrap(c.getValue())));
-
+ session.execute(boundStatement.bind(ByteBuffer.wrap(rowkey), colName, ByteBuffer.wrap(c.getValue())));
} else {
- session.execute(boundStatement.bind(ByteBuffer.wrap(rowkey), StandardConverters.convertFromBytes(String.class, c.getName()),
- ByteBuffer.wrap(new byte[0])));
+ session.execute(boundStatement.bind(ByteBuffer.wrap(rowkey), colName, ByteBuffer.wrap(new byte[0])));
}
} catch (Exception e) {
@@ -117,6 +116,24 @@ private void persist(Persist action, MetaLookup ormSession) {
}
+ private void checkIfRowExsits(String table, byte[] rowKey, String colName) {
+ Clause rkClause = QueryBuilder.eq("id", ByteBuffer.wrap(rowKey));
+ Clause cnClause = QueryBuilder.eq("colname", colName);
+ try {
+ Query query = QueryBuilder.select().all().from(keys, table).where(rkClause).and(cnClause);
+ ResultSet resultSet = session.execute(query);
+ if (resultSet.isExhausted())
+ return;
+ else {
+ Query delQuery = QueryBuilder.delete().from(keys, table).where(rkClause).and(cnClause);
+ session.execute(delQuery);
+ }
+ } catch (Exception e) {
+ System.out.println(" Exception:" + e.getMessage());
+ }
+
+ }
+
private void persistIndex(PersistIndex action, MetaLookup ormSession) {
String indexCfName = action.getIndexCfName();
String table = lookupOrCreate(indexCfName, ormSession);

0 comments on commit 121cbe0

Please sign in to comment.