-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmodel-sqlite-opt.php
More file actions
executable file
·849 lines (726 loc) · 23.7 KB
/
Copy pathmodel-sqlite-opt.php
File metadata and controls
executable file
·849 lines (726 loc) · 23.7 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
<?php
include_once('fileutils.php');
include_once('model-sqlite.php');
define("DB_VERSION_VALIDATION",false);
class ElementTableOpt extends ElementTable
{
/* function __construct($type, $name, $latlon = 0)
{
}
function __destruct()
{
}
*/
function InitialiseSchema()
{
//Position table using rtree
if($this->latlon)
{
$sql="CREATE VIRTUAL TABLE position USING rtree(id,minLat,maxLat,minLon,maxLon);";
$this->CheckTableExistsOtherwiseCreate("position",$sql);
}
$sql="CREATE TABLE idmap (elid INTEGER PRIMARY KEY,rowid INTEGER);";
$this->CheckTableExistsOtherwiseCreate("idmap",$sql);
$sql="CREATE TABLE nodechildren (id INTEGER PRIMARY KEY,parents BLOB);";
$this->CheckTableExistsOtherwiseCreate("nodechildren",$sql);
$sql="CREATE TABLE waychildren (id INTEGER PRIMARY KEY,parents BLOB);";
$this->CheckTableExistsOtherwiseCreate("waychildren",$sql);
$sql="CREATE TABLE relationchildren (id INTEGER PRIMARY KEY,parents BLOB);";
$this->CheckTableExistsOtherwiseCreate("relationchildren",$sql);
$sql="CREATE TABLE history (id INTEGER PRIMARY KEY,history BLOB);";
$this->CheckTableExistsOtherwiseCreate("history",$sql);
ElementTable::InitialiseSchema();
}
//************************************
//Insert element into optimised tables
//************************************
function InsertObjectIntoPositionTable($el)
{
if(!$this->latlon) return;
$id = $el->attr['id'];
$lat = $el->attr['lat'];
$lon = $el->attr['lon'];
//Try to update existing position
$sql="UPDATE position SET minLat=".(float)$lat.",maxLat=".(float)$lat;
$sql.=",minLon=".(float)$lon.",maxLon=".(float)$lon." WHERE id=".(int)$id.";";
//print_r($sql);
$ret = $this->dbh->exec($sql);
//echo "ret=".$ret;
if($ret===false) {$err= $this->dbh->errorInfo();throw new Exception($sql.",".$err[2]);}
if($ret===0) //If it doesn't exist, insert new data
{
$insertsql = "INSERT INTO position (id,minLat,maxLat,minLon,maxLon) VALUES (";
$insertsql .= (int)$id.", ";
$insertsql .= (float)$lat.", ";
$insertsql .= (float)$lat.", ";
$insertsql .= (float)$lon.", ";
$insertsql .= (float)$lon;
$insertsql .= ");\n";
$ret = $this->dbh->exec($insertsql);
if($ret===false) {$err= $this->dbh->errorInfo();throw new Exception($insertsql.",".$err[2]);}
}
}
function InsertObjectIntoIdMapTable($el,$rowid)
{
$id = $el->attr['id'];
//Try to update existing position
$sql="UPDATE idmap SET rowid=".(int)$rowid." WHERE elid=".(int)$id.";";
$ret = $this->dbh->exec($sql);
if($ret===false) {$err= $this->dbh->errorInfo();throw new Exception($sql.",".$err[2]);}
if($ret===0) //If it doesn't exist, insert new data
{
$insertsql = "INSERT INTO idmap (elid,rowid) VALUES (";
$insertsql .= (int)$id.", ";
$insertsql .= (float)$rowid;
$insertsql .= ");\n";
$ret = $this->dbh->exec($insertsql);
if($ret===false) {$err= $this->dbh->errorInfo();throw new Exception($insertsql.",".$err[2]);}
}
}
function ClearCurrentFlagInElementTable($id)
{
$rowid = $this->GetRowIdOfElementCurrent($id);
if(is_null($rowid)) return null;
//Set current version to not current
$sql = "UPDATE elements SET current=0 WHERE i=".(int)$rowid.";";
$ret = $this->dbh->exec($sql);
if($ret===false) {$err= $this->dbh->errorInfo();throw new Exception($sql.",".$err[2]);}
if($ret!==1) throw new Exception("Error clearing current flag");
}
//*****************************
//Get and update children links
//*****************************
function GetParentsForChildElement($type,$id)
{
//Get existing parents of child
$query = "SELECT * FROM ".$type."children WHERE id = ".(int)$id.";";
$ret = $this->dbh->query($query);
if($ret===false) {$err= $this->dbh->errorInfo();throw new Exception($query.",".$err[2]);}
$parents = null;
foreach($ret as $row)
{
$parents = unserialize($row['parents']);
}
return $parents;
}
function SetParentsForChildElement($type,$childId,$parents,$exists)
{
//Get existing parents of child
if($exists)
{
$sql = "UPDATE ".$type."children SET parents=? WHERE id=".(int)$childId.";";
}
else
{
$sql = "INSERT INTO ".$type."children (id,parents) VALUES (";
$sql .= (int)$childId.",?);";
}
//echo $sql."\n";
//Delete entry for empty array
//TODO
$sth = $this->dbh->prepare($sql);
if($sth===false)
{
$err= $this->dbh->errorInfo();
throw new Exception($sql.",".$err[2]);
}
$ret = $sth->execute(array(serialize($parents)));
if($ret===false) {$err= $this->dbh->errorInfo();throw new Exception($sql.",".$err[2]);}
}
function AddParentForChild($type,$childId,$parentId)
{
//Get existing parents
$parents = $this->GetParentsForChildElement($type,$childId);
$found = !is_null($parents);
if(is_null($parents)) $parents=array();
//Append id to list
//if(in_array($parentId,$parents)) return; //Duplicates SHOULD be retained
array_push($parents,$parentId);
//Update database
$this->SetParentsForChildElement($type,$childId,$parents,$found);
}
function RemoveParentForChild($type,$childId,$parentId)
{
//echo "RemoveParentForChild".$childId." ".$parentId."\n";
//Get existing parents
$parents = $this->GetParentsForChildElement($type,$childId);
$found = !is_null($parents);
if(is_null($parents)) $parents=array();
//Append id to list
if(!in_array($parentId,$parents))
{
//print_r($parents);
throw new Exception("Trying to remove non existant child link ".$childId." from ".$parentId.".");
}
$offset = array_search($parentId,$parents);
//print_r($offset);
if($offset===null)
throw new Exception("Trying to remove non existant child link ".$childId." from ".$parentId.".");
unset($parents[$offset]);
//Update database
$this->SetParentsForChildElement($type,$childId,$parents,$found);
}
function AddChildrenLinksForElement($el)
{
if(!is_object($el)) throw new Exception ('Argument should be an object');
foreach($el->members as $child)
$this->AddParentForChild($child[0],$child[1],$el->attr['id']);
}
function RemoveChildrenLinksForElement($el)
{
if(!is_object($el)) throw new Exception ('Argument should be an object');
foreach($el->members as $child)
$this->RemoveParentForChild($child[0],$child[1],$el->attr['id']);
}
//**********************
//History functions
//**********************
function GetHistoryRows($id)
{
$query = "SELECT history FROM history WHERE id = ".(int)$id.";";
$ret = $this->dbh->query($query);
if($ret===false) {$err= $this->dbh->errorInfo();throw new Exception($query.",".$err[2]);}
foreach($ret as $row)
{
return unserialize($row['history']);
}
return null;
}
function AppendRowToHistory($el,$rowid)
{
//Get history
$id = $el->attr['id'];
$rowids = $this->GetHistoryRows($id);
$found = 1;
if(is_null($rowids)) {$found = 0; $rowids = array();}
//Append row id
$rowids[$el->attr['version']] = $rowid;
//Write history
if($found)
{
$sql = "UPDATE history SET history=? WHERE id=?;";
}
else
{
$sql = "INSERT INTO history (history, id) VALUES (?,?);";
}
$sth = $this->dbh->prepare($sql);
if($sth===false) {
$err= $this->dbh->errorInfo();
throw new Exception($sql.",".$err[2]);}
$sqlVals = array(serialize($rowids),(int)$id);
$ret = $sth->execute($sqlVals);
if($ret===false)
{$err= $this->dbh->errorInfo();throw new Exception($sql.",".$err[2]); }
}
//***************************
//Main modification functions
//***************************
public function Insert($el)
{
$this->CheckTransactionIsOpen();
$timers = array();
$oldObj = $this->GetElement($el->attr['id']);
//Check versions, only newer allowed
if(DB_VERSION_VALIDATION and !is_null($oldObj) and $el->attr['version'] <= $oldObj->attr['version'])
{
$err = "Modified version must be newer for ".$el->GetType()." ".$el->attr['id'];
$err .= " (".$el->attr['version']." vs. ".$oldObj->attr['version'].")";
throw new Exception($err);
}
//Remove previous version data
$startTimer = microtime(1);
$this->ClearCurrentFlagInElementTable($el->attr['id']);
$timers['clear current flag']=(microtime(1) - $startTimer);
if(!is_null($oldObj))
{
$startTimer = microtime(1);
$this->RemoveChildrenLinksForElement($oldObj);
$timers['remove children']=(microtime(1) - $startTimer);
}
//Add new version to tables
$startTimer = microtime(1);
$rowId = $this->InsertObjectIntoElementTable($el);
$timers['insert into element']=(microtime(1) - $startTimer);
$startTimer = microtime(1);
$this->InsertObjectIntoPositionTable($el);
$timers['insert into position']=(microtime(1) - $startTimer);
$startTimer = microtime(1);
$this->InsertObjectIntoIdMapTable($el,$rowId);
$timers['insert into idmap']=(microtime(1) - $startTimer);
$startTimer = microtime(1);
$this->AddChildrenLinksForElement($el);
$timers['add children']=(microtime(1) - $startTimer);
//Add element row to history
$startTimer = microtime(1);
$this->AppendRowToHistory($el,$rowId);
$timers['update history']=(microtime(1) - $startTimer);
//print_r($timers);
}
/*
function FlushWrite()
{
}
*/
public function Delete($el)
{
$this->CheckTransactionIsOpen();
$oldObj = $this->GetElement($el->attr['id']);
//Check versions, only newer allowed
if(DB_VERSION_VALIDATION and $el->attr['version'] <= $oldObj->attr['version'])
{
$err = "Modified version must be newer for ".$el->GetType()." ".$el->attr['id'];
$err .= " (".$el->attr['version']." vs. ".$oldObj->attr['version'].")";
throw new Exception($err);
}
if($this->latlon)
{
//Delete from position table
$sql = "DELETE FROM position WHERE id =".(int)$el->attr['id'].";";
$ret = $this->dbh->exec($sql);
if($ret===false) {$err= $this->dbh->errorInfo();throw new Exception($sql.",".$err[2]);}
if($ret!==1) throw new Exception("Failed to remove element from position table");
}
//Delete children links
if(!is_null($oldObj))
{
$startTimer = microtime(1);
$this->RemoveChildrenLinksForElement($oldObj);
$timers['remove children']=(microtime(1) - $startTimer);
}
//Delete from idmap table
$sql = "DELETE FROM idmap WHERE elid =".(int)$el->attr['id'].";";
//echo $sql."\n";
$ret = $this->dbh->exec($sql);
if($ret===false) {$err= $this->dbh->errorInfo();throw new Exception($sql.",".$err[2]);}
if($ret!==1) throw new Exception("Failed to remove element from idmap table");
//Add new version to element table
$startTimer = microtime(1);
$rowId = $this->InsertObjectIntoElementTable($el);
$timers['insert into element']=(microtime(1) - $startTimer);
//Add element row to history
$startTimer = microtime(1);
$this->AppendRowToHistory($el,$rowId);
$timers['update history']=(microtime(1) - $startTimer);
}
/*
function DbRowToObj($row)
{
}
*/
//*************************************
//Get specific data functions
//*************************************
//Get row id for specific element
function GetRowIdOfElementCurrent($id)
{
//Get the row ID for the current version of the element
$query = "SELECT rowid FROM idmap WHERE elid = ".(int)$id.";";
//echo "--".$query."\n";
$ret = $this->dbh->query($query);
if($ret===false) {$err= $this->dbh->errorInfo();throw new Exception($query.",".$err[2]);}
$rowid = null;
foreach($ret as $row)
{
//print_r($row);
$rowid = $row['rowid'];
}
return $rowid;
}
//Optimised get current function
public function GetElementCurrent($id)
{
$rowid = $this->GetRowIdOfElementCurrent($id);
//echo "test".$rowid."\n";
if(is_null($rowid)) return null;
//Get the actual object
$query = "SELECT * FROM elements WHERE i = ".(int)$rowid.";";
$ret = $this->dbh->query($query);
if($ret===false) {$err= $this->dbh->errorInfo();throw new Exception($query.",".$err[2]);}
$rowid = null;
foreach($ret as $row)
{
return $this->DbRowToObj($row);
}
return null;
}
public function GetElementHistoric($id,$version)
{
$rowids = $this->GetHistoryRows($id);
if(is_null($rowids)) return null;
if(!isset($rowids[$version])) return null;
$rowid = $rowids[$version];
$query = "SELECT * FROM elements WHERE i = ".(int)$rowid.";";
$ret = $this->dbh->query($query);
if($ret===false) {$err= $this->dbh->errorInfo();throw new Exception($query.",".$err[2]);}
$rowid = null;
foreach($ret as $row)
{
return $this->DbRowToObj($row);
}
throw new Exception("History row id not found in element table");
}
public function GetElement($id, $version = null)
{
if(is_null($version))
return $this->GetElementCurrent($id);
return $this->GetElementHistoric($id, $version);
}
public function GetElementHistory($id)
{
$rowids = $this->GetHistoryRows($id);
if(is_null($rowids)) return null;
$out = array();
foreach($rowids as $id)
{
$query = "SELECT * FROM elements WHERE i = ".(int)$id.";";
$ret = $this->dbh->query($query);
if($ret===false) {$err= $this->dbh->errorInfo();throw new Exception($query.",".$err[2]);}
$rowid = null;
foreach($ret as $row)
{
array_push($out,$this->DbRowToObj($row));
}
}
return $out;
}
public function GetElementsWithMembers(&$queryObjs)
{
$foundIds = array();
$out = array();
foreach($queryObjs as $obj)
{
//print_r($obj);
$parents = $this->GetParentsForChildElement($obj->GetType(),$obj->attr['id']);
//print_r($parents);
//die();
if(is_null($parents)) continue;
foreach($parents as $parId)
{
$parObj = $this->GetElement($parId);
if(is_null($parObj)) throw new Exception("Could not retrieve parent object");
if(in_array($parObj->attr['id'],$foundIds)) continue;
array_push($foundIds,$parObj->attr['id']);
array_push($out,$parObj);
}
}
//print_r($out);
//die();
return $out;
}
public function GetElementsInBbox($bbox)
{
//Unoptimised version
//return ElementTable::GetElementsInBbox($bbox);
if(!$this->latlon) throw new Exception("Position was not enabled for this object.");
$timers = array();
$bbox = ValidateBbox($bbox);
//Use internal rtree to quickly find nodes
//print_r($bbox);
$query = "SELECT id FROM position" ;
$query .= " WHERE minLat > ".(float)$bbox[1];
$query .= " and maxLat < ".(float)$bbox[3];
$query .= " and maxLon < ".(float)$bbox[2]." and minLon > ".(float)$bbox[0];
$query .= ";";
//echo $query;
$startTimer = microtime(1);
$ret = $this->dbh->query($query);
$timers['position']=(microtime(1) - $startTimer);
if($ret===false) {$err= $this->dbh->errorInfo();throw new Exception($query.",".$err[2]);}
$ids = array();
foreach($ret as $row)
array_push($ids,$row['id']);
//print_r($ids);
//For each id, get the data object
$out = array();
$startTimer = microtime(1);
foreach($ids as $id)
{
$obj = $this->GetElement($id);
if(is_null($obj))
throw new Exception("Position table contains node ".$id." this is not in element table.");
array_push($out,$obj);
}
$timers['nodes']=(microtime(1) - $startTimer);
//print_r($timers); die();
return $out;
}
public function Count()
{
$query = "SELECT COUNT(elid) FROM idmap;";
$ret = $this->dbh->query($query);
if($ret===false) {$err= $this->dbh->errorInfo();throw new Exception($query.",".$err[2]);}
foreach($ret as $row)
return $row[0];
return null;
}
public function Purge()
{
$this->DropTableIfExists("position");
$this->DropTableIfExists("idmap");
$this->DropTableIfExists("nodechildren");
$this->DropTableIfExists("waychildren");
$this->DropTableIfExists("relationchildren");
$this->DropTableIfExists("history");
ElementTable::Purge();
}
}
//*************************
//Test and repair functions
//*************************
function CheckElementTableOptAgainstIdmap(&$table, &$db)
{
$query = "SELECT * FROM elements;";
$ret = $table->dbh->query($query);
if($ret===false) {$err= $table->dbh->errorInfo();throw new Exception($query.",".$err[2]);}
foreach($ret as $row)
{
$obj = $table->DbRowToObj($row);
$rowid = $row['i'];
$id = $obj->attr['id'];
$visible = (strcmp($obj->attr['visible'],"true")==0);
if($row['current']!=1) continue;
//Check idmap
$sql = "SELECT rowid FROM idmap WHERE elid=".$id.";";
$ret = $table->dbh->query($sql);
if($ret===false) {$err= $table->dbh->errorInfo();throw new Exception($sql.",".$err[2]);}
foreach($ret as $row)
{
//echo $row['rowid']." ". $rowid."\n";
if($visible and $row['rowid'] != $rowid)
{
echo "idmap incorrect for ".$obj->GetType()." ";
echo $obj->attr['id']." ".$row['rowid']." ".$rowid."\n";
}
if(!$visible)
echo "found deleted element in idmap\n";
}
}
}
function CheckElementTableOptAgainstPosition(&$table, &$db)
{
if(!$table->latlon) return;
$query = "SELECT * FROM elements;";
$ret = $table->dbh->query($query);
if($ret===false) {
$err= $table->dbh->errorInfo();
throw new Exception($query.",".$err[2]);}
foreach($ret as $row)
{
$obj = $table->DbRowToObj($row);
$rowid = $row['i'];
$id = $obj->attr['id'];
$visible = (strcmp($obj->attr['visible'],"true")==0);
if($row['current']!=1) continue;
//Check position table
$sql = "SELECT * FROM position WHERE id=".$id.";";
$ret = $table->dbh->query($sql);
if($ret===false) {$err= $table->dbh->errorInfo();throw new Exception($sql.",".$err[2]);}
$count = 0;
foreach($ret as $row)
{
$tolerance = 1.0E-5;
$error = 0;
if(abs($row['minLat'] - $obj->attr['lat']) > $tolerance) $error = 1;
if(abs($row['maxLat'] - $obj->attr['lat']) > $tolerance) $error = 1;
if(abs($row['minLon'] - $obj->attr['lon']) > $tolerance) $error = 1;
if(abs($row['maxLon'] - $obj->attr['lon']) > $tolerance) $error = 1;
$count = $count + 1;
if($error) echo ("Positions mismatch\n");
}
if($visible and $count != 1)
echo ("missing entry for position table ".$obj->GetType()." ".$id."\n");
if(!$visible and $count != 0) echo "Deleted node found in position table";
}
}
function CheckElementTableOptAgainstChildren(&$table, &$db)
{
$query = "SELECT * FROM elements;";
$ret = $table->dbh->query($query);
if($ret===false) {$err= $table->dbh->errorInfo();throw new Exception($query.",".$err[2]);}
foreach($ret as $row)
{
$obj = $table->DbRowToObj($row);
$rowid = $row['i'];
$id = $obj->attr['id'];
$visible = (strcmp($obj->attr['visible'],"true")==0);
if($row['current']!=1) continue;
//Check Children
foreach($obj->members as $data)
{
$childtype = $data[0];
$idchild = $data[1];
$parentsOfChild = $table->GetParentsForChildElement($childtype,$idchild);
if($visible)
{
if(is_null($parentsOfChild)) echo "Child links not defined\n";
if(!in_array($id,$parentsOfChild)) echo "Child link broken\n";
$childObj = $db->GetElementById($childtype,$idchild);
if(!is_object($childObj))
echo "Child object of ".$obj->GetType()." ".$id." doesn't exist ".$idchild."\n";
}
else
{
if(!is_null($parentsOfChild)) echo "Child links exist for delete element\n";
}
}
/*foreach($obj->ways as $data)
{
$idchild = $data[0];
$parentsOfChild = $table->GetParentsForChildElement("way",$idchild);
if($visible)
{
if(is_null($parentsOfChild)) echo "Child links not defined\n";
if(!in_array($id,$parentsOfChild)) echo "Child link broken\n";
$childObj = $db->GetElementById("way",$idchild);
if(!is_object($childObj))
echo "Child object of ".$obj->GetType()." ".$id." doesn't exist ".$idchild."\n";
}
else
{
if(!is_null($parentsOfChild)) echo "Child links exist for delete element\n";
}
}
foreach($obj->relations as $data)
{
$idchild = $data[0];
$parentsOfChild = $table->GetParentsForChildElement("relation",$idchild);
if($visible)
{
if(is_null($parentsOfChild)) echo "Child links not defined\n";
if(!in_array($id,$parentsOfChild)) echo "Child link broken\n";
$childObj = $db->GetElementById("relation",$idchild);
if(!is_object($childObj))
echo "Child object of ".$obj->GetType()." ".$id." doesn't exist ".$idchild."\n";
}
else
{
if(!is_null($parentsOfChild)) echo "Child links exist for delete element\n";
}
}*/
}
}
function CheckElementTableOptAgainstHistory(&$table, &$db)
{
$query = "SELECT * FROM elements;";
$ret = $table->dbh->query($query);
if($ret===false) {$err= $table->dbh->errorInfo();throw new Exception($query.",".$err[2]);}
foreach($ret as $row)
{
$obj = $table->DbRowToObj($row);
$rowid = $row['i'];
$id = $obj->attr['id'];
$visible = (strcmp($obj->attr['visible'],"true")==0);
$version = $obj->attr['version'];
//Check history
$history = $table->GetHistoryRows($id);
$historyRowId = $history[$version];
if($rowid != $historyRowId)
{
echo "History row id incorrect for ".$obj->GetType()." ".$id." v";
echo $version.":".$rowid."vs.".$historyRowId."\n";
}
}
}
function CheckIdmapAgainstElementTableOpt($table, $db)
{
$query = "SELECT * FROM idmap;";
$ret = $table->dbh->query($query);
if($ret===false) {$err= $table->dbh->errorInfo();throw new Exception($query.",".$err[2]);}
foreach($ret as $row)
{
//print_r($row);
$rowid = $row['rowid'];
$id = $row['elid'];
$query = "SELECT * FROM elements WHERE i = ".$rowid.";";
$ret2 = $table->dbh->query($query);
if($ret2===false) {$err= $table->dbh->errorInfo();throw new Exception($query.",".$err[2]);}
$count = 0;
foreach($ret2 as $row2)
{
$obj = $table->DbRowToObj($row2);
//print_r($row2);
if($obj->attr['id'] != $id) echo "Idmap entry points to incorrect element id\n";
if($row2['current'] != 1) echo "Idmap entry is not current element\n";
if($row2['visible'] != 1) echo "Idmap entry is not visible (it was deleted?)\n";
$count = $count + 1;
}
if($count!==1) echo "Idmap entry points to one entry\n";
}
}
function CheckElementDatabaseSqliteOpt(&$db)
{
echo "Checking idmap\n";
CheckElementTableOptAgainstIdmap($db->nodeTable,$db);
CheckElementTableOptAgainstIdmap($db->wayTable,$db);
CheckElementTableOptAgainstIdmap($db->relationTable,$db);
echo "Checking position table\n";
CheckElementTableOptAgainstPosition($db->nodeTable,$db);
CheckElementTableOptAgainstPosition($db->wayTable,$db);
CheckElementTableOptAgainstPosition($db->relationTable,$db);
echo "Checking children\n";
CheckElementTableOptAgainstChildren($db->nodeTable,$db);
CheckElementTableOptAgainstChildren($db->wayTable,$db);
CheckElementTableOptAgainstChildren($db->relationTable,$db);
echo "Checking history\n";
CheckElementTableOptAgainstHistory($db->nodeTable,$db);
CheckElementTableOptAgainstHistory($db->wayTable,$db);
CheckElementTableOptAgainstHistory($db->relationTable,$db);
echo "Checking idmap (reverse)\n";
CheckIdmapAgainstElementTableOpt($db->nodeTable,$db);
CheckIdmapAgainstElementTableOpt($db->wayTable,$db);
CheckIdmapAgainstElementTableOpt($db->relationTable,$db);
}
function RepairHistoryOfTable(&$table, &$db)
{
$table->DropTableIfExists("history");
$table->InitialiseSchema();
$query = "SELECT * FROM elements;";
$ret = $table->dbh->query($query);
if($ret===false) {$err= $table->dbh->errorInfo();throw new Exception($query.",".$err[2]);}
foreach($ret as $row)
{
$obj = $table->DbRowToObj($row);
$rowid = $row['i'];
$id = $obj->attr['id'];
$version = $obj->attr['version'];
//print_r($obj);
$table->AppendRowToHistory($obj,$rowid);
}
}
//****************************
//Model Storage
//****************************
class OsmDatabaseSqliteOpt extends OsmDatabaseSqlite
{
function __construct()
{
$this->nodeTable = new ElementTableOpt("node","sqlite/nodeopt.db",1);
$this->wayTable = new ElementTableOpt("way","sqlite/wayopt.db");
$this->relationTable = new ElementTableOpt("relation","sqlite/relationopt.db");
}
function __destruct()
{
unset($this->nodeTable);
unset($this->wayTable);
unset($this->relationTable);
}
//******************
//Utility functions
//******************
function CheckPermissions()
{
$filesToCheck=array('sqlite/nodeopt.db','sqlite/wayopt.db','sqlite/relationopt.db');
foreach($filesToCheck as $f)
if(!is_writable($f))
{
return $f;
}
return 1;
}
//**********************
//General main query
//**********************
}
//$test = new OsmDatabaseSqliteOpt();
?>