Class: RailsDbBrowser::DbBrowser

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/rails_db_browser/db_browser.rb

Constant Summary collapse

DEFAULT_QUERY =
'select * from'

Instance Method Summary collapse

Instance Method Details

#extract_fields_from_special(sql) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rails_db_browser/db_browser.rb', line 113

def extract_fields_from_special(sql)
  fields = []
  sql = sql.gsub(/\[\[([\w.]+)\.\*(?:\s*-\s*((?:\w+[,\s]+)*(?:\w+))\s*)?\]\]/) do 
    table, without = $1, ($2 || '').scan(/\w+/)
    table_fields = column_names(table) - without

    table_fields.map{|fld|
      as = "#{table}.#{fld}"
      fields << as
      "#{quote_table_name(table)}.#{quote_column_name(fld)} as #{quote_column_name(as)}"
    }.join(',')
  end
  [ sql, fields ]
end

#quote(v) ⇒ Object



105
106
107
# File 'lib/rails_db_browser/db_browser.rb', line 105

def quote(v)
  connection.quote(v)
end

#quote_column_name(c) ⇒ Object



101
102
103
# File 'lib/rails_db_browser/db_browser.rb', line 101

def quote_column_name(c)
  connection.quote_column_name(c)
end

#quote_table_name(t) ⇒ Object



97
98
99
# File 'lib/rails_db_browser/db_browser.rb', line 97

def quote_table_name(t)
  connection.quote_table_name(t)
end

#select_all(sql, fields = nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rails_db_browser/db_browser.rb', line 128

def select_all(sql, fields=nil)
  set_default_perpage
  unless fields.present?
    sql, fields = extract_fields_from_special(sql)
  end
  connection.query(sql,
                   :perpage => params[:perpage],
                   :page    => params[:page],
                   :fields  => fields
                  )
end

#set_default_perpageObject



109
110
111
# File 'lib/rails_db_browser/db_browser.rb', line 109

def set_default_perpage
  params['perpage'] = '25' unless params.has_key?('perpage')
end