aboutsummaryrefslogtreecommitdiff
path: root/app/Models/DatabaseDAOPGSQL.php
blob: 1b3f7408dec45ec8daa97167b0da89ff0e256108 (plain)
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
<?php

/**
 * This class is used to test database is well-constructed.
 */
class FreshRSS_DatabaseDAOPGSQL extends FreshRSS_DatabaseDAO {
	public function tablesAreCorrect() {
		$db = FreshRSS_Context::$system_conf->db;
		$dbowner = $db['user'];
		$sql = 'SELECT * FROM pg_catalog.pg_tables where tableowner=?';
		$stm = $this->bd->prepare($sql);
		$values = array($dbowner);
		$stm->execute($values);
		$res = $stm->fetchAll(PDO::FETCH_ASSOC);

		$tables = array(
			$this->prefix . 'category' => false,
			$this->prefix . 'feed' => false,
			$this->prefix . 'entry' => false,
		);
		foreach ($res as $value) {
			$tables[array_pop($value)] = true;
		}

		return count(array_keys($tables, true, true)) == count($tables);
	}

	public function getSchema($table) {
		$sql = 'select column_name as field, data_type as type, column_default as default, is_nullable as null from INFORMATION_SCHEMA.COLUMNS where table_name = ?';
		$stm = $this->bd->prepare($sql);
		$stm->execute(array($this->prefix . $table));
		return $this->listDaoToSchema($stm->fetchAll(PDO::FETCH_ASSOC));
	}

	public function daoToSchema($dao) {
		return array(
			'name' => $dao['field'],
			'type' => strtolower($dao['type']),
			'notnull' => (bool)$dao['null'],
			'default' => $dao['default'],
		);
	}

	public function size($all = true) {
		$db = FreshRSS_Context::$system_conf->db;
		$sql = 'SELECT pg_size_pretty(pg_database_size(?))';
		$values = array($db['base']);
		$stm = $this->bd->prepare($sql);
		$stm->execute($values);
		$res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
		return $res[0];
	}

	public function optimize() {
		$ok = true;

		$sql = 'VACUUM `' . $this->prefix . 'entry`';
		$stm = $this->bd->prepare($sql);
		$ok &= $stm != false;
		if ($stm) {
			$ok &= $stm->execute();
		}

		$sql = 'VACUUM `' . $this->prefix . 'feed`';
		$stm = $this->bd->prepare($sql);
		$ok &= $stm != false;
		if ($stm) {
			$ok &= $stm->execute();
		}

		$sql = 'VACUUM `' . $this->prefix . 'category`';
		$stm = $this->bd->prepare($sql);
		$ok &= $stm != false;
		if ($stm) {
			$ok &= $stm->execute();
		}

		return $ok;
	}
}