7 * LICENSE: This source file is licensed under the terms of the GNU General Public License.
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
16 require_once($gEnvManager->getDbPath() . '/baseDb.php');
18 class admin_tableDb extends BaseDb
23 * @param string $tableName テーブル名
24 * @param string $fieldName フィールド名(空文字列=テーブル名保持用)
25 * @param string $dispName 項目表示名
26 * @param string $dataType データ型
27 * @param string $defaultValue デフォルト値
28 * @return true = 正常、false=異常
30 function addTableField($tableName, $fieldName, $dispName, $dataType, $defaultValue)
35 if (empty($tableName)) return false;
38 $this->startTransaction();
42 $queryStr = 'SELECT * FROM _table_def ';
43 $queryStr .= 'WHERE td_table_id = ? ';
44 $ret = $this->isRecordExists($queryStr, array($tableName));
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;
53 $queryStr = 'INSERT INTO _table_def ';
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));
66 $ret = $this->endTransaction();
72 * @param int $serial シリアル番号
73 * @return bool true=成功、false=失敗
75 function deleteTableField($serial)
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'];
85 $this->startTransaction();
88 $queryStr = 'DELETE FROM _table_def ';
89 $queryStr .= 'WHERE td_serial = ? ';
90 $this->execStatement($queryStr, array($serial));
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);
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']));
104 $this->endTransaction();
110 $ret = $this->endTransaction();
116 * @param array $updateValues 更新データ
117 * @return bool true=成功、false=失敗
119 function updateTableField($updateValues)
122 $this->startTransaction();
124 for ($j = 0; $j < count($updateValues); $j++){
126 $line = $updateValues[$j];
129 $keys = array_keys($line);
132 $queryStr = 'UPDATE _table_def ';
135 for ($i = 0; $i < count($keys); $i++){
136 if ($keys[$i] != 'td_serial'){
137 $queryStr .= $keys[$i] . ' = ?, ';
138 $values[] = $line[$keys[$i]];
141 $queryStr = substr($queryStr, 0, strlen($queryStr) -2);// 最後の「, 」を削除
143 $queryStr .= 'WHERE td_serial = ? ';
144 $values[] = $line['td_serial'];
145 $ret =$this->execStatement($queryStr, $values);
147 $this->endTransaction();
153 $ret = $this->endTransaction();
159 * @param string $tableName テーブル名
160 * @param function $callback コールバック関数
163 function getTableDef($tableName, $callback)
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);
174 * @param function $callback コールバック関数
177 function getAllTableIdList($callback)
179 $queryStr = 'SELECT DISTINCT td_table_id FROM _table_def ';
180 $queryStr .= 'ORDER BY td_serial desc';
181 $this->selectLoop($queryStr, array(), $callback, null);
186 * @param string $tableName テーブル名
187 * @param string $fieldName フィールド名
188 * @return bool true=存在する、false=存在しない
190 function isExistsField($tableName, $fieldName)
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));
200 * @param string $tableName テーブル名
201 * @param int $limit 取得する項目数
202 * @param int $page 取得するページ(1~)
203 * @param function $callback コールバック関数
204 * @return true=正常、false=異常
206 function getTableData($tableName, $limit, $page, $callback)
208 $offset = $limit * ($page -1);
209 if ($offset < 0) $offset = 0;
211 $queryStr = 'SELECT * FROM ' . $tableName . ' ';
212 $queryStr .= 'ORDER BY _serial ';
213 $queryStr .= 'limit ' . $limit . ' offset ' . $offset;
214 $this->selectLoop($queryStr, array(), $callback, null);
219 * @param string $tableName テーブル名
222 function getTableDataListCount($tableName)
224 $queryStr = 'SELECT * FROM ' . $tableName . ' ';
225 return $this->selectRecordCount($queryStr, array());
230 * @param string $tableName テーブル名
231 * @param int $serialNo シリアル番号
232 * @param array $row 取得データ
233 * @return true=正常、false=異常
235 function getTableDataBySerial($tableName, $serialNo, &$row)
237 $queryStr = 'SELECT * FROM ' . $tableName . ' ';
238 $queryStr .= 'WHERE _serial = ? ';
239 $ret = $this->selectRecord($queryStr, array($serialNo), $row);
245 * @param string $tableName テーブル名
246 * @param array $addValues 追加データ
247 * @param int $newSerial 追加成功時のシリアル番号
248 * @return true=正常、false=異常
250 function addTableData($tableName, $addValues, &$newSerial)
253 $this->startTransaction();
255 $keys = array_keys($addValues);
258 $queryStr = 'INSERT INTO ' . $tableName;
263 for ($i = 0; $i < count($keys); $i++){
264 if ($i < count($keys) -1){
265 $queryStr .= $keys[$i] . ', ';
267 $values[] = $addValues[$keys[$i]];
269 $queryStr .= $keys[$i] . ') ';
271 $values[] = $addValues[$keys[$i]];
274 $queryStr .= 'VALUES ';
275 $queryStr .= $valueStr;
276 $ret =$this->execStatement($queryStr, $values);
278 $this->endTransaction();
282 $queryStr = 'SELECT max(_serial) as ms FROM ' . $tableName;
283 $ret = $this->selectRecord($queryStr, array(), $row);
284 if ($ret) $newSerial = $row['ms'];
287 $ret = $this->endTransaction();
293 * @param string $tableName テーブル名
294 * @param int $serialNo シリアル番号
295 * @param array $updateValues 更新データ
296 * @return true=正常、false=異常
298 function updateTableData($tableName, $serialNo, $updateValues)
301 $this->startTransaction();
304 $keys = array_keys($updateValues);
307 $queryStr = 'UPDATE ' . $tableName . ' ';
310 for ($i = 0; $i < count($keys); $i++){
311 if ($i < count($keys) -1){
312 $queryStr .= $keys[$i] . ' = ?, ';
313 $values[] = $updateValues[$keys[$i]];
315 $queryStr .= $keys[$i] . ' = ? ';
316 $values[] = $updateValues[$keys[$i]];
319 $queryStr .= 'WHERE _serial = ? ';
320 $values[] = $serialNo;
322 $ret =$this->execStatement($queryStr, $values);
324 $this->endTransaction();
329 $ret = $this->endTransaction();
335 * @param string $tableName テーブル名
336 * @param array $serial シリアルNo
337 * @return true=成功、false=失敗
339 function deleteTableDataBySerial($tableName, $serial)
344 if (!is_array($serial)) return false;
345 if (count($serial) <= 0) return true;
348 $this->startTransaction();
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 // 存在しない場合は、既に削除されたとして終了
357 $this->endTransaction();
363 $queryStr = 'DELETE FROM ' . $tableName . ' ';
364 $queryStr .= 'WHERE _serial in (' . implode($serial, ',') . ') ';
365 $this->execStatement($queryStr, array());
368 $ret = $this->endTransaction();