Paperyard
dbHandler.php
Go to the documentation of this file.
1 <?php
2 
3  require_once('ppyrd.base.php');
4 
5 
15  class dbHandler extends ppyrd {
24  var $db;
25 
29  public function __construct() {
30  // connects or creates sqlite db file
31  $this->open();
32 
33  // updating table model
34  $this->update();
35 
36  //$this->alterTableAddColumns("testtable", "createdDate", " TEXT");
37  //$this->alterTableDropColumns("testtable", "createdDate,modifiedDate,publishedDate");
38 
39  } // End constructor
40 
41 
47  function query($query)
48  {
49  $result = $this->db->query($query);
50  if (!$result) {
51  // the query failed
52  $this->output("There was an error in query: $query");
53  $this->output($this->db->lastErrorMsg());
54  return false;
55  }
56  return $result;
57  }
58 
64  function exec($query)
65  {
66  $result = $this->db->exec($query);
67  if (!$result) {
68  // the query failed
69  $this->output("There was an error in query: $query");
70  $this->output(
71  $this->db->lastErrorMsg());
72  }
73  return $result;
74  }
75 
80  {
81  return $this->query("SELECT * FROM rule_archive WHERE isActive = 1");
82  }
83 
87  function getActiveSenders ()
88  {
89  return $this->query("SELECT * FROM rule_senders WHERE isActive = 1");
90  }
91 
97  function getConfigValue ($varname)
98  {
99  $results = $this->query("SELECT * FROM config WHERE configVariable = '$varname'");
100  if ($results == false)
101  return false;
102  $row = $results->fetchArray();
103  return $row['configValue'];
104  }
105 
106 
113  {
114  return $this->query("SELECT * FROM rule_personalInfo WHERE isActive = 1");
115  }
116 
117 
121  function getActiveSubjects ()
122  {
123  return $this->query("SELECT * FROM rule_subjects WHERE isActive = 1");
124  }
125 
130  {
131  return $this->query("SELECT * FROM rule_recipients WHERE isActive = 1");
132  }
133 
141  function writeLog($oldName, $newName, $content, $log)
142  {
143  $safe = SQLite3::escapeString($content);
144  $this->exec("INSERT INTO logs (oldFileName, newFileName, fileContent, log) VALUES ('$oldName', '$newName', '$safe', '$log');");
145  }
146 
147 
151  function open()
152  {
153  $this->db = new SQLite3("/data/database/paperyard.sqlite");
154  $this->db->busyTimeout(15000);
155  // WAL mode has better control over concurrency.
156  // Source: https://www.sqlite.org/wal.html
157  $this->db->exec('PRAGMA journal_mode = wal;');
158  }
159 
163  function update()
164  {
165 
166  $this->output("looking for DB updates");
167 
168  // changing to sqlite update directory
169  chdir("/www/updates/sqlite/");
170 
171  // getting all .sql files
172  $updates = glob("*.sql");
173 
174  // checking each file
175  foreach($updates as $update){
176  $dbversion = $this->getConfigValue("databaseVersion");
177  if ($dbversion == false)
178  $dbversion = 0;
179  $version = str_replace(".sql", "", $update);
180  // really only execute next one - if that fails no further updates shall be attempted
181  if ($version == $dbversion+1) {
182  $this->output ("applying " . $update);
183  $this->output("found update script:" . $version);
184  $sql = file_get_contents ($update);
186  $this->exec($sql);
187  }
188  }
189  }
190 
194  function close()
195  {
196 
197  $res = $this->db->close();
198  unset($this->db);
199  }
200 
201  }
202 
203 ?>
query($query)
function queries sqlite
Definition: dbHandler.php:47
getActiveSubjects()
Definition: dbHandler.php:121
writeLog($oldName, $newName, $content, $log)
Definition: dbHandler.php:141
handling database connection and queries
Definition: dbHandler.php:15
__construct()
takes care of basic db handling
Definition: dbHandler.php:29
exec($query)
function executes sqlite
Definition: dbHandler.php:64
getActiveRecipients()
Definition: dbHandler.php:129
getActiveSenders()
Definition: dbHandler.php:87
getConfigValue($varname)
Definition: dbHandler.php:97
update()
checks if the database schema needs to be updated
Definition: dbHandler.php:163
close()
closes database connection
Definition: dbHandler.php:194
getActiveArchiveRules()
Definition: dbHandler.php:79
getPersonalVariables()
Definition: dbHandler.php:112
output($string, $debug=0)
outputs string
Definition: ppyrd.base.php:78
open()
opens database connection or creates file is not found
Definition: dbHandler.php:151