-
Notifications
You must be signed in to change notification settings - Fork 1
/
Persistence.php
207 lines (176 loc) · 6.22 KB
/
Persistence.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
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
<?php
/**
*
* (c) Ruben Dorado <ruben.dorados@google.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SiteAnalyzer;
use Exception;
use PDO;
/**
* class Persistence
*
* @package SiteAnalyzer
* @author Ruben Dorado <ruben.dorados@gmail.com>
* @copyright 2018 Ruben Dorado
* @license http://www.opensource.org/licenses/MIT The MIT License
*/
class Persistence
{
/*
* @param Configuration $config
*
* @return PDO
*/
public static function getPDO($config) {
$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
if ($config->getDsn()) {
try {
return new PDO($config->getDsn(), $config->getUser(), $config->getPassword(), $options);
} catch (Exception $e) {
if (!$config->getUseOnMemoryDB()) {
throw new Exception("Could not create a db connection. Check permissions, configuration, and documentation. ".$e->getMessage());
}
}
}
if ($config->getUseOnMemoryDB()) {
try {
return new PDO("sqlite::memory:", null, null, $options);
} catch (Exception $e) {
throw new Exception("Could not create a db connection. Check permissions, configuration, and documentation. ".$e->getMessage());
}
}
throw new Exception("Error when trying to obtain a connection to a database. Check the configuration. ");
}
/*
* @param $pdo PDO
* @param $config Configuration
*
*/
public static function crateDatabase($pdo, $config) {
try {
$db_hit_table = $config->getHitTableName();
$db_options_table = $config->getOptionsTableName();
$db_from_table = $config->getFromTableName();
$db_url_table = $config->getUrlTableName();
$stmt = $pdo->prepare("CREATE TABLE $db_hit_table (id VARCHAR(255), count INT)");
$stmt->execute();
$stmt = $pdo->prepare("CREATE TABLE $db_options_table (id VARCHAR(255), time TIMESTAMP, user VARCHAR(255))");
$stmt->execute();
$stmt = $pdo->prepare("CREATE TABLE $db_from_table (id VARCHAR(255), from_id VARCHAR(255), count INT)");
$stmt->execute();
$stmt = $pdo->prepare("CREATE TABLE $db_url_table (id VARCHAR(255), url VARCHAR(255), count INT)");
$stmt->execute();
} catch (Exception $e) {
throw new Exception("Could not create the database. ".$e->getMessage());
}
return true;
}
/*
* @param $pdo PDO
* @param $config Configuration
*
*/
public static function deleteDatabase($pdo, $config) {
$resp = true;
$db_hit_table = $config->getHitTableName();
$db_options_table = $config->getOptionsTableName();
$db_from_table = $config->getFromTableName();
$db_url_table = $config->getUrlTableName();
$resp = $resp && Persistence::dropTable($pdo, $db_hit_table);
$resp = $resp && Persistence::dropTable($pdo, $db_options_table);
$resp = $resp && Persistence::dropTable($pdo, $db_from_table);
$resp = $resp && Persistence::dropTable($pdo, $db_url_table);
return $resp;
}
/*
* @param $pdo PDO
* @param $config Configuration
*
*/
private static function dropTable($pdo, $tableName) {
try {
$stmt = $pdo->prepare("DROP TABLE $tableName");
$stmt->execute();
} catch (Exception $e) {
throw new Exception("Problem deleting the table $tableName. ".$e->getMessage());
}
return true;
}
/*
* @param $pdo PDO
* @param $config Configuration
*
*/
public static function checkTables($pdo, $config) {
$resp = true;
try {
$resp = $resp && Persistence::checkHitTable($pdo, $config);
$resp = $resp && Persistence::checkOptionsTable($pdo, $config);
$resp = $resp && Persistence::checkFromTable($pdo, $config);
$resp = $resp && Persistence::checkUrlTable($pdo, $config);
} catch (Exception $e) {
return false;
}
return $resp;
}
/*
* @param $pdo PDO
* @param $config Configuration
*
*/
public static function updateCount($pdo, $config, $options = []) {
$db_hit_table = $config->getHitTableName();
$db_options_table = $config->getOptionsTableName();
$db_from_table = $config->getFromTableName();
$db_url_table = $config->getUrlTableName();
$store_from = true;
$store_time = true;
$store_user = true;
Persistence::countHit($pdo, $config, $options);
Persistence::countFrom($pdo, $config, $options);
Persistence::countOptions($pdo, $config, $options);
return true;
}
/*
* @param $pdo PDO
* @param $config Configuration
*
*/
public static function getCounts($pdo, $config)
{
$resp = [];
try {
$dbHitTable = $config->getHitTableName();
$dbUrlTable = $config->getUrlTableName();
$stmt = $pdo->prepare("SELECT h.id, u.url, h.count FROM $dbHitTable h, $dbUrlTable u WHERE h.id=u.id");
if ($stmt->execute()) {
while ($row = $stmt->fetch()) {
$resp[] = [$row[0], $row[1], $row[2]];
}
}
} catch (Exception $e) {
throw new Exception("Error reading the database. Method getCounts().".$e->getMessage());
}
return $resp;
}
/*
* @param $pdo PDO
* @param $config Configuration
*/
public static function checkUrlTable($pdo, $config) {
return UrlDAO::checkUrlTable($pdo, $config);
}
/*
* @param $pdo PDO
* @param $config Configuration
*
*/
public static function getCountsById($pdo, $config) {
return "Works";
}
}