001/* 002 * JKNIV, whinstone one contract to access your database. 003 * 004 * Copyright (C) 2017, the original author or authors. 005 * 006 * This library is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 2.1 of the License. 010 * 011 * This library is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with this library; if not, write to the Free Software Foundation, Inc., 018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 019 */ 020package net.sf.jkniv.whinstone.cassandra.commands; 021 022import java.util.ArrayList; 023import java.util.Iterator; 024import java.util.List; 025 026import org.slf4j.Logger; 027 028import com.datastax.driver.core.Row; 029import com.datastax.driver.core.Session; 030import com.datastax.driver.core.SimpleStatement; 031import com.datastax.driver.core.Statement; 032 033import net.sf.jkniv.exception.HandleableException; 034import net.sf.jkniv.sqlegance.Insertable; 035import net.sf.jkniv.whinstone.Queryable; 036import net.sf.jkniv.whinstone.statement.AutoKey; 037 038/** 039 * Call an database sequence and put the value into parameter object from {@link Queryable} 040 * 041 * @author Alisson Gomes 042 * @since 0.6.0 043 */ 044public class CassandraSequenceGeneratedKey implements AutoKey<Object> 045{ 046 private static final Logger SQLLOG = net.sf.jkniv.whinstone.cassandra.LoggerFactory.getLogger(); 047 048 private List<Object> keys; 049 050 public CassandraSequenceGeneratedKey(Insertable isql, Session session, HandleableException handlerException) 051 { 052 this.keys = new ArrayList<Object>(); 053 //try 054 //{ 055 String query = isql.getAutoGeneratedKey().getText(); 056 SQLLOG.info(query); 057 Statement statement = new SimpleStatement(query); 058 com.datastax.driver.core.ResultSet generatedKeys= session.execute(statement); 059 for(Row row : generatedKeys.all()) 060 { 061 Object id = row.getObject(0); 062 this.keys.add(String.valueOf(id)); 063 } 064 //} 065 //catch (SQLException e) 066 //{ 067 // handlerException.handle(e); 068 //} 069 } 070 071 @Override 072 public Object getId() 073 { 074 if (isEmpty()) 075 return null; 076 077 return (Long) this.keys.get(0); 078 } 079 080 @Override 081 public String getUId() 082 { 083 if (isEmpty()) 084 return null; 085 086 return String.valueOf(this.keys.get(0)); 087 } 088 089 @Override 090 public Iterator<Object> iterator() 091 { 092 return this.keys.iterator(); 093 } 094 095 @Override 096 public boolean hasItem() 097 { 098 return this.keys.size() > 0; 099 } 100 101 @Override 102 public boolean isEmpty() 103 { 104 return this.keys.isEmpty(); 105 } 106 107 @Override 108 public int size() 109 { 110 return this.keys.size(); 111 } 112 113}