vidalia 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/vidalia/mysql.rb +156 -0
- data/lib/vidalia.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 92a70e783f733342346ebffc2d6b66a9f96f97eb
|
4
|
+
data.tar.gz: 4a3507832379a02bea49cc367ba2bbc4bb3b668e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 07778b6c8d0679fb6d98851b285fb88e97e5f8588b7168338e790bd510a151cdc60684d17ccda1019baf5aad54b2802f145d6521a4c91b66fe4f4aa36a755191
|
7
|
+
data.tar.gz: 5c49f169c08208c1f69f7b5807b2ab4a66f875937d3eace20009816897b8e161512e75597944aafdf8540d9bae53386f8fefd734a615ffe3342ac59437c7a748
|
@@ -0,0 +1,156 @@
|
|
1
|
+
module Vidalia
|
2
|
+
|
3
|
+
class Mysql
|
4
|
+
|
5
|
+
require 'mysql2'
|
6
|
+
|
7
|
+
# Instantiate a database object
|
8
|
+
#
|
9
|
+
# @param printable_name [String] Human-readable name of the database (for
|
10
|
+
# logging purposes)
|
11
|
+
# @param host [String] Hostname of the database
|
12
|
+
# @param port [String] Port on which the database can be accessed.
|
13
|
+
# @param database_name [String] Name of the database (in MySQL)
|
14
|
+
# @param username [String] Database account username
|
15
|
+
# @param password [String] Database account password
|
16
|
+
# @param sslca [String] Full file path of the SSL CA bundle
|
17
|
+
# @example
|
18
|
+
# db = Vidalia::Mysql.new(
|
19
|
+
# printable_name: 'Application MySQL Database',
|
20
|
+
# host: 'sql.server.com',
|
21
|
+
# database_name: 'appdb01',
|
22
|
+
# username: 'admin',
|
23
|
+
# password: 'passw0rd',
|
24
|
+
# sslca: '/tmp/bundle.pem'
|
25
|
+
# )
|
26
|
+
#
|
27
|
+
def initialize(
|
28
|
+
printable_name:,
|
29
|
+
host:,
|
30
|
+
port: '3306',
|
31
|
+
database_name:,
|
32
|
+
username:,
|
33
|
+
password:,
|
34
|
+
sslca: nil
|
35
|
+
)
|
36
|
+
@printable_name = printable_name
|
37
|
+
@username = username
|
38
|
+
@password = password
|
39
|
+
@host = host
|
40
|
+
@database_name = database_name
|
41
|
+
@port = port
|
42
|
+
@sslca = sslca
|
43
|
+
end
|
44
|
+
|
45
|
+
# Run a query against the database
|
46
|
+
#
|
47
|
+
# This method opens a database connection and ensures that it is closed in
|
48
|
+
# the event of an error.
|
49
|
+
#
|
50
|
+
# @param query [String] SQL query to be run against the database.
|
51
|
+
# @returns results [Array] Array of rows of query results
|
52
|
+
# @example
|
53
|
+
# db = Vidalia::Mysql.new(
|
54
|
+
# printable_name: 'Application MySQL Database',
|
55
|
+
# host: 'sql.server.com',
|
56
|
+
# database_name: 'appdb01',
|
57
|
+
# username: 'admin',
|
58
|
+
# password: 'passw0rd',
|
59
|
+
# sslca: '/tmp/bundle.pem'
|
60
|
+
# )
|
61
|
+
# db.run(query: "SELECT * FROM my_table")
|
62
|
+
#
|
63
|
+
def run(query:)
|
64
|
+
Vidalia::log("SQL query text: \"#{query}\"",:style => :debug)
|
65
|
+
open_connection
|
66
|
+
result = launch_query_command(query: query)
|
67
|
+
if result.size > 0
|
68
|
+
count = 1
|
69
|
+
result.each do |r|
|
70
|
+
Vidalia::log("SQL query result (#{count}): \"#{r.inspect}\"",:style => :debug)
|
71
|
+
count += 1
|
72
|
+
end
|
73
|
+
else
|
74
|
+
Vidalia::log("SQL query: NO RESULTS FOUND",:style => :debug)
|
75
|
+
end
|
76
|
+
rescue Exception => e
|
77
|
+
raise e
|
78
|
+
else
|
79
|
+
return result
|
80
|
+
ensure
|
81
|
+
close_connection
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
# Open a connection to the database
|
87
|
+
#
|
88
|
+
# @example
|
89
|
+
# db = Vidalia::Mysql.new(
|
90
|
+
# printable_name: 'Application MySQL Database',
|
91
|
+
# host: 'sql.server.com',
|
92
|
+
# database_name: 'appdb01',
|
93
|
+
# username: 'admin',
|
94
|
+
# password: 'passw0rd',
|
95
|
+
# sslca: '/tmp/bundle.pem'
|
96
|
+
# )
|
97
|
+
# db.open_connection
|
98
|
+
#
|
99
|
+
def open_connection
|
100
|
+
if sslca then
|
101
|
+
@db = Mysql2::Client.new(
|
102
|
+
host: @host,
|
103
|
+
username: @username,
|
104
|
+
password: @password,
|
105
|
+
port: @port,
|
106
|
+
database: @database_name,
|
107
|
+
sslca: @sslca)
|
108
|
+
else
|
109
|
+
@db = Mysql2::Client.new(
|
110
|
+
host: @host,
|
111
|
+
username: @username,
|
112
|
+
password: @password,
|
113
|
+
port: @port,
|
114
|
+
database: @database_name)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
# Run a query against the database
|
119
|
+
#
|
120
|
+
# @param query [String] SQL query to be run against the database
|
121
|
+
# @returns results [Array] Array of rows of query results
|
122
|
+
# @example
|
123
|
+
# db = Vidalia::Mysql.new(
|
124
|
+
# printable_name: 'Application MySQL Database',
|
125
|
+
# host: 'sql.server.com',
|
126
|
+
# database_name: 'appdb01',
|
127
|
+
# username: 'admin',
|
128
|
+
# password: 'passw0rd',
|
129
|
+
# sslca: '/tmp/bundle.pem'
|
130
|
+
# )
|
131
|
+
# db.open
|
132
|
+
# my_results = db.query("SELECT * FROM my_table")
|
133
|
+
#
|
134
|
+
def launch_query_command(query:)
|
135
|
+
@db.query(query, as: :hash)
|
136
|
+
end
|
137
|
+
|
138
|
+
# Close a connection to the database
|
139
|
+
#
|
140
|
+
# @example
|
141
|
+
# db = Vidalia::Mysql.new(
|
142
|
+
# printable_name: 'Application MySQL Database',
|
143
|
+
# host: 'sql.server.com',
|
144
|
+
# database_name: 'appdb01',
|
145
|
+
# username: 'admin',
|
146
|
+
# password: 'passw0rd',
|
147
|
+
# sslca: '/tmp/bundle.pem'
|
148
|
+
# )
|
149
|
+
# db.open_connection
|
150
|
+
# db.close_connection
|
151
|
+
#
|
152
|
+
def close_connection
|
153
|
+
@db.close if @db
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
data/lib/vidalia.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vidalia
|
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
|
- Jeremy Rotter
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-09-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -38,6 +38,7 @@ files:
|
|
38
38
|
- lib/vidalia/element_definition.rb
|
39
39
|
- lib/vidalia/interface.rb
|
40
40
|
- lib/vidalia/interface_definition.rb
|
41
|
+
- lib/vidalia/mysql.rb
|
41
42
|
- lib/vidalia/object.rb
|
42
43
|
- lib/vidalia/object_definition.rb
|
43
44
|
homepage: https://github.com/jrotter/vidalia
|