1: <?php
2:
3: /**
4: * db.php
5: *
6: * PHP version 8
7: *
8: * LICENSE: This program is free software: you can redistribute it and/or modify
9: * it under the terms of the GNU Affero General Public License as
10: * published by the Free Software Foundation, either version 3 of the
11: * License, or (at your option) any later version.
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU Affero General Public License for more details.
16: * You should have received a copy of the GNU Affero General Public License
17: * along with this program. If not, see <http://www.gnu.org/licenses/>.
18: *
19: * @category Database
20: * @package DB
21: * @author Henri Schumacher <henri.hulski@gazeta.pl>
22: * @copyright 2007-2014 Henri Schumacher
23: * @license http://www.gnu.org/licenses/agpl.html GNU Affero General Public License v3
24: * @version 1.0
25: * @link https://research.openhomeo.info/download/OpenHomeopath_1.0.2.tar.gz
26: */
27:
28: require_once ("include/classes/db/config_db.php");
29: if (DB_TYPE == 'mysqli') {
30: require_once ("include/classes/db/mysqli.php");
31: } elseif (DB_TYPE == 'mysql') {
32: require_once ("include/classes/db/mysql.php");
33: } else {
34: die ("\n<br><strong>" . _("Please configure in the file 'include/classes/db/config_db.php' the database! If your server supports it, 'mysqli' otherwise 'mysql'.") . "</strong><br>\n");
35: }
36:
37: /**
38: * Database wrapper class
39: *
40: * The DB class allow easy and clean access to common database commands.
41: * At the moment there're drivers for PHP mysql and mysqli.
42: *
43: * @category Database
44: * @package DB
45: * @author Henri Schumacher <henri.hulski@gazeta.pl>
46: * @copyright 2007-2014 Henri Schumacher
47: * @license http://www.gnu.org/licenses/agpl.html GNU Affero General Public License v3
48: */
49: class DB extends DbPlugin {
50:
51: /**
52: * db_fetch_row works analog to mysql_fetch_row
53: *
54: * @param resource $result optional SQL result
55: * @return array SQL result as numeric array
56: * @access public
57: */
58: function db_fetch_row() {
59: $numargs = func_num_args();
60: if ($numargs >= 1) {
61: $result = func_get_arg(0);
62: } else {
63: $result = end($this->result_ar);
64: }
65: return $this->db_fetch_array("NUM", $result);
66: }
67:
68: /**
69: * db_fetch_assoc works analog to mysql_fetch_assoc
70: *
71: * @param resource $result optional SQL result
72: * @return array SQL result as associative array
73: * @access public
74: */
75: function db_fetch_assoc() {
76: $numargs = func_num_args();
77: if ($numargs >= 1) {
78: $result = func_get_arg(0);
79: } else {
80: $result = end($this->result_ar);
81: }
82: return $this->db_fetch_array("ASSOC", $result);
83: }
84:
85: /**
86: * reset_result resets the SQL result, so that the result pointer is on the first element.
87: *
88: * @param resource $result optional SQL result
89: * @return boolean Returns true on success or false on failure.
90: * @access public
91: */
92: function reset_result() {
93: $numargs = func_num_args();
94: if ($numargs >= 1) {
95: $result = func_get_arg(0);
96: } else {
97: $result = end($this->result_ar);
98: }
99: return $this->db_data_seek(0, $result);
100: }
101:
102: /**
103: * send_query_limit is used for pagination of the SQL result
104: *
105: * @param string $query SQL query
106: * @param integer $records number of records to fetch
107: * @param integer $start_from the record number from which to start
108: * @param resource $connection optional SQL link
109: * @return resource SQL result
110: * @access public
111: */
112: function send_query_limit() {
113: $numargs = func_num_args();
114: if ($numargs >= 4) {
115: $query = func_get_arg(0);
116: $records = func_get_arg(1);
117: $start_from = func_get_arg(2);
118: $connection = func_get_arg(3);
119: } else {
120: $query = func_get_arg(0);
121: $records = func_get_arg(1);
122: $start_from = func_get_arg(2);
123: $connection = $this->connection;
124: }
125: $limit = '';
126: if ($start_from >= 0 || $records >= 0)
127: {
128: $start_from = ($start_from >= 0) ? $start_from . "," : '';
129: $records = ($records >= 0) ? $records : '18446744073709551615';
130: $limit = ' LIMIT ' . $start_from . ' ' . $records;
131: }
132: $query .= $limit;
133: $this->send_query($query, $connection);
134: return end($this->result_ar);
135: }
136:
137: /**
138: * get_primary_key returns the primary key of a SQL table or false if not available
139: *
140: * @param string $table SQL table
141: * @return string|false Returns the primary key or false if no primary key is defined
142: * @access public
143: */
144: function get_primary_key($table) {
145: $primary_key = false;
146: if (substr($table, 0, 9) == "archive__") {
147: $table = substr($table, 9);
148: }
149: $query = "DESCRIBE $table";
150: $this->send_query($query);
151: while ($row = $this->db_fetch_assoc()) {
152: if ($row["Key"] == "PRI") {
153: $primary_key = $row["Field"];
154: break;
155: }
156: }
157: $this->free_result();
158: return $primary_key;
159: }
160:
161: /**
162: * table_exists checks if a table exists in the database
163: *
164: * @param string $table SQL table
165: * @return boolean Returns true if table exists or false otherwise.
166: * @access public
167: */
168: function table_exists($table) {
169: $query = "SHOW TABLES";
170: $this->send_query($query);
171: $table_exists = false;
172: while ($row = $this->db_fetch_row()) {
173: if ($table == $row[0]) {
174: $table_exists = true;
175: break;
176: }
177: }
178: return $table_exists;
179: }
180: }
181: // end of class DB
182: