jsql 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 12ea729fa8a9e6946d05e182820ce5cddbe6ed68e998cbfe860c93c807bcd46a
4
+ data.tar.gz: b69d80750a643bb02b8727375c5ec678c059eea65d95cc18e9c3416dec71e501
5
+ SHA512:
6
+ metadata.gz: 25f11e8f9af75a9b27da5ca03d4ea6c777729188a8f8163906e18a9d8d9ef7313c056b7f74384d4856d822d134db106a05eb8ba2de54bc702b3ac795c56f6090
7
+ data.tar.gz: 997aff066a7093825459c53f63663ead87c3beb4593de8217d099265d74e52faf3d226d085a62f2ef4997b39a163ab01d43245fd32313bd1aa7c9d518222dbee
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Adam Radecki
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # Jsql
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'jsql'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install jsql
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Jsql'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
@@ -0,0 +1,2 @@
1
+ //= link_directory ../javascripts/jsql .js
2
+ //= link_directory ../stylesheets/jsql .css
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require rails-ujs
14
+ //= require activestorage
15
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ module Jsql
2
+ class ApplicationController < ActionController::Base
3
+ protect_from_forgery with: :exception
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ module Jsql
2
+ class DeleteController < JSQLController
3
+ def create
4
+ perform_method('delete')
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ module Jsql
2
+ class ExecuteController < JSQLController
3
+ def create
4
+ perform_method('commit')
5
+ end
6
+
7
+ def perform_method(method)
8
+ manager = transaction_control
9
+ manager.commit_transaction
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ module Jsql
2
+ class InsertController < JSQLController
3
+ def create
4
+ perform_method('insert')
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Jsql
2
+ class SelectController < JSQLController
3
+ def create
4
+ perform_method('select')
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Jsql
2
+ class UpdateController < JSQLController
3
+ def create
4
+ perform_method('update')
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,289 @@
1
+ require 'digest'
2
+ require 'timeout'
3
+ class JSQLController < ActionController::API
4
+ @@transaction = Array.new
5
+ @@database_type = nil
6
+ def perform_method(method)
7
+ manager = nil
8
+ puts "@database_type"
9
+ puts @@database_type
10
+ puts "@@transaction"
11
+ puts @@transaction
12
+ puts Time::now.tv_sec
13
+ begin
14
+ Timeout::timeout(10) do
15
+ request_body = JSON.parse(request.body.string)
16
+ delete_manager_time
17
+ manager = transaction_control
18
+
19
+
20
+ query = create_query(request_body)
21
+
22
+ query = params_switcher(query, request_body)
23
+ query_method_compare(query, method)
24
+ end
25
+ rescue Timeout::Error
26
+ rollback_manager(manager)
27
+ end
28
+ end
29
+
30
+ def delete_manager_time()
31
+ @@transaction.delete_if {|a| a[2] < Time::now.tv_sec - 10}
32
+ end
33
+
34
+ def delete_manager(manager)
35
+ @@transaction.delete_if {|y| y[1] == manager}
36
+ end
37
+
38
+
39
+ def transaction_control
40
+ request_headers = request.headers
41
+ transactional = request_headers['TX']
42
+ transaction_id = request_headers['TXID']
43
+ puts transactional
44
+ puts transaction_id
45
+ if transactional
46
+ puts "This should be trasnacttional!"
47
+ now = Time.now.to_s
48
+ if transaction_id == nil || transaction_id.size < 1
49
+ transaction_id = Digest::MD5.new.update(now)
50
+ puts transaction_id
51
+ manager = ActiveRecord::Base.connection.transaction_manager
52
+ @@transaction.push([transaction_id, manager, Time::now.tv_sec])
53
+ puts "31"
54
+ else
55
+ puts "33"
56
+ puts manager.class
57
+ puts manager.class
58
+ manager = find_manager(transaction_id)
59
+ if manager == nil
60
+ manager = ActiveRecord::Base.connection.transaction_manager
61
+ @@transaction.push([transaction_id, manager, Time::now.tv_sec])
62
+ end
63
+ end
64
+ puts "ManageR"
65
+ puts manager
66
+ puts "ManageR"
67
+ puts "@@transaction"
68
+ puts @@transaction
69
+ manager.begin_transaction
70
+ end
71
+ manager
72
+ end
73
+
74
+ def find_manager(transaction_id)
75
+ begin
76
+ puts "+++++====++++"
77
+ puts "@@transaction.select{|(x, y)| x == transaction_id}"
78
+ manager = @@transaction.select {|(x, y)| x == transaction_id}
79
+ manager = manager[0][1]
80
+ puts manager
81
+ manager
82
+ puts "+++++====++++"
83
+ rescue NoMethodError
84
+ manager = nil
85
+ end
86
+
87
+ manager
88
+ end
89
+
90
+ def create_query(request_body)
91
+ query = ""
92
+ token = request_body['token']
93
+
94
+ if token.class == Array
95
+ for i in 0..token.size - 1
96
+ if i > 0
97
+ query += " "
98
+ end
99
+ query += get_queries(token[i])
100
+ puts query
101
+ end
102
+ else
103
+ query = get_queries(token.to_s)
104
+ puts query
105
+ end
106
+
107
+ query
108
+ end
109
+
110
+ def get_queries(token)
111
+ #url and headers
112
+ base_url = 'http://softwarecartoon.com:'
113
+ port = '9291'
114
+ queries = '/api/request/queries'
115
+ member_key = 'Z6kEovODxAv2I5hKekMyUw=='
116
+ api_key = '==iSqF8rKvVeSgqudKDOXpjiFgGMJh1PbeouIz9IW/YQ==9CI8ox66gogpoSXm6yrU'
117
+ url = base_url + port + queries
118
+ database_type((base_url + port), member_key, api_key)
119
+ #parsing token into right regex
120
+ token = "[\"" + token + "\"]"
121
+
122
+ query = HTTP
123
+ .headers('Content-Type' => 'application/json', 'memberKey' => member_key, 'apiKey' => api_key)
124
+ .post(url, :body => token).body.to_s
125
+
126
+ #parsing response to json
127
+
128
+ header = HTTP
129
+ .headers('Content-Type' => 'application/json', 'memberKey' => member_key, 'apiKey' => api_key)
130
+ .post(url, :body => token).headers
131
+ puts header.class
132
+
133
+ no_such_hash = "Cannot get property 'query' on null object"
134
+ if (JSON.parse(query)[0] == nil || JSON.parse(query)[0]['query'] == nil) && JSON.parse(query)['message'] == no_such_hash
135
+ render_error_msg("No such hash for given ApiKey and MemberKey")
136
+ else
137
+ (JSON.parse(query)[0]['query'])
138
+ end
139
+ end
140
+
141
+ def database_type(base, member_key, api_key)
142
+ if @@database_type == nil
143
+ url = base + "/api/request/options/all"
144
+ response = HTTP
145
+ .headers('Content-Type' => 'application/json', 'memberKey' => member_key, 'apiKey' => api_key)
146
+ .get(url).body.to_s
147
+ @@database_type = JSON.parse(response)['data']['databaseDialect']
148
+ end
149
+ end
150
+
151
+ def params_switcher(query, request_body)
152
+ #method replaces all question marks with params
153
+ if (query.include? '?')
154
+ query = question_mark_switcher(query, request_body)
155
+ puts "query.include? '?'"
156
+ puts query
157
+ elsif (query.include? ':')
158
+ query = colon_switcher(query, request_body)
159
+ puts "query.include? ':'"
160
+ end
161
+ query
162
+ end
163
+
164
+ def question_mark_switcher(query, request_body)
165
+ if (request_body['params'].class == Array) && (query.include? "?") && (query.count('?') - request_body['params'].size < 1)
166
+ puts " posiada '?'"
167
+ question_marks = query.count('?')
168
+ puts question_marks
169
+ params = request_body['params']
170
+ puts params
171
+ puts "(query.count('?') - request_body['params'].size)"
172
+ puts (query.count('?') - request_body['params'].size)
173
+ puts "question_marks"
174
+ puts query.count('?')
175
+ puts "query"
176
+ puts query
177
+ puts "request_body['params'].size"
178
+ puts request_body['params'].size
179
+ for i in 0..question_marks
180
+ puts query.sub!('?', "'" + params[i].to_s + "'")
181
+ request_body['params'].delete_if {|e| e[i] == params[i]}
182
+ end
183
+ else
184
+ begin
185
+ missing_params = (query.count('?') - request_body['params'].size).to_s
186
+ rescue
187
+ missing_params = query.count('?').to_s
188
+ end
189
+ query = render_error_msg("There are " + missing_params + " missing parameters")
190
+ puts query
191
+ end
192
+ puts query
193
+ query
194
+ end
195
+
196
+
197
+ def missing_params_collector(missing_params, param)
198
+ if missing_params.size > 1
199
+ missing_params += ", "
200
+ end
201
+ missing_params += ":" + param
202
+ end
203
+
204
+ def query_method_compare(query, method)
205
+ puts "query_method_compare(" + query.to_s + ", " + method.to_s + ")"
206
+ if query.to_s.downcase.start_with?(method)
207
+ begin
208
+ if method == 'insert'
209
+ if @@database_type == 'POSTGRES'
210
+ query+=" returning *"
211
+ end
212
+ #puts response.class
213
+ #puts response
214
+ response = ActiveRecord::Base.connection.execute(query).as_json
215
+ # manager.commit_transaction
216
+ last_id = response[0]['id']
217
+ render json: {status: 'OK', lastId: last_id}, status: :ok
218
+ elsif method == 'delete'
219
+ if @@database_type == 'POSTGRES'
220
+ query+=" returning *"
221
+ end
222
+ response = ActiveRecord::Base.connection.execute(query).as_json
223
+ if response.empty?
224
+ render_error_msg('given column does not exist')
225
+ else
226
+ render json: {status: 'OK', response: JSON.parse(response.to_json)}, status: :ok
227
+ end
228
+
229
+ else
230
+ response = ActiveRecord::Base.connection.execute(query).as_json
231
+ render json: {status: 'OK', response: JSON.parse(response.to_json)}, status: :ok
232
+ end
233
+ rescue
234
+ msg = "#{$!}"
235
+ render_error_msg(msg.split("\n")[0])
236
+ end
237
+ elsif query.start_with?("{")
238
+ query
239
+ else
240
+ render json: {code: 400, response: "Only " + method.upcase + " queries allowed"}, status: :ok
241
+ end
242
+ end
243
+
244
+ def render_error_msg(message)
245
+ render json: {code: 400, response: message}, status: :ok
246
+ end
247
+
248
+ def colon_switcher(query, request_body)
249
+ splited_query = query.to_s.gsub('(', ' ').gsub(')', ' ').gsub(',', ' ').gsub('=', ' ').split
250
+ words = splited_query.size
251
+ missing_params_flag = false
252
+ missing_params = "["
253
+ params_array = request_body['params']
254
+ =begin
255
+ puts "request_body['params'].class"
256
+ puts request_body['params'].class
257
+ =end
258
+ #puts "params_array: " + params_array.to_s
259
+ puts "words"
260
+ puts words
261
+ for i in 0..words
262
+ puts "params_array.class != Hash"
263
+ puts params_array.class != Hash
264
+ if splited_query[i].to_s.start_with?(':')
265
+ param = splited_query[i].split(/:/)[1]
266
+ puts "params_array.class != Hash"
267
+ puts params_array.class != Hash
268
+ if (params_array.class != Hash) || (params_array == nil) || (request_body['params'][param] == nil)
269
+ missing_params = missing_params_collector(missing_params, param)
270
+ puts "Missing Params!"
271
+ missing_params_flag = true
272
+ else
273
+ query.gsub!(':' + param.to_s, "'" + params_array[param].to_s + "'")
274
+ end
275
+ end
276
+
277
+ end
278
+ if missing_params_flag
279
+ missing_params += "]"
280
+ query = render_error_msg('There are missing parameters: ' + missing_params)
281
+ end
282
+ query
283
+ end
284
+
285
+ def rollback_manager(manager)
286
+ delete_manager(manager)
287
+ manager.rollback_transaction
288
+ end
289
+ end
@@ -0,0 +1,4 @@
1
+ module Jsql
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Jsql
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module Jsql
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module Jsql
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Jsql</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "jsql/application", media: "all" %>
9
+ <%= javascript_include_tag "jsql/application" %>
10
+ </head>
11
+ <body>
12
+
13
+ <%= yield %>
14
+
15
+ </body>
16
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,5 @@
1
+ Jsql::Engine.routes.draw do
2
+
3
+ resources :select, :insert, :delete, :update, :execute
4
+
5
+ end
@@ -0,0 +1,10 @@
1
+ module Jsql
2
+ class Engine < ::Rails::Engine
3
+ begin
4
+ isolate_namespace Jsql
5
+ rescue NameError
6
+ puts "znalazłem problem w engine.rb!"
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module Jsql
2
+ VERSION = '0.1.5'
3
+ end
data/lib/jsql.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "jsql/engine"
2
+
3
+ module Jsql
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :jsql do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsql
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.5
5
+ platform: ruby
6
+ authors:
7
+ - Adam Radecki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-01-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: none
42
+ email:
43
+ - adam.radecki@jsql.pl
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - app/assets/config/jsql_manifest.js
52
+ - app/assets/javascripts/jsql/application.js
53
+ - app/assets/stylesheets/jsql/application.css
54
+ - app/controllers/jsql/application_controller.rb
55
+ - app/controllers/jsql/delete_controller.rb
56
+ - app/controllers/jsql/execute_controller.rb
57
+ - app/controllers/jsql/insert_controller.rb
58
+ - app/controllers/jsql/select_controller.rb
59
+ - app/controllers/jsql/update_controller.rb
60
+ - app/controllers/jsql_controller.rb
61
+ - app/helpers/jsql/application_helper.rb
62
+ - app/jobs/jsql/application_job.rb
63
+ - app/mailers/jsql/application_mailer.rb
64
+ - app/models/jsql/application_record.rb
65
+ - app/views/layouts/jsql/application.html.erb
66
+ - config/routes.rb
67
+ - lib/jsql.rb
68
+ - lib/jsql/engine.rb
69
+ - lib/jsql/version.rb
70
+ - lib/tasks/jsql_tasks.rake
71
+ homepage: https://rubygems.org/gems/jsql
72
+ licenses:
73
+ - MIT
74
+ metadata:
75
+ allowed_push_host: https://rubygems.org/gems/jsql
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.7.6
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Gem used to communicate with JSQL application
96
+ test_files: []