require_dependency "dbconsole/application_controller" module Dbconsole class DatabasesController < ApplicationController def index #env = ENV["RAILS_ENV"] @tables = ActiveRecord::Base.connection.tables @tables.delete_if {|value| value == "schema_migrations" } #@connection = ActiveRecord::Base.establish_connection( # :adapter => "sqlite3", # :database => "db/development.sqlite3", #) #@tables = @connection.connection.tables #ActiveRecord::Base.connection.tables.map do |model| #puts model.capitalize.singularize.camelize #end #sql = "SHOW TABLES" #@result = @connection.connection.execute(sql); #@result.each(:as => :hash) do |row| # puts row #end respond_to do |format| format.html # index.html.erb #format.json { render json: @tables } end end def show @colinfo = Hash.new("") @colidinfo = Hash.new("") @tables = ActiveRecord::Base.connection.tables @tables.delete_if {|value| value == "schema_migrations" } conn = ActiveRecord::Base.connection @class_name_org = params[:id] @class_name= params[:id].capitalize.singularize.camelize.constantize @col_names= @class_name.column_names @col_names.each_with_index do | col_name, index| @colinfo[col_name] = @class_name.columns_hash[col_name].type @colidinfo[index] = col_name end @records = conn.execute("select * from "+params[:id]) #@records =@class_name.all puts @records respond_to do |format| format.html # show.html.erb format.json { render json: @records } end end def destroy @class_name = params[:tablename].capitalize.singularize.camelize.constantize if @class_name.delete(params[:recordid].to_i) == 1 flash[:notice] = "Record successfully deleted" end redirect_to({ :action=>'show', :id => params[:tablename] }) end def update end def create @class_name_org = params[:tablename] @class_name= params[:tablename].capitalize.singularize.camelize.constantize @col_names= @class_name.column_names @tabinst = @class_name.new @col_names.each_with_index do | col_name, index| @tabinst[col_name] = params[col_name] end if @tabinst.save flash[:notice] = "Record successfully inserted and id is "+@tabinst.id.to_s else flash[:notice] = "Record not successfully inserted" end redirect_to({ :action=>'show', :id => params[:tablename] }) end end end