Skip to content

Commit 8a05d1c

Browse files
authored
allow AbstractString in various interfaces (#190)
In particular, an AbstractString value can now be used as - a connection parameter or an option value; - a SQL statement; - an input parameter; - a string literal to escape.
1 parent c514ff6 commit 8a05d1c

File tree

6 files changed

+79
-60
lines changed

6 files changed

+79
-60
lines changed

src/MySQL.jl

+42-42
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mutable struct Connection <: DBInterface.Connection
2121
db::String
2222
lastexecute::Any
2323

24-
function Connection(host::String, user::String, passwd::Union{String, Nothing}, db::String, port::Integer, unix_socket::String; kw...)
24+
function Connection(host::AbstractString, user::AbstractString, passwd::Union{AbstractString, Nothing}, db::AbstractString, port::Integer, unix_socket::AbstractString; kw...)
2525
mysql = API.init()
2626
API.setoption(mysql, API.MYSQL_PLUGIN_DIR, API.PLUGIN_DIR)
2727
API.setoption(mysql, API.MYSQL_SET_CHARSET_NAME, "utf8mb4")
@@ -105,38 +105,38 @@ function clientflags(;
105105
end
106106

107107
function setoptions!(mysql;
108-
init_command::Union{String, Nothing}=nothing,
108+
init_command::Union{AbstractString, Nothing}=nothing,
109109
connect_timeout::Union{Integer, Nothing}=nothing,
110110
reconnect::Union{Bool, Nothing}=nothing,
111111
read_timeout::Union{Integer, Nothing}=nothing,
112112
write_timeout::Union{Integer, Nothing}=nothing,
113113
data_truncation::Union{Bool, Nothing}=nothing,
114-
charset_dir::Union{String, Nothing}=nothing,
115-
charset_name::Union{String, Nothing}=nothing,
116-
bind::Union{String, Nothing}=nothing,
114+
charset_dir::Union{AbstractString, Nothing}=nothing,
115+
charset_name::Union{AbstractString, Nothing}=nothing,
116+
bind::Union{AbstractString, Nothing}=nothing,
117117
max_allowed_packet::Union{Integer, Nothing}=nothing,
118118
net_buffer_length::Union{Integer, Nothing}=nothing,
119119
named_pipe::Union{Bool, Nothing}=nothing,
120120
protocol::Union{API.mysql_protocol_type, Nothing}=nothing,
121-
ssl_key::Union{String, Nothing}=nothing,
122-
ssl_cert::Union{String, Nothing}=nothing,
123-
ssl_ca::Union{String, Nothing}=nothing,
124-
ssl_capath::Union{String, Nothing}=nothing,
125-
ssl_cipher::Union{String, Nothing}=nothing,
126-
ssl_crl::Union{String, Nothing}=nothing,
127-
ssl_crlpath::Union{String, Nothing}=nothing,
128-
passphrase::Union{String, Nothing}=nothing,
121+
ssl_key::Union{AbstractString, Nothing}=nothing,
122+
ssl_cert::Union{AbstractString, Nothing}=nothing,
123+
ssl_ca::Union{AbstractString, Nothing}=nothing,
124+
ssl_capath::Union{AbstractString, Nothing}=nothing,
125+
ssl_cipher::Union{AbstractString, Nothing}=nothing,
126+
ssl_crl::Union{AbstractString, Nothing}=nothing,
127+
ssl_crlpath::Union{AbstractString, Nothing}=nothing,
128+
passphrase::Union{AbstractString, Nothing}=nothing,
129129
ssl_verify_server_cert::Union{Bool, Nothing}=nothing,
130130
ssl_enforce::Union{Bool, Nothing}=nothing,
131-
default_auth::Union{String, Nothing}=nothing,
132-
connection_handler::Union{String, Nothing}=nothing,
133-
plugin_dir::Union{String, Nothing}=nothing,
131+
default_auth::Union{AbstractString, Nothing}=nothing,
132+
connection_handler::Union{AbstractString, Nothing}=nothing,
133+
plugin_dir::Union{AbstractString, Nothing}=nothing,
134134
secure_auth::Union{Bool, Nothing}=nothing,
135-
server_public_key::Union{String, Nothing}=nothing,
135+
server_public_key::Union{AbstractString, Nothing}=nothing,
136136
read_default_file::Union{Bool, Nothing}=nothing,
137-
option_file::Union{String, Nothing}=nothing,
137+
option_file::Union{AbstractString, Nothing}=nothing,
138138
read_default_group::Union{Bool, Nothing}=nothing,
139-
option_group::Union{String, Nothing}=nothing,
139+
option_group::Union{AbstractString, Nothing}=nothing,
140140
kw...
141141
)
142142
if init_command !== nothing
@@ -239,12 +239,12 @@ function setoptions!(mysql;
239239
end
240240

241241
"""
242-
DBInterface.connect(MySQL.Connection, host::String, user::String, passwd::String; db::String="", port::Integer=3306, unix_socket::String=API.MYSQL_DEFAULT_SOCKET, client_flag=API.CLIENT_MULTI_STATEMENTS, opts = Dict())
242+
DBInterface.connect(MySQL.Connection, host::AbstractString, user::AbstractString, passwd::AbstractString; db::AbstractString="", port::Integer=3306, unix_socket::AbstractString=API.MYSQL_DEFAULT_SOCKET, client_flag=API.CLIENT_MULTI_STATEMENTS, opts = Dict())
243243
244244
Connect to a MySQL database with provided `host`, `user`, and `passwd` positional arguments. Supported keyword arguments include:
245-
* `db::String=""`: attach to a database by default
245+
* `db::AbstractString=""`: attach to a database by default
246246
* `port::Integer=3306`: connect to the database on a specific port
247-
* `unix_socket::String`: specifies the socket or named pipe that should be used
247+
* `unix_socket::AbstractString`: specifies the socket or named pipe that should be used
248248
* `found_rows::Bool=false`: Return the number of matched rows instead of number of changed rows
249249
* `no_schema::Bool=false`: Forbids the use of database.tablename.column syntax and forces the SQL parser to generate an error.
250250
* `compress::Bool=false`: Use compression protocol
@@ -258,34 +258,34 @@ Connect to a MySQL database with provided `host`, `user`, and `passwd` positiona
258258
* `read_timeout::Integer`: Specifies the timeout in seconds for reading packets from the server.
259259
* `write_timeout::Integer`: Specifies the timeout in seconds for reading packets from the server.
260260
* `data_truncation::Bool`: Enable or disable reporting data truncation errors for prepared statements
261-
* `charset_dir::String`: character set files directory
262-
* `charset_name::String`: Specify the default character set for the connection
263-
* `bind::String`: Specify the network interface from which to connect to the database, like `"192.168.8.3"`
261+
* `charset_dir::AbstractString`: character set files directory
262+
* `charset_name::AbstractString`: Specify the default character set for the connection
263+
* `bind::AbstractString`: Specify the network interface from which to connect to the database, like `"192.168.8.3"`
264264
* `max_allowed_packet::Integer`: The maximum packet length to send to or receive from server. The default is 16MB, the maximum 1GB.
265265
* `net_buffer_length::Integer`: The buffer size for TCP/IP and socket communication. Default is 16KB.
266266
* `named_pipe::Bool`: For Windows operating systems only: Use named pipes for client/server communication.
267267
* `protocol::MySQL.API.mysql_protocol_type`: Specify the type of client/server protocol. Possible values are: `MySQL.API.MYSQL_PROTOCOL_TCP`, `MySQL.API.MYSQL_PROTOCOL_SOCKET`, `MySQL.API.MYSQL_PROTOCOL_PIPE`, `MySQL.API.MYSQL_PROTOCOL_MEMORY`.
268-
* `ssl_key::String`: Defines a path to a private key file to use for TLS. This option requires that you use the absolute path, not a relative path. If the key is protected with a passphrase, the passphrase needs to be specified with `passphrase` keyword argument.
269-
* `passphrase::String`: Specify a passphrase for a passphrase-protected private key, as configured by the `ssl_key` keyword argument.
270-
* `ssl_cert::String`: Defines a path to the X509 certificate file to use for TLS. This option requires that you use the absolute path, not a relative path.
271-
* `ssl_ca::String`: Defines a path to a PEM file that should contain one or more X509 certificates for trusted Certificate Authorities (CAs) to use for TLS. This option requires that you use the absolute path, not a relative path.
272-
* `ssl_capath::String`: Defines a path to a directory that contains one or more PEM files that should each contain one X509 certificate for a trusted Certificate Authority (CA) to use for TLS. This option requires that you use the absolute path, not a relative path. The directory specified by this option needs to be run through the openssl rehash command.
273-
* `ssl_cipher::String`: Defines a list of permitted ciphers or cipher suites to use for TLS, like `"DHE-RSA-AES256-SHA"`
274-
* `ssl_crl::String`: Defines a path to a PEM file that should contain one or more revoked X509 certificates to use for TLS. This option requires that you use the absolute path, not a relative path.
275-
* `ssl_crlpath::String`: Defines a path to a directory that contains one or more PEM files that should each contain one revoked X509 certificate to use for TLS. This option requires that you use the absolute path, not a relative path. The directory specified by this option needs to be run through the openssl rehash command.
268+
* `ssl_key::AbstractString`: Defines a path to a private key file to use for TLS. This option requires that you use the absolute path, not a relative path. If the key is protected with a passphrase, the passphrase needs to be specified with `passphrase` keyword argument.
269+
* `passphrase::AbstractString`: Specify a passphrase for a passphrase-protected private key, as configured by the `ssl_key` keyword argument.
270+
* `ssl_cert::AbstractString`: Defines a path to the X509 certificate file to use for TLS. This option requires that you use the absolute path, not a relative path.
271+
* `ssl_ca::AbstractString`: Defines a path to a PEM file that should contain one or more X509 certificates for trusted Certificate Authorities (CAs) to use for TLS. This option requires that you use the absolute path, not a relative path.
272+
* `ssl_capath::AbstractString`: Defines a path to a directory that contains one or more PEM files that should each contain one X509 certificate for a trusted Certificate Authority (CA) to use for TLS. This option requires that you use the absolute path, not a relative path. The directory specified by this option needs to be run through the openssl rehash command.
273+
* `ssl_cipher::AbstractString`: Defines a list of permitted ciphers or cipher suites to use for TLS, like `"DHE-RSA-AES256-SHA"`
274+
* `ssl_crl::AbstractString`: Defines a path to a PEM file that should contain one or more revoked X509 certificates to use for TLS. This option requires that you use the absolute path, not a relative path.
275+
* `ssl_crlpath::AbstractString`: Defines a path to a directory that contains one or more PEM files that should each contain one revoked X509 certificate to use for TLS. This option requires that you use the absolute path, not a relative path. The directory specified by this option needs to be run through the openssl rehash command.
276276
* `ssl_verify_server_cert::Bool`: Enables (or disables) server certificate verification.
277277
* `ssl_enforce::Bool`: Whether to force TLS
278-
* `default_auth::String`: Default authentication client-side plugin to use.
279-
* `connection_handler::String`: Specify the name of a connection handler plugin.
280-
* `plugin_dir::String`: Specify the location of client plugins. The plugin directory can also be specified with the MARIADB_PLUGIN_DIR environment variable.
278+
* `default_auth::AbstractString`: Default authentication client-side plugin to use.
279+
* `connection_handler::AbstractString`: Specify the name of a connection handler plugin.
280+
* `plugin_dir::AbstractString`: Specify the location of client plugins. The plugin directory can also be specified with the MARIADB_PLUGIN_DIR environment variable.
281281
* `secure_auth::Bool`: Refuse to connect to the server if the server uses the mysql_old_password authentication plugin. This mode is off by default, which is a difference in behavior compared to MySQL 5.6 and later, where it is on by default.
282-
* `server_public_key::String`: Specifies the name of the file which contains the RSA public key of the database server. The format of this file must be in PEM format. This option is used by the caching_sha2_password client authentication plugin.
282+
* `server_public_key::AbstractString`: Specifies the name of the file which contains the RSA public key of the database server. The format of this file must be in PEM format. This option is used by the caching_sha2_password client authentication plugin.
283283
* `read_default_file::Bool`: only the default option files are read
284-
* `option_file::String`: the argument is interpreted as a path to a custom option file, and only that option file is read.
284+
* `option_file::AbstractString`: the argument is interpreted as a path to a custom option file, and only that option file is read.
285285
* `read_default_group::Bool`: only the default option groups are read from specified option file(s)
286-
* `option_group::String`: it is interpreted as a custom option group, and that custom option group is read in addition to the default option groups.
286+
* `option_group::AbstractString`: it is interpreted as a custom option group, and that custom option group is read in addition to the default option groups.
287287
"""
288-
DBInterface.connect(::Type{Connection}, host::String, user::String, passwd::Union{String, Nothing}=nothing; db::String="", port::Integer=3306, unix_socket::String=API.MYSQL_DEFAULT_SOCKET, kw...) =
288+
DBInterface.connect(::Type{Connection}, host::AbstractString, user::AbstractString, passwd::Union{AbstractString, Nothing}=nothing; db::AbstractString="", port::Integer=3306, unix_socket::AbstractString=API.MYSQL_DEFAULT_SOCKET, kw...) =
289289
Connection(host, user, passwd, db, port, unix_socket; kw...)
290290

291291
"""
@@ -317,10 +317,10 @@ include("prepare.jl")
317317
include("load.jl")
318318

319319
"""
320-
MySQL.escape(conn::MySQL.Connection, str::String) -> String
320+
MySQL.escape(conn::MySQL.Connection, str::AbstractString) -> String
321321
322322
Escapes a string using `mysql_real_escape_string()`, returns the escaped string.
323323
"""
324-
escape(conn::Connection, sql::String) = API.escapestring(conn.mysql, sql)
324+
escape(conn::Connection, sql::AbstractString) = API.escapestring(conn.mysql, sql)
325325

326326
end # module

src/api/capi.jl

+10-10
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ ER_WRONG_DB_NAME
108108
109109
The database name was too long.
110110
"""=#
111-
function changeuser(mysql::MYSQL, user::String, password::String, db::String)
111+
function changeuser(mysql::MYSQL, user::AbstractString, password::AbstractString, db::AbstractString)
112112
return @checksuccess mysql mysql_change_user(mysql.ptr, user, password, isempty(db) ? C_NULL : db)
113113
end
114114

@@ -137,7 +137,7 @@ name: The plugin name.
137137
138138
type: The plugin type.
139139
"""=#
140-
function findplugin(mysql::MYSQL, name::String, type::Integer)
140+
function findplugin(mysql::MYSQL, name::AbstractString, type::Integer)
141141
return @checknull mysql mysql_client_find_plugin(mysql.ptr, name, type)
142142
end
143143

@@ -1034,7 +1034,7 @@ value: A pointer to the option value.
10341034
Return Values
10351035
Zero for success, 1 if an error occurred. If the plugin has an option handler, that handler should also return zero for success and 1 if an error occurred.
10361036
"""=#
1037-
function pluginoption(plugin::Ptr{Cvoid}, option::String, value)
1037+
function pluginoption(plugin::Ptr{Cvoid}, option::AbstractString, value)
10381038
mysql_plugin_options(plugin, option, value)
10391039
end
10401040

@@ -1129,7 +1129,7 @@ If no value is found in an option file for a parameter, its default value is use
11291129
Return Values
11301130
A MYSQL* connection handler if the connection was successful, NULL if the connection was unsuccessful. For a successful connection, the return value is the same as the value of the first parameter.
11311131
"""=#
1132-
function connect(mysql::MYSQL, host::String, user::String, passwd::Union{String, Nothing}, db::String, port::Integer, unix_socket::String, client_flag)
1132+
function connect(mysql::MYSQL, host::AbstractString, user::AbstractString, passwd::Union{AbstractString, Nothing}, db::AbstractString, port::Integer, unix_socket::AbstractString, client_flag)
11331133
@checknull mysql mysql_real_connect(mysql.ptr, host, user, passwd === nothing ? Ptr{UInt8}(C_NULL) : passwd, db, port, unix_socket, client_flag)
11341134
return mysql
11351135
end
@@ -1145,7 +1145,7 @@ The string pointed to by from must be length bytes long. You must allocate the t
11451145
11461146
If you must change the character set of the connection, use the mysql_set_character_set() function rather than executing a SET NAMES (or SET CHARACTER SET) statement. mysql_set_character_set() works like SET NAMES but also affects the character set used by mysql_real_escape_string(), which SET NAMES does not.
11471147
"""=#
1148-
function escapestring(mysql::MYSQL, str::String)
1148+
function escapestring(mysql::MYSQL, str::AbstractString)
11491149
len = sizeof(str)
11501150
to = Base.StringVector(len * 2 + 1)
11511151
tolen = mysql_real_escape_string(mysql.ptr, to, str, len)
@@ -1198,7 +1198,7 @@ if (mysql_real_query(&mysql,query,(unsigned int) (end - query)))
11981198
}
11991199
The my_stpcpy() function used in the example is included in the libmysqlclient library and works like strcpy() but returns a pointer to the terminating null of the first parameter.
12001200
"""=#
1201-
function escapestringquote(mysql::MYSQL, str::String, q::Char)
1201+
function escapestringquote(mysql::MYSQL, str::AbstractString, q::Char)
12021202
len = sizeof(str)
12031203
to = Base.StringVector(len * 2 + 1)
12041204
tolen = mysql_real_escape_string_quote(mysql.ptr, to, str, len, q)
@@ -1216,7 +1216,7 @@ If you want to know whether the statement returns a result set, you can use mysq
12161216
Return Values
12171217
Zero for success. Nonzero if an error occurred.
12181218
"""=#
1219-
function query(mysql::MYSQL, sql::String)
1219+
function query(mysql::MYSQL, sql::AbstractString)
12201220
return @checksuccess mysql mysql_real_query(mysql.ptr, sql, sizeof(sql))
12211221
end
12221222

@@ -1321,14 +1321,14 @@ Causes the database specified by db to become the default (current) database on
13211321
13221322
mysql_select_db() fails unless the connected user can be authenticated as having permission to use the database or some object within it.
13231323
"""=#
1324-
function selectdb(mysql::MYSQL, db::String)
1324+
function selectdb(mysql::MYSQL, db::AbstractString)
13251325
return @checksuccess mysql mysql_select_db(mysql.ptr, db)
13261326
end
13271327

13281328
#="""
13291329
This function is used to set the default character set for the current connection. The string csname specifies a valid character set name. The connection collation becomes the default collation of the character set. This function works like the SET NAMES statement, but also sets the value of mysql->charset, and thus affects the character set used by mysql_real_escape_string()
13301330
"""=#
1331-
function setcharacterset(mysql::MYSQL, csname::String)
1331+
function setcharacterset(mysql::MYSQL, csname::AbstractString)
13321332
return @checksuccess mysql mysql_set_character_set(mysql.ptr, csname)
13331333
end
13341334

@@ -1445,7 +1445,7 @@ cipher: The list of permissible ciphers for SSL encryption.
14451445
Return Values
14461446
This function always returns 0. If SSL setup is incorrect, a subsequent mysql_real_connect() call returns an error when you attempt to connect.
14471447
"""=#
1448-
function sslset(mysql::MYSQL, key::String, cert::String, ca::String, capath::String, cipher::String)
1448+
function sslset(mysql::MYSQL, key::AbstractString, cert::AbstractString, ca::AbstractString, capath::AbstractString, cipher::AbstractString)
14491449
return mysql_ssl_set(mysql.ptr, key, cert, ca, capath, cipher)
14501450
end
14511451

src/api/ccalls.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function mysql_autocommit(mysql::Ptr{Cvoid}, mode)
3737
end
3838

3939
#bool mysql_change_user(MYSQL *mysql, const char *user, const char *password, const char *db)
40-
function mysql_change_user(mysql::Ptr{Cvoid}, user::String, password::String, db)
40+
function mysql_change_user(mysql::Ptr{Cvoid}, user::AbstractString, password::AbstractString, db)
4141
return @c(:mysql_change_user,
4242
Bool,
4343
(Ptr{Cvoid}, Ptr{UInt8}, Ptr{UInt8}, Ptr{UInt8}),
@@ -53,7 +53,7 @@ function mysql_character_set_name(mysql::Ptr{Cvoid})
5353
end
5454

5555
#struct st_mysql_client_plugin *mysql_client_find_plugin(MYSQL *mysql, const char *name, int type)
56-
function mysql_client_find_plugin(mysql::Ptr{Cvoid}, name::String, type::Int)
56+
function mysql_client_find_plugin(mysql::Ptr{Cvoid}, name::AbstractString, type::Int)
5757
return @c(:mysql_client_find_plugin,
5858
Ptr{Cvoid},
5959
(Ptr{Cvoid}, Ptr{UInt8}, Cint),
@@ -477,7 +477,7 @@ function mysql_row_tell(result::Ptr{Cvoid})
477477
end
478478

479479
#int mysql_select_db(MYSQL *mysql, const char *db)
480-
function mysql_select_db(mysql::Ptr{Cvoid}, db::String)
480+
function mysql_select_db(mysql::Ptr{Cvoid}, db::AbstractString)
481481
return @c(:mysql_select_db,
482482
Cint,
483483
(Ptr{Cvoid}, Ptr{UInt8}),
@@ -501,7 +501,7 @@ function mysql_session_track_get_next(mysql::Ptr{Cvoid}, type, data, len)
501501
end
502502

503503
#int mysql_set_character_set(MYSQL *mysql, const char *csname)
504-
function mysql_set_character_set(mysql::Ptr{Cvoid}, csname::String)
504+
function mysql_set_character_set(mysql::Ptr{Cvoid}, csname::AbstractString)
505505
return @c(:mysql_set_character_set,
506506
Cint,
507507
(Ptr{Cvoid}, Ptr{UInt8}),
@@ -806,4 +806,4 @@ function mysql_stmt_store_result(stmt::Ptr{Cvoid})
806806
Cint,
807807
(Ptr{Cvoid},),
808808
stmt)
809-
end
809+
end

0 commit comments

Comments
 (0)