-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParadox_Database.class.php
executable file
·250 lines (214 loc) · 4.82 KB
/
Paradox_Database.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
/**
* Paradox Database Class
*
* Makes use of PHP's Paradox functions (via pxlib) to conveniently access data
* in a Paradox database with SQL-style functions.
* @package Paradox DB
* @author Luke Mundy
*/
class Paradox_Database
{
private $_px = NULL;
private $_fp = NULL;
private $_file = '';
private $_mode = '';
private $_select = array();
private $_where = array();
private $_limit = 0;
private $_offset = 0;
/**
* Constructor
*
* Set some default values
* @return void
*/
public function __construct()
{
// Create PHP's Paradox database object
$this->_px = new paradox_db();
}
/**
* Destructor
*
* Close the database and file pointer
* @return void
*/
public function __destruct()
{
$this->_px->close();
if ($this->_fp) fclose($this->_fp);
}
/**
* Select which fields to return
* @return void
*/
public function select()
{
$args = func_get_args();
// Was an array of fields supplied?
if (is_array($args[0]))
{
foreach ($args[0] as $field)
{
// Make sure it is a string and not already in the list
if (is_string($field) && ! in_array($field, $this->_select)) $this->_select[] = $field;
}
}
// Or a comma seperated list?
elseif (is_string($args[0]))
{
$fields = explode(', ', $args[0]);
foreach ($fields as $field)
{
$field = trim($field);
// Make sure field isn't already in the list
if ( ! in_array($field, $this->_select)) $this->_select[] = $field;
}
}
// Return $this so we can do some cool method chaining
return $this;
}
/**
* Filter returned rows by a certain criteria
* @return void
*/
public function where()
{
$args = func_get_args();
if (is_array($args[0]))
{
foreach ($args[0] as $test)
{
$this->_where[] = array(
'field' => $test[0],
'operator' => $test[1],
'value' => escapeshellarg($test[2])
);
}
}
elseif (is_string($args[0]))
{
$this->_where[] = array(
'field' => $args[0],
'operator' => $args[1],
'value' => escapeshellarg($args[2])
);
}
// Return $this so we can do some cool method chaining
return $this;
}
/**
* Limit the amount of return results
* @return void
*/
public function limit()
{
if (func_num_args() == 1) $this->_limit = func_get_arg(0);
else
{
$this->_offset = func_get_arg(0);
$this->_limit = func_get_arg(1);
}
// Return $this so we can do some cool method chaining
return $this;
}
/**
* Get records
* @return array Matched records
*/
public function get()
{
// Start with an empty array
$ret = array();
// Loop through all records in the database
for ($x = 0; ($x < $this->num_records() && count($ret) < $this->_limit); $x++)
{
$row = $this->_px->retrieve_record($x);
if ($this->_test($row))
{
foreach ($row as $key => $val)
{
// Find all fields not in the select array
if ( ! in_array($key, $this->_select) && ! empty($this->_select)) unset($row[$key]);
}
$ret[] = $row;
}
}
return $ret;
}
/**
* Open the database
* @param string $file Path to the database file
* @param string $mode File open mode
* @return bool TRUE on success, FALSE otherwise
*/
public function open($file, $mode = 'r')
{
$this->_file = $file;
$this->_mode = $mode;
return $this->_open();
}
/**
* Tests the supplied row
* @param array $row
* @return bool TRUE is row passes all tests
*/
private function _test($row)
{
$pass = TRUE;
// If there are no tests, all rows will pass
if ( ! empty($this->_where))
{
foreach ($this->_where as $test)
{
$field = escapeshellarg($row[$test['field']]);
$txt = "return ({$field} {$test['operator']} {$test['value']});";
// Check for failure
if ( ! eval($txt))
{
$pass = FALSE;
// No need to try other tests
break;
}
}
}
return $pass;
}
/**
* Private open function
* @return bool TRUE on success, FALSE otherwise
*/
public function _open()
{
$ret = FALSE;
// Check file exists
if (file_exists($this->_file))
{
$this->_fp = fopen($this->_file, $this->_mode);
// Opened successfully?
if ($this->_fp)
{
// Database opened successfully?
if ($this->_px->open_fp($this->_fp))
{
$ret = TRUE;
}
}
}
return $ret;
}
/** ------------------------------------------------------------------------
* Accessor Functions
*/
public function get_file_pointer() { return $this->_fp; }
public function get_paradox_object() { return $this->_px; }
public function num_records() { return $this->_px->numrecords(); }
public function num_fields() { return $this->_px->numfields(); }
public function debug()
{
echo 'SELECT '. print_r($this->_select, TRUE) ."\n";
echo 'WHERE '. print_r($this->_where, TRUE) ."\n";
}
}
// END - class Paradox_Database