db_memoize 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -0
- data/lib/db_memoize/metal.rb +32 -7
- data/lib/db_memoize/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ec9a05ceb15a0252b3e145983035f1192d9252c4
|
4
|
+
data.tar.gz: 53190328b2d6a04e861bc0d4d88dd29aea61ad7c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1da1fe68e2389d0d6a74ac03d38f486fcd249f58142f52e4fa5aa3b683a1905f2dc879bd57f188957519fb3181bcbd2fca2dcd521071e155ed3fda6e20e3cfe4
|
7
|
+
data.tar.gz: f448a8946e63346fe401d763d396994638ced3cf9e25340ea36b40fcc26372654da88e9593bcbf18f596497325f3dc40c5ae4fc7877421a36d7f5b3489f41d0b
|
data/README.md
CHANGED
@@ -103,6 +103,14 @@ class CreateMemoizedValues < ActiveRecord::Migration
|
|
103
103
|
end
|
104
104
|
```
|
105
105
|
|
106
|
+
### Testing
|
107
|
+
|
108
|
+
Note that db_memoize needs Postgres. To set up the database needed to run tests, this is what you can do:
|
109
|
+
|
110
|
+
# sudo su postgresql
|
111
|
+
# createuser >>yourusername<<
|
112
|
+
# createdb -O >>yourusername<< db_memoize_test
|
113
|
+
|
106
114
|
|
107
115
|
Have fun!
|
108
116
|
|
data/lib/db_memoize/metal.rb
CHANGED
@@ -46,20 +46,37 @@ module DbMemoize
|
|
46
46
|
@base_klass.columns_hash.key?(column_name)
|
47
47
|
end
|
48
48
|
|
49
|
-
|
50
|
-
|
49
|
+
class Inserter
|
50
|
+
def initialize(sql:, bytea_indices:)
|
51
|
+
@sql = sql
|
52
|
+
@bytea_indices = bytea_indices
|
53
|
+
end
|
54
|
+
|
55
|
+
def exec(raw_connection:, values:)
|
56
|
+
@bytea_indices.each do |bytea_index|
|
57
|
+
value = values[bytea_index]
|
58
|
+
values[bytea_index] = PGconn.escape_bytea(value) if value
|
59
|
+
end
|
60
|
+
|
61
|
+
raw_connection.exec_params(@sql, values)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# returns an Inserter
|
66
|
+
def inserter(field_names)
|
67
|
+
@query_cache[field_names] ||= _inserter(field_names)
|
51
68
|
end
|
52
69
|
|
53
70
|
DATABASE_IDENTIFIER_REGEX = /\A\w+\z/
|
54
71
|
|
55
72
|
def check_database_identifiers!(*strings)
|
56
73
|
strings.each do |s|
|
57
|
-
next if DATABASE_IDENTIFIER_REGEX =~ s
|
74
|
+
next if DATABASE_IDENTIFIER_REGEX =~ s
|
58
75
|
raise ArgumentError, "Invalid database identifier: #{s.inspect}"
|
59
76
|
end
|
60
77
|
end
|
61
78
|
|
62
|
-
def
|
79
|
+
def _inserter(field_names)
|
63
80
|
check_database_identifiers! table_name, *field_names
|
64
81
|
|
65
82
|
placeholders = 0.upto(field_names.count - 1).map { |idx| "$#{idx + 1}" }
|
@@ -76,16 +93,24 @@ module DbMemoize
|
|
76
93
|
|
77
94
|
sql = "INSERT INTO #{table_name} (#{field_names.join(',')}) VALUES(#{placeholders.join(',')})"
|
78
95
|
sql += " RETURNING #{primary_key.column}" if primary_key.column
|
79
|
-
|
96
|
+
|
97
|
+
columns_hash = @base_klass.columns_hash
|
98
|
+
bytea_indices = []
|
99
|
+
field_names.each_with_index { |column, idx|
|
100
|
+
next unless :binary == columns_hash.fetch(column).type
|
101
|
+
bytea_indices << idx
|
102
|
+
}
|
103
|
+
|
104
|
+
Inserter.new sql: sql, bytea_indices: bytea_indices
|
80
105
|
end
|
81
106
|
|
82
107
|
public
|
83
108
|
|
84
109
|
def create!(record)
|
85
110
|
keys, values = record.to_a.transpose
|
111
|
+
keys = keys.map(&:to_s)
|
86
112
|
|
87
|
-
|
88
|
-
result = raw_connection.exec_params(sql, values)
|
113
|
+
result = inserter(keys).exec(raw_connection: raw_connection, values: values)
|
89
114
|
|
90
115
|
# if we don't have an ID column then the sql does not return any value. The result
|
91
116
|
# object would be this: #<PG::Result status=PGRES_COMMAND_OK ntuples=0 nfields=0 cmd_tuples=1>
|
data/lib/db_memoize/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: db_memoize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- johannes-kostas goetzinger
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-06-
|
11
|
+
date: 2017-06-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|