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.jpa2.commands; 021 022import java.sql.Connection; 023import java.sql.ResultSet; 024import java.util.Iterator; 025 026import org.slf4j.Logger; 027 028import net.sf.jkniv.exception.HandleableException; 029import net.sf.jkniv.whinstone.Param; 030import net.sf.jkniv.whinstone.QueryFactory; 031import net.sf.jkniv.whinstone.Queryable; 032import net.sf.jkniv.whinstone.commands.Command; 033import net.sf.jkniv.whinstone.commands.CommandHandler; 034import net.sf.jkniv.whinstone.statement.StatementAdapter; 035 036/** 037 * 038 * @author Alisson Gomes 039 * @since 0.6.0 040 */ 041public class BulkJpaCommand implements Command 042{ 043 private final static Logger LOG = org.slf4j.LoggerFactory.getLogger(BulkJpaCommand.class); 044 protected HandleableException handlerException; 045 protected CommandHandler commandHandler; 046 protected final Queryable queryable; 047 protected StatementAdapter<?, ResultSet> stmt; 048 049 public BulkJpaCommand(Queryable queryable, Connection conn) 050 { 051 this.queryable = queryable; 052 } 053 054 @Override 055 public <T> Command with(T stmt) 056 { 057 this.stmt = (StatementAdapter) stmt; 058 return this; 059 } 060 061 @SuppressWarnings("unchecked") 062 public <T> T execute() 063 { 064 Integer rowsAffected = 0; 065 // TODO implements Bulk operation for JPA 066 try 067 { 068 Iterator<Param> it = queryable.iterator(); 069 while (it.hasNext()) 070 { 071 Queryable queryableIt = QueryFactory.of(queryable.getName(), it.next(), queryable.getOffset(), 072 queryable.getMax()); 073 //QueryableJpaAdapter queryableJpaAdapter = QueryJpaFactory.build(em, sqlContext, queryableIt, null); 074 //rowsAffected += queryableJpaAdapter.executeUpdate(); 075 } 076 077 //if (queryable.getDynamicSql().isBatch() || queryable.isTypeOfBulk()) 078 //rowsAffected = this.queryable.bind(stmt).onBulk(); 079 } 080 finally 081 { 082 stmt.close(); 083 } 084 return (T) rowsAffected; 085 } 086 087 @Override 088 public Command with(HandleableException handlerException) 089 { 090 this.handlerException = handlerException; 091 return this; 092 } 093 094 @Override 095 public Command with(CommandHandler commandHandler) 096 { 097 this.commandHandler = commandHandler; 098 return this; 099 } 100}