From: Florian G. <re...@us...> - 2007-03-11 01:28:45
|
Update of /cvsroot/perfparse/_perfparse/db_tools In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26207/db_tools Modified Files: convert.c Log Message: first table copy. Index: convert.c =================================================================== RCS file: /cvsroot/perfparse/_perfparse/db_tools/convert.c,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** convert.c 6 Mar 2007 23:57:04 -0000 1.14 --- convert.c 11 Mar 2007 01:28:44 -0000 1.15 *************** *** 61,65 **** void addTableRawSummaryData (); void pp_mysql_convert_sql_failure(char*f,int l, const char*str, const char *error); ! void --- 61,67 ---- void addTableRawSummaryData (); void pp_mysql_convert_sql_failure(char*f,int l, const char*str, const char *error); ! void dbtool_mysql_query(const char *sql, MYSQL *p_tmp_db_mysql, MYSQL_RES *p_tmp_query_result); ! void dbtool_freeResult(MYSQL_RES *p_tmp_query_result); ! int dbtool_rows(MYSQL_RES *p_tmp_query_result); void *************** *** 122,125 **** --- 124,129 ---- int iNDBport; + int iStep=1; + int iNumSteps=20; // Number of steps to convert the database MYSQL db_mysql_old; *************** *** 130,133 **** --- 134,146 ---- MYSQL_ROW result_row_new; + + GString *gsNDBSql = g_string_new (""); + GString *gsODBSql = g_string_new (""); + + int iODBnumrows; + int iNDBnumrows; + + int i; + printf(_("The database schema for perfparse changed dramatically. ")); printf(_("Changing the tables\nwould be too error-prone, so we decided to copy the data into a new database.\n")); *************** *** 233,237 **** } ! // connect to old databases if (!mysql_init (&db_mysql_old)) { --- 246,250 ---- } ! // connect to both databases if (!mysql_init (&db_mysql_old)) { *************** *** 243,247 **** sODBhost,sODBuser,sODBpass,sODBdatabase, iODBport,sODBsocket,0)) { ! printf(_("Cannot connect to old database. Errormessage: %s")); exit(EXIT_FAILURE); } --- 256,261 ---- sODBhost,sODBuser,sODBpass,sODBdatabase, iODBport,sODBsocket,0)) { ! printf(_("Cannot connect to old database. Errormessage: %s"), ! mysql_error(&db_mysql_old)); exit(EXIT_FAILURE); } *************** *** 250,257 **** sNDBhost,sNDBuser,sNDBpass,sNDBdatabase, iNDBport,sNDBsocket,0)) { ! printf(_("Cannot connect to new database. Errormessage: %s")); exit(EXIT_FAILURE); } /* --- 264,285 ---- sNDBhost,sNDBuser,sNDBpass,sNDBdatabase, iNDBport,sNDBsocket,0)) { ! printf(_("Cannot connect to new database. Errormessage: %s"), ! mysql_error(&db_mysql_new)); exit(EXIT_FAILURE); } + printf(_("Copying data of old perfdata_host_group to new perfdata_groups table.\n")); + printf(_("(Step %d of %d)\n"),iStep,iNumSteps); + + g_string_printf(gsODBSql, "select * from perfdata_host_group"); + dbtool_mysql_query(gsODBSql->str, &db_mysql_old, query_result_old); + iODBnumrows=dbtool_rows(query_result_old); + for (i=1 ; i<=iODBnumrows; i++) { + printf(_("Copying entry %d/%d"),i,iODBnumrows); + g_string_printf(gsNDBSql, "insert into perfdata_groups(group_name) values('%s')"); + dbtool_mysql_query(gsNDBSql->str, &db_mysql_new, query_result_new); + printf("\r"); + } + printf(_("\ndone\n")); /* *************** *** 405,408 **** --- 433,480 ---- } + void dbtool_mysql_query(const char *sql, MYSQL *p_tmp_db_mysql, MYSQL_RES *p_tmp_query_result) + { + #ifdef SHOW_SQL + printf("SQL: \"%s\"\n", sql); + fflush(stdout); + #endif + dbtool_freeResult(p_tmp_query_result); + + if (mysql_query(p_tmp_db_mysql, sql)) { + printf(_("A error occured executing the sql command %s. The error message is: %s"), + sql, mysql_error(p_tmp_db_mysql)); + exit(EXIT_FAILURE); + } + + /*iRowsAffected = mysql_affected_rows(&tmp_db_mysql);*/ + + p_tmp_query_result = mysql_store_result(p_tmp_db_mysql); + + #ifdef SHOW_SQL + printf("Rows: %d\n", dbtool_rows(p_tmp_query_result)); + fflush(stdout); + #endif + + } + + void dbtool_freeResult(MYSQL_RES *p_tmp_query_result) + { + if (p_tmp_query_result) { + mysql_free_result(p_tmp_query_result); + p_tmp_query_result = NULL; + } + + } + + + int dbtool_rows(MYSQL_RES *p_tmp_query_result) + { + if (p_tmp_query_result) + return mysql_num_rows(p_tmp_query_result); + else + return 0; + } + + void pp_mysql_convert_sql_failure(char*f,int l, const char*str, const char *error) { printf(f,l,"%s (%s)",str,error); |