app/server.rb in sqlui-0.1.24 vs app/server.rb in sqlui-0.1.25

- old
+ new

@@ -23,26 +23,23 @@ get '/-/health' do status 200 body 'OK' end + get '/?' do + redirect config.list_url_path, 301 + end + get "#{config.list_url_path}/?" do erb :databases, locals: { config: config } end config.database_configs.each do |database| - get database.url_path.to_s do - redirect "#{database.url_path}/app", 301 + get "#{database.url_path}/?" do + redirect "#{database.url_path}/query", 301 end - get "#{database.url_path}/app" do - @html ||= File.read(File.join(resources_dir, 'sqlui.html')) - status 200 - headers 'Content-Type': 'text/html' - body @html - end - get "#{database.url_path}/sqlui.css" do @css ||= File.read(File.join(resources_dir, 'sqlui.css')) status 200 headers 'Content-Type': 'text/css' body @css @@ -57,71 +54,78 @@ get "#{database.url_path}/metadata" do metadata = database.with_client do |client| { server: "#{config.name} - #{database.display_name}", + list_url_path: config.list_url_path, schemas: DatabaseMetadata.lookup(client, database), - saved: Dir.glob("#{database.saved_path}/*.sql").map do |path| - comment_lines = File.readlines(path).take_while do |l| + saved: Dir.glob("#{database.saved_path}/*.sql").to_h do |path| + contents = File.read(path) + comment_lines = contents.split("\n").take_while do |l| l.start_with?('--') end + filename = File.basename(path) description = comment_lines.map { |l| l.sub(/^-- */, '') }.join - { - filename: File.basename(path), - description: description - } + [ + filename, + { + filename: filename, + description: description, + contents: contents.strip + } + ] end } end status 200 headers 'Content-Type': 'application/json' body metadata.to_json end - get "#{database.url_path}/query_file" do - break client_error('missing file param') unless params[:file] - - file = File.join(database.saved_path, params[:file]) - break client_error('no such file') unless File.exist?(file) - - sql = File.read(file) - result = database.with_client do |client| - execute_query(client, sql).tap { |r| r[:file] = params[:file] } - end - - status 200 - headers 'Content-Type': 'application/json' - body result.to_json - end - post "#{database.url_path}/query" do params.merge!(JSON.parse(request.body.read, symbolize_names: true)) break client_error('missing sql') unless params[:sql] + full_sql = params[:sql] sql = params[:sql] - selection = params[:selection] - if selection - selection = selection.split(':').map { |v| Integer(v) } + if params[:selection] + selection = params[:selection] + if selection.include?('-') + # sort because the selection could be in either direction + selection = params[:selection].split('-').map { |v| Integer(v) }.sort + else + selection = Integer(selection) + selection = [selection, selection] + end sql = if selection[0] == selection[1] SqlParser.find_statement_at_cursor(params[:sql], selection[0]) else - params[:sql][selection[0], selection[1]] + full_sql[selection[0], selection[1]] end + break client_error("can't find query at selection") unless sql end result = database.with_client do |client| execute_query(client, sql) end result[:selection] = params[:selection] + result[:query] = full_sql status 200 headers 'Content-Type': 'application/json' body result.to_json end + + get(%r{#{Regexp.escape(database.url_path)}/(query|graph|structure|saved)}) do + @html ||= File.read(File.join(resources_dir, 'sqlui.html')) + status 200 + headers 'Content-Type': 'text/html' + body @html + end end error do |e| status 500 headers 'Content-Type': 'application/json' @@ -163,10 +167,9 @@ column_types = [] rows = [] columns = [] end { - query: sql, columns: columns, column_types: column_types, total_rows: rows.size, rows: rows.take(Sqlui::MAX_ROWS) }