Class: JSQLController

Inherits:
ActionController::API
  • Object
show all
Defined in:
app/controllers/jsql_controller.rb

Constant Summary collapse

@@transaction =
Array.new
@@database_type =
nil

Instance Method Summary collapse

Instance Method Details

#colon_switcher(query, request_body) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'app/controllers/jsql_controller.rb', line 274

def colon_switcher(query, request_body)
  splited_query = query.to_s.gsub('(', ' ').gsub(')', ' ').gsub(',', ' ').gsub('=', ' ').split
  words = splited_query.size
  missing_params_flag = false
  missing_params = "["
  params_array = request_body['params']
=begin
  puts "request_body['params'].class"
  puts request_body['params'].class
=end
  #puts "params_array: " + params_array.to_s
  puts "words"
  puts words
  for i in 0..words
    puts "params_array.class != Hash"
    puts params_array.class != Hash
    if splited_query[i].to_s.start_with?(':')
      param = splited_query[i].split(/:/)[1]
      puts "params_array.class != Hash"
      puts params_array.class != Hash
      if (params_array.class != Hash) || (params_array == nil) || (request_body['params'][param] == nil)
        missing_params = missing_params_collector(missing_params, param)
        puts "Missing Params!"
        missing_params_flag = true
      else
        query.gsub!(':' + param.to_s, "'" + params_array[param].to_s + "'")
      end
    end

  end
  if missing_params_flag
    missing_params += "]"
    query = render_error_msg('There are missing parameters: ' + missing_params)
  end
  query
end

#create_query(request_body) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/controllers/jsql_controller.rb', line 92

def create_query(request_body)
  query = ""
  token = request_body['token']

  if token.class == Array

    for i in 0..token.size - 1
      if i > 0
        query += " "
      end
      query += get_queries(token[i])
      puts "query.class"
      puts query.class
      puts "query"
      puts query
      if query.include? "code"
        query
        break
      end
    end


  else
    query = get_queries(token.to_s)
    puts query
  end

  query
end

#database_type(base, member_key, api_key) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/controllers/jsql_controller.rb', line 161

def database_type(base, member_key, api_key)
  if @@database_type == nil
    url = base + "/api/request/options/all"
    response = HTTP
                   .headers('Content-Type' => 'application/json', 'memberKey' => member_key, 'apiKey' => api_key)
                   .get(url).body.to_s
    puts response
    if JSON.parse(response)['code'] != 200
      render_error_msg(JSON.parse(response)['description'])
    else
      @@database_type = JSON.parse(response)['data']['databaseDialect']
    end

  end
end

#delete_manager(manager) ⇒ Object



36
37
38
# File 'app/controllers/jsql_controller.rb', line 36

def delete_manager(manager)
  @@transaction.delete_if {|y| y[1] == manager}
end

#delete_manager_timeObject



32
33
34
# File 'app/controllers/jsql_controller.rb', line 32

def delete_manager_time()
  @@transaction.delete_if {|a| a[2] < Time::now.tv_sec - 10}
end

#find_manager(transaction_id) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/jsql_controller.rb', line 76

def find_manager(transaction_id)
  begin
    puts "+++++====++++"
    puts "@@transaction.select{|(x, y)| x == transaction_id}"
    manager = @@transaction.select {|(x, y)| x == transaction_id}
    manager = manager[0][1]
    puts manager
    manager
    puts "+++++====++++"
  rescue NoMethodError
    manager = nil
  end

  manager
end

#get_queries(token) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/controllers/jsql_controller.rb', line 122

def get_queries(token)
#url and headers
  base_url = 'http://softwarecartoon.com:'
  port = '9291'
  queries = '/api/request/queries'
  member_key = Jsql.configuration.member_key
  api_key = Jsql.configuration.api_key
  url = base_url + port + queries
  database_type((base_url + port), member_key, api_key)
#parsing token into right regex
  token = "[\"" + token + "\"]"

  query = HTTP
              .headers('Content-Type' => 'application/json', 'memberKey' => member_key, 'apiKey' => api_key)
              .post(url, :body => token).body.to_s
  puts "query"
  puts query
#parsing response to json

  header = HTTP
               .headers('Content-Type' => 'application/json', 'memberKey' => member_key, 'apiKey' => api_key)
               .post(url, :body => token).headers
  puts header.class

  no_such_hash = "Cannot get property 'query' on null object"
  begin
    if (JSON.parse(query)[0] == nil || JSON.parse(query)[0]['query'] == nil) && JSON.parse(query)['message'] == no_such_hash
      puts query
      render_error_msg("No such hash for given ApiKey and MemberKey")
    elsif JSON.parse(query)['code'] != 200
      query
    else
      (JSON.parse(query)[0]['query'])
    end
  rescue
    (JSON.parse(query)[0]['query'])
  end
end

#missing_params_collector(missing_params, param) ⇒ Object



223
224
225
226
227
228
# File 'app/controllers/jsql_controller.rb', line 223

def missing_params_collector(missing_params, param)
  if missing_params.size > 1
    missing_params += ", "
  end
  missing_params += ":" + param
end

#params_switcher(query, request_body) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/controllers/jsql_controller.rb', line 177

def params_switcher(query, request_body)
#method replaces all question marks with params
  if (query.include? '?')
    query = question_mark_switcher(query, request_body)
    puts "query.include? '?'"
    puts query
  elsif (query.include? ':')
    query = colon_switcher(query, request_body)
    puts "query.include? ':'"
  end
  query
end

#perform_method(method) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/jsql_controller.rb', line 7

def perform_method(method)
  manager = nil
  puts Jsql.configuration.member_key
  puts Jsql.configuration.api_key
  puts "@database_type"
  puts @@database_type
  puts "@@transaction"
  puts @@transaction
  puts Time::now.tv_sec
  begin

    request_body = JSON.parse(request.body.string)
    delete_manager_time
    manager = transaction_control

    query = create_query(request_body)

    query = params_switcher(query, request_body)
    query_method_compare(query, method)

  rescue Timeout::Error
    rollback_manager(manager)
  end
end

#query_method_compare(query, method) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'app/controllers/jsql_controller.rb', line 230

def query_method_compare(query, method)
  puts "query_method_compare(" + query.to_s + ", " + method.to_s + ")"
  if query.to_s.downcase.start_with?(method)
    begin
      if method == 'insert'
        if @@database_type == 'POSTGRES'
          query += " returning *"
        end
        #puts response.class
        #puts response
        response = ActiveRecord::Base.connection.execute(query).as_json
        # manager.commit_transaction
        last_id = response[0]['id']
        render json: {status: 'OK', lastId: last_id}, status: :ok
      elsif method == 'delete'
        if @@database_type == 'POSTGRES'
          query += " returning *"
        end
        response = ActiveRecord::Base.connection.execute(query).as_json
        if response.empty?
          render_error_msg('given column does not exist')
        else
          render json: {status: 'OK', response: JSON.parse(response.to_json)}, status: :ok
        end

      else
        response = ActiveRecord::Base.connection.execute(query).as_json
        render json: {status: 'OK', response: JSON.parse(response.to_json)}, status: :ok
      end
    rescue
      msg = "#{$!}"
      render_error_msg(msg.split("\n")[0])
    end
  elsif query.start_with?("{")
    query
  else
    render json: {code: 400, response: "Only " + method.upcase + " queries allowed"}, status: :ok
  end
end

#question_mark_switcher(query, request_body) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'app/controllers/jsql_controller.rb', line 190

def question_mark_switcher(query, request_body)
  if (request_body['params'].class == Array) && (query.include? "?") && (query.count('?') - request_body['params'].size < 1)
    puts " posiada '?'"
    question_marks = query.count('?')
    puts question_marks
    params = request_body['params']
    puts params
    puts "(query.count('?') - request_body['params'].size)"
    puts (query.count('?') - request_body['params'].size)
    puts "question_marks"
    puts query.count('?')
    puts "query"
    puts query
    puts "request_body['params'].size"
    puts request_body['params'].size
    for i in 0..question_marks
      puts query.sub!('?', "'" + params[i].to_s + "'")
      request_body['params'].delete_if {|e| e[i] == params[i]}
    end
  else
    begin
      missing_params = (query.count('?') - request_body['params'].size).to_s
    rescue
      missing_params = query.count('?').to_s
    end
    query = render_error_msg("There are " + missing_params + " missing parameters")
    puts query
  end
  puts query
  query
end

#render_error_msg(message) ⇒ Object



270
271
272
# File 'app/controllers/jsql_controller.rb', line 270

def render_error_msg(message)
  render json: {code: 400, response: message}, status: :ok
end

#rollback_manager(manager) ⇒ Object



311
312
313
314
# File 'app/controllers/jsql_controller.rb', line 311

def rollback_manager(manager)
  delete_manager(manager)
  manager.rollback_transaction
end

#transaction_controlObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/jsql_controller.rb', line 41

def transaction_control
  request_headers = request.headers
  transactional = request_headers['TX']
  transaction_id = request_headers['TXID']
  puts transactional
  puts transaction_id
  if transactional
    puts "This should be trasnacttional!"
    now = Time.now.to_s
    if transaction_id == nil || transaction_id.size < 1
      transaction_id = Digest::MD5.new.update(now)
      puts transaction_id
      manager = ActiveRecord::Base.connection.transaction_manager
      @@transaction.push([transaction_id, manager, Time::now.tv_sec])
      puts "31"
    else
      puts "33"
      puts manager.class
      puts manager.class
      manager = find_manager(transaction_id)
      if manager == nil
        manager = ActiveRecord::Base.connection.transaction_manager
        @@transaction.push([transaction_id, manager, Time::now.tv_sec])
      end
    end
    puts "ManageR"
    puts manager
    puts "ManageR"
    puts "@@transaction"
    puts @@transaction
    manager.begin_transaction
  end
  manager
end