~oh-dev/openhealth/phit-tools

« back to all changes in this revision

Viewing changes to ihris-suite/lib/i2ce/lib/I2CE_MagicDataStorageDB.php

  • Committer: litlfred at ibiblio
  • Date: 2009-10-23 12:59:28 UTC
  • Revision ID: litlfred@ibiblio.org-20091023125928-u5lkafz0urm9t8eq
updated ihris-suite to 4.0.1-prerelease -- not debugged

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
<?php
2
 
/*
3
 
 * © Copyright 2008 IntraHealth International, Inc.
4
 
 * 
5
 
 * This File is part of iHRIS
6
 
 * 
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
 
2
/**
 
3
 * @copyright © 2007, 2008, 2009 Intrahealth International, Inc.
 
4
 * This File is part of I2CE
 
5
 *
 
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.
11
10
 * 
16
15
 * 
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/>.
19
 
 */
20
 
/**
21
18
 * @package I2CE
22
19
 * @author Luke Duncan <lduncan@intrahealth.org>
23
 
 * @copyright Copyright &copy; 2007, 2008 IntraHealth International, Inc. 
24
20
 * @since v3.0.0
25
21
 * @version v3.0.0
26
22
 */
 
23
 
 
24
/**
 
25
 * Deps.
 
26
 */
 
27
require_once "I2CE_MagicDataStorage.php";
 
28
require_once "MDB2.php";
 
29
 
 
30
if (!class_exists('I2CE_MagicDataStorageDB',false)) {
 
31
 
27
32
/**
28
33
 * Configuration class to lookup and save configuration options.
29
34
 * @package I2CE 
30
35
 * @access public
31
36
 */
32
 
require_once "I2CE_MagicDataStorage.php";
33
 
require_once "MDB2.php";
34
 
 
35
 
if (!class_exists('I2CE_MagicDataStorageDB',false)) {
36
 
 
37
37
    class I2CE_MagicDataStorageDB extends I2CE_MagicDataStorage {
38
38
 
39
39
        /**
40
40
         * @var array An array of prepared statements for looking up stored magic data.
41
41
         */
42
 
        static private $db_statements;
 
42
        private $db_statements;
 
43
 
 
44
 
 
45
        public function __construct($name) {
 
46
            parent::__construct($name);
 
47
            $this->setUpStatements();
 
48
        }
43
49
 
44
50
        /**
45
51
         * Set up a cache of prepared statements.
46
52
         * @return MDB2_Statement_Common
47
53
         */
48
 
        static protected function setUpStatement( $table, $stmt ) {
49
 
            if ( !is_array( self::$db_statements ) ) {
50
 
                self::$db_statements = array();
51
 
            }
52
 
            if ( !(array_key_exists($table, self::$db_statements) && is_array( self::$db_statements[$table] ) ) ) {
53
 
                self::$db_statements[$table] = array();
54
 
            }
 
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];
58
 
            }
59
 
            switch( $stmt ) {
60
 
            case "retrieve" :
61
 
                $db_stmt = $db->prepare( "SELECT type,value,children FROM " . $table . " WHERE hash = ? LIMIT 1", 
62
 
                                         array( 'text' ), array( 'integer', 'text', 'text' ) );
63
 
                break;
64
 
            case "destroy":
65
 
                $db_stmt = $db->prepare( "DELETE  FROM " . $table . " WHERE hash = ? LIMIT 1", array('text'), MDB2_PREPARE_RESULT );
66
 
                break;
67
 
            case "store" :
68
 
                $db_stmt = $db->prepare( "REPLACE INTO " . $table 
69
 
                                         . " ( hash, path, type, value, children ) VALUES ( ?, ?, ?, ?, ? )", 
70
 
                                         array( 'text', 'text', 'integer', 'text', 'text' ) );
71
 
                break;
72
 
            }
73
 
            if ( PEAR::isError( $db_stmt ) ) {
74
 
                die( $db_stmt->getMessage() );
75
 
            } else {
76
 
                self::$db_statements[$table][$stmt] = $db_stmt;
77
 
                return $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]);
 
69
                }
78
70
            }
79
71
        }
80
72
 
86
78
         */
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 );
94
85
            } else {
95
86
                $children_str = null;
96
87
            }
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 )) {
102
 
                return false;
103
 
            }
104
 
            return true;
 
88
            if($this->db_statements['store']->
 
89
               execute( array( $hash, $node->getPath(),
 
90
                               $node->getType(), $value, $children_str ) )) {
 
91
                return TRUE;
 
92
            }
 
93
            else {
 
94
                I2CE_MagicDataNode::raiseError( "Error saving to DB " . $node->getPath() .
 
95
                                                " Type: " . $node->getType() . 
 
96
                                                " Value: " . $value .
 
97
                                                " Children: " . implode(',',$children) );
 
98
                return FALSE;
 
99
            }
105
100
        }
106
101
        /**
107
102
         * Retrieve the given I2CE_MagicDataNode value and type.
110
105
         */
111
106
        public function retrieve( $node ) {
112
107
            $hash = $this->getHash( $node );
113
 
            $stmt = self::setUpStatement( $this->name, "retrieve" );
114
 
            $result = $stmt->execute( array( $hash ) );
115
 
            if (I2CE::pearError($result,"Cannot retrieve " . $node->__getPath() . " from DB:")) {
 
108
            $result = $this->db_statements['retrieve']->execute( array( $hash ) );
 
109
            if (I2CE::pearError($result,"Cannot retrieve " . $node->getPath() . " from DB:")) {
116
110
                return false;
117
111
            }
118
112
            if ( $row = $result->fetchRow() ) {
133
127
            if (I2CE::pearError($result,"Cannot access database")) {
134
128
                return false;
135
129
            }
136
 
            return ($result->numRows() > 0);        
 
130
            if ($result->numRows() == 0) {
 
131
                return false;
 
132
            }
 
133
            if (count($this->db_statements) < 3) {
 
134
                return false;
 
135
            }
 
136
            return true;
137
137
        }
138
138
 
139
139
 
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:")) {
145
 
                return false;
146
 
            }
147
 
        }
 
142
            $result = $this->db_statements['destroy']->execute( array( $hash ) );
 
143
            return !I2CE::pearError($result,"Cannot destroy " . $node->getPath() . " from DB:");
 
144
        }
 
145
 
 
146
 
 
147
        /**
 
148
         * Clear the all keys/values associated with this storage 
 
149
         * @return boolean
 
150
         */
 
151
        public function clear() {
 
152
            return !I2CE::pearError( MDB2::singleton()->exec("DELETE  FROM " . $this->name),"Unable to clear DB magic data table {$this->name}");
 
153
        }
 
154
 
 
155
 
148
156
 
149
157
    }
150
158
 
151
159
}
152
160
# Local Variables:
153
161
# mode: php
154
 
# c-default-style: "bsd"
 
162
# c-default-style: bsd
155
163
# indent-tabs-mode: nil
156
164
# c-basic-offset: 4
157
165
# End: