MSSQLSyncNumberQueryManager.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.database.JdbcPoolManagerDB;
00032 import org.openmobileis.common.util.exception.DatabaseException;
00033 
00044 public class MSSQLSyncNumberQueryManager extends AbstractQueryManager  implements SyncNumberQueryManager {
00045 
00046   // query pattern used to insert a new version number
00047   private static String queryUpdate  =
00048     "UPDATE SYNC_ID SET SYNCNUMBER=%0% , TIME_STAMP=%1% " +
00049     "WHERE SYNCNUMBER=%2%";
00050 
00051    private static String queryCreate  =
00052      "INSERT INTO SYNC_ID (SYNCNUMBER , TIME_STAMP) " +
00053       "VALUES (%0%, %1%)";
00054 
00055   // query pattern used to create syncNB table
00056   private static String queryCreateTable  =
00057                 "CREATE TABLE SYNC_ID ("
00058                 +" SYNCNUMBER BIGINT NOT NULL,"
00059                 +" TIME_STAMP BIGINT NOT NULL,"
00060                 +" CONSTRAINT syncnum PRIMARY KEY (SYNCNUMBER)"
00061                 +")";
00062 
00063  /* private static String existsTable  =
00064    "select count(*) from SYSTEM_COLUMNS where TABLE_NAME like 'SYNC_ID'";*/
00065 
00066   // query pattern used to delete a user and its version number
00067   private static String queryDelete =
00068     "DELETE FROM SYNC_ID WHERE SYNCNUMBER = %0%";
00069 
00070 
00071   /********  SELECT QUERIES ********************/
00072 
00073   // query pattern used to get the current version number
00074   private static String queryGetTimeStamp  =
00075     "SELECT TIME_STAMP  FROM SYNC_ID WHERE SYNCNUMBER = %0% ";
00076 
00077   private static String queryGetMax  =
00078     "SELECT Max(SYNCNUMBER)  FROM SYNC_ID";
00079     
00080         public MSSQLSyncNumberQueryManager(){}
00081     
00082         public MSSQLSyncNumberQueryManager(JdbcPoolManagerDB pool){
00083                 this.registerManagerDB(pool);
00084         }
00085   
00095   public void create (String parameters[]) throws DatabaseException {
00096     this.executeUpdate(queryCreate, parameters);
00097   }
00098 
00099   /*
00100   * check if the sync table exists. IF it does not exist, create it
00101   */
00102   public void createSyncTable() throws DatabaseException, java.sql.SQLException {
00103     try {
00104       Connection con = this.getDbManager().getConnection();
00105       DatabaseMetaData dbmd = con.getMetaData();
00106       String[] tableType = {"TABLE"};
00107       ResultSet result = dbmd.getTables (null,null,"sync_id", tableType);
00108       if (!result.next()) {
00109         this.executeUpdate(queryCreateTable, null);
00110         String[] data = {"0", "0"};
00111         this.create(data);
00112       }
00113     } finally {
00114                         this.getDbManager().garbageOpenedConnection();
00115     }
00116   }
00122  // public void createTable() throws ServiceException {
00123  //   this.executeUpdate(queryCreateTable, new String[0]);
00124  // }
00125 
00132  public void delete (String parameters[]) throws DatabaseException {
00133     this.executeUpdate(queryDelete, parameters);
00134  }
00135 
00142  public void update (String parameters[]) throws DatabaseException {
00143     this.executeUpdate(queryUpdate, parameters);
00144  }
00145 
00146 
00153  public ResultSet getTimeStampForsn (String parameters[]) throws DatabaseException {
00154     return (this.executeQuery(queryGetTimeStamp, parameters));
00155   }
00156 
00162    public ResultSet getLastSyncNumber () throws DatabaseException {
00163       return (this.executeQuery(queryGetMax, null));
00164   }
00165 
00172   public ResultSet getSyncNumberbysn (String parameters[]) throws DatabaseException  {
00173     return this.getTimeStampForsn(parameters);
00174   }
00175 
00176 }
00177 
00178 

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