HSQLSyncNumberQueryManager.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  * 
00024  */
00025 
00026 package org.openmobileis.synchro.algo.syncnumber.impl;
00027 
00028 import java.sql.*;
00029 
00030 import org.openmobileis.common.util.database.AbstractQueryManager;
00031 import org.openmobileis.common.util.exception.DatabaseException;
00032 
00043 public class HSQLSyncNumberQueryManager extends AbstractQueryManager  implements SyncNumberQueryManager {
00044 
00045   // query pattern used to insert a new version number
00046   private static String queryUpdate  =
00047     "UPDATE SYNC_ID SET SYNCNUMBER=%0% , TIME_STAMP=%1% " +
00048     "WHERE SYNCNUMBER=%2%";
00049 
00050    private static String queryCreate  =
00051      "INSERT INTO SYNC_ID (SYNCNUMBER , TIME_STAMP) " +
00052       "VALUES (%0%, %1%)";
00053 
00054   // query pattern used to create syncNB table
00055   private static String queryCreateTable  =
00056                 "CREATE TABLE SYNC_ID ("
00057                 +" SYNCNUMBER BIGINT NOT NULL,"
00058                 +" TIME_STAMP BIGINT NOT NULL,"
00059                 +" CONSTRAINT syncnum PRIMARY KEY (SYNCNUMBER)"
00060                 +")";
00061 
00062  /* private static String existsTable  =
00063    "select count(*) from SYSTEM_COLUMNS where TABLE_NAME like 'SYNC_ID'";*/
00064 
00065   // query pattern used to delete a user and its version number
00066   private static String queryDelete =
00067     "DELETE FROM SYNC_ID WHERE SYNCNUMBER = %0%";
00068 
00069 
00070   /********  SELECT QUERIES ********************/
00071 
00072   // query pattern used to get the current version number
00073   private static String queryGetTimeStamp  =
00074     "SELECT TIME_STAMP  FROM SYNC_ID WHERE SYNCNUMBER = %0% ";
00075 
00076   private static String queryGetMax  =
00077     "SELECT Max(SYNCNUMBER)  FROM SYNC_ID";
00078   
00088   public void create (String parameters[]) throws DatabaseException {
00089     this.executeUpdate(queryCreate, parameters);
00090   }
00091 
00092   /*
00093   * check if the sync table exists. IF it does not exist, create it
00094   */
00095   public void createSyncTable() throws DatabaseException, java.sql.SQLException {
00096     try {
00097       Connection con = this.getDbManager().getConnection();
00098       DatabaseMetaData dbmd = con.getMetaData();
00099       String[] tableType = {"TABLE"};
00100       ResultSet result = dbmd.getTables (null,null,"SYNC_ID", tableType);
00101       if (!result.next()) {
00102         this.executeUpdate(queryCreateTable, null);
00103         String[] data = {"0", "0"};
00104         this.create(data);
00105       }
00106     } finally {
00107                         this.getDbManager().garbageOpenedConnection();
00108     }
00109   }
00115  // public void createTable() throws ServiceException {
00116  //   this.executeUpdate(queryCreateTable, new String[0]);
00117  // }
00118 
00125  public void delete (String parameters[]) throws DatabaseException {
00126     this.executeUpdate(queryDelete, parameters);
00127  }
00128 
00135  public void update (String parameters[]) throws DatabaseException {
00136     this.executeUpdate(queryUpdate, parameters);
00137  }
00138 
00139 
00146  public ResultSet getTimeStampForsn (String parameters[]) throws DatabaseException {
00147     return (this.executeQuery(queryGetTimeStamp, parameters));
00148   }
00149 
00155    public ResultSet getLastSyncNumber () throws DatabaseException {
00156       return (this.executeQuery(queryGetMax, null));
00157   }
00158 
00165   public ResultSet getSyncNumberbysn (String parameters[]) throws DatabaseException  {
00166     return this.getTimeStampForsn(parameters);
00167   }
00168 
00169 }
00170 
00171 

Generated on Mon Dec 4 11:03:27 2006 for OpenMobileIS by  doxygen 1.5.1-p1