qx 0.1.0 → 0.1.1
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 +4 -4
- data/lib/qx.rb +26 -3
- 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: db585fa41cbb9f2c67f375cac4081cb4b4a7c3f2
|
4
|
+
data.tar.gz: 601b4cc468ec989ac5392ea66b80a3c4b48af75c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b24084742d4c6c918ad274258a3e4242c760af4499a83b50e135bb0f3b9f53b6c2cdc8b91625e212dbdbfd1f9a07bff2310872e4a87e3affc839eec0e1cf2f3d
|
7
|
+
data.tar.gz: 7144420b1b4b48bc65de1241deffe8102e098f2a34f174a80a74e77f26fe452586efcabf448316bf456fc997644214e653ee8f5f88f6b314d18fe7c5b67ada55
|
data/lib/qx.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'active_record'
|
2
2
|
require 'colorize'
|
3
|
+
|
3
4
|
class Qx
|
4
5
|
|
5
6
|
##
|
@@ -28,7 +29,11 @@ class Qx
|
|
28
29
|
|
29
30
|
def self.parse_select(expr)
|
30
31
|
str = 'SELECT'
|
31
|
-
|
32
|
+
if expr[:DISTINCT_ON]
|
33
|
+
str += " DISTINCT ON (#{expr[:DISTINCT_ON].map(&:to_s).join(', ')})"
|
34
|
+
elsif expr[:DISTINCT]
|
35
|
+
str += " DISTINCT"
|
36
|
+
end
|
32
37
|
str += ' ' + expr[:SELECT].map{|expr| expr.is_a?(Qx) ? expr.parse : expr}.join(", ")
|
33
38
|
throw ArgumentError.new("FROM clause is missing for SELECT") unless expr[:FROM]
|
34
39
|
str += ' FROM ' + expr[:FROM]
|
@@ -65,14 +70,16 @@ class Qx
|
|
65
70
|
str = parse_select(expr)
|
66
71
|
elsif expr[:DELETE_FROM]
|
67
72
|
str = 'DELETE FROM ' + expr[:DELETE_FROM]
|
68
|
-
|
73
|
+
throw ArgumentError.new("WHERE clause is missing for DELETE FROM") unless expr[:WHERE]
|
74
|
+
str += ' WHERE ' + expr[:WHERE].map{|w| "(#{w})"}.join(" AND ")
|
69
75
|
str += " RETURNING " + expr[:RETURNING].join(", ") if expr[:RETURNING]
|
70
76
|
elsif expr[:UPDATE]
|
71
77
|
str = 'UPDATE ' + expr[:UPDATE]
|
72
78
|
throw ArgumentError.new("SET clause is missing for UPDATE") unless expr[:SET]
|
79
|
+
throw ArgumentError.new("WHERE clause is missing for UPDATE") unless expr[:WHERE]
|
73
80
|
str += ' SET ' + expr[:SET]
|
74
81
|
str += ' FROM ' + expr[:FROM] if expr[:FROM]
|
75
|
-
str += ' WHERE ' + expr[:WHERE].map{|w| "(#{w})"}.join(" AND ")
|
82
|
+
str += ' WHERE ' + expr[:WHERE].map{|w| "(#{w})"}.join(" AND ")
|
76
83
|
str += " RETURNING " + expr[:RETURNING].join(", ") if expr[:RETURNING]
|
77
84
|
end
|
78
85
|
return str
|
@@ -101,6 +108,9 @@ class Qx
|
|
101
108
|
# format: 'csv' | 'hash' give data csv style with Arrays -- good for exports or for saving memory
|
102
109
|
def self.execute_raw(expr, options={})
|
103
110
|
puts expr if options[:verbose]
|
111
|
+
if options[:copy_csv]
|
112
|
+
expr = "COPY (#{expr}) TO '#{options[:copy_csv]}' DELIMITER ',' CSV HEADER"
|
113
|
+
end
|
104
114
|
result = ActiveRecord::Base.connection.execute(expr)
|
105
115
|
result.map_types!(@@type_map) if @@type_map
|
106
116
|
if options[:format] == 'csv'
|
@@ -114,6 +124,10 @@ class Qx
|
|
114
124
|
return data
|
115
125
|
end
|
116
126
|
|
127
|
+
def self.execute_file(path, data={}, options={})
|
128
|
+
Qx.execute_raw(Qx.interpolate_expr(File.open(path, 'r').read, data), options)
|
129
|
+
end
|
130
|
+
|
117
131
|
# helpers for JSON conversion
|
118
132
|
def to_json(name)
|
119
133
|
name = name.to_s
|
@@ -129,6 +143,10 @@ class Qx
|
|
129
143
|
@tree[:SELECT] = cols
|
130
144
|
self
|
131
145
|
end
|
146
|
+
def add_select(*cols)
|
147
|
+
@tree[:SELECT].push(cols)
|
148
|
+
self
|
149
|
+
end
|
132
150
|
def self.insert_into(table_name, cols=[])
|
133
151
|
self.new(INSERT_INTO: Qx.quote_ident(table_name), INSERT_COLUMNS: cols.map{|c| Qx.quote_ident(c)})
|
134
152
|
end
|
@@ -156,6 +174,11 @@ class Qx
|
|
156
174
|
|
157
175
|
# - SELECT sub-clauses
|
158
176
|
|
177
|
+
def distinct
|
178
|
+
@tree[:DISTINCT] = true
|
179
|
+
self
|
180
|
+
end
|
181
|
+
|
159
182
|
def distinct_on(*cols)
|
160
183
|
@tree[:DISTINCT_ON] = cols
|
161
184
|
self
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: qx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jay R Bolton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05
|
11
|
+
date: 2016-12-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colorize
|