package votorola.a.count; // Copyright 2009, 2012, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import java.sql.*; import votorola.a.*; import votorola.a.voter.*; import votorola.g.*; import votorola.g.lang.*; import votorola.g.logging.*; import votorola.g.sql.*; /** The voter input table for all polls. */ public final @ThreadSafe class InputTable extends VoterInputTable { /** Partially constructs an InputTable. * * @see #voterService() * @see #init() */ InputTable( PollService _voterService ) { super( _voterService, "in_vote" ); } public @Override void init() throws SQLException { final String sKey = statementKeyBase + "init"; synchronized( database ) { PreparedStatement s = database.statementCache().get( sKey ); if( s == null ) { s = database.connection().prepareStatement( "CREATE TABLE IF NOT EXISTS \"" + tableName + "\"" + " (serviceName character varying," + " voterEmail character varying," + " xml character varying NOT NULL," + " PRIMARY KEY ( serviceName, voterEmail ))" ); database.statementCache().put( sKey, s ); } s.execute(); } } // - V o t e r - I n p u t - T a b l e ------------------------------------------------ public @Override void delete( final String voterEmail ) throws SQLException { final String sKey = statementKeyBase + "delete"; synchronized( database ) { PreparedStatement s = database.statementCache().get( sKey ); if( s == null ) { s = database.connection().prepareStatement( "DELETE FROM \"" + tableName + "\" WHERE serviceName = ? AND voterEmail = ?" ); database.statementCache().put( sKey, s ); } s.setString( 1, voterService.name() ); s.setString( 2, voterEmail ); s.executeUpdate(); } } public @Override String get( final String voterEmail ) throws SQLException { final String sKey = statementKeyBase + "get"; synchronized( database ) { PreparedStatement s = database.statementCache().get( sKey ); if( s == null ) { s = database.connection().prepareStatement( "SELECT xml FROM \"" + tableName + "\"" + " WHERE serviceName = ? AND voterEmail = ?" ); database.statementCache().put( sKey, s ); } s.setString( 1, voterService.name() ); s.setString( 2, voterEmail ); final ResultSet r = s.executeQuery(); try { if( !r.next() ) return null; return r.getString( 1 ); } finally{ r.close(); } } } public @Override void put( final String voterEmail, final String xml, final ServiceSession userSession ) throws BadInputException, SQLException { if( userSession != null) testAccessAllowed( voterEmail, userSession ); lengthConstrained( voterEmail ); LoggerX.i(getClass()).finer( "voter input table " + tableName + ", storing for voter " + voterEmail + " : " + xml ); synchronized( database ) { // effect an "upsert" in PostgreSQL // http://stackoverflow.com/questions/1109061/insert-on-duplicate-update-postgresql/6527838#6527838 final Connection c = database.connection(); { final String sKey = statementKeyBase + "putU"; PreparedStatement s = database.statementCache().get( sKey ); if( s == null ) { s = c.prepareStatement( "UPDATE \"" + tableName + "\"" + " SET xml = ? WHERE serviceName = ? AND voterEmail = ?" ); database.statementCache().put( sKey, s ); } s.setString( 1, xml ); s.setString( 2, voterService.name() ); s.setString( 3, voterEmail ); final int updatedRows = s.executeUpdate(); if( updatedRows > 0 ) { assert updatedRows == 1; return; } } { final String sKey = statementKeyBase + "putI"; PreparedStatement s = database.statementCache().get( sKey ); if( s == null ) { s = c.prepareStatement( "INSERT INTO \"" + tableName + "\"" + " (serviceName, voterEmail, xml) SELECT ?, ?, ? WHERE NOT EXISTS" + " (SELECT 1 FROM \"" + tableName + "\"" + " WHERE serviceName = ? AND voterEmail = ?)" ); database.statementCache().put( sKey, s ); } s.setString( 1, voterService.name() ); s.setString( 2, voterEmail ); s.setString( 3, xml ); s.setString( 4, voterService.name() ); s.setString( 5, voterEmail ); s.executeUpdate(); } } } }