1 | <?php |
---|
2 | /** |
---|
3 | * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved. |
---|
4 | * |
---|
5 | * http://www.lockon.co.jp/ |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * SC_Query¥¯¥é¥¹ |
---|
10 | * |
---|
11 | * @author LOCKON CO.,LTD. |
---|
12 | * @access public |
---|
13 | */ |
---|
14 | class SC_Query { |
---|
15 | /**#@+ |
---|
16 | * @access private |
---|
17 | */ |
---|
18 | |
---|
19 | /** |
---|
20 | * SC_DBConn¥ª¥Ö¥¸¥§¥¯¥È |
---|
21 | * @var SC_DBConn |
---|
22 | */ |
---|
23 | var $conn; |
---|
24 | |
---|
25 | /** |
---|
26 | * LIMIT,OFFSET ¶ç |
---|
27 | * @var string |
---|
28 | */ |
---|
29 | var $option; |
---|
30 | |
---|
31 | /** |
---|
32 | * WHERE ¶ç |
---|
33 | * @var string |
---|
34 | */ |
---|
35 | var $where; |
---|
36 | |
---|
37 | /** |
---|
38 | * GROUP BY ¶ç |
---|
39 | * @var string |
---|
40 | */ |
---|
41 | var $groupby; |
---|
42 | |
---|
43 | /** |
---|
44 | * ORDER BY ¶ç |
---|
45 | * @var string |
---|
46 | */ |
---|
47 | var $order; |
---|
48 | |
---|
49 | /**#@-*/ |
---|
50 | |
---|
51 | /** |
---|
52 | * SC_Query¥¯¥é¥¹¤Î¥³¥ó¥¹¥È¥é¥¯¥¿ |
---|
53 | * |
---|
54 | * @access public |
---|
55 | * @param string $dsn DSN¾ðÊó |
---|
56 | * @param boolean $err_disp ¥¨¥é¡¼É½¼¨¤ò¹Ô¤¦¤«¤É¤¦¤« |
---|
57 | * @param boolean $new ¿·µ¬¤ËDBÀܳ¤ò¹Ô¤¦¤«¤É¤¦¤« |
---|
58 | */ |
---|
59 | function SC_Query($dsn = "", $err_disp = true, $new = false) { |
---|
60 | $this->conn = new SC_DBconn($dsn, $err_disp, $new); |
---|
61 | $this->where = ""; |
---|
62 | $this->option = ""; |
---|
63 | $this->groupby = ""; |
---|
64 | return $this->conn; //? |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * DB¥¨¥é¡¼¤ÎȽÄê |
---|
69 | * |
---|
70 | * @access public |
---|
71 | * @return boolean À®¸ù»þ¡§true ¼ºÇÔ»þ¡§false |
---|
72 | */ |
---|
73 | function isError() { |
---|
74 | if(PEAR::isError($this->conn->conn)) { |
---|
75 | return true; |
---|
76 | } |
---|
77 | return false; |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * COUNTʸ¤Î¼Â¹Ô |
---|
82 | * |
---|
83 | * @access public |
---|
84 | * @param string $table ¥Æ¡¼¥Ö¥ë̾ |
---|
85 | * @param string $where WHERE¶ç |
---|
86 | * @param array $arrval ¥×¥ì¡¼¥¹¥Û¥ë¥À¤ÎÇÛÎó |
---|
87 | * @return string ¥ì¥³¡¼¥É·ï¿ô |
---|
88 | */ |
---|
89 | function count($table, $where = "", $arrval = array()) { |
---|
90 | if(strlen($where) <= 0) { |
---|
91 | $sqlse = "SELECT COUNT(*) FROM $table"; |
---|
92 | } else { |
---|
93 | $sqlse = "SELECT COUNT(*) FROM $table WHERE $where"; |
---|
94 | } |
---|
95 | // ¥«¥¦¥ó¥Èʸ¤Î¼Â¹Ô |
---|
96 | $ret = $this->conn->getOne($sqlse, $arrval); |
---|
97 | return $ret; |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * SELECTʸ¤Î¼Â¹Ô |
---|
102 | * |
---|
103 | * @access public |
---|
104 | * @param string $col ¥«¥é¥à̾ |
---|
105 | * @param string $table ¥Æ¡¼¥Ö¥ë̾ |
---|
106 | * @param string $where WHERE¶ç |
---|
107 | * @param array $arrval ¥×¥ì¡¼¥¹¥Û¥ë¥À¤ÎÇÛÎó |
---|
108 | * @return array SELECTʸ¤Î¼Â¹Ô·ë²Ì |
---|
109 | */ |
---|
110 | function select($col, $table, $where = "", $arrval = array()){ |
---|
111 | $sqlse = $this->getsql($col, $table, $where); |
---|
112 | $ret = $this->conn->getAll($sqlse, $arrval); |
---|
113 | return $ret; |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * ºÇ¸å¤Ë¼Â¹Ô¤·¤¿SQLʸ¤ò¼èÆÀ¤¹¤ë |
---|
118 | * |
---|
119 | * @access public |
---|
120 | * @param boolean $disp SQLʸ¤òprint¤¹¤ë¤«¤É¤¦¤« |
---|
121 | * @return string $disp==false¤Î¾ì¹ç¡§ºÇ¸å¤Ë¼Â¹Ô¤·¤¿SQLʸ $disp==true¤Î¾ì¹ç¡§¤Ê¤· |
---|
122 | */ |
---|
123 | function getLastQuery($disp = true) { |
---|
124 | $sql = $this->conn->conn->last_query; |
---|
125 | if($disp) { |
---|
126 | print($sql.";<br />\n"); |
---|
127 | } |
---|
128 | return $sql; |
---|
129 | } |
---|
130 | |
---|
131 | function commit() { |
---|
132 | $this->conn->query("COMMIT"); |
---|
133 | } |
---|
134 | |
---|
135 | function begin() { |
---|
136 | $this->conn->query("BEGIN"); |
---|
137 | } |
---|
138 | |
---|
139 | function rollback() { |
---|
140 | $this->conn->query("ROLLBACK"); |
---|
141 | } |
---|
142 | |
---|
143 | function exec($str, $arrval = array()) { |
---|
144 | $this->conn->query($str, $arrval); |
---|
145 | } |
---|
146 | |
---|
147 | function autoselect($col, $table, $arrwhere = array(), $arrcon = array()) { |
---|
148 | $strw = ""; |
---|
149 | $find = false; |
---|
150 | foreach ($arrwhere as $key => $val) { |
---|
151 | if(strlen($val) > 0) { |
---|
152 | if(strlen($strw) <= 0) { |
---|
153 | $strw .= $key ." LIKE ?"; |
---|
154 | } else if(strlen($arrcon[$key]) > 0) { |
---|
155 | $strw .= " ". $arrcon[$key]. " " . $key ." LIKE ?"; |
---|
156 | } else { |
---|
157 | $strw .= " AND " . $key ." LIKE ?"; |
---|
158 | } |
---|
159 | |
---|
160 | $arrval[] = $val; |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | if(strlen($strw) > 0) { |
---|
165 | $sqlse = "SELECT $col FROM $table WHERE $strw ".$this->option; |
---|
166 | } else { |
---|
167 | $sqlse = "SELECT $col FROM $table ".$this->option; |
---|
168 | } |
---|
169 | $ret = $this->conn->getAll($sqlse, $arrval); |
---|
170 | return $ret; |
---|
171 | } |
---|
172 | |
---|
173 | function getall($sql, $arrval = array()) { |
---|
174 | $ret = $this->conn->getAll($sql, $arrval); |
---|
175 | return $ret; |
---|
176 | } |
---|
177 | |
---|
178 | /** |
---|
179 | * SELECTʸ¤ò¹½ÃÛ¤¹¤ë |
---|
180 | * |
---|
181 | * @access public |
---|
182 | * @param string $col ¥«¥é¥à̾ |
---|
183 | * @param string $table ¥Æ¡¼¥Ö¥ë̾ |
---|
184 | * @param string $where WHERE¶ç |
---|
185 | * @return string SQLʸ |
---|
186 | */ |
---|
187 | function getsql($col, $table, $where="") { |
---|
188 | if($where != "") { |
---|
189 | // °ú¿ô¤Î$where¤òÍ¥À褷¤Æ¼Â¹Ô¤¹¤ë¡£ |
---|
190 | $sqlse = "SELECT $col FROM $table WHERE $where " . $this->groupby . " " . $this->order . " " . $this->option; |
---|
191 | } else { |
---|
192 | if($this->where != "") { |
---|
193 | $sqlse = "SELECT $col FROM $table WHERE $this->where " . $this->groupby . " " . $this->order . " " . $this->option; |
---|
194 | } else { |
---|
195 | $sqlse = "SELECT $col FROM $table " . $this->groupby . " " . $this->order . " " . $this->option; |
---|
196 | } |
---|
197 | } |
---|
198 | return $sqlse; |
---|
199 | } |
---|
200 | |
---|
201 | /** |
---|
202 | * WHERE,GROUPBY,ORDERBY°Ê³°¤Î¥ª¥×¥·¥ç¥Ê¥ë¤Ê¶ç¤ò¥»¥Ã¥È¤¹¤ë |
---|
203 | * |
---|
204 | * @access public |
---|
205 | * @param string $str ¥ª¥×¥·¥ç¥ó¤Ë»ÈÍѤ¹¤ëʸ»úÎó |
---|
206 | */ |
---|
207 | function setoption($str) { |
---|
208 | $this->option = $str; |
---|
209 | } |
---|
210 | |
---|
211 | /** |
---|
212 | * LIMIT¶ç¡¢OFFSET¶ç¤ò¥»¥Ã¥È¤¹¤ë |
---|
213 | * |
---|
214 | * @access public |
---|
215 | * @param mixed $limit LIMIT¤Î·ï¿ô |
---|
216 | * @param mixed $offset OFFSET¤Î·ï¿ô |
---|
217 | * @param string $return À¸À®¤·¤¿LIMIT,OFFSET¶ç¤òreturn¤¹¤ë¤«¤É¤¦¤« |
---|
218 | * @return string À¸À®¤·¤¿LIMIT,OFFSET¶ç |
---|
219 | */ |
---|
220 | function setlimitoffset($limit, $offset = 0, $return = false) { |
---|
221 | if (is_numeric($limit) && is_numeric($offset)){ |
---|
222 | |
---|
223 | $option.= " LIMIT " . $limit; |
---|
224 | $option.= " OFFSET " . $offset; |
---|
225 | |
---|
226 | if($return){ |
---|
227 | return $option; |
---|
228 | }else{ |
---|
229 | $this->option.= $option; |
---|
230 | } |
---|
231 | } |
---|
232 | } |
---|
233 | |
---|
234 | /** |
---|
235 | * GROUP BY ¶ç¤ò¥»¥Ã¥È¤¹¤ë |
---|
236 | * |
---|
237 | * @access public |
---|
238 | * @param string $str ¥«¥é¥à̾ |
---|
239 | */ |
---|
240 | function setgroupby($str) { |
---|
241 | $this->groupby = "GROUP BY " . $str; |
---|
242 | } |
---|
243 | |
---|
244 | /** |
---|
245 | * WHERE ¶ç¤ò¥»¥Ã¥È¤¹¤ë(AND) |
---|
246 | * |
---|
247 | * @access public |
---|
248 | * @param string $str WHERE ¶ç |
---|
249 | * @example $objQuery->andWhere('product_id = ?'); |
---|
250 | */ |
---|
251 | function andwhere($str) { |
---|
252 | if($this->where != "") { |
---|
253 | $this->where .= " AND " . $str; |
---|
254 | } else { |
---|
255 | $this->where = $str; |
---|
256 | } |
---|
257 | } |
---|
258 | |
---|
259 | /** |
---|
260 | * WHERE ¶ç¤ò¥»¥Ã¥È¤¹¤ë(OR) |
---|
261 | * |
---|
262 | * @access public |
---|
263 | * @param string $str WHERE ¶ç |
---|
264 | * @example $objQuery->orWhere('product_id = ?'); |
---|
265 | */ |
---|
266 | function orwhere($str) { |
---|
267 | if($this->where != "") { |
---|
268 | $this->where .= " OR " . $str; |
---|
269 | } else { |
---|
270 | $this->where = $str; |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | /** |
---|
275 | * WHERE ¶ç¤ò¥»¥Ã¥È¤¹¤ë |
---|
276 | * |
---|
277 | * @access public |
---|
278 | * @param string $str WHERE ¶ç |
---|
279 | * @example $objQuery->setWhere('product_id = ?'); |
---|
280 | */ |
---|
281 | function setwhere($str) { |
---|
282 | $this->where = $str; |
---|
283 | } |
---|
284 | |
---|
285 | /** |
---|
286 | * ORDER BY ¶ç¤ò¥»¥Ã¥È¤¹¤ë |
---|
287 | * |
---|
288 | * @access public |
---|
289 | * @param string $str ¥«¥é¥à̾ |
---|
290 | * @example $objQuery->setorder("rank DESC"); |
---|
291 | */ |
---|
292 | function setorder($str) { |
---|
293 | $this->order = "ORDER BY " . $str; |
---|
294 | } |
---|
295 | |
---|
296 | /** |
---|
297 | * LIMIT ¶ç¤ò¥»¥Ã¥È¤¹¤ë |
---|
298 | * |
---|
299 | * @access public |
---|
300 | * @param mixed $limit LIMIT¤Î·ï¿ô |
---|
301 | * @example $objQuery->setlimit(50); |
---|
302 | */ |
---|
303 | function setlimit($limit){ |
---|
304 | if ( is_numeric($limit)){ |
---|
305 | $this->option = " LIMIT " .$limit; |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | /** |
---|
310 | * OFFSET ¶ç¤ò¥»¥Ã¥È¤¹¤ë |
---|
311 | * |
---|
312 | * @access public |
---|
313 | * @param mixed $offset OFFSET¤Î·ï¿ô |
---|
314 | * @example $objQuery->setOffset(30); |
---|
315 | */ |
---|
316 | function setoffset($offset) { |
---|
317 | if ( is_numeric($offset)){ |
---|
318 | $this->offset = " OFFSET " .$offset; |
---|
319 | } |
---|
320 | } |
---|
321 | |
---|
322 | |
---|
323 | // INSERTʸ¤ÎÀ¸À®¡¦¼Â¹Ô |
---|
324 | // $table :¥Æ¡¼¥Ö¥ë̾ |
---|
325 | // $sqlval :Îó̾ => ÃͤγÊǼ¤µ¤ì¤¿¥Ï¥Ã¥·¥åÇÛÎó |
---|
326 | function insert($table, $sqlval) { |
---|
327 | $strcol = ''; |
---|
328 | $strval = ''; |
---|
329 | $find = false; |
---|
330 | |
---|
331 | if(count($sqlval) <= 0 ) return false; |
---|
332 | |
---|
333 | foreach ($sqlval as $key => $val) { |
---|
334 | $strcol .= $key . ','; |
---|
335 | if(eregi("^Now\(\)$", $val)) { |
---|
336 | $strval .= 'Now(),'; |
---|
337 | } else { |
---|
338 | $strval .= '?,'; |
---|
339 | if($val != ""){ |
---|
340 | $arrval[] = $val; |
---|
341 | } else { |
---|
342 | $arrval[] = NULL; |
---|
343 | } |
---|
344 | } |
---|
345 | $find = true; |
---|
346 | } |
---|
347 | if(!$find) { |
---|
348 | return false; |
---|
349 | } |
---|
350 | // ʸËö¤Î","¤òºï½ü |
---|
351 | $strcol = ereg_replace(",$","",$strcol); |
---|
352 | // ʸËö¤Î","¤òºï½ü |
---|
353 | $strval = ereg_replace(",$","",$strval); |
---|
354 | $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")"; |
---|
355 | |
---|
356 | // INSERTʸ¤Î¼Â¹Ô |
---|
357 | $ret = $this->conn->query($sqlin, $arrval); |
---|
358 | |
---|
359 | return $ret; |
---|
360 | } |
---|
361 | |
---|
362 | // INSERTʸ¤ÎÀ¸À®¡¦¼Â¹Ô |
---|
363 | // $table :¥Æ¡¼¥Ö¥ë̾ |
---|
364 | // $sqlval :Îó̾ => ÃͤγÊǼ¤µ¤ì¤¿¥Ï¥Ã¥·¥åÇÛÎó |
---|
365 | function fast_insert($table, $sqlval) { |
---|
366 | $strcol = ''; |
---|
367 | $strval = ''; |
---|
368 | $find = false; |
---|
369 | |
---|
370 | foreach ($sqlval as $key => $val) { |
---|
371 | $strcol .= $key . ','; |
---|
372 | if($val != ""){ |
---|
373 | $eval = pg_escape_string($val); |
---|
374 | $strval .= "'$eval',"; |
---|
375 | } else { |
---|
376 | $strval .= "NULL,"; |
---|
377 | } |
---|
378 | $find = true; |
---|
379 | } |
---|
380 | if(!$find) { |
---|
381 | return false; |
---|
382 | } |
---|
383 | // ʸËö¤Î","¤òºï½ü |
---|
384 | $strcol = ereg_replace(",$","",$strcol); |
---|
385 | // ʸËö¤Î","¤òºï½ü |
---|
386 | $strval = ereg_replace(",$","",$strval); |
---|
387 | $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")"; |
---|
388 | |
---|
389 | // INSERTʸ¤Î¼Â¹Ô |
---|
390 | $ret = $this->conn->query($sqlin); |
---|
391 | |
---|
392 | return $ret; |
---|
393 | } |
---|
394 | |
---|
395 | |
---|
396 | // UPDATEʸ¤ÎÀ¸À®¡¦¼Â¹Ô |
---|
397 | // $table :¥Æ¡¼¥Ö¥ë̾ |
---|
398 | // $sqlval :Îó̾ => ÃͤγÊǼ¤µ¤ì¤¿¥Ï¥Ã¥·¥åÇÛÎó |
---|
399 | // $where :WHEREʸ»úÎó |
---|
400 | function update($table, $sqlval, $where = "", $arradd = "", $addcol = "") { |
---|
401 | $strcol = ''; |
---|
402 | $strval = ''; |
---|
403 | $find = false; |
---|
404 | foreach ($sqlval as $key => $val) { |
---|
405 | if(eregi("^Now\(\)$", $val)) { |
---|
406 | $strcol .= $key . '= Now(),'; |
---|
407 | } else { |
---|
408 | $strcol .= $key . '= ?,'; |
---|
409 | if($val != ""){ |
---|
410 | $arrval[] = $val; |
---|
411 | } else { |
---|
412 | $arrval[] = NULL; |
---|
413 | } |
---|
414 | } |
---|
415 | $find = true; |
---|
416 | } |
---|
417 | if(!$find) { |
---|
418 | return false; |
---|
419 | } |
---|
420 | |
---|
421 | if($addcol != "") { |
---|
422 | foreach($addcol as $key => $val) { |
---|
423 | $strcol .= "$key = $val,"; |
---|
424 | } |
---|
425 | } |
---|
426 | |
---|
427 | // ʸËö¤Î","¤òºï½ü |
---|
428 | $strcol = ereg_replace(",$","",$strcol); |
---|
429 | // ʸËö¤Î","¤òºï½ü |
---|
430 | $strval = ereg_replace(",$","",$strval); |
---|
431 | |
---|
432 | if($where != "") { |
---|
433 | $sqlup = "UPDATE $table SET $strcol WHERE $where"; |
---|
434 | } else { |
---|
435 | $sqlup = "UPDATE $table SET $strcol"; |
---|
436 | } |
---|
437 | |
---|
438 | if(is_array($arradd)) { |
---|
439 | // ¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼ÍѤËÇÛÎó¤òÄɲà |
---|
440 | foreach($arradd as $val) { |
---|
441 | $arrval[] = $val; |
---|
442 | } |
---|
443 | } |
---|
444 | |
---|
445 | // INSERTʸ¤Î¼Â¹Ô |
---|
446 | $ret = $this->conn->query($sqlup, $arrval); |
---|
447 | return $ret; |
---|
448 | } |
---|
449 | |
---|
450 | // MAXʸ¤Î¼Â¹Ô |
---|
451 | function max($table, $col, $where = "", $arrval = array()) { |
---|
452 | if(strlen($where) <= 0) { |
---|
453 | $sqlse = "SELECT MAX($col) FROM $table"; |
---|
454 | } else { |
---|
455 | $sqlse = "SELECT MAX($col) FROM $table WHERE $where"; |
---|
456 | } |
---|
457 | // MAXʸ¤Î¼Â¹Ô |
---|
458 | $ret = $this->conn->getOne($sqlse, $arrval); |
---|
459 | return $ret; |
---|
460 | } |
---|
461 | |
---|
462 | // MINʸ¤Î¼Â¹Ô |
---|
463 | function min($table, $col, $where = "", $arrval = array()) { |
---|
464 | if(strlen($where) <= 0) { |
---|
465 | $sqlse = "SELECT MIN($col) FROM $table"; |
---|
466 | } else { |
---|
467 | $sqlse = "SELECT MIN($col) FROM $table WHERE $where"; |
---|
468 | } |
---|
469 | // MINʸ¤Î¼Â¹Ô |
---|
470 | $ret = $this->conn->getOne($sqlse, $arrval); |
---|
471 | return $ret; |
---|
472 | } |
---|
473 | |
---|
474 | // ÆÃÄê¤Î¥«¥é¥à¤ÎÃͤò¼èÆÀ |
---|
475 | function get($table, $col, $where = "", $arrval = array()) { |
---|
476 | if(strlen($where) <= 0) { |
---|
477 | $sqlse = "SELECT $col FROM $table"; |
---|
478 | } else { |
---|
479 | $sqlse = "SELECT $col FROM $table WHERE $where"; |
---|
480 | } |
---|
481 | // SQLʸ¤Î¼Â¹Ô |
---|
482 | $ret = $this->conn->getOne($sqlse, $arrval); |
---|
483 | return $ret; |
---|
484 | } |
---|
485 | |
---|
486 | function getone($sql, $arrval = array()) { |
---|
487 | // SQLʸ¤Î¼Â¹Ô |
---|
488 | $ret = $this->conn->getOne($sql, $arrval); |
---|
489 | return $ret; |
---|
490 | |
---|
491 | } |
---|
492 | |
---|
493 | // °ì¹Ô¤ò¼èÆÀ |
---|
494 | function getrow($table, $col, $where = "", $arrval = array()) { |
---|
495 | if(strlen($where) <= 0) { |
---|
496 | $sqlse = "SELECT $col FROM $table"; |
---|
497 | } else { |
---|
498 | $sqlse = "SELECT $col FROM $table WHERE $where"; |
---|
499 | } |
---|
500 | // SQLʸ¤Î¼Â¹Ô |
---|
501 | $ret = $this->conn->getRow($sqlse, $arrval); |
---|
502 | |
---|
503 | return $ret; |
---|
504 | } |
---|
505 | |
---|
506 | // ¥ì¥³¡¼¥É¤Îºï½ü |
---|
507 | function delete($table, $where = "", $arrval = array()) { |
---|
508 | if(strlen($where) <= 0) { |
---|
509 | $sqlde = "DELETE FROM $table"; |
---|
510 | } else { |
---|
511 | $sqlde = "DELETE FROM $table WHERE $where"; |
---|
512 | } |
---|
513 | $ret = $this->conn->query($sqlde, $arrval); |
---|
514 | return $ret; |
---|
515 | } |
---|
516 | |
---|
517 | //»ØÄꤷ¤¿¥«¥é¥à¤Î°ìÈֺǸå¤Ë¥ì¥³¡¼¥É¤òÁÞÆþ |
---|
518 | function nextval($table, $colname) { |
---|
519 | $sql = ""; |
---|
520 | // postgresql¤Èmysql¤È¤Ç½èÍý¤òʬ¤±¤ë |
---|
521 | if (DB_TYPE == "pgsql") { |
---|
522 | $seqtable = $table . "_" . $colname . "_seq"; |
---|
523 | $sql = "SELECT NEXTVAL('$seqtable')"; |
---|
524 | $ret = $this->conn->getOne($sql); |
---|
525 | }else if (DB_TYPE == "mysql") { |
---|
526 | $sql = "SELECT last_insert_id();"; |
---|
527 | $ret = $this->conn->getOne($sql); |
---|
528 | } |
---|
529 | |
---|
530 | |
---|
531 | return $ret; |
---|
532 | } |
---|
533 | |
---|
534 | function currval($table, $colname) { |
---|
535 | $sql = ""; |
---|
536 | if (DB_TYPE == "pgsql") { |
---|
537 | $seqtable = $table . "_" . $colname . "_seq"; |
---|
538 | $sql = "SELECT CURRVAL('$seqtable')"; |
---|
539 | }else if (DB_TYPE == "mysql") { |
---|
540 | $sql = "SELECT last_insert_id();"; |
---|
541 | } |
---|
542 | $ret = $this->conn->getOne($sql); |
---|
543 | |
---|
544 | return $ret; |
---|
545 | } |
---|
546 | |
---|
547 | function setval($table, $colname, $data) { |
---|
548 | $sql = ""; |
---|
549 | if (DB_TYPE == "pgsql") { |
---|
550 | $seqtable = $table . "_" . $colname . "_seq"; |
---|
551 | $sql = "SELECT SETVAL('$seqtable', $data)"; |
---|
552 | $ret = $this->conn->getOne($sql); |
---|
553 | }else if (DB_TYPE == "mysql") { |
---|
554 | $sql = "ALTER TABLE $table AUTO_INCREMENT=$data"; |
---|
555 | $ret = $this->conn->query($sql); |
---|
556 | } |
---|
557 | |
---|
558 | return $ret; |
---|
559 | } |
---|
560 | |
---|
561 | function query($n ,$arr = "", $ignore_err = false){ |
---|
562 | $result = $this->conn->query($n, $arr, $ignore_err); |
---|
563 | return $result; |
---|
564 | } |
---|
565 | |
---|
566 | // auto_increment¤ò¼èÆÀ¤¹¤ë |
---|
567 | function get_auto_increment($table_name){ |
---|
568 | // ¥í¥Ã¥¯¤¹¤ë |
---|
569 | $this->BEGIN(); |
---|
570 | |
---|
571 | // ¼¡¤ÎIncrement¤ò¼èÆÀ |
---|
572 | $arrRet = $this->getAll("SHOW TABLE STATUS LIKE ?", array($table_name)); |
---|
573 | $auto_inc_no = $arrRet[0]["Auto_increment"]; |
---|
574 | |
---|
575 | // Ãͤò¥«¥¦¥ó¥È¥¢¥Ã¥×¤·¤Æ¤ª¤¯ |
---|
576 | $this->conn->query("ALTER TABLE $table_name AUTO_INCREMENT=?" , $auto_inc_no + 1); |
---|
577 | |
---|
578 | // ²ò½ü¤¹¤ë |
---|
579 | $this->COMMIT(); |
---|
580 | |
---|
581 | return $auto_inc_no; |
---|
582 | } |
---|
583 | } |
---|
584 | ?> |
---|