Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rbs for mysql2-0.5 #563

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions gems/mysql2/0.5/_test/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "mysql2"

client = Mysql2::Client.new(
host: '127.0.0.1',
port: 3306,
username: 'root',
password: 'password',
database: 'foo',
symbolize_keys: true,
cast_booleans: true,
reconnect: true,
)

client.query("SELECT * FROM users").each do |row|
raise unless row.is_a?(Hash)
row[:id]
end

client.query("SELECT id FROM users", as: :array).each do |row|
raise unless row.is_a?(Array)
row[0]
end
42 changes: 42 additions & 0 deletions gems/mysql2/0.5/client.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module Mysql2
class Client
self.@default_query_options: untyped

@read_timeout: untyped

@query_options: untyped

attr_reader query_options: untyped

attr_reader read_timeout: untyped

def self.default_query_options: () -> untyped

def initialize: (Hash[(String | Symbol), untyped] opts) -> void

def parse_ssl_mode: (untyped mode) -> untyped

def parse_flags_array: (untyped flags, ?::Integer initial) -> untyped

# Find any default system CA paths to handle system roots
# by default if stricter validation is requested and no
# path is provide.
def find_default_ca_path: () -> untyped

# Set default program_name in performance_schema.session_connect_attrs
# and performance_schema.session_account_connect_attrs
def parse_connect_attrs: (untyped conn_attrs) -> (::Hash[untyped, untyped] | untyped)

def query: (String sql, ?::Hash[untyped, untyped] options) -> Mysql2::result
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The query method changes the type returned by the as keyword argument.
If as: :hash or unspecified, it returns Array[Hash]; if as: :array, it returns Array[Array].

If RBS could distinguish and overload literals rather than the type of the argument value, it should be able to uniquely determine the type.

def query: (String sql) -> Mysql2::ResultAsHash
         | (String sql, as: :hash) -> Mysql2::ResultAsHash
         | (String sql, as: :array) -> Mysql2::ResultAsArray

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above RBS syntax was found to work correctly. I was mistaken.

On the other hand, the query method was implemented to override the options passed in initialize, so the query method argument did not uniquely determine the type of the return value...

Mysql2::Client.new.query(“SELECT * FROM users”) # => Array[Hash].
Mysql2::Client.new(as: :array).query(“SELECT * FROM users”) # => maybe it should be Array[Array] 


def query_info: () -> (::Hash[untyped, untyped] | untyped)

def info: () -> untyped

def last_id: () -> Integer

private

def self.local_offset: () -> untyped
end
end
20 changes: 20 additions & 0 deletions gems/mysql2/0.5/result.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Mysql2
type row_value_type = String | Integer | nil
type result = ResultAsHash | ResultAsArray

class ResultAsHash
attr_reader server_flags: untyped

include Enumerable[Hash[Symbol, row_value_type]]

def each: () { (Hash[Symbol, row_value_type]) -> void } -> void
end

class ResultAsArray
attr_reader server_flags: untyped

include Enumerable[Array[row_value_type]]

def each: () { (Array[row_value_type]) -> void } -> void
end
end