PostgreSQLAtomicObjectQueryManager.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  *  
00023  *  Modifications :
00024  *  2006 Creation P.Delrieu
00025  * 
00026  */
00027 
00028 package org.openmobileis.synchro.algo.replication.utils.impl;
00029 
00030 import java.sql.*;
00031 
00032 import org.openmobileis.common.util.exception.DatabaseException;
00033 
00034 
00042 public class PostgreSQLAtomicObjectQueryManager extends AtomicObjectQueryManager {
00043   // query pattern used to insert a new UID
00044         private static String queryUpdate  =
00045                 "UPDATE SYNC_UID SET TIME_STAMP=%0%, CHECKSUM='%1%', MODIFICATION_TYPE =%2% , USERLINK ='%3%' " +
00046                 "WHERE UID='%4%' AND SERVICEID='%5%'";
00047 
00048          private static String queryCreate  =
00049                  "INSERT INTO SYNC_UID (UID , SERVICEID, TIME_STAMP, CHECKSUM, MODIFICATION_TYPE, CREATION_DATE, USERLINK) " +
00050                         "VALUES ('%0%', '%1%', %2%, '%3%', %4%, %5%, '%6%')";
00051 
00052   private static String createIndex =
00053     "CREATE INDEX serviceid ON SYNC_UID (SERVICEID)";
00054 
00055   private static String uidIndex =
00056     "CREATE INDEX uidid ON SYNC_UID (UID, SERVICEID)";
00057 
00058   private static String modifIndex =
00059     "CREATE INDEX modifid ON SYNC_UID (SERVICEID, TIME_STAMP,  MODIFICATION_TYPE)";
00060 
00061   // query pattern used to create syncNB table
00062   private static String queryCreateTable  =
00063     "CREATE TABLE SYNC_UID ("
00064     +"UID       VARCHAR(250) NOT NULL,"
00065     +"SERVICEID VARCHAR(50) NOT NULL,"
00066     +"TIME_STAMP BIGINT,"
00067     +"CHECKSUM  VARCHAR(25) NOT NULL,"
00068     +"MODIFICATION_TYPE SMALLINT,"
00069     +"CREATION_DATE BIGINT,"
00070           +"USERLINK    VARCHAR(25) NOT NULL,"
00071           +" CONSTRAINT uid PRIMARY KEY (UID, SERVICEID)"
00072     +") ";
00073 
00074   // delete all atomic object that are deleted and with a TS inferior to the specified date.
00075   private static String purgeDetetedAtomicObject =
00076     "DELETE FROM SYNC_UID WHERE SERVICEID = '%0%' AND TIME_STAMP < %1% AND MODIFICATION_TYPE=3";
00077 
00078 
00079   /********  SELECT QUERIES ********************/
00080 
00081   // query pattern used to get all Atomic Object for a service id
00082   private static String queryGetAtomicObject  =
00083     "SELECT UID, CHECKSUM, TIME_STAMP, MODIFICATION_TYPE, CREATION_DATE FROM SYNC_UID WHERE UID='%0%' AND SERVICEID = '%1%'";
00084 
00085   // query pattern used to get all modified Atomic Object for a service id since date
00086         private static String queryGetAllModifiedAtomicObjectForServiceSince  =
00087                 "SELECT UID, MODIFICATION_TYPE, CREATION_DATE FROM SYNC_UID WHERE SERVICEID = '%0%' AND TIME_STAMP > %1% AND USERLINK='%2%'";
00088 
00089   
00090    public void createAtomicObject(String[] parameters) throws DatabaseException {
00091     this.executeUpdate(queryCreate, parameters);
00092   }
00093 
00094  /*
00095  * check if the sync table exists. IF it does not exist, create it
00096  */
00097  public void createTable() throws DatabaseException, java.sql.SQLException {
00098      try  {
00099       Connection con = this.getDbManager().getConnection();
00100       DatabaseMetaData dbmd = con.getMetaData();
00101       String[] tableType = {"TABLE"};
00102       ResultSet result = dbmd.getTables (null,null,"sync_uid", tableType);
00103       if (!result.next()) {
00104         this.executeUpdate(queryCreateTable, null);
00105         this.executeUpdate(createIndex, null);
00106         this.executeUpdate(uidIndex, null);
00107         this.executeUpdate(modifIndex, null);
00108         //init default AO
00109         String[] data = {"0", "0", "0","0", "0", "0", ""};
00110         this.createAtomicObject(data);
00111       }
00112     } finally {
00113                         this.getDbManager().garbageOpenedConnection();
00114     }
00115   }
00116 
00117  public void updateAtomicObject(String[] parameters) throws DatabaseException {
00118     this.executeUpdate(queryUpdate, parameters);
00119   }
00120 
00121         public void purgeDeletedAtomicObject(String[] parameters) throws DatabaseException {
00122                 this.executeUpdate(purgeDetetedAtomicObject, parameters);
00123         }
00124 
00125   /* getAtomicObject() get the atomic object for specified UID
00126   * @param     : array containing atomic object uid
00127   * @return    ResultSet : rows containing UID, CHECKSUM, TIME_STAMP, MODIFICATION_TYPE, CREATION_DATE
00128   * @exception ServiceException
00129   */
00130   public ResultSet getAtomicObject(String parameters[]) throws DatabaseException {
00131     return (this.executeQuery(queryGetAtomicObject, parameters));
00132   }
00133 
00134   /* getAtomicObjectsForService() return all atomic object uid modified since specified date for service
00135   * @param     : array containing service id and date
00136   * @return    ResultSet : rows containing UID
00137   * @exception ServiceException
00138   */
00139   public ResultSet getAllModifiedAtomicObjectForServiceSince (String parameters[]) throws DatabaseException {
00140     return (this.executeQuery(queryGetAllModifiedAtomicObjectForServiceSince, parameters));
00141   }
00142 
00143 }

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