OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / admin_main / include / db / admin_tableDb.php
1 <?php
2 /**
3  * DBクラス
4  *
5  * PHP versions 5
6  *
7  * LICENSE: This source file is licensed under the terms of the GNU General Public License.
8  *
9  * @package    Magic3 Framework
10  * @author     平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
11  * @copyright  Copyright 2006-2008 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: admin_tableDb.php 384 2008-03-14 05:09:32Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getDbPath() . '/baseDb.php');
17
18 class admin_tableDb extends BaseDb
19 {       
20         /**
21          * テーブル定義情報にフィールド追加
22          *
23          * @param string $tableName             テーブル名
24          * @param string $fieldName             フィールド名(空文字列=テーブル名保持用)
25          * @param string $dispName              項目表示名
26          * @param string $dataType              データ型
27          * @param string $defaultValue  デフォルト値
28          * @return                                              true = 正常、false=異常
29          */
30         function addTableField($tableName, $fieldName, $dispName, $dataType, $defaultValue)
31         {
32                 global $gEnvManager;
33                 
34                 // 引数エラーチェック
35                 if (empty($tableName)) return false;
36                 
37                 // トランザクション開始
38                 $this->startTransaction();
39                         
40                 // 追加用のインデックスNoを作成
41                 $index = 0;
42                 $queryStr  = 'SELECT * FROM _table_def ';
43                 $queryStr .=   'WHERE td_table_id = ? ';
44                 $ret = $this->isRecordExists($queryStr, array($tableName));
45                 if ($ret){
46                         $queryStr  = 'SELECT max(td_index) as ms FROM _table_def ';
47                         $queryStr .=   'WHERE td_table_id = ? ';
48                         $ret = $this->selectRecord($queryStr, array($tableName), $row);
49                         if ($ret) $index = $row['ms'] + 1;
50                 }
51
52                 // 新規レコード追加
53                 $queryStr = 'INSERT INTO _table_def ';
54                 $queryStr .=  '(';
55                 $queryStr .=  'td_table_id, ';
56                 $queryStr .=  'td_id, ';
57                 $queryStr .=  'td_index, ';
58                 $queryStr .=  'td_type, ';
59                 $queryStr .=  'td_name, ';
60                 $queryStr .=  'td_default_value) ';
61                 $queryStr .=  'VALUES ';
62                 $queryStr .=  '(?, ?, ?, ?, ?, ?)';
63                 $ret =$this->execStatement($queryStr, array($tableName, $fieldName, $index, $dataType, $dispName, $defaultValue));
64                         
65                 // トランザクション確定
66                 $ret = $this->endTransaction();
67                 return $ret;
68         }
69         /**
70          * フィールド定義の削除
71          *
72          * @param int $serial                   シリアル番号
73          * @return bool                                 true=成功、false=失敗
74          */
75         function deleteTableField($serial)
76         {
77                 // テーブルID取得
78                 $queryStr  = 'select * from _table_def ';
79                 $queryStr .=   'where td_serial = ? ';
80                 $ret = $this->selectRecord($queryStr, array($serial), $row);
81                 if (!$ret) return false;
82                 $tableId = $row['td_table_id'];
83                 
84                 // トランザクション開始
85                 $this->startTransaction();
86                 
87                 // データ削除
88                 $queryStr = 'DELETE FROM _table_def ';
89                 $queryStr .=  'WHERE td_serial = ? ';
90                 $this->execStatement($queryStr, array($serial));
91                 
92                 // インデックス番号を更新
93                 $queryStr  = 'SELECT * FROM _table_def ';
94                 $queryStr .=   'WHERE td_table_id = ? ';
95                 $queryStr .=   'ORDER BY td_index';
96                 $retValue = $this->selectRecords($queryStr, array($tableId), $rows);
97                 
98                 for ($i = 0; $i < count($rows); $i++){
99                         $queryStr  = 'UPDATE _table_def ';
100                         $queryStr .=   'SET td_index = ? ';     // インデックス
101                         $queryStr .=   'WHERE td_serial = ?';
102                         $ret = $this->execStatement($queryStr, array($i, $rows[$i]['td_serial']));
103                         if (!$ret){
104                                 $this->endTransaction();
105                                 return false;
106                         }
107                 }
108                 
109                 // トランザクション確定
110                 $ret = $this->endTransaction();
111                 return $ret;
112         }
113         /**
114          * フィールド定義の一括更新
115          *
116          * @param array   $updateValues         更新データ
117          * @return bool                                         true=成功、false=失敗
118          */
119         function updateTableField($updateValues)
120         {       
121                 // トランザクション開始
122                 $this->startTransaction();
123
124                 for ($j = 0; $j < count($updateValues); $j++){
125                         // 1レコード取得
126                         $line = $updateValues[$j];
127                         
128                         // キーを取得
129                         $keys = array_keys($line);
130                                 
131                         // レコード更新
132                         $queryStr = 'UPDATE _table_def ';
133                         $queryStr .=  'SET ';
134                         $values = array();
135                         for ($i = 0; $i < count($keys); $i++){
136                                 if ($keys[$i] != 'td_serial'){
137                                         $queryStr .= $keys[$i] . ' = ?, ';
138                                         $values[] = $line[$keys[$i]];
139                                 }
140                         }
141                         $queryStr = substr($queryStr, 0, strlen($queryStr) -2);// 最後の「, 」を削除
142                         $queryStr .= ' ';
143                         $queryStr .=  'WHERE td_serial = ? ';
144                         $values[] = $line['td_serial'];
145                         $ret =$this->execStatement($queryStr, $values);
146                         if (!$ret){
147                                 $this->endTransaction();
148                                 return false;
149                         }
150                 }
151                         
152                 // トランザクション確定
153                 $ret = $this->endTransaction();
154                 return $ret;
155         }
156         /**
157          * テーブル定義情報の一覧を取得
158          *
159          * @param string        $tableName                      テーブル名
160          * @param function      $callback                       コールバック関数
161          * @return                      なし
162          */
163         function getTableDef($tableName, $callback)
164         {
165                 $queryStr = 'SELECT * FROM _table_def ';
166                 $queryStr .=  'WHERE td_table_id = ? ';
167                 $queryStr .=  'AND td_id != \'\' ';             // 空フィールド以外
168                 $queryStr .=  'ORDER BY td_index';
169                 $this->selectLoop($queryStr, array($tableName), $callback, null);
170         }
171         /**
172          * テーブル定義IDの一覧を取得
173          *
174          * @param function      $callback                       コールバック関数
175          * @return                      なし
176          */
177         function getAllTableIdList($callback)
178         {
179                 $queryStr = 'SELECT DISTINCT td_table_id FROM _table_def ';
180                 $queryStr .=  'ORDER BY td_serial desc';
181                 $this->selectLoop($queryStr, array(), $callback, null);
182         }
183         /**
184          * フィールド名の存在チェック
185          *
186          * @param string  $tableName    テーブル名
187          * @param string  $fieldName    フィールド名
188          * @return bool                                 true=存在する、false=存在しない
189          */
190         function isExistsField($tableName, $fieldName)
191         {
192                 $queryStr = 'SELECT * FROM _table_def ';
193                 $queryStr .=  'WHERE td_table_id = ? ';
194                 $queryStr .=  'AND td_id = ? ';
195                 return $this->isRecordExists($queryStr, array($tableName, $fieldName));
196         }
197         /**
198          * データ取得
199          *
200          * @param string  $tableName    テーブル名
201          * @param int           $limit                          取得する項目数
202          * @param int           $page                           取得するページ(1~)
203          * @param function      $callback       コールバック関数
204          * @return                                              true=正常、false=異常
205          */
206         function getTableData($tableName, $limit, $page, $callback)
207         {
208                 $offset = $limit * ($page -1);
209                 if ($offset < 0) $offset = 0;
210                 
211                 $queryStr  = 'SELECT * FROM ' . $tableName . ' ';
212                 $queryStr .=   'ORDER BY _serial ';
213                 $queryStr .=   'limit ' . $limit . ' offset ' . $offset;
214                 $this->selectLoop($queryStr, array(), $callback, null);
215         }
216         /**
217          * データ総数取得
218          *
219          * @param string  $tableName    テーブル名
220          * @return int                                  総数
221          */
222         function getTableDataListCount($tableName)
223         {
224                 $queryStr  = 'SELECT * FROM ' . $tableName . ' ';
225                 return $this->selectRecordCount($queryStr, array());
226         }
227         /**
228          * シリアル番号でデータ取得
229          *
230          * @param string  $tableName    テーブル名
231          * @param int $serialNo                 シリアル番号
232          * @param array $row                    取得データ
233          * @return                                              true=正常、false=異常
234          */
235         function getTableDataBySerial($tableName, $serialNo, &$row)
236         {
237                 $queryStr  = 'SELECT * FROM ' . $tableName . ' ';
238                 $queryStr .=  'WHERE _serial = ? ';
239                 $ret = $this->selectRecord($queryStr, array($serialNo), $row);
240                 return $ret;
241         }
242         /**
243          * データ新規追加
244          *
245          * @param string  $tableName    テーブル名
246          * @param array   $addValues    追加データ
247          * @param int     $newSerial    追加成功時のシリアル番号
248          * @return                                              true=正常、false=異常
249          */
250         function addTableData($tableName, $addValues, &$newSerial)
251         {
252                 // トランザクション開始
253                 $this->startTransaction();
254
255                 $keys = array_keys($addValues);
256                 
257                 // 新規レコード追加
258                 $queryStr = 'INSERT INTO ' . $tableName;
259                 $queryStr .=  '(';
260                 
261                 $valueStr = '(';
262                 $values = array();
263                 for ($i = 0; $i < count($keys); $i++){
264                         if ($i < count($keys) -1){
265                                 $queryStr .= $keys[$i] . ', ';
266                                 $valueStr .= '?, ';
267                                 $values[] = $addValues[$keys[$i]];
268                         } else {
269                                 $queryStr .= $keys[$i] . ') ';
270                                 $valueStr .= '?) ';
271                                 $values[] = $addValues[$keys[$i]];
272                         }
273                 }
274                 $queryStr .=  'VALUES ';
275                 $queryStr .=  $valueStr;
276                 $ret =$this->execStatement($queryStr, $values);
277                 if (!$ret){
278                         $this->endTransaction();
279                         return false;
280                 }
281                 
282                 $queryStr  = 'SELECT max(_serial) as ms FROM ' . $tableName;
283                 $ret = $this->selectRecord($queryStr, array(), $row);
284                 if ($ret) $newSerial = $row['ms'];
285                         
286                 // トランザクション確定
287                 $ret = $this->endTransaction();
288                 return $ret;
289         }
290         /**
291          * データ更新
292          *
293          * @param string  $tableName            テーブル名
294          * @param int     $serialNo                     シリアル番号
295          * @param array   $updateValues         更新データ
296          * @return                                                      true=正常、false=異常
297          */
298         function updateTableData($tableName, $serialNo, $updateValues)
299         {
300                 // トランザクション開始
301                 $this->startTransaction();
302
303                 // キーを取得
304                 $keys = array_keys($updateValues);
305                                 
306                 // レコード更新
307                 $queryStr = 'UPDATE ' . $tableName . ' ';
308                 $queryStr .=  'SET ';
309                 $values = array();
310                 for ($i = 0; $i < count($keys); $i++){
311                         if ($i < count($keys) -1){
312                                 $queryStr .= $keys[$i] . ' = ?, ';
313                                 $values[] = $updateValues[$keys[$i]];
314                         } else {
315                                 $queryStr .= $keys[$i] . ' = ? ';
316                                 $values[] = $updateValues[$keys[$i]];
317                         }
318                 }
319                 $queryStr .=  'WHERE _serial = ? ';
320                 $values[] = $serialNo;
321                 
322                 $ret =$this->execStatement($queryStr, $values);
323                 if (!$ret){
324                         $this->endTransaction();
325                         return false;
326                 }
327                         
328                 // トランザクション確定
329                 $ret = $this->endTransaction();
330                 return $ret;
331         }
332         /**
333          * データ削除
334          *
335          * @param string  $tableName    テーブル名
336          * @param array $serial                 シリアルNo
337          * @return                                              true=成功、false=失敗
338          */
339         function deleteTableDataBySerial($tableName, $serial)
340         {
341                 global $gEnvManager;
342                         
343                 // 引数のエラーチェック
344                 if (!is_array($serial)) return false;
345                 if (count($serial) <= 0) return true;
346                 
347                 // トランザクション開始
348                 $this->startTransaction();
349                 
350                 // 指定のシリアルNoのレコードが削除状態でないかチェック
351                 for ($i = 0; $i < count($serial); $i++){
352                         $queryStr  = 'SELECT * FROM ' . $tableName . ' ';
353                         $queryStr .=   'WHERE _serial = ? ';
354                         $ret = $this->isRecordExists($queryStr, array($serial[$i]));
355                         // 存在しない場合は、既に削除されたとして終了
356                         if (!$ret){
357                                 $this->endTransaction();
358                                 return false;
359                         }
360                 }
361                 
362                 // データ削除
363                 $queryStr = 'DELETE FROM ' . $tableName . ' ';
364                 $queryStr .=  'WHERE _serial in (' . implode($serial, ',') . ') ';
365                 $this->execStatement($queryStr, array());
366                 
367                 // トランザクション確定
368                 $ret = $this->endTransaction();
369                 return $ret;
370         }
371 }
372 ?>