Skip to content

Commit f55ab74

Browse files
committed
Create _db.php
0 parents  commit f55ab74

File tree

1 file changed

+317
-0
lines changed

1 file changed

+317
-0
lines changed

_db.php

+317
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,317 @@
1+
function refValues($arr){
2+
if (strnatcmp(phpversion(),'5.3') >= 0)
3+
{
4+
$refs = array();
5+
foreach($arr as $key => $value)
6+
$refs[$key] = &$arr[$key];
7+
return $refs;
8+
}
9+
return $arr;
10+
}
11+
12+
function _bd(){
13+
global $_bd;
14+
$h="localhost";
15+
$l="deight";
16+
$p="pas";
17+
$b="base";
18+
$tmp=false;
19+
if(is_null($_bd)){
20+
$_bd = new mysqli($h,$l,$p,$b);
21+
$_bd->set_charset("cp1251");
22+
}elseif(is_null($_bd->stat)){
23+
$tmp = new mysqli($h,$l,$p,$b);
24+
$tmp->set_charset("cp1251");
25+
}
26+
return $tmp?:$_bd;
27+
}
28+
29+
function _db($q,$s='',$v=[],$p=[]){
30+
if(strlen($s)!=count($v))
31+
return false;
32+
$stmt=_bd()->prepare($q);
33+
if(strlen($s)){
34+
array_unshift($v,$s);
35+
call_user_func_array(array($stmt, 'bind_param'),refValues($v));
36+
}
37+
$stmt->execute();
38+
if(count($p))
39+
call_user_func_array(array($stmt, 'bind_result'),$p);
40+
return $stmt;
41+
}
42+
43+
function _dbq($s,$v=[],$o=false){
44+
$c=-1;
45+
$p=array();
46+
$pp=array();
47+
$n=array();
48+
$a='';
49+
$t=array();
50+
$se='';
51+
$tp=array();
52+
if($o){
53+
$o = new stdClass;
54+
$oo=array();
55+
}
56+
foreach(explode(';',$s) as $k=>$w)if($w){
57+
$r[$k]=explode(':',$w);
58+
$t[]=$r[$k][0];
59+
if($o)$o->{$r[$k][0]}=new stdClass;
60+
foreach(explode(',',$r[$k][1]) as $k1=>$w1){
61+
$c++;
62+
$q=explode('.',$w1);
63+
if($q[0])$se.=$r[$k][0].'.'.$q[0].',';
64+
if($o && $q[0]){
65+
$o->{$r[$k][0]}->{$q[0]}=null;
66+
$oo[]=&$o->{$r[$k][0]}->{$q[0]};
67+
}
68+
if(!is_null($v[$c])&&$q[0]){
69+
if(!$k)$tp[]=$q[0];
70+
if(is_numeric($q[1])){
71+
if($k)$pp[]=[$r[$k][0].'.'.$q[0],$q[1],$v[$c]];
72+
else $p[]=[$v[$c],$q[1]];
73+
}else{
74+
$n[]=$v[$c];
75+
$a.=$q[1]?$q[1]:'i';
76+
if($k){
77+
$pp[] = $r[$k][0] . '.' . $q[0] . '?';
78+
}else
79+
$p[]='?';
80+
}
81+
}
82+
}
83+
}
84+
foreach($p as $k=>$w)
85+
if(is_array($w))
86+
$p[$k]=$r[$w[1]][0].'.'.$w[0];
87+
foreach($pp as $k=>$w)
88+
if(is_array($w))
89+
$pp[$k]=$w[0].$r[$w[1]][0].'.'.$w[2];
90+
return [$t,$tp,$p,$a,$n,$pp,substr($se,0,-1),$oo,$o];
91+
}
92+
93+
function _dbw($a,$t,$o='AND'){
94+
$r='';
95+
$s='';
96+
$p=array();
97+
foreach($a as $k=>$v)if(is_array($v)){
98+
if(is_array($v[0])){
99+
$db=_dbw($v,$t,$o=='AND'?'OR':'AND');
100+
if($db[0]){
101+
$r.='('.$db[0].') '.$o.' ';
102+
$s.=$db[1];
103+
foreach($db[2] as $x)
104+
$p[]=$x;
105+
}
106+
}elseif(!is_null($v[1])){
107+
$r.=($v[2]=='b'?'BINARY ':'').($t[(int)$v[3]]?$t[(int)$v[3]]:$t[0]).'.'.$v[0].($v[2]=='p'?$t[(int)$v[4]].'.'.$v[1]:'?').' '.$o.' ';
108+
if($v[2]!='p'){
109+
if($v[2]=='l'){
110+
$s.='s';
111+
$p[]='%'.$v[1].'%';
112+
}elseif($v[2]=='b'){
113+
$s.='s';
114+
$p[]=$v[1];
115+
}else{
116+
$s.=$v[2]?$v[2]:'i';
117+
$p[]=$v[1];
118+
}
119+
}
120+
}
121+
}
122+
return [substr($r,0,-strlen($o)-2),$s,$p];
123+
}
124+
125+
function _dbs($q,$l=null){
126+
//'таблица[:столбцы,][;таблицы[:столбцы,]]',[true|object|string],[array('столбец[=|>|<| LIKE ]',значение,[i,s,b,p,l],[номер таблицы для столбца],[номер таблицы для значения])]
127+
$a=func_get_args();
128+
if(is_int($l))$l='LIMIT 0,'.$l;
129+
$j=[is_string($l),is_object($l),$l===true];
130+
if($j[0]||$j[2]||$j[1])
131+
unset($a[1]);
132+
unset($a[0]);
133+
$q=_dbq($q,null,true);
134+
$w=_dbw($a,$q[0]);
135+
$s='SELECT '.$q[6];
136+
if($j[1]){
137+
if($q[6])$s.=',';
138+
foreach($l as $k=>$v)if($k!='limit' && $k!='join'){
139+
$q[7][]=&$l->{$k};
140+
$s.=$l->{$k}.',';
141+
}
142+
$s=substr($s,0,-1);
143+
}
144+
$s.=' FROM '.implode(',',$q[0]);
145+
if($j[1])if($l->join)
146+
$s.=' '.$l->join;
147+
if($w[0])
148+
$s.=' WHERE '.$w[0];
149+
if($j[0]||$j[2])
150+
$s.=' '.($j[2]?'LIMIT 1':$l);
151+
elseif($j[1])
152+
if($l->limit)
153+
$s.=' '.$l->limit;
154+
$stmt=_db($s,$w[1],$w[2],$q[7]);
155+
$stmt->result=$q[8];
156+
if($j[2]){
157+
$stmt->fetch();
158+
$stmt->close();
159+
$stmt=$q[8];
160+
}
161+
return $stmt;
162+
}
163+
164+
function _dbi($q,$w){
165+
$a=func_get_args();
166+
unset($a[1]);
167+
unset($a[0]);
168+
$q=_dbq($q,$w);
169+
$w=_dbw($a,array_slice($q[0],1));
170+
$s='INSERT INTO '.$q[0][0].'('.implode(',',$q[1]).') ';
171+
if(count($q[0])>1){
172+
$s.='SELECT '.implode(',',$q[2]).' FROM '.substr(implode(',',$q[0]),strlen($q[0][0])+1);
173+
if($w[0]||$q[5][0])
174+
$s.=' WHERE '.implode(' AND ',$q[5]).($w[0]&&$q[5][0]?' AND ':'').$w[0];
175+
$q[3].=$w[1];
176+
$q[4]=array_merge($q[4],$w[2]);
177+
}elseif(is_array($q[4][0])){
178+
$s.='VALUES ';
179+
$qq=[];
180+
$qs='';
181+
foreach($q[4] as $k=>$v){
182+
$s.='('.implode(',',$q[2]).'),';
183+
foreach($v as $vv)
184+
$qq[]=$vv;
185+
$qs.=$q[3];
186+
}
187+
$q[4]=$qq;
188+
$q[3]=$qs;
189+
$s=substr($s,0,-1);
190+
}else
191+
$s.='VALUES ('.implode(',',$q[2]).')';
192+
$stmt=_db($s,$q[3],$q[4]);
193+
$r=$stmt->insert_id;
194+
$stmt->close();
195+
return $r;
196+
}
197+
198+
function _dbu($q,$w,$l=null){
199+
$a=func_get_args();
200+
unset($a[1]);
201+
unset($a[0]);
202+
if(!is_array($l))
203+
unset($a[2]);
204+
$q=_dbq($q,$w);
205+
foreach($q[4] as $k=>$v)
206+
if(is_array($v)){
207+
$q[2][$k]=$v[0].$q[2][$k].$v[2];
208+
$q[4][$k]=$v[1];
209+
}
210+
$w=_dbw($a,$q[0]);
211+
$s='UPDATE '.$q[0][0].' SET '._arrays_str($q[1],$q[2]);
212+
if($w[0])
213+
$s.=' WHERE '.$w[0];
214+
if(is_bool($l))
215+
$s.=' LIMIT 1';
216+
elseif(is_string($l))
217+
$s.=' '.$l;
218+
elseif(is_int($l))
219+
$s.=' LIMIT '.$l;
220+
if($w[0]){
221+
$q[3].=$w[1];
222+
$q[4]=array_merge($q[4],$w[2]);
223+
}
224+
$stmt=_db($s,$q[3],$q[4]);
225+
$r=$stmt->affected_rows;
226+
$stmt->close();
227+
return $r;
228+
}
229+
230+
function _dbp($q,$v,$l){//Изменить или добавить
231+
$ar=func_get_args();
232+
$result=call_user_func_array("_dbu",$ar);
233+
if(!$result){
234+
unset($ar[1]);
235+
unset($ar[0]);
236+
if(!is_array($l))
237+
unset($ar[2]);
238+
foreach($ar as $w){
239+
$q.=','.substr($w[0],0,-1).($w[2]?".$w[2]":'');
240+
$v[]=$w[1];
241+
}
242+
$result=_dbi($q,$v);
243+
}
244+
return $result;
245+
}
246+
247+
function _dba($q){//Все поля
248+
$a=func_get_args();
249+
unset($a[0]);
250+
$q=_dbq($q,null,true);
251+
$w=_dbw($a,$q[0]);
252+
$temp=$q[0][0];
253+
$stmt=_db("SELECT * FROM $temp WHERE $w[0] LIMIT 1",$w[1],$w[2]);
254+
$result=$stmt->get_result()->fetch_object();
255+
$stmt->close();
256+
return $result;
257+
}
258+
259+
function _dbc(){//Количество
260+
$ar=func_get_args();
261+
array_splice($ar,1,0,'');
262+
$ar[1]=new stdClass;
263+
$ar[1]->count='count(*)';
264+
$stmt=call_user_func_array('_dbs',$ar);
265+
$stmt->fetch();
266+
$stmt->close();
267+
return $ar[1]->count;
268+
}
269+
270+
function _dbd($q,$l=null){//Удалить
271+
$ar=func_get_args();
272+
unset($ar[0]);
273+
if($l===true)
274+
unlink($ar[1]);
275+
$q=_dbq($q,null,true);
276+
$w=_dbw($ar,$q[0]);
277+
$temp=$q[0][0];
278+
$stmt=_db("DELETE FROM $temp WHERE $w[0]".($l===true?' LIMIT 1':''),$w[1],$w[2]);
279+
$r=$stmt->affected_rows;
280+
$stmt->close();
281+
return $r;
282+
}
283+
284+
function _dbds($q){// Удалить выборку // 'таблица:id'
285+
$q=explode(';',$q);
286+
$q=explode(':',$q[0]);
287+
$stmt=call_user_func_array("_dbs",func_get_args());
288+
$ar=$stmt->result->{$q[0]};
289+
$w='';
290+
$or='';
291+
while($stmt->fetch()){
292+
$w.="$or$q[0].$q[1]=".$ar->{$q[1]};
293+
$or=' OR ';
294+
}
295+
$stmt->close();
296+
$stmt=_db("DELETE FROM $q[0] WHERE $w");
297+
$r=$stmt->affected_rows;
298+
$stmt->close();
299+
return $r;
300+
}
301+
302+
function _dbl($limit,$count=12){
303+
$limit=is_object($limit)?$limit->limit:$limit;
304+
return 'LIMIT '.($limit*$count).','.$count;
305+
}
306+
307+
function _dbf($stmt,&$count=0){
308+
$result=[];
309+
while($stmt->fetch()){
310+
$result[$count]=new stdClass;
311+
foreach($stmt->result as $k=>$v)
312+
$result[$count]->{$k}=copyObject($v);
313+
$count++;
314+
}
315+
$stmt->close();
316+
return $result;
317+
}

0 commit comments

Comments
 (0)