Skip to content

Commit

Permalink
Update Persistence.php
Browse files Browse the repository at this point in the history
  • Loading branch information
rdorado committed Nov 12, 2018
1 parent 4052d52 commit 8cbf733
Showing 1 changed file with 3 additions and 269 deletions.
272 changes: 3 additions & 269 deletions src/Persistence.php
Expand Up @@ -53,7 +53,6 @@ public static function getPDO($config) {

}


/*
* @param $pdo PDO
* @param $config Configuration
Expand All @@ -80,7 +79,6 @@ public static function crateDatabase($pdo, $config) {
return true;
}


/*
* @param $pdo PDO
* @param $config Configuration
Expand All @@ -102,7 +100,6 @@ public static function deleteDatabase($pdo, $config) {
return $resp;
}


/*
* @param $pdo PDO
* @param $config Configuration
Expand All @@ -119,7 +116,6 @@ private static function dropTable($pdo, $tableName) {
return true;
}


/*
* @param $pdo PDO
* @param $config Configuration
Expand All @@ -139,103 +135,6 @@ public static function checkTables($pdo, $config) {
return $resp;

}

/*
* @param $pdo PDO
* @param $config Configuration
*/
public static function checkFromTable($pdo, $config) {
try {
$db_from_table = $config->getFromTableName();
$stmt = $pdo->prepare("SELECT * FROM $db_from_table WHERE 1==0");
$stmt->execute();
} catch (Exception $e) {
return false;
}
return true;
}

/*
* @param $pdo PDO
* @param $config Configuration
*/
public static function checkUrlTable($pdo, $config) {
try {
$db_url_table = $config->getUrlTableName();
$stmt = $pdo->prepare("SELECT * FROM $db_url_table WHERE 1==0");
$stmt->execute();
} catch (Exception $e) {
return false;
}
return true;
}



/*
* @param $pdo PDO
* @param $config Configuration
*/
public static function checkOptionsTable($pdo, $config) {
try {
$db_options_table = $config->getOptionsTableName();
$stmt = $pdo->prepare("SELECT * FROM $db_options_table WHERE 1==0");
$stmt->execute();
} catch (Exception $e) {
return false;
}
return true;
}





/*
* @param $pdo PDO
* @param $config Configuration
*
*/
public static function countFrom($pdo, $config, $options = []) {
if (array_key_exists('from_id', $options)) {
$ids = [$options['from_id']];
} else {
$from_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'No referer info';
$ids = Persistence::findHitIdsByUrl($pdo, $config, $from_url);
if (count($ids)==0) {
$stmt = $pdo->prepare("INSERT INTO $db_url_table (id, url, count) VALUES (?, ?, 1)");
$stmt->execute([$from_url, $from_url]);
$ids = [$from_url];
}
}
foreach ($ids as $from_id) {
$stmt = $pdo->prepare("UPDATE $db_from_table SET count = count + 1 WHERE id = ? and from_id = ?");
$stmt->execute([$id, $from_id]);
if ($stmt->rowCount()==0) {
$stmt = $pdo->prepare("INSERT INTO $db_from_table (id, from_id, count) VALUES (?, ?, 1)");
$stmt->execute([$id, $from_id]);
}
}
}

/*
* @param $pdo PDO
* @param $config Configuration
*
*/
public static function countOptions($pdo, $config, $options = []) {
$user = null;
if ($store_user) {
if (array_key_exists('user', $options)) {
$user = $options['user'];
}
}

if ($store_time || $store_user) {
$stmt = $pdo->prepare("INSERT INTO $db_options_table (id, time, user) VALUES (?, ?, ?)");
$stmt->execute([$id, time(), $user]);
}
}

/*
* @param $pdo PDO
Expand All @@ -260,156 +159,6 @@ public static function updateCount($pdo, $config, $options = []) {
return true;
}

/*
* @param $pdo PDO
* @param $config Configuration
*
*/
public static function findHitIdsByUrl($pdo, $config, $url) {
$resp = [];
try {

$dbtable = $config->getUrlTableName();
$stmt = $pdo->prepare("SELECT id,url,count FROM $dbtable WHERE url = '$url'");
if ($stmt->execute()) {
while ($row = $stmt->fetch()) {
$resp[] = $row['id'];
}
}
} catch (Exception $e) {
throw new Exception("Error executing function 'findHitsByUrl'. ".$e->getMessage());
}
return $resp;
}



/*
* @param $pdo PDO
* @param $config Configuration
*
*/
public static function findUrls($pdo, $config, $by = []) {
$resp = [];
try {
$dbtable = $config->getUrlTableName();
$qdata = [];
$tquery = [];
if (array_key_exists('id', $by)) {
$qdata[] = $by['id'];
$tquery[] = "id = ?";
}

if (array_key_exists('url', $by)) {
$qdata[] = $by['url'];
$tquery[] = "url = ?";
}

$sql = "SELECT id,url,count FROM $dbtable";
if (count($tquery) > 0) {
$sql = $sql." WHERE ".join(" AND ", $tquery);
}

$stmt = $pdo->prepare($sql);
if ($stmt->execute($qdata)) {
while ($row = $stmt->fetch()) {
$resp[] = [$row['id'], $row['url'], $row['count']];
}
}

} catch (Exception $e) {
throw new Exception("Error executing function 'getAllUrls'. ".$e->getMessage());
}
return $resp;
}

/*
* @param $pdo PDO
* @param $config Configuration
*
*/
public static function findIdByTimeUser($pdo, $config, $by = []) {
$resp = [];
try {
$dbtable = $config->getOptionsTableName();
$qdata = [];
$tquery = [];
if (array_key_exists('from', $by)) {
$qdata[] = $by['from'];
$tquery[] = "time >= ?";
}

if (array_key_exists('to', $by)) {
$qdata[] = $by['to'];
$tquery[] = "time <= ?";
}

if (array_key_exists('user', $by)) {
$qdata[] = $by['user'];
$tquery[] = "user = ?";
}

$sql = "SELECT id,time,user FROM $dbtable";
if (count($tquery) > 0) {
$sql = $sql." WHERE ".join(" AND ", $tquery);
}

$stmt = $pdo->prepare($sql);
if ($stmt->execute($qdata)) {
while ($row = $stmt->fetch()) {
$resp[] = [$row['id'], $row['time'], $row['user']];
}
}

} catch (Exception $e) {
throw new Exception("Error executing function 'getAllUrls'. ".$e->getMessage());
}
return $resp;
}


/*
* @param $pdo PDO
* @param $config Configuration
*
*/
public static function findByFrom($pdo, $config, $by = []) {
$resp = [];
try {
$dbFromtable = $config->getFromTableName();
$dbUrltable = $config->getUrlTableName();
$qdata = [];
$tquery = [];

if (array_key_exists('url', $by) && array_key_exists('id', $by)) {
$qdata = [$by['url'], $by['id']];
$tquery = "SELECT f.* FROM $dbFromtable f,$dbUrltable u WHERE (f.from_id = u.id and f.url = ?) or f.from_id = ?";
} else if (array_key_exists('url', $by)) {
$qdata = [$by['url']];
$tquery = "SELECT f.* FROM $dbFromtable f,$dbUrltable u where f.from_id = u.id and u.url = ?";
} else if (array_key_exists('id', $by)) {
$qdata = [$by['id']];
$tquery = "SELECT f.* FROM $dbFromtable f where f.from_id = ?";
} else {
$qdata = [];
$tquery = "SELECT f.* FROM $dbFromtable f";
}

$stmt = $pdo->prepare($tquery);
if ($stmt->execute($qdata)) {
while ($row = $stmt->fetch()) {
$resp[] = [$row['id'], $row['from_id'], $row['count']];
}
}

} catch (Exception $e) {
throw new Exception("Error executing function 'findByFrom'. ".$e->getMessage());
}
return $resp;
}



/*
* @param $pdo PDO
* @param $config Configuration
Expand All @@ -435,28 +184,12 @@ public static function getCounts($pdo, $config)
return $resp;
}


/*
* @param $pdo PDO
* @param $config Configuration
*
*/
public static function getHitsWithOptions($pdo, $config) {
$resp = [];
try {

$dbOptionsTable = $config->getOptionsTableName();
$stmt = $pdo->prepare("SELECT o.id, o.time, o.user FROM $dbOptionsTable o");
if ($stmt->execute()) {
while ($row = $stmt->fetch()) {
$resp[] = ['id'=>$row[0], 'time'=>$row[1], 'user'=>$row[2]];
}
}

} catch (Exception $e) {
throw new Exception("Error reading the database. Method getCounts().".$e->getMessage());
}
return $resp;
public static function checkUrlTable($pdo, $config) {
return UrlDAO::checkUrlTable($pdo, $config);
}

/*
Expand All @@ -467,6 +200,7 @@ public static function getHitsWithOptions($pdo, $config) {
public static function getCountsById($pdo, $config) {
return "Works";
}

}


Expand Down

0 comments on commit 8cbf733

Please sign in to comment.