@@ -62,41 +62,16 @@ def connect(self, config: ConnectionConfig) -> Any:
6262 "database" : config .database ,
6363 }
6464
65- # Handle optional fields if they exist in config.extra or explicitly defined
66- # Since we added them to schema, they should be in config dictionary if accessed correctly,
67- # but ConnectionConfig usually stores extra fields in a flexible way or we need to access them specifically.
68- # ConnectionConfig is a Pydantic model or dataclass. Let's assume standard fields.
69- # For extra fields defined in schema but not in ConnectionConfig core fields, they might be in `config` if it's a dict,
70- # but here `config` is an object.
71- # Looking at `sqlit/config.py` would confirm, but usually extra fields are passed differently or
72- # we might need to check how `sqlit` handles schema-specific fields.
73- # For now, I'll access standard fields. If `warehouse` is stored in `extra`, I need to know how to access it.
74- # Let's assume for now they might be passed via some mechanism or we stick to standard args.
75- # However, `ConnectionConfig` likely has an `extras` dict or similar?
76-
77- # Let's check `config` object structure in `sqlit/config.py`.
78- # I'll rely on the user providing them in the specific fields if I can access them.
79-
80- # NOTE: Without checking `ConnectionConfig` definition, I'll assume I can access extras.
81- # But wait, `ConnectionConfig` is imported in `base.py`. Let's check it in a separate turn if needed.
82- # For now, I'll assume standard connection.
83-
8465 # Additional args from our schema:
8566 # warehouse, schema, role.
86- # If the config object allows dynamic attribute access or has a dict method, we can use that.
87- # I'll try to pull them from `config` assuming it might have them or `extra` dict.
88-
89- extras = getattr (config , "extras" , {}) or {}
67+ extras = config .options
9068 if "warehouse" in extras :
9169 connect_args ["warehouse" ] = extras ["warehouse" ]
9270 if "schema" in extras :
9371 connect_args ["schema" ] = extras ["schema" ]
9472 if "role" in extras :
9573 connect_args ["role" ] = extras ["role" ]
9674
97- # Also check if they are top-level attributes if `ConnectionConfig` is dynamic (unlikely).
98- # But let's look at `sqlit/config.py` later.
99-
10075 return sf .connect (** connect_args )
10176
10277 def get_databases (self , conn : Any ) -> list [str ]:
@@ -107,34 +82,20 @@ def get_databases(self, conn: Any) -> list[str]:
10782
10883 def get_tables (self , conn : Any , database : str | None = None ) -> list [TableInfo ]:
10984 """Get list of tables."""
110- cursor = conn .cursor ()
111- # Snowflake doesn't support changing database in connection easily for query context without USE.
112- # But we can query information_schema or SHOW TABLES.
113- # SHOW TABLES IN DATABASE ...
114-
115- query = "SHOW TABLES"
116- if database :
117- query += f" IN DATABASE { self .quote_identifier (database )} "
85+ # Use information_schema for robustness across versions
86+ return self .get_tables_via_info_schema (conn , database )
11887
119- cursor .execute (query )
120- # SHOW TABLES returns: created_on, name, database_name, schema_name, ...
121- # We need (schema, name)
122- return [(row [3 ], row [1 ]) for row in cursor .fetchall ()]
88+ def get_tables_via_info_schema (self , conn : Any , database : str | None = None ) -> list [TableInfo ]:
89+ """Fallback or alternative to get tables."""
90+ cursor = conn .cursor ()
91+ db_prefix = f"{ self .quote_identifier (database )} ." if database else ""
92+ sql = f"SELECT table_schema, table_name FROM { db_prefix } information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema != 'INFORMATION_SCHEMA' ORDER BY table_schema, table_name"
93+ cursor .execute (sql )
94+ return [(row [0 ], row [1 ]) for row in cursor .fetchall ()]
12395
12496 def get_views (self , conn : Any , database : str | None = None ) -> list [TableInfo ]:
12597 """Get list of views."""
12698 cursor = conn .cursor ()
127- query = "SHOW VIEWS"
128- if database :
129- query += f" IN DATABASE { self .quote_identifier (database )} "
130- cursor .execute (query )
131- # SHOW VIEWS returns similar structure: ..., name, ..., schema_name, ...
132- # Check column index for SHOW VIEWS. Usually: created_on, name, kind, database_name, schema_name
133- # Actually it's best to use INFORMATION_SCHEMA for consistency if possible, but SHOW commands are faster in Snowflake sometimes.
134- # Let's check column indices or use dict cursor if available? No, usually list.
135- # SHOW TABLES: 1=name, 3=schema_name
136- # SHOW VIEWS: 1=name, 4=schema_name (need verification, varies by version)
137-
13899 # Alternative: INFORMATION_SCHEMA
139100 sql = "SELECT table_schema, table_name FROM information_schema.views"
140101 if database :
@@ -145,18 +106,6 @@ def get_views(self, conn: Any, database: str | None = None) -> list[TableInfo]:
145106 cursor .execute (sql )
146107 return [(row [0 ], row [1 ]) for row in cursor .fetchall ()]
147108
148- def get_tables_via_info_schema (self , conn : Any , database : str | None = None ) -> list [TableInfo ]:
149- """Fallback or alternative to get tables."""
150- cursor = conn .cursor ()
151- db_prefix = f"{ self .quote_identifier (database )} ." if database else ""
152- sql = f"SELECT table_schema, table_name FROM { db_prefix } information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema != 'INFORMATION_SCHEMA' ORDER BY table_schema, table_name"
153- cursor .execute (sql )
154- return [(row [0 ], row [1 ]) for row in cursor .fetchall ()]
155-
156- # I'll stick to INFORMATION_SCHEMA for robustness across versions unless slow.
157- def get_tables (self , conn : Any , database : str | None = None ) -> list [TableInfo ]:
158- return self .get_tables_via_info_schema (conn , database )
159-
160109 def get_columns (
161110 self , conn : Any , table : str , database : str | None = None , schema : str | None = None
162111 ) -> list [ColumnInfo ]:
@@ -191,8 +140,13 @@ def get_columns(
191140 # Note: cursor might be consumed.
192141 rows = cursor .fetchall ()
193142
194- cursor .execute (pk_sql , (schema , table ))
195- pk_columns = {row [0 ] for row in cursor .fetchall ()}
143+ pk_columns = set ()
144+ try :
145+ cursor .execute (pk_sql , (schema , table ))
146+ pk_columns = {row [0 ] for row in cursor .fetchall ()}
147+ except Exception :
148+ # Fallback if TABLE_CONSTRAINTS/KEY_COLUMN_USAGE is not available (e.g. insufficient privs or fakesnow)
149+ pass
196150
197151 return [
198152 ColumnInfo (name = row [0 ], data_type = row [1 ], is_primary_key = row [0 ] in pk_columns )
0 commit comments