Skip to content

Display total table relation size in the drawer #257

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 4 commits 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
62 changes: 56 additions & 6 deletions autoload/db_ui/drawer.vim
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,27 @@ function! s:drawer.add_db(db) abort
endif
endfunction

function! s:drawer.get_table_label(table, tables, level)
if !g:db_ui_show_size || !self.show_details
let name = a:table
else
Comment on lines +462 to +464
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just return early here and avoid having an else block:

Suggested change
if !g:db_ui_show_size || !self.show_details
let name = a:table
else
if !g:db_ui_show_size || !self.show_details
return a:table
endif

let icon = self.get_toggle_icon('table', a:tables.items[a:table])
let label = repeat(' ', shiftwidth() * a:level).icon.(!empty(icon) ? " " : "")
if strchars(label) + strchars(a:table) > g:db_ui_winwidth - 8
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not rely on db_ui_winwidth variable for this. Table names can be longer than the width.
We should find the longest table name and use that as a reference for padding.
We also need to put the values in parenthesis so it's picked up by highlighter.

This should be enough for the current else block:

  let longest = max(map(copy(a:tables.list), 'strchars(v:val)'))
  return printf('%-'.longest.'s (%s)', a:table, a:tables.items[a:table].size)

let name = strpart(a:table, 0, g:db_ui_winwidth - strchars(label) - 8).repeat(' ', 8 - len(a:tables.items[a:table].size)).a:tables.items[a:table].size
else
let name = a:table.repeat(' ', g:db_ui_winwidth - strchars(label) - strchars(a:table) - len(a:tables.items[a:table].size)).a:tables.items[a:table].size
endif
endif
return name
endfunction

function! s:drawer.render_tables(tables, db, path, level, schema) abort
if !a:tables.expanded
return
endif
for table in a:tables.list
call self.add(table, 'toggle', a:path.'->'.table, self.get_toggle_icon('table', a:tables.items[table]), a:db.key_name, a:level, { 'expanded': a:tables.items[table].expanded })
call self.add(self.get_table_label(table, a:tables, a:level), 'toggle', a:path.'->'.table, self.get_toggle_icon('table', a:tables.items[table]), a:db.key_name, a:level, { 'expanded': a:tables.items[table].expanded })
if a:tables.items[table].expanded
for [helper_name, helper] in items(a:db.table_helpers)
call self.add(helper_name, 'open', 'table', g:db_ui_icons.tables, a:db.key_name, a:level + 1, {'table': table, 'content': helper, 'schema': a:schema })
Expand Down Expand Up @@ -628,10 +643,10 @@ function! s:drawer.populate_tables(db) abort
return a:db
endfunction

function! s:drawer.populate_table_items(tables) abort
function! s:drawer.populate_table_items(tables, sizes_by_table) abort
for table in a:tables.list
if !has_key(a:tables.items, table)
let a:tables.items[table] = {'expanded': 0 }
let a:tables.items[table] = {'expanded': 0, 'size': a:sizes_by_table[table] }
endif
endfor
endfunction
Expand All @@ -643,10 +658,20 @@ function! s:drawer.populate_schemas(db) abort
endif
let scheme = db_ui#schemas#get(a:db.scheme)
let schemas = scheme.parse_results(db_ui#schemas#query(a:db, scheme, scheme.schemes_query), 1)
let tables = scheme.parse_results(db_ui#schemas#query(a:db, scheme, scheme.schemes_tables_query), 2)
if !g:db_ui_show_size || !has_key(scheme, 'schemes_tables_size_query')
let tables = scheme.parse_results(db_ui#schemas#query(a:db, scheme, scheme.schemes_tables_query), 2)
else
let tables = scheme.parse_results(db_ui#schemas#query(a:db, scheme, scheme.schemes_tables_size_query), 3)
endif
let schemas = filter(schemas, {i, v -> !self._is_schema_ignored(v)})
let tables_by_schema = {}
for [scheme_name, table] in tables
let sizes_by_table = {}
for item in tables
if len(item) == 2
call add(item, '')
endif
endfor
for [scheme_name, table, size] in tables
if self._is_schema_ignored(scheme_name)
continue
endif
Expand All @@ -655,6 +680,31 @@ function! s:drawer.populate_schemas(db) abort
endif
call add(tables_by_schema[scheme_name], table)
call add(a:db.tables.list, table)
if size != ''
let num = str2nr(size, 10)
if num > 1099511627776
let num = num / 1073741824
let div = num % 1024 / 100
let num = num / 1024
let val = "TB"
elseif num > 1073741824
let num = num / 1048576
let div = num % 1024 / 100
let num = num / 1024
let val = "GB"
elseif num > 1048576
let num = num / 1024
let div = num % 1024 / 100
let num = num / 1024
let val = "MB"
else
let div = num % 1024 / 100
let num = num / 1024
let val = "KB"
endif
let size = num.'.'.div[0].' '.val
endif
let sizes_by_table[table] = size
endfor
let a:db.schemas.list = schemas
for schema in schemas
Expand All @@ -670,7 +720,7 @@ function! s:drawer.populate_schemas(db) abort

endif
let a:db.schemas.items[schema].tables.list = sort(get(tables_by_schema, schema, []))
call self.populate_table_items(a:db.schemas.items[schema].tables)
call self.populate_table_items(a:db.schemas.items[schema].tables, sizes_by_table)
endfor
return a:db
endfunction
Expand Down
33 changes: 28 additions & 5 deletions autoload/db_ui/schemas.vim
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,27 @@ let s:postgres_list_schema_query = "
\ order by nspname"

if empty(g:db_ui_use_postgres_views)
let postgres_tables_and_views = "
\ SELECT table_schema, table_name FROM information_schema.tables ;"
let postgres_tables_and_views = '
\ SELECT table_schema, table_name FROM information_schema.tables ;'
let postgres_tables_and_views_size = '
\ SELECT table_schema, table_name, pg_total_relation_size(''"''||table_schema||''"."''||table_name||''"'') FROM information_schema.tables ;'

else
let postgres_tables_and_views = "
\ SELECT table_schema, table_name FROM information_schema.tables UNION ALL
\ select schemaname, matviewname from pg_matviews;"
let postgres_tables_and_views = '
\ SELECT table_schema, table_name FROM information_schema.tables UNION ALL SELECT schemaname, matviewname from pg_matviews ;'
let postgres_tables_and_views_size = '
\ SELECT table_schema, table_name, pg_total_relation_size(''"''||table_schema||''"."''||table_name||''"'') FROM information_schema.tables UNION ALL SELECT schemaname, matviewname, null from pg_matviews ;'

endif
let s:postgres_tables_and_views = postgres_tables_and_views
let s:postgres_tables_and_views_size = postgres_tables_and_views_size

let s:postgresql = {
\ 'args': ['-A', '-c'],
\ 'foreign_key_query': s:postgres_foreign_key_query,
\ 'schemes_query': s:postgres_list_schema_query,
\ 'schemes_tables_query': s:postgres_tables_and_views,
\ 'schemes_tables_size_query': s:postgres_tables_and_views_size,
\ 'select_foreign_key_query': 'select * from "%s"."%s" where "%s" = %s',
\ 'cell_line_number': 2,
\ 'cell_line_pattern': '^-\++-\+',
Expand Down Expand Up @@ -78,6 +85,22 @@ let s:sqlserver = {
\ 'foreign_key_query': trim(s:sqlserver_foreign_keys_query),
\ 'schemes_query': 'SELECT schema_name FROM INFORMATION_SCHEMA.SCHEMATA',
\ 'schemes_tables_query': 'SELECT table_schema, table_name FROM INFORMATION_SCHEMA.TABLES',
\ 'schemes_tables_size_query': 'SELECT
\ s.name AS schema_name,
\ t.name AS table_name,
\ SUM(a.total_pages) * 8 * 1024 AS total_bytes
\ FROM
\ sys.tables t
\ INNER JOIN
\ sys.indexes i ON t.object_id = i.object_id
\ INNER JOIN
\ sys.partitions p ON i.object_id = p.object_id AND i.index_id = p.index_id
\ INNER JOIN
\ sys.allocation_units a ON p.partition_id = a.container_id
\ INNER JOIN
\ sys.schemas s ON t.schema_id = s.schema_id
\ GROUP BY
\ t.name, s.name, p.rows',
\ 'select_foreign_key_query': 'select * from %s.%s where %s = %s',
\ 'cell_line_number': 2,
\ 'cell_line_pattern': '^-\+.-\+',
Expand Down
9 changes: 9 additions & 0 deletions doc/dadbod-ui.txt
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,15 @@ g:db_ui_winwidth

Default value: `40`

*g:db_ui_show_size*
g:db_ui_show_size
Toggle query of total table relation size.
Use `H` to toggle on and off in the drawer.
Currently supports postgres and mssql.

Default value: `0`


*g:db_ui_win_position*
g:db_ui_win_position
On which side of the screen should DBUI drawer open.
Expand Down
1 change: 1 addition & 0 deletions plugin/db_ui.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ let g:db_ui_disable_progress_bar = get(g:, 'db_ui_disable_progress_bar', 0)
let g:db_ui_use_postgres_views = get(g:, 'db_ui_use_postgres_views', 1)
let g:db_ui_notification_width = get(g:, 'db_ui_notification_width', 40)
let g:db_ui_winwidth = get(g:, 'db_ui_winwidth', 40)
let g:db_ui_show_size = get(g:, 'db_ui_show_size', 0)
let g:db_ui_win_position = get(g:, 'db_ui_win_position', 'left')
let g:db_ui_default_query = get(g:, 'db_ui_default_query', 'SELECT * from "{table}" LIMIT 200;')
let g:db_ui_save_location = get(g:, 'db_ui_save_location', '~/.local/share/db_ui')
Expand Down
Loading