1 | <?php |
---|
2 | /* |
---|
3 | * Copyright(c) 2000-2006 LOCKON CO.,LTD. All Rights Reserved. |
---|
4 | * |
---|
5 | * http://www.lockon.co.jp/ |
---|
6 | */ |
---|
7 | |
---|
8 | class SC_Query { |
---|
9 | var $option; |
---|
10 | var $where; |
---|
11 | var $conn; |
---|
12 | |
---|
13 | // ¥³¥ó¥¹¥È¥é¥¯¥¿ |
---|
14 | /* |
---|
15 | $err_disp:¥¨¥é¡¼É½¼¨¤ò¹Ô¤¦¤« |
---|
16 | $new¡§¿·µ¬¤ËÀܳ¤ò¹Ô¤¦¤« |
---|
17 | */ |
---|
18 | function SC_Query($dsn = "", $err_disp = true, $new = false) { |
---|
19 | $this->conn = new SC_DBconn($dsn, $err_disp, $new); |
---|
20 | $this->where = ""; |
---|
21 | return $this->conn; |
---|
22 | } |
---|
23 | |
---|
24 | // ¥¨¥é¡¼È½Äê |
---|
25 | function isError() { |
---|
26 | if(PEAR::isError($this->conn->conn)) { |
---|
27 | return true; |
---|
28 | } |
---|
29 | return false; |
---|
30 | } |
---|
31 | |
---|
32 | // COUNTʸ¤Î¼Â¹Ô |
---|
33 | function count($table, $where = "", $arrval = array()) { |
---|
34 | if(strlen($where) <= 0) { |
---|
35 | $sqlse = "SELECT COUNT(*) FROM $table"; |
---|
36 | } else { |
---|
37 | $sqlse = "SELECT COUNT(*) FROM $table WHERE $where"; |
---|
38 | } |
---|
39 | // ¥«¥¦¥ó¥Èʸ¤Î¼Â¹Ô |
---|
40 | $ret = $this->conn->getOne($sqlse, $arrval); |
---|
41 | return $ret; |
---|
42 | } |
---|
43 | |
---|
44 | function select($col, $table, $where = "", $arrval = array()){ |
---|
45 | $sqlse = $this->getsql($col, $table, $where); |
---|
46 | $ret = $this->conn->getAll($sqlse, $arrval); |
---|
47 | return $ret; |
---|
48 | } |
---|
49 | |
---|
50 | function getLastQuery($disp = true) { |
---|
51 | $sql = $this->conn->conn->last_query; |
---|
52 | if($disp) { |
---|
53 | print($sql.";<br />\n"); |
---|
54 | } |
---|
55 | return $sql; |
---|
56 | } |
---|
57 | |
---|
58 | function commit() { |
---|
59 | $this->conn->query("COMMIT"); |
---|
60 | } |
---|
61 | |
---|
62 | function begin() { |
---|
63 | $this->conn->query("BEGIN"); |
---|
64 | } |
---|
65 | |
---|
66 | function rollback() { |
---|
67 | $this->conn->query("ROLLBACK"); |
---|
68 | } |
---|
69 | |
---|
70 | function exec($str, $arrval = array()) { |
---|
71 | $this->conn->query($str, $arrval); |
---|
72 | } |
---|
73 | |
---|
74 | function autoselect($col, $table, $arrwhere = array(), $arrcon = array()) { |
---|
75 | $strw = ""; |
---|
76 | $find = false; |
---|
77 | foreach ($arrwhere as $key => $val) { |
---|
78 | if(strlen($val) > 0) { |
---|
79 | if(strlen($strw) <= 0) { |
---|
80 | $strw .= $key ." LIKE ?"; |
---|
81 | } else if(strlen($arrcon[$key]) > 0) { |
---|
82 | $strw .= " ". $arrcon[$key]. " " . $key ." LIKE ?"; |
---|
83 | } else { |
---|
84 | $strw .= " AND " . $key ." LIKE ?"; |
---|
85 | } |
---|
86 | |
---|
87 | $arrval[] = $val; |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | if(strlen($strw) > 0) { |
---|
92 | $sqlse = "SELECT $col FROM $table WHERE $strw ".$this->option; |
---|
93 | } else { |
---|
94 | $sqlse = "SELECT $col FROM $table ".$this->option; |
---|
95 | } |
---|
96 | $ret = $this->conn->getAll($sqlse, $arrval); |
---|
97 | return $ret; |
---|
98 | } |
---|
99 | |
---|
100 | function getall($sql, $arrval = array()) { |
---|
101 | $ret = $this->conn->getAll($sql, $arrval); |
---|
102 | return $ret; |
---|
103 | } |
---|
104 | |
---|
105 | function getsql($col, $table, $where) { |
---|
106 | if($where != "") { |
---|
107 | // °ú¿ô¤Î$where¤òÍ¥À褷¤Æ¼Â¹Ô¤¹¤ë¡£ |
---|
108 | $sqlse = "SELECT $col FROM $table WHERE $where " . $this->groupby . " " . $this->order . " " . $this->option; |
---|
109 | } else { |
---|
110 | if($this->where != "") { |
---|
111 | $sqlse = "SELECT $col FROM $table WHERE $this->where " . $this->groupby . " " . $this->order . " " . $this->option; |
---|
112 | } else { |
---|
113 | $sqlse = "SELECT $col FROM $table " . $this->groupby . " " . $this->order . " " . $this->option; |
---|
114 | } |
---|
115 | } |
---|
116 | return $sqlse; |
---|
117 | } |
---|
118 | |
---|
119 | function setoption($str) { |
---|
120 | $this->option = $str; |
---|
121 | } |
---|
122 | |
---|
123 | function setlimitoffset($limit, $offset = 0, $return = false) { |
---|
124 | if (is_numeric($limit) && is_numeric($offset)){ |
---|
125 | |
---|
126 | $option.= " LIMIT " . $limit; |
---|
127 | $option.= " OFFSET " . $offset; |
---|
128 | |
---|
129 | if($return){ |
---|
130 | return $option; |
---|
131 | }else{ |
---|
132 | $this->option.= $option; |
---|
133 | } |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | function setgroupby($str) { |
---|
138 | $this->groupby = "GROUP BY " . $str; |
---|
139 | } |
---|
140 | |
---|
141 | function andwhere($str) { |
---|
142 | if($this->where != "") { |
---|
143 | $this->where .= " AND " . $str; |
---|
144 | } else { |
---|
145 | $this->where = $str; |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | function orwhere($str) { |
---|
150 | if($this->where != "") { |
---|
151 | $this->where .= " OR " . $str; |
---|
152 | } else { |
---|
153 | $this->where = $str; |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | function setwhere($str) { |
---|
158 | $this->where = $str; |
---|
159 | } |
---|
160 | |
---|
161 | function setorder($str) { |
---|
162 | $this->order = "ORDER BY " . $str; |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | function setlimit($limit){ |
---|
167 | if ( is_numeric($limit)){ |
---|
168 | $this->option = " LIMIT " .$limit; |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | function setoffset($offset) { |
---|
173 | if ( is_numeric($offset)){ |
---|
174 | $this->offset = " OFFSET " .$offset; |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | // INSERTʸ¤ÎÀ¸À®¡¦¼Â¹Ô |
---|
180 | // $table :¥Æ¡¼¥Ö¥ë̾ |
---|
181 | // $sqlval :Îó̾ => ÃͤγÊǼ¤µ¤ì¤¿¥Ï¥Ã¥·¥åÇÛÎó |
---|
182 | function insert($table, $sqlval) { |
---|
183 | $strcol = ''; |
---|
184 | $strval = ''; |
---|
185 | $find = false; |
---|
186 | |
---|
187 | if(count($sqlval) <= 0 ) return false; |
---|
188 | |
---|
189 | foreach ($sqlval as $key => $val) { |
---|
190 | $strcol .= $key . ','; |
---|
191 | if(eregi("^Now\(\)$", $val)) { |
---|
192 | $strval .= 'Now(),'; |
---|
193 | } else { |
---|
194 | $strval .= '?,'; |
---|
195 | if($val != ""){ |
---|
196 | $arrval[] = $val; |
---|
197 | } else { |
---|
198 | $arrval[] = NULL; |
---|
199 | } |
---|
200 | } |
---|
201 | $find = true; |
---|
202 | } |
---|
203 | if(!$find) { |
---|
204 | return false; |
---|
205 | } |
---|
206 | // ʸËö¤Î","¤òºï½ü |
---|
207 | $strcol = ereg_replace(",$","",$strcol); |
---|
208 | // ʸËö¤Î","¤òºï½ü |
---|
209 | $strval = ereg_replace(",$","",$strval); |
---|
210 | $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")"; |
---|
211 | |
---|
212 | // INSERTʸ¤Î¼Â¹Ô |
---|
213 | $ret = $this->conn->query($sqlin, $arrval); |
---|
214 | |
---|
215 | return $ret; |
---|
216 | } |
---|
217 | |
---|
218 | // INSERTʸ¤ÎÀ¸À®¡¦¼Â¹Ô |
---|
219 | // $table :¥Æ¡¼¥Ö¥ë̾ |
---|
220 | // $sqlval :Îó̾ => ÃͤγÊǼ¤µ¤ì¤¿¥Ï¥Ã¥·¥åÇÛÎó |
---|
221 | function fast_insert($table, $sqlval) { |
---|
222 | $strcol = ''; |
---|
223 | $strval = ''; |
---|
224 | $find = false; |
---|
225 | |
---|
226 | foreach ($sqlval as $key => $val) { |
---|
227 | $strcol .= $key . ','; |
---|
228 | if($val != ""){ |
---|
229 | $eval = pg_escape_string($val); |
---|
230 | $strval .= "'$eval',"; |
---|
231 | } else { |
---|
232 | $strval .= "NULL,"; |
---|
233 | } |
---|
234 | $find = true; |
---|
235 | } |
---|
236 | if(!$find) { |
---|
237 | return false; |
---|
238 | } |
---|
239 | // ʸËö¤Î","¤òºï½ü |
---|
240 | $strcol = ereg_replace(",$","",$strcol); |
---|
241 | // ʸËö¤Î","¤òºï½ü |
---|
242 | $strval = ereg_replace(",$","",$strval); |
---|
243 | $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")"; |
---|
244 | |
---|
245 | // INSERTʸ¤Î¼Â¹Ô |
---|
246 | $ret = $this->conn->query($sqlin); |
---|
247 | |
---|
248 | return $ret; |
---|
249 | } |
---|
250 | |
---|
251 | |
---|
252 | // UPDATEʸ¤ÎÀ¸À®¡¦¼Â¹Ô |
---|
253 | // $table :¥Æ¡¼¥Ö¥ë̾ |
---|
254 | // $sqlval :Îó̾ => ÃͤγÊǼ¤µ¤ì¤¿¥Ï¥Ã¥·¥åÇÛÎó |
---|
255 | // $where :WHEREʸ»úÎó |
---|
256 | function update($table, $sqlval, $where = "", $arradd = "", $addcol = "") { |
---|
257 | $strcol = ''; |
---|
258 | $strval = ''; |
---|
259 | $find = false; |
---|
260 | foreach ($sqlval as $key => $val) { |
---|
261 | if(eregi("^Now\(\)$", $val)) { |
---|
262 | $strcol .= $key . '= Now(),'; |
---|
263 | } else { |
---|
264 | $strcol .= $key . '= ?,'; |
---|
265 | if($val != ""){ |
---|
266 | $arrval[] = $val; |
---|
267 | } else { |
---|
268 | $arrval[] = NULL; |
---|
269 | } |
---|
270 | } |
---|
271 | $find = true; |
---|
272 | } |
---|
273 | if(!$find) { |
---|
274 | return false; |
---|
275 | } |
---|
276 | |
---|
277 | if($addcol != "") { |
---|
278 | foreach($addcol as $key => $val) { |
---|
279 | $strcol .= "$key = $val,"; |
---|
280 | } |
---|
281 | } |
---|
282 | |
---|
283 | // ʸËö¤Î","¤òºï½ü |
---|
284 | $strcol = ereg_replace(",$","",$strcol); |
---|
285 | // ʸËö¤Î","¤òºï½ü |
---|
286 | $strval = ereg_replace(",$","",$strval); |
---|
287 | |
---|
288 | if($where != "") { |
---|
289 | $sqlup = "UPDATE $table SET $strcol WHERE $where"; |
---|
290 | } else { |
---|
291 | $sqlup = "UPDATE $table SET $strcol"; |
---|
292 | } |
---|
293 | |
---|
294 | if(is_array($arradd)) { |
---|
295 | // ¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼ÍѤËÇÛÎó¤òÄɲà |
---|
296 | foreach($arradd as $val) { |
---|
297 | $arrval[] = $val; |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | // INSERTʸ¤Î¼Â¹Ô |
---|
302 | $ret = $this->conn->query($sqlup, $arrval); |
---|
303 | return $ret; |
---|
304 | } |
---|
305 | |
---|
306 | // MAXʸ¤Î¼Â¹Ô |
---|
307 | function max($table, $col, $where = "", $arrval = array()) { |
---|
308 | if(strlen($where) <= 0) { |
---|
309 | $sqlse = "SELECT MAX($col) FROM $table"; |
---|
310 | } else { |
---|
311 | $sqlse = "SELECT MAX($col) FROM $table WHERE $where"; |
---|
312 | } |
---|
313 | // MAXʸ¤Î¼Â¹Ô |
---|
314 | $ret = $this->conn->getOne($sqlse, $arrval); |
---|
315 | return $ret; |
---|
316 | } |
---|
317 | |
---|
318 | // MINʸ¤Î¼Â¹Ô |
---|
319 | function min($table, $col, $where = "", $arrval = array()) { |
---|
320 | if(strlen($where) <= 0) { |
---|
321 | $sqlse = "SELECT MIN($col) FROM $table"; |
---|
322 | } else { |
---|
323 | $sqlse = "SELECT MIN($col) FROM $table WHERE $where"; |
---|
324 | } |
---|
325 | // MINʸ¤Î¼Â¹Ô |
---|
326 | $ret = $this->conn->getOne($sqlse, $arrval); |
---|
327 | return $ret; |
---|
328 | } |
---|
329 | |
---|
330 | // ÆÃÄê¤Î¥«¥é¥à¤ÎÃͤò¼èÆÀ |
---|
331 | function get($table, $col, $where = "", $arrval = array()) { |
---|
332 | if(strlen($where) <= 0) { |
---|
333 | $sqlse = "SELECT $col FROM $table"; |
---|
334 | } else { |
---|
335 | $sqlse = "SELECT $col FROM $table WHERE $where"; |
---|
336 | } |
---|
337 | // SQLʸ¤Î¼Â¹Ô |
---|
338 | $ret = $this->conn->getOne($sqlse, $arrval); |
---|
339 | return $ret; |
---|
340 | } |
---|
341 | |
---|
342 | function getone($sql, $arrval = array()) { |
---|
343 | // SQLʸ¤Î¼Â¹Ô |
---|
344 | $ret = $this->conn->getOne($sql, $arrval); |
---|
345 | return $ret; |
---|
346 | |
---|
347 | } |
---|
348 | |
---|
349 | // °ì¹Ô¤ò¼èÆÀ |
---|
350 | function getrow($table, $col, $where = "", $arrval = array()) { |
---|
351 | if(strlen($where) <= 0) { |
---|
352 | $sqlse = "SELECT $col FROM $table"; |
---|
353 | } else { |
---|
354 | $sqlse = "SELECT $col FROM $table WHERE $where"; |
---|
355 | } |
---|
356 | // SQLʸ¤Î¼Â¹Ô |
---|
357 | $ret = $this->conn->getRow($sqlse, $arrval); |
---|
358 | |
---|
359 | return $ret; |
---|
360 | } |
---|
361 | |
---|
362 | // ¥ì¥³¡¼¥É¤Îºï½ü |
---|
363 | function delete($table, $where = "", $arrval = array()) { |
---|
364 | if(strlen($where) <= 0) { |
---|
365 | $sqlde = "DELETE FROM $table"; |
---|
366 | } else { |
---|
367 | $sqlde = "DELETE FROM $table WHERE $where"; |
---|
368 | } |
---|
369 | $ret = $this->conn->query($sqlde, $arrval); |
---|
370 | return $ret; |
---|
371 | } |
---|
372 | |
---|
373 | function nextval($table, $colname) { |
---|
374 | $sql = ""; |
---|
375 | // postgresql¤Èmysql¤È¤Ç½èÍý¤òʬ¤±¤ë |
---|
376 | if (DB_TYPE == "pgsql") { |
---|
377 | $seqtable = $table . "_" . $colname . "_seq"; |
---|
378 | $sql = "SELECT NEXTVAL('$seqtable')"; |
---|
379 | }else if (DB_TYPE == "mysql") { |
---|
380 | $sql = "SELECT last_insert_id();"; |
---|
381 | } |
---|
382 | $ret = $this->conn->getOne($sql); |
---|
383 | |
---|
384 | return $ret; |
---|
385 | } |
---|
386 | |
---|
387 | function currval($table, $colname) { |
---|
388 | $sql = ""; |
---|
389 | if (DB_TYPE == "pgsql") { |
---|
390 | $seqtable = $table . "_" . $colname . "_seq"; |
---|
391 | $sql = "SELECT CURRVAL('$seqtable')"; |
---|
392 | }else if (DB_TYPE == "mysql") { |
---|
393 | $sql = "SELECT last_insert_id();"; |
---|
394 | } |
---|
395 | $ret = $this->conn->getOne($sql); |
---|
396 | |
---|
397 | return $ret; |
---|
398 | } |
---|
399 | |
---|
400 | function setval($table, $colname, $data) { |
---|
401 | $sql = ""; |
---|
402 | if (DB_TYPE == "pgsql") { |
---|
403 | $seqtable = $table . "_" . $colname . "_seq"; |
---|
404 | $sql = "SELECT SETVAL('$seqtable', $data)"; |
---|
405 | $ret = $this->conn->getOne($sql); |
---|
406 | }else if (DB_TYPE == "mysql") { |
---|
407 | $sql = "ALTER TABLE $table AUTO_INCREMENT=$data"; |
---|
408 | $ret = $this->conn->query($sql); |
---|
409 | } |
---|
410 | |
---|
411 | return $ret; |
---|
412 | } |
---|
413 | |
---|
414 | function query($n ,$arr = "", $ignore_err = false){ |
---|
415 | $result = $this->conn->query($n, $arr, $ignore_err); |
---|
416 | return $result; |
---|
417 | } |
---|
418 | |
---|
419 | // auto_increment¤ò¼èÆÀ¤¹¤ë |
---|
420 | function get_auto_increment($table_name){ |
---|
421 | // ¥í¥Ã¥¯¤¹¤ë |
---|
422 | $this->BEGIN(); |
---|
423 | |
---|
424 | // ¼¡¤ÎIncrement¤ò¼èÆÀ |
---|
425 | $arrRet = $this->getAll("SHOW TABLE STATUS LIKE ?", array($table_name)); |
---|
426 | $auto_inc_no = $arrRet[0]["Auto_increment"]; |
---|
427 | |
---|
428 | // Ãͤò¥«¥¦¥ó¥È¥¢¥Ã¥×¤·¤Æ¤ª¤¯ |
---|
429 | $this->conn->query("ALTER TABLE $table_name AUTO_INCREMENT=?" , $auto_inc_no + 1); |
---|
430 | |
---|
431 | // ²ò½ü¤¹¤ë |
---|
432 | $this->COMMIT(); |
---|
433 | |
---|
434 | return $auto_inc_no; |
---|
435 | } |
---|
436 | } |
---|
437 | |
---|
438 | ?> |
---|