-
Notifications
You must be signed in to change notification settings - Fork 0
/
orm.php
156 lines (132 loc) · 2.81 KB
/
orm.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
<?php
class MysqlDriver
{
function connect()
{
mysql_connect( "", "", "" );
mysql_select_db( "" );
}
function safe( $var )
{
return mysql_real_escape_string( $var );
}
function query( $query )
{
return mysql_query( $query );
}
function row_number( $result )
{
return mysql_num_rows( $result );
}
function get_row( $result )
{
return mysql_fetch_assoc( $result );
}
function get_error()
{
return mysql_error();
}
}
class ORM extends MysqlDriver
{
var $__table = "";
var $__ans = null;
var $__count = 0;
var $__errors = array();
var $__PK = 'id';
function __construct( $table )
{
$this->__table = $table;
// $this->connect();
}
public static function getInstance( $table )
{
if(file_exists("models/$table.php"))
{
include_once("models/$table.php");
$objname = $table."Model";
return new $objname($table);
} else {
return new ORM($table);
}
}
/* Model */
function count()
{
return $this->__count;
}
/* Get data from Database.
Split into two seperate functions, one for easy access by PK, one for more advanced constraints */
function get( $by )
{
$by = $this->safe( $by );
$this->__ans = $this->query( "SELECT * FROM ".$this->__table." WHERE ".$this->__PK." = '$by' LIMIT 1;" ); // TODO: Automatically find out primary key?
if( !$this->__ans )
{
$this->__errors[] = $this->get_error();
$this->__count = 0;
} else {
$this->__count = $this->row_number( $this->__ans );
}
return $this->get_row( $this->__ans );
}
function getBy( $by )
{
$sql = "SELECT * FROM ".$this->__table." WHERE ";
end( $by );
$end = key( $by );
foreach($by as $key => $value)
{
$sql .= "`".$this->safe($key)."` = '". $this->safe( $value ) ."'";
if($end != $key)
{
$sql .= ' AND ';
}
}
$sql .= " AND 1=1;";
$this->__ans = $this->query( $sql );
if( !$this->__ans )
{
$this->__errors[] = $this->get_error();
} else {
return $this->getList();
}
}
function getList($ans = null)
{
if(is_null($ans))
$ans = $this->__ans;
$arr = array();
while( $row = $this->get_row( $this->__ans ) )
{
$arr[] = $row;
}
return $arr;
}
function save( $values )
{
$sql = "";
if( empty( $values[$this->__PK] ) )
{
$sql = "INSERT INTO " . $this->__table . " SET ";
} else {
$sql = "UPDATE " . $this->__table . " SET ";
}
end( $values );
$end = key( $values );
foreach($values as $key => $value)
{
$sql .= "`".$this->safe($key)."` = '". $this->safe( $value ) ."'";
if($end != $key)
{
$sql .= ', ';
}
}
$this->__ans = $this->query( $sql );
if( !$this->__ans )
{
$this->__errors[] = $this->get_error();
}
}
}
?>