Login ProductsSalesSupportDownloadsAbout |
Home » Technical Support » ElevateDB Technical Support » Technical Articles » Building a Sales Quote Replication System in ElevateDB |
CREATE DATABASE "MainOffice" PATH 'c:\mainoffice' DESCRIPTION 'Main office example database'
CREATE TABLE "SalesPerson" ( "ID" INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 0, INCREMENT BY 1) NOT NULL, "Name" VARCHAR(40) COLLATE "ANSI" NOT NULL, "CustomerNoStart" INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 10000) NOT NULL, "QuoteNoStart" INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 100000) NOT NULL, CONSTRAINT "ID" PRIMARY KEY ("ID") ) CREATE TRIGGER "CreateStores" AFTER INSERT ON "SalesPerson" BEGIN DECLARE SalesPersonSuffix VARCHAR DEFAULT CAST(NEWROW.ID AS VARCHAR); EXECUTE IMMEDIATE 'CREATE STORE "SalesPerson' + SalesPersonSuffix + 'In" AS LOCAL PATH ''c:\mainoffice\salesperson' + SalesPersonSuffix + 'in'''; EXECUTE IMMEDIATE 'CREATE STORE "SalesPerson' + SalesPersonSuffix + 'Out" AS LOCAL PATH ''c:\mainoffice\salesperson' + SalesPersonSuffix + 'out'''; EXCEPTION END CREATE TABLE "Customer" ( "No" INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 0, INCREMENT BY 1) NOT NULL, "Name" VARCHAR(60) COLLATE "ANSI" NOT NULL, "Address1" VARCHAR(60) COLLATE "ANSI" NOT NULL, "Address2" VARCHAR(60) COLLATE "ANSI", "City" VARCHAR(40) COLLATE "ANSI" NOT NULL, "StateProvince" VARCHAR(40) COLLATE "ANSI" NOT NULL, "PostalCode" VARCHAR(30) COLLATE "ANSI" NOT NULL, "Country" VARCHAR(40) COLLATE "ANSI" NOT NULL, "SalesPersonID" INTEGER NOT NULL, CONSTRAINT "No" PRIMARY KEY ("No"), CONSTRAINT "SalesPersonID" FOREIGN KEY ("SalesPersonID") REFERENCES "SalesPerson" ("ID") ) CREATE TABLE "Quotes" ( "CustomerNo" INTEGER NOT NULL, "QuoteNo" INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 0, INCREMENT BY 1) NOT NULL, "QuoteDate" DATE NOT NULL, "QuoteTotal" DECIMAL(20,2) DEFAULT 0.00 NOT NULL, CONSTRAINT "QuoteNo" PRIMARY KEY ("QuoteNo"), CONSTRAINT "CustomerNo" FOREIGN KEY ("CustomerNo") REFERENCES "Customer" ("No") ) CREATE PROCEDURE "UpdateIdentityRanges" (IN "SalesPersonID" INTEGER) BEGIN DECLARE SalesPersonCursor CURSOR FOR SalesPersonStmt; DECLARE CustomerNoStart INTEGER DEFAULT 0; DECLARE QuoteNoStart INTEGER DEFAULT 0; -- Select the correct salesperson PREPARE SalesPersonStmt FROM 'SELECT * FROM SalesPerson WHERE ID = ?'; OPEN SalesPersonCursor USING SalesPersonID; -- Verify that the salesperson was found, and then alter the tables to -- ensure the proper seed value for the identity columns IF ROWCOUNT(SalesPersonCursor) = 0 THEN RAISE ERROR CODE 10000 MESSAGE 'Invalid salesperson ID'; ELSE FETCH FROM SalesPersonCursor (CustomerNoStart) INTO CustomerNoStart; FETCH FROM SalesPersonCursor (QuoteNoStart) INTO QuoteNoStart; -- Need to make sure to close SalesPerson table completely -- because it will have both the Customer and Quotes tables -- open due to the foreign key constraints CLOSE SalesPersonCursor; UNPREPARE SalesPersonStmt; EXECUTE IMMEDIATE 'ALTER TABLE Customer ALTER COLUMN No RESTART WITH ' + CAST(CustomerNoStart AS VARCHAR); EXECUTE IMMEDIATE 'ALTER TABLE Quotes ALTER COLUMN QuoteNo RESTART WITH ' + CAST(QuoteNoStart AS VARCHAR); END IF; END
"CustomerNoStart" INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 10000) NOT NULL, "QuoteNoStart" INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 100000) NOT NULL,
Table | Direction |
SalesPerson | To Laptop |
Customer | To Laptop and From Laptop |
Quotes | To Laptop and From Laptop |
SCRIPT BEGIN DECLARE ConfigCursor CURSOR FOR ConfigStmt; DECLARE SalesPersonStmt STATEMENT; -- Create necessary stores if not present USE Configuration; PREPARE ConfigStmt FROM 'SELECT * FROM Stores WHERE Name = ?'; OPEN ConfigCursor USING 'MainSetup'; IF ROWCOUNT(ConfigCursor) = 0 THEN EXECUTE IMMEDIATE 'CREATE STORE "MainSetup" AS LOCAL PATH ''c:\mainoffice\setup'''; END IF; CLOSE ConfigCursor; OPEN ConfigCursor USING 'MainUpdates'; IF ROWCOUNT(ConfigCursor) = 0 THEN EXECUTE IMMEDIATE 'CREATE STORE "MainUpdates" AS LOCAL PATH ''c:\mainoffice\updates'''; END IF; USE MainOffice; -- Insert the salesperson rows PREPARE SalesPersonStmt FROM 'INSERT INTO "SalesPerson" VALUES (?,?,?,?)'; EXECUTE SalesPersonStmt USING NULL,'Guy Smiley',NULL,NULL; EXECUTE SalesPersonStmt USING NULL,'Tom Sawyer',NULL,NULL; -- Be sure to unprepare the statement so that we can publish this table later UNPREPARE SalesPersonStmt; -- Backup the database so that the laptops can use it for setup EXECUTE IMMEDIATE 'BACKUP DATABASE "MainOffice" AS "SetupLaptop" TO STORE "MainSetup" DESCRIPTION ''Laptop setup'' INCLUDE CATALOG'; -- Finally, publish the database EXECUTE IMMEDIATE 'PUBLISH DATABASE "MainOffice"'; END
SCRIPT (IN SalesPersonID INTEGER) BEGIN DECLARE ConfigCursor CURSOR FOR ConfigStmt; DECLARE SalesPersonSuffix VARCHAR DEFAULT CAST(SalesPersonID AS VARCHAR); USE Configuration; -- Create necessary stores if not present PREPARE ConfigStmt FROM 'SELECT * FROM Stores WHERE Name = ?'; OPEN ConfigCursor USING 'LaptopSetup' + SalesPersonSuffix; IF ROWCOUNT(ConfigCursor) = 0 THEN EXECUTE IMMEDIATE 'CREATE STORE "LaptopSetup' + SalesPersonSuffix +'" AS LOCAL PATH ''c:\laptop' + SalesPersonSuffix + '\setup'''; END IF; CLOSE ConfigCursor; OPEN ConfigCursor USING 'MainOfficeSetup'; IF ROWCOUNT(ConfigCursor) = 0 THEN EXECUTE IMMEDIATE 'CREATE STORE "MainOfficeSetup" AS REMOTE ADDRESS ''127.0.0.1'' PORT 12010 USER "Administrator" PASSWORD ''EDBDefault'' STORE "MainSetup"'; END IF; -- Create necessary database if not present PREPARE ConfigStmt FROM 'SELECT * FROM Databases WHERE Name = ?'; OPEN ConfigCursor USING 'Laptop' + SalesPersonSuffix; IF ROWCOUNT(ConfigCursor) = 0 THEN EXECUTE IMMEDIATE 'CREATE DATABASE "Laptop' + SalesPersonSuffix +'" PATH ''c:\laptop' + SalesPersonSuffix + ''' DESCRIPTION ''Laptop example database'''; -- Now copy the backup file from the remote store to the local store EXECUTE IMMEDIATE 'COPY FILE "SetupLaptop.EDBBkp" IN STORE "MainOfficeSetup" TO "SetupLaptop.EDBBkp" IN STORE "LaptopSetup' + SalesPersonSuffix + '"'; -- Now we can restore the backup into the database EXECUTE IMMEDIATE 'RESTORE DATABASE "Laptop'+ SalesPersonSuffix +'" FROM "SetupLaptop" IN STORE "LaptopSetup' + SalesPersonSuffix + '" INCLUDE CATALOG'; -- And, finally we can publish the Customer and Quotes tables EXECUTE IMMEDIATE 'PUBLISH DATABASE "Laptop' + SalesPersonSuffix +'" TABLES Customer, Quotes'; END IF; END
SCRIPT BEGIN DECLARE SalesPersonCursor CURSOR FOR SalesPersonStmt; DECLARE ConfigCursor CURSOR FOR ConfigStmt; DECLARE UpdateCursor CURSOR FOR UpdateStmt; DECLARE UpdateFile VARCHAR DEFAULT ''; DECLARE ID INTEGER DEFAULT 0; DECLARE SalesPersonSuffix VARCHAR DEFAULT ''; -- First we'll load in all of the updates from the salespersons USE MainOffice; PREPARE SalesPersonStmt FROM 'SELECT * FROM SalesPerson'; OPEN SalesPersonCursor; FETCH FIRST FROM SalesPersonCursor ('ID') INTO ID; USE Configuration; WHILE NOT EOF(SalesPersonCursor) DO SET SalesPersonSuffix = CAST(ID AS VARCHAR); -- Find out what update files are available for loading from -- this salesperson, being sure to order them by their creation -- order EXECUTE IMMEDIATE 'SET UPDATES STORE TO "SalesPerson' + SalesPersonSuffix + 'In"'; PREPARE UpdateStmt FROM 'SELECT * FROM Updates ORDER BY CreatedOn'; OPEN UpdateCursor; FETCH FIRST FROM UpdateCursor ('Name') INTO UpdateFile; WHILE NOT EOF(UpdateCursor) DO -- Load the update file for the salesperson EXECUTE IMMEDIATE 'LOAD UPDATES FOR DATABASE "MainOffice" FROM "' + UpdateFile + '" IN STORE "SalesPerson' + SalesPersonSuffix + 'In"'; -- Since the load was successful for this update file, go -- ahead and delete it now EXECUTE IMMEDIATE 'DELETE FILE "' + UpdateFile + '.EDBUpd" FROM STORE "SalesPerson' + SalesPersonSuffix + 'In"'; FETCH NEXT FROM UpdateCursor ('Name') INTO UpdateFile; END WHILE; FETCH NEXT FROM SalesPersonCursor ('ID') INTO ID; END WHILE; -- Now we need to create the main update file to send back to the -- the salespersons -- Assign main update file name SET UpdateFile = 'MainOffice-' + REPLACE(':', '-', CAST(CURRENT_TIMESTAMP AS VARCHAR)); EXECUTE IMMEDIATE 'SAVE UPDATES FOR DATABASE "MainOffice" AS "' + UpdateFile + '" TO STORE "MainUpdates"'; FETCH FIRST FROM SalesPersonCursor ('ID') INTO ID; WHILE NOT EOF(SalesPersonCursor) DO SET SalesPersonSuffix = CAST(ID AS VARCHAR); -- Copy the main office update file to each salesperson store EXECUTE IMMEDIATE 'COPY FILE "' + UpdateFile + '.EDBUpd" IN STORE "MainUpdates" TO "' + UpdateFile + '.EDBUpd" IN STORE "SalesPerson' + SalesPersonSuffix + 'Out"'; FETCH NEXT FROM SalesPersonCursor ('ID') INTO ID; END WHILE; END
SCRIPT (IN SalesPersonID INTEGER) BEGIN DECLARE ConfigCursor CURSOR FOR ConfigStmt; DECLARE UpdateCursor CURSOR FOR UpdateStmt; DECLARE UpdateFile VARCHAR DEFAULT ''; DECLARE SalesPersonSuffix VARCHAR DEFAULT CAST(SalesPersonID AS VARCHAR); USE Configuration; -- Create necessary stores if not present PREPARE ConfigStmt FROM 'SELECT * FROM Stores WHERE Name = ?'; OPEN ConfigCursor USING 'LaptopUpdates' + SalesPersonSuffix + 'In'; IF ROWCOUNT(ConfigCursor) = 0 THEN EXECUTE IMMEDIATE 'CREATE STORE "LaptopUpdates' + SalesPersonSuffix + 'In" AS LOCAL PATH ''c:\laptop' + SalesPersonSuffix + '\updatesin'''; END IF; CLOSE ConfigCursor; OPEN ConfigCursor USING 'LaptopUpdates' + SalesPersonSuffix + 'Out'; IF ROWCOUNT(ConfigCursor) = 0 THEN EXECUTE IMMEDIATE 'CREATE STORE "LaptopUpdates' + SalesPersonSuffix + 'Out" AS LOCAL PATH ''c:\laptop' + SalesPersonSuffix + '\updatesout'''; END IF; CLOSE ConfigCursor; OPEN ConfigCursor USING 'MainOffice' + SalesPersonSuffix + 'In'; IF ROWCOUNT(ConfigCursor) = 0 THEN EXECUTE IMMEDIATE 'CREATE STORE "MainOffice' + SalesPersonSuffix + 'In" AS REMOTE ADDRESS ''127.0.0.1'' PORT 12010 USER "Administrator" PASSWORD ''EDBDefault'' STORE "SalesPerson' + SalesPersonSuffix + 'In"'; END IF; CLOSE ConfigCursor; OPEN ConfigCursor USING 'MainOffice' + SalesPersonSuffix + 'Out'; IF ROWCOUNT(ConfigCursor) = 0 THEN EXECUTE IMMEDIATE 'CREATE STORE "MainOffice' + SalesPersonSuffix + 'Out" AS REMOTE ADDRESS ''127.0.0.1'' PORT 12010 USER "Administrator" PASSWORD ''EDBDefault'' STORE "SalesPerson' + SalesPersonSuffix + 'Out"'; END IF; -- Save any current updates to the update file SET UpdateFile = 'Laptop' + SalesPersonSuffix + '-' + REPLACE(':', '-', CAST(CURRENT_TIMESTAMP AS VARCHAR)); EXECUTE IMMEDIATE 'SAVE UPDATES FOR DATABASE "Laptop' + SalesPersonSuffix + '" AS "' + UpdateFile + '" TO STORE "LaptopUpdates' + SalesPersonSuffix + 'Out" TABLES Customer, Quotes'; -- Get a list all of the un-transferred update files and copy -- them all into the main office store EXECUTE IMMEDIATE 'SET UPDATES STORE TO "LaptopUpdates' + SalesPersonSuffix + 'Out"'; PREPARE UpdateStmt FROM 'SELECT * FROM Updates ORDER BY CreatedOn'; OPEN UpdateCursor; FETCH FIRST FROM UpdateCursor ('Name') INTO UpdateFile; WHILE NOT EOF(UpdateCursor) DO -- Copy each update file to the main office store EXECUTE IMMEDIATE 'COPY FILE "' + UpdateFile + '.EDBUpd" IN STORE "LaptopUpdates' + SalesPersonSuffix + 'Out" TO "' + UpdateFile + '.EDBUpd" IN STORE "MainOffice' + SalesPersonSuffix + 'In"'; -- If copied successfully, then delete it EXECUTE IMMEDIATE 'DELETE FILE "' + UpdateFile + '.EDBUpd" FROM STORE "LaptopUpdates' + SalesPersonSuffix + 'Out"'; FETCH NEXT FROM UpdateCursor ('Name') INTO UpdateFile; END WHILE; -- Get a list all of the update files in the main office store and copy -- them to the local store EXECUTE IMMEDIATE 'SET UPDATES STORE TO "MainOffice' + SalesPersonSuffix + 'Out"'; PREPARE UpdateStmt FROM 'SELECT * FROM Updates ORDER BY CreatedOn'; OPEN UpdateCursor; FETCH FIRST FROM UpdateCursor ('Name') INTO UpdateFile; WHILE NOT EOF(UpdateCursor) DO -- Copy each update file to the local store EXECUTE IMMEDIATE 'COPY FILE "' + UpdateFile + '.EDBUpd" IN STORE "MainOffice' + SalesPersonSuffix + 'Out" TO "' + UpdateFile + '.EDBUpd" IN STORE "LaptopUpdates' + SalesPersonSuffix + 'In"'; -- If copied successfully, then delete it EXECUTE IMMEDIATE 'DELETE FILE "' + UpdateFile + '.EDBUpd" FROM STORE "MainOffice' + SalesPersonSuffix + 'Out"'; FETCH NEXT FROM UpdateCursor ('Name') INTO UpdateFile; END WHILE; -- Get a list all of the update files in the local store and load them EXECUTE IMMEDIATE 'SET UPDATES STORE TO "LaptopUpdates' + SalesPersonSuffix + 'In"'; PREPARE UpdateStmt FROM 'SELECT * FROM Updates ORDER BY CreatedOn'; OPEN UpdateCursor; FETCH FIRST FROM UpdateCursor ('Name') INTO UpdateFile; WHILE NOT EOF(UpdateCursor) DO -- Load the updates from the update file EXECUTE IMMEDIATE 'LOAD UPDATES FOR DATABASE "Laptop' + SalesPersonSuffix + '" FROM "' + UpdateFile + '" IN STORE "LaptopUpdates' + SalesPersonSuffix + 'In"'; -- If loaded successfully, then delete it EXECUTE IMMEDIATE 'DELETE FILE "' + UpdateFile + '.EDBUpd" FROM STORE "LaptopUpdates' + SalesPersonSuffix + 'In"'; FETCH NEXT FROM UpdateCursor ('Name') INTO UpdateFile; END WHILE; END
SCRIPT (IN SalesPersonID INTEGER) BEGIN DECLARE CustomerStmt STATEMENT; DECLARE CustomerNo INTEGER; DECLARE QuotesStmt STATEMENT; CASE SalesPersonID WHEN 1 THEN USE Laptop1; -- Call the UpdateIdentityRanges procedure CALL UpdateIdentityRanges(1); -- Prepare the INSERT statements PREPARE CustomerStmt FROM 'INSERT INTO Customer VALUES (?,?,?,?,?,?,?,?,?)'; PREPARE QuotesStmt FROM 'INSERT INTO Quotes VALUES (?,?,?,?)'; -- Be sure to set the customer # to NULL so that it is assigned -- a new value SET CustomerNo = NULL; EXECUTE CustomerStmt USING CustomerNo, 'Acme Fasteners, Inc.', '100 Main Street', NULL, 'Pleasantville', 'NY', '99999', 'United States', 1; EXECUTE QuotesStmt USING CustomerNo, NULL, DATE '2007-05-26', 2500; SET CustomerNo = NULL; EXECUTE CustomerStmt USING CustomerNo, 'Mister Plow, Inc.', '742 Evergreen Terrace', NULL, 'Springfield', 'Illinois', '55555', 'United States', 1; EXECUTE QuotesStmt USING CustomerNo, NULL, DATE '2007-06-14', 2200; EXECUTE QuotesStmt USING CustomerNo, NULL, DATE '2007-01-06', 5000; EXECUTE QuotesStmt USING CustomerNo, NULL, DATE '2007-03-29', 4300; SET CustomerNo = NULL; EXECUTE CustomerStmt USING CustomerNo, 'Planet Express, Inc.', '3.14159265 Circle Street', NULL, 'New New York', 'NY', '3000', 'United States', 1; EXECUTE QuotesStmt USING CustomerNo, NULL, DATE '2007-09-19', 3000; EXECUTE QuotesStmt USING CustomerNo, NULL, DATE '2007-10-02', 9800; EXECUTE QuotesStmt USING CustomerNo, NULL, DATE '2007-12-16', 3800; WHEN 2 THEN USE Laptop2; -- Call the UpdateIdentityRanges procedure CALL UpdateIdentityRanges(2); -- Prepare the INSERT statements PREPARE CustomerStmt FROM 'INSERT INTO Customer VALUES (?,?,?,?,?,?,?,?,?)'; PREPARE QuotesStmt FROM 'INSERT INTO Quotes VALUES (?,?,?,?)'; -- Be sure to set the customer # to NULL so that it is assigned -- a new value SET CustomerNo = NULL; EXECUTE CustomerStmt USING CustomerNo, 'Smith Brothers Machinery, Inc.', '200 Cherry Street', NULL, 'Orangeland', 'CA', '77777', 'United States', 2; EXECUTE QuotesStmt USING CustomerNo, NULL, DATE '2007-07-18', 12000; EXECUTE QuotesStmt USING CustomerNo, NULL, DATE '2007-04-12', 6000; END CASE; END
This web page was last updated on Wednesday, January 13, 2021 at 12:15 AM | Privacy PolicySite Map © 2025 Elevate Software, Inc. All Rights Reserved Questions or comments ? E-mail us at info@elevatesoft.com |