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

我正在尝试使用 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 驱动程序,而这很可能是一个全新的驱动程序。


但是,对于那些比我更有经验的人,您对处理这种情况有什么建议或方法吗?


不负相思意
浏览 88回答 1
1回答

守着一只汪

最终,我处理了 RCON 命令的输出。缺点是它特别静态。我必须围绕诸如使用 SQL 语句选择的列并将结果调整为适当的类型等内容进行构建。我正在使用https://github.com/gorcon/rcon-cli并围绕它包装了一个查询类:class RconDatabase{&nbsp; &nbsp; const RCON_EXECUTABLE = __DIR__ . '/../bin/rcon';&nbsp; &nbsp; public function __construct()&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; public function query($sql)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (!is_executable(self::RCON_EXECUTABLE)) throw new FilePermissionException('Unable to execute RCON');&nbsp; &nbsp; &nbsp; &nbsp; $cmd = self::RCON_EXECUTABLE.' -a '.Config::get('rcon.host').':'.Config::get('rcon.port').' -p '.Config::get('rcon.password').' -c "sql '. $sql .'"';&nbsp; &nbsp; &nbsp; &nbsp; $output = shell_exec($cmd);&nbsp; &nbsp; &nbsp; &nbsp; if ($output == null) throw new RconConnectionException('No response from RCON server');&nbsp; &nbsp; &nbsp; &nbsp; if (strpos($output, 'authentication failed') !== false) throw new RconAuthenticationException();&nbsp; &nbsp; &nbsp; &nbsp; if (strpos($output, 'dial tcp') !== false) throw new RconNetworkException();&nbsp; &nbsp; &nbsp; &nbsp; $lines = preg_split("/((\r?\n)|(\r\n?))/", $output);&nbsp; &nbsp; &nbsp; &nbsp; $results = array();&nbsp; &nbsp; &nbsp; &nbsp; $last_column = 0;&nbsp; &nbsp; &nbsp; &nbsp; for ($i = 0; $i < count($lines); $i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (empty($lines[$i])) continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $columns = str_getcsv($lines[$i], '|');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ($x = 0; $x < count($columns); $x++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($i == 0 && empty($columns[$x])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $last_column = $x;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($x == 0 && preg_match('/^#[0-9]+\s+(\S+)/', $columns[0], $match))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $columns[$x] = $match[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $columns[$x] = trim($columns[$x]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($i == 0) continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($last_column > 0) unset($columns[$last_column]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($results, $columns);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $results;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP