pragmarx/sqli

一个 Laravel Artisan SQL 交互式界面

v1.0.1 2015-03-19 22:02 UTC

This package is auto-updated.

Last update: 2024-09-21 20:05:33 UTC


README

Latest Stable Version License Downloads

A Laravel 4 & 5 Artisan SQL 交互式界面,还包括一些 Artisan 命令来执行 SQL 查询。

sqli

就像 tinker 对于 SQL 一样,只需运行

php artisan sqli

然后你可以在你的 SQL 中执行任何你喜欢的 SQL 查询

postgresql:laravel> select email from users;

你应该会看到这样的结果

+----+------------------------------+
| id | email                        |
+----+------------------------------+
|  1 | arnold@schwarzenegger.com    |
|  2 | danny@devito.com             |
+----+------------------------------+
Executed in 0.0602 seconds.

你可以通过运行来获取你的表列表

postgresql:laravel> tables count

count 选项是可选的

+--------------+-----------------------------+-----------+
| table_schema | table_name                  | row_count |
+--------------+-----------------------------+-----------+
| public       | firewall                    | 2         |
| public       | migrations                  | 3         |
| public       | sessions                    | 1         |
| public       | users                       | 1         |
| public       | actors                      | 3431326   |
| public       | movies                      | 1764727   |
+--------------+-----------------------------+-----------+

你可以查看数据库列表

postgresql:laravel> databases

+-----------------+--------+--------------------------+-----------+
| Connection Name | Driver | Database                 | Host      |
+-----------------+--------+--------------------------+-----------+
| postgres        | pgsql  | production               | localhost |
| tracker         | mysql  | tracker                  | localhost |
+-----------------+--------+--------------------------+-----------+

你可以通过以下方式更改当前数据库连接

postgresql:laravel> database mysql
mysql:staging>

你可以通过执行来列出所有命令

postgresql:laravel> help

+----------+------------------------------------------------------------------------------+
| command  | description                                                                  |
+----------+------------------------------------------------------------------------------+
| quit     | Exit interface.                                                              |
| tables   | List all tables. Use "tables count" to list with row count.                  |
| help     | Show this help.                                                              |
| database | Change the current database connection. Usage: "database [connection name]". |
+----------+------------------------------------------------------------------------------+

要退出,只需键入 CTRL-CCTRL-Dquit

其他 Artisan 命令

你不需要输入 sqli 来执行命令,你可以通过直接 Artisan 命令访问最常用的 DML 命令

select
insert
update
delete

一个特殊的 DML 命令,用于执行你可能需要的任何其他内容

sql

还有一个用于列出表的命令

tables

语法

语法非常简单,只需执行

php artisan select email, first_name, last_name from users

你应该会得到一个类似的结果

+----+------------------------------+----------------+----------------+
| id | email                        | first_name     | last_name      |
+----+------------------------------+----------------+----------------+
|  1 | arnold@schwarzenegger.com    | Arnold         | Schwarzenegger |
|  2 | danny@devito.com             | Danny          | DeVito         |
+----+------------------------------+----------------+----------------+

为 Artisan 创建一个非常小的别名

alias a='php artisan'

这样你就可以像在 SQL 界面中一样操作了

a select * from posts where post_id < 100

a update posts set author_id = 1

a delete from posts

a sql call removeOldPosts()

命令 'table'

该命令

php artisan tables --count

会给你一个带有可选行数的表列表

+--------------+-----------------------------+-----------+
| table_schema | table_name                  | row_count |
+--------------+-----------------------------+-----------+
| public       | firewall                    | 2         |
| public       | migrations                  | 3         |
| public       | sessions                    | 1         |
| public       | users                       | 1         |
| public       | actors                      | 3431326   |
| public       | movies                      | 1764727   |
+--------------+-----------------------------+-----------+

太多列看起来不合适?

使用 less 命令来帮助你处理这个问题

a select * from users | less -S

应该会给你一个可滚动的视图

+----+------------------------------+-------------+-----------+--------------------------------------------+---------------------+---------------------+--------------------------------------------------------------+---------------------+----------------+----------------+----------------------------+----------------------------+--------------------------------------------------------------+-----------+-------------+-----------+-----------+-------------+------------+--------------+------------------+-------------------+-----------------+-------------------+-----------------+-----------------+
| id | email                        | permissions | activated | activation_code                            | activated_at        | last_login          | persist_code                                                 | reset_password_code | first_name     | last_name      | created_at                 | updated_at                 | password                                                     | gender_id | middle_name | nick_name | birth_day | birth_month | birth_year | early_signup | imported_from_id | registration_time | registration_ip | registrated_by_id | activation_time | beta_invitation |
+----+------------------------------+-------------+-----------+--------------------------------------------+---------------------+---------------------+--------------------------------------------------------------+---------------------+----------------+----------------+----------------------------+----------------------------+--------------------------------------------------------------+-----------+-------------+-----------+-----------+-------------+------------+--------------+------------------+-------------------+-----------------+-------------------+-----------------+-----------------+
| 38 | arnold@schwarzenegger.com    |             | 1         | V38ScwjCORUvCpuhjkieR4KbnQSlVbhFHujmsyVvN8 | 2014-02-16 14:07:59 | 2014-03-27 18:59:56 | $2y$10$POQ18Kc5JXftOtJswQujBO0PAQ4cfqsSXLKckn9aZOM4VgaExRDHa |                     | Arnold         | Schwarzenegger | 2014-03-29 18:38:39.998522 | 2014-03-27 18:59:56        | $2y$10$5S3KaI6PPHnySECVRwRcferQdiJZP6QgX5adxK7z/WPlxP386HW0e |           |             |           | 31        | 10          |            |              |                  |                   |                 |                   |                 |                 |
| 40 | clint@eastwood.com           |             |           |                                            |                     |                     |                                                              |                     | Clint          | Eastwood       | 2014-03-29 18:38:39.998522 | 2014-03-29 18:26:17.402382 |                                                              |           |             |           |           |             |            |              |                  |                   |                 |                   |                 |                 |
| 41 | paul@newman.com              |             |           |                                            |                     |                     |                                                              |                     | Paul           | Newman         | 2014-03-29 18:38:39.998522 | 2014-03-29 18:32:22.489968 |                                                              |           |             |           |           |             |            |              |                  |                   |                 |                   |                 |                 |
+----+------------------------------+-------------+-----------+--------------------------------------------+---------------------+---------------------+--------------------------------------------------------------+---------------------+----------------+----------------+----------------------------+----------------------------+--------------------------------------------------------------+-----------+-------------+-----------+-----------+-------------+------------+--------------+------------------+-------------------+-----------------+-------------------+-----------------+-----------------+

缺点

在向脚本传递参数时,基于 Linux 的系统可能会删除引号,并误解查询中的括号,如果你需要使用它们,你将不得不使用双引号

a insert "into users (email, first_name, last_name, created_at, updated_at) values ('clint@eastwood.com', 'Clint', 'Eastwood', 'NOW', 'NOW')"

或者只是带有引号的部分

a insert into users "(email, first_name, last_name, created_at, updated_at)" values "('clint@eastwood.com', 'Clint', 'Eastwood', 'NOW', 'NOW')"

但你也可以用 \ 来转义它们

a update users set created_at = \'NOW\'

命令行补全

所有 SQL 命令、连接、表名和列都包含在补全中,只需使用 Tab 键来完成你的命令。

安装

需求

  • Laravel 4.1+ / Laravel 5+

安装

使用 Composer 安装包

composer require pragmarx/sqli

将服务提供者添加到你的 app/config/app.php 文件中

'PragmaRX\Sqli\Vendor\Laravel\ServiceProvider',

作者

Antonio Carlos Ribeiro

许可

sqli 使用 BSD 3-Clause 许可证 - 有关详细信息,请参阅 LICENSE 文件

贡献

欢迎提交拉取请求和问题。