Package: py-dbx
doc on github.iodbxdbxis the command line tool included in py-dbx package.  It was named sretools-dbx as you can see in below examples until fully updated.you rundbxorpython -mdbxother thansretools-dbxin the past.Installationargswork with db2/postgresql/mysql and moreexport datapluginInstallationpip install py-dbxrun withpython -mdbxor justdbxdbx (sretools-dbx)sretools-dbx is a generic database client using JDBC.  It's not designed to replace any current factory client but to provide people who work under terminals a new way to access their data with better console output, and ablility to export as JSON, YAML or HTML tables.It's at command level which means it's open to full bash access and one can always utilize his or her expertis on unix tools.$sretools-dbx-h
usage:sretools-dbx[-h][-JJAR][-uUSER][-pPASSWORD][-DDRIVER][-UURL][-C][-QSQL][-wMAXWIDTH][-FFORMAT][-v][-X]optionalarguments:-h,--helpshowthishelpmessageandexit-JJAR,--jarJARjdbcdriverjarfile-uUSER,--userUSERuser.defaultascurrentunixuser.-pPASSWORD,--passwordPASSWORDpassword.ENV["dbx_mypwd"]-DDRIVER,--driverDRIVERjdbcdrivername-UURL,--urlURLjdbcurlorconnectionstring-C,--connectforceconnectasserver-QSQL,--sqlSQLsqlstatementtorun-wMAXWIDTH,--maxwidthMAXWIDTHmaxwidthofcolumn-FFORMAT,--outformatFORMATjson,yaml,csv,html-v,--pivotpivottheview-X,--debugdebugmodeTests with DB2, Postgresql and MySQLconnect to database (-C)user/passwod can be specifed in URL/connection string. or explicitly specified.
if not specified, dbx will try to use current unix user; as to password, it will check environment variable dbx_password then will prompt.
in case ssl being used and not password needed, use 'dummy' for password.$# all in connection string$sretools-dbx-J~/jdbc/mariadb-java-client-2.7.1.jar-Dorg.mariadb.jdbc.Driver-U"jdbc:mysql://localhost:3306/mysql?user=yonghang&password=password&allowPublicKeyRetrieval=true&useSSL=false"-C# DBXServer 972045@/home/yonghang/.cache/sretools/.dbx.971825$ps-ef|grepdbx|grep-vgrep
yonghang97204512110:24pts/100:00:01/usr/bin/python3/usr/local/bin/sretools-dbx-J/home/yonghang/jdbc/mariadb-java-client-2.7.1.jar-Dorg.mariadb.jdbc.Driver-Ujdbc:mysql://localhost:3306/mysql?user=yonghang&password=password&allowPublicKeyRetrieval=true&useSSL=false-C

$# or$sretools-dbx-J~/jdbc/mariadb-java-client-2.7.1.jar-Dorg.mariadb.jdbc.Driver-U"jdbc:mysql://localhost:3306/mysql?allowPublicKeyRetrieval=true&useSSL=false"-uyonghang-ppassword-C# DBXServer 972146@/home/yonghang/.cache/sretools/.dbx.971825$ps-ef|grepdbx|grep-vgrep
yonghang97214618810:24pts/100:00:01/usr/bin/python3/usr/local/bin/sretools-dbx-J/home/yonghang/jdbc/mariadb-java-client-2.7.1.jar-Dorg.mariadb.jdbc.Driver-Ujdbc:mysql://localhost:3306/mysql?allowPublicKeyRetrieval=true&useSSL=false-uyonghang-ppassword-C


$# or$exportdbx_password=password
$sretools-dbx-J~/jdbc/mariadb-java-client-2.7.1.jar-Dorg.mariadb.jdbc.Driver-U"jdbc:mysql://localhost:3306/mysql?allowPublicKeyRetrieval=true&useSSL=false"-uyonghang-C# DBXServer 972374@/home/yonghang/.cache/sretools/.dbx.971825$ps-ef|grepdbx|grep-vgrep
yonghang97237413210:25pts/100:00:01/usr/bin/python3/usr/local/bin/sretools-dbx-J/home/yonghang/jdbc/mariadb-java-client-2.7.1.jar-Dorg.mariadb.jdbc.Driver-Ujdbc:mysql://localhost:3306/mysql?allowPublicKeyRetrieval=true&useSSL=false-uyonghang-Cdisconnect against databasesend "\q" will cause disconnection from the database.
note, dbx can get the SQL to run with -Q or from unix pipe.$ps-ef|grepdbx|grep-vgrep
yonghang9723741010:25pts/100:00:01/usr/bin/python3/usr/local/bin/sretools-dbx-J/home/yonghang/jdbc/mariadb-java-client-2.7.1.jar-Dorg.mariadb.jdbc.Driver-Ujdbc:mysql://localhost:3306/mysql?allowPublicKeyRetrieval=true&useSSL=false-uyonghang-C
$sretools-dbx-Q"\q"$ps-ef|grepdbx|grep-vgrep

$# or$ps-ef|grepdbx|grep-vgrep
yonghang97358618910:31pts/100:00:01/usr/bin/python3/usr/local/bin/sretools-dbx-J/home/yonghang/jdbc/mariadb-java-client-2.7.1.jar-Dorg.mariadb.jdbc.Driver-Ujdbc:mysql://localhost:3306/mysql?allowPublicKeyRetrieval=true&useSSL=false-uyonghang-C
$echo"\q"|sretools-dbx
$ps-ef|grepdbx|grep-vgrep
$Query databaseuse mysql as example$sretools-dbx-J/home/yonghang/jdbc/mariadb-java-client-2.7.1.jar-Dorg.mariadb.jdbc.Driver-U'jdbc:mysql://localhost:3306/mysql?user=yonghang&password=password&allowPublicKeyRetrieval=true&useSSL=false'-C# DBXServer 974814@/home/yonghang/.cache/sretools/.dbx.971825$echo"select user,host,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Create_priv,Drop_priv from mysql.user limit 2"|sretools-dbxuserhostSelect_privInsert_privUpdate_privDelete_privCreate_privCreate_privDrop_priv
-----------------------------------------------------------------------------------------------
root%YYYYYYY
yonghang%YYYYYYY
$# for very wide table, it's easier to pivot the result with -v$echo"select user,host,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Create_priv,Drop_priv from mysql.user limit 2"|sretools-dbx-v
user:root
host:%
Select_priv:Y
Insert_priv:Y
Update_priv:Y
Delete_priv:Y
Create_priv:Y
Create_priv:Y
Drop_priv:Y
--
user:yonghang
host:%
Select_priv:Y
Insert_priv:Y
Update_priv:Y
Delete_priv:Y
Create_priv:Y
Create_priv:Y
Drop_priv:Y
$$echo"select table_schema,table_name,view_definition from information_schema.views limit 2"|sretools-dbx
TABLE_SCHEMATABLE_NAMEVIEW_DEFINITION
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
sysversionselect'2.1.1'AS`sys_version`,version()AS`mysql_version`sysinnodb_buffer_stats_by_schemaselectif((locate('.',`ibp`.`TABLE_NAME`)=0),'InnoDB System',replace(substring_index(`ibp`.`TABLE_NAME`,'.',1),'`',''))AS`object_schema`,format_bytes(sum(if((`ibp`.`COMPRESSED_SIZE`=0),16384,`ibp`.`COMPRESSED_SIZE`)))AS`allocated`,format_bytes(sum(`ibp`.`DATA_SIZE`))AS`data`,count(`ibp`.`PAGE_NUMBER`)AS`pages`,count(if((`ibp`.`IS_HASHED`='YES'),1,NULL))AS`pages_hashed`,count(if((`ibp`.`IS_OLD`='YES'),1,NULL))AS`pages_old`,round((sum(`ibp`.`NUMBER_RECORDS`)/count(distinct`ibp`.`INDEX_NAME`)),0)AS`rows_cached`from`information_schema`.`INNODB_BUFFER_PAGE``ibp`where(`ibp`.`TABLE_NAME`isnotnull)groupby`object_schema`orderbysum(if((`ibp`.`COMPRESSED_SIZE`=0),16384,`ibp`.`COMPRESSED_SIZE`))desc
$$# limit max col width to 80 for better look$echo"select table_schema,table_name,view_definition from information_schema.views limit 2"|sretools-dbx-w80
TABLE_SCHEMATABLE_NAMEVIEW_DEFINITION
---------------------------------------------------------------------------------------------------------------------------
sysversionselect'2.1.1'AS`sys_version`,version()AS`mysql_version`sysinnodb_buffer_stats_by_schemaselectif((locate('.',`ibp`.`TABLE_NAME`)=0),'InnoDB System',replace(substring_index(`ibp`.`TABLE_NAME`,'.',1),'`',''))AS`object_schema`,format_bytes(sum(if((`ibp`.`COMPRESSED_SIZE`=0),16384,`ibp`.`COMPRESSED_SIZE`)))AS`allocated`,format_bytes(sum(`ibp`.`DATA_SIZE`))AS`data`,count(`ibp`.`PAGE_NUMBER`)AS`pages`,count(if((`ibp`.`IS_HASHED`='YES'),1,NULL))AS`pages_hashed`,count(if((`ibp`.`IS_OLD`='YES'),1,NULL))AS`pages_old`,round((sum(`ibp`.`NUMBER_RECORDS`)/count(distinct`ibp`.`INDEX_NAME`)),0)AS`rows_cached`from`information_schema`.`INNODB_BUFFER_PAGE``ibp`where(`ibp`.`TABLE_NAME`isnotnull)groupby`object_schema`orderbysum(if((`ibp`.`COMPRESSED_SIZE`=0),16384,`ibp`.`COMPRESSED_SIZE`))descin mysql, the view definition text is kind of '1 line' text which is not good for reading.  most likely we may have line breaks there.
below let's look at another example for the formatting part. the new example is with ibm-db2.$sretools-dbx-J/home/yonghang/jdbc/db2jcc4.jar-Dcom.ibm.db2.jcc.DB2Driver-U"jdbc:db2://localhost:50000/sample:user=db2inst1;password=db2inst1;"-C# DBXServer 978653@/home/yonghang/.cache/sretools/.dbx.971825$$echo"select viewschema,viewname,varchar(text) as text from syscat.views fetch first 1 rows only with ur"|sretools-dbxVIEWSCHEMAVIEWNAMETEXT
-------------------------------------------------------------------------------------------------------
SYSIBMCHECK_CONSTRAINTSCREATEORREPLACEVIEWSYSIBM.CHECK_CONSTRAINTS(CONSTRAINT_CATALOG,CONSTRAINT_SCHEMA,CONSTRAINT_NAME,CHECK_CLAUSE)ASSELECTCAST(CURRENTSERVERASVARCHAR(128)),TBCREATOR,CAST(NAMEASVARCHAR(128)),TEXTFROMSYSIBM.SYSCHECKSWHERETYPE='C'UNIONALLSELECTCAST(CURRENTSERVERASVARCHAR(128)),TBCREATOR,CAST(CONCAT(RTRIM(CONCAT(CHAR(CTIME),CHAR(FID))),RTRIM(CHAR(COLNO)))ASVARCHAR(128)),CAST(CONCAT(CONCAT('CHECK (',C.NAME),' IS NOT NULL)')ASCLOB(64K))FROMSYSIBM.SYSCOLUMNSC,SYSIBM.SYSTABLESTWHEREC.TBCREATOR=T.CREATORANDC.TBNAME=T.NAMEANDTYPEIN('U','T')ANDNULLS='N'export to JSON,YAML or HTML$echo"select viewschema,viewname,varchar(text) as text from syscat.views fetch first 1 rows only with ur"|sretools-dbx-Fjson[{"VIEWSCHEMA":"SYSIBM  ","VIEWNAME":"CHECK_CONSTRAINTS","TEXT":"CREATE OR REPLACE VIEW SYSIBM.CHECK_CONSTRAINTS \n(CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, CONSTRAINT_NAME, CHECK_CLAUSE) \nAS SELECT \nCAST(CURRENT SERVER AS VARCHAR(128)), TBCREATOR, \nCAST(NAME AS VARCHAR(128)), TEXT \nFROM SYSIBM.SYSCHECKS \nWHERE TYPE='C' \nUNION ALL \nSELECT CAST(CURRENT SERVER AS VARCHAR(128)), TBCREATOR, \nCAST(CONCAT(RTRIM(CONCAT(CHAR(CTIME), CHAR(FID) ) ), \nRTRIM(CHAR(COLNO)) ) AS VARCHAR(128) ), \nCAST(CONCAT(CONCAT('CHECK (', C.NAME), ' IS NOT NULL)') AS CLOB(64K) ) \nFROM SYSIBM.SYSCOLUMNS C, SYSIBM.SYSTABLES T \nWHERE C.TBCREATOR = T.CREATOR AND C.TBNAME = T.NAME AND TYPE IN('U', 'T') \nAND NULLS ='N'\n"}]$echo"select viewschema,viewname,varchar(text) as text from syscat.views fetch first 1 rows only with ur"|sretools-dbx-Fyaml
-TEXT:"CREATE OR REPLACE VIEW SYSIBM.CHECK_CONSTRAINTS \n(CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA,\\ CONSTRAINT_NAME, CHECK_CLAUSE) \nAS SELECT \nCAST(CURRENT SERVER AS VARCHAR(128)),\\ TBCREATOR, \nCAST(NAME AS VARCHAR(128)), TEXT \nFROM SYSIBM.SYSCHECKS \nWHERE\\ TYPE='C' \nUNION ALL \nSELECT CAST(CURRENT SERVER AS VARCHAR(128)), TBCREATOR,\\ \nCAST(CONCAT(RTRIM(CONCAT(CHAR(CTIME), CHAR(FID) ) ), \nRTRIM(CHAR(COLNO))\\ ) AS VARCHAR(128) ), \nCAST(CONCAT(CONCAT('CHECK (', C.NAME), ' IS NOT NULL)')\\ AS CLOB(64K) ) \nFROM SYSIBM.SYSCOLUMNS C, SYSIBM.SYSTABLES T \nWHERE C.TBCREATOR\\ = T.CREATOR AND C.TBNAME = T.NAME AND TYPE IN('U', 'T') \nAND NULLS ='N'\n"VIEWNAME:CHECK_CONSTRAINTSVIEWSCHEMA:'SYSIBM  '$$$echo"select viewschema,viewname,varchar(text) as text from syscat.views fetch first 1 rows only with ur"|sretools-dbx-Fhtml
<tableborder=1style="border-collapse:collapse;">
<tr>
<td><b>VIEWSCHEMA</b></td>
<td><b>VIEWNAME</b></td>
<td><b>TEXT</b></td>
</tr>
<tr>
<td>SYSIBM</td>
<td>CHECK_CONSTRAINTS</td>
<td>CREATEORREPLACEVIEWSYSIBM.CHECK_CONSTRAINTS<br>(CONSTRAINT_CATALOG,CONSTRAINT_SCHEMA,CONSTRAINT_NAME,CHECK_CLAUSE)<br>ASSELECT<br>CAST(CURRENTSERVERASVARCHAR(128)),TBCREATOR,<br>CAST(NAMEASVARCHAR(128)),TEXT<br>FROMSYSIBM.SYSCHECKS<br>WHERETYPE='C'<br>UNIONALL<br>SELECTCAST(CURRENTSERVERASVARCHAR(128)),TBCREATOR,<br>CAST(CONCAT(RTRIM(CONCAT(CHAR(CTIME),CHAR(FID))),<br>RTRIM(CHAR(COLNO)))ASVARCHAR(128)),<br>CAST(CONCAT(CONCAT('CHECK (',C.NAME),' IS NOT NULL)')ASCLOB(64K))<br>FROMSYSIBM.SYSCOLUMNSC,SYSIBM.SYSTABLEST<br>WHEREC.TBCREATOR=T.CREATORANDC.TBNAME=T.NAMEANDTYPEIN('U','T')<br>ANDNULLS='N'<br></td>
</tr>use plugin for sql shortcutplugin is YAML file including queries.  I made a random example from some SQL from internet as below.generated YAML file,mysql:db:usage:'SELECTs.schema_name,CONCAT(IFNULL(ROUND((SUM(t.data_length)+SUM(t.index_length))/1024/1024,2),0.00),"Mb")total_size,CONCAT(IFNULL(ROUND(((SUM(t.data_length)+SUM(t.index_length))-SUM(t.data_free))/1024/1024,2),0.00),"Mb")data_used,CONCAT(IFNULL(ROUND(SUM(data_free)/1024/1024,2),0.00),"Mb")data_free,IFNULL(ROUND((((SUM(t.data_length)+SUM(t.index_length))-SUM(t.data_free))/((SUM(t.data_length)+SUM(t.index_length)))*100),2),0)pct_usedFROMINFORMATION_SCHEMA.SCHEMATAs,INFORMATION_SCHEMA.TABLEStWHEREs.schema_name=t.table_schemaGROUPBYs.schema_nameORDERBYtotal_sizeDESC'perf:{}tablespace:usage:'SELECTs.schema_name,table_name,CONCAT(IFNULL(ROUND((SUM(t.data_length)+SUM(t.index_length))/1024/1024,2),0.00),"Mb")total_size,CONCAT(IFNULL(ROUND(((SUM(t.data_length)+SUM(t.index_length))-SUM(t.data_free))/1024/1024,2),0.00),"Mb")data_used,CONCAT(IFNULL(ROUND(SUM(data_free)/1024/1024,2),0.00),"Mb")data_free,IFNULL(ROUND((((SUM(t.data_length)+SUM(t.index_length))-SUM(t.data_free))/((SUM(t.data_length)+SUM(t.index_length)))*100),2),0)pct_usedFROMINFORMATION_SCHEMA.SCHEMATAs,INFORMATION_SCHEMA.TABLEStWHEREs.schema_name=t.table_schemaGROUPBYs.schema_name,table_nameORDERBYtotal_sizeDESC'user:select user,host from mysql.userthen load the YAML plugin with -P. This can be loaded when starting the connection then all following connections can use the commands defined.  Or you can specify the plugin for each run -- this also means you can use differnt plugins.$sretools-dbx-J~/jdbc/mariadb-java-client-2.7.1.jar-Dorg.mariadb.jdbc.Driver-U"jdbc:mysql://localhost:3306/mysql?user=yonghang&password=password&allowPublicKeyRetrieval=true&useSSL=false"-C# DBXServer 1071685@/home/yonghang/.cache/sretools/.dbx.1065306$echo"\mysql.user"|./dbx.py-Ptest/test.plugin.yaml
userhost
--------------------------
root%
yonghang%
mysql.infoschemalocalhost
mysql.sessionlocalhost
mysql.syslocalhost
rootlocalhost
$sretools-dbx-Ptest/test.plugin.yaml-J~/jdbc/mariadb-java-client-2.7.1.jar-Dorg.mariadb.jdbc.Driver-U"jdbc:mysql://localhost:3306/mysql?user=yonghang&password=password&allowPublicKeyRetrieval=true&useSSL=false"-C# DBXServer 1071773@/home/yonghang/.cache/sretools/.dbx.1065306$$echo"\mysql.user"|./dbx.pyuserhost
--------------------------
root%
yonghang%
mysql.infoschemalocalhost
mysql.sessionlocalhost
mysql.syslocalhost
rootlocalhost
$$echo"\mysql.db.usage"|./dbx.pySCHEMA_NAMEtotal_sizedata_useddata_freepct_used
----------------------------------------------------------
mysql7.70Mb-120.30Mb128.00Mb-1561.66
sys0.02Mb0.02Mb0.00Mb100.0
sample0.02Mb0.02Mb0.00Mb100.0
information_schema0.00Mb0.00Mb0.00Mb0.0
performance_schema0.00Mb0.00Mb0.00Mb0.0
$$echo"\mysql.tablespace.usage"|./dbx.pySCHEMA_NAMETABLE_NAMEtotal_sizedata_useddata_freepct_used
---------------------------------------------------------------------------------------------------------------
mysqltime_zone_transition4.52Mb0.52Mb4.00Mb11.42
mysqlhelp_topic1.61Mb-2.39Mb4.00Mb-148.54
mysqltime_zone_transition_type0.44Mb-3.56Mb4.00Mb-814.29
mysqltime_zone_name0.25Mb-3.75Mb4.00Mb-1500.0
mysqlhelp_keyword0.23Mb-3.77Mb4.00Mb-1606.67
mysqlhelp_relation0.08Mb-3.92Mb4.00Mb-5020.0
mysqltime_zone0.08Mb-3.92Mb4.00Mb-5020.0
mysqlglobal_grants0.05Mb-3.95Mb4.00Mb-8433.33
mysqldb0.03Mb-3.97Mb4.00Mb-12700.0
mysqlhelp_category0.03Mb-3.97Mb4.00Mb-12700.0
mysqlprocs_priv0.03Mb-3.97Mb4.00Mb-12700.0
mysqlproxies_priv0.03Mb-3.97Mb4.00Mb-12700.0
mysqltables_priv0.03Mb-3.97Mb4.00Mb-12700.0
mysqlcolumns_priv0.02Mb-3.98Mb4.00Mb-25500.0
mysqlcomponent0.02Mb-3.98Mb4.00Mb-25500.0
mysqldefault_roles0.02Mb-3.98Mb4.00Mb-25500.0
mysqlengine_cost0.02Mb-3.98Mb4.00Mb-25500.0
mysqlfunc0.02Mb-3.98Mb4.00Mb-25500.0
mysqlgtid_executed0.02Mb-3.98Mb4.00Mb-25500.0
mysqlinnodb_index_stats0.02Mb-3.98Mb4.00Mb-25500.0
mysqlinnodb_table_stats0.02Mb-3.98Mb4.00Mb-25500.0
mysqlpassword_history0.02Mb-3.98Mb4.00Mb-25500.0
......
performance_schemasession_connect_attrs0.00Mb0.00Mb0.00Mb0.0
performance_schemasession_status0.00Mb0.00Mb0.00Mb0.0
performance_schemasession_variables0.00Mb0.00Mb0.00Mb0.0
performance_schemasetup_actors0.00Mb0.00Mb0.00Mb0.0
performance_schemasetup_consumers0.00Mb0.00Mb0.00Mb0.0
$