在MariaDB Xpand中使用PostgreSQL分页器

I’m not an anti-GUI person. In fact, I wrote three books about web GUI development with Java. However, I also like the command-line interface (CLI), especially text-based UIs. After a year of exploring MariaDB and the DevOps world, I got to discover and play with many text-based CLI tools that I didn’t know even existed. These tools are especially useful when connecting to remote servers that don’t have a GUI.

我經常使用的一個特殊CLI工具是mariadb SQL 客戶端(在MySQL世界中則是mysql)——一個用於連接到兼容MariaDB數據庫的CLI程序。透過它,你可以向數據庫服務器發送SQL查詢和其他命令。

基於CLI的MariaDB SQL客戶端

mariadb SQL客戶端擁有多種配置選項,其中之一是可以設置一個終端分頁器。如果你熟悉Linux,你可能已經聽說過或使用過moreless分頁器。你可以通過環境變量PAGER來設置分頁器,mariadb將自動使用它。或者,你也可以僅在當前會話中使用mariadb提示符來設置分頁器。例如,一旦你連接到數據庫,要使用less分頁器,請運行以下命令:

MariaDB SQL

 

pager less

下次你運行一個SQL查詢時,你將能夠使用鍵盤上的箭頭鍵瀏覽結果集。

使用mariadb SQL客戶端設置分頁器

less 分頁器雖然實用,但對於以表格形式顯示的 SQL 結果集來說並非最佳選擇。有一款名為 pspg 的開源工具(其文檔和源碼可在 GitHub 上找到),最初是為 PostgreSQL 開發的,後來增加了對包括 MariaDB 在內的多種其他資料庫的支持。由於 mariadb SQL 客戶端能夠連接到 MariaDB Xpand 資料庫,我嘗試了一下,效果非常理想。繼續閱讀以了解如何進行嘗試。

啟動 Xpand 資料庫最簡單的方式是在 SkySQL 上創建一個服務(免費)。當然,你也可以使用 Docker 運行本地實例。以下是你需要的代碼片段:

Shell

 

docker run --name xpand \
  -d \
  -p 3306:3306 \
  
--ulimit memlock=-1 \
  mariadb/xpand-single

資料庫有數據時會更有趣。此網站提供了一個簡單而有趣的演示資料庫。在類似 Linux 的操作系統上,運行以下命令(如果你的 Xpand 資料庫運行在其他地方,請在最后一條命令中更改 IP 地址):

Shell

 

sudo apt install curl -y
curl https://www.mariadbtutorial.com/wp-content/uploads/2019/10/nation.zip --output nation.zip
unzip nation.zip
mariadb -h 127.0.0.1 -u xpand < nation.sql
rm nation.zip nation.sql

別忘了安裝 pspg

Shell

 

apt install pspg -y

使用 mariadb SQL 客戶端連接到資料庫,並使用一個顯示“Xpand”的定制且更酷的提示符:

Shell

 

mariadb -h 127.0.0.1 -u xpand --prompt="Xpand [\d]> " nation

I learned this tip from my colleague Patrick Bossman (Product Manager at MariaDB) during a webinar on MariaDB Xpand + Docker. I recommend watching it if you want to learn more.

透過自訂提示連接至MariaDB Xpand

設定當前會話的pspg分頁器:

MariaDB SQL

 

pager pspg -s 14 -X --force-uniborder --quit-if-one-screen

A nice feature in pspg is that it shows the fancy text-based UI only when it makes sense (--quit-if-one-screen). So if your query returns only a few rows that fit in the screen, it will just show them right there on the screen as usual. For example, try running the following query:

MariaDB SQL

 

select * from continents;

此處無新奇之處。

若僅顯示少數幾行,pspg分頁器不會啟動

然而,試試以下操作:

MariaDB SQL

 

select * from countries;

A navigable text-based interface allows you to explore the data more efficiently.

pspg分頁器渲染MariaDB Xpand的數據

您可以搜尋行、排序、匯出至CSV、凍結欄位、標記行,甚至使用滑鼠與工具互動,以及其他功能。

部分pspg中的選單選項

I hope this tool helps you the next time you have to interact with a database via SSH and the command line. You can find more information about how to install pspg on your operating system, configuration options, and documentation on the GitHub repository for the project. If you want to learn more about distributed SQL and the MariaDB Xpand database, watch this short video, take a look at this datasheet, and explore some of the blog posts and documentation.

Source:
https://dzone.com/articles/using-the-postgressql-pager-with-mariadb-xpand