/** * 属性工厂 * * @param mixed $type * @param array $params * @static * @access public * @return void */ public static function operator_factory($type, $params = array()) { $db = \swan\db\sw_db::singleton(); \swan\member\sw_member::set_namespace("\\lib\\"); $operator = \swan\member\sw_member::operator_factory('member', $type, $params); $operator->set_db($db); return $operator; }
/** * 获取子类的对象 * * @param string $type * @access public * @return void */ public function get_operator($type) { if (!array_key_exists($type, $this->__operator_types)) { throw new sw_exception("Invalid operator type `{$type}`"); } $class_name = $this->__namespace . "sw_{$type}"; $operator = new $class_name($this); $operator->set_db(\swan\db\sw_db::singleton()); return $operator; }
/** * 获取图表的自增长 ID * * @param intger $graph * @param string $table_name * @param sw_db $db * @param int $init_id * @access public * @return intger */ public static function get_next_graph($graph_id, $table_name, $db = null, $init_id = 1) { if (!isset($db)) { $db = sw_db::singleton(); } //判断是否是合法的 graph_id try { $graph_id = $db->fetch_one($db->select()->from(SWAN_TBN_GRAPH_BASIC, array('graph_id'))->where('graph_id= ?'), $graph_id); } catch (sw_exception $e) { throw new sw_exception('invalid graph id, get sequence faild. '); } if (false === $graph_id) { throw new sw_exception('invalid graph id, get sequence faild. '); } try { $db->begin_transaction(); $transaction = true; } catch (\swan\exception\sw_exception $e) { $transaction = false; } try { $fields = array('sequence_id' => new sw_db_expr('sequence_id+' . self::get_increment_num())); $where = $db->quote_into('table_name = ?', $table_name); $where .= $db->quote_into(' AND graph_id = ?', $graph_id); $try_num = 1; do { $affected = $db->update(SWAN_TBN_SEQUENCE_GRAPH, $fields, $where); if (1 === $affected) { break; } //更新失败,记录不存在则添加 $attribute = array('graph_id' => $graph_id, 'table_name' => $table_name, 'sequence_id' => $init_id); try { $affected = $db->insert(SWAN_TBN_SEQUENCE_GRAPH, $attribute); } catch (sw_exception $e) { } } while ($affected !== 1 && $try_num < self::MAX_TRY_NUM); if (1 === $affected) { $id = $db->fetch_one($db->select()->from(SWAN_TBN_SEQUENCE_GRAPH, array('sequence_id' => 'sequence_id'))->where('table_name= ? AND graph_id= ?'), array($table_name, $graph_id)); } if (1 !== $affected || false === $id) { throw new sw_exception('get graph sequence faild.'); } if ($transaction) { $db->commit(); } return $id; } catch (\swan\exception\sw_exception $e) { if ($transaction) { $db->rollback(); } throw new sw_exception($e); } }
/** * 回滚事务 * * @access protected * @return void */ protected function _rollback() { $db = \swan\db\sw_db::singleton(); $db->rollback(); }
/** * 删除设备 * * @access public * @return void */ public function action_del() { $did = $this->__request->get_post('device_id', ''); if (!$did) { return $this->render_json(null, 10001, '`device_id` not allow is empty.'); } $db = \swan\db\sw_db::singleton(); $db->begin_transaction(); // 删除设备 $device = sw_member::operator_factory('device'); try { $condition = sw_member::condition_factory('del_device_key', array('device_id' => $did)); $condition->set_in('device_id'); $device->get_operator('key')->del_key($condition); } catch (\swan\exception\sw_exception $e) { $db->rollback(); return $this->render_json(null, 10002, $e->getMessage()); } try { $condition = sw_member::condition_factory('del_device_basic', array('device_id' => $did)); $condition->set_in('device_id'); $device->get_operator('basic')->del_basic($condition); } catch (\swan\exception\sw_exception $e) { $db->rollback(); return $this->render_json(null, 10003, $e->getMessage()); } // 获取核心监控适配器 $condition = sw_member::condition_factory('get_madapter_basic'); $condition->set_in('madapter_type'); $condition->set_madapter_type('1'); $madapter = sw_member::operator_factory('madapter'); $madapter_basic = $madapter->get_operator('basic')->get_basic($condition); try { $property_key = sw_member::property_factory('device_key', array('device_id' => $did)); $condition = sw_member::condition_factory('del_device_monitor'); $condition->set_in('device_id'); $device = sw_member::operator_factory('device', $property_key); $monitor = $device->get_operator('monitor')->del_monitor($condition); } catch (\swan\exception\sw_exception $e) { $db->rollback(); return $this->render_json(null, 10004, $e->getMessage()); } $db->commit(); return $this->render_json(null, 10000, 'delete device success.'); }