source: branches/comu-utf8/data/class/SC_Query.php @ 15099

Revision 15099, 9.9 KB checked in by Yammy, 17 years ago (diff)

UTF-8変換済みファイルインポート
1.3.4ベース

Line 
1<?php
2/*
3 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
4 *
5 * http://www.lockon.co.jp/
6 */
7
8class SC_Query {
9    var $option;
10    var $where;
11    var $conn;
12    var $groupby;
13    var $order;
14   
15    // コンストラクタ
16    /*
17        $err_disp:エラー表示を行うか
18        $new:新規に接続を行うか
19     */
20    function SC_Query($dsn = "", $err_disp = true, $new = false) {
21        $this->conn = new SC_DBconn($dsn, $err_disp, $new);
22        $this->where = "";
23        return $this->conn;
24    }
25   
26    // エラー判定
27    function isError() {
28        if(PEAR::isError($this->conn->conn)) {
29            return true;
30        }
31        return false;
32    }
33   
34    // COUNT文の実行
35    function count($table, $where = "", $arrval = array()) {
36        if(strlen($where) <= 0) {
37            $sqlse = "SELECT COUNT(*) FROM $table";
38        } else {
39            $sqlse = "SELECT COUNT(*) FROM $table WHERE $where";
40        }
41        // カウント文の実行
42        $ret = $this->conn->getOne($sqlse, $arrval);
43        return $ret;
44    }
45   
46    function select($col, $table, $where = "", $arrval = array()){
47        $sqlse = $this->getsql($col, $table, $where);
48        $ret = $this->conn->getAll($sqlse, $arrval);
49        return $ret;
50    }
51
52    function getLastQuery($disp = true) {
53        $sql = $this->conn->conn->last_query;
54        if($disp) {
55            print($sql.";<br />\n");
56        }
57        return $sql;
58    }
59
60    function commit() {
61        $this->conn->query("COMMIT");
62    }
63   
64    function begin() {
65        $this->conn->query("BEGIN");
66    }
67   
68    function rollback() {
69        $this->conn->query("ROLLBACK");
70    }
71   
72    function exec($str, $arrval = array()) {
73        $this->conn->query($str, $arrval);
74    }
75
76    function autoselect($col, $table, $arrwhere = array(), $arrcon = array()) {
77        $strw = "";         
78        $find = false;
79        foreach ($arrwhere as $key => $val) {
80            if(strlen($val) > 0) {
81                if(strlen($strw) <= 0) {
82                    $strw .= $key ." LIKE ?";
83                } else if(strlen($arrcon[$key]) > 0) {
84                    $strw .= " ". $arrcon[$key]. " " . $key ." LIKE ?";
85                } else {
86                    $strw .= " AND " . $key ." LIKE ?";
87                }
88               
89                $arrval[] = $val;
90            }
91        }
92       
93        if(strlen($strw) > 0) {
94            $sqlse = "SELECT $col FROM $table WHERE $strw ".$this->option;
95        } else {
96            $sqlse = "SELECT $col FROM $table ".$this->option;
97        }
98        $ret = $this->conn->getAll($sqlse, $arrval);
99        return $ret;
100    }
101   
102    function getall($sql, $arrval = array()) {
103        $ret = $this->conn->getAll($sql, $arrval);
104        return $ret;
105    }
106
107    function getsql($col, $table, $where) {
108        if($where != "") {
109            // 引数の$whereを優先して実行する。
110            $sqlse = "SELECT $col FROM $table WHERE $where " . $this->groupby . " " . $this->order . " " . $this->option;
111        } else {
112            if($this->where != "") {
113                    $sqlse = "SELECT $col FROM $table WHERE $this->where " . $this->groupby . " " . $this->order . " " . $this->option;
114                } else {
115                    $sqlse = "SELECT $col FROM $table " . $this->groupby . " " . $this->order . " " . $this->option;
116            }
117        }
118        return $sqlse;
119    }
120           
121    function setoption($str) {
122        $this->option = $str;
123    }
124   
125    function setlimitoffset($limit, $offset = 0, $return = false) {
126        if (is_numeric($limit) && is_numeric($offset)){
127           
128            $option.= " LIMIT " . $limit;
129            $option.= " OFFSET " . $offset;
130           
131            if($return){
132                return $option;
133            }else{
134                $this->option.= $option;
135            }
136        }
137    }
138   
139    function setgroupby($str) {
140        $this->groupby = "GROUP BY " . $str;
141    }
142   
143    function andwhere($str) {
144        if($this->where != "") {
145            $this->where .= " AND " . $str;
146        } else {
147            $this->where = $str;
148        }
149    }
150   
151    function orwhere($str) {
152        if($this->where != "") {
153            $this->where .= " OR " . $str;
154        } else {
155            $this->where = $str;
156        }
157    }
158       
159    function setwhere($str) {
160        $this->where = $str;
161    }
162   
163    function setorder($str) {
164        $this->order = "ORDER BY " . $str;
165    }
166   
167       
168    function setlimit($limit){
169        if ( is_numeric($limit)){
170            $this->option = " LIMIT " .$limit;
171        }   
172    }
173   
174    function setoffset($offset) {
175        if ( is_numeric($offset)){
176            $this->offset = " OFFSET " .$offset;
177        }   
178    }
179   
180   
181    // INSERT文の生成・実行
182    // $table   :テーブル名
183    // $sqlval  :列名 => 値の格納されたハッシュ配列
184    function insert($table, $sqlval) {
185        $strcol = '';
186        $strval = '';
187        $find = false;
188       
189        if(count($sqlval) <= 0 ) return false;
190       
191        foreach ($sqlval as $key => $val) {
192            $strcol .= $key . ',';
193            if(eregi("^Now\(\)$", $val)) {
194                $strval .= 'Now(),';
195            } else {
196                $strval .= '?,';
197                if($val != ""){
198                    $arrval[] = $val;
199                } else {
200                    $arrval[] = NULL;
201                }
202            }
203            $find = true;
204        }
205        if(!$find) {
206            return false;
207        }
208        // 文末の","を削除
209        $strcol = ereg_replace(",$","",$strcol);
210        // 文末の","を削除
211        $strval = ereg_replace(",$","",$strval);
212        $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")";
213       
214        // INSERT文の実行
215        $ret = $this->conn->query($sqlin, $arrval);
216       
217        return $ret;       
218    }
219   
220        // INSERT文の生成・実行
221    // $table   :テーブル名
222    // $sqlval  :列名 => 値の格納されたハッシュ配列
223    function fast_insert($table, $sqlval) {
224        $strcol = '';
225        $strval = '';
226        $find = false;
227       
228        foreach ($sqlval as $key => $val) {
229                $strcol .= $key . ',';
230                if($val != ""){
231                    $eval = pg_escape_string($val);
232                    $strval .= "'$eval',";
233                } else {
234                    $strval .= "NULL,";
235                }
236                $find = true;
237        }
238        if(!$find) {
239            return false;
240        }
241        // 文末の","を削除
242        $strcol = ereg_replace(",$","",$strcol);
243        // 文末の","を削除
244        $strval = ereg_replace(",$","",$strval);
245        $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")";
246       
247        // INSERT文の実行
248        $ret = $this->conn->query($sqlin);
249       
250        return $ret;       
251    }
252   
253   
254    // UPDATE文の生成・実行
255    // $table   :テーブル名
256    // $sqlval  :列名 => 値の格納されたハッシュ配列
257    // $where   :WHERE文字列
258    function update($table, $sqlval, $where = "", $arradd = "", $addcol = "") {
259        $strcol = '';
260        $strval = '';
261        $find = false;
262        foreach ($sqlval as $key => $val) {
263            if(eregi("^Now\(\)$", $val)) {
264                $strcol .= $key . '= Now(),';
265            } else {
266                $strcol .= $key . '= ?,';
267                if($val != ""){
268                    $arrval[] = $val;
269                } else {
270                    $arrval[] = NULL;
271                }
272            }
273            $find = true;
274        }
275        if(!$find) {
276            return false;
277        }
278       
279        if($addcol != "") {
280            foreach($addcol as $key => $val) {
281                $strcol .= "$key = $val,";
282            }
283        }
284               
285        // 文末の","を削除
286        $strcol = ereg_replace(",$","",$strcol);
287        // 文末の","を削除
288        $strval = ereg_replace(",$","",$strval);
289       
290        if($where != "") {
291            $sqlup = "UPDATE $table SET $strcol WHERE $where";
292        } else {
293            $sqlup = "UPDATE $table SET $strcol";
294        }
295       
296        if(is_array($arradd)) {
297            // プレースホルダー用に配列を追加
298            foreach($arradd as $val) {
299                $arrval[] = $val;
300            }
301        }
302       
303        // INSERT文の実行
304        $ret = $this->conn->query($sqlup, $arrval);
305        return $ret;       
306    }
307
308    // MAX文の実行
309    function max($table, $col, $where = "", $arrval = array()) {
310        if(strlen($where) <= 0) {
311            $sqlse = "SELECT MAX($col) FROM $table";
312        } else {
313            $sqlse = "SELECT MAX($col) FROM $table WHERE $where";
314        }
315        // MAX文の実行
316        $ret = $this->conn->getOne($sqlse, $arrval);
317        return $ret;
318    }
319   
320    // MIN文の実行
321    function min($table, $col, $where = "", $arrval = array()) {
322        if(strlen($where) <= 0) {
323            $sqlse = "SELECT MIN($col) FROM $table";
324        } else {
325            $sqlse = "SELECT MIN($col) FROM $table WHERE $where";
326        }
327        // MIN文の実行
328        $ret = $this->conn->getOne($sqlse, $arrval);
329        return $ret;
330    }
331   
332    // 特定のカラムの値を取得
333    function get($table, $col, $where = "", $arrval = array()) {
334        if(strlen($where) <= 0) {
335            $sqlse = "SELECT $col FROM $table";
336        } else {
337            $sqlse = "SELECT $col FROM $table WHERE $where";
338        }
339        // SQL文の実行
340        $ret = $this->conn->getOne($sqlse, $arrval);
341        return $ret;
342    }
343   
344    function getone($sql, $arrval = array()) {
345        // SQL文の実行
346        $ret = $this->conn->getOne($sql, $arrval);
347        return $ret;
348       
349    }
350       
351    // 一行を取得
352    function getrow($table, $col, $where = "", $arrval = array()) {
353        if(strlen($where) <= 0) {
354            $sqlse = "SELECT $col FROM $table";
355        } else {
356            $sqlse = "SELECT $col FROM $table WHERE $where";
357        }
358        // SQL文の実行
359        $ret = $this->conn->getRow($sqlse, $arrval);
360       
361        return $ret;
362    }
363       
364    // レコードの削除
365    function delete($table, $where = "", $arrval = array()) {
366        if(strlen($where) <= 0) {
367            $sqlde = "DELETE FROM $table";
368        } else {
369            $sqlde = "DELETE FROM $table WHERE $where";
370        }
371        $ret = $this->conn->query($sqlde, $arrval);
372        return $ret;
373    }
374   
375    function nextval($table, $colname) {
376        $sql = "";
377        // postgresqlとmysqlとで処理を分ける
378        if (DB_TYPE == "pgsql") {
379            $seqtable = $table . "_" . $colname . "_seq";
380            $sql = "SELECT NEXTVAL('$seqtable')";
381        }else if (DB_TYPE == "mysql") {
382            $sql = "SELECT last_insert_id();";
383        }
384        $ret = $this->conn->getOne($sql);
385       
386        return $ret;
387    }
388   
389    function currval($table, $colname) {
390        $sql = "";
391        if (DB_TYPE == "pgsql") {
392            $seqtable = $table . "_" . $colname . "_seq";
393            $sql = "SELECT CURRVAL('$seqtable')";
394        }else if (DB_TYPE == "mysql") {
395            $sql = "SELECT last_insert_id();";
396        }
397        $ret = $this->conn->getOne($sql);
398       
399        return $ret;
400    }   
401   
402    function setval($table, $colname, $data) {
403        $sql = "";
404        if (DB_TYPE == "pgsql") {
405            $seqtable = $table . "_" . $colname . "_seq";
406            $sql = "SELECT SETVAL('$seqtable', $data)";
407            $ret = $this->conn->getOne($sql);
408        }else if (DB_TYPE == "mysql") {
409            $sql = "ALTER TABLE $table AUTO_INCREMENT=$data";
410            $ret = $this->conn->query($sql);
411        }
412       
413        return $ret;
414    }       
415   
416    function query($n ,$arr = "", $ignore_err = false){
417        $result = $this->conn->query($n, $arr, $ignore_err);
418        return $result;
419    }
420   
421    // auto_incrementを取得する
422    function get_auto_increment($table_name){
423        // ロックする
424        $this->query("LOCK TABLES $table_name WRITE");
425       
426        // 次のIncrementを取得
427        $arrRet = $this->getAll("SHOW TABLE STATUS LIKE ?", array($table_name));
428        $auto_inc_no = $arrRet[0]["Auto_increment"];
429       
430        // 値をカウントアップしておく
431        $this->conn->query("ALTER TABLE $table_name AUTO_INCREMENT=?" , $auto_inc_no + 1);
432       
433        // 解除する
434        $this->query('UNLOCK TABLES');
435       
436        return $auto_inc_no;
437    }
438}
439
440?>
Note: See TracBrowser for help on using the repository browser.