@@ -38,10 +38,12 @@ static ID intern_brackets, intern_merge, intern_merge_bang, intern_new_with_args
3838 #define CONNECTED (wrapper ) (wrapper->client->net.pvio != NULL && wrapper->client->net.fd != -1 && VIO_IS_CONNECTED(wrapper))
3939#endif
4040
41+ #define MYSQL_CLIENT_NOT_CONNECTED_STR "MySQL client is not connected"
42+
4143#define REQUIRE_CONNECTED (wrapper ) \
4244 REQUIRE_INITIALIZED(wrapper) \
4345 if (!CONNECTED(wrapper) && !wrapper->reconnect_enabled) { \
44- rb_raise(cMysql2Error, "MySQL client is not connected" ); \
46+ rb_raise(cMysql2Error, MYSQL_CLIENT_NOT_CONNECTED_STR ); \
4547 }
4648
4749#define REQUIRE_NOT_CONNECTED (wrapper ) \
@@ -50,6 +52,17 @@ static ID intern_brackets, intern_merge, intern_merge_bang, intern_new_with_args
5052 rb_raise(cMysql2Error, "MySQL connection is already open"); \
5153 }
5254
55+ /*
56+ * assert that we've connected at least once by using
57+ * `client->server_version`, which is a string that is initialized to the char*
58+ * server name once the client has connected
59+ */
60+ #define REQUIRE_CONNECTED_ONCE (wrapper ) \
61+ REQUIRE_INITIALIZED(wrapper) \
62+ if (!wrapper->client->server_version) { \
63+ rb_raise(cMysql2Error, MYSQL_CLIENT_NOT_CONNECTED_STR); \
64+ }
65+
5366/*
5467 * compatability with mysql-connector-c, where LIBMYSQL_VERSION is the correct
5568 * variable to use, but MYSQL_SERVER_VERSION gives the correct numbers when
@@ -819,7 +832,7 @@ static VALUE rb_mysql_client_real_escape(VALUE self, VALUE str) {
819832 rb_encoding * conn_enc ;
820833 GET_CLIENT (self );
821834
822- REQUIRE_CONNECTED (wrapper );
835+ REQUIRE_CONNECTED_ONCE (wrapper );
823836 Check_Type (str , T_STRING );
824837 default_internal_enc = rb_default_internal_encoding ();
825838 conn_enc = rb_to_encoding (wrapper -> encoding );
0 commit comments