Class: QueriesController

Inherits:
ApplicationController
  • Object
show all
Defined in:
lib/generators/templates/controllers/queries_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /queries POST /queries.json



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/generators/templates/controllers/queries_controller.rb', line 40

def create
  @query = Query.new(params[:query])
  # update,insert文だと思われる場合は登録を禁止する。
  if QueryStorage.is_insert_or_update_sql(@query.sql)
    flash.now[:alert] =  "更新処理(UPDATE文,INSERT文)の恐れがあるため登録を禁止します."
    render :new
    return
  end

  respond_to do |format|
    if @query.save
      format.html { redirect_to @query, :notice => 'Query was successfully created.' }
      format.json { render :show, :status => :created, :location => @query }
    else
      format.html { render :new }
      format.json { render :json => @query.errors, :status => :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /queries/1 DELETE /queries/1.json



83
84
85
86
87
88
89
90
# File 'lib/generators/templates/controllers/queries_controller.rb', line 83

def destroy
  @query = Query.find_by_id(params[:id])
  @query.destroy
  respond_to do |format|
    format.html { redirect_to queries_url, :notice => 'Query was successfully destroyed.' }
    format.json { head :no_content }
  end
end

#download_csvObject



92
93
94
95
96
97
98
99
100
101
# File 'lib/generators/templates/controllers/queries_controller.rb', line 92

def download_csv
  query = Query.find_by_id(params[:id])
  result = QueryStorage.execute_sql(query.sql)
  csv_data = QueryStorage.get_csv_data_array(result)
  # csv_data = csv_data.encode(Encoding::SJIS, :invalid => :replace, :undef => :replace)
  respond_to do |format|
    format.html
    format.csv { send_data csv_data, :type => 'text/csv; charset=shift_jis', :filename => query.title+".csv" }
  end
end

#download_tsvObject



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/generators/templates/controllers/queries_controller.rb', line 103

def download_tsv
  query = Query.find_by_id(params[:id])
  result = QueryStorage.execute_sql(query.sql)
  has_header = true
  csv_data = QueryStorage.get_tsv_data(result, has_header)
  csv_data = csv_data.encode(Encoding::SJIS, :invalid => :replace, :undef => :replace)
  respond_to do |format|
    format.html
    format.csv { send_data csv_data, :type => 'text/csv; charset=shift_jis', :filename => query.title+".tsv" }
  end
end

#editObject

GET /queries/1/edit



34
35
36
# File 'lib/generators/templates/controllers/queries_controller.rb', line 34

def edit
  @query = Query.find_by_id(params[:id])
end

#indexObject

GET /queries GET /queries.json



9
10
11
12
13
14
# File 'lib/generators/templates/controllers/queries_controller.rb', line 9

def index
  @queries = Query.all
  # if @queries.present?
  #   @queries.order(:updated_at).reverse_order
  # end
end

#newObject

GET /queries/new



29
30
31
# File 'lib/generators/templates/controllers/queries_controller.rb', line 29

def new
  @query = Query.new
end

#showObject

GET /queries/1 GET /queries/1.json



18
19
20
21
22
23
24
25
26
# File 'lib/generators/templates/controllers/queries_controller.rb', line 18

def show
  @query = Query.find_by_id(params[:id])
  sql = @query.sql
  @result = QueryStorage.execute_sql(sql)
  # @csv_date = QueryStorage.get_csv_data_array(@result)
  # @tsv_date = QueryStorage.get_tsv_data(@result, has_header)
  @header = @result[0].keys
  @result = Kaminari.paginate_array(@result.to_a).page(params[:page]).per(1000)
end

#updateObject

PATCH/PUT /queries/1 PATCH/PUT /queries/1.json



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/generators/templates/controllers/queries_controller.rb', line 62

def update
  @query = Query.find_by_id(params[:id])
  # update,insert文だと思われる場合は登録を禁止する。
  if QueryStorage.is_insert_or_update_sql(@query.sql)
    flash.now[:alert] =  "更新処理(UPDATE文,INSERT文)の恐れがあるため登録を禁止します."
    render :edit
    return
  end
  respond_to do |format|
    if @query.update_attributes(params[:query])
      format.html { redirect_to @query, :notice => 'Query was successfully updated.' }
      format.json { render :show, :status => :ok, :location => @query }
    else
      format.html { render :edit }
      format.json { render :json => @query.errors, :status => :unprocessable_entity }
    end
  end
end