Skip to content

Commit

Permalink
Add BigQuery Adapter (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhynes authored Nov 23, 2023
1 parent fecf5a3 commit 738cfc2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ take on [dbext.vim][], improving on it on the following ways:
* All interaction is through invoking `:DB`, not 53 different commands and 35
different maps (omitting many of the more esoteric features, of course)
* Supports a modern array of backends, including NoSQL databases:
- Big Query
- ClickHouse
- Impala
- jq
Expand Down
9 changes: 7 additions & 2 deletions autoload/db.vim
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,13 @@ function! db#connect(url) abort
endif
let pattern = db#adapter#call(url, 'auth_pattern', [], 'auth\|login')
try
call writefile(split(db#adapter#call(url, 'auth_input', [], "\n"), "\n", 1), input, 'b')
let [out, exit_status] = call('s:systemlist', filter)
let auth_input = db#adapter#call(url, 'auth_input', [], "\n")
" Short-circuit the authentication if unnecessary for the adapter
if auth_input == v:false
return url
endif
call writefile(split(auth_input, "\n", 1), input, 'b')
let [out, exit_status] = call('s:systemlist', s:filter(url, input))
if exit_status && join(out, "\n") =~? pattern && resolved =~# '^[^:]*://[^:/@]*@'
let password = inputsecret('Password: ')
let url = substitute(resolved, '://[^:/@]*\zs@', ':'.db#url#encode(password).'@', '')
Expand Down
34 changes: 34 additions & 0 deletions autoload/db/adapter/bigquery.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function! db#adapter#bigquery#auth_input() abort
return v:false
endfunction

function! s:command_for_url(url, subcmd) abort
let cmd = ['bq']
let parsed = db#url#parse(a:url)
if has_key(parsed, 'opaque')
let host_targets = split(substitute(parsed.opaque, '/', '', 'g'), ':')

" If the host is specified as bigquery:project:dataset, then parse
" the optional (project, dataset) to supply them to the CLI.
if len(host_targets) == 2
call add(cmd, '--project_id=' . host_targets[0])
call add(cmd, '--dataset_id=' . host_targets[1])
elseif len(host_targets) == 1
call add(cmd, '--project_id=' . host_targets[0])
endif
endif

for [k, v] in items(parsed.params)
let op = '--'.k.'='.v
call add(cmd, op)
endfor
return cmd + [a:subcmd]
endfunction

function! db#adapter#bigquery#filter(url) abort
return s:command_for_url(a:url, 'query')
endfunction

function! db#adapter#bigquery#interactive(url) abort
return s:command_for_url(a:url, 'shell')
endfunction
11 changes: 11 additions & 0 deletions doc/dadbod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ Handling these parameters is up to the individual adapters. By default,
unrecognized parameters are ignored. Pass Boolean parameters as the literal
strings "true" and "false" (e.g., `?ssl=true`).

*dadbod-bigquery*
bigquery ~
>
bigquery:[<project_id>[:<dataset_id>]][?...]
bigquery:?project_id=<project_id>&dataset_id=<dataset_id>[&...]
<

The first form of the URL assumes your default dataset belongs to your default project. If it doesn't, use the second form.

Additional query parameters can be any valid global flag, for example, `&disable_ssl_validation=true`. Note that subcommand flags are not supported as query parameters, but they can be specified in `~/.bigqueryrc`.

*dadbod-dbext*
dbext ~
>
Expand Down

0 comments on commit 738cfc2

Please sign in to comment.