-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql2xml.php
216 lines (162 loc) · 4.45 KB
/
sql2xml.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
<?PHP
class DB {
public function Select($file, $where = "")
{
$file = XMLDIR.$file.".xml";
if(!file_exists($file)) return false;
$xml = simplexml_load_file($file);
for($i = 0; $i < count($xml->item); $i ++)
foreach($xml->item[$i] as $key => $val)
$allRecs[$i][$key] = (string)$val;
if(!$where) return $allRecs;
if(substr_count($where, " LIMIT ") == 1)
{
$arrLimit = explode(" LIMIT ", $where);
$arrAnd = explode(" AND ", $arrLimit[0]);
$arrComma = explode(",", $arrLimit[1]);
for($i = 0; $i < count($arrAnd); $i ++)
$arrLike[$i] = explode(" LIKE ", $arrAnd[$i]);
for($i = 0; $i < count($arrLike); $i ++)
$array[trim($arrLike[$i][0])] = trim($arrLike[$i][1]);
}
else
{
$arrAnd = explode(" AND ", $where);
for($i = 0; $i < count($arrAnd); $i ++)
$arrLike[$i] = explode(" LIKE ", $arrAnd[$i]);
for($i = 0; $i < count($arrLike); $i ++)
$array[trim($arrLike[$i][0])] = trim($arrLike[$i][1]);
}
for($i = 0; $i < count($allRecs); $i ++)
{
foreach($allRecs[$i] as $key => $val)
foreach($array as $key2 => $val2)
if($key == $key2 and $val == $val2)
$count ++;
if($count == count($array))
$resArr[] = $allRecs[$i];
$count = 0;
}
if(substr_count($where, " LIMIT ") == 1)
{
$start = trim($arrComma[0]);
$newsNum = trim($arrComma[1]);
$afterStart = count($resArr) - $start;
if($start < 1) $start = 1;
if($newsNum < 1) $newsNum = 1;
if($newsNum > $afterStart)
$newsNum = $afterStart + 1;
if($start > count($resArr))
{
$start = count($resArr);
$newsNum = 1;
}
for($i = 0; $i < $newsNum; $i ++)
{
foreach($resArr[$start - 1] as $key => $val)
$resArr2[$i][$key] = $val;
$start++;
}
return $resArr2;
}
return $resArr;
}
public function Insert($file, $data)
{
if(!$file or !$data) return false;
if(!file_exists(XMLDIR.$file.".xml"))
{
array_unshift($data, "");
foreach($data as $key => $val)
{
if($key === 0)
{
$key = 'id';
$val = 1;
}
$resArr[$key] = $val;
}
$dom = new DomDocument('1.0');
$items = $dom->appendChild($dom->createElement($file));
$item = $items->appendChild($dom->createElement('item'));
foreach($resArr as $key => $val)
$item->appendChild($dom->createElement($key, $val));
// $dom->formatOutput = true;
$dom->save(XMLDIR.$file.".xml");
return true;
}
$dom = new DomDocument();
// $dom->formatOutput = true;
// $dom->preserveWhiteSpace = false;
$dom->load(XMLDIR.$file.".xml");
$item = $dom->createElement("item");
$allRecs = $this->Select($file);
$lastArr = array_pop($allRecs);
$lastId = $lastArr['id'];
array_unshift($data, "");
foreach($data as $key => $val)
{
if($key === 0)
{
$key = 'id';
$val = $lastId + 1;
}
$resArr[$key] = $val;
}
if(count($resArr) != count($lastArr)) return false;
foreach($resArr as $key => $val)
$item->appendChild($dom->createElement($key, $val));
$dom->getElementsByTagName($file)->item(0)->appendChild($item);
$dom->save(XMLDIR.$file.".xml");
}
public function Update($file, $data, $where)
{
if(!file_exists(XMLDIR.$file.".xml") or !$where or !$data) return false;
$arr = explode(" LIKE ", $where);
$id = trim($arr[1]);
$allRecs = $this->Select($file);
for($i = 0; $i < count($allRecs); $i ++)
foreach($allRecs[$i] as $key => $val)
{
if($allRecs[$i]['id'] == $id)
$array[$i] = $data;
else
$array[$i][$key] = $val;
}
if($allRecs != $array)
{
unlink(XMLDIR.$file.".xml");
for($i = 0; $i < count($array); $i++)
$this->Insert($file, $array[$i]);
}
}
public function Delete($file, $where)
{
if(!file_exists(XMLDIR.$file.".xml") or !$where) return false;
$arr = explode(" LIKE ", $where);
$id = trim($arr[1]);
$allRecs = $this->Select($file);
if(!$allRecs) return false;
$cnt = 0;
for($i = 0; $i < count($allRecs); $i ++)
{
foreach($allRecs[$i] as $key => $val)
{
if($val == $id) $cnt--;
if($key == 'id') continue;
if($id == 1 and count($allRecs) == 1)
return unlink(XMLDIR.$file.".xml");
if($allRecs[$i]['id'] != $id)
$array[$cnt][$key] = $val;
}
$cnt++;
}
if($allRecs and $array and $allRecs != $array)
{
unlink(XMLDIR.$file.".xml");
for($i = 0; $i < count($array); $i++)
$this->Insert($file, $array[$i]);
}
}
}
?>