Expose MySQL connection state
This commit is contained in:
parent
da04af1cbf
commit
3e423f49dd
@ -1,6 +1,7 @@
|
||||
MySQL Functions
|
||||
|
||||
mysql_connect
|
||||
mysql_connected
|
||||
mysql_disconnect
|
||||
mysql_error
|
||||
mysql_escape
|
||||
|
||||
@ -11,7 +11,7 @@ return value : pointer to the MySQL connection struct
|
||||
>mysql
|
||||
|
||||
:content
|
||||
Establishes a connection to a MySQL server and returns a pointer to the connection struct.
|
||||
Establishes a connection to a MySQL server and returns a request-owned pointer to the connection struct. A failed connection also returns a wrapper so that `mysql_error()` can report the failure; use `mysql_connected()` before issuing optional work that requires a live server handle.
|
||||
|
||||
This connection handle is then used with helpers such as `mysql_query()`, `mysql_error()`, and `mysql_disconnect()`.
|
||||
|
||||
@ -19,5 +19,5 @@ MySQL handles are request-scoped framework resources. Repeated `mysql_connect()`
|
||||
|
||||
:example
|
||||
MySQL* db = mysql_connect();
|
||||
print(db != 0 ? "connected to MySQL" : "no MySQL server reachable with these credentials", "\n");
|
||||
print(mysql_connected(db) ? "connected to MySQL" : "no MySQL server reachable with these credentials", "\n");
|
||||
if(db != 0) mysql_disconnect(db);
|
||||
|
||||
20
site/doc/pages/mysql_connected.txt
Normal file
20
site/doc/pages/mysql_connected.txt
Normal file
@ -0,0 +1,20 @@
|
||||
:sig
|
||||
bool mysql_connected(MySQL* m)
|
||||
|
||||
:params
|
||||
m : pointer returned by mysql_connect()
|
||||
return value : true only when the wrapper owns a live server connection
|
||||
|
||||
:see
|
||||
>mysql
|
||||
|
||||
:content
|
||||
Tests whether a MySQL wrapper has a usable server connection. `mysql_connect()` retains a failed wrapper so that callers can retrieve its diagnostic through `mysql_error()`; this helper distinguishes that state from a live handle without consuming the diagnostic.
|
||||
|
||||
:example
|
||||
MySQL* db = mysql_connect();
|
||||
if(!mysql_connected(db))
|
||||
{
|
||||
print("database unavailable: ", mysql_error(db), "\n");
|
||||
}
|
||||
if(db != 0) mysql_disconnect(db);
|
||||
@ -117,6 +117,11 @@ RENDER(Request& context)
|
||||
);
|
||||
MySQL unavailable_mysql;
|
||||
unavailable_mysql.connect("127.0.0.1", "__uce_intentionally_missing__", "not-a-real-password");
|
||||
mark(
|
||||
"mysql_connected() distinguishes a failed connection wrapper",
|
||||
!mysql_connected(&unavailable_mysql) ? "pass" : "fail",
|
||||
mysql_connected(&unavailable_mysql) ? "unexpected live handle" : "no live handle"
|
||||
);
|
||||
unavailable_mysql.query("SELECT 1");
|
||||
String unavailable_mysql_error = unavailable_mysql.error();
|
||||
mark(
|
||||
|
||||
@ -71,6 +71,14 @@ inline MySQL* mysql_connect(String host = "localhost", String username = "root",
|
||||
return(db);
|
||||
}
|
||||
|
||||
// A failed connection retains a request-owned wrapper so mysql_error() remains
|
||||
// available. Callers that need to decide whether to issue work must test the
|
||||
// connection state rather than only the wrapper pointer.
|
||||
inline bool mysql_connected(MySQL* m)
|
||||
{
|
||||
return(m != 0 && m->connection != 0);
|
||||
}
|
||||
|
||||
inline void mysql_disconnect(MySQL* m)
|
||||
{
|
||||
if(!m)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user