xo / usql

Universal command-line interface for SQL databases
MIT License
8.94k stars 352 forks source link

Add metadata catalog support database driver layer customization #369

Closed travelliu closed 2 years ago

travelliu commented 2 years ago

there will be differences in the information that different databases need to view. It is difficult to do complete unification.

PostgreSQL

go run ./  pg://postgres:root@127.0.0.1:5435/postgres\?sslmode=disable

Connected with driver postgres (PostgreSQL 13.4 (Debian 13.4-1.pgdg100+1))
Type "help" for help.

pg:postgres@127.0.0.1:5435/postgres=> \l
                                List of databases
  Catalog   |  Owner   | Encoding | Collate |   Ctype    |   Access privileges   
------------+----------+----------+---------+------------+-----------------------
 en_US.utf8 | postgres | UTF8     |         | en_US.utf8 |  
 en_US.utf8 | postgres | UTF8     |         | en_US.utf8 |  
 en_US.utf8 | postgres | UTF8     |         | en_US.utf8 |  
 en_US.utf8 | postgres | UTF8     |         | en_US.utf8 | =c/postgres          +
            |          |          |         |            | postgres=CTc/postgres 
 en_US.utf8 | postgres | UTF8     |         | en_US.utf8 | =c/postgres          +
            |          |          |         |            | postgres=CTc/postgres 
 en_US.utf8 | postgres | UTF8     |         | en_US.utf8 |  
(6 rows)
pg:postgres@127.0.0.1:5435/postgres=>  

Oracle:

go run -tags godror ./  gr://system:oracle@127.0.0.1:1521/mtk
Connected with driver godror (Oracle Database 19.0.0.0.0)
Type "help" for help.

gr:system@127.0.0.1:1521/mtk=> \l
List of databases
  Catalog  
-----------
 ORCL 
 SYS_HUB 
 TO_BAYMAX 
(3 rows)
gr:system@127.0.0.1:1521/mtk=>  
travelliu commented 2 years ago

will bring other problems

func (s CatalogSet) Get() *Catalog {
    return s.results[s.current-1].(*Catalog)
}

pg:mogdb@127.0.0.1:5436/postgres=> \dt pg_catalogpanic: interface conversion: metadata.Result is *mogdb.Catalog, not *metadata.Catalog

goroutine 24 [running]:
github.com/xo/usql/drivers/metadata.CatalogSet.Get(...)
        /Users/travel/Documents/Code/Enmotech/mogdb/usql/drivers/metadata/metadata.go:192
github.com/xo/usql/drivers/completer.completer.getNamespaces.func2({0x49df0c0?, 0xc0001280e8?})
        /Users/travel/Documents/Code/Enmotech/mogdb/usql/drivers/completer/completer.go:894 +0x85
travelliu commented 2 years ago

fix metadata.CatalogSet.Get(...)

go run ./  pg://postgres:root@127.0.0.1:5435/postgres\?sslmode=disable
Connected with driver postgres (PostgreSQL 13.4 (Debian 13.4-1.pgdg100+1))
Type "help" for help.

pg:postgres@127.0.0.1:5435/postgres=> \l
                                    List of databases
     Catalog      |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
------------------+----------+----------+------------+------------+-----------------------
 d1               | postgres | UTF8     | en_US.utf8 | en_US.utf8 |  
 d2               | postgres | UTF8     | en_US.utf8 | en_US.utf8 |  
 postgres         | postgres | UTF8     | en_US.utf8 | en_US.utf8 |  
 template0        | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
                  |          |          |            |            | postgres=CTc/postgres 
 template1        | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
                  |          |          |            |            | postgres=CTc/postgres 
 template_postgis | postgres | UTF8     | en_US.utf8 | en_US.utf8 |  
(6 rows)

pg:postgres@127.0.0.1:5435/postgres=> \d public.t1
                 table "public.t1"
 Name |         Type          | Nullable | Default 
------+-----------------------+----------+---------
 id   | bigint                | "YES"    |  
 col1 | character varying(10) | "YES"    |  
Indexes:
  "t1_index" index (col1)

go run -tags godror ./  gr://system:oracle@127.0.0.1:1521/mtk         
Connected with driver godror (Oracle Database 19.0.0.0.0)
Type "help" for help.

gr:system@127.0.0.1:1521/mtk=> \d ora_mtk.mtk_test
                                TABLE "ORA_MTK.MTK_TEST"
              Name              |               Type                | Nullable | Default 
--------------------------------+-----------------------------------+----------+---------
 COL_NUMBER                     | NUMBER                            | "YES"    |  
 COL_VARCHAR2                   | VARCHAR2                          | "YES"    |  
 COL_VARCHAR2_BYTE              | VARCHAR2                          | "YES"    |  
 COL_VARCHAR2_CHAR              | VARCHAR2                          | "YES"    |  
 COL_DATE                       | DATE                              | "YES"    |  
 COL_CLOB                       | CLOB                              | "YES"    |  
 COL_BLOB                       | BLOB                              | "YES"    |