From 481cee3c2d82231c4ba839fa86d4d58d6d620c8e Mon Sep 17 00:00:00 2001 From: nlinaje Date: Fri, 24 May 2024 06:35:40 +0200 Subject: [PATCH] feat(oracle): provide toggle layout functionality --- autoload/db_ui/dbout.vim | 15 +++++++++++- autoload/db_ui/schemas.vim | 47 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/autoload/db_ui/dbout.vim b/autoload/db_ui/dbout.vim index 3948ec8..bf65b19 100644 --- a/autoload/db_ui/dbout.vim +++ b/autoload/db_ui/dbout.vim @@ -86,6 +86,20 @@ function! db_ui#dbout#toggle_layout() abort endif let content = join(readfile(b:db.input), "\n") let expanded_layout = get(b:, 'db_ui_expanded_layout', 0) + let layout_query = '' + let filetype = get(scheme, 'filetype', '') + + if filetype == 'plsql' + let layout_query = get(scheme, 'layout_query') + " Extract SELECT statement from content (it gets only the first one) + let content = matchstr(content, '\vSELECT.{-};') + let content = substitute(content, ';\?$', ' '.scheme.layout_flag, '') + let content = substitute(content, '\n', ' ', 'g') + let content = substitute(content, "'", "''", "g") + let content = layout_query . "exec print_cols_as_rows('".content."');\nset feedback off;\ndrop procedure print_cols_as_rows;\n" + else + let content = substitute(content, ';\?$', ' '.scheme.layout_flag, '') + endif if expanded_layout let b:db_ui_expanded_layout = !expanded_layout @@ -93,7 +107,6 @@ function! db_ui#dbout#toggle_layout() abort return endif - let content = substitute(content, ';\?$', ' '.scheme.layout_flag, '') let tmp = tempname() call writefile(split(content, "\n"), tmp) let old_db_input = b:db.input diff --git a/autoload/db_ui/schemas.vim b/autoload/db_ui/schemas.vim index b0cf30d..871e6e4 100644 --- a/autoload/db_ui/schemas.vim +++ b/autoload/db_ui/schemas.vim @@ -118,6 +118,51 @@ let s:oracle_foreign_key_query = " \ WHERE CON.constraint_type = 'R' \ AND U.common = 'NO' \ AND RFRING.column_name = '{col_name}'" +let s:oracle_toggle_layout_query = " + \ set feedback off \n + \ create or replace procedure print_cols_as_rows( p_query in varchar2 )\n + \ AUTHID CURRENT_USER is\n + \ l_descriptionTable dbms_sql.desc_tab;\n + \ l_execStatus integer;\n + \ l_columnCount integer;\n + \ l_rowCount integer;\n + \ l_currentColumnLength integer;\n + \ l_maxColumnLength integer := 0;\n + \ l_theCursor integer default dbms_sql.open_cursor;\n + \ l_columnValue varchar2(4000);\n + \ dash_line varchar2(30) := rpad('-', 30, '-'); + \ begin \n + \ dbms_sql.parse(l_theCursor, p_query, dbms_sql.native);\n + \ dbms_sql.describe_columns(l_theCursor, l_columnCount, l_descriptionTable);\n + \ + \ for i in 1 .. l_columnCount loop\n + \ dbms_sql.define_column(l_theCursor, i, l_columnValue, 4000);\n + \ l_currentColumnLength := LENGTH(l_descriptionTable(i).col_name);\n + \ if l_currentColumnLength > l_maxColumnLength then\n + \ l_maxColumnLength := l_currentColumnLength;\n + \ end if;\n + \ end loop;\n + \ + \ l_execStatus := dbms_sql.execute(l_theCursor);\n + \ l_rowCount := 0;\n + \ while ( dbms_sql.fetch_rows(l_theCursor) > 0 ) loop\n + \ l_rowCount := l_rowCount + 1;\n + \ dbms_output.put_line( dash_line || ' ' || l_rowCount || '. row ' || dash_line);\n + \ for i in 1 .. l_columnCount loop\n + \ dbms_sql.column_value(l_theCursor, i, l_columnValue );\n + \ dbms_output.put_line(rpad(l_descriptionTable(i).col_name, + \ l_maxColumnLength + 1) + \ || ': ' || l_columnValue );\n + \ end loop;\n + \ end loop;\n + \ if l_rowCount = 0 then\n + \ dbms_output.put_line('no rows found');\n + \ end if;\n + \ end;\n + \ /\n + \ + \ set feedback on \n + \ set serveroutput on \n" let s:oracle_schemes_tables_query = " \SELECT /*csv*/ T.owner, T.table_name \ FROM ( @@ -136,6 +181,8 @@ let s:oracle = { \ 'default_scheme': '', \ 'foreign_key_query': printf(s:oracle_args, s:oracle_foreign_key_query), \ 'has_virtual_results': v:true, + \ 'layout_flag': '', + \ 'layout_query': s:oracle_toggle_layout_query, \ 'parse_results': {results, min_len -> s:results_parser(results[3:], '\s\s\+', min_len)}, \ 'parse_virtual_results': {results, min_len -> s:results_parser(results[3:], '\s\s\+', min_len)}, \ 'requires_stdin': v:true,