/* * main.c * * Created on: Apr 4, 2014 * Author: memphis */ #include #include #include #include "ast_db.h" GdaConnection* ast_db_get_connection() { GError* error= NULL; GdaConnection* result = NULL; result = gda_connection_open_from_string("MySQL", "DB_NAME=ast", "USERNAME=root;PASSWORD=",GDA_CONNECTION_OPTIONS_NONE,&error); if (result != NULL) { GdaSqlParser* parserConnection = NULL; //Create parser parserConnection = gda_connection_create_parser (result); if (!parserConnection) /* @cnc doe snot provide its own parser => use default one */ parserConnection = gda_sql_parser_new (); /* attach the parser object to the connection */ g_object_set_data_full (G_OBJECT (result), "parser", parserConnection, g_object_unref); } else { g_critical("Ast:DB Failed to connect to the database. Error : %s", ast_error_get_error_message(error)); if (error != NULL) g_error_free(error); } return result; } void ast_db_release(GdaConnection* connection) { gpointer parser = g_object_steal_data(G_OBJECT(connection), "parser"); if (parser != NULL) g_object_unref(parser); gda_connection_close(connection); g_object_unref(connection); } GValue* ast_db_record_new_full(GdaConnection* dbConnection, const gchar* filename, gint size, guint64 startTime, guint64 endTime, gboolean isProtected, const gchar* timecode, gint sourceID) { GValue* result = NULL; GValue* filenameValue = gda_value_new_from_string(filename, G_TYPE_STRING); GValue* sizeValue = gda_value_new(G_TYPE_INT); g_value_set_int(sizeValue, size); GValue* startTimeValue = gda_value_new(G_TYPE_UINT64); g_value_set_uint64(startTimeValue, startTime); GValue* endTimeValue = gda_value_new(G_TYPE_UINT64); g_value_set_uint64(endTimeValue, endTime); GValue* protectedValue = gda_value_new(G_TYPE_INT); g_value_set_int(protectedValue, isProtected ? 1 : 0); GValue* sourceIDValue = gda_value_new(G_TYPE_INT); g_value_set_int(sourceIDValue, sourceID); GValue* timecodeValue = gda_value_new_from_string(timecode, G_TYPE_STRING); GdaStatement *stmt = NULL; GdaSet * set = NULL; GdaSet * lastRow = NULL; GdaHolder *p = NULL; GError * err = NULL; GObject * res = NULL; stmt = ast_db_common_get_statement(dbConnection, "INSERT INTO Records (filename, size, startTime, endTime, isProtected, timecode, fk_Sources ) VALUES ( ##the_filename::string , ##the_size::gint , ##the_startTime::guint64 , ##the_endTime::guint64 , ##the_protected::gint, ##the_timecode::string, ##the_sourceID::gint )"); if (stmt != NULL) { gda_statement_get_parameters (stmt, &set, NULL); p = gda_set_get_holder (set, "the_filename"); gda_holder_set_value (p, filenameValue, NULL); p = gda_set_get_holder (set, "the_size"); gda_holder_set_value (p, sizeValue, NULL); p = gda_set_get_holder (set, "the_startTime"); gda_holder_set_value (p, startTimeValue, NULL); p = gda_set_get_holder (set, "the_endTime"); gda_holder_set_value (p, endTimeValue, NULL); p = gda_set_get_holder (set, "the_protected"); gda_holder_set_value (p, protectedValue, NULL); p = gda_set_get_holder (set, "the_timecode"); gda_holder_set_value (p, timecodeValue, NULL); p = gda_set_get_holder (set, "the_sourceID"); gda_holder_set_value (p, sourceIDValue, NULL); res = gda_connection_statement_execute(dbConnection,stmt, set,GDA_STATEMENT_MODEL_RANDOM_ACCESS, &lastRow, &err); if (res != NULL) { GdaHolder *h = GDA_HOLDER(lastRow->holders->data); const GValue* holderValue = gda_holder_get_value(h); result = gda_value_new(G_TYPE_INT); g_value_set_int(result, g_value_get_int(holderValue)); g_object_unref(lastRow); g_object_unref(res); } else { g_error ("Could not INSERT data into the 'Recorders' table: %s\n", ast_error_get_error_message(err)); } if (err != NULL) g_error_free(err); g_object_unref(stmt); g_object_unref(set); } gda_value_free(filenameValue); gda_value_free(sizeValue); gda_value_free(startTimeValue); gda_value_free(endTimeValue); gda_value_free(protectedValue); gda_value_free(sourceIDValue); gda_value_free(timecodeValue); return result; } int main(int argc, char* argv[]) { g_type_init(); gint count = 0; gint limit = 10; gda_init(); while (count < limit) { GdaConnection* connection = ast_db_get_connection(); GValue* id = ast_db_record_new_full(connection, "filename", 0, 0, 0, 0, "fddsfsdfdsffds", 7); g_print(" ID : %d ", g_value_get_int(id)); gda_value_free(id); ast_db_release(connection); count++; g_print("Count : %d\n", count); } return 0; }