db_memoize 0.3.9 → 0.3.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/db_memoize/value.rb +28 -35
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eccd7dee2a2a8e43545c7adfc0fcf471a5cd59d2a06b02dfdd8e6adea8f6352e
|
4
|
+
data.tar.gz: 4683ab98fab4e440b3e61124d9c1ae30fcb87b7d939814f874702cc23f020602
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 374ff41e01ec60b1e3e51f2e99969aa13199b512e364bf376462b79fdbf79e0fc07b25988da1f9619f0f6b1e588cea3934c92cf2036367ac49a29f8c52ec510b
|
7
|
+
data.tar.gz: f32dd2bc3cf5d8f66e6f4566dd8be90a6cd0b1c6c2cf1d45fd5fef4566ef63fa3e1f23fd9e551c5f820f7b01ee9bd97990c4b844ceb6e4c56ee8f675f3a80a29
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.10
|
data/lib/db_memoize/value.rb
CHANGED
@@ -44,6 +44,16 @@ module DbMemoize
|
|
44
44
|
SQL
|
45
45
|
end
|
46
46
|
|
47
|
+
ALL_COLUMNS = [
|
48
|
+
:val_string,
|
49
|
+
:val_integer,
|
50
|
+
:val_float,
|
51
|
+
:val_time,
|
52
|
+
:val_object,
|
53
|
+
:val_boolean,
|
54
|
+
:val_nil
|
55
|
+
].freeze
|
56
|
+
|
47
57
|
def self.fast_create(entity_table_name, entity_id, method_name, value)
|
48
58
|
method_name = method_name.to_s
|
49
59
|
|
@@ -66,38 +76,17 @@ module DbMemoize
|
|
66
76
|
raise "Unsupported value of type #{value.class.name}: #{value.inspect}"
|
67
77
|
end
|
68
78
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
# ActiveRecord method; for everything else we use Simple::SQL (for
|
74
|
-
# performance reasons: it is 10 times as fast.)
|
75
|
-
if column != :val_time
|
76
|
-
simple_sql_create_value column, entity_table_name: entity_table_name, entity_id: entity_id,
|
77
|
-
method_name: method_name, value: value
|
78
|
-
else
|
79
|
-
ar_create_value column, entity_table_name: entity_table_name, entity_id: entity_id,
|
80
|
-
method_name: method_name, value: value
|
79
|
+
# some types need special encoding
|
80
|
+
case column
|
81
|
+
when :val_object then value = _reformat_object(value)
|
82
|
+
when :val_time then value = _reformat_time(value)
|
81
83
|
end
|
82
|
-
end
|
83
84
|
|
84
|
-
ALL_COLUMNS = [
|
85
|
-
:val_string,
|
86
|
-
:val_integer,
|
87
|
-
:val_float,
|
88
|
-
:val_time,
|
89
|
-
:val_object,
|
90
|
-
:val_boolean,
|
91
|
-
:val_nil
|
92
|
-
].freeze
|
93
|
-
|
94
|
-
def self.simple_sql_create_value(column, entity_table_name:, entity_id:, method_name:, value:)
|
95
85
|
other_columns = ALL_COLUMNS - [column]
|
96
86
|
default_updates = other_columns.map { |c| "#{c}=NULL" }
|
97
87
|
|
98
88
|
sql = <<~SQL.freeze
|
99
|
-
INSERT INTO #{table_name}
|
100
|
-
(entity_table_name, entity_id, method_name, #{column}, created_at)
|
89
|
+
INSERT INTO #{table_name}(entity_table_name, entity_id, method_name, #{column}, created_at)
|
101
90
|
VALUES($1,$2,$3,$4,NOW())
|
102
91
|
ON CONFLICT (entity_id, entity_table_name, method_name)
|
103
92
|
DO UPDATE
|
@@ -107,16 +96,20 @@ module DbMemoize
|
|
107
96
|
SQL.ask sql, entity_table_name, entity_id, method_name, value
|
108
97
|
end
|
109
98
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
99
|
+
# Apparently the pg, and for that matter also simple-sql, drops subsecond
|
100
|
+
# resolution when passing in time objects. (Note that this seems not always
|
101
|
+
# to be the case, it probably depends on some encoder configuration within
|
102
|
+
# pg - which simple-sql is not touching, since this is a setting on a
|
103
|
+
# connection which might not be exclusive to simple-sql.)
|
104
|
+
#
|
105
|
+
# Instead we'll just pass along a string, postgresql will then convert it
|
106
|
+
# into a proper timestamp.
|
107
|
+
def self._reformat_time(t) # rubocop:disable Naming/UncommunicativeMethodParamName
|
108
|
+
format('%04d%02d-%02d %02d:%02d:%02d.%06d', t.year, t.mon, t.day, t.hour, t.min, t.sec, t.usec)
|
109
|
+
end
|
118
110
|
|
119
|
-
|
111
|
+
def self._reformat_object(value)
|
112
|
+
JSON.generate(value)
|
120
113
|
end
|
121
114
|
end
|
122
115
|
end
|