-
Notifications
You must be signed in to change notification settings - Fork 1
/
findUpdates.pl
142 lines (123 loc) · 4.2 KB
/
findUpdates.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env perl
use strict ;
use warnings ;
# Database
use DBI;
my $DBSERVER = 'localhost';
my $DBNAME = 'classCalSync';
my $DBUSER = '**username**';
my $DBPASS = '**password**';
my $DBTABLE = 'tmpEnroll';
##############################################################
#Prepare use of SQL database
my $dsn = "DBI:mysql:$DBNAME:$DBSERVER";
my $dbh = DBI->connect($dsn, $DBUSER, $DBPASS) or die ("Cannot connect");
# Find pending updates
my $toAdd = $dbh->prepare("select count(eventID) from toAdd");
$toAdd->execute();
my $numAdd = $toAdd->fetchrow_array();
my $toUpdate = $dbh->prepare("select count(eventID) from toUpdate");
$toUpdate->execute();
my $numUpdate = $toUpdate->fetchrow_array();
my $toDelete = $dbh->prepare("select count(eventID) from toDelete");
$toDelete->execute();
my $numDelete = $toDelete->fetchrow_array();
if ($numAdd + $numUpdate + $numDelete)
{
print "Updates pending
Add:\t$numAdd
Update:\t$numUpdate
Delete:\t$numDelete\n";
exit 0;
}
else
{
# Zero old updates pending so DROP now empty tables
$dbh->do("DROP TABLE toAdd");
$dbh->do("DROP TABLE toUpdate");
$dbh->do("DROP TABLE toDelete");
# Create temporary table of all new pending updates
$dbh->do("create temporary table toPush
select eventID,classID,eventDate,start,end,description,room,curEvents.googleID
from tmpEvents
left join curEvents using (eventID,classID,eventDate,start,end,description,room)
where curEvents.eventID is null");
# From the temporary table, find those events we need to add
$dbh->do("create table toAdd
select toPush.eventID,toPush.classID,toPush.eventDate,toPush.start,
toPush.end,toPush.description,toPush.room,toPush.googleID
from toPush
left join curEvents using (eventID)
where curEvents.eventID is null");
# From the temporary table, find those events we need to update
$dbh->do("create table toUpdate
select toPush.eventID,toPush.classID,toPush.eventDate,toPush.start,
toPush.end,toPush.description,toPush.room,toPush.googleID
from toPush
left join toAdd using(eventID)
where toAdd.eventID is null");
$dbh->do("update toUpdate
join curEvents on toUpdate.eventID = curEvents.eventID
set toUpdate.googleID = curEvents.googleID");
$dbh->do("ALTER TABLE `toUpdate` ADD UNIQUE (`eventID`)");
# From the temporary table, find those events we need to delete
$dbh->do("create table toDelete
select eventID,classID,eventDate,start,end,description,room, curEvents.googleID
from curEvents
left join tmpEvents using (eventID,classID,eventDate,start,end,description,room)
where tmpEvents.eventID is null");
$dbh->do("DELETE FROM toDelete
WHERE googleID IN ( SELECT googleID FROM toUpdate )");
# Find enrollments to remove
my $selectEvent = $dbh->prepare("
SELECT curEnroll.Class, curEnroll.Person
FROM curEnroll
LEFT JOIN tmpEnroll
ON curEnroll.Class = tmpEnroll.Class
AND curEnroll.Person = tmpEnroll.Person
WHERE tmpEnroll.Class IS NULL");
$selectEvent->execute();
while (my ($class, $person) =
$selectEvent->fetchrow_array())
{
# Delete from curEnroll table
my $deleteEvent = $dbh->prepare("DELETE FROM curEnroll
WHERE `Class` = ? AND `Person` = ?");
$deleteEvent->execute($class, $person);
# Update toUpdate table
stageEventUpdate($class);
}
# Find enrollments to add
$selectEvent = $dbh->prepare("
SELECT tmpEnroll.Class, tmpEnroll.Person
FROM tmpEnroll
LEFT JOIN curEnroll
ON curEnroll.Class = tmpEnroll.Class
AND curEnroll.Person = tmpEnroll.Person
WHERE curEnroll.Class IS NULL;");
$selectEvent->execute();
while (my ($class, $person) =
$selectEvent->fetchrow_array())
{
# Add to curEnroll table
my $insertEvent = $dbh->prepare("INSERT INTO curEnroll
(`Class`, `Person`) VALUES (?, ?)");
$insertEvent->execute($class, $person);
# Update toUpdate table
stageEventUpdate($class);
}
exit 0 ;
}
# -----------------------------------------------
# Subroutines
sub stageEventUpdate
{
my ($class) = @_;
# Update toUpdate table
my $updateEvent = $dbh->prepare("INSERT IGNORE INTO toUpdate
(`eventID`, `classID`, `eventDate`, `start`, `end`, `description`, `room`, `googleID`)
SELECT `eventID`, `classID`, `eventDate`, `start`, `end`, `description`, `room`, `googleID`
FROM curEvents
WHERE `classID` = ?");
$updateEvent->execute($class);
}