Skip to content

Commit 41a8b99

Browse files
committed
WIP: ODBC
1 parent 27144dd commit 41a8b99

2 files changed

Lines changed: 19 additions & 27 deletions

File tree

ggsql-jupyter/src/connection.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ fn list_catalogs(reader: &dyn Reader) -> Result<Vec<ObjectSchema>, String> {
6565

6666
let col = df
6767
.column("catalog_name")
68-
.map_err(|e| format!("Missing catalog_name column: {}", e))?;
68+
.or_else(|_| df.column("name"))
69+
.map_err(|e| format!("Missing catalog_name/name column: {}", e))?;
6970

7071
let mut catalogs = Vec::new();
7172
for i in 0..df.height() {
@@ -88,7 +89,8 @@ fn list_schemas(reader: &dyn Reader, catalog: &str) -> Result<Vec<ObjectSchema>,
8889

8990
let col = df
9091
.column("schema_name")
91-
.map_err(|e| format!("Missing schema_name column: {}", e))?;
92+
.or_else(|_| df.column("name"))
93+
.map_err(|e| format!("Missing schema_name/name column: {}", e))?;
9294

9395
let mut schemas = Vec::new();
9496
for i in 0..df.height() {
@@ -115,10 +117,12 @@ fn list_tables(
115117

116118
let name_col = df
117119
.column("table_name")
118-
.map_err(|e| format!("Missing table_name column: {}", e))?;
120+
.or_else(|_| df.column("name"))
121+
.map_err(|e| format!("Missing table_name/name column: {}", e))?;
119122
let type_col = df
120123
.column("table_type")
121-
.map_err(|e| format!("Missing table_type column: {}", e))?;
124+
.or_else(|_| df.column("kind"))
125+
.map_err(|e| format!("Missing table_type/kind column: {}", e))?;
122126

123127
let mut objects = Vec::new();
124128
for i in 0..df.height() {

src/reader/odbc.rs

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,7 @@ pub enum OdbcVariant {
3838
impl super::SqlDialect for OdbcDialect {
3939
fn sql_list_catalogs(&self) -> String {
4040
match self.variant {
41-
OdbcVariant::Snowflake => {
42-
"SELECT database_name AS catalog_name \
43-
FROM snowflake.information_schema.databases \
44-
ORDER BY database_name"
45-
.into()
46-
}
41+
OdbcVariant::Snowflake => "SHOW DATABASES".into(),
4742
_ => {
4843
"SELECT DISTINCT catalog_name FROM information_schema.schemata \
4944
ORDER BY catalog_name"
@@ -53,17 +48,13 @@ impl super::SqlDialect for OdbcDialect {
5348
}
5449

5550
fn sql_list_schemas(&self, catalog: &str) -> String {
56-
let catalog = catalog.replace('\'', "''");
5751
match self.variant {
5852
OdbcVariant::Snowflake => {
5953
let catalog_ident = catalog.replace('"', "\"\"");
60-
format!(
61-
"SELECT schema_name \
62-
FROM \"{catalog_ident}\".information_schema.schemata \
63-
ORDER BY schema_name"
64-
)
54+
format!("SHOW SCHEMAS IN DATABASE \"{catalog_ident}\"")
6555
}
6656
_ => {
57+
let catalog = catalog.replace('\'', "''");
6758
format!(
6859
"SELECT DISTINCT schema_name FROM information_schema.schemata \
6960
WHERE catalog_name = '{catalog}' ORDER BY schema_name"
@@ -73,19 +64,17 @@ impl super::SqlDialect for OdbcDialect {
7364
}
7465

7566
fn sql_list_tables(&self, catalog: &str, schema: &str) -> String {
76-
let schema = schema.replace('\'', "''");
7767
match self.variant {
7868
OdbcVariant::Snowflake => {
7969
let catalog_ident = catalog.replace('"', "\"\"");
70+
let schema_ident = schema.replace('"', "\"\"");
8071
format!(
81-
"SELECT table_name, table_type \
82-
FROM \"{catalog_ident}\".information_schema.tables \
83-
WHERE table_schema = '{schema}' \
84-
ORDER BY table_name"
72+
"SHOW OBJECTS IN SCHEMA \"{catalog_ident}\".\"{schema_ident}\""
8573
)
8674
}
8775
_ => {
8876
let catalog = catalog.replace('\'', "''");
77+
let schema = schema.replace('\'', "''");
8978
format!(
9079
"SELECT DISTINCT table_name, table_type FROM information_schema.tables \
9180
WHERE table_catalog = '{catalog}' AND table_schema = '{schema}' \
@@ -96,20 +85,19 @@ impl super::SqlDialect for OdbcDialect {
9685
}
9786

9887
fn sql_list_columns(&self, catalog: &str, schema: &str, table: &str) -> String {
99-
let schema = schema.replace('\'', "''");
100-
let table = table.replace('\'', "''");
10188
match self.variant {
10289
OdbcVariant::Snowflake => {
10390
let catalog_ident = catalog.replace('"', "\"\"");
91+
let schema_ident = schema.replace('"', "\"\"");
92+
let table_ident = table.replace('"', "\"\"");
10493
format!(
105-
"SELECT column_name, data_type \
106-
FROM \"{catalog_ident}\".information_schema.columns \
107-
WHERE table_schema = '{schema}' AND table_name = '{table}' \
108-
ORDER BY ordinal_position"
94+
"SHOW COLUMNS IN TABLE \"{catalog_ident}\".\"{schema_ident}\".\"{table_ident}\""
10995
)
11096
}
11197
_ => {
11298
let catalog = catalog.replace('\'', "''");
99+
let schema = schema.replace('\'', "''");
100+
let table = table.replace('\'', "''");
113101
format!(
114102
"SELECT column_name, data_type FROM information_schema.columns \
115103
WHERE table_catalog = '{catalog}' AND table_schema = '{schema}' \

0 commit comments

Comments
 (0)