-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
180 lines (134 loc) · 4.34 KB
/
index.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
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
<?php
define('UNDER_INDEX', true);
require('config.php');
require('functions.php');
require('database.php');
require('parser.php');
require('component.php');
class DataPipe {
private $database;
public function __construct() {
$this->database = new Database();
$this->database->connect($GLOBALS['DB']['host'], $GLOBALS['DB']['port'], $GLOBALS['DB']['username'], $GLOBALS['DB']['password'], $GLOBALS['DB']['dbname']);
$this->route();
}
public function route() {
$section = (!empty($_GET['section'])) ? $_GET['section']: '';
switch ($section) {
case 'index':
$this->indexSection();
break;
case 'sources':
$this->sourcesSection();
break;
case 'attributes':
$this->attributesSection();
break;
case 'process':
$this->processSection();
break;
default:
$this->indexSection();
}
}
public function getSources() {
$sql = "
SELECT tablename
FROM pg_tables
WHERE (
tablename NOT LIKE 'pg_%'
) AND (
tablename NOT LIKE 'sql_%'
);
";
$sources = $this->database->execute($sql);
$result = array();
foreach ($sources as $source) {
$result[] = $source['tablename'];
}
return $result;
}
private function getAttributes($source) {
$sql = "
SELECT attname
FROM pg_attribute JOIN pg_type ON (attrelid = typrelid)
WHERE (
typname = :source
) AND (
attname NOT IN ('cmin', 'cmax', 'ctid', 'oid', 'tableoid', 'xmin', 'xmax')
) AND (
attname NOT LIKE '.%'
);
";
$attributes = $this->database->execute($sql, array('source' => $source));
$result = array();
foreach ($attributes as $attribute) {
$result[] = $attribute['attname'];
}
return $result;
}
public function indexSection() {
include('template.php');
}
public function sourcesSection() {
$sources = $this->getSources();
$result = array();
foreach ($sources as $source) {
$result[$source] = $this->getAttributes($source);
}
echo json_encode($result);
}
public function attributesSection() {
if (empty($_GET['source'])) {
return;
}
$source = $_GET['source'];
echo json_encode($this->getAttributes($source));
}
public function processSection() {
if (empty($_POST['flow'])) {
echo '{"error": "Datos inválidos"}';
return;
}
$flow = json_decode($_POST['flow'], true);
$parser = new Parser();
try {
$sql = $parser->parse($flow);
try {
$result = $this->database->execute($sql);
//FIXME!
$data = '';
if (count($result) > 0) {
$attributes = array_keys($result[0]);
$head = '';
foreach ($attributes as $attribute) {
$head .= '<div class="head">'.$attribute.'</div>';
}
$data .= '
<div class="row">
'.$head.'
</div>';
foreach ($result as $key => $resultRow) {
$row = '';
foreach ($resultRow as $attribute => $value) {
$row .= '<div class="column">'.$value.'</div>';
}
$data .= '
<div class="row">
'.$row.'
</div>';
}
} else {
$data = 'No hay registros con estas características';
}
echo $data;
} catch (Exception $e) {
throw new Exception('Error en la integridad, revise el flujo');
}
} catch (Exception $e) {
echo $e->getMessage();
}
}
}
$datapipe = new DataPipe();
?>