db2_query 0.3.2 → 0.3.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a863b9451d52ea28e71b1477cfd7275bb76b34c91a1302208f4052674dc7b901
4
- data.tar.gz: 555eafaa282e3cab7a0d4231592a6d2a6d3087d0c194f98040dff1724f5da0f2
3
+ metadata.gz: 3dd6ee9200ac62f38f2a81e202c0df39eb374778398b31ee41021749957d20bf
4
+ data.tar.gz: 71a6155efe5dded92661e02435c60f7f9b5f47c85353fd662836988ec3a217db
5
5
  SHA512:
6
- metadata.gz: 917d8a2a7a1eefbb85185d021642f42115e7fd51969b302752f1284e274799fca9d7b24c3f6a7fdc764fa9a79cfd6cac7245e4500b0005d5e498f1e718e63a5b
7
- data.tar.gz: 353f50ef752eaaee361c939c21f6552e4fa5c8b5bc81a0face640aae420d889fcb1fb148bd0d7b74e8fec3b1867ffa31df9996cacb05465302e512ec0a5d2147
6
+ metadata.gz: c70095fb9df32696b8b30bd9a589767af759f2ea341724e3e9891d4850c5379c22de27082186d14b131da1c5dc96a48d7d4552a4c0382c66427bcaec64080f28
7
+ data.tar.gz: c5237f837262f80453f6dbf426769e50ae72cb9907306d70f6fc915ae4e281a15d0f0de94d3a116beadf98ce2a1ce98bc892801b28bd05dd2497acbc16cdadad
data/README.md CHANGED
@@ -36,17 +36,17 @@ Complete the configurations by editing the files according to your application r
36
36
  File **config/db2query.yml** consist of DSN/database name and connection pool config:
37
37
  ```yml
38
38
  development:
39
- dsn: ARUNIT
39
+ dsn: LIBDEV
40
40
  idle: 5
41
41
  pool: 5
42
42
  timeout: 5
43
43
  test:
44
- dsn: ARUNIT
44
+ dsn: LIBTEST
45
45
  idle: 5
46
46
  pool: 5
47
47
  timeout: 5
48
48
  production:
49
- dsn: ARUNIT
49
+ dsn: LIBPROD
50
50
  idle: 5
51
51
  pool: 5
52
52
  timeout: 5
@@ -70,7 +70,9 @@ end
70
70
  ```
71
71
 
72
72
  ### Custom Field Type
73
- **FieldTypes** are classes that used by **Db2Query** to format the data before sending it to the database by using `serialize` method and converting the **query result** before consumed by your **Rails application** by using `deserialize` method. Both `serialize` and `deserialize` operations are only applied when you provide **QueryDefinitions** on your query. By default, there are ten field types that can be used in your [query definitions](#32-querydefinitions) :
73
+ **FieldTypes** are classes that are used by **Db2Query** to format the data before sending it to the database by using `serialize` method and `deserialize` the returned query result data by converting the **query result** before consumed by your **Rails application**. Both `serialize` and `deserialize` operations are only applied when you provide **QueryDefinitions** on your query.
74
+
75
+ By default, there are ten field types that can be used in your [query definitions](#32-querydefinitions) :
74
76
 
75
77
  ```ruby
76
78
  DEFAULT_FIELD_TYPES = {
@@ -100,7 +102,7 @@ You can use your own Field type class by extending **Db2Query::Type::Value** cla
100
102
  end
101
103
  end
102
104
  ```
103
- Then map the classes into a variable and load it into the **Db2Query::Base** by using **set_field_types** method in the initializer file.
105
+ Then put the classes into a field types hash constant and load it into the **Db2Query::Base** by using **set_field_types** method in the initializer file.
104
106
  ```ruby
105
107
  # app_root/config/initializers/db2query.rb
106
108
 
@@ -122,17 +124,10 @@ end
122
124
 
123
125
  ## 3. Usage
124
126
 
125
- Once you completely do [**Installation**](#1-installation) & [**Initialization**](#2-initialization), basically you has been ready to use **Db2Query::Base** with three additional conventions: **SQL Convention**, **Field Type Convention**, **Argument Key Convention**.
127
+ Once you completely do the [**Installation**](#1-installation) & [**Initialization**](#2-initialization) steps, basically you has been ready to use **Db2Query::Base**. There are three additional rules that help **Db2Query** run properly: **SQL Convention**, **Field Type Convention**, and **Argument Key Convention**.
126
128
 
127
129
  **SQL Convention**:
128
- > **"** Dollar symbol **$** is used as the prefix of all column names **in the WHERE clause** of provided **Parameterized Query** SQL string. It is used as a pointer in the binding process of key and value of query arguments. We have to provide it manually in the SQL string of each **Parameterized Query**. Here, **Parameterized Query** is used to minimize SQL injection risks.**"**
129
-
130
- **Field Type Convention**:
131
- > **"** **field_name** written in **query_definition** block must be in downcased format.**"**
132
-
133
- **Argument Key Convention**:
134
- > **"** **Argument Key** passed into query have to follow its parameter written in the SQL. It is case-sensitive. If the parameter in your SQL is written in downcase format, then your argument key has to be in downcase format too.**"**
135
-
130
+ > Dollar symbol **$** is used as the prefix of all column names **in the WHERE clause** of provided **Parameterized Query** SQL string. It is used as a pointer in the query arguments key and value binding process. We have to provide it manually in the SQL string of each **Parameterized Query**. Here, **Parameterized Query** is used to minimize SQL injection risks.
136
131
 
137
132
  ```ruby
138
133
  # SQL Convention Examples
@@ -143,7 +138,12 @@ Db2Query::Base.query("SELECT * FROM USERS WHERE $email = ?", "my_account@email.c
143
138
  # Example of Normal SQL usage
144
139
 
145
140
  Db2Query::Base.query("SELECT * FROM USERS WHERE email = 'my_account@email.com'")
141
+ ```
142
+
143
+ **Field Type Convention**:
144
+ > Query definition's **field_name** written in **query_definition** block must be in downcased format.
146
145
 
146
+ ```ruby
147
147
  # Field Type Convention Example
148
148
 
149
149
  module Definitions
@@ -158,9 +158,22 @@ module Definitions
158
158
  end
159
159
  end
160
160
  end
161
+ ```
162
+
163
+ **Argument Key Convention**:
164
+ > The letter case of a **Named Argument** key that passed into a query, has to follow its parameter letter case format that is written in the SQL. The argument key is case-sensitive. If the parameter in your SQL is written in downcase format, then your argument key has to be in downcase format too, and vice versa.
161
165
 
166
+ ```ruby
162
167
  # Argument Key Convention Example
163
168
 
169
+ class MyQuery < Db2Query::Base
170
+ ...
171
+ query :find_by, <<-SQL
172
+ SELECT * FROM USERS WHERE $id = ?
173
+ SQL
174
+ ...
175
+ end
176
+
164
177
  MyQuery.find_user_by_id id: 10000
165
178
 
166
179
  ```
@@ -207,11 +220,13 @@ Db2Query::Base.execute("DELETE FROM users WHERE $id = ?", 10000)
207
220
 
208
221
  ### 3.2 QueryDefinitions
209
222
 
223
+ #### 3.2.1 Query Field Definitions
224
+
210
225
  QueryDefinitions is helpful when you need formatter methods that **serialize** the data before it being sent to the database and **deserialize** database output data before being consumed by **Rails application**. The real examples are **Binary** and **Boolean** field types.
211
226
  At **Db2Query::Type::Binary**, the data `unpacked` by `serialize` method before sending to the database and do `deserialize` operation to `pack` the database returned data.
212
227
  QueryDefinition can be used as **Query Schema** where the **field types** of a query are outlined. The field-type written in QueryDefinition has to follow the **Field Type Convention**.
213
228
 
214
- A QueryDefinitions reside in `app_root/app/queries/definitions` directory. It is automatically created when you create your query by using run `rails g query query_name` [**generator**](#33-generator) command. The QueryDefinitions class can be defined as follow:
229
+ A QueryDefinitions reside in `app_root/app/queries/definitions` directory. It is automatically created when you create your query by running `rails g query query_name` [**generator**](#33-generator) command. The QueryDefinitions class can be defined as follow:
215
230
  ```ruby
216
231
  # app_root/app/queries/definitions/your_query_definitions.rb
217
232
  module Definitions
@@ -256,7 +271,21 @@ module Definitions
256
271
  end
257
272
 
258
273
  ```
274
+ #### 3.2.2 Query Argument Types
259
275
 
276
+ Sometimes, the `query arguments` do not exist in query definitions fields. In such a case, we have to provide `query argument types` at the Query class.
277
+
278
+ ```ruby
279
+ module NameSpace
280
+ class QueryName < Db2Query::Base
281
+ arguments :user_by_email, { email: :string, trim: true }
282
+
283
+ def user_by_email_sql
284
+ "SELECT id, first_name, last_name FROM USERS WHERE $email = ?"
285
+ end
286
+ end
287
+ end
288
+ ```
260
289
 
261
290
  ### 3.3 Generator
262
291
 
@@ -265,7 +294,7 @@ Create query class by using `rails g query NAME` commands. For example:
265
294
  ```bash
266
295
  $ rails g query NameSpace::Name --defines=first_query --queries=next_query --lambdas=last_query
267
296
  create app/queries/name_space/name_query.rb
268
- create app/queries/definitions/movies_query_definitions.rb
297
+ create app/queries/definitions/name_space/name_query_definitions.rb
269
298
  create test/queries/name_space/name_query_test.rb
270
299
  ```
271
300
  This will create `app/queries/name_space/name_query.rb` file in `app/queries` directory.
@@ -366,7 +395,7 @@ class MyQuery < Db2Query::Base
366
395
 
367
396
  query :find_user_by_id, <<-SQL
368
397
  SELECT * FROM USERS WHERE $id = ?
369
- end
398
+ SQL
370
399
  end
371
400
  ```
372
401
 
@@ -425,13 +454,14 @@ user.last_name # => "Lumentut"
425
454
  user.email # => "yohanes@github.com"
426
455
  ```
427
456
 
428
- ### 3.5 SQL extention (`@extention`)
429
- For a reusable `sql`, we can extend it by using a combination of `extention` and `sql_with_extention` methods, with an `@extention` pointer at SQL statement.
457
+ ### 3.5 SQL extension (`@extension`)
458
+ For the sake of reusable SQL string, we can reuse the most commonly used SQL part by implementing `sql_with_extension` methods with an SQL string argument contain `@extension` pointer at SQL statement.
459
+
430
460
  ```ruby
431
461
  class MyQuery < Db2Query::Base
432
462
  # reusable SQL
433
- _SQL = -> extention {
434
- sql_with_extention("SELECT * FROM USERS WHERE @extention", extention)
463
+ _SQL = -> extension {
464
+ sql_with_extension("SELECT * FROM USERS WHERE @extension", extension)
435
465
  }
436
466
 
437
467
  # implementation
@@ -439,7 +469,7 @@ class MyQuery < Db2Query::Base
439
469
  end
440
470
  ```
441
471
  ```bash
442
- irb(main):001:0> MyQuery.user_by_email
472
+ irb(main):001:0> MyQuery.user_by_email email: "yohanes@github.com"
443
473
  SQL (2.7ms) SELECT * FROM USERS email = ? [["email", "yohanes@github.com"]]
444
474
  => #<Db2Query::Result [#<Record id: 10000, first_name: Yohanes, ...]>
445
475
  ```
@@ -535,6 +565,7 @@ SQL Load (3.28ms) SELECT * FROM USERS WHERE age > 500
535
565
  ## 6. Examples
536
566
 
537
567
  For complete examples please see the basic examples [here](https://github.com/yohaneslumentut/db2_query/blob/master/test/dummy/app/queries/user_query.rb).
568
+ Please see [**Db2Session**](https://github.com/yohaneslumentut/db2_session) for **REST** and **GraphQL** implementation of multi-user on the remote server.
538
569
 
539
570
  ## 7. License
540
571
  The gem is available as open-source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -4,7 +4,6 @@ module Db2Query
4
4
  class Base
5
5
  include Config
6
6
  include Helper
7
- include Quoting
8
7
  include DbConnection
9
8
  include FieldType
10
9
  include Core
@@ -25,27 +25,44 @@ module Db2Query
25
25
  end
26
26
 
27
27
  def query(*query_args)
28
- if query_args[1].respond_to?(:call)
28
+ if query_args.first.is_a?(Symbol)
29
29
  query_name, body = query_args
30
- singleton_class.define_method(query_name) do |*args|
31
- body.call(args << { query_name: query_name })
30
+
31
+ body_lambda = if body.is_a?(Proc)
32
+ -> args { body.call(args << { query_name: query_name }) }
33
+ elsif body.is_a?(String)
34
+ definition = definitions.lookup_query(query_name, body.strip)
35
+ -> args { exec_query_result(definition, args) }
36
+ else
37
+ raise Db2Query::QueryMethodError.new
32
38
  end
33
- elsif query_args[0].is_a?(String)
34
- sql, query_args = [query_args.first.strip, query_args.drop(1)]
35
- query = raw_query(sql, query_args)
36
- connection.raw_query(query.db2_spec_sql, query.args)
37
- elsif query_args[1].is_a?(String) && query_args[1].strip.length > 0
38
- query_name, sql = query_args
39
- query = definitions.lookup_query(query_name, sql.strip)
39
+
40
40
  singleton_class.define_method(query_name) do |*args|
41
- exec_query_result(query, args)
41
+ body_lambda.call(args)
42
+ end
43
+ elsif query_args.first.is_a?(String)
44
+ sql, args = [query_args.first.strip, query_args.drop(1)]
45
+
46
+ definition = Query.new.tap do |d|
47
+ d.define_sql(sql)
48
+ d.define_args(args)
42
49
  end
50
+
51
+ connection.raw_query(definition.db2_spec_sql, definition.args)
43
52
  else
44
- raise Db2Query::QueryMethodError.new
53
+ raise Db2Query::Error, "Wrong query implementation"
45
54
  end
46
55
  end
47
56
  alias define query
48
57
 
58
+ def query_arguments_map
59
+ @query_arguments_map ||= {}
60
+ end
61
+
62
+ def query_arguments(query_name, argument_types)
63
+ query_arguments_map[query_name] = argument_types
64
+ end
65
+
49
66
  def fetch(sql, args = [])
50
67
  query = definitions.lookup_query(args, sql)
51
68
  query.validate_select_query
@@ -60,7 +77,10 @@ module Db2Query
60
77
  private
61
78
  def new_definitions
62
79
  definition_class = "Definitions::#{name}Definitions"
63
- Object.const_get(definition_class).new(field_types_map)
80
+ Object.const_get(definition_class).new(
81
+ query_arguments_map,
82
+ field_types_map
83
+ )
64
84
  rescue Exception => e
65
85
  raise Db2Query::Error, e.message
66
86
  end
@@ -71,13 +91,6 @@ module Db2Query
71
91
  end
72
92
  end
73
93
 
74
- def raw_query(sql, args)
75
- Query.new.tap do |query|
76
- query.define_sql(sql)
77
- query.define_args(args)
78
- end
79
- end
80
-
81
94
  def define_sql_query(method_name)
82
95
  sql_query_name = sql_query_symbol(method_name)
83
96
  sql_statement = allocate.method(sql_query_name).call
@@ -48,6 +48,7 @@ module Db2Query
48
48
  @config = config
49
49
  @instrumenter = ActiveSupport::Notifications.instrumenter
50
50
  @lock = ActiveSupport::Concurrency::LoadInterlockAwareMonitor.new
51
+ @connection_pool = nil
51
52
  create_connection_pool
52
53
  end
53
54
 
@@ -3,9 +3,11 @@
3
3
  module Db2Query
4
4
  class Definitions
5
5
  attr_accessor :types, :types_map
6
+ attr_reader :arguments_map
6
7
 
7
- def initialize(types_map)
8
- @types_map = types_map
8
+ def initialize(query_arguments_map, field_types_map)
9
+ @arguments_map = query_arguments_map
10
+ @types_map = field_types_map
9
11
  describe
10
12
  initialize_types
11
13
  end
@@ -34,6 +36,10 @@ module Db2Query
34
36
  query_name, sql = query_definitions(args)
35
37
  lookup(query_name).tap do |query|
36
38
  query.define_sql(sql)
39
+ query.argument_keys.each do |key|
40
+ key, key_def = query_arg_key(query, key)
41
+ query.argument_types.store(key, data_type_instance(key_def))
42
+ end
37
43
  end
38
44
  end
39
45
 
@@ -75,5 +81,13 @@ module Db2Query
75
81
  else args
76
82
  end
77
83
  end
84
+
85
+ def query_arg_key(query, key)
86
+ [key, unless arguments_map[query.query_name].nil?
87
+ arguments_map[query.query_name][key]
88
+ else
89
+ query.columns[key]
90
+ end]
91
+ end
78
92
  end
79
93
  end
@@ -39,7 +39,7 @@ module Db2Query
39
39
  end
40
40
  end
41
41
 
42
- class ExtentionError < StandardError
42
+ class ExtensionError < StandardError
43
43
  end
44
44
 
45
45
  class ImplementationError < StandardError
@@ -16,12 +16,13 @@ module Db2Query
16
16
  sql.gsub("@list", "'#{list.join("', '")}'")
17
17
  end
18
18
 
19
- def sql_with_extention(sql, extention)
20
- if sql.scan(/\@extention+/).length == 0
21
- raise Db2Query::ExtentionError, "Missing @extention pointer at SQL"
19
+ def sql_with_extension(sql, extension)
20
+ if sql.scan(/\@extension+/).length == 0
21
+ raise Db2Query::ExtensionError, "Missing @extension pointer at SQL"
22
22
  end
23
- sql.gsub("@extention", extention.strip)
23
+ sql.gsub("@extension", extension.strip)
24
24
  end
25
+ alias sql_with_extention sql_with_extension
25
26
 
26
27
  private
27
28
  def sql_query_methods
@@ -42,7 +43,7 @@ module Db2Query
42
43
  end
43
44
 
44
45
  def fetch_error_message
45
- "`fetch`, `fetch_list` and `fetch_extention` methods applied for SQL `select` statement only."
46
+ "`fetch`, `fetch_list` and `fetch_extension` methods applied for SQL `select` statement only."
46
47
  end
47
48
  end
48
49
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Db2Query
4
4
  class Query
5
- attr_reader :columns, :keys, :query_name, :sql, :types
5
+ attr_reader :query_name, :sql, :columns, :keys, :types, :argument_types
6
6
 
7
7
  include SqlStatement
8
8
 
@@ -10,11 +10,12 @@ module Db2Query
10
10
  @columns = {}
11
11
  @query_name = query_name
12
12
  @sql_statement = nil
13
+ @argument_types = {}
13
14
  @types = {}
14
15
  end
15
16
 
16
17
  def define_sql(sql)
17
- @keys ||= new_keys(sql)
18
+ @keys ||= sql_arguments(sql)
18
19
  @sql ||= sql.tr("$", "")
19
20
  end
20
21
 
@@ -33,6 +34,19 @@ module Db2Query
33
34
  raise Db2Query::Error, "No column #{column} found at query: #{query_name} definitions"
34
35
  end
35
36
 
37
+ def argument_type(key)
38
+ argument_types.fetch(key) || data_type(key)
39
+ rescue
40
+ raise Db2Query::Error, "No argument #{key} type found at query: #{query_name}"
41
+ end
42
+
43
+ def argument_keys
44
+ keys.map do |key|
45
+ arg_key = "#{key}".split(".").last
46
+ arg_key.to_sym unless arg_key.nil?
47
+ end
48
+ end
49
+
36
50
  def length
37
51
  columns.length
38
52
  end
@@ -52,7 +66,7 @@ module Db2Query
52
66
  end
53
67
 
54
68
  def sorted_args(args)
55
- keys.map.with_index do |key, index|
69
+ argument_keys.map.with_index do |key, index|
56
70
  serialized_arg(args.is_a?(Hash) ? args[key] : args[index], key)
57
71
  end
58
72
  end
@@ -82,12 +96,12 @@ module Db2Query
82
96
  end
83
97
 
84
98
  private
85
- def new_keys(raw_sql)
86
- raw_sql.scan(/\$\S+/).map { |key| key.gsub!(/[$=,)]/, "").to_sym }
99
+ def sql_arguments(raw_sql)
100
+ raw_sql.scan(/\$\S+/).map { |arg| arg.gsub!(/[$=,)]/, "").to_sym }
87
101
  end
88
102
 
89
103
  def serialized_arg(arg, key)
90
- query_name.nil? ? arg : data_type(key).serialize(arg)
104
+ query_name.nil? ? arg : argument_type(key).serialize(arg)
91
105
  end
92
106
 
93
107
  def column_from_key(key)
@@ -104,14 +118,11 @@ module Db2Query
104
118
  end
105
119
  end
106
120
 
107
- def validate_arguments(given, expected)
108
- raise Db2Query::ArgumentError.new(given, expected) unless given == expected
109
- end
110
-
111
121
  def validated_args(args)
112
- args = args.first.is_a?(Hash) ? args.first : args
113
- validate_arguments(args.length, keys.length)
114
- sorted_args(args)
122
+ arguments = args.first.is_a?(Hash) ? args.first : args
123
+ given, expected = [arguments.length, keys.length]
124
+ raise Db2Query::ArgumentError.new(given, expected) unless given == expected
125
+ sorted_args(arguments)
115
126
  end
116
127
  end
117
128
  end
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "db2_query"
4
- require "rails"
5
-
6
3
  module Db2Query
7
4
  class Railtie < ::Rails::Railtie
8
5
  railtie_name :db2_query
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Db2Query
4
- VERSION = "0.3.2"
4
+ VERSION = "0.3.3"
5
5
  end
data/lib/db2_query.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_record"
4
3
  require "active_support"
5
4
  require "active_support/concurrency/load_interlock_aware_monitor"
5
+ require "active_record"
6
6
  require "active_model/type"
7
7
  require "connection_pool"
8
8
  require "odbc_utf8"
@@ -44,6 +44,6 @@ module Db2Query
44
44
  def self.root
45
45
  __dir__
46
46
  end
47
-
48
- require "db2_query/railtie" if defined?(Rails)
49
47
  end
48
+
49
+ require "db2_query/railtie" if defined?(Rails)
@@ -2,11 +2,17 @@
2
2
 
3
3
  <% module_definitions_namespacing do -%>
4
4
  class <%= query_class_name %>Definitions < <%= "Db2Query::Definitions" %>
5
+ <% if @query_methods.empty? %>
6
+ <%= indent("def describe\n") -%>
7
+ <%= indent("") %>
8
+ <%= indent("end") %>
9
+ <% else %>
5
10
  <%= indent("def describe") -%>
6
11
  <% @query_methods.each do |method| %>
7
12
  <%= indent("query_definition :#{method} do |c|\n") %>
8
13
  <%= indent("end") %>
9
14
  <% end -%>
10
15
  <%= indent("end") %>
16
+ <% end %>
11
17
  <%= 'end' -%>
12
18
  <% end %>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db2_query
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - yohanes_l
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-01 00:00:00.000000000 Z
11
+ date: 2021-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tty-progressbar
@@ -160,7 +160,6 @@ files:
160
160
  - lib/db2_query/helper.rb
161
161
  - lib/db2_query/logger.rb
162
162
  - lib/db2_query/query.rb
163
- - lib/db2_query/quoting.rb
164
163
  - lib/db2_query/railtie.rb
165
164
  - lib/db2_query/result.rb
166
165
  - lib/db2_query/sql_statement.rb
@@ -208,7 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
208
207
  - !ruby/object:Gem::Version
209
208
  version: '0'
210
209
  requirements: []
211
- rubygems_version: 3.0.3
210
+ rubygems_version: 3.1.3
212
211
  signing_key:
213
212
  specification_version: 4
214
213
  summary: Rails Db2 ODBC plugin
@@ -1,102 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Db2Query
4
- module Quoting
5
- def self.included(base)
6
- base.send(:extend, ClassMethods)
7
- end
8
-
9
- module ClassMethods
10
- def quoted_true
11
- "TRUE"
12
- end
13
-
14
- def unquoted_true
15
- 1
16
- end
17
-
18
- def quoted_false
19
- "FALSE"
20
- end
21
-
22
- def unquoted_false
23
- 0
24
- end
25
-
26
- def quoted_binary(value)
27
- "x'#{value.hex}'"
28
- end
29
-
30
- def quoted_time(value)
31
- value = value.change(year: 2000, month: 1, day: 1)
32
- quoted_date(value).sub(/\A\d\d\d\d-\d\d-\d\d /, "")
33
- end
34
-
35
- def quoted_date(value)
36
- if value.acts_like?(:time)
37
- if ActiveRecord::Base.default_timezone == :utc
38
- value = value.getutc if !value.utc?
39
- else
40
- value = value.getlocal
41
- end
42
- end
43
-
44
- result = value.to_s(:db)
45
- if value.respond_to?(:usec) && value.usec > 0
46
- result << "." << sprintf("%06d", value.usec)
47
- else
48
- result
49
- end
50
- end
51
-
52
- private
53
- def _quote(value)
54
- case value
55
- when String, Symbol, ActiveSupport::Multibyte::Chars
56
- "'#{quote_string(value.to_s)}'"
57
- when true
58
- quoted_true
59
- when false
60
- quoted_false
61
- when nil
62
- "NULL"
63
- when BigDecimal
64
- value.to_s("F")
65
- when Numeric, ActiveSupport::Duration
66
- value.to_s
67
- when Db2Query::Type::Binary::Data
68
- quoted_binary(value)
69
- when ActiveRecord::Type::Time::Value
70
- "'#{quoted_time(value)}'"
71
- when Date, Time
72
- "'#{quoted_date(value)}'"
73
- when Class
74
- "'#{value}'"
75
- else raise TypeError, "can't quote #{value.class.name}"
76
- end
77
- end
78
-
79
- def _type_cast(value)
80
- case value
81
- when Symbol, ActiveSupport::Multibyte::Chars
82
- value.to_s
83
- when Db2Query::Type::Binary::Data
84
- value.hex
85
- when true
86
- unquoted_true
87
- when false
88
- unquoted_false
89
- when BigDecimal
90
- value.to_s("F")
91
- when nil, Numeric, String
92
- value
93
- when ActiveRecord::Type::Time::Value
94
- quoted_time(value)
95
- when Date, Time
96
- quoted_date(value)
97
- else raise TypeError, "can't cast #{value.class.name}"
98
- end
99
- end
100
- end
101
- end
102
- end