forked from Emma-Hearnden/Train-track
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_read_sched.php
executable file
·71 lines (65 loc) · 2.78 KB
/
json_read_sched.php
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
<?php
$db = new SQLite3('train_progress.db' );
$db->exec('CREATE TABLE IF NOT EXISTS Schedule ( train_uid TEXT, schedule_days_run TEXT, service_code TEXT );' );
$db->exec('CREATE INDEX IF NOT EXISTS idx_Schedule_train_uid ON Schedule (train_uid );' );
$db->exec('CREATE TABLE IF NOT EXISTS Segments ( sched_id INTEGER, train_uid INTEGER, type TEXT, tiploc TEXT, tme TEXT );' );
$db->exec('CREATE INDEX IF NOT EXISTS idx_segments_train_uid ON Segments (train_uid );' );
$cnt = 0;
for( $i = 1; $i < count($argv ); $i++ ){
$db->exec( 'BEGIN;' );
if (($handle = fopen($argv[$i], "r")) !== FALSE) {
while (($data = fgets($handle )) !== FALSE) {
$sched_ = json_decode( $data );
if( property_exists( $sched_, "JsonScheduleV1" ) ){
# print( $sched_->JsonScheduleV1->CIF_train_uid . "\n" );
print( $cnt . "\r" );
$cnt = $cnt + 1;
if( $cnt % 1000 == 1 ){
$db->exec( "COMMIT;" );
$db->exec( "BEGIN;");
}
$stmt = $db->prepare( 'INSERT INTO Schedule (train_uid, schedule_days_run, service_code ) VALUES (?,?,? );' );
$stmt->bindValue( 1, $sched_->JsonScheduleV1->CIF_train_uid );
$stmt->bindValue( 2, $sched_->JsonScheduleV1->schedule_days_runs );
$stmt->bindValue( 3, $sched_->JsonScheduleV1->schedule_segment->CIF_train_service_code );
$stmt->execute();
if(property_exists( $sched_->JsonScheduleV1, 'schedule_segment' ) && property_exists( $sched_->JsonScheduleV1->schedule_segment, 'schedule_location' ) ){
$locations =$sched_->JsonScheduleV1->schedule_segment->schedule_location;
for( $j = 0; $j < count($locations ); $j++ ){
$stmt = $db->prepare( 'INSERT INTO Segments ( sched_id, train_uid, type, tiploc, tme ) VALUES( ?,?,?,?,?);' );
$stmt->bindValue( 1, $j );
$stmt->bindValue( 2, $sched_->JsonScheduleV1->CIF_train_uid );
$stmt->bindValue( 3, $locations[$j]->location_type );
$stmt->bindValue( 4, $locations[$j]->tiploc_code );
$tme = "";
if( $locations[$j]->location_type == "LT" ){ # terminates
$tme=$locations[$j]->arrival;
}else {
$tme = $locations[$j]->departure;
}
$stmt->bindValue( 5, $tme );
$stmt->execute();
}
}
}
# var_dump( $sched_);
}
}
$db->exec( 'COMMIT;' );
}
if( 0 ){
$row = 1;
$db->exec( "BEGIN;" );
$tiploc = $data->TIPLOCDATA;
foreach( $tiploc as $key => $value ){
# var_dump( $value->STANOX );
$stmt = $db->prepare( 'INSERT INTO Stanox (STANOX, Alpha3, TIPLOC, Desc ) VALUES ( ?,?,?,? );' );
$stmt->bindValue( 1, $value->STANOX );
$stmt->bindValue( 2, $value->{'3ALPHA'} );
$stmt->bindValue( 3, $value->TIPLOC );
$stmt->bindValue( 4, $value->NLCDESC );
$stmt->execute();
}
$db->exec( "COMMIT;" );
}
?>