db-mariadb 0.2.1 → 0.3.1

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: 8b625dc4c4f14093794812d057af4aced8ca7ed30f65e5b0c6b8a0b29a6262f1
4
- data.tar.gz: e42b0449b3609e63df290e7ff75a6c1b7319ff136641483a4428f5b6a9df0d2d
3
+ metadata.gz: 3b1fb0496909a9d442eefe8576721a57eb4affee9693f9bb7c69d682546d74b2
4
+ data.tar.gz: a98067cc177c1ee492769caa3ffcae13453f913e240a71a92b71a562ed9c778c
5
5
  SHA512:
6
- metadata.gz: a9bc494987c948c66a7898c404701ca2a002ae9838d4af632d638fbcde44691fd83270a6a86ec7579c52e224e1d42ebf985bbca669ad7eb4295f2f622ccf3d60
7
- data.tar.gz: fe00875308c4d7bd14eb4966c4bb86ad1ddb7aa48e5e82e888adc0b4a93c9ece55b0328ec9e18f9ef015ac5406346f3f0e2e648e7ad3c586026757346f19448a
6
+ metadata.gz: 4f3fd799d28712eea7ca8e27471c515dfd18925e11204883a601f7f4a91b831477bbda99a696f50d84cc921487b0531b44771dc0cc45deb876dbefc2adf8bd4f
7
+ data.tar.gz: 36b0ea16a2b0a2249a9f731e6ddb5de6999d21f691cd1a02e764ca7ca049a1aaccbdac316cc484546e6f79ce5111ab30862bdae666ed1fbb3afe786b049060ce
@@ -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,29 +70,33 @@ 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
 
85
97
  def next_result
86
98
  @native.next_result
87
99
  end
88
-
89
- def call(statement, streaming: false)
90
- @native.send_query(statement)
91
-
92
- last_result = nil
93
-
94
- while result = @native.next_result
95
- last_result = result
96
- end
97
-
98
- return last_result
99
- end
100
100
  end
101
101
  end
102
102
  end
@@ -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,8 +63,14 @@ 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
- 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
+ def self.connect(wrapper: 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)
68
75
  Native.mysql_options(pointer, MYSQL_OPT_NONBLOCK, nil)
69
76
 
@@ -78,7 +85,7 @@ module DB
78
85
  status = Native.mysql_real_connect_start(result, pointer, host, user, password, database, port, unix_socket, client_flags);
79
86
 
80
87
  if status > 0
81
- io = IO.new(Native.mysql_get_socket(pointer), "r+")
88
+ io = wrapper.new(Native.mysql_get_socket(pointer), "r+")
82
89
 
83
90
  while status > 0
84
91
  if status & MYSQL_WAIT_READ
@@ -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.1"
23
+ VERSION = "0.3.1"
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.1
4
+ version: 0.3.1
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.