Paperyard
Public Member Functions | Public Attributes | List of all members
dbHandler Class Reference

handling database connection and queries More...

Inheritance diagram for dbHandler:
ppyrd

Public Member Functions

 __construct ()
 takes care of basic db handling More...
 
 query ($query)
 function queries sqlite More...
 
 exec ($query)
 function executes sqlite More...
 
 getActiveArchiveRules ()
 
 getActiveSenders ()
 
 getConfigValue ($varname)
 
 getPersonalVariables ()
 
 getActiveSubjects ()
 
 getActiveRecipients ()
 
 writeLog ($oldName, $newName, $content, $log)
 
 open ()
 opens database connection or creates file is not found More...
 
 update ()
 checks if the database schema needs to be updated More...
 
 close ()
 closes database connection More...
 
- Public Member Functions inherited from ppyrd
 __construct ($db)
 constructor More...
 
 checkCliVsWebserver ()
 
 output ($string, $debug=0)
 outputs string More...
 

Public Attributes

 $db
 
- Public Attributes inherited from ppyrd
 $db
 

Detailed Description

handling database connection and queries

Author
Till Witt
Bug:
no error handling implemented yet

database handler

Definition at line 15 of file dbHandler.php.

Constructor & Destructor Documentation

dbHandler::__construct ( )

takes care of basic db handling

Definition at line 29 of file dbHandler.php.

29  {
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
update()
checks if the database schema needs to be updated
Definition: dbHandler.php:163
open()
opens database connection or creates file is not found
Definition: dbHandler.php:151

Member Function Documentation

dbHandler::close ( )

closes database connection

Definition at line 194 of file dbHandler.php.

195  {
196 
197  $res = $this->db->close();
198  unset($this->db);
199  }
dbHandler::exec (   $query)

function executes sqlite

Parameters
string$queryto execute
Returns
pointer to db results

Definition at line 64 of file dbHandler.php.

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  }
output($string, $debug=0)
outputs string
Definition: ppyrd.base.php:78
dbHandler::getActiveArchiveRules ( )

gets active ruleset

Definition at line 79 of file dbHandler.php.

80  {
81  return $this->query("SELECT * FROM rule_archive WHERE isActive = 1");
82  }
query($query)
function queries sqlite
Definition: dbHandler.php:47
dbHandler::getActiveRecipients ( )

gets active recipients

Definition at line 129 of file dbHandler.php.

130  {
131  return $this->query("SELECT * FROM rule_recipients WHERE isActive = 1");
132  }
query($query)
function queries sqlite
Definition: dbHandler.php:47
dbHandler::getActiveSenders ( )

gets active senders

Definition at line 87 of file dbHandler.php.

88  {
89  return $this->query("SELECT * FROM rule_senders WHERE isActive = 1");
90  }
query($query)
function queries sqlite
Definition: dbHandler.php:47
dbHandler::getActiveSubjects ( )

gets active subjects

Definition at line 121 of file dbHandler.php.

122  {
123  return $this->query("SELECT * FROM rule_subjects WHERE isActive = 1");
124  }
query($query)
function queries sqlite
Definition: dbHandler.php:47
dbHandler::getConfigValue (   $varname)

gets config values

Parameters
string$varnameto query
Returns
string containing variable value or -1 for error

Definition at line 97 of file dbHandler.php.

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  }
query($query)
function queries sqlite
Definition: dbHandler.php:47
dbHandler::getPersonalVariables ( )

gets all personal variables

Parameters
none
Returns
link to query

Definition at line 112 of file dbHandler.php.

113  {
114  return $this->query("SELECT * FROM rule_personalInfo WHERE isActive = 1");
115  }
query($query)
function queries sqlite
Definition: dbHandler.php:47
dbHandler::open ( )

opens database connection or creates file is not found

Definition at line 151 of file dbHandler.php.

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  }
dbHandler::query (   $query)

function queries sqlite

Parameters
string$queryto execute
Returns
pointer to db results

Definition at line 47 of file dbHandler.php.

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  }
output($string, $debug=0)
outputs string
Definition: ppyrd.base.php:78
dbHandler::update ( )

checks if the database schema needs to be updated

Bug:
more error handling needed?

Definition at line 163 of file dbHandler.php.

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  }
exec($query)
function executes sqlite
Definition: dbHandler.php:64
getConfigValue($varname)
Definition: dbHandler.php:97
output($string, $debug=0)
outputs string
Definition: ppyrd.base.php:78
dbHandler::writeLog (   $oldName,
  $newName,
  $content,
  $log 
)

writes to logs

Parameters
string$oldNamename of the old file
string$newNameof the file
string$contentof the file
string$logmessage

Definition at line 141 of file dbHandler.php.

142  {
143  $safe = SQLite3::escapeString($content);
144  $this->exec("INSERT INTO logs (oldFileName, newFileName, fileContent, log) VALUES ('$oldName', '$newName', '$safe', '$log');");
145  }
exec($query)
function executes sqlite
Definition: dbHandler.php:64

Member Data Documentation

dbHandler::$db

Definition at line 24 of file dbHandler.php.


The documentation for this class was generated from the following file: