3
* © Copyright 2008 IntraHealth International, Inc.
5
* This File is part of iHRIS
7
* iHRIS is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
3
* @copyright © 2007, 2008, 2009 Intrahealth International, Inc.
4
* This File is part of I2CE
6
* I2CE is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License as published by
9
8
* the Free Software Foundation; either version 3 of the License, or
10
9
* (at your option) any later version.
17
16
* You should have received a copy of the GNU General Public License
18
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22
19
* @author Luke Duncan <lduncan@intrahealth.org>
23
* @copyright Copyright © 2007, 2008 IntraHealth International, Inc.
27
require_once "I2CE_MagicDataStorage.php";
28
require_once "MDB2.php";
30
if (!class_exists('I2CE_MagicDataStorageDB',false)) {
28
33
* Configuration class to lookup and save configuration options.
32
require_once "I2CE_MagicDataStorage.php";
33
require_once "MDB2.php";
35
if (!class_exists('I2CE_MagicDataStorageDB',false)) {
37
37
class I2CE_MagicDataStorageDB extends I2CE_MagicDataStorage {
40
40
* @var array An array of prepared statements for looking up stored magic data.
42
static private $db_statements;
42
private $db_statements;
45
public function __construct($name) {
46
parent::__construct($name);
47
$this->setUpStatements();
45
51
* Set up a cache of prepared statements.
46
52
* @return MDB2_Statement_Common
48
static protected function setUpStatement( $table, $stmt ) {
49
if ( !is_array( self::$db_statements ) ) {
50
self::$db_statements = array();
52
if ( !(array_key_exists($table, self::$db_statements) && is_array( self::$db_statements[$table] ) ) ) {
53
self::$db_statements[$table] = array();
54
protected function setUpStatements() {
55
$this->db_statements = array();
55
56
$db = MDB2::singleton();
56
if ( array_key_exists( $stmt, self::$db_statements[$table] ) && !PEAR::isError( self::$db_statements[$table][$stmt] ) ) {
57
return self::$db_statements[$table][$stmt];
61
$db_stmt = $db->prepare( "SELECT type,value,children FROM " . $table . " WHERE hash = ? LIMIT 1",
62
array( 'text' ), array( 'integer', 'text', 'text' ) );
65
$db_stmt = $db->prepare( "DELETE FROM " . $table . " WHERE hash = ? LIMIT 1", array('text'), MDB2_PREPARE_RESULT );
68
$db_stmt = $db->prepare( "REPLACE INTO " . $table
69
. " ( hash, path, type, value, children ) VALUES ( ?, ?, ?, ?, ? )",
70
array( 'text', 'text', 'integer', 'text', 'text' ) );
73
if ( PEAR::isError( $db_stmt ) ) {
74
die( $db_stmt->getMessage() );
76
self::$db_statements[$table][$stmt] = $db_stmt;
57
$this->db_statements['retrieve']
58
=$db->prepare( "SELECT type,value,children FROM " . $this->name . " WHERE hash = ? LIMIT 1",
59
array( 'text' ), array( 'integer', 'text', 'text' ) );
60
$this->db_statements['destroy']
61
=$db->prepare( "DELETE FROM " . $this->name . " WHERE hash = ? LIMIT 1", array('text'), MDB2_PREPARE_RESULT );
62
$this->db_statements['store']
63
= $db->prepare( "REPLACE INTO " . $this->name
64
. " ( hash, path, type, value, children ) VALUES ( ?, ?, ?, ?, ? )",
65
array( 'text', 'text', 'integer', 'text', 'text' ) );
66
foreach ($this->db_statements as $type=>$db_stmt) {
67
if ( PEAR::isError( $db_stmt ) ) {
68
unset($this->db_statements[$type]);
87
79
public function store( $node ){
88
80
$hash = $this->getHash( $node );
89
$stmt = self::setUpStatement($this->name, "store" );
90
$value = $node->__getSaveValue();
91
$children = $node->getKeys();
81
$value = $node->getSaveValue();
82
$children = $node->getKeys(null,true);
92
83
if ( count( $children ) > 0 ) {
93
84
$children_str = implode( ",", $children );
95
86
$children_str = null;
97
$ret = $stmt->execute( array( $hash, $node->__getPath(), $node->__getType(), $value, $children_str ) );
98
if (I2CE::pearError( $ret, "Error saving " . $node->__getPath() .
99
" Type: " . $node->__getType() .
100
" Value: " . $value .
101
" Children: " . $children_str )) {
88
if($this->db_statements['store']->
89
execute( array( $hash, $node->getPath(),
90
$node->getType(), $value, $children_str ) )) {
94
I2CE_MagicDataNode::raiseError( "Error saving to DB " . $node->getPath() .
95
" Type: " . $node->getType() .
97
" Children: " . implode(',',$children) );
107
102
* Retrieve the given I2CE_MagicDataNode value and type.
133
127
if (I2CE::pearError($result,"Cannot access database")) {
136
return ($result->numRows() > 0);
130
if ($result->numRows() == 0) {
133
if (count($this->db_statements) < 3) {
140
140
public function destroy($node) {
141
141
$hash = $this->getHash( $node );
142
$stmt = self::setUpStatement( $this->name, "destroy" );
143
$result = $stmt->execute( array( $hash ) );
144
if (I2CE::pearError($result,"Cannot destroy " . $node->__getPath() . " from DB:")) {
142
$result = $this->db_statements['destroy']->execute( array( $hash ) );
143
return !I2CE::pearError($result,"Cannot destroy " . $node->getPath() . " from DB:");
148
* Clear the all keys/values associated with this storage
151
public function clear() {
152
return !I2CE::pearError( MDB2::singleton()->exec("DELETE FROM " . $this->name),"Unable to clear DB magic data table {$this->name}");
152
160
# Local Variables:
154
# c-default-style: "bsd"
162
# c-default-style: bsd
155
163
# indent-tabs-mode: nil
156
164
# c-basic-offset: 4