sequel-hstore 1.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/README.md +4 -0
- data/lib/hstore/hstore.rb +72 -0
- data/lib/sequel-hstore.rb +18 -0
- metadata +6 -3
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2007-2008 Sharon Rosner
|
2
|
+
Copyright (c) 2008-2011 Jeremy Evans
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
+
of this software and associated documentation files (the "Software"), to
|
6
|
+
deal in the Software without restriction, including without limitation the
|
7
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
8
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
9
|
+
furnished to do so, subject to the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be included in
|
12
|
+
all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
17
|
+
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
18
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
19
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'strscan'
|
2
|
+
|
3
|
+
class Sequel::Postgres::HStore < Hash
|
4
|
+
def self.quoted_string(scanner)
|
5
|
+
key = scanner.scan(/(\\"|[^"])*/)
|
6
|
+
key = key.gsub(/\\(.)/, '\1')
|
7
|
+
scanner.skip(/"/)
|
8
|
+
key
|
9
|
+
end
|
10
|
+
def self.parse_quotable_string(scanner)
|
11
|
+
if scanner.scan(/"/)
|
12
|
+
value = quoted_string(scanner)
|
13
|
+
else
|
14
|
+
value = scanner.scan(/\w+/)
|
15
|
+
value = nil if value == "NULL"
|
16
|
+
# TODO: values but not keys may be NULL
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.skip_key_value_delimiter(scanner)
|
21
|
+
scanner.skip(/\s*=>\s*/)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.skip_pair_delimiter(scanner)
|
25
|
+
scanner.skip(/,\s*/)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.new_from_string(string)
|
29
|
+
hash = {}
|
30
|
+
|
31
|
+
# remove single quotes around literal if necessary
|
32
|
+
string = string[1..-2] if string[0] == "'" and string[-1] == "'"
|
33
|
+
|
34
|
+
scanner = StringScanner.new(string)
|
35
|
+
while !scanner.eos?
|
36
|
+
k = parse_quotable_string(scanner)
|
37
|
+
skip_key_value_delimiter(scanner)
|
38
|
+
v = parse_quotable_string(scanner)
|
39
|
+
skip_pair_delimiter(scanner)
|
40
|
+
# controversial...
|
41
|
+
# to_sym, or what?
|
42
|
+
hash[k.to_sym] = v
|
43
|
+
end
|
44
|
+
self[hash]
|
45
|
+
end
|
46
|
+
|
47
|
+
def initialize(hash)
|
48
|
+
@hash = hash
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_s_escaped(str)
|
52
|
+
str.to_s.gsub(/\\(?!")/) {'\\\\'}.gsub(/"/, '\"').gsub(/'/, "''")
|
53
|
+
end
|
54
|
+
|
55
|
+
def sql_literal(dataset)
|
56
|
+
string = self.map do |(k,v)|
|
57
|
+
if v.nil?
|
58
|
+
# represent nil as NULL without quotes
|
59
|
+
v = "NULL"
|
60
|
+
else
|
61
|
+
# otherwise, write a double-quoted string with backslash escapes for quotes
|
62
|
+
v = to_s_escaped(v)
|
63
|
+
v = "\"#{v}\""
|
64
|
+
end
|
65
|
+
|
66
|
+
# TODO: throw an error if there is a NULL key
|
67
|
+
"\"#{to_s_escaped(k)}\" => #{v}"
|
68
|
+
end.join(", ")
|
69
|
+
"'#{string}'"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# This is an awful monkey patch lifted from some other project. Can we improve on this somehow?
|
2
|
+
# (See github.com/couchrest/couchrest-core(?))
|
3
|
+
require 'sequel/adapters/postgres'
|
4
|
+
require_relative 'hstore/hstore'
|
5
|
+
|
6
|
+
class Hash
|
7
|
+
def to_hstore
|
8
|
+
Sequel::Postgres::HStore[self.dup]
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.===(other)
|
12
|
+
return false if self == Hash && other.is_a?(Sequel::Postgres::HStore)
|
13
|
+
super
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
Sequel::Postgres::PG_NAMED_TYPES[:hstore] = lambda{|s| Sequel::Postgres::HStore.new_from_string(s) }
|
18
|
+
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: sequel-hstore
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version:
|
5
|
+
version: 1.0.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Peter van Hardenberg
|
@@ -44,8 +44,11 @@ extensions: []
|
|
44
44
|
|
45
45
|
extra_rdoc_files: []
|
46
46
|
|
47
|
-
files:
|
48
|
-
|
47
|
+
files:
|
48
|
+
- LICENSE
|
49
|
+
- README.md
|
50
|
+
- lib/hstore/hstore.rb
|
51
|
+
- lib/sequel-hstore.rb
|
49
52
|
has_rdoc: true
|
50
53
|
homepage: http://github.com/pvh/sequel-hstore
|
51
54
|
licenses: []
|