db-mysql 0.1.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.
- checksums.yaml +7 -0
- data/lib/.DS_Store +0 -0
- data/lib/db/mysql.rb +27 -0
- data/lib/db/mysql/adapter.rb +41 -0
- data/lib/db/mysql/connection.rb +65 -0
- data/lib/db/mysql/native.rb +31 -0
- data/lib/db/mysql/native/connection.rb +179 -0
- data/lib/db/mysql/native/result.rb +169 -0
- data/lib/db/mysql/version.rb +25 -0
- metadata +134 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b5a976a9b54b5e0693f4b778d1b8aeea3c0a312b8dbe35c349c55b4f84117eaa
|
4
|
+
data.tar.gz: e02bf0d3fd54172e97f5c1f36e8114c9c855ab82c94272d9cfa896b91b9c6f2e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b00cfa274d3c1137cd0d79d818346a2e79691b354ffde5cc11a3cb63acce921ebaa1b056b6508cd3d7a2236288e7a380e1dee19aa6113e15297b303a8209a9ea
|
7
|
+
data.tar.gz: 3990a60f59f0ea9a2bcab5a41cfc85cb6b8f32ef82738059e0a5ba013c3b7d696623034a516339999228e8ccc246760a93c667f6399181c99036164aad80852f
|
data/lib/.DS_Store
ADDED
Binary file
|
data/lib/db/mysql.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require_relative 'mysql/native'
|
22
|
+
require_relative 'mysql/connection'
|
23
|
+
|
24
|
+
require_relative 'mysql/adapter'
|
25
|
+
|
26
|
+
require 'db/adapters'
|
27
|
+
DB::Adapters.register(:mysql, DB::MySQL::Adapter)
|
@@ -0,0 +1,41 @@
|
|
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
|
+
require_relative 'connection'
|
24
|
+
|
25
|
+
module DB
|
26
|
+
module MySQL
|
27
|
+
LOCAL = "mysql://localhost/test"
|
28
|
+
|
29
|
+
class Adapter
|
30
|
+
def initialize(connection_string = LOCAL)
|
31
|
+
@connection_string = connection_string
|
32
|
+
end
|
33
|
+
|
34
|
+
attr :connection_string
|
35
|
+
|
36
|
+
def call
|
37
|
+
Connection.new(self.connection_string)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,65 @@
|
|
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
|
+
require 'async/pool/resource'
|
24
|
+
require_relative 'native/connection'
|
25
|
+
|
26
|
+
require 'async/io/generic'
|
27
|
+
|
28
|
+
module DB
|
29
|
+
module MySQL
|
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
|
+
|
36
|
+
# This implements the interface between the underlying
|
37
|
+
class Connection < Async::Pool::Resource
|
38
|
+
def initialize(connection_string)
|
39
|
+
@native = Native::Connection.connect(
|
40
|
+
connection_string, io: IO
|
41
|
+
)
|
42
|
+
|
43
|
+
super()
|
44
|
+
end
|
45
|
+
|
46
|
+
def close
|
47
|
+
@native.close
|
48
|
+
|
49
|
+
super
|
50
|
+
end
|
51
|
+
|
52
|
+
def status
|
53
|
+
@native.status
|
54
|
+
end
|
55
|
+
|
56
|
+
def send_query(statement)
|
57
|
+
@native.send_query(statement)
|
58
|
+
end
|
59
|
+
|
60
|
+
def next_result
|
61
|
+
@native.next_result
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'ffi'
|
22
|
+
|
23
|
+
module DB
|
24
|
+
module MySQL
|
25
|
+
module Native
|
26
|
+
extend FFI::Library
|
27
|
+
|
28
|
+
ffi_lib 'mariadb'
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,179 @@
|
|
1
|
+
# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require_relative 'result'
|
22
|
+
|
23
|
+
module DB
|
24
|
+
module MySQL
|
25
|
+
module Native
|
26
|
+
MYSQL_OPT_NONBLOCK = 6000
|
27
|
+
|
28
|
+
MYSQL_WAIT_READ = 1
|
29
|
+
MYSQL_WAIT_WRITE = 2
|
30
|
+
MYSQL_WAIT_EXCEPT = 4
|
31
|
+
MYSQL_WAIT_TIMEOUT = 8
|
32
|
+
|
33
|
+
CLIENT_MULTI_STATEMENT = 0x00010000
|
34
|
+
CLIENT_MULTI_RESULTS = 0x00020000
|
35
|
+
|
36
|
+
attach_function :mysql_init, [:pointer], :pointer
|
37
|
+
attach_function :mysql_options, [:pointer, :int, :pointer], :int
|
38
|
+
attach_function :mysql_get_socket, [:pointer], :int
|
39
|
+
|
40
|
+
attach_function :mysql_real_connect_start, [:pointer, :pointer, :string, :string, :string, :string, :int, :string, :long], :int
|
41
|
+
attach_function :mysql_real_connect_cont, [:pointer, :pointer, :int], :int
|
42
|
+
|
43
|
+
attach_function :mysql_real_query_start, [:pointer, :pointer, :string, :ulong], :int
|
44
|
+
attach_function :mysql_real_query_cont, [:pointer, :pointer, :int], :int
|
45
|
+
|
46
|
+
attach_function :mysql_use_result, [:pointer], :pointer
|
47
|
+
attach_function :mysql_next_result, [:pointer], :int
|
48
|
+
attach_function :mysql_free_result, [:pointer], :void
|
49
|
+
|
50
|
+
attach_function :mysql_close, [:pointer], :void
|
51
|
+
attach_function :mysql_errno, [:pointer], :uint
|
52
|
+
attach_function :mysql_error, [:pointer], :string
|
53
|
+
|
54
|
+
attach_function :mysql_stat, [:pointer], :string
|
55
|
+
|
56
|
+
class Connection < FFI::Pointer
|
57
|
+
def self.connect(connection_string = "", io: ::IO)
|
58
|
+
pointer = Native.mysql_init(nil)
|
59
|
+
Native.mysql_options(pointer, MYSQL_OPT_NONBLOCK, nil)
|
60
|
+
|
61
|
+
uri = URI(connection_string)
|
62
|
+
host = uri.host
|
63
|
+
user = uri.user
|
64
|
+
password = uri.password
|
65
|
+
database = uri.path.gsub(/^\//, '')
|
66
|
+
port = uri.port || 0
|
67
|
+
unix_socket = nil
|
68
|
+
client_flags = CLIENT_MULTI_STATEMENT | CLIENT_MULTI_RESULTS
|
69
|
+
|
70
|
+
result = FFI::MemoryPointer.new(:pointer)
|
71
|
+
|
72
|
+
status = Native.mysql_real_connect_start(result, pointer, host, user, password, database, port, unix_socket, client_flags);
|
73
|
+
|
74
|
+
if status > 0
|
75
|
+
io = io.new(Native.mysql_get_socket(pointer), "r+")
|
76
|
+
|
77
|
+
while status > 0
|
78
|
+
if status & MYSQL_WAIT_READ
|
79
|
+
io.wait_readable
|
80
|
+
elsif status & MYSQL_WAIT_WRITE
|
81
|
+
io.wait_writable
|
82
|
+
else
|
83
|
+
io.wait_any
|
84
|
+
end
|
85
|
+
|
86
|
+
status = Native.mysql_real_connect_cont(result, pointer, status)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
if result.read_pointer.null?
|
91
|
+
raise "Could not connect: #{Native.mysql_error(pointer)}!"
|
92
|
+
end
|
93
|
+
|
94
|
+
return self.new(pointer, io)
|
95
|
+
end
|
96
|
+
|
97
|
+
def initialize(address, io)
|
98
|
+
super(address)
|
99
|
+
|
100
|
+
@io = io
|
101
|
+
@result = nil
|
102
|
+
end
|
103
|
+
|
104
|
+
def wait_for(status)
|
105
|
+
if status & MYSQL_WAIT_READ
|
106
|
+
@io.wait_readable
|
107
|
+
elsif status & MYSQL_WAIT_WRITE
|
108
|
+
@io.wait_writable
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def check_error!(message)
|
113
|
+
if Native.mysql_errno(self) != 0
|
114
|
+
raise "#{message}: #{Native.mysql_error(self)}!"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def status
|
119
|
+
Native.mysql_stat(self)
|
120
|
+
end
|
121
|
+
|
122
|
+
def free_result
|
123
|
+
if @result
|
124
|
+
Native.mysql_free_result(@result)
|
125
|
+
|
126
|
+
@result = nil
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def close
|
131
|
+
self.free_result
|
132
|
+
|
133
|
+
Native.mysql_close(self)
|
134
|
+
|
135
|
+
@io.close
|
136
|
+
end
|
137
|
+
|
138
|
+
def send_query(statement)
|
139
|
+
self.free_result
|
140
|
+
|
141
|
+
error = FFI::MemoryPointer.new(:int)
|
142
|
+
|
143
|
+
status = Native.mysql_real_query_start(error, self, statement, statement.bytesize)
|
144
|
+
|
145
|
+
while status != 0
|
146
|
+
self.wait_for(status)
|
147
|
+
|
148
|
+
status = Native.mysql_real_query_cont(error, self, status)
|
149
|
+
end
|
150
|
+
|
151
|
+
if error.read_int != 0
|
152
|
+
raise "Could not send query: #{Native.mysql_error(self)}!"
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def next_result
|
157
|
+
if @result
|
158
|
+
self.free_result
|
159
|
+
|
160
|
+
# Successful and there are no more results:
|
161
|
+
return if Native.mysql_next_result(self) == -1
|
162
|
+
|
163
|
+
check_error!("Next result")
|
164
|
+
end
|
165
|
+
|
166
|
+
@result = Native.mysql_use_result(self)
|
167
|
+
|
168
|
+
if @result.null?
|
169
|
+
check_error!("Next result")
|
170
|
+
|
171
|
+
return nil
|
172
|
+
else
|
173
|
+
return Result.new(self, @result)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
@@ -0,0 +1,169 @@
|
|
1
|
+
# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require_relative '../native'
|
22
|
+
|
23
|
+
module DB
|
24
|
+
module MySQL
|
25
|
+
module Native
|
26
|
+
attach_function :mysql_fetch_row_start, [:pointer, :pointer], :int
|
27
|
+
attach_function :mysql_fetch_row_cont, [:pointer, :pointer, :int], :int
|
28
|
+
|
29
|
+
attach_function :mysql_num_rows, [:pointer], :uint64
|
30
|
+
attach_function :mysql_num_fields, [:pointer], :uint32
|
31
|
+
|
32
|
+
# FieldType = enum(
|
33
|
+
# :decimal, Mysql::Field::TYPE_DECIMAL,
|
34
|
+
# :tiny, Mysql::Field::TYPE_TINY,
|
35
|
+
# :short, Mysql::Field::TYPE_SHORT,
|
36
|
+
# :long, Mysql::Field::TYPE_LONG,
|
37
|
+
# :float, Mysql::Field::TYPE_FLOAT,
|
38
|
+
# :double, Mysql::Field::TYPE_DOUBLE,
|
39
|
+
# :null, Mysql::Field::TYPE_NULL,
|
40
|
+
# :timestamp, Mysql::Field::TYPE_TIMESTAMP,
|
41
|
+
# :longlong, Mysql::Field::TYPE_LONGLONG,
|
42
|
+
# :int24, Mysql::Field::TYPE_INT24,
|
43
|
+
# :date, Mysql::Field::TYPE_DATE,
|
44
|
+
# :time, Mysql::Field::TYPE_TIME,
|
45
|
+
# :datetime, Mysql::Field::TYPE_DATETIME,
|
46
|
+
# :year, Mysql::Field::TYPE_YEAR,
|
47
|
+
# :newdate, Mysql::Field::TYPE_NEWDATE,
|
48
|
+
# :varchar, Mysql::Field::TYPE_VARCHAR,
|
49
|
+
# :bit, Mysql::Field::TYPE_BIT,
|
50
|
+
# :newdecimal, Mysql::Field::TYPE_NEWDECIMAL,
|
51
|
+
# :enum, Mysql::Field::TYPE_ENUM,
|
52
|
+
# :set, Mysql::Field::TYPE_SET,
|
53
|
+
# :tiny_blob, Mysql::Field::TYPE_TINY_BLOB,
|
54
|
+
# :medium_blob, Mysql::Field::TYPE_MEDIUM_BLOB,
|
55
|
+
# :long_blob, Mysql::Field::TYPE_LONG_BLOB,
|
56
|
+
# :blob, Mysql::Field::TYPE_BLOB,
|
57
|
+
# :var_string, Mysql::Field::TYPE_VAR_STRING,
|
58
|
+
# :string, Mysql::Field::TYPE_STRING,
|
59
|
+
# :geometry, Mysql::Field::TYPE_GEOMETRY,
|
60
|
+
# :char, Mysql::Field::TYPE_CHAR,
|
61
|
+
# :interval, Mysql::Field::TYPE_INTERVAL
|
62
|
+
# )
|
63
|
+
|
64
|
+
FieldType = :uchar
|
65
|
+
|
66
|
+
class Field < FFI::Struct
|
67
|
+
layout(
|
68
|
+
:name, :string,
|
69
|
+
:org_name, :string,
|
70
|
+
:table, :string,
|
71
|
+
:org_table, :string,
|
72
|
+
:db, :string,
|
73
|
+
:catalog, :string,
|
74
|
+
:def, :string,
|
75
|
+
:length, :ulong,
|
76
|
+
:max_length, :ulong,
|
77
|
+
:name_length, :uint,
|
78
|
+
:org_name_length, :uint,
|
79
|
+
:table_length, :uint,
|
80
|
+
:org_table_length, :uint,
|
81
|
+
:db_length, :uint,
|
82
|
+
:catalog_length, :uint,
|
83
|
+
:def_length, :uint,
|
84
|
+
:flags, :uint,
|
85
|
+
:decimals, :uint,
|
86
|
+
:charsetnr, :uint,
|
87
|
+
:type, FieldType
|
88
|
+
)
|
89
|
+
|
90
|
+
def name
|
91
|
+
self[:name]
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
attach_function :mysql_fetch_fields, [:pointer], :pointer
|
96
|
+
|
97
|
+
class Result < FFI::Pointer
|
98
|
+
def initialize(connection, address)
|
99
|
+
super(address)
|
100
|
+
|
101
|
+
@connection = connection
|
102
|
+
@fields = nil
|
103
|
+
end
|
104
|
+
|
105
|
+
def field_count
|
106
|
+
Native.mysql_num_fields(self)
|
107
|
+
end
|
108
|
+
|
109
|
+
def fields
|
110
|
+
unless @fields
|
111
|
+
pointer = Native.mysql_fetch_fields(self)
|
112
|
+
|
113
|
+
@fields = field_count.times.map do |index|
|
114
|
+
Field.new(pointer + index * Field.size)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
return @fields
|
119
|
+
end
|
120
|
+
|
121
|
+
def field_names
|
122
|
+
fields.map(&:name)
|
123
|
+
end
|
124
|
+
|
125
|
+
def row_count
|
126
|
+
Native.mysql_num_rows(self)
|
127
|
+
end
|
128
|
+
|
129
|
+
alias count row_count
|
130
|
+
alias keys field_names
|
131
|
+
|
132
|
+
def each
|
133
|
+
row = FFI::MemoryPointer.new(:pointer)
|
134
|
+
field_count = self.field_count
|
135
|
+
|
136
|
+
while true
|
137
|
+
status = Native.mysql_fetch_row_start(row, self)
|
138
|
+
|
139
|
+
while status != 0
|
140
|
+
@connection.wait_for(status)
|
141
|
+
|
142
|
+
status = Native.mysql_fetch_row_cont(row, self, status)
|
143
|
+
end
|
144
|
+
|
145
|
+
pointer = row.read_pointer
|
146
|
+
|
147
|
+
if pointer.null?
|
148
|
+
break
|
149
|
+
else
|
150
|
+
yield pointer.get_array_of_string(0, field_count)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
@connection.check_error!("Reading recordset")
|
155
|
+
end
|
156
|
+
|
157
|
+
def to_a
|
158
|
+
rows = []
|
159
|
+
|
160
|
+
self.each do |row|
|
161
|
+
rows << row
|
162
|
+
end
|
163
|
+
|
164
|
+
return rows
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
module DB
|
22
|
+
module MySQL
|
23
|
+
VERSION = "0.1.0"
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: db-mysql
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Samuel Williams
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-06-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ffi
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: async-io
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: covered
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.6'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.6'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
executables: []
|
100
|
+
extensions: []
|
101
|
+
extra_rdoc_files: []
|
102
|
+
files:
|
103
|
+
- lib/.DS_Store
|
104
|
+
- lib/db/mysql.rb
|
105
|
+
- lib/db/mysql/adapter.rb
|
106
|
+
- lib/db/mysql/connection.rb
|
107
|
+
- lib/db/mysql/native.rb
|
108
|
+
- lib/db/mysql/native/connection.rb
|
109
|
+
- lib/db/mysql/native/result.rb
|
110
|
+
- lib/db/mysql/version.rb
|
111
|
+
homepage:
|
112
|
+
licenses:
|
113
|
+
- MIT
|
114
|
+
metadata: {}
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '2.5'
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
requirements: []
|
130
|
+
rubygems_version: 3.1.2
|
131
|
+
signing_key:
|
132
|
+
specification_version: 4
|
133
|
+
summary: Ruby FFI bindings for libpq C interface.
|
134
|
+
test_files: []
|