db-mariadb 0.1.0 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e48fc978d5a9616b176cfd22ebaac97ef4aa64ef46e8741c4842f3ac22bffd0f
4
- data.tar.gz: '0349fcd36f4b9a4fa36c4c75fca6a3d780b02056dc418a941118533b60ffe16b'
3
+ metadata.gz: a86f522a5df8d261feb1be7625f9ac23ba8768184f4103a3479a6d6acfb2638e
4
+ data.tar.gz: 551fe857397d0921d0f134c3701b26ff04cc4eabe1995c63128c0045350b17a2
5
5
  SHA512:
6
- metadata.gz: 950fed54c6d1eb1c71c4f81657b49ee82aa0b2fed6f2598d62757064522db8b94dd8a80bc38131ffbdc12d950334e46e6258242876d261cd1bb263c3da4ea36a
7
- data.tar.gz: e4d6eaca7a2ce5b095301f8af3d34b84e20594db296e7f2842e12886cf5faecf5dfe3ffbef6ddb6ac247f8cf7478437aa182fe2da6abfa7e883658ca9619f71c
6
+ metadata.gz: 978358ac2a66906fa116fa610d3280863b16412624f7d9ece0d17f010b70dd20a399d474c6005dc87ffb5756381ac8cf8dd4733935acd4cdc0deebcad195c137
7
+ data.tar.gz: 4f1a7fb2d36f7c243ad7e737bb75dc79cf352284975da61a95cbbb7d644539aa9e5131fb811ce34aba5b3cd24fa195dab09c30a1b622dca62e8fba8f8061679d
@@ -27,14 +27,14 @@ module DB
27
27
  LOCAL = "mysql://localhost/test"
28
28
 
29
29
  class Adapter
30
- def initialize(connection_string = LOCAL)
31
- @connection_string = connection_string
30
+ def initialize(**options)
31
+ @options = options
32
32
  end
33
33
 
34
- attr :connection_string
34
+ attr :options
35
35
 
36
36
  def call
37
- Connection.new(self.connection_string)
37
+ Connection.new(**@options)
38
38
  end
39
39
  end
40
40
  end
@@ -21,10 +21,18 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  require 'async/pool/resource'
24
+ require 'async/io/generic'
25
+
24
26
  require_relative 'native/connection'
25
27
 
26
28
  module DB
27
29
  module MariaDB
30
+ module IO
31
+ def self.new(fd, mode)
32
+ Async::IO::Generic.new(::IO.new(fd, mode, autoclose: false))
33
+ end
34
+ end
35
+
28
36
  # This implements the interface between the underyling native interface interface and "standardised" connection interface.
29
37
  class Connection < Async::Pool::Resource
30
38
  def initialize(**options)
@@ -49,8 +57,12 @@ module DB
49
57
  case value
50
58
  when Numeric
51
59
  buffer << value.to_s
60
+ when TrueClass
61
+ buffer << 'TRUE'
62
+ when FalseClass
63
+ buffer << 'FALSE'
52
64
  when nil
53
- buffer << "NULL"
65
+ buffer << 'NULL'
54
66
  else
55
67
  append_string(value, buffer)
56
68
  end
@@ -64,11 +76,27 @@ module DB
64
76
  return buffer
65
77
  end
66
78
 
79
+ def id_column(name = 'id', primary_key: true)
80
+ buffer = String.new
81
+
82
+ append_identifier(name, buffer)
83
+
84
+ buffer << " BIGINT AUTO_INCREMENT"
85
+
86
+ if primary_key
87
+ buffer << " PRIMARY KEY"
88
+ end
89
+
90
+ return buffer
91
+ end
92
+
67
93
  def status
68
94
  @native.status
69
95
  end
70
96
 
71
97
  def send_query(statement)
98
+ @native.discard_results
99
+
72
100
  @native.send_query(statement)
73
101
  end
74
102
 
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ module DB
24
+ module MariaDB
25
+ class Error < StandardError
26
+ end
27
+ end
28
+ end
@@ -19,7 +19,7 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  require_relative 'result'
22
- require 'async/io/generic'
22
+ require_relative '../error'
23
23
 
24
24
  module DB
25
25
  module MariaDB
@@ -48,6 +48,7 @@ module DB
48
48
 
49
49
  attach_function :mysql_use_result, [:pointer], :pointer
50
50
  attach_function :mysql_next_result, [:pointer], :int
51
+ attach_function :mysql_more_results, [:pointer], :int
51
52
  attach_function :mysql_free_result, [:pointer], :void
52
53
 
53
54
  attach_function :mysql_affected_rows, [:pointer], :uint64
@@ -62,14 +63,8 @@ module DB
62
63
 
63
64
  attach_function :mysql_real_escape_string, [:pointer, :pointer, :string, :size_t], :size_t
64
65
 
65
- module IO
66
- def self.new(fd, mode)
67
- Async::IO::Generic.new(::IO.new(fd, mode, autoclose: false))
68
- end
69
- end
70
-
71
66
  class Connection < FFI::Pointer
72
- def self.connect(host: 'localhost', user: nil, password: nil, database: nil, port: 0, unix_socket: nil, client_flags: 0, compression: false, types: DEFAULT_TYPES, **options)
67
+ def self.connect(io: IO, host: 'localhost', user: nil, password: nil, database: nil, port: 0, unix_socket: nil, client_flags: 0, compression: false, types: DEFAULT_TYPES, **options)
73
68
  pointer = Native.mysql_init(nil)
74
69
  Native.mysql_options(pointer, MYSQL_OPT_NONBLOCK, nil)
75
70
 
@@ -100,13 +95,13 @@ module DB
100
95
  end
101
96
 
102
97
  if result.read_pointer.null?
103
- raise "Could not connect: #{Native.mysql_error(pointer)}!"
98
+ raise Error, "Could not connect: #{Native.mysql_error(pointer)}!"
104
99
  end
105
100
 
106
- return self.new(pointer, io, types: types, **options)
101
+ return self.new(pointer, io, types, **options)
107
102
  end
108
103
 
109
- def initialize(address, io, types: {})
104
+ def initialize(address, io, types, **options)
110
105
  super(address)
111
106
 
112
107
  @io = io
@@ -125,7 +120,7 @@ module DB
125
120
 
126
121
  def check_error!(message)
127
122
  if Native.mysql_errno(self) != 0
128
- raise "#{message}: #{Native.mysql_error(self)}!"
123
+ raise Error, "#{message}: #{Native.mysql_error(self)}!"
129
124
  end
130
125
  end
131
126
 
@@ -174,42 +169,62 @@ module DB
174
169
  end
175
170
 
176
171
  if error.read_int != 0
177
- raise "Could not send query: #{Native.mysql_error(self)}!"
172
+ raise Error, "Could not send query: #{Native.mysql_error(self)}!"
178
173
  end
179
174
  end
180
175
 
176
+ # @returns [Boolean] If there are more results.
177
+ def more_results?
178
+ Native.mysql_more_results(self) == 1
179
+ end
180
+
181
181
  def next_result(types: @types)
182
+ if result = self.get_result
183
+ return Result.new(self, types, result)
184
+ end
185
+ end
186
+
187
+ # Silently discard any results that application didn't read.
188
+ def discard_results
189
+ while result = self.get_result
190
+ end
191
+
192
+ return nil
193
+ end
194
+
195
+ def affected_rows
196
+ Native.mysql_affected_rows(self)
197
+ end
198
+
199
+ def insert_id
200
+ Native.mysql_insert_id(self)
201
+ end
202
+
203
+ def info
204
+ Native.mysql_info(self)
205
+ end
206
+
207
+ protected
208
+ def get_result
182
209
  if @result
183
210
  self.free_result
184
211
 
185
212
  # Successful and there are no more results:
186
213
  return if Native.mysql_next_result(self) == -1
187
214
 
188
- check_error!("Next result")
215
+ check_error!("Get result")
189
216
  end
190
217
 
191
218
  @result = Native.mysql_use_result(self)
192
219
 
193
220
  if @result.null?
194
- check_error!("Next result")
221
+ check_error!("Get result")
195
222
 
196
223
  return nil
197
224
  else
198
- return Result.new(self, types, @result)
225
+ return @result
199
226
  end
200
227
  end
201
-
202
- def affected_rows
203
- Native.mysql_affected_rows(self)
204
- end
205
-
206
- def insert_id
207
- Native.mysql_insert_id(self)
208
- end
209
-
210
- def info
211
- Native.mysql_info(self)
212
- end
213
228
  end
214
229
  end
215
230
  end
@@ -25,7 +25,7 @@ require_relative 'types'
25
25
  module DB
26
26
  module MariaDB
27
27
  module Native
28
- Type = enum(FFI::Type::UCHAR,
28
+ Type = enum(
29
29
  :decimal,
30
30
  :tiny,
31
31
  :short,
@@ -98,7 +98,8 @@ module DB
98
98
  :flags, :uint,
99
99
  :decimals, :uint,
100
100
  :charsetnr, :uint,
101
- :type, Type
101
+ :type, Type,
102
+ :extension, :pointer,
102
103
  )
103
104
 
104
105
  def name
@@ -20,6 +20,6 @@
20
20
 
21
21
  module DB
22
22
  module MariaDB
23
- VERSION = "0.1.0"
23
+ VERSION = "0.2.4"
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db-mariadb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-30 00:00:00.000000000 Z
11
+ date: 2020-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi
@@ -114,10 +114,10 @@ executables: []
114
114
  extensions: []
115
115
  extra_rdoc_files: []
116
116
  files:
117
- - lib/.DS_Store
118
117
  - lib/db/mariadb.rb
119
118
  - lib/db/mariadb/adapter.rb
120
119
  - lib/db/mariadb/connection.rb
120
+ - lib/db/mariadb/error.rb
121
121
  - lib/db/mariadb/native.rb
122
122
  - lib/db/mariadb/native/connection.rb
123
123
  - lib/db/mariadb/native/field.rb
@@ -143,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
143
  - !ruby/object:Gem::Version
144
144
  version: '0'
145
145
  requirements: []
146
- rubygems_version: 3.1.2
146
+ rubygems_version: 3.0.3
147
147
  signing_key:
148
148
  specification_version: 4
149
149
  summary: An event-driven interface for MariaDB and MySQL servers.
Binary file