dohmysql 0.2.33 → 0.2.34
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dohmysql/handle.rb +25 -25
- data/lib/dohmysql/smart_row.rb +14 -0
- data/lib/dohmysql/typed_row_builder.rb +15 -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: 781550d6ef60c6d499005d5b89634c70a760a250
|
4
|
+
data.tar.gz: a59db4a488222896c14c6b1b27bd0363b947a285
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d4ff31f3263f689a4be21cf9c032e906280500ffc3814d459d02646665c062ed3958d82cd9dd82cac0c24f0b14ebab8ee6b8bcfd063da746db55cc5b6c23c91f
|
7
|
+
data.tar.gz: 0d5716c9067c15684993be13a4f5e4949300f9e0ceb72073f3a74e0ff572d5971525a318ea38c1edf528151a142656cdb37351320f79c44e0fb53df1178e04ce
|
data/lib/dohmysql/handle.rb
CHANGED
@@ -97,18 +97,18 @@ class Handle
|
|
97
97
|
|
98
98
|
# The most generic form of select.
|
99
99
|
# It calls to_s on the statement object to facilitate the use of sql builder objects.
|
100
|
-
def select(statement,
|
100
|
+
def select(statement, build_arg = nil)
|
101
101
|
result_set = generic_query(statement)
|
102
102
|
DohDb.logger.call('result', "selected #{result_set.size} rows")
|
103
|
-
rows = get_row_builder(
|
103
|
+
rows = get_row_builder(build_arg).build_rows(result_set)
|
104
104
|
rows
|
105
105
|
end
|
106
106
|
|
107
107
|
# Simple convenience wrapper around the generic select call.
|
108
108
|
# Throws an exception unless the result set is a single row.
|
109
109
|
# Returns the row selected.
|
110
|
-
def select_row(statement,
|
111
|
-
rows = select(statement,
|
110
|
+
def select_row(statement, build_arg = nil)
|
111
|
+
rows = select(statement, build_arg)
|
112
112
|
raise UnexpectedQueryResult, "selected #{rows.size} rows; expected 1" unless rows.size == 1
|
113
113
|
rows[0]
|
114
114
|
end
|
@@ -116,22 +116,22 @@ class Handle
|
|
116
116
|
# Simple convenience wrapper around the generic select call.
|
117
117
|
# Throws an exception unless the result set is empty or a single row.
|
118
118
|
# Returns nil if the result set is empty, or the row selected.
|
119
|
-
def select_optional_row(statement,
|
120
|
-
rows = select(statement,
|
119
|
+
def select_optional_row(statement, build_arg = nil)
|
120
|
+
rows = select(statement, build_arg)
|
121
121
|
raise UnexpectedQueryResult, "selected #{rows.size} rows; expected 0 or 1" if rows.size > 1
|
122
122
|
if rows.empty? then nil else rows[0] end
|
123
123
|
end
|
124
124
|
|
125
125
|
# Simple convenience wrapper around select_row.
|
126
126
|
# Returns the first (and typically, the only) field from the selected row.
|
127
|
-
def select_field(statement,
|
128
|
-
select_row(statement,
|
127
|
+
def select_field(statement, build_arg = nil)
|
128
|
+
select_row(statement, build_arg).at(0)
|
129
129
|
end
|
130
130
|
|
131
131
|
# Simple convenience wrapper around select_optional_row.
|
132
132
|
# Returns the first (and typically, the only) field from the selected row, if any, or nil.
|
133
|
-
def select_optional_field(statement,
|
134
|
-
row = select_optional_row(statement,
|
133
|
+
def select_optional_field(statement, build_arg = nil)
|
134
|
+
row = select_optional_row(statement, build_arg)
|
135
135
|
row && row.at(0)
|
136
136
|
end
|
137
137
|
|
@@ -139,8 +139,8 @@ class Handle
|
|
139
139
|
# If there are 2 fields, returns a hash where each key is the first field in the result set, and the value is the second field.
|
140
140
|
# If there are more than 2 fields, returns a hash where each key is the first field in the result set,
|
141
141
|
# and the value is the row itself, as a Hash, and without the field used as a key.
|
142
|
-
def select_transpose(statement,
|
143
|
-
rows = select(statement,
|
142
|
+
def select_transpose(statement, build_arg = nil)
|
143
|
+
rows = select(statement, build_arg)
|
144
144
|
return {} if rows.empty?
|
145
145
|
field_count = rows.first.size
|
146
146
|
if field_count < 2
|
@@ -158,13 +158,13 @@ class Handle
|
|
158
158
|
end
|
159
159
|
|
160
160
|
# Returns an array of arrays, where the individual arrays contain just the values from each database row -- they lack field names.
|
161
|
-
def select_values(statement,
|
162
|
-
select(statement,
|
161
|
+
def select_values(statement, build_arg = nil)
|
162
|
+
select(statement, build_arg).collect { |row| row.values }
|
163
163
|
end
|
164
164
|
|
165
165
|
# Returns an array of the first (and typically, the only) field of every row in the result set.
|
166
|
-
def select_list(statement,
|
167
|
-
select(statement,
|
166
|
+
def select_list(statement, build_arg = nil)
|
167
|
+
select(statement, build_arg).collect { |row| row.at(0) }
|
168
168
|
end
|
169
169
|
|
170
170
|
def transaction
|
@@ -255,21 +255,21 @@ private
|
|
255
255
|
insert("#{keyword} INTO #{table} #{keystr} VALUES #{valuestrs.join(",")}")
|
256
256
|
end
|
257
257
|
|
258
|
-
def get_row_builder(
|
259
|
-
if
|
258
|
+
def get_row_builder(build_arg = nil)
|
259
|
+
if build_arg.nil?
|
260
260
|
TypedRowBuilder.new
|
261
|
-
elsif
|
261
|
+
elsif build_arg == :read
|
262
262
|
TypedRowBuilder.new(ReadOnlyRow)
|
263
|
-
elsif
|
263
|
+
elsif build_arg == :hash
|
264
264
|
TypedRowBuilder.new(HashRow)
|
265
|
-
elsif
|
265
|
+
elsif build_arg == :write
|
266
266
|
TypedRowBuilder.new(WritableRow)
|
267
|
-
elsif
|
267
|
+
elsif build_arg == :smart
|
268
268
|
TypedRowBuilder.new(SmartRow)
|
269
|
-
elsif
|
270
|
-
|
269
|
+
elsif build_arg.respond_to?('build_rows')
|
270
|
+
build_arg
|
271
271
|
else
|
272
|
-
TypedRowBuilder.new(
|
272
|
+
TypedRowBuilder.new(build_arg)
|
273
273
|
end
|
274
274
|
end
|
275
275
|
|
data/lib/dohmysql/smart_row.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'dohmysql/abstract_row'
|
2
|
+
require 'sqlstmt/update'
|
2
3
|
|
3
4
|
module DohDb
|
4
5
|
|
@@ -66,6 +67,19 @@ class AbstractSmartRow < AbstractRow
|
|
66
67
|
def guess_missing_set(key, value)
|
67
68
|
set(key, value)
|
68
69
|
end
|
70
|
+
|
71
|
+
def db_update
|
72
|
+
return if @changed_keys.empty?
|
73
|
+
sqlb = SqlStmt::Update.new.table(@table)
|
74
|
+
sqlb.where("#{primary_key} = #{get(primary_key).to_sql}")
|
75
|
+
@changed_keys.each {|key| sqlb.field(key, get(key).to_sql)}
|
76
|
+
Doh.db.query(sqlb)
|
77
|
+
@changed_keys.clear
|
78
|
+
end
|
79
|
+
|
80
|
+
def primary_key
|
81
|
+
@primary_key ||= "#{@table}_id"
|
82
|
+
end
|
69
83
|
end
|
70
84
|
|
71
85
|
class SmartRow < AbstractSmartRow
|
@@ -1,10 +1,17 @@
|
|
1
1
|
require 'dohmysql/readonly_row'
|
2
|
+
require 'dohmysql/smart_row'
|
2
3
|
|
3
4
|
module DohDb
|
4
5
|
|
5
6
|
class TypedRowBuilder
|
6
|
-
def initialize(
|
7
|
-
|
7
|
+
def initialize(arg = ReadOnlyRow)
|
8
|
+
if arg.is_a?(String)
|
9
|
+
@row_klass = SmartRow
|
10
|
+
@table = arg
|
11
|
+
else
|
12
|
+
@row_klass = arg
|
13
|
+
@table = nil
|
14
|
+
end
|
8
15
|
end
|
9
16
|
|
10
17
|
def build_rows(result_set)
|
@@ -22,7 +29,12 @@ class TypedRowBuilder
|
|
22
29
|
values.push(value)
|
23
30
|
end
|
24
31
|
end
|
25
|
-
|
32
|
+
if @table
|
33
|
+
row = @row_klass.new(keys, values, @table)
|
34
|
+
else
|
35
|
+
row = @row_klass.new(keys, values)
|
36
|
+
end
|
37
|
+
retval.push(row)
|
26
38
|
end
|
27
39
|
retval
|
28
40
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dohmysql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.34
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Makani Mason
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-07-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: dohroot
|