db-mariadb 0.2.0 → 0.3.0

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: a67b6bf206b453ebb0fb94ee60f27581bc6ebe6d9acdc5190e6e61b3ba35a839
4
- data.tar.gz: afca519606ee8d3125ac11c1301612dcb496d1bf93eeaec951e7162c0ff6b7fa
3
+ metadata.gz: d18fa619ccfc3b043299790a3e6a275670ce48614304355e3f75ba5ed1cdc202
4
+ data.tar.gz: eb9e90465532de40869b545c1a65158136e2efbedeff6440f061c4f45d486e8d
5
5
  SHA512:
6
- metadata.gz: 94b8a9d6b33e6603dfafe5b0935cb335f13c1265da97a7f814bdff5cc9cd4ca91cbfc396d8723260232f00025c2a237c34287ab765c6a4b8f8c730840a32e0e6
7
- data.tar.gz: c15bfe8bf00dfde7c022e88a737382eb4e0ceb8ece8af80a5e54eb70d5f368cfe3b500c2806d3c4f65f9ca12b61f62d720bce0c8c3fab09690cb816a40ee2afe
6
+ metadata.gz: ec5a4ceca8186959f19019e92cc77a53e945e4834f2c7776d60b78688c32d5f614e243a868b19f584e93c70bd8c19360525a1e2b11d9b597a37a83ae247e239b
7
+ data.tar.gz: 65b204be2c1c3f4dfa24f114246a2ec32913aceb580c7be40333aafe8c89a5680098612fae30136f8d537821d246ae9bf508def1f367d57db9c0bb59f767bd82
@@ -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,16 +21,12 @@
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
28
- module IO
29
- def self.new(fd, mode)
30
- Async::IO::Generic.new(::IO.new(fd, mode, autoclose: false))
31
- end
32
- end
33
-
34
30
  # This implements the interface between the underyling native interface interface and "standardised" connection interface.
35
31
  class Connection < Async::Pool::Resource
36
32
  def initialize(**options)
@@ -74,11 +70,27 @@ module DB
74
70
  return buffer
75
71
  end
76
72
 
73
+ def id_column(name = 'id', primary_key: true)
74
+ buffer = String.new
75
+
76
+ append_identifier(name, buffer)
77
+
78
+ buffer << " BIGINT AUTO_INCREMENT"
79
+
80
+ if primary_key
81
+ buffer << " PRIMARY KEY"
82
+ end
83
+
84
+ return buffer
85
+ end
86
+
77
87
  def status
78
88
  @native.status
79
89
  end
80
90
 
81
91
  def send_query(statement)
92
+ @native.discard_results
93
+
82
94
  @native.send_query(statement)
83
95
  end
84
96
 
@@ -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,6 +63,12 @@ module DB
62
63
 
63
64
  attach_function :mysql_real_escape_string, [:pointer, :pointer, :string, :size_t], :size_t
64
65
 
66
+ module IO
67
+ def self.new(fd, mode)
68
+ Async::IO::Generic.new(::IO.new(fd, mode, autoclose: false))
69
+ end
70
+ end
71
+
65
72
  class Connection < FFI::Pointer
66
73
  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)
67
74
  pointer = Native.mysql_init(nil)
@@ -94,13 +101,13 @@ module DB
94
101
  end
95
102
 
96
103
  if result.read_pointer.null?
97
- raise "Could not connect: #{Native.mysql_error(pointer)}!"
104
+ raise Error, "Could not connect: #{Native.mysql_error(pointer)}!"
98
105
  end
99
106
 
100
107
  return self.new(pointer, io, types, **options)
101
108
  end
102
109
 
103
- def initialize(address, io, types)
110
+ def initialize(address, io, types, **options)
104
111
  super(address)
105
112
 
106
113
  @io = io
@@ -119,7 +126,7 @@ module DB
119
126
 
120
127
  def check_error!(message)
121
128
  if Native.mysql_errno(self) != 0
122
- raise "#{message}: #{Native.mysql_error(self)}!"
129
+ raise Error, "#{message}: #{Native.mysql_error(self)}!"
123
130
  end
124
131
  end
125
132
 
@@ -168,42 +175,62 @@ module DB
168
175
  end
169
176
 
170
177
  if error.read_int != 0
171
- raise "Could not send query: #{Native.mysql_error(self)}!"
178
+ raise Error, "Could not send query: #{Native.mysql_error(self)}!"
172
179
  end
173
180
  end
174
181
 
182
+ # @returns [Boolean] If there are more results.
183
+ def more_results?
184
+ Native.mysql_more_results(self) == 1
185
+ end
186
+
175
187
  def next_result(types: @types)
188
+ if result = self.get_result
189
+ return Result.new(self, types, result)
190
+ end
191
+ end
192
+
193
+ # Silently discard any results that application didn't read.
194
+ def discard_results
195
+ while result = self.get_result
196
+ end
197
+
198
+ return nil
199
+ end
200
+
201
+ def affected_rows
202
+ Native.mysql_affected_rows(self)
203
+ end
204
+
205
+ def insert_id
206
+ Native.mysql_insert_id(self)
207
+ end
208
+
209
+ def info
210
+ Native.mysql_info(self)
211
+ end
212
+
213
+ protected
214
+ def get_result
176
215
  if @result
177
216
  self.free_result
178
217
 
179
218
  # Successful and there are no more results:
180
219
  return if Native.mysql_next_result(self) == -1
181
220
 
182
- check_error!("Next result")
221
+ check_error!("Get result")
183
222
  end
184
223
 
185
224
  @result = Native.mysql_use_result(self)
186
225
 
187
226
  if @result.null?
188
- check_error!("Next result")
227
+ check_error!("Get result")
189
228
 
190
229
  return nil
191
230
  else
192
- return Result.new(self, types, @result)
231
+ return @result
193
232
  end
194
233
  end
195
-
196
- def affected_rows
197
- Native.mysql_affected_rows(self)
198
- end
199
-
200
- def insert_id
201
- Native.mysql_insert_id(self)
202
- end
203
-
204
- def info
205
- Native.mysql_info(self)
206
- end
207
234
  end
208
235
  end
209
236
  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.2.0"
23
+ VERSION = "0.3.0"
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db-mariadb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -117,6 +117,7 @@ files:
117
117
  - lib/db/mariadb.rb
118
118
  - lib/db/mariadb/adapter.rb
119
119
  - lib/db/mariadb/connection.rb
120
+ - lib/db/mariadb/error.rb
120
121
  - lib/db/mariadb/native.rb
121
122
  - lib/db/mariadb/native/connection.rb
122
123
  - lib/db/mariadb/native/field.rb
@@ -142,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
143
  - !ruby/object:Gem::Version
143
144
  version: '0'
144
145
  requirements: []
145
- rubygems_version: 3.1.2
146
+ rubygems_version: 3.0.3
146
147
  signing_key:
147
148
  specification_version: 4
148
149
  summary: An event-driven interface for MariaDB and MySQL servers.