Skip to content

feat(oracle): provide toggle layout functionality #239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion autoload/db_ui/dbout.vim
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,27 @@ 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
norm R
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
Expand Down
47 changes: 47 additions & 0 deletions autoload/db_ui/schemas.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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,
Expand Down