Logo Search packages:      
Sourcecode: dballe version File versions

dba_err dba_db_reset ( dba_db  db,
const char *  repinfo_file 
)

Reset the database, removing all existing DBALLE tables and re-creating them empty.

Parameters:
db The dballe session id
repinfo_file The name of the CSV file with the report type information data to load. The file is in CSV format with 6 columns: report code, mnemonic id, description, priority, descriptor, table A category. If repinfo_file is NULL, then the default of /etc/dballe/repinfo.csv is used.
Returns:
The error indicator for the function (
See also:
dba_err)

Definition at line 321 of file dba_db.c.

References DBA_RUN_OR_GOTO.

{
      int res;
      int i;
      SQLHSTMT stm;
      dba_err err;

      assert(db);

      if (deffile == 0)
      {
            deffile = getenv("DBA_REPINFO");
            if (deffile == 0 || deffile[0] == 0)
                  deffile = TABLE_DIR "/repinfo.csv";
      }

      /* Open the input CSV file */
      FILE* in = fopen(deffile, "r");
      if (in == NULL)
            return dba_error_system("opening file %s", deffile);

      /* Drop existing tables */
      DBA_RUN_OR_GOTO(fail0, dba_db_delete_tables(db));

      /* Allocate statement handle */
      DBA_RUN_OR_GOTO(fail0, dba_db_statement_create(db, &stm));

      /* Create tables */
      for (i = 0; i < sizeof(init_queries) / sizeof(init_queries[0]); i++)
      {
            /* Casting out 'const' because ODBC API is not const-conscious */
            res = SQLExecDirect(stm, (unsigned char*)init_queries[i], SQL_NTS);
            if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO))
            {
                  err = dba_db_error_odbc(SQL_HANDLE_STMT, stm,
                              "Executing database-initialization query %s", init_queries[i]);
                  goto fail1;
            }
      }

      /* Populate the tables with values */
      {
            int id;
            char memo[30];
            char description[255];
            int prio;
            char descriptor[6];
            int tablea;
            int i;
            char* columns[7];
            int line;

            res = SQLPrepare(stm, (unsigned char*)
                        "INSERT INTO repinfo (id, memo, description, prio, descriptor, tablea)"
                        "     VALUES (?, ?, ?, ?, ?, ?)", SQL_NTS);
            if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO))
            {
                  err = dba_db_error_odbc(SQL_HANDLE_STMT, stm, "compiling query to insert into 'repinfo'");
                  goto fail1;
            }

            SQLBindParameter(stm, 1, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER, 0, 0, &id, 0, 0);
            SQLBindParameter(stm, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, memo, 0, 0);
            SQLBindParameter(stm, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, description, 0, 0);
            SQLBindParameter(stm, 4, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, &prio, 0, 0);
            SQLBindParameter(stm, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, descriptor, 0, 0);
            SQLBindParameter(stm, 6, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER, 0, 0, &tablea, 0, 0);

            for (line = 0; (i = dba_csv_read_next(in, columns, 7)) != 0; line++)
            {
                  if (i != 6)
                  {
                        err = dba_error_parse(deffile, line, "Expected 6 columns, got %d", i);
                        goto fail1;
                  }
                        
                  id = strtol(columns[0], 0, 10);
                  strncpy(memo, columns[1], 30); memo[29] = 0;
                  strncpy(description, columns[2], 255); description[254] = 0;
                  prio = strtol(columns[3], 0, 10);
                  strncpy(descriptor, columns[4], 6); descriptor[5] = 0;
                  tablea = strtol(columns[5], 0, 10);

                  res = SQLExecute(stm);
                  if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO))
                  {
                        err = dba_db_error_odbc(SQL_HANDLE_STMT, stm, "inserting new data into 'data'");
                        goto fail1;
                  }

                  for (i = 0; i < 6; i++)
                        free(columns[i]);
            }
      }

      SQLFreeHandle(SQL_HANDLE_STMT, stm);

      return dba_error_ok();

fail1:
      SQLFreeHandle(SQL_HANDLE_STMT, stm);
fail0:
      fclose(in);
      return err;
}


Generated by  Doxygen 1.6.0   Back to index