-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
130 lines (121 loc) · 3.37 KB
/
app.js
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
var JDBMS = angular.module('JDBMS',[]);
JDBMS.controller('MainCtrl',function($scope, $http){
$http.get('database.json').success(function(data) {
$scope.data = data;
$scope.tupleToInsert;
$scope.tupletoUpdate;
$scope.attrIndex;
$scope.updateValue;
$scope.oldValue;
});
$scope.displayTable = function(table){
$scope.selectedTable = table;
}
$scope.shouldDisplay = function(row){
var obj = JSON.parse($scope.constraintText);
var retval = true;
for (var i = obj.length - 1; i >= 0; i--) {
if(obj[i][1] === 'equals'){
if(!(row[obj[i][0]] === obj[i][2]))
retval = false;
}
else if(obj[i][1] === 'greater'){
if(!(row[obj[i][0]] > obj[i][2]))
retval = false;
}
else{
if(!(row[obj[i][0]] < obj[i][2]))
retval = false;
}
}
return retval;
}
$scope.applyConstraint = function(constraint){
$scope.constraintText = constraint;
}
$scope.insertTuple = function(){
var obj = JSON.parse($scope.tupleToInsert);
$scope.selectedTable.data.push(obj);
console.log($scope.selectedTable);
}
$scope.addTable = function(){
var queryFinal = '{'+$scope.tableQuery+',"data": []}';
//console.log(queryFinal);
var obj = JSON.parse(queryFinal);
$scope.data.push(obj);
$scope.tableQuery='';
}
$scope.dropTable = function(){
var tableName = $scope.selectedTable.name;
for (var i = $scope.data.length - 1; i >= 0; i--) {
if($scope.data[i] == $scope.selectedTable)
break;
}
$scope.data.splice(i, 1);
var i, j;
for (i = $scope.data.length - 1; i >= 0; i--) {
for (j = $scope.data[i].schema.length - 1; j >= 0; j--) {
if($scope.data[i].schema[j].hasOwnProperty('foreign_key') && $scope.data[i].schema[j].foreign_key === tableName)
break;
}
if(j >= 0){
$scope.data[i].schema.splice(j, 1);
for (var k = 0; k < $scope.data[i].data.length; k++) {
$scope.data[i].data[k].splice(j, 1);
}
}
}
$scope.selectedTable = '';
}
$scope.setUpdateField = function(tuple,col_no){
$scope.tupletoUpdate = tuple;
$scope.attrIndex = col_no;
}
$scope.getPrimaryKeyIndex = function(table){//return table.primary_key;
for(var i=0;i<table.schema.length;i++)
{
if(table.schema[i].field === table.primary_key)
{
return i;
}
};
}
$scope.updateOtherTables = function(operation){
for(var i=0;i<$scope.data.length;i++)
{
for(var j=0; j<$scope.data[i].schema.length; j++)
{
if($scope.data[i].schema[j].hasOwnProperty('foreign_key') && $scope.data[i].schema[j].foreign_key === $scope.selectedTable.name)
{
for(var k=$scope.data[i].data.length-1;k>=0;k--)
{
if($scope.data[i].data[k][j] === $scope.oldValue)
{
if(operation == "update")
{
$scope.data[i].data[k][j] = $scope.updateValue;
}
else
{
$scope.data[i].data.splice(k,1);
}
}
}
}
}
}
}
$scope.update = function(){
$scope.oldValue = $scope.tupletoUpdate[$scope.attrIndex];
$scope.tupletoUpdate[$scope.attrIndex] = $scope.updateValue;
if($scope.getPrimaryKeyIndex($scope.selectedTable) == $scope.attrIndex)
{
$scope.updateOtherTables("update");
}
}
$scope.deleteTuples = function(index){
$scope.oldValue = $scope.selectedTable.data[index][$scope.getPrimaryKeyIndex($scope.selectedTable)];
$scope.selectedTable.data.splice(index,1);
$scope.updateOtherTables("delete");
}
});