wikk_sql 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. checksums.yaml +7 -0
  2. data/History.txt +10 -0
  3. data/Manifest.txt +5 -0
  4. data/README.md +104 -0
  5. data/Rakefile +27 -0
  6. data/lib/wikk_sql.rb +262 -0
  7. metadata +86 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a744a86c6e09edf76e687152ed1cfd7a142abf24
4
+ data.tar.gz: 8d28c677617a9ca95dc050c388e229246178ec08
5
+ SHA512:
6
+ metadata.gz: c1e9ac49a33767e2a8275c2c0c7b234ac555662e16c19462576ac333f78502e0aaa66b5f0221cb9c221218556aaf647986f780b1a13853c94a7e2db23c3a30c4
7
+ data.tar.gz: ccb74288baa0bbe90431f5376a64f16c75ab8de8a19259b0a15f96c42b7c2ca5e715fea771025910299febb0a4a9e90f391525d9d82ecd1a136b4c6e5c494235
data/History.txt ADDED
@@ -0,0 +1,10 @@
1
+ robertburrowes Sun Jun 19 14:01:08 2016 +1200
2
+ Encapsulation of SQL into module WIKK
3
+ robertburrowes Sun Jun 19 13:59:27 2016 +1200
4
+ Encapsulated in WIKK module. Allowed db_config to be Hash.
5
+ robertburrowes Sat Jun 18 21:16:41 2016 +1200
6
+ added args to class level examples
7
+ robertburrowes Sat Jun 18 21:13:02 2016 +1200
8
+ Format cleanup
9
+ robertburrowes Sat Jun 18 21:04:49 2016 +1200
10
+ First commit
data/Manifest.txt ADDED
@@ -0,0 +1,5 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.md
4
+ Rakefile
5
+ lib/wikk_sql.rb
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # wikk_sql
2
+
3
+ * http://rbur004.github.com/wikk_sql/
4
+ * Source https://github.com/wikarekare/wikk_sql
5
+ * Gem https://rubygems.org/gems/wikk_sql
6
+
7
+ ## DESCRIPTION:
8
+
9
+ Wrappers around mysql gem.
10
+ Consolidation of bits of code from many of my projects, going back 10+ years, into a gem.
11
+
12
+ ###Instance level
13
+ ```
14
+ connect(config) { |sql| rest of block } #Aliased to open
15
+ close #Call if connect didn't get passed a block
16
+ query(the_query)
17
+ each_row(the_query) { |row| rest of block }
18
+ each_hash(the_query, with_table_names) { |hash| rest of block }
19
+ each_sym(the_query) { |sym:, sym:, ..., **hash| rest of block }
20
+ transaction { calls to query, each_row, each_hash or each_sym }
21
+ fetch_fields #Returns table field info
22
+ affected_rows #Returns the number of rows changed, inserted or deleted.
23
+ ```
24
+
25
+ ###Class level calls mirror the instance level calls taking blocks
26
+ ```
27
+ WIKK::SQL::connect(config) { |sql| rest of block }
28
+ WIKK::SQL::each_row(config,the_query) { |row| rest of block }
29
+ WIKK::SQL::each_hash(config,the_query, with_table_names=false) { |hash| rest of block }
30
+ WIKK::SQL::each_sym(config,the_query) { |sym:, sym:, ..., **hash| rest of block }
31
+ ```
32
+
33
+ ###Config
34
+ ```
35
+ Any class, such as the WIKK::Configuration class, with attr_readers
36
+ host #String hostname or IP Address
37
+ db #String database name
38
+ dbuser #String user to connect to DB as.
39
+ key #"password" for db user account.
40
+ Or Hash
41
+ {"host" => "hostname", "db" => "database", "dbuser" => "username", "key" => "password"}
42
+ ```
43
+
44
+ ## FEATURES/PROBLEMS:
45
+
46
+
47
+ ## SYNOPSIS:
48
+
49
+ ###Instance example
50
+ ```
51
+ WIKK::SQL::connect(@config) do |sql|
52
+ sql.each_hash("select * from customer limit 2", with_table_names = true) do |row|
53
+ row.each do |k,v|
54
+ printf " %s => %s\n", k, v
55
+ end
56
+ end
57
+ puts "Number of rows returned: #{sql.affected_rows}"
58
+ end
59
+ ```
60
+
61
+ ###Class level example
62
+ ```
63
+ WIKK::SQL::each_sym(@config, "select * from customer limit 2") do |customer_id:, name:, site_name:, **row|
64
+ printf "customer_id %s site_name %s name %s\n", customer_id, site_name, name
65
+ end
66
+ ```
67
+
68
+
69
+ ## REQUIREMENTS:
70
+
71
+ * require 'wikk_sql'
72
+
73
+ Can use wikk_configuration gem to load config from a json file.
74
+
75
+ ## INSTALL:
76
+
77
+ * sudo gem install wikk_sql
78
+
79
+ ## LICENSE:
80
+
81
+ (The MIT License)
82
+
83
+ Encapsulates Wikarekare code used in library form, from 2004 to present day, as a gem.
84
+
85
+ Copyright (c) 2004-2016
86
+
87
+ Permission is hereby granted, free of charge, to any person obtaining
88
+ a copy of this software and associated documentation files (the
89
+ 'Software'), to deal in the Software without restriction, including
90
+ without limitation the rights to use, copy, modify, merge, publish,
91
+ distribute, sublicense, and/or sell copies of the Software, and to
92
+ permit persons to whom the Software is furnished to do so, subject to
93
+ the following conditions:
94
+
95
+ The above copyright notice and this permission notice shall be
96
+ included in all copies or substantial portions of the Software.
97
+
98
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
99
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
100
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
101
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
102
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
103
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
104
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ Hoe.plugin :yard
6
+
7
+ Hoe.spec 'wikk_sql' do
8
+ self.readme_file = "README.md"
9
+ self.developer( "Rob Burrowes","r.burrowes@auckland.ac.nz")
10
+ remote_rdoc_dir = '' # Release to root
11
+
12
+ self.yard_title = 'wikk_sql'
13
+ self.yard_options = ['--markup', 'markdown', '--protected']
14
+ end
15
+
16
+
17
+ #Validate manfest.txt
18
+ #rake check_manifest
19
+
20
+ #Local checking. Creates pkg/
21
+ #rake gem
22
+
23
+ #create doc/
24
+ #rake docs
25
+
26
+ #Copy up to rubygem.org
27
+ #rake release VERSION=1.0.1
data/lib/wikk_sql.rb ADDED
@@ -0,0 +1,262 @@
1
+ module WIKK
2
+ require 'mysql'
3
+ require 'pp'
4
+ require 'json'
5
+
6
+ #WIKK_SQL wrapper for ruby mysql gem.
7
+ # @attr_reader [Numeric] affected_rows the number of rows changed, deleted, or added.
8
+ # @attr_reader [Mysql::Result] result the last query's result
9
+ # @attr_reader [Mysql] my the DB connection descriptor
10
+ class SQL
11
+ VERSION = '0.1.1'
12
+
13
+ attr_reader :affected_rows, :result, :my
14
+
15
+ #Create WIKK::SQL instance and set up the mySQL connection.
16
+ # @param db_config [Configuration] Configuration class, Hash, or any class with appropriate attr_readers.
17
+ # @yieldparam sql [WIKK_SQL] if a block is given.
18
+ # @return [NilClass] if block is given, and closes the mySQL connection.
19
+ # @return [WIKK_SQL] if no block is given, and caller must call sql.close
20
+ def self.connect(db_config)
21
+ sql = self.new
22
+ sql.connect(db_config)
23
+ if block_given?
24
+ yield sql
25
+ return sql.close
26
+ else
27
+ return sql
28
+ end
29
+ end
30
+
31
+ #Set up the mySQL connection.
32
+ # @param db_config [Configuration] Configuration class, Hash, or any class with appropriate attr_readers.
33
+ # @yieldparam [] if a block is given.
34
+ # @return [NilClass] if block is given, and closes the mySQL connection.
35
+ # @return [WIKK_SQL] if no block is given, and caller must call sql.close
36
+ def connect(db_config)
37
+ if db_config.class == Hash
38
+ sym = db_config.each_with_object({}) { |(k,v),h| h[k.to_sym] = v }
39
+ db_config = Struct.new(*(k = sym.keys)).new(*sym.values_at(*k))
40
+ end
41
+
42
+ @my = Mysql::new(db_config.host, db_config.dbuser, db_config.key, db_config.db )
43
+ #@@my.reconnect = true
44
+ if block_given?
45
+ yield
46
+ return close
47
+ end
48
+ return @my
49
+ end
50
+
51
+ alias open connect
52
+
53
+ #close the mySQL connection. Call only if connect was not given a block.
54
+ # @return [NilClass]
55
+ def close
56
+ @my.close if @my != nil
57
+ return (@my = nil)
58
+ end
59
+
60
+ #Run a query on the DB server.
61
+ # @param the_query [String] Sql query to send to DB server.
62
+ # @raise [Mysql] passes on Mysql errors, freeing the result.
63
+ # @yieldparam [Mysql::Result] @result and @affected_rows are also set.
64
+ # @return [Mysql::Result] @result and @affected_rows are also set.
65
+ def query(the_query)
66
+ begin
67
+ if result != nil
68
+ @result.free #Free any result we had left over from previous use.
69
+ @result = nil
70
+ end
71
+ @affected_rows = 0 #incase this query crashes and burns, this will have a value.
72
+ @affected_rows = @my.affected_rows #This is non-zero for insert/delete/update of rows
73
+ @result = @my.query(the_query)
74
+ if block_given?
75
+ yield @result
76
+ else
77
+ return @result
78
+ end
79
+ rescue Mysql::Error => e
80
+ if result != nil
81
+ @result.free #Free any result we had left over from previous use.
82
+ @result = nil
83
+ end
84
+ raise e
85
+ end
86
+ end
87
+
88
+ #Perform a transaction in the passed block.
89
+ #RollBACK on error, otherwise COMMIT
90
+ # @yieldparam [] yields to block, where the queries are performed.
91
+ # @raise [Mysql] passes on Mysql errors, freeing the result.
92
+ def transaction
93
+ puts "transaction"
94
+ if block_given?
95
+ begin
96
+ @my.query("START TRANSACTION WITH CONSISTENT SNAPSHOT")
97
+ yield #Start executing the query black.
98
+ @my.query("COMMIT")
99
+ rescue Mysql::Error => e
100
+ @my.query("ROLLBACK")
101
+ raise e
102
+ end
103
+ end
104
+ end
105
+
106
+ #Yields query query results row by row, as Array
107
+ # @param the_query [String] Sql query to send to DB server.
108
+ # @raise [Mysql] passes on Mysql errors, freeing the result.
109
+ # @yieldparam [Array] each result row
110
+ # @note @result and @affected_rows are also set via call to query().
111
+ def each_row(the_query)
112
+ begin
113
+ query(the_query)
114
+ if @result != nil && block_given?
115
+ @affected_rows = @result.num_rows() #This is non-zero is we do a select, and get results.
116
+ @result.each do |row|
117
+ yield row #return one row at a time to the block
118
+ end
119
+ end
120
+ rescue Mysql::Error => e
121
+ #puts "#{e.errno}: #{e.error}"
122
+ raise e
123
+ ensure
124
+ if block_given? && @result != nil
125
+ @result.free
126
+ end
127
+ end
128
+ end
129
+
130
+ #Yields query result row by row, as Hash, using String keys
131
+ # @param the_query [String] Sql query to send to DB server.
132
+ # @param with_table_names [Boolean] if TrueClass, then table names are included in the hash keys.
133
+ # @raise [Mysql] passes on Mysql errors, freeing the result.
134
+ # @yieldparam [Hash] each result row
135
+ # @note @result and @affected_rows are also set via call to query().
136
+ def each_hash(the_query, with_table_names=false)
137
+ begin
138
+ query(the_query)
139
+ if @result != nil && block_given?
140
+ @affected_rows = @result.num_rows() #This is non-zero is we do a select, and get results.
141
+ @result.each_hash(with_table_names) do |row|
142
+ yield row
143
+ end
144
+ end
145
+ rescue Mysql::Error => e
146
+ #puts "#{e.errno}: #{e.error}"
147
+ raise e
148
+ ensure
149
+ if block_given? && @result != nil
150
+ @result.free
151
+ end
152
+ end
153
+ end
154
+
155
+ #Yields query result row by row, as Hash using Symbol keys, so can't have table names included.
156
+ #This can be used with keyword arguments. eg. each_sym { |key1:, key2:, ..., **rest_of_args| do something }
157
+ # @param the_query [String] Sql query to send to DB server.
158
+ # @raise [Mysql] passes on Mysql errors, freeing the result.
159
+ # @yieldparam [Hash] each result row
160
+ # @note @result and @affected_rows are also set via call to query().
161
+ def each_sym(the_query)
162
+ each_hash(the_query) do |row_hash|
163
+ yield row_hash.each_with_object({}) { |(k,v),h| h[k.to_sym] = v }
164
+ end
165
+ end
166
+
167
+ #Get the database field attributes from a query result.
168
+ # @yieldparam [Array][Mysql::Field] Array of field records
169
+ # @note fields are name (of field), table (name), def, type, length, max_length, flags,decimals
170
+ def fetch_fields
171
+ @result.fetch_fields
172
+ end
173
+
174
+ #Create WIKK::SQL instance and set up the mySQL connection, and Run a query on the DB server.
175
+ # @param db_config [Configuration] Configuration class, Hash, or any class with appropriate attr_readers.
176
+ # @param the_query [String] Sql query to send to DB server.
177
+ # @raise [Mysql] passes on Mysql errors, freeing the result.
178
+ # @yieldparam [Mysql::Result] @result and @affected_rows are also set.
179
+ # @return [Mysql::Result] @result and @affected_rows are also set.
180
+ def self.query(db_config, the_query)
181
+ sql = self.new
182
+ sql.open db_config
183
+ begin
184
+ result = sql.query(the_query)
185
+ if block_given?
186
+ yield result
187
+ end
188
+ ensure
189
+ sql.close
190
+ end
191
+ return result
192
+ end
193
+
194
+ #Create WIKK::SQL instance and set up the mySQL connection, and Run a query on the DB server.
195
+ #Yields query query results row by row, as Array
196
+ # @param db_config [Configuration] Configuration class, Hash, or any class with appropriate attr_readers.
197
+ # @param the_query [String] Sql query to send to DB server.
198
+ # @raise [Mysql] passes on Mysql errors, freeing the result.
199
+ # @yieldparam [Array] each result row
200
+ # @note @result and @affected_rows are also set via call to query().
201
+ def self.each_row(db_config, query)
202
+ sql = self.new
203
+ sql.open db_config
204
+ begin
205
+ if block_given?
206
+ sql.each_row(query) { |y| yield y }
207
+ end
208
+ ensure
209
+ sql.close
210
+ end
211
+ return sql
212
+ end
213
+
214
+ #Create WIKK::SQL instance and set up the mySQL connection, and Run a query on the DB server.
215
+ #Yields query result row by row, as Hash, using String keys
216
+ # @param db_config [Configuration] Configuration class, Hash, or any class with appropriate attr_readers.
217
+ # @param the_query [String] Sql query to send to DB server.
218
+ # @param with_table_names [Boolean] if TrueClass, then table names are included in the hash keys.
219
+ # @raise [Mysql] passes on Mysql errors, freeing the result.
220
+ # @yieldparam [Hash] each result row
221
+ # @note @result and @affected_rows are also set via call to query().
222
+ def self.each_hash(db_config, query, with_table_names=false)
223
+ sql = self.new
224
+ sql.open db_config
225
+ begin
226
+ if block_given?
227
+ sql.each_hash(query, with_table_names) do |res|
228
+ yield res
229
+ end
230
+ end
231
+ ensure
232
+ sql.close
233
+ end
234
+ return sql
235
+ end
236
+
237
+ #Create WIKK::SQL instance and set up the mySQL connection, and Run a query on the DB server.
238
+ #Yields query result row by row, as Hash using Symbol keys, so can't have table names included.
239
+ # @param db_config [Configuration] Configuration class, Hash, or any class with appropriate attr_readers.
240
+ #This can be used with keyword arguments. eg. each_sym { |key1:, key2:, ..., **rest_of_args| do something }
241
+ # @param the_query [String] Sql query to send to DB server.
242
+ # @raise [Mysql] passes on Mysql errors, freeing the result.
243
+ # @yieldparam [Hash] each result row
244
+ # @note @result and @affected_rows are also set via call to query().
245
+ def self.each_sym(db_config, query)
246
+ sql = self.new
247
+ sql.open db_config
248
+ begin
249
+ if block_given?
250
+ sql.each_sym(query) do |**res|
251
+ yield **res
252
+ end
253
+ end
254
+ ensure
255
+ sql.close
256
+ end
257
+ return sql
258
+ end
259
+ end
260
+ end
261
+
262
+
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wikk_sql
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Rob Burrowes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hoe-yard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.2
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: hoe
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.15'
41
+ description: "```"
42
+ email:
43
+ - r.burrowes@auckland.ac.nz
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files:
47
+ - History.txt
48
+ - Manifest.txt
49
+ - README.md
50
+ files:
51
+ - History.txt
52
+ - Manifest.txt
53
+ - README.md
54
+ - Rakefile
55
+ - lib/wikk_sql.rb
56
+ homepage: http://rbur004.github.com/wikk_sql/
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options:
62
+ - "--markup"
63
+ - markdown
64
+ - "--protected"
65
+ - "--title"
66
+ - wikk_sql
67
+ - "--quiet"
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.5.1
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: "```"
86
+ test_files: []