Ashhar commited on
Commit
4380c2b
·
1 Parent(s): af1fee3

give view support

Browse files
Files changed (1) hide show
  1. app.py +34 -12
app.py CHANGED
@@ -239,8 +239,12 @@ def execute_query(query):
239
  try:
240
  start_time = time.time()
241
  with st.spinner("Executing SQL query..."):
242
- with st.session_state.engine.connect() as conn:
243
- df = pd.read_sql(query, conn)
 
 
 
 
244
  execution_time = time.time() - start_time
245
  pprint(f"[Query Execution] Latency: {execution_time:.2f}s")
246
  return df
@@ -325,27 +329,45 @@ if connection_string and connection_string != st.session_state.connection_string
325
 
326
  # Table Selection Section
327
  if st.session_state.connection_string:
328
- st.header("2. Table Selection")
329
  inspector = inspect(st.session_state.engine)
 
 
330
  tables = inspector.get_table_names()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
331
 
332
- # Set default index to 'lsq_leads' if present, otherwise 0
333
- default_index = tables.index('lsq_leads') if 'lsq_leads' in tables else 0
334
- selected_table = st.selectbox("Select a table", tables, index=default_index)
 
335
 
336
  # Create containers for schema and data
337
  schema_container = st.container()
338
  data_container = st.container()
339
 
340
- # Always load table data if we have a selected table
341
- if selected_table:
342
  # Update session state
343
- if selected_table != st.session_state.selected_table:
344
- st.session_state.selected_table = selected_table
345
 
346
  # Always fetch schema and sample data
347
- st.session_state.table_schema = get_table_schema(selected_table)
348
- st.session_state.sample_data = get_sample_data(selected_table)
349
 
350
  # Always display schema and sample data if available
351
  with schema_container:
 
239
  try:
240
  start_time = time.time()
241
  with st.spinner("Executing SQL query..."):
242
+ # Create a connection and begin a transaction
243
+ with st.session_state.engine.begin() as conn:
244
+ # Execute the query using text() to ensure proper SQL compilation
245
+ result = conn.execute(text(query))
246
+ # Convert the result to a pandas DataFrame
247
+ df = pd.DataFrame(result.fetchall(), columns=result.keys())
248
  execution_time = time.time() - start_time
249
  pprint(f"[Query Execution] Latency: {execution_time:.2f}s")
250
  return df
 
329
 
330
  # Table Selection Section
331
  if st.session_state.connection_string:
332
+ st.header("2. Database Object Selection")
333
  inspector = inspect(st.session_state.engine)
334
+
335
+ # Get both tables and views
336
  tables = inspector.get_table_names()
337
+ views = inspector.get_view_names()
338
+
339
+ # Create a list of tuples with (name, type) for all database objects
340
+ db_objects = [(table, 'Table') for table in tables] + [(view, 'View') for view in views]
341
+ db_objects.sort(key=lambda x: x[0]) # Sort alphabetically by name
342
+
343
+ # Create two columns for the selection
344
+ col1, col2 = st.columns([3, 1])
345
+
346
+ with col1:
347
+ # Extract just the names for the selectbox
348
+ object_names = [obj[0] for obj in db_objects]
349
+ # Set default index to 'lsq_leads' if present, otherwise 0
350
+ default_index = object_names.index('lsq_leads') if 'lsq_leads' in object_names else 0
351
+ selected_object = st.selectbox("Select a table or view", object_names, index=default_index)
352
 
353
+ with col2:
354
+ # Display the object type (Table/View)
355
+ object_type = next(obj_type for obj_name, obj_type in db_objects if obj_name == selected_object)
356
+ st.text_input("Type", value=object_type, disabled=True)
357
 
358
  # Create containers for schema and data
359
  schema_container = st.container()
360
  data_container = st.container()
361
 
362
+ # Always load object data if we have a selection
363
+ if selected_object:
364
  # Update session state
365
+ if selected_object != st.session_state.selected_table:
366
+ st.session_state.selected_table = selected_object
367
 
368
  # Always fetch schema and sample data
369
+ st.session_state.table_schema = get_table_schema(selected_object)
370
+ st.session_state.sample_data = get_sample_data(selected_object)
371
 
372
  # Always display schema and sample data if available
373
  with schema_container: