/* * The contents of this file are subject to the Mozilla Public License Version 1.1 * (the "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at . * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT * WARRANTY OF ANY KIND, either express or implied. See the License for the specific * language governing rights and limitations under the License. * * The Original Code is the Venice Web Communities System. * * The Initial Developer of the Original Code is Eric J. Bowersox , * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are * Copyright (C) 2003 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. * * Contributor(s): */ package com.silverwrist.dynamo.dict; import java.sql.*; import com.silverwrist.util.*; import com.silverwrist.dynamo.except.*; import com.silverwrist.dynamo.iface.*; public class DatabaseDictOps_mysql extends DatabaseDictOps { /*-------------------------------------------------------------------------------- * Constructor *-------------------------------------------------------------------------------- */ public DatabaseDictOps_mysql(DBConnectionPool pool) { super(pool); } // end constructor /*-------------------------------------------------------------------------------- * Abstract implementations from class DatabaseDictOps *-------------------------------------------------------------------------------- */ int getSize() throws DatabaseException { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { // get a connection conn = getConnection(); // execute the count statement stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT COUNT(*) FROM dictionary;"); return SQLUtils.getReturnCountInt(rs,1); } // end try catch (SQLException e) { // translate to a general DatabaseException throw generalException(e); } // end catch finally { // shut everything down SQLUtils.shutdown(rs); SQLUtils.shutdown(stmt); SQLUtils.shutdown(conn); } // end finally } // end getSize boolean checkWord(String word) throws DatabaseException { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { // get a connection conn = getConnection(); // prepare and execute a statement stmt = conn.prepareStatement("SELECT word FROM dictionary WHERE word = ?;"); stmt.setString(1,word); rs = stmt.executeQuery(); return rs.next(); } // end try catch (SQLException e) { // translate to a general DatabaseException throw generalException(e); } // end catch finally { // shut everything down SQLUtils.shutdown(rs); SQLUtils.shutdown(stmt); SQLUtils.shutdown(conn); } // end finally } // end checkWord boolean addWord(String word) throws DatabaseException { Connection conn = null; PreparedStatement stmt = null; Statement stmt2 = null; ResultSet rs = null; try { // get a connection conn = getConnection(); // lock the table stmt2 = conn.createStatement(); stmt2.executeUpdate("LOCK TABLES dictionary WRITE;"); // see if the word's in the database stmt = conn.prepareStatement("SELECT word FROM dictionary WHERE word = ?;"); stmt.setString(1,word); rs = stmt.executeQuery(); if (rs.next()) return false; SQLUtils.shutdown(rs); rs = null; SQLUtils.shutdown(stmt); stmt = conn.prepareStatement("INSERT INTO dictionary (word) VALUES (?);"); stmt.setString(1,word); stmt.executeUpdate(); return true; } // end try catch (SQLException e) { // translate to a general DatabaseException throw generalException(e); } // end catch finally { // shut everything down MySQLUtils.unlockTables(conn); SQLUtils.shutdown(rs); SQLUtils.shutdown(stmt); SQLUtils.shutdown(stmt2); SQLUtils.shutdown(conn); } // end finally } // end addWord boolean removeWord(String word) throws DatabaseException { Connection conn = null; PreparedStatement stmt = null; Statement stmt2 = null; ResultSet rs = null; try { // get a connection conn = getConnection(); // lock the table stmt2 = conn.createStatement(); stmt2.executeUpdate("LOCK TABLES dictionary WRITE;"); // prepare and execute the delete statement stmt = conn.prepareStatement("DELETE FROM dictionary WHERE word = ?;"); stmt.setString(1,word); int rows = stmt.executeUpdate(); return (rows>0); } // end try catch (SQLException e) { // translate to a general DatabaseException throw generalException(e); } // end catch finally { // shut everything down MySQLUtils.unlockTables(conn); SQLUtils.shutdown(rs); SQLUtils.shutdown(stmt); SQLUtils.shutdown(stmt2); SQLUtils.shutdown(conn); } // end finally } // end removeWord void clearDictionary() throws DatabaseException { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { // get a connection conn = getConnection(); // lock the table stmt = conn.createStatement(); stmt.executeUpdate("LOCK TABLES dictionary WRITE;"); SQLUtils.shutdown(stmt); // execute the statement stmt = conn.createStatement(); stmt.executeUpdate("DELETE FROM dictionary;"); } // end try catch (SQLException e) { // translate to a general DatabaseException throw generalException(e); } // end catch finally { // shut everything down MySQLUtils.unlockTables(conn); SQLUtils.shutdown(rs); SQLUtils.shutdown(stmt); SQLUtils.shutdown(conn); } // end finally } // end clearDictionary } // end class DatabaseDictOps_mysql