Changeset 15601

Show
Ignore:
Timestamp:
20.05.2008 15:58:55 (12 years ago)
Author:
mdewsnip
Message:

(Adding new DB support) Started adding sqliteclass internals, beginning with an sqlexec() function.

Location:
gsdl/trunk/lib
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • gsdl/trunk/lib/sqliteclass.cpp

    r15599 r15601  
    2525 
    2626#include "sqliteclass.h" 
     27 
     28 
     29#define SQLITE_MAX_RETRIES 8 
    2730 
    2831 
     
    124127  return g_EmptyText; 
    125128} 
     129 
     130 
     131// sqlexec simply executes the given sql statement - it doesn't obtain a 
     132// result set - returns true if the sql statement was executed successfully 
     133// logerrors may be set to false to prevent this method from logging an 
     134// error if it fails - this is useful in some circumstances when we execute 
     135// an sql command specifically to see if it fails (e.g. when testing if a 
     136// table exists in the db) 
     137bool sqliteclass::sqlexec(const text_t &sql_cmd, bool logerrors) 
     138{ 
     139  char *sql_cmd_cstr = sql_cmd.getcstr(); 
     140 
     141  int rv = 0; 
     142  int tries = 0; 
     143  while ((rv = sqlite3_exec(sqlitefile, sql_cmd_cstr, NULL, NULL, NULL)) == SQLITE_BUSY) 
     144  { 
     145    sleep(1000); 
     146    tries++; 
     147    if (tries > SQLITE_MAX_RETRIES) 
     148    { 
     149      outconvertclass text_t2ascii; 
     150      (*logout) << text_t2ascii << "max_retries exceeded for sql query: " << sql_cmd << "\n"; 
     151      break; 
     152    } 
     153  } 
     154 
     155  delete[] sql_cmd_cstr; 
     156 
     157  if (rv == SQLITE_OK) return true; 
     158 
     159  if (logerrors) 
     160  { 
     161    outconvertclass text_t2ascii; 
     162    (*logout) << text_t2ascii << "Error executing sql statement: " << sql_cmd << "\n"; 
     163  } 
     164 
     165  return false; 
     166} 
     167 
     168 
     169// sleep for the given number of milliseconds 
     170void sleep(int m) 
     171{ 
     172#ifdef __WIN32__ 
     173  Sleep(m); 
     174#else 
     175  usleep(m); 
     176#endif 
     177} 
  • gsdl/trunk/lib/sqliteclass.h

    r15599 r15601  
    6464  text_t openfile; 
    6565  sqlite3* sqlitefile; 
     66 
     67  bool sqlexec(const text_t &sql_cmd, bool logerrors); 
    6668}; 
    6769