2 // vim: foldmethod=marker
6 * @author Masaki Fujimoto <fujimoto@php.net>
7 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
12 // {{{ Ethna_AppObject
14 * ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¥ª¥Ö¥¸¥§¥¯¥È¤Î¥Ù¡¼¥¹¥¯¥é¥¹
16 * @author Masaki Fujimoto <fujimoto@php.net>
19 * @todo Ê£¿ô¥Æ¡¼¥Ö¥ë¤ÎJOINÂбþ
27 /** @var object Ethna_Backend backend¥ª¥Ö¥¸¥§¥¯¥È */
30 /** @var object Ethna_Config ÀßÄꥪ¥Ö¥¸¥§¥¯¥È */
33 /** @var object Ethna_DB DB¥ª¥Ö¥¸¥§¥¯¥È */
36 /** @var object Ethna_I18N i18n¥ª¥Ö¥¸¥§¥¯¥È */
39 /** @var object Ethna_ActionForm ¥¢¥¯¥·¥ç¥ó¥Õ¥©¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È */
42 /** @var object Ethna_ActionForm ¥¢¥¯¥·¥ç¥ó¥Õ¥©¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È(¾Êά·Á) */
45 /** @var object Ethna_Session ¥»¥Ã¥·¥ç¥ó¥ª¥Ö¥¸¥§¥¯¥È */
48 /** @var array ¥Æ¡¼¥Ö¥ëÄêµÁ */
49 var $table_def = array();
51 /** @var array ¥×¥í¥Ñ¥Æ¥£ÄêµÁ */
52 var $prop_def = array();
54 /** @var array ¥×¥í¥Ñ¥Æ¥£ */
56 var $prop_backup = null;
58 /** @var array ¥×¥é¥¤¥Þ¥ê¥¡¼ÄêµÁ */
61 /** @var int ¥ª¥Ö¥¸¥§¥¯¥ÈID */
68 * Ethna_AppObject¥¯¥é¥¹¤Î¥³¥ó¥¹¥È¥é¥¯¥¿
71 * @param object Ethna_Backend &$backend Ethna_Backend¥ª¥Ö¥¸¥§¥¯¥È
72 * @param mixed $key_type ¸¡º÷¥¡¼Ì¾
73 * @param mixed $key ¸¡º÷¥¡¼
74 * @param array $prop ¥×¥í¥Ñ¥Æ¥£°ìÍ÷
75 * @return mixed 0:Àµ¾ï½ªÎ» -1:¥¡¼/¥×¥í¥Ñ¥Æ¥£Ì¤»ØÄê Ethna_Error:¥¨¥é¡¼
77 function Ethna_AppObject(&$backend, $key_type = null, $key = null, $prop = null)
79 $this->backend =& $backend;
80 $this->config =& $backend->getConfig();
81 $this->action_form =& $backend->getActionForm();
82 $this->af =& $this->action_form;
83 $this->session =& $backend->getSession();
85 $db_list = $backend->getDBList();
86 if (Ethna::isError($db_list) == false) {
87 foreach ($db_list as $elt) {
88 $varname = $elt['varname'];
89 $this->$varname =& $elt['db'];
91 if ($elt['type'] == DB_TYPE_RW) {
92 $this->my_db_rw =& $elt['db'];
93 } else if ($elt['type'] == DB_TYPE_RO) {
94 $this->my_db_ro =& $elt['db'];
98 if (isset($this->my_db_ro) == false && $this->my_db_rw) {
99 $this->my_db_ro =& $this->my_db_rw;
102 $c =& $backend->getController();
104 // Ethna_AppManager¥ª¥Ö¥¸¥§¥¯¥È¤ÎÀßÄê
105 $manager_list = $c->getManagerList();
106 foreach ($manager_list as $k => $v) {
107 $this->$k = $backend->getManager($v);
110 // ¥ª¥Ö¥¸¥§¥¯¥È¤Î¥×¥é¥¤¥Þ¥ê¥¡¼ÄêµÁ¹½ÃÛ
111 foreach ($this->prop_def as $k => $v) {
112 if ($v['primary'] == false) {
115 if (is_null($this->id_def)) {
117 } else if (is_array($this->id_def)) {
118 $this->id_def[] = $k;
120 $this->id_def = array($this->id_def, $k);
125 if (is_null($this->my_db_ro)) {
126 return Ethna::raiseError("Ethna_AppObject¤òÍøÍѤ¹¤ë¤Ë¤Ï¥Ç¡¼¥¿¥Ù¡¼¥¹ÀßÄ꤬ɬÍפǤ¹", E_DB_NODSN);
127 } else if (Ethna::isError($db_list)) {
131 // ¥¡¼ÂÅÅöÀ¥Á¥§¥Ã¥¯
132 if (is_null($key_type) && is_null($key) && is_null($prop)) {
133 // perhaps for adding object
138 if (is_null($prop)) {
139 $this->_setPropByDB($key_type, $key);
141 $this->_setPropByValue($prop);
144 $this->prop_backup = $this->prop;
146 if (is_array($this->id_def)) {
148 foreach ($this->id_def as $k) {
149 $this->id[] = $this->prop[$k];
152 $this->id = $this->prop[$this->id_def];
159 * ͸ú¤Ê¥ª¥Ö¥¸¥§¥¯¥È¤«¤É¤¦¤«¤òÊÖ¤¹
162 * @return bool true:͸ú false:̵¸ú
166 return is_null($this->id) ? false : true;
170 * ¥¢¥¯¥Æ¥£¥Ö¤Ê¥ª¥Ö¥¸¥§¥¯¥È¤«¤É¤¦¤«¤òÊÖ¤¹
172 * isValid()¥á¥½¥Ã¥É¤Ï¥ª¥Ö¥¸¥§¥¯¥È¼«ÂΤ¬Í¸ú¤«¤É¤¦¤«¤òȽÄꤹ¤ë¤Î¤ËÂФ·
173 * isActive()¤Ï¥ª¥Ö¥¸¥§¥¯¥È¤¬¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤È¤·¤Æ͸ú¤«¤É¤¦¤«¤òÊÖ¤¹
176 * @return bool true:¥¢¥¯¥Æ¥£¥Ö false:Èó¥¢¥¯¥Æ¥£¥Ö
180 if ($this->isValid() == false) {
183 return $this->prop['state'] == OBJECT_STATE_ACTIVE ? true : false;
187 * ¥ª¥Ö¥¸¥§¥¯¥È¤Î¥×¥í¥Ñ¥Æ¥£ÄêµÁ¤òÊÖ¤¹
190 * @return array ¥ª¥Ö¥¸¥§¥¯¥È¤Î¥×¥í¥Ñ¥Æ¥£ÄêµÁ
194 return $this->prop_def;
198 * ¥×¥é¥¤¥Þ¥ê¥¡¼ÄêµÁ¤òÊÖ¤¹
201 * @return mixed ¥×¥é¥¤¥Þ¥ê¥¡¼¤È¤Ê¤ë¥×¥í¥Ñ¥Æ¥£Ì¾
205 return $this->id_def;
209 * ¥ª¥Ö¥¸¥§¥¯¥ÈID¤òÊÖ¤¹
212 * @return mixed ¥ª¥Ö¥¸¥§¥¯¥ÈID
220 * ¥ª¥Ö¥¸¥§¥¯¥È¥×¥í¥Ñ¥Æ¥£¤Ø¤Î¥¢¥¯¥»¥µ(R)
223 * @param string $key ¥×¥í¥Ñ¥Æ¥£Ì¾
224 * @return mixed ¥×¥í¥Ñ¥Æ¥£
228 if (isset($this->prop_def[$key]) == false) {
229 trigger_error(sprintf("Unknown property [%s]", $key), E_USER_ERROR);
232 if (isset($this->prop[$key])) {
233 return $this->prop[$key];
239 * ¥ª¥Ö¥¸¥§¥¯¥È¥×¥í¥Ñ¥Æ¥£É½¼¨Ì¾¤Ø¤Î¥¢¥¯¥»¥µ
242 * @param string $key ¥×¥í¥Ñ¥Æ¥£Ì¾
243 * @return string ¥×¥í¥Ñ¥Æ¥£¤Îɽ¼¨Ì¾
245 function getName($key)
247 return $this->get($key);
251 * ¥ª¥Ö¥¸¥§¥¯¥È¥×¥í¥Ñ¥Æ¥£É½¼¨Ì¾(¾ÜºÙ)¤Ø¤Î¥¢¥¯¥»¥µ
254 * @param string $key ¥×¥í¥Ñ¥Æ¥£Ì¾
255 * @return string ¥×¥í¥Ñ¥Æ¥£¤Îɽ¼¨Ì¾(¾ÜºÙ)
257 function getLongName($key)
259 return $this->get($key);
263 * ¥×¥í¥Ñ¥Æ¥£É½¼¨Ì¾¤ò³ÊǼ¤·¤¿Ï¢ÁÛÇÛÎó¤ò¼èÆÀ¤¹¤ë
266 * @return array ¥×¥í¥Ñ¥Æ¥£É½¼¨Ì¾¤ò³ÊǼ¤·¤¿Ï¢ÁÛÇÛÎó
268 function getNameObject()
272 foreach ($this->prop_def as $key => $elt) {
273 $object[$elt['form_name']] = $this->getName($key);
280 * ¥ª¥Ö¥¸¥§¥¯¥È¥×¥í¥Ñ¥Æ¥£¤Ø¤Î¥¢¥¯¥»¥µ(W)
283 * @param string $key ¥×¥í¥Ñ¥Æ¥£Ì¾
284 * @param string $value ¥×¥í¥Ñ¥Æ¥£ÃÍ
286 function set($key, $value)
288 if (isset($this->prop_def[$key]) == false) {
289 trigger_error(sprintf("Unknown property [%s]", $key), E_USER_ERROR);
292 $this->prop[$key] = $value;
296 * ¥ª¥Ö¥¸¥§¥¯¥È¥×¥í¥Ñ¥Æ¥£¤ò»ØÄê¤Î·Á¼°¤Ç¥À¥ó¥×¤¹¤ë(¸½ºß¤ÏCSV·Á¼°¤Î¤ß¥µ¥Ý¡¼¥È)
299 * @param string $type ¥À¥ó¥×·Á¼°("csv"...)
300 * @return string ¥À¥ó¥×·ë²Ì(¥¨¥é¡¼¤Î¾ì¹ç¤Ïnull)
302 function dump($type = "csv")
304 $method = "_dump_$type";
305 if (method_exists($this, $method) == false) {
306 return Ethna::raiseError("¥á¥½¥Ã¥É̤ÄêµÁ[%s]", E_APP_NOMETHOD, $method);
309 return $this->$method();
313 * ¥Õ¥©¡¼¥àÃͤ«¤é¥ª¥Ö¥¸¥§¥¯¥È¥×¥í¥Ñ¥Æ¥£¤ò¥¤¥ó¥Ý¡¼¥È¤¹¤ë
316 * @param int $option ¥¤¥ó¥Ý¡¼¥È¥ª¥×¥·¥ç¥ó(OBJECT_IMPORT_IGNORE_NULL,...)
318 function importForm($option = null)
320 foreach ($this->getDef() as $k => $def) {
321 $value = $this->af->get($def['form_name']);
322 if (is_null($value)) {
323 // ¥Õ¥©¡¼¥à¤«¤éÃͤ¬Á÷¿®¤µ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Î¿¶Éñ¤¤
324 if ($option == OBJECT_IMPORT_IGNORE_NULL) {
327 } else if ($option == OBJECT_IMPORT_CONVERT_NULL) {
332 $this->set($k, $value);
337 * ¥ª¥Ö¥¸¥§¥¯¥È¥×¥í¥Ñ¥Æ¥£¤ò¥Õ¥©¡¼¥àÃͤ˥¨¥¯¥¹¥Ý¡¼¥È¤¹¤ë
341 function exportForm()
343 foreach ($this->getDef() as $k => $def) {
344 $this->af->set($def['form_name'], $this->get($k));
349 * ¥ª¥Ö¥¸¥§¥¯¥È¤òÄɲ乤ë
352 * @return mixed 0:Àµ¾ï½ªÎ» Ethna_Error:¥¨¥é¡¼
353 * @todo MySQL°Ê³°¤Î¥·¡¼¥±¥ó¥¹·¿(Ū¤Ê¤â¤Î)Âбþ
357 $sql = $this->_getSQL_Add();
358 for ($i = 0; $i < 4; $i++) {
359 $r =& $this->my_db_rw->query($sql);
360 if (Ethna::isError($r)) {
361 if ($r->getCode() == E_DB_DUPENT) {
362 // ½ÅÊ£¥¨¥é¡¼¥¡¼¤ÎȽÊÌ
363 $duplicate_key_list = $this->_getDuplicateKeyList();
364 if (Ethna::isError($duplicate_key_list)) {
365 return $duplicate_key_list;
367 if (is_array($duplicate_key_list) && count($duplicate_key_list) > 0) {
368 foreach ($duplicate_key_list as $k) {
369 return Ethna::raiseNotice('½ÅÊ£¥¨¥é¡¼[%s]', E_APP_DUPENT, $k);
381 return Ethna::raiseError('½ÅÊ£¥¨¥é¡¼¥¡¼È½ÊÌ¥¨¥é¡¼', E_GENERAL);
384 $this->prop_backup = $this->prop;
386 // ID¤Î¼èÆÀ(MySQL¤Î¤ßÂбþ)
387 if ($this->my_db_rw->getType() == 'mysql') {
388 // MySQL¤ÎAUTO_INCREMENT¤Ï¥Æ¡¼¥Ö¥ë¤Ë1¥«¥é¥à¤Ç³î¤ÄPRIMARY KEY
389 foreach (to_array($this->id_def) as $id_def) {
390 if ($this->prop_def[$id_def]['seq']) {
391 $this->prop[$id_def] = $this->my_db_rw->getInsertId();
398 if (is_array($this->id_def)) {
400 foreach ($this->id_def as $k) {
401 $this->id[] = $this->prop[$k];
404 $this->id = $this->prop[$this->id_def];
407 $this->prop_backup = $this->prop;
413 * ¥ª¥Ö¥¸¥§¥¯¥È¤ò¹¹¿·¤¹¤ë
416 * @return mixed 0:Àµ¾ï½ªÎ» Ethna_Error:¥¨¥é¡¼
420 $sql = $this->_getSQL_Update();
421 for ($i = 0; $i < 4; $i++) {
422 $r =& $this->my_db_rw->query($sql);
423 if (Ethna::isError($r)) {
424 if ($r->getCode() == E_DB_DUPENT) {
425 // ½ÅÊ£¥¨¥é¡¼¥¡¼¤ÎȽÊÌ
426 $duplicate_key_list = $this->_getDuplicateKeyList();
427 if (Ethna::isError($duplicate_key_list)) {
428 return $duplicate_key_list;
430 if (is_array($duplicate_key_list) && count($duplicate_key_list) > 0) {
431 foreach ($duplicate_key_list as $k) {
432 return Ethna::raiseNotice('½ÅÊ£¥¨¥é¡¼[%s]', E_APP_DUPENT, $k);
444 return Ethna::raiseError('½ÅÊ£¥¨¥é¡¼¥¡¼È½ÊÌ¥¨¥é¡¼', E_GENERAL);
447 $affected_rows = $this->my_db_rw->affectedRows();
448 if ($affected_rows <= 0) {
449 $this->backend->log(LOG_DEBUG, "update query with 0 updated rows");
452 // ¥Ð¥Ã¥¯¥¢¥Ã¥×/¥¥ã¥Ã¥·¥å¹¹¿·
453 $this->prop_backup = $this->prop;
454 $this->_clearPropCache();
460 * ¥ª¥Ö¥¸¥§¥¯¥È¤òÃÖ´¹¤¹¤ë
462 * MySQL¤ÎREPLACEʸ¤ËÁêÅö¤¹¤ëÆ°ºî¤ò¹Ô¤¦(add()¤Ç½ÅÊ£¥¨¥é¡¼¤¬È¯À¸¤·¤¿¤é
466 * @return mixed 0:Àµ¾ï½ªÎ» >0:¥ª¥Ö¥¸¥§¥¯¥ÈID(Äɲûþ) Ethna_Error:¥¨¥é¡¼
470 $sql = $this->_getSQL_Select($this->getIdDef(), $this->getId());
472 for ($i = 0; $i < 3; $i++) {
473 $r = $this->my_db_rw->query($sql);
474 if (Ethna::isError($r)) {
480 $r = $this->update();
484 if (Ethna::isError($r) == false) {
486 } else if ($r->getCode() != E_APP_DUPENT) {
496 * ¥ª¥Ö¥¸¥§¥¯¥È¤òºï½ü¤¹¤ë
499 * @return mixed 0:Àµ¾ï½ªÎ» Ethna_Error:¥¨¥é¡¼
503 $sql = $this->_getSQL_Remove();
504 $r =& $this->my_db_rw->query($sql);
505 if (Ethna::isError($r)) {
509 // ¥×¥í¥Ñ¥Æ¥£/¥Ð¥Ã¥¯¥¢¥Ã¥×/¥¥ã¥Ã¥·¥å¥¯¥ê¥¢
510 $this->id = $this->prop = $this->prop_backup = null;
511 $this->_clearPropCache();
517 * ¥ª¥Ö¥¸¥§¥¯¥ÈID¤ò¸¡º÷¤¹¤ë
520 * @param array $filter ¸¡º÷¾ò·ï
521 * @param array $order ¸¡º÷·ë²Ì¥½¡¼¥È¾ò·ï
522 * @param int $offset ¸¡º÷·ë²Ì¼èÆÀ¥ª¥Õ¥»¥Ã¥È
523 * @param int $count ¸¡º÷·ë²Ì¼èÆÀ¿ô
524 * @return mixed array(0 => ¸¡º÷¾ò·ï¤Ë¥Þ¥Ã¥Á¤·¤¿·ï¿ô, 1 => $offset, $count¤Ë¤è¤ê»ØÄꤵ¤ì¤¿·ï¿ô¤Î¥ª¥Ö¥¸¥§¥¯¥ÈID°ìÍ÷) Ethna_Error:¥¨¥é¡¼
526 function searchId($filter = null, $order = null, $offset = null, $count = null)
528 if (is_null($filter) == false) {
529 $sql = $this->_getSQL_SearchLength($filter);
530 $r =& $this->my_db_ro->query($sql);
531 if (Ethna::isError($r)) {
534 $row = $r->fetchRow(DB_FETCHMODE_ASSOC);
535 $length = $row['id_count'];
541 $sql = $this->_getSQL_SearchId($filter, $order, $offset, $count);
542 $r =& $this->my_db_ro->query($sql);
543 if (Ethna::isError($r)) {
547 for ($i = 0; $i < $n; $i++) {
548 $row = $r->fetchRow(DB_FETCHMODE_ASSOC);
550 // ¥×¥é¥¤¥Þ¥ê¥¡¼¤¬1¥«¥é¥à¤Ê¤é¥¹¥«¥é¡¼ÃͤËÊÑ´¹
551 if (is_array($this->id_def) == false) {
552 $row = $row[$this->id_def];
556 if (is_null($length)) {
557 $length = count($id_list);
560 return array($length, $id_list);
564 * ¥ª¥Ö¥¸¥§¥¯¥È¥×¥í¥Ñ¥Æ¥£¤ò¸¡º÷¤¹¤ë
567 * @param array $keys ¼èÆÀ¤¹¤ë¥×¥í¥Ñ¥Æ¥£
568 * @param array $filter ¸¡º÷¾ò·ï
569 * @param array $order ¸¡º÷·ë²Ì¥½¡¼¥È¾ò·ï
570 * @param int $offset ¸¡º÷·ë²Ì¼èÆÀ¥ª¥Õ¥»¥Ã¥È
571 * @param int $count ¸¡º÷·ë²Ì¼èÆÀ¿ô
572 * @return mixed array(0 => ¸¡º÷¾ò·ï¤Ë¥Þ¥Ã¥Á¤·¤¿·ï¿ô, 1 => $offset, $count¤Ë¤è¤ê»ØÄꤵ¤ì¤¿·ï¿ô¤Î¥ª¥Ö¥¸¥§¥¯¥È¥×¥í¥Ñ¥Æ¥£°ìÍ÷) Ethna_Error:¥¨¥é¡¼
574 function searchProp($keys = null, $filter = null, $order = null, $offset = null, $count = null)
576 if (is_null($filter) == false) {
577 $sql = $this->_getSQL_SearchLength($filter);
578 $r =& $this->my_db_ro->query($sql);
579 if (Ethna::isError($r)) {
582 $row = $r->fetchRow(DB_FETCHMODE_ASSOC);
583 $length = $row['id_count'];
588 $prop_list = array();
589 $sql = $this->_getSQL_SearchProp($keys, $filter, $order, $offset, $count);
590 $r =& $this->my_db_ro->query($sql);
591 if (Ethna::isError($r)) {
595 for ($i = 0; $i < $n; $i++) {
596 $row = $r->fetchRow(DB_FETCHMODE_ASSOC);
599 if (is_null($length)) {
600 $length = count($prop_list);
603 return array($length, $prop_list);
607 * ¥ª¥Ö¥¸¥§¥¯¥È¤Î¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¥Ç¥Õ¥©¥ë¥È¥×¥í¥Ñ¥Æ¥£¤òÀßÄꤹ¤ë
609 * ¥³¥ó¥¹¥È¥é¥¯¥¿¤Ë¤è¤ê»ØÄꤵ¤ì¤¿¥¡¼¤Ë¥Þ¥Ã¥Á¤¹¤ë¥¨¥ó¥È¥ê¤¬¤Ê¤«¤Ã¤¿¾ì¹ç¤Î
610 * ¥Ç¥Õ¥©¥ë¥È¥×¥í¥Ñ¥Æ¥£¤ò¤³¤³¤ÇÀßÄꤹ¤ë¤³¤È¤¬½ÐÍè¤ë
613 * @param mixed $key_type ¸¡º÷¥¡¼Ì¾
614 * @param mixed $key ¸¡º÷¥¡¼
615 * @return int 0:Àµ¾ï½ªÎ»
617 function _setDefault($key_type, $key)
623 * ¥ª¥Ö¥¸¥§¥¯¥È¥×¥í¥Ñ¥Æ¥£¤òDB¤«¤é¼èÆÀ¤¹¤ë
626 * @param mixed $key_type ¸¡º÷¥¡¼Ì¾
627 * @param mixed $key ¸¡º÷¥¡¼
629 function _setPropByDB($key_type, $key)
631 global $_ETHNA_APP_OBJECT_CACHE;
633 $key_type = to_array($key_type);
634 $key = to_array($key);
635 if (count($key_type) != count($key)) {
636 trigger_error(sprintf("Unmatched key_type & key length [%d-%d]", count($key_type), count($key)), E_USER_ERROR);
639 foreach ($key_type as $elt) {
640 if (isset($this->prop_def[$elt]) == false) {
641 trigger_error("Invalid key_type [$elt]", E_USER_ERROR);
646 // ¥¥ã¥Ã¥·¥å¥Á¥§¥Ã¥¯
647 $class_name = strtolower(get_class($this));
648 if (is_array($_ETHNA_APP_OBJECT_CACHE) == false || array_key_exists($class_name, $_ETHNA_APP_OBJECT_CACHE) == false) {
649 $_ETHNA_APP_OBJECT_CACHE[$class_name] = array();
651 $cache_key = serialize(array($key_type, $key));
652 if (array_key_exists($cache_key, $_ETHNA_APP_OBJECT_CACHE[$class_name])) {
653 $this->prop = $_ETHNA_APP_OBJECT_CACHE[$class_name][$cache_key];
658 $sql = $this->_getSQL_Select($key_type, $key);
661 $r =& $this->my_db_ro->query($sql);
662 if (Ethna::isError($r)) {
668 if ($this->_setDefault($key_type, $key) == false) {
673 trigger_error("Invalid key (multiple rows found) [$key]", E_USER_ERROR);
676 $this->prop = $r->fetchRow(DB_FETCHMODE_ASSOC);
678 // ¥¥ã¥Ã¥·¥å¥¢¥Ã¥×¥Ç¡¼¥È
679 $_ETHNA_APP_OBJECT_CACHE[$class_name][$cache_key] = $this->prop;
683 * ¥³¥ó¥¹¥È¥é¥¯¥¿¤Ç»ØÄꤵ¤ì¤¿¥×¥í¥Ñ¥Æ¥£¤òÀßÄꤹ¤ë
686 * @param array $prop ¥×¥í¥Ñ¥Æ¥£°ìÍ÷
688 function _setPropByValue($prop)
690 $def = $this->getDef();
691 foreach ($def as $key => $value) {
692 if ($value['primary'] && isset($prop[$key]) == false) {
693 // ¥×¥é¥¤¥Þ¥ê¥¡¼¤Ï¾ÊάÉÔ²Ä
694 trigger_error("primary key is not identical", E_USER_ERROR);
696 $this->prop[$key] = $prop[$key];
701 * ¥ª¥Ö¥¸¥§¥¯¥È¤Î¥×¥é¥¤¥Þ¥ê¥Æ¡¼¥Ö¥ë¤ò¼èÆÀ¤¹¤ë
704 * @return string ¥ª¥Ö¥¸¥§¥¯¥È¤Î¥×¥é¥¤¥Þ¥ê¥Æ¡¼¥Ö¥ë̾
706 function _getPrimaryTable()
708 $tables = array_keys($this->table_def);
718 * @return mixed 0:½ÅÊ£¤Ê¤· Ethna_Error:¥¨¥é¡¼ array:½ÅÊ£¥¡¼¤Î¥×¥í¥Ñ¥Æ¥£Ì¾°ìÍ÷
720 function _getDuplicateKeyList()
722 $duplicate_key_list = array();
724 // ¸½ºßÀßÄꤵ¤ì¤Æ¤¤¤ë¥×¥é¥¤¥Þ¥ê¥¡¼¤ËNULL¤¬´Þ¤Þ¤ì¤ë¾ì¹ç¤Ï¸¡º÷¤·¤Ê¤¤
726 foreach (to_array($this->id_def) as $k) {
727 if (isset($this->prop[$k]) == false || is_null($this->prop[$k])) {
733 // ¥×¥é¥¤¥Þ¥ê¥¡¼¤Ïmulti columns¤Ë¤Ê¤êÆÀ¤ë¤Î¤ÇÊÌ°·¤¤
735 $sql = $this->_getSQL_Duplicate($this->id_def);
736 $r =& $this->my_db_rw->query($sql);
737 if (Ethna::isError($r)) {
739 } else if ($r->numRows() > 0) {
740 $duplicate_key_list = to_array($this->id_def); // we can overwrite $key_list here
745 foreach ($this->prop_def as $k => $v) {
746 if ($v['primary'] == true || $v['key'] == false) {
749 $sql = $this->_getSQL_Duplicate($k);
750 $r =& $this->my_db_rw->query($sql);
751 if (Ethna::isError($r)) {
753 } else if ($r->NumRows() > 0) {
754 $duplicate_key_list[] = $k;
758 if (count($duplicate_key_list) > 0) {
759 return $duplicate_key_list;
766 * ¥ª¥Ö¥¸¥§¥¯¥È¥×¥í¥Ñ¥Æ¥£¤ò¼èÆÀ¤¹¤ëSQLʸ¤ò¹½ÃÛ¤¹¤ë
769 * @param array $key_type ¥¡¼¤È¤Ê¤ë¥×¥í¥Ñ¥Æ¥£Ì¾°ìÍ÷
770 * @param array $key $key_type¤ËÂбþ¤¹¤ë¥¡¼°ìÍ÷
771 * @return string SELECTʸ
773 function _getSQL_Select($key_type, $key)
775 $key_type = to_array($key_type);
779 for ($i = 0; $i < count($key_type); $i++) {
783 $key = to_array($key);
787 Ethna_AppSQL::escapeSQL($key);
789 $tables = implode(',', array_keys($this->table_def));
790 $columns = implode(',', array_keys($this->prop_def));
794 for ($i = 0; $i < count($key_type); $i++) {
795 if (is_null($condition)) {
796 $condition = "WHERE ";
798 $condition .= " AND ";
800 $condition .= Ethna_AppSQL::getCondition($key_type[$i], $key[$i]);
803 $sql = "SELECT $columns FROM $tables $condition";
809 * ¥ª¥Ö¥¸¥§¥¯¥È¤ÈÄɲ乤ëSQLʸ¤ò¹½ÃÛ¤¹¤ë
812 * @return string ¥ª¥Ö¥¸¥§¥¯¥È¤òÄɲ乤뤿¤á¤ÎINSERTʸ
814 function _getSQL_Add()
816 $tables = implode(',', array_keys($this->table_def));
820 $prop_arg_list = $this->prop;
821 Ethna_AppSQL::escapeSQL($prop_arg_list);
822 foreach ($this->prop_def as $k => $v) {
823 if (isset($prop_arg_list[$k]) == false) {
826 if ($set_list != "") {
829 $set_list .= sprintf("%s=%s", $k, $prop_arg_list[$k]);
832 $sql = "INSERT INTO $tables SET $set_list";
838 * ¥ª¥Ö¥¸¥§¥¯¥È¥×¥í¥Ñ¥Æ¥£¤ò¹¹¿·¤¹¤ëSQLʸ¤ò¹½ÃÛ¤¹¤ë
841 * @return ¥ª¥Ö¥¸¥§¥¯¥È¥×¥í¥Ñ¥Æ¥£¤ò¹¹¿·¤¹¤ë¤¿¤á¤ÎUPDATEʸ
843 function _getSQL_Update()
845 $tables = implode(',', array_keys($this->table_def));
849 $prop_arg_list = $this->prop;
850 Ethna_AppSQL::escapeSQL($prop_arg_list);
851 foreach ($this->prop_def as $k => $v) {
852 if ($set_list != "") {
855 $set_list .= sprintf("%s=%s", $k, $prop_arg_list[$k]);
858 // ¸¡º÷¾ò·ï(primary key)
860 foreach (to_array($this->id_def) as $k) {
861 if (is_null($condition)) {
862 $condition = "WHERE ";
864 $condition .= " AND ";
866 $v = $this->prop_backup[$k]; // equals to $this->id
867 Ethna_AppSQL::escapeSQL($v);
868 $condition .= Ethna_AppSQL::getCondition($k, $v);
871 $sql = "UPDATE $tables SET $set_list $condition";
877 * ¥ª¥Ö¥¸¥§¥¯¥È¤òºï½ü¤¹¤ëSQLʸ¤ò¹½ÃÛ¤¹¤ë
880 * @return string ¥ª¥Ö¥¸¥§¥¯¥È¤òºï½ü¤¹¤ë¤¿¤á¤ÎDELETEʸ
882 function _getSQL_Remove()
884 $tables = implode(',', array_keys($this->table_def));
886 // ¸¡º÷¾ò·ï(primary key)
888 foreach (to_array($this->id_def) as $k) {
889 if (is_null($condition)) {
890 $condition = "WHERE ";
892 $condition = " AND ";
894 $v = $this->prop_backup[$k]; // equals to $this->id
895 Ethna_AppSQL::escapeSQL($v);
896 $condition .= Ethna_AppSQL::getCondition($k, $v);
898 if (is_null($condition)) {
899 trigger_error("DELETE with no conditon", E_USER_ERROR);
903 $sql = "DELETE FROM $tables $condition";
909 * ¥ª¥Ö¥¸¥§¥¯¥È¥×¥í¥Ñ¥Æ¥£¤Î¥æ¥Ë¡¼¥¯¥Á¥§¥Ã¥¯¤ò¹Ô¤¦SQLʸ¤ò¹½ÃÛ¤¹¤ë
912 * @param mixed $key ¥æ¥Ë¡¼¥¯¥Á¥§¥Ã¥¯¤ò¹Ô¤¦¥×¥í¥Ñ¥Æ¥£Ì¾
913 * @return string ¥æ¥Ë¡¼¥¯¥Á¥§¥Ã¥¯¤ò¹Ô¤¦¤¿¤á¤ÎSELECTʸ
915 function _getSQL_Duplicate($key)
917 $tables = implode(',', array_keys($this->table_def));
918 $columns = implode(',', array_keys($this->prop_def)); // any column will do
921 // ¸¡º÷¾ò·ï(¸½ºßÀßÄꤵ¤ì¤Æ¤¤¤ë¥×¥é¥¤¥Þ¥ê¥¡¼¤Ï¸¡º÷Âоݤ«¤é½ü¤¯)
922 if (is_null($this->id) == false) {
923 $primary_value = to_array($this->getId());
925 foreach (to_array($this->id_def) as $k) {
926 if (is_null($condition)) {
927 $condition = "WHERE ";
929 $condition .= " AND ";
931 $value = $primary_value[$n];
932 Ethna_AppSQL::escapeSQL($value);
933 $condition .= Ethna_AppSQL::getCondition($k, $value, OBJECT_CONDITION_NE);
938 foreach (to_array($key) as $k) {
939 if (is_null($condition)) {
940 $condition = "WHERE ";
942 $condition .= " AND ";
944 $v = $this->prop[$k];
945 Ethna_AppSQL::escapeSQL($v);
946 $condition .= Ethna_AppSQL::getCondition($k, $v);
949 $sql = "SELECT $columns FROM $tables $condition";
955 * ¥ª¥Ö¥¸¥§¥¯¥È¸¡º÷Áí¿ô(offset, count½ü³°)¤ò¼èÆÀ¤¹¤ëSQLʸ¤ò¹½ÃÛ¤¹¤ë
958 * @param array $filter ¸¡º÷¾ò·ï
959 * @return string ¸¡º÷Áí¿ô¤ò¼èÆÀ¤¹¤ë¤¿¤á¤ÎSELECTʸ
961 function _getSQL_SearchLength($filter)
964 $tables = implode(',', array_keys($this->table_def));
965 if ($this->_isAdditionalField($filter)) {
966 $tables .= " " . $this->_SQLPlugin_SearchTable();
969 $id_def = to_array($this->id_def);
970 $column_id = $this->_getPrimaryTable() . "." . $id_def[0]; // any id columns will do
972 $condition = $this->_getSQL_SearchCondition($filter);
973 $sql = "SELECT COUNT(DISTINCT $column_id) AS id_count FROM $tables $condition";
979 * ¥ª¥Ö¥¸¥§¥¯¥ÈID¸¡º÷¤ò¹Ô¤¦SQLʸ¤ò¹½ÃÛ¤¹¤ë
982 * @param array $filter ¸¡º÷¾ò·ï
983 * @param array $order ¸¡º÷·ë²Ì¥½¡¼¥È¾ò·ï
984 * @param int $offset ¸¡º÷·ë²Ì¼èÆÀ¥ª¥Õ¥»¥Ã¥È
985 * @param int $count ¸¡º÷·ë²Ì¼èÆÀ¿ô
986 * @return string ¥ª¥Ö¥¸¥§¥¯¥È¸¡º÷¤ò¹Ô¤¦SELECTʸ
988 function _getSQL_SearchId($filter, $order, $offset, $count)
991 $tables = implode(',', array_keys($this->table_def));
992 if ($this->_isAdditionalField($filter) || $this->_isAdditionalField($order)) {
993 $tables .= " " . $this->_SQLPlugin_SearchTable();
997 foreach (to_array($this->id_def) as $id) {
998 if ($column_id != "") {
1001 $column_id .= $this->_getPrimaryTable() . "." . $id;
1003 $condition = $this->_getSQL_SearchCondition($filter);
1006 if (is_array($order)) {
1007 foreach ($order as $k => $v) {
1009 $sort = "ORDER BY ";
1013 $sort .= sprintf("%s %s", $k, $v == OBJECT_SORT_ASC ? "ASC" : "DESC");
1018 if (is_null($count) == false) {
1020 if (is_null($offset) == false) {
1021 $limit .= sprintf("%d,", $offset);
1023 $limit .= sprintf("%d", $count);
1026 $sql = "SELECT DISTINCT $column_id FROM $tables $condition $sort $limit";
1032 * ¥ª¥Ö¥¸¥§¥¯¥È¥×¥í¥Ñ¥Æ¥£¸¡º÷¤ò¹Ô¤¦SQLʸ¤ò¹½ÃÛ¤¹¤ë
1035 * @param array $keys ¼èÆÀ¥×¥í¥Ñ¥Æ¥£°ìÍ÷
1036 * @param array $filter ¸¡º÷¾ò·ï
1037 * @param array $order ¸¡º÷·ë²Ì¥½¡¼¥È¾ò·ï
1038 * @param int $offset ¸¡º÷·ë²Ì¼èÆÀ¥ª¥Õ¥»¥Ã¥È
1039 * @param int $count ¸¡º÷·ë²Ì¼èÆÀ¿ô
1040 * @return string ¥ª¥Ö¥¸¥§¥¯¥È¸¡º÷¤ò¹Ô¤¦SELECTʸ
1042 function _getSQL_SearchProp($keys, $filter, $order, $offset, $count)
1045 $tables = implode(',', array_keys($this->table_def));
1046 if ($this->_isAdditionalField($filter) || $this->_isAdditionalField($order)) {
1047 $tables .= " " . $this->_SQLPlugin_SearchTable();
1049 $p_table = $this->_getPrimaryTable();
1051 // ¸¡º÷ÍÑÄɲåץí¥Ñ¥Æ¥£
1052 if ($this->_isAdditionalField($filter) || $this->_isAdditionalField($order)) {
1053 $search_prop_def = $this->_SQLPlugin_SearchPropDef();
1055 $search_prop_def = array();
1057 $def = array_merge($this->getDef(), $search_prop_def);
1062 if (is_null($keys)) {
1063 $keys = array_keys($def);
1065 foreach (to_array($keys) as $key) {
1066 if (isset($def[$key]) == false) {
1069 if ($column != "") {
1072 $t = isset($def[$key]['table']) ? $def[$key]['table'] : $p_table;
1073 $column .= sprintf("%s.%s", $t, $key);
1075 // ¥Õ¥£¡¼¥ë¥É¤¬¥×¥é¥¤¥Þ¥ê¡¼¥¡¼¤Ê¤éGROUP BY¤¹¤ë
1076 if ((isset($def[$key]['table']) && $def[$key]['table'] == $p_table) ||
1077 isset($def[$key]['table']) == false) {
1078 if ($def[$key]['primary']) {
1079 if ($group_by != "") {
1082 $group_by .= "GROUP BY";
1084 $group_by .= " $column";
1089 $condition = $this->_getSQL_SearchCondition($filter);
1092 if (is_array($order)) {
1093 foreach ($order as $k => $v) {
1095 $sort = "ORDER BY ";
1099 $sort .= sprintf("%s %s", $k, $v == OBJECT_SORT_ASC ? "ASC" : "DESC");
1104 if (is_null($count) == false) {
1106 if (is_null($offset) == false) {
1107 $limit .= sprintf("%d,", $offset);
1109 $limit .= sprintf("%d", $count);
1112 $sql = "SELECT $column FROM $tables $condition $group_by $sort $limit";
1118 * ¥ª¥Ö¥¸¥§¥¯¥È¸¡º÷SQL¤Î¾ò·ïʸ¤ò¹½ÃÛ¤¹¤ë
1121 * @param array $filter ¸¡º÷¾ò·ï
1122 * @return string ¥ª¥Ö¥¸¥§¥¯¥È¸¡º÷¤Î¾ò·ïʸ(¥¨¥é¡¼¤Ê¤énull)
1124 function _getSQL_SearchCondition($filter)
1126 if (is_array($filter) == false) {
1130 $p_table = $this->_getPrimaryTable();
1132 // ¸¡º÷ÍÑÄɲåץí¥Ñ¥Æ¥£
1133 if ($this->_isAdditionalField($filter)) {
1134 $search_prop_def = $this->_SQLPlugin_SearchPropDef();
1136 $search_prop_def = array();
1138 $prop_def = array_merge($this->prop_def, $search_prop_def);
1141 foreach ($filter as $k => $v) {
1142 if (isset($prop_def[$k]) == false) {
1143 trigger_error(sprintf("Unknown property [%s]", $k), E_USER_ERROR);
1147 if (is_null($condition)) {
1148 $condition = "WHERE ";
1150 $condition .= " AND ";
1153 $t = isset($prop_def[$k]['table']) ? $prop_def[$k]['table'] : $p_table;
1155 if (is_object($v)) {
1156 // Ethna_AppSearchObject¤¬»ØÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç
1157 $condition .= $v->toString("$t.$k");
1158 } else if (is_array($v) && count($v) > 0 && is_object($v[0])) {
1159 // Ethna_AppSearchObject¤¬ÇÛÎó¤Ç»ØÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç
1161 foreach ($v as $so) {
1163 $condition .= " AND ";
1165 $condition .= $so->toStrong("$t.$k");
1168 } else if ($prop_def[$k]['type'] == VAR_TYPE_STRING) {
1170 Ethna_AppSQL::escapeSQL($v);
1171 $condition .= Ethna_AppSQL::getCondition("$t.$k", $v, OBJECT_CONDITION_LIKE);
1174 Ethna_AppSQL::escapeSQL($v);
1175 $condition .= Ethna_AppSQL::getCondition("$t.$k", $v, OBJECT_CONDITION_EQ);
1183 * ¥ª¥Ö¥¸¥§¥¯¥È¸¡º÷SQL¥×¥é¥°¥¤¥ó(Äɲåơ¼¥Ö¥ë)
1187 * return " LEFT JOIN bar_tbl ON foo_tbl.user_id=bar_tbl.user_id";
1191 * @return string ¥Æ¡¼¥Ö¥ëJOIN¤ÎSQLʸ
1193 function _SQLPlugin_SearchTable()
1199 * ¥ª¥Ö¥¸¥§¥¯¥È¸¡º÷SQL¥×¥é¥°¥¤¥ó(Äɲþò·ïÄêµÁ)
1203 * $search_prop_def = array(
1204 * 'group_id' => array(
1205 * 'primary' => true, 'key' => true, 'type' => VAR_TYPE_INT,
1206 * 'form_name' => 'group_id', 'table' => 'group_user_tbl',
1209 * return $search_prop_def;
1213 * @return array Äɲþò·ïÄêµÁ
1215 function _SQLPlugin_SearchPropDef()
1220 * ¥ª¥Ö¥¸¥§¥¯¥È¥×¥í¥Ñ¥Æ¥£¤òCSV·Á¼°¤Ç¥À¥ó¥×¤¹¤ë
1223 * @return string ¥À¥ó¥×·ë²Ì
1225 function _dump_csv()
1230 foreach ($this->getDef() as $k => $def) {
1234 $dump .= Ethna_Util::escapeCSV($this->getName($k));
1242 * (¸¡º÷¾ò·ï|¥½¡¼¥È¾ò·ï)¥Õ¥£¡¼¥ë¥É¤ËÄɲåե£¡¼¥ë¥É¤¬´Þ¤Þ¤ì¤ë¤«¤É¤¦¤«¤òÊÖ¤¹
1245 * @param array $field (¸¡º÷¾ò·ï|¥½¡¼¥È¾ò·ï)ÄêµÁ
1246 * @return bool true:´Þ¤Þ¤ì¤ë false:´Þ¤Þ¤ì¤Ê¤¤
1248 function _isAdditionalField($field)
1250 if (is_array($field) == false) {
1254 $def = $this->getDef();
1255 foreach ($field as $key => $value) {
1256 if (array_key_exists($key, $def) == false) {
1264 * ¥¥ã¥Ã¥·¥å¥Ç¡¼¥¿¤òºï½ü¤¹¤ë
1268 function _clearPropCache()
1270 $class_name = strtolower(get_class($this));
1271 foreach (array('_ETHNA_APP_OBJECT_CACHE', '_ETHNA_APP_MANAGER_OL_CACHE', '_ETHNA_APP_MANAGER_OPL_CACHE', '_ETHNA_APP_MANAGER_OP_CACHE') as $key) {
1272 if (array_key_exists($key, $GLOBALS) && array_key_exists($class_name, $GLOBALS[$key])) {
1273 unset($GLOBALS[$key][$class_name]);