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.jdbc.commands; 021 022import java.sql.PreparedStatement; 023import java.sql.ResultSet; 024import java.sql.SQLException; 025import java.util.ArrayList; 026import java.util.Iterator; 027import java.util.List; 028 029import net.sf.jkniv.exception.HandleableException; 030import net.sf.jkniv.whinstone.Queryable; 031import net.sf.jkniv.whinstone.statement.AutoKey; 032 033/** 034 * Call an database sequence and put the value into parameter object from {@link Queryable} 035 * 036 * @author Alisson Gomes 037 * @since 0.6.0 038 */ 039public class JdbcAutoGeneratedKey implements AutoKey<Long> 040{ 041 private List<Long> keys; 042 private final PreparedStatement stmt; 043 private final HandleableException handlerException; 044 private boolean initated; 045 046 public JdbcAutoGeneratedKey(PreparedStatement stmt, HandleableException handlerException) 047 { 048 this.keys = new ArrayList<Long>(); 049 this.stmt = stmt; 050 this.handlerException = handlerException; 051 this.initated = false; 052 } 053 054 private void generateKeys() 055 { 056 this.initated = true; 057 try 058 { 059 ResultSet generatedKeys = stmt.getGeneratedKeys(); 060 while (generatedKeys.next()) 061 { 062 Long id = generatedKeys.getLong(1); 063 this.keys.add(id); 064 } 065 } 066 catch (SQLException e) 067 { 068 handlerException.handle(e); 069 } 070 } 071 072 @Override 073 public Long getId() 074 { 075 if (!initated) generateKeys(); 076 077 if (isEmpty()) 078 return null; 079 080 return (Long) this.keys.get(0); 081 } 082 083 @Override 084 public String getUId() 085 { 086 if (!initated) generateKeys(); 087 088 if (isEmpty()) 089 return null; 090 091 return String.valueOf(this.keys.get(0)); 092 } 093 094 @Override 095 public Iterator<Long> iterator() 096 { 097 if (!initated) generateKeys(); 098 099 return this.keys.iterator(); 100 } 101 102 @Override 103 public boolean hasItem() 104 { 105 if (!initated) generateKeys(); 106 107 return this.keys.size() > 0; 108 } 109 110 @Override 111 public boolean isEmpty() 112 { 113 if (!initated) generateKeys(); 114 115 return this.keys.isEmpty(); 116 } 117 118 @Override 119 public int size() 120 { 121 if (!initated) generateKeys(); 122 123 return this.keys.size(); 124 } 125}