gloo-mysql 1.0

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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/gloo-mysql.rb +20 -0
  3. data/lib/mysql.rb +250 -0
  4. metadata +66 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: beba2426ad60ec2727ed3486d9187bd19a47886316aa7e2eead82035f2c6f10e
4
+ data.tar.gz: 9b7c4869ff4e00bad57da63bdd0af0862230e9c3b03f3e7188fdf99a9282a7a5
5
+ SHA512:
6
+ metadata.gz: c1f304466d3ee1f9cb1f4295d6bfc3b1adf1cab8c95fda234d1feba698efddf97251beb837ffcd9da4b1f7760b8869bbcff22eb6006e8ec41184bef90c2bfc98
7
+ data.tar.gz: d9893e456e7c933a4d4f6e7e456e1f079f13f577c8754ef426b3a53d83ba51bc745a0a6c166da54df339e9467b89392a3b0f152946f5025ca262582f09e9aa35
data/lib/gloo-mysql.rb ADDED
@@ -0,0 +1,20 @@
1
+ #
2
+ # Shim to allow `require 'gloo-mysql'`
3
+ #
4
+ # This file is loaded when someone does `require 'gloo-mysql'`
5
+ #
6
+ require 'mysql'
7
+
8
+ #
9
+ # Registers the extension.
10
+ #
11
+ class MysqlInit < Gloo::Plugin::Base
12
+
13
+ #
14
+ # Register verbs and objects.
15
+ #
16
+ def register( callback )
17
+ callback.register_obj( Mysql )
18
+ end
19
+
20
+ end
data/lib/mysql.rb ADDED
@@ -0,0 +1,250 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2020 Eric Crane. All rights reserved.
3
+ #
4
+ # A MySQL database connection.
5
+ #
6
+ #
7
+ # https://github.com/brianmario/mysql2
8
+ # https://www.rubydoc.info/gems/mysql2/0.2.3/Mysql2/Client
9
+ #
10
+ # Connection Parameters
11
+ # user = opts[:username]
12
+ # pass = opts[:password]
13
+ # host = opts[:host] || 'localhost'
14
+ # port = opts[:port] || 3306
15
+ # database = opts[:database]
16
+ # socket = opts[:socket]
17
+ # flags = opts[:flags] || 0
18
+ #
19
+ require 'mysql2'
20
+
21
+ class Mysql < Gloo::Core::Obj
22
+
23
+ KEYWORD = 'mysql'.freeze
24
+ KEYWORD_SHORT = 'mysql'.freeze
25
+
26
+ HOST = 'host'.freeze
27
+ DB = 'database'.freeze
28
+ USER = 'username'.freeze
29
+ PASSWD = 'password'.freeze
30
+
31
+ #
32
+ # The name of the object type.
33
+ #
34
+ def self.typename
35
+ return KEYWORD
36
+ end
37
+
38
+ #
39
+ # The short name of the object type.
40
+ #
41
+ def self.short_typename
42
+ return KEYWORD_SHORT
43
+ end
44
+
45
+ # ---------------------------------------------------------------------
46
+ # Children
47
+ # ---------------------------------------------------------------------
48
+
49
+ #
50
+ # Does this object have children to add when an object
51
+ # is created in interactive mode?
52
+ # This does not apply during obj load, etc.
53
+ #
54
+ def add_children_on_create?
55
+ return true
56
+ end
57
+
58
+ #
59
+ # Add children to this object.
60
+ # This is used by containers to add children needed
61
+ # for default configurations.
62
+ #
63
+ def add_default_children
64
+ fac = @engine.factory
65
+ fac.create_string HOST, nil, self
66
+ fac.create_string DB, nil, self
67
+ fac.create_string USER, nil, self
68
+ fac.create_string PASSWD, nil, self
69
+ end
70
+
71
+ # ---------------------------------------------------------------------
72
+ # Messages
73
+ # ---------------------------------------------------------------------
74
+
75
+ #
76
+ # Get a list of message names that this object receives.
77
+ #
78
+ def self.messages
79
+ return super + [ 'verify' ]
80
+ end
81
+
82
+ #
83
+ # SSH to the host and execute the command, then update result.
84
+ #
85
+ def msg_verify
86
+ return unless connects?
87
+
88
+ @engine.heap.it.set_to true
89
+ end
90
+
91
+
92
+ # ---------------------------------------------------------------------
93
+ # DB functions (all database connections)
94
+ # ---------------------------------------------------------------------
95
+
96
+ #
97
+ # Get the client object.
98
+ # It might be cached, so check first.
99
+ # If it is not cached, create a new one.
100
+ #
101
+ def get_client
102
+ app = @engine.running_app
103
+
104
+ client = app.db_client_for_obj( self ) if app
105
+
106
+ if client && client.ping
107
+ @engine.log.debug "Connection is established and active."
108
+ return client
109
+ elsif client
110
+ @engine.log.debug "Connection is established but NOT active. Reconnecting."
111
+ else
112
+ @engine.log.debug "Opening a new Connection."
113
+ end
114
+
115
+ h = {
116
+ host: host_value,
117
+ database: db_value,
118
+ username: user_value,
119
+ password: passwd_value
120
+ }
121
+ client = Mysql2::Client.new( h )
122
+
123
+ app.cache_db_client( self, client ) if app
124
+
125
+ return client
126
+ end
127
+
128
+ #
129
+ # Open a connection and execute the SQL statement.
130
+ # Return the resulting data.
131
+ #
132
+ def query( sql, params = nil )
133
+ client = get_client
134
+
135
+ heads = []
136
+ data = []
137
+ begin
138
+ if params
139
+ pst = client.prepare( sql )
140
+ rs = pst.execute( *params, :as => :array )
141
+ if rs
142
+ rs.each do |row|
143
+ arr = []
144
+ row.each do |o|
145
+ arr << o
146
+ end
147
+ data << arr
148
+ end
149
+ end
150
+ else
151
+ rs = client.query( sql, :as => :array )
152
+ if rs
153
+ rs.each do |row|
154
+ data << row
155
+ end
156
+ end
157
+ end
158
+
159
+ heads = rs.fields if rs
160
+ rescue => e
161
+ @engine.log_exception e
162
+ end
163
+
164
+ return [ heads, data ]
165
+ end
166
+
167
+ #
168
+ # Based on the result set, build a QueryResult object.
169
+ #
170
+ def get_query_result( result )
171
+ return QueryResult.new result[0], result[1], @engine
172
+ end
173
+
174
+
175
+ # ---------------------------------------------------------------------
176
+ # Private functions
177
+ # ---------------------------------------------------------------------
178
+
179
+ private
180
+
181
+ #
182
+ # Get the host from the child object.
183
+ # Returns nil if there is none.
184
+ #
185
+ def host_value
186
+ o = find_child HOST
187
+ return nil unless o
188
+
189
+ o = Gloo::Objs::Alias.resolve_alias( @engine, o )
190
+ return o.value
191
+ end
192
+
193
+ #
194
+ # Get the Database name from the child object.
195
+ # Returns nil if there is none.
196
+ #
197
+ def db_value
198
+ o = find_child DB
199
+ return nil unless o
200
+
201
+ o = Gloo::Objs::Alias.resolve_alias( @engine, o )
202
+ return o.value
203
+ end
204
+
205
+ #
206
+ # Get the Username from the child object.
207
+ # Returns nil if there is none.
208
+ #
209
+ def user_value
210
+ o = find_child USER
211
+ return nil unless o
212
+
213
+ o = Gloo::Objs::Alias.resolve_alias( @engine, o )
214
+ return o.value
215
+ end
216
+
217
+ #
218
+ # Get the Password name from the child object.
219
+ # Returns nil if there is none.
220
+ #
221
+ def passwd_value
222
+ o = find_child PASSWD
223
+ return nil unless o
224
+
225
+ o = Gloo::Objs::Alias.resolve_alias( @engine, o )
226
+ return o.value
227
+ end
228
+
229
+ #
230
+ # Try the connection and make sure it works.
231
+ # Returns true if we can establish a connection.
232
+ #
233
+ def connects?
234
+ begin
235
+ h = {
236
+ host: host_value,
237
+ database: db_value,
238
+ username: user_value,
239
+ password: passwd_value
240
+ }
241
+ Mysql2::Client.new( h )
242
+ rescue => e
243
+ @engine.log_exception e
244
+ @engine.heap.it.set_to false
245
+ return false
246
+ end
247
+ return true
248
+ end
249
+
250
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gloo-mysql
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Eric Crane
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-02-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mysql2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.5'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.5.3
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.5'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.5.3
33
+ description: Adds MySQL support to Gloo.
34
+ email:
35
+ - eric.crane@mac.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - lib/gloo-mysql.rb
41
+ - lib/mysql.rb
42
+ homepage: https://gloo.ecrane.us/
43
+ licenses:
44
+ - MIT
45
+ metadata:
46
+ gloo.type: core-library
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.5.16
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Gloo core library. MySQL support.
66
+ test_files: []