spectre-mysql 1.0.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 (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/spectre/mysql.rb +102 -0
  3. metadata +76 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 685821f911b5f2737bf5108616c72f76a92b5dd3dd5bbc0a8914752b719e491c
4
+ data.tar.gz: 1b02c99301bf149e3987e7f7fbf39d52b9aa2d0f81ff232a4efb067177e8b138
5
+ SHA512:
6
+ metadata.gz: 5d8748f72ceba93ee2e97fff503bcf3a4f4f8de2fd204bcad6935b925e78aee3014495c9c65d4b291641c0a47f50058da32feacee84840835b97f52fb4b72317
7
+ data.tar.gz: e6cfb2acd654c2c6b77a56e8bc1b8053561a6281401144e68a55c74db4761ecade71ce662bae3ad7f6e39a7e6919ecb75a642a7ae6ab70eab7dbf4d9b5cd711e
@@ -0,0 +1,102 @@
1
+ require 'logger'
2
+ require 'mysql2'
3
+ require 'spectre'
4
+ require 'ostruct'
5
+
6
+
7
+ module Spectre
8
+ module MySQL
9
+
10
+ class MySqlQuery < Spectre::DslClass
11
+ def initialize query
12
+ @__query = query
13
+ end
14
+
15
+ def host hostname
16
+ @__query['host'] = hostname
17
+ end
18
+
19
+ def username user
20
+ @__query['username'] = user
21
+ end
22
+
23
+ def password pass
24
+ @__query['password'] = pass
25
+ end
26
+
27
+ def database name
28
+ @__query['database'] = name
29
+ end
30
+
31
+ def query statement
32
+ @__query['query'] = [] if not @__query.key? 'query'
33
+ @__query['query'].append(statement)
34
+ end
35
+ end
36
+
37
+ class << self
38
+ @@logger = ::Logger.new(STDOUT)
39
+ @@mysql_cfg = {}
40
+ @@result = nil
41
+ @@last_conn = nil
42
+
43
+ def mysql name = nil, &block
44
+ query = {}
45
+
46
+ if name != nil and @@mysql_cfg.key? name
47
+ query.merge! @@mysql_cfg[name]
48
+ raise "No `host' set for MySQL client '#{name}'. Check your MySQL config in your environment." if !query['host']
49
+ elsif name != nil
50
+ query['host'] = name
51
+ elsif @@last_conn == nil
52
+ raise 'No name given and there was no previous MySQL connection to use'
53
+ end
54
+
55
+ MySqlQuery.new(query).instance_eval(&block) if block_given?
56
+
57
+ if name != nil
58
+ @@last_conn = {
59
+ host: query['host'],
60
+ username: query['username'],
61
+ password: query['password'],
62
+ database: query['database']
63
+ }
64
+ end
65
+
66
+ @@logger.info "Connecting to database #{query['username']}@#{query['host']}:#{query['database']}"
67
+
68
+ client = ::Mysql2::Client.new(**@@last_conn)
69
+
70
+ res = []
71
+
72
+ query['query'].each do |statement|
73
+ @@logger.info 'Executing statement "' + statement + '"'
74
+ res = client.query(statement, cast_booleans: true)
75
+ end if query['query']
76
+
77
+ @@result = res.map { |row| OpenStruct.new row } if res
78
+
79
+ client.close
80
+ end
81
+
82
+ def result
83
+ raise 'No MySQL query has been executed yet' unless @@result
84
+ @@result
85
+ end
86
+ end
87
+
88
+ Spectre.register do |config|
89
+ @@logger = ::Logger.new config['log_file'], progname: 'spectre/mysql'
90
+
91
+ if config.key? 'mysql'
92
+ @@mysql_cfg = {}
93
+
94
+ config['mysql'].each do |name, cfg|
95
+ @@mysql_cfg[name] = cfg
96
+ end
97
+ end
98
+ end
99
+
100
+ Spectre.delegate :mysql, :result, to: self
101
+ end
102
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spectre-mysql
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Christian Neubauer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-06-10 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.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.5.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: spectre-core
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.8.4
41
+ description: Adds MySQL access functionality to the spectre framework
42
+ email:
43
+ - me@christianneubauer.de
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/spectre/mysql.rb
49
+ homepage: https://bitbucket.org/cneubaur/spectre-mysql
50
+ licenses:
51
+ - MIT
52
+ metadata:
53
+ allowed_push_host: https://rubygems.org/
54
+ homepage_uri: https://bitbucket.org/cneubaur/spectre-mysql
55
+ source_code_uri: https://bitbucket.org/cneubaur/spectre-mysql
56
+ changelog_uri: https://bitbucket.org/cneubaur/spectre-mysql/src/master/CHANGELOG.md
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 2.5.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.0.8
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: MySQL module for spectre
76
+ test_files: []