Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

MSSQLSyncNumberQueryManager.java

00001 /*
00002  *        OpenMobileIS - a free Java Framework for mobile applications
00003  *
00004  *   Copyright (C) 2002  Philippe Delrieu.
00005  *
00006  *   This program is free software; you can redistribute it and/or
00007  *   modify it under the terms of the GNU General Public
00008  *   License as published by the Free Software Foundation; either
00009  *   version 2 of the License, or (at your option) any later version.
00010  *
00011  *   This program is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *   General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU Library General Public
00017  *   License along with this library; if not, write to the Free
00018  *   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  * 
00020  * Philippe Delrieu kept the rigth to distribute all code Copyrighted by philippe Delrieu
00021  *  under other licence term even commercial one.
00022  *  
00023  *  Modifications :
00024  *  2002 Creation P.Delrieu
00025  * 
00026  */
00027 
00028 package org.openmobileis.synchro.algo.syncnumber.impl;
00029 
00030 import java.sql.*;
00031 
00032 import org.openmobileis.common.util.database.AbstractQueryManager;
00033 import org.openmobileis.common.util.database.JdbcPoolManagerDB;
00034 import org.openmobileis.common.util.exception.ServiceException;
00035 
00046 public class MSSQLSyncNumberQueryManager extends AbstractQueryManager  implements SyncNumberQueryManager {
00047 
00048   // query pattern used to insert a new version number
00049   private static String queryUpdate  =
00050     "UPDATE SYNC_ID SET SYNCNUMBER=%0% , TIME_STAMP=%1% " +
00051     "WHERE SYNCNUMBER=%2%";
00052 
00053    private static String queryCreate  =
00054      "INSERT INTO SYNC_ID (SYNCNUMBER , TIME_STAMP) " +
00055       "VALUES (%0%, %1%)";
00056 
00057   // query pattern used to create syncNB table
00058   private static String queryCreateTable  =
00059                 "CREATE TABLE SYNC_ID ("
00060                 +" SYNCNUMBER BIGINT NOT NULL,"
00061                 +" TIME_STAMP BIGINT NOT NULL,"
00062                 +" CONSTRAINT syncnum PRIMARY KEY (SYNCNUMBER)"
00063                 +")";
00064 
00065  /* private static String existsTable  =
00066    "select count(*) from SYSTEM_COLUMNS where TABLE_NAME like 'SYNC_ID'";*/
00067 
00068   // query pattern used to delete a user and its version number
00069   private static String queryDelete =
00070     "DELETE FROM SYNC_ID WHERE SYNCNUMBER = %0%";
00071 
00072 
00073   /********  SELECT QUERIES ********************/
00074 
00075   // query pattern used to get the current version number
00076   private static String queryGetTimeStamp  =
00077     "SELECT TIME_STAMP  FROM SYNC_ID WHERE SYNCNUMBER = %0% ";
00078 
00079   private static String queryGetMax  =
00080     "SELECT Max(SYNCNUMBER)  FROM SYNC_ID";
00081     
00082         public MSSQLSyncNumberQueryManager(){}
00083     
00084         public MSSQLSyncNumberQueryManager(JdbcPoolManagerDB pool){
00085                 this.registerManagerDB(pool);
00086         }
00087   
00097   public void create (String parameters[]) throws ServiceException {
00098     this.executeUpdate(queryCreate, parameters);
00099   }
00100 
00101   /*
00102   * check if the sync table exists. IF it does not exist, create it
00103   */
00104   public void createSyncTable() throws ServiceException, java.sql.SQLException {
00105     try {
00106       Connection con = this.getDbManager().getConnection();
00107       DatabaseMetaData dbmd = con.getMetaData();
00108       String[] tableType = {"TABLE"};
00109       ResultSet result = dbmd.getTables (null,null,"sync_id", tableType);
00110       if (!result.next()) {
00111         this.executeUpdate(queryCreateTable, null);
00112         String[] data = {"0", "0"};
00113         this.create(data);
00114       }
00115     } finally {
00116                         this.getDbManager().garbageOpenedConnection();
00117     }
00118   }
00124  // public void createTable() throws ServiceException {
00125  //   this.executeUpdate(queryCreateTable, new String[0]);
00126  // }
00127 
00134  public void delete (String parameters[]) throws ServiceException {
00135     this.executeUpdate(queryDelete, parameters);
00136  }
00137 
00144  public void update (String parameters[]) throws ServiceException {
00145     this.executeUpdate(queryUpdate, parameters);
00146  }
00147 
00148 
00155  public ResultSet getTimeStampForsn (String parameters[]) throws ServiceException {
00156     return (this.executeQuery(queryGetTimeStamp, parameters));
00157   }
00158 
00164    public ResultSet getLastSyncNumber () throws ServiceException {
00165       return (this.executeQuery(queryGetMax, null));
00166   }
00167 
00174   public ResultSet getSyncNumberbysn (String parameters[]) throws ServiceException  {
00175     return this.getTimeStampForsn(parameters);
00176   }
00177 
00178 }
00179 
00180 

Generated on Wed Dec 14 21:05:34 2005 for OpenMobileIS by  doxygen 1.4.4