diff options
Diffstat (limited to 'lib/SQL.cc')
-rw-r--r-- | lib/SQL.cc | 61 |
1 files changed, 52 insertions, 9 deletions
@@ -5,15 +5,17 @@ SQLConnection::SQLConnection(String host, String user, String passwd, unsigned long cflags) throw (GeneralException) : res(0) { mysql_init(&con); - char * p; - - if (!mysql_real_connect(&con, - p = ((ugly_string)(host)).p ? p : 0, - ((ugly_string)(user)).p, - ((ugly_string)(passwd)).p, - ((ugly_string)(db)).p, port, - ((ugly_string)(socket)).p, cflags)) { - throw GeneralException(String("Could not connect to MySQL host ") + p); + const char * phost = ((ugly_string) host).p; + const char * puser = ((ugly_string) user).p; + const char * ppasswd = ((ugly_string) passwd).p; + const char * pdb = ((ugly_string) db).p; + const char * psocket = ((ugly_string) socket).p; + + phost = *phost ? phost : 0; + psocket = *psocket ? psocket : 0; + + if (!mysql_real_connect(&con, phost, puser, ppasswd, pdb, port, psocket, cflags)) { + throw GeneralException("Could not connect to MySQL host " + host); } } @@ -22,4 +24,45 @@ SQLConnection::~SQLConnection() { } void SQLConnection::query(String q) throw(GeneralException) { + if (res) { + mysql_free_result(res); + } + + if (mysql_real_query(&con, ((ugly_string)q).p, q.strlen())) { + throw GeneralException(String("Couldn't run query ") + q); + } + + res = mysql_store_result(&con); + + if (res) { + nr = mysql_num_rows(res); + nf = mysql_num_fields(res); + fields = mysql_fetch_fields(res); + } else { + nr = 0; + nf = 0; + fields = 0; + } +} + +int SQLConnection::numrows() { + return nr; +} + +int SQLConnection::numfields() { + return nf; +} + +AssocArray SQLConnection::fetchrow() { + AssocArray r; + MYSQL_ROW row; + int i; + + row = mysql_fetch_row(res); + + for (i = 0; i < nf; i++) { + r[fields[i].name] = row[i]; + } + + return r; } |