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