/** * 静态方法, 单例统一访问入口 * @return object 返回对象的唯一实例 */ public static function getInstance() { if (is_null(self::$_instance) || !isset(self::$_instance)) { self::$_instance = new self(); } return self::$_instance; }
/** * 如果出现多条数据 都删除 * @param int $id */ public static function deleteByID($id) { $sql = "DELETE from " . DB_PREFIX . "_user_devices_metas WHERE id={$id}"; Yii::trace("function: '{__FUNCTION__}',sql:'{$sql}'"); $db_manager = MDbManager::getInstance(); if ($db_manager->deleteDb($sql) === false) { return false; } return true; }
/** * 更新文件引用次数 * @param int $id * @return */ public static function updateRefCountById($id, $isAdd = true) { $sql = "UPDATE " . DB_PREFIX . "_file_versions "; if ($isAdd) { $sql .= "SET ref_count = ref_count + 1, updated_at = now() "; } else { $sql .= "SET ref_count = ref_count - 1, updated_at = now() "; } $sql .= "WHERE id={$id}"; Yii::trace("function: '{__FUNCTION__}',sql:'{$sql}'"); $db_manager = MDbManager::getInstance(); return $db_manager->updateDb($sql); }
/** * 根据用户的id与meta的key查询用户meta信息 * @param string $user_id 用户的唯一标示 * @param string $key meta的键 */ public function queryMetaByIDAndKey($user_id, $key) { $db = MDbManager::getInstance(); // 用户名称登录 $sql = "select * from " . DB_PREFIX . "_user_metas where user_id={$user_id} and meta_key=\"{$key}\""; Yii::trace("queryMetaByIDAndKey:" . $sql); $db_data = $db->selectDb($sql); if (empty($db_data)) { return false; } return self::assembleUserMeta($db_data[0]); }
/** * * 根据id更新parentid * @param int $fromId * @param int $toId */ public static function updateParentId($fromId, $toId) { $sql = "UPDATE " . DB_PREFIX . "_files SET "; $sql .= "parent_file_id={$toId}"; $sql .= " WHERE parent_file_id = {$fromId}"; Yii::trace("function: '{updateParentId}',sql:'{$sql}'"); $db_manager = MDbManager::getInstance(); $db_manager->updateDb($sql); }
/** * * 根据查询出的用户信息组装user对象 * @param int $user_id 用户id * @return mixed $value 返回最终需要执行完的结果 */ public function assembleUser($user_data) { $this->id = $user_data["id"]; $this->user_id = $user_data["id"]; $this->user_uuid = $user_data["user_uuid"]; $this->user_name = $user_data["user_name"]; $this->user_pass = $user_data["user_pass"]; $this->user_status = $user_data["user_status"]; $this->created_at = $user_data["created_at"]; $this->updated_at = $user_data["updated_at"]; //填充用户当前空间,与当前使用空间 $db = MDbManager::getInstance(); $sql = "select * from " . DB_PREFIX . "_user_metas where user_id={$this->user_id} and meta_key in('space','phone','email','nick')"; $items = $db->selectDb($sql); $this->nick = $this->user_name; //用户昵称 $this->phone = ""; //用户电话 $this->email = ""; //用户邮件 $this->space = FALSE; foreach ($items as $index => $item) { $value = $item["meta_value"]; if ($item["meta_key"] == "space") { $this->space = doubleval($value) * 1024 * 1024; } if ($item["meta_key"] == "nick" && strlen(trim($value)) > 0) { $this->nick = $value; } if ($item["meta_key"] == "phone") { $this->phone = $value; } if ($item["meta_key"] == "email") { $this->email = $value; } } //查询用户的默认空间 if ($this->space === FALSE) { $this->space = MUtils::defaultTotalSize(); } $this->usedSpace = $this->getUsedSpaceById($this->user_id); return $this; }
/** * 批量处理添加数据 * @param array $create_array 包含需要存储的数组 * @param string $meta_key 查询关键字 * @return mixed 创建成功返回true,否则返回false * @since 1.0.7 */ public static function batchCreateFileMetas($create_array, $meta_key) { $driver = Yii::app()->db->driverName; $sql = "insert into " . DB_PREFIX . "_file_metas(file_path,meta_key,"; $sql .= "meta_value,created_at,updated_at) values "; if ($driver == 'sqlite') { $sql = "insert into " . DB_PREFIX . "_file_metas(file_path,meta_key,"; $sql .= "meta_value,created_at,updated_at) select "; } $sql_value = ''; $len = strlen($sql); $result = true; $db_manager = MDbManager::getInstance(); $index = 0; $count = count($create_array); foreach ($create_array as $file_meta) { if ($file_meta->is_add === false) { // 存在记录,不需要添加 continue; } $sql_value .= $driver == 'sqlite' ? "" : "("; $sql_value .= "\"{$file_meta->file_path}\","; $sql_value .= "\"{$meta_key}\","; $sql_value .= "'{$file_meta->meta_value}',"; $sql_value .= "now(),now()"; $sql_value .= $driver == 'sqlite' ? "" : ")"; //添加 union all select 或者 ',' $index++; if ($index < $count) { $sql_value .= $driver == 'sqlite' ? " union all select " : ","; } if (strlen($sql_value) + $len >= MConst::MAX_SQL_STRING_LENGTH || strlen($sql_value) + $len >= MConst::MAX_SQL_STRING_LENGTH - 1000) { $sql_exe = $sql . $sql_value; Yii::trace("function: '{batchCreateFileMetas}',sql:'{$sql_exe}'"); $result = $db_manager->insertDb($sql_exe); if ($result == false) { return $result; } $sql_value = ''; } } if (!empty($sql_value)) { $sql_exe = $sql . $sql_value; $result = $db_manager->insertDb($sql_exe); } return $result; }
/** * * 根据设备id 删除用户设备记录 * @param $device_id 设备id */ public static function deleteUserDeviceById($device_id) { $db_manager = MDbManager::getInstance(); $sql = "delete from " . DB_PREFIX . "_user_devices where id = {$device_id}"; $db_manager->deleteDb($sql); $sql = "delete from " . DB_PREFIX . "_server_token where ost_usa_id_ref = {$device_id}"; $db_manager->deleteDb($sql); }