-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQL_Object.php
159 lines (132 loc) · 4.81 KB
/
SQL_Object.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
<?php
####################################################
# Extendable SQL Object Class #
# ------------------------------------------------ #
# This class takes a valid SQL string and #
# converts the resulting MySQL resource into an #
# object that uses dynamic variables. #
# #
# This allows ease of use and quick access to #
# data information. #
# ------------------------------------------------ #
# CREATED BY: Thomas Eynon #
# www.thomaseynon.com #
# CREATED ON: 1/15/2012 #
####################################################
if (!interface_exists("Object_Array")) {
// Blend the interfaces.
interface Object_Array extends Iterator, Countable {
}
}
if (!class_exists("SQL_Object")) {
class SQL_Object implements Object_Array {
public $data, $object, $sql, $pointer, $size;
// Default constructor
function __construct($sql = "", $autoRun = true) {
$this->data = array();
$this->object = array();
$this->sql = "";
$this->pointer = 0;
$this->size = 0;
}
// Run a custom query.
function SQL_Object($sql = "", $autoRun = true) {
$this->data = array();
$this->object = array();
$this->sql = "";
$this->pointer = 0;
$this->size = 0;
if (!empty($sql)) {
$this->run($sql, $autoRun);
}
}
function run($sql, $autoRun = true) {
$this->sql = $sql;
if ($autoRun) {
$this->runQuery();
}
}
function runQuery() {
if ($query = @mysql_query($this->sql)) {
while ($req = mysql_fetch_assoc($query)) {
foreach ($req as $key => $value) {
$this->data[$this->pointer][$key] = $value;
}
$this->pointer++;
}
$this->size = $this->pointer;
$this->pointer = 0;
}
else {
trigger_error("SQL Object: " . mysql_error(), E_USER_WARNING);
}
}
function check($object) {
if (isset($this->object[$this->pointer][$object])) {
return $this->object[$this->pointer][$object];
}
else {
if (method_exists($this, "build{$object}")) {
$method = "build{$object}";
$this->$method();
if (isset($this->object[$this->pointer][$object])) {
return $this->object[$this->pointer][$object];
}
}
}
return false;
}
// PHP 5 Magic Method
// ----------------------------
// Pulls the requested variable.
// If the variable is not defined, throw a notice.
function __get($param) {
// Check if its a data value.
if (isset($this->data[$this->pointer][$param])) {
return $this->data[$this->pointer][$param];
}
// Check if its an object.
$this->check($param);
// Check if its a object value.
if (isset($this->object[$this->pointer][$param])) {
return $this->object[$this->pointer][$param];
}
// See if variable exists now.
if (isset($this->$param)) {
return $this->$param;
}
$trace = debug_backtrace();
trigger_error('Undefined property: ' . $param .
' in ' . $trace[0]['file'] .
' on line ' . $trace[0]['line'],
E_USER_NOTICE);
return null;
}
// Iterator template methods
function current() {
return $this->data[$this->pointer];
}
function next() {
$this->pointer++;
if (isset($this->data[$this->pointer])) {
return $this->data[$this->pointer];
}
return false;
}
function valid() {
if (isset($this->data[$this->pointer])) {
return true;
}
return false;
}
function rewind() {
$this->pointer = 0;
}
function key() {
return $this->pointer;
}
function count() {
return $this->size;
}
}
}