简体   繁体   English

使用 Laravel Eloquent 通过第三方“代理”查询 SQLite

[英]Query SQLite through third-party "proxy" with Laravel Eloquent

I am attempting to query (manipulation optional) a SQLite database using Laravel Eloquent.我正在尝试使用 Laravel Eloquent 查询(操作可选)SQLite 数据库。 A driver already exists for this an is delightfully easy to use.为此已经存在一个驱动程序并且非常易于使用。

However, the database is remote and part of a video game.但是,该数据库是远程的并且是视频游戏的一部分。 The game supports RCON which allows me to send commands and in this case, I can send SQL statements.游戏支持 RCON,它允许我发送命令,在这种情况下,我可以发送 SQL 语句。

My current state:我现在的状态:

  1. Send a SQL statement prefixed with "sql" to remote machine via third party library:通过第三方库向远程机器发送以“sql”为前缀的 SQL 语句:
sql SELECT id, level, guild, isAlive FROM characters
  1. Receive a line delimited string, prefixed by record number:接收以行分隔的字符串,以记录号为前缀:
     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 |
  1. Parse the output line by line in a particularly unsavory method and manually assign them to a custom built model/class.以一种特别令人讨厌的方法逐行解析输出,并手动将它们分配给自定义构建的模型/类。

I would really like to incorporate Eloquent in any capacity rather than use my own custom classes.我真的很想以任何方式合并 Eloquent,而不是使用我自己的自定义类。 As I typed this post out, I realized I don't believe I'll be able to "piggy back" off of the existing SQLite driver and this would likely be a completely new driver altogether.当我打出这篇文章时,我意识到我不相信我能够“背负”现有的 SQLite 驱动程序,这可能是一个全新的驱动程序。

However, to those more experienced than myself, do you have any suggestions or methods of approaching this situation?但是,对于那些比我更有经验的人,您有什么解决这种情况的建议或方法吗?

Ultimately I ended up processing the output of the RCON command.最终我最终处理了 RCON 命令的输出。 The downside is that it is particularly static.缺点是它特别静态。 I have to build around things like the columns being selected with the SQL statement and adjusting the results to proper types.我必须围绕诸如使用 SQL 语句选择的列并将结果调整为正确类型之类的内容进行构建。

I am using https://github.com/gorcon/rcon-cli and wrapped a query class around it:我正在使用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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM