01: package com.xoetrope.service.replication.mssql;
02:
03: import net.xoetrope.debug.DebugLogger;
04: import com.xoetrope.carousel.build.BuildProperties;
05:
06: /**
07: * A service proxy to support replication of data to a client side database.
08: * This object must be setup as an application object if used in a servlet
09: * context. If a call for more data is made and the context of a previous
10: * request cannot be found then an exception will be thrown.
11: *
12: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
13: * the GNU Public License (GPL), please see license.txt for more details. If
14: * you make commercial use of this software you must purchase a commercial
15: * license from Xoetrope.</p>
16: * <p> $Revision: 1.14 $</p>
17: */
18: public class XMsSqlReplicationService extends
19: com.xoetrope.service.XReplicationService {
20: public XMsSqlReplicationService() {
21: super ();
22: }
23:
24: /**
25: * Creates the timestamps tables.
26: */
27: protected void setupTimestampTables() {
28: doUpdate("CREATE TABLE XSYSSERVERTIMESTAMPS (tableName VARCHAR(64), lastUpdate DATETIME, lastDelete DATETIME)");
29: doUpdate("CREATE TABLE XSYSDELETIONS (tableName VARCHAR(64), pseudoId INT, deleteDate DATETIME)");
30: doUpdate("CREATE TABLE XSYSNEXTIDS( TABLENAME VARCHAR(64), FIELDNAME VARCHAR(64), NEXTID INT, MODE INT )");
31: }
32:
33: /**
34: * Add the required PseudoId column and Timestamp column to the named table.
35: * @param tableName the table to tag.
36: */
37: protected void tagTable(String tableName) {
38: // The pseudo name andn timestamp are qualified with the table name so that
39: // they do not give conflicts during joins
40: try {
41: String nowStr = new Long(new java.util.Date().getTime())
42: .toString();
43: doUpdate("ALTER TABLE " + tableName
44: + " ADD PseudoId INT IDENTITY(1,1)");
45: doUpdate("ALTER TABLE " + tableName + " ADD " + tableName
46: + "_ts DATETIME");
47: doUpdate("UPDATE " + tableName + " SET " + tableName
48: + "_ts=GETDATE()");
49:
50: doUpdate("CREATE TRIGGER "
51: + tableName
52: + "_updtTrigger ON "
53: + tableName
54: + " FOR UPDATE, INSERT AS "
55: + " BEGIN "
56: + " SET NOCOUNT ON; "
57: + " UPDATE XSYSSERVERTIMESTAMPS SET lastUpdate=GETDATE() WHERE tableName='"
58: + tableName + "';" + " UPDATE " + tableName
59: + " SET " + tableName
60: + "_ts=GETDATE() FROM inserted WHERE " + tableName
61: + ".PseudoId=inserted.PseudoId;" + " END");
62:
63: doUpdate("CREATE TRIGGER "
64: + tableName
65: + "_delTrigger ON "
66: + tableName
67: + " FOR DELETE AS "
68: + " BEGIN "
69: + " SET NOCOUNT ON; "
70: + " UPDATE XSYSSERVERTIMESTAMPS SET lastDelete=GETDATE(), lastUpdate=GETDATE() WHERE tableName='"
71: + tableName + "'" + " END");
72:
73: doUpdate("CREATE TRIGGER " + tableName
74: + "_deleteTrigger ON " + tableName
75: + " FOR DELETE AS " + " BEGIN "
76: + " SET NOCOUNT ON; "
77: + " INSERT INTO XSYSDELETIONS SELECT '" + tableName
78: + "',PSEUDOID,GETDATE() FROM deleted;" + " END");
79: } catch (Exception ex1) {
80: if (BuildProperties.DEBUG)
81: DebugLogger.logError("Error tagging table: "
82: + tableName);
83: }
84: }
85: }
|