#include #include #include #include #include #include "ant.h" #include "mysql.h" #include "SDDAS_types.h" #include "mainMySQLWrap.h" #include "libdbMySQL.h" #include "local.h" /* local object variables */ static const char *DEFAULT_CLIENT_DB_NAME = "idfs"; static MYSQL *_SQL = NULL; static char *_name = NULL; static SDDAS_BOOL MySQL_local = sTrue; static pthread_mutex_t _mysql_mutex = PTHREAD_MUTEX_INITIALIZER; SDDAS_BOOL MySQL_dbIsReadOnly(const char* tablename) { return sFalse; } SDDAS_BOOL MySQL_TestDBIntegrity () { MYSQL_RES *result; MYSQL_ROW row; int fields_found = 0; SDDAS_BOOL ret_val = sTrue; /* See if the two functions I usually need are installed */ if (!mysql_query (_SQL, "SELECT * FROM mysql.func;")) { result = mysql_use_result (_SQL); while ((row = mysql_fetch_row (result))) { if (strcmp ("compare_times", row [0]) == 0) fields_found++; else if (strcmp ("between_times", row [0]) == 0) fields_found++; } mysql_free_result (result); } if (fields_found < 2) { char error [1024]; sprintf (error, "Database is not configured with IDFS/SDDAS functions!\n%s", mysql_error (_SQL)); SetErrorString (error); ret_val = sFalse; } return (ret_val); } SDDAS_BOOL MySQL_dbIsLocal() { if(MySQL_local == sTrue) return sTrue; else return sFalse; } SDDAS_BOOL MySQL_dbCreate (const char *filename) { char query_str[256]; void *result; /* If _SQL is not NULL, assume the database is opened! */ if (_SQL == NULL) { /* set the database name */ if (filename != NULL) _name = strdup (filename); /* Create our structure */ _SQL = mysql_init (NULL); if (_SQL == NULL) { SetErrorString ("dbInitialize - Out of memory?"); return (sFalse); } /* Connect to the database server on this machine */ if (mysql_real_connect (_SQL, NULL, "", NULL, NULL, 0, NULL, 0) == NULL) { SetErrorString (mysql_error (_SQL)); mysql_close (_SQL); _SQL = NULL; return (sFalse); } if (!MySQL_TestDBIntegrity()) return (sFalse); } /* Now that the database is open, create a new databse */ sprintf (query_str, "CREATE DATABASE %s;", filename); if ((result = MySQL_dbQuery (query_str)) == NULL) { if(MySQL_dbBadQuery ()) { // if there was no result and a bad query SetErrorString (mysql_error (_SQL)); MySQL_dbFreeResult (result); return sFalse; } } MySQL_dbFreeResult (result); return sTrue; } void MySQL_dbSetDatabaseName (const char *name) { free (_name); _name = strdup (name); } const char *MySQL_dbGetDatabaseName () { if (_name == NULL) { if ((_name = GetEnv ("SDDAS_DB_NAME")) == NULL) { _name = strdup (DEFAULT_CLIENT_DB_NAME); } else { _name = strdup (GetEnv ("SDDAS_DB_NAME")); } } return _name; } SDDAS_BOOL MySQL_dbInitializeMsg () { /* If _SQL is not NULL, assume the database is opened! */ if (_SQL == NULL) { /* set the database name */ if (_name == NULL) MySQL_dbGetDatabaseName(); /* Create our structure */ _SQL = mysql_init (NULL); if (_SQL == NULL) { SetErrorString ("dbInitialize - Out of memory?"); return (sFalse); } /* Connect to the database server on this machine */ if (mysql_real_connect (_SQL, NULL, "", NULL, NULL, 0, NULL, 0) == NULL) { SetErrorString (mysql_error (_SQL)); mysql_close (_SQL); _SQL = NULL; return (sFalse); } /* See if the database exists */ if (mysql_select_db (_SQL, _name) != 0) { char error [1024]; sprintf (error, "mysql_select_db: %s does not exist!", _name); SetErrorString (error); mysql_close (_SQL); _SQL = NULL; return (sFalse); } return (MySQL_TestDBIntegrity ()); } /* All is well if it gets this far */ return (sTrue); } SDDAS_BOOL MySQL_dbInitializeRemoteMsg (const char *host, const char *user, const char *passwd, const char *db, const unsigned int port) { /* If _SQL is not NULL, assume the database is opened! */ if (_SQL == NULL) { /* Create our structure */ _SQL = mysql_init (NULL); if (_SQL == NULL) { SetErrorString ("dbInitialize - Out of memory?"); return (sFalse); } /* Connect to the database server on this machine */ if (mysql_real_connect (_SQL, host, user, passwd, db, port, NULL, 0) == NULL) { SetErrorString (mysql_error (_SQL)); mysql_close (_SQL); _SQL = NULL; return (sFalse); } /* See if the database exists */ /* if (mysql_select_db (_SQL, db) != 0) { sprintf (_error_msg, "mysql_select_db: %s does not exist!", db); mysql_close (_SQL); return (sFalse); } return (TestDBIntegrity ()); */ } /* All is well if it gets this far */ MySQL_local = sFalse; return (sTrue); } void MySQL_dbInitialize () { if (MySQL_dbInitializeMsg () == sFalse) { printf ("dbInitialize: %s\n", GetErrorString ()); exit (-1); } } void MySQL_dbClose () { if (_SQL != NULL) { mysql_close (_SQL); free (_name); _name = NULL; _SQL = NULL; } } void *MySQL_dbQuery (const char *query_str) { MYSQL_RES *result; assert (_SQL != NULL); if (!mysql_query (_SQL, query_str)) { result = mysql_use_result (_SQL); return ((void *) result); } SetErrorString (mysql_error (_SQL)); printf ("dbQuery: %s\n", GetErrorString ()); return (NULL); } int MySQL_dbQueryExec (const char *query_str) { void *rc; if ((rc = MySQL_dbQueryStore (query_str)) != NULL) { MySQL_dbFreeResult (rc); } return (mysql_errno (_SQL) == 0 ? 1 : 0); /* return a zero if error occured or a one if no error - MySQL is opposite of SQLite */ } void *MySQL_dbQueryStore (const char *query_str) { MYSQL_RES *result; assert (_SQL != NULL); pthread_mutex_lock (&_mysql_mutex); /* locks the database */ if (!mysql_query (_SQL, query_str)) { result = mysql_store_result (_SQL); pthread_mutex_unlock (&_mysql_mutex); /* unlocks the database */ return ((void *) result); } SetErrorString (mysql_error (_SQL)); pthread_mutex_unlock (&_mysql_mutex); /* unlocks the database */ return (NULL); } unsigned int MySQL_dbBadQuery () { assert (_SQL != NULL); unsigned int ret_val = mysql_errno (_SQL); if (ret_val == 0) { if (mysql_affected_rows (_SQL) == 0) { ret_val = 1; SetErrorString ("No rows affected by query!"); } } return (ret_val); } void MySQL_dbFreeResult (void *result) { mysql_free_result ((MYSQL_RES *) result); } void *MySQL_dbFetchRow (void *result) { return ((void *)mysql_fetch_row ((MYSQL_RES *) result)); } char *MySQL_dbFieldName (void *result, int i) { MYSQL_FIELD *fields = mysql_fetch_fields ((MYSQL_RES*)result); return (fields [i].name); } int MySQL_dbNumberRows (void *result) { return (mysql_num_rows ((MYSQL_RES *) result)); } int MySQL_dbNumberFields (void *result) { return (mysql_num_fields ((MYSQL_RES *) result)); } SDDAS_BOOL MySQL_dbConnected () { if (_SQL == NULL) return sFalse; else return sTrue; } SDDAS_BOOL MySQL_dbTableExists (const char *table_name) { char query_str [1024]; void *result; void *row; int count = 0; snprintf (query_str, 1024, "SELECT COUNT(*) FROM information_schema.tables WHERE table_name = '%s'", table_name); if ((result = MySQL_dbQuery (query_str)) != NULL) { row = MySQL_dbFetchRow (result); count = atoi (row); MySQL_dbFreeResult (result); } return (count == 1 ? sTrue : sFalse); }