亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Laravel Eloquent 通過第三方“代理”查詢 SQLite

使用 Laravel Eloquent 通過第三方“代理”查詢 SQLite

PHP
不負相思意 2022-10-14 10:40:42
我正在嘗試使用 Laravel Eloquent 查詢(可選操作)SQLite 數據庫。為此已經存在一個驅動程序,并且非常易于使用。然而,該數據庫是遠程的并且是視頻游戲的一部分。游戲支持 RCON,它允許我發送命令,在這種情況下,我可以發送 SQL 語句。我現在的狀態:通過第三方庫向遠程機器發送以“sql”為前綴的 SQL 語句:sql SELECT id, level, guild, isAlive FROM characters接收以記錄號為前綴的行分隔字符串:     id |  level |  guild |  isAlive |#0 1183 |     14 |     60 |        1 |#1  636 |     10 |     60 |        1 |#2   41 |     30 |     60 |        1 |#3   47 |     27 |     60 |        1 |#4   49 |     38 |     60 |        1 |#5  403 |     32 |     60 |        1 |#6   50 |     31 |     60 |        1 |#7 1389 |     44 |     60 |        1 |以特別令人討厭的方法逐行解析輸出,然后手動將它們分配給自定義構建的模型/類。我真的很想以任何身份合并 Eloquent,而不是使用我自己的自定義類。當我打出這篇文章時,我意識到我不相信我能夠“搭載”現有的 SQLite 驅動程序,而這很可能是一個全新的驅動程序。但是,對于那些比我更有經驗的人,您對處理這種情況有什么建議或方法嗎?
查看完整描述

1 回答

?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

最終,我處理了 RCON 命令的輸出。缺點是它特別靜態。我必須圍繞諸如使用 SQL 語句選擇的列并將結果調整為適當的類型等內容進行構建。

我正在使用https://github.com/gorcon/rcon-cli并圍繞它包裝了一個查詢類:

class RconDatabase

{

    const RCON_EXECUTABLE = __DIR__ . '/../bin/rcon';


    public function __construct()

    {


    }


    public function query($sql)

    {

        if (!is_executable(self::RCON_EXECUTABLE)) throw new FilePermissionException('Unable to execute RCON');


        $cmd = self::RCON_EXECUTABLE.' -a '.Config::get('rcon.host').':'.Config::get('rcon.port').' -p '.Config::get('rcon.password').' -c "sql '. $sql .'"';

        $output = shell_exec($cmd);


        if ($output == null) throw new RconConnectionException('No response from RCON server');

        if (strpos($output, 'authentication failed') !== false) throw new RconAuthenticationException();

        if (strpos($output, 'dial tcp') !== false) throw new RconNetworkException();


        $lines = preg_split("/((\r?\n)|(\r\n?))/", $output);


        $results = array();

        $last_column = 0;

        for ($i = 0; $i < count($lines); $i++) {

            if (empty($lines[$i])) continue;


            $columns = str_getcsv($lines[$i], '|');


            for ($x = 0; $x < count($columns); $x++) {

                if ($i == 0 && empty($columns[$x])) {

                    $last_column = $x;

                    continue;

                }


                if ($x == 0 && preg_match('/^#[0-9]+\s+(\S+)/', $columns[0], $match))

                    $columns[$x] = $match[1];


                $columns[$x] = trim($columns[$x]);

            }


            if ($i == 0) continue;


            if ($last_column > 0) unset($columns[$last_column]);


            array_push($results, $columns);

        }


        return $results;

    }

}



查看完整回答
反對 回復 2022-10-14
  • 1 回答
  • 0 關注
  • 124 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號