/** * 执行指定的动作 * * @return mixed */ function execute($action_name, array $args = array()) { $action_method = "action{$action_name}"; // 执行指定的动作方法 $this->_before_execute(); #IFDEF DBEUG QLog::log('EXECUTE ACTION: ' . get_class($this) . '::' . $action_method . '()', QLog::DEBUG); #ENDIF $this->_view['_MSG'] = $this->_app->getFlashMessage(); Q::replaceIni('OrderAvailable', true); Q::replaceIni('isAdmin', false); #dump(Q::ini('appini/managers')); if ($this->_user) { if (in_array($this->_user->user_mail, Q::ini('appini/managers'))) { Q::replaceIni('isAdmin', true); } } $this->_view['_UDI'] = QContext::instance()->requestUDI(false); $response = call_user_func_array(array($this, $action_method), $args); $this->_after_execute($response); if (is_null($response) && is_array($this->_view)) { // 如果动作没有返回值,并且 $this->view 不为 null, // 则假定动作要通过 $this->view 输出数据 $config = array('view_dir' => $this->_getViewDir()); $response = new $this->_view_class($config); $response->setViewname($this->_getViewName())->assign($this->_view); #dump($response); $this->_before_render($response); } elseif ($response instanceof $this->_view_class) { $response->assign($this->_view); $this->_before_render($response); } #dump($response); return $response; }
public static function queryKeywordWithPrefix($prefix = '', $limit = 10) { global $db; $keyword_quoted = $db->quote($prefix . "%"); $sql = "SELECT `keyword` as itemName FROM `keyword` \n\t\t\tWHERE `keyword` like {$keyword_quoted}\n\t\t\tORDER BY keyword\n\t\t\tLIMIT " . intval($limit); $keyword_list = $db->getAll($sql); QLog::log("KeywordModel::queryKeywordWithPrefix({$prefix},{$limit})->sql: " . $sql . " -> list: " . json_encode($keyword_list)); return $keyword_list; }
public static function queryYamaiWithTypeAndPrefix($which, $prefix = '', $limit = 10) { global $db; if (!in_array($which, array('yamai_type', 'yamai_system', 'yamai_name'))) { return false; } $keyword_quoted = $db->quote($prefix . "%"); $sql = "SELECT distinct `{$which}` as itemName FROM yamai \n\t\t\tWHERE `{$which}` like {$keyword_quoted} \n\t\t\tLIMIT " . intval($limit); $list = $db->getAll($sql); QLog::log("YamaiModel::queryYamaiWithTypeAndPrefix({$which},{$prefix},{$limit})->sql: " . $sql . " -> list: " . json_encode($list)); return $list; }
/** * 执行指定的动作 * * @return mixed */ function execute($action_name, array $args = array()) { $action_method = "action{$action_name}"; // 执行指定的动作方法 $this->_before_execute(); #IFDEF DBEUG QLog::log('EXECUTE ACTION: ' . get_class($this) . '::' . $action_method . '()', QLog::DEBUG); #ENDIF $response = call_user_func_array(array($this, $action_method), $args); $this->_after_execute($response); if (is_null($response) && is_array($this->_view)) { // 如果动作没有返回值,并且 $this->view 不为 null, // 则假定动作要通过 $this->view 输出数据 $config = array('view_dir' => $this->_getViewDir()); $response = new $this->_view_class($config); $response->setViewname($this->_getViewName())->assign($this->_view); } return $response; }
function actionMigrationOut() { $this->_pathway->addStep('数据迁出'); QLog::log('开始数据迁出!'); try { $db = QDB::getConn(); $db->startTrans(); $db->completeTrans(); } catch (QException $ex) { QLog::log($ex->getMessage(), QLog::ERR); } echo '正在迁移,请稍后...<br/>'; for ($i = 0; $i < 100; $i++) { QLog::log('正在迁出数据:' . $i); echo $i . '<br/>'; } QLog::log('数据迁出完成!'); return '迁出成功'; }
public function getOne($sql) { //FETCH_BOTH $stmt = $this->pdo->query($sql); if ($stmt === false) { QLog::log("STMT GET FALSE FOR SQL: " . $sql); throw new Exception("STMT GET FALSE FOR SQL: " . $sql, -1); } $rows = $stmt->fetchAll(PDO::FETCH_BOTH); if ($rows) { $row = $rows[0]; if ($row) { return $row[0]; } else { return false; } } else { return false; } }
public function execute($sql, $inputarr = null) { if (!$this->isConnected()) { $this->connect(); } $sth = $this->_conn->prepare($sql); if ($this->_log_enabled) { QLog::log($sql, QLog::DEBUG); } $result = $sth->execute((array) $inputarr); if (false === $result) { $error = $sth->errorInfo(); $this->_last_err = $error[2]; $this->_last_err_code = $error[1]; $this->_has_failed_query = true; throw new QDB_Exception($sql, $this->_last_err, $this->_last_err_code); } $this->_lastrs = $sth; if ('select' == strtolower(substr($sql, 0, 6))) { return new QDB_Result_Pdo($this->_lastrs, $this->_fetch_mode); } else { return $this->affectedRows(); } }
public static function searchKeywords($keywords = array()) { global $db; if (empty($keywords)) { return false; } $keywords_in_sql = "'" . implode("','", $keywords) . "'"; $sql = "SELECT keyword_id FROM keyword WHERE keyword in ({$keywords_in_sql})"; $keyword_id_list = $db->getColumn($sql); QLog::log("KYStoreModel::searchKeywords(" . implode(',', $keywords) . ") -> sql: " . $sql . " -> " . implode(',', $keyword_id_list)); if (empty($keyword_id_list) || count($keyword_id_list) != count($keywords)) { return false; } $yamai_list = null; foreach ($keyword_id_list as $keyword_id) { $sql = "SELECT yamai_id FROM yamai_keyword yk \n\t\t\t\tWHERE yk.keyword_id={$keyword_id} and yk.relation_score>0\n\t\t\t"; if ($yamai_list !== null) { $sql .= " and yk.yamai_id in (" . implode(",", $yamai_list) . ") "; } $yamai_list_once = $db->getColumn($sql); QLog::log("KYStoreModel::searchKeywords once for {$keyword_id} -> sql: " . $sql . " -> list: " . implode(',', $yamai_list_once)); if ($yamai_list === null) { $yamai_list = $yamai_list_once; } else { $yamai_list = array_intersect($yamai_list, $yamai_list_once); } QLog::log("KYStoreModel::searchKeywords intersect list: " . implode(',', $yamai_list)); if (empty($yamai_list)) { return false; } } $sql = "SELECT y.*,group_concat('',k.keyword) keyword_list FROM yamai y\n\t\t\tLEFT JOIN yamai_keyword yk ON y.yamai_id=yk.yamai_id\n\t\t\tLEFT JOIN keyword k ON k.keyword_id=yk.keyword_id\n\t\t\tWHERE y.yamai_id in (" . implode(',', $yamai_list) . ") \n\t\t\tGROUP BY y.yamai_id\n\t\t"; $list = $db->getAll($sql); QLog::log("KYStoreModel::searchKeywords find details -> sql: " . $sql . " -> " . implode(',', $keyword_id_list)); return $list; }
/** * 初始化应用程序设置 */ protected function _initConfig() { #IFDEF DEBUG QLog::log(__METHOD__, QLog::DEBUG); #ENDIF // 载入配置文件 if ($this->_app_config['CONFIG_CACHED']) { /** * 从缓存载入配置文件内容 */ // 构造缓存服务对象 $backend = $this->_app_config['CONFIG_CACHE_BACKEND']; $settings = isset($this->_app_config['CONFIG_CACHE_SETTINGS'][$backend]) ? $this->_app_config['CONFIG_CACHE_SETTINGS'][$backend] : null; $cache = new $backend($settings); // 载入缓存内容 $cache_id = $this->_app_config['APPID'] . '_app_config'; $config = $cache->get($cache_id); if (!empty($config)) { Q::replaceIni($config); return; } } // 没有使用缓存,或缓存数据失效 $config = self::loadConfigFiles($this->_app_config); if ($this->_app_config['CONFIG_CACHED']) { $cache->set($cache_id, $config); } Q::replaceIni($config); }
function execute($sql, $inputarr = null) { if (is_array($inputarr)) { $sql = $this->_fakebind($sql, $inputarr); } if ($this->_log_enabled) { QLog::log('[DB EXECUTE] ' . str_replace("\n", ' ', $sql), QLog::DEBUG); } if (!$this->_conn) { $this->connect(); } $result = mysql_query($sql, $this->_conn); if (is_resource($result)) { return new QDB_Result_Mysql($result, $this->_fetch_mode); } elseif ($result) { $this->_last_err = null; $this->_last_err_code = null; return $result; } else { $this->_last_err = mysql_error($this->_conn); $this->_last_err_code = mysql_errno($this->_conn); $this->_has_failed_query = true; if ($this->_last_err_code == 1062) { throw new QDB_Exception_DuplicateKey($sql, $this->_last_err, $this->_last_err_code); } else { throw new QDB_Exception($sql, $this->_last_err, $this->_last_err_code); } } }
/** * 执行一个查询,返回一个查询对象或者 boolean 值,出错时抛出异常 * * $sql 是要执行的 SQL 语句字符串,而 $inputarr 则是提供给 SQL 语句中参数占位符需要的值。 * * 如果执行的查询是诸如 INSERT、DELETE、UPDATE 等不会返回结果集的操作, * 则 execute() 执行成功后会返回 true,失败时将抛出异常。 * * 如果执行的查询是 SELECT 等会返回结果集的操作, * 则 execute() 执行成功后会返回一个 DBO_Result 对象,失败时将抛出异常。 * * QDB_Result_Abstract 对象封装了查询结果句柄,而不是结果集。 * * @param string $sql * @param array $inputarr * * @return QDB_Result_Abstract */ function execute($sql, $inputarr = null) { if (is_array($inputarr)) { $sql = $this->_fakebind($sql, $inputarr); } if (!$this->_conn) { $this->connect(); } //print_R($sql); $this->_lastrs = @pg_query($this->_conn, $sql); if ($this->_log_enabled) { QLog::log($sql, QLog::DEBUG); } if (is_resource($this->_lastrs)) { Q::loadClass('Qdb_Result_Pgsql'); return new QDB_Result_Pgsql($this->_lastrs, $this->_fetch_mode); } elseif ($this->_lastrs) { $this->_last_err = null; $this->_last_err_code = null; return $this->_lastrs; } else { $this->_last_err = pg_errormessage($this->_conn); $this->_last_err_code = null; $this->_has_failed_query = true; throw new QDB_Exception($sql, $this->_last_err, $this->_last_err_code); } }
/** * 构造函数 * * @param int $type * @param array $config * @param QDB_ActiveRecord_Meta $source_meta * * @return QDB_ActiveRecord_Association_Abstract */ protected function __construct($type, array $config, QDB_ActiveRecord_Meta $source_meta) { $this->type = $type; #IFDEF DEBUG QLog::log(__METHOD__ . "({$config['mapping_name']})", QLog::DEBUG); #ENDIF foreach (self::$_init_config_keys as $key) { if (!empty($config[$key])) { $this->{$key} = $config[$key]; } } $this->_init_config = $config; $this->source_meta = $source_meta; }
/** * 执行指定的 Action 方法 * * @param QContext $context * @param array $args * * @return mixed */ protected function _executeAction(QContext $context, array $args = array()) { // 检查是否有权限访问 $controller_name = $context->controller_name; $action_name = $context->action_name; $namespace = $context->namespace; $module = $context->module_name; QLog::log(sprintf('Execute controller action: "%s".', $context->getRequestUDI())); if (!$this->checkAuthorized($controller_name, $action_name, $namespace, $module)) { $response = call_user_func($context->getIni('dispatcher_on_access_denied'), $context); } else { // 尝试载入控制器 $class_name = $context->getIni('controller_class_prefix') . 'Controller_'; if ($namespace) { $class_name .= ucfirst($namespace) . '_'; } $class_name .= ucfirst(str_replace('_', '', $controller_name)); $app_config = self::getAppConfig($this->_appid); if ($module) { $dir = $app_config['ROOT_DIR'] . "/modules/{$module}/controller"; } else { $dir = $app_config['ROOT_DIR'] . "/app/controller"; } if ($namespace) { $dir .= DS . $namespace; } // 构造控制器对象 try { $filename = $controller_name . '_controller.php'; Q::loadClassFile($filename, array($dir), $class_name); } catch (QException $ex) { $response = call_user_func($this->context->getIni('dispatcher_on_action_not_found'), $context); if (is_null($response)) { $response = ''; } } if (!isset($response)) { $controller = new $class_name($this, $context); /* @var $controller QController_Abstract */ if ($context->isAJAX()) { $controller->view = null; } $response = $controller->_execute($args); } } if (is_object($response) && method_exists($response, 'execute')) { $response = $response->execute(); } elseif ($response instanceof QController_Forward) { // 更新 flash message $key = $this->context->getIni('app_flash_message_key'); unset($_SESSION[$key]); $response = $this->_executeAction($response->context, $response->args); } return $response; }
/** * 创建对象时,保存 many_to_many 关联 */ function testCreateWithManyToMany() { QLog::log(__METHOD__, QLog::DEBUG); $tags = array('PHP', 'C++', 'Java'); $content = new Content(array('title' => 'title - ' . mt_rand(), 'author_id' => 0)); foreach ($tags as $tag_name) { $content->tags[] = new Tag(array('name' => $tag_name)); } $content->save(); $this->assertNotNull($content->id()); $row = $this->_queryContent($content->id()); $this->_checkContent($row, $content); foreach ($tags as $offset => $tag_name) { $row = $this->_queryTag($tag_name); $this->assertType('array', $row); $this->_checkTag($row, $content->tags[$offset]); } $mid_table_name = $content->getMeta()->associations['tags']->mid_table->qtable_name; $sql = "SELECT * FROM {$mid_table_name} WHERE content_id = ?"; $rowset = $this->_conn->getAll($sql, array($content->id())); $this->assertEquals(count($tags), count($rowset)); $tags = array_flip($tags); foreach ($rowset as $row) { $this->assertTrue(isset($tags[$row['tag_name']])); unset($tags[$row['tag_name']]); } }
keyword_id ~ keyword yamai_id ~ yamai_name yamai_type yamai_system yamai_id keyword_id ~ relation_score */ $act = getRequest('act'); if ($act == 'update') { $yamai_name = getRequest('yamai_name', ''); $yamai_type = getRequest('yamai_type', null); $yamai_system = getRequest('yamai_system', null); $keyword = getRequest('keyword', ''); $score = getRequest('score', 3); QLog::log("COMMON UPDATE: [{$yamai_name}][{$yamai_type}][{$yamai_system}]+[{$keyword}]={$score}"); $yamai_id = YamaiModel::getYamaiId($yamai_name, $yamai_type, $yamai_system); $keyword_id = KeywordModel::getKeywordId($keyword); $afx = KYStoreModel::updateKYScore($keyword_id, $yamai_id, $score); QLog::log("DB RESULT: Y[{$yamai_id}]+K[{$keyword_id}]=" . ($afx === false ? 'FALSE' : $afx)); echo json_encode(array('updated' => $afx === false ? '0' : '1')); exit; } elseif ($act == "list_yamai") { $yamai_name = getRequest('yamai_name', ''); $yamai_type = getRequest('yamai_type', null); $yamai_system = getRequest('yamai_system', null); $limit = 0; $offset = 0; $list_of_yamai = YamaiModel::listYamai($yamai_name, $yamai_type, $yamai_system, $limit, $offset); echo json_encode(array('list' => $list_of_yamai === false ? array() : $list_of_yamai)); exit; } elseif ($act == "search") { $keywords = getRequest('keywords', array()); $keywords = preg_split("/[\\s,]+/", $keywords); $list_of_yamai = KYStoreModel::searchKeywords($keywords);