Beispiel #1
0
BOOL CSQLite::Execute(const uchar* query)
{
    int rc;

    // prepare query
    finalizeStatement();
    rc = sqlite3_prepare(sdb, (const char*) query, -1, &pStmt, NULL);
    if (rc != SQLITE_OK) {
        strcpy(lastError, sqlite3_errmsg(sdb));
        finalizeStatement();
        return false;
    }
    // execute step
    rc = sqlite3_step(pStmt);
    switch (rc) {
        case SQLITE_DONE:
            finalizeStatement();
            break;
        case SQLITE_ROW:
            firstFetch = true;
            break;
        case SQLITE_ERROR:
            strcpy(lastError, sqlite3_errmsg(sdb));
            finalizeStatement();
            return false;
            break;
    }

    return true;
}
Beispiel #2
0
BOOL CSQLite::WriteScorcoData(char* SQL, BYTE* pData, int Length)
{
    int rc;
    char* pSQL = (char*) malloc(strlen(SQL) + 1);
    sprintf(pSQL, SQL, "?");

    finalizeStatement();
    rc = sqlite3_prepare(sdb, pSQL, -1, &pStmt, NULL);
    free(pSQL);

    if (rc != SQLITE_OK) {
        strcpy(lastError, sqlite3_errmsg(sdb));
        finalizeStatement();
        return false;
    }

    rc = sqlite3_bind_blob(pStmt, 1, (const void*) pData, Length, SQLITE_STATIC);
    rc = sqlite3_step(pStmt);
    finalizeStatement();

    if (rc != SQLITE_DONE) {
        strcpy(lastError, sqlite3_errmsg(sdb));
        return false;
    }

    return true;
}
Beispiel #3
0
static void fillGlueTables(sqlite3 *db)
{
  int i;
  sqlite3_stmt *statement;
  int res;
                
  statement = prepareStatement(db,
                               "INSERT OR IGNORE INTO event_kind (name, description, enum)"
                               "VALUES (?, ?, ?)");
        
  i = 0;
  EventKindENUM(EVENT_KIND_DO_INSERT, X);
        
  finalizeStatement(db, statement);
        
  statement = prepareStatement(db, 
                               "INSERT OR IGNORE INTO event_type (name, code, always, kind)"
                               "VALUES (?, ?, ?, ?)");
  EVENT_LIST(EVENT_TYPE_DO_INSERT, X);
        
  finalizeStatement(db, statement);

  statement = prepareStatement(db,
                               "INSERT OR IGNORE INTO event_param (type, param_index, sort, ident)"
                               "VALUES (?, ?, ?, ?)");
  EVENT_LIST(EVENT_TYPE_INSERT_PARAMS, X);
        
  finalizeStatement(db, statement);
}
Beispiel #4
0
static int tableExists(sqlite3* db, const char *tableName)
{
  int res;
  int exists = 0;
  sqlite3_stmt *statement = NULL;

  statement = prepareStatement(db,
                               "SELECT 1 FROM sqlite_master WHERE type='table' AND name=?");
  res = sqlite3_bind_text(statement, 1, tableName, -1, SQLITE_STATIC);
  if (res != SQLITE_OK)
    sqlite_error(res, db, "table existence bind of name failed.");
  res = sqlite3_step(statement);
  switch(res) {
  case SQLITE_DONE:
    exists = 0;
    break;
  case SQLITE_ROW:
    exists = 1;
    break;
  default:
    sqlite_error(res, db, "select from sqlite_master failed.");
  }
  finalizeStatement(db, statement);
  return exists;
}
Beispiel #5
0
void CSQLite::Disconnect()
{
    int rc;

    // end implicit transaction
    finalizeStatement();
    rc = sqlite3_prepare(sdb, "COMMIT", -1, &pStmt, NULL);
    if (rc != SQLITE_OK)
        strcpy(lastError, sqlite3_errmsg(sdb));
    else {
        rc = sqlite3_step(pStmt);
        if (rc != SQLITE_DONE)
            strcpy(lastError, sqlite3_errmsg(sdb));
    }
    finalizeStatement();
    sqlite3_close(sdb);
}
Beispiel #6
0
BYTE * CSQLite::ReadScorcoData(const char * SQL, const char * param, BOOL * pSqlError, int * size)
{
    int rc;
    const void* pBlob;

    if (strcmp(param, "FETCHMODE") != 0) {
        finalizeStatement();
        rc = sqlite3_prepare(sdb, SQL, -1, &pStmt, NULL);
        if (rc != SQLITE_OK) {
            strcpy(lastError, sqlite3_errmsg(sdb));
            finalizeStatement();
            *pSqlError = true;
            return NULL;
        }
    }

    // execute step
    rc = sqlite3_step(pStmt);
    if (rc == SQLITE_ERROR) {
        strcpy(lastError, sqlite3_errmsg(sdb));
        *pSqlError = true;
        finalizeStatement();
        return NULL;
    }

    *pSqlError = false;
    if (rc == SQLITE_ROW) {
        unsigned long length = sqlite3_column_bytes(pStmt, 0);
        *size = length;
        if (length == 0) return NULL;
        char* buf = new char[length];
        if (buf == NULL) return NULL;
        pBlob = sqlite3_column_blob(pStmt, 0);
        if (pBlob == NULL) { delete [] buf; return NULL; }
        memcpy(buf, pBlob, length);
        return (BYTE*)buf; // NWN VM will delete this block of memory
    } else if (rc == SQLITE_DONE) {
        finalizeStatement();
    }
    return NULL;
}
Beispiel #7
0
static void logFileCompleted(sqlite3 *db,
                             int64 completed)
{
  sqlite3_stmt *statement;
  int res;

  statement = prepareStatement(db,
                               "UPDATE event_log SET completed=? WHERE serial=?");
  res = sqlite3_bind_int64(statement, 2, logSerial);
  if (res != SQLITE_OK)
    sqlite_error(res, db, "event_log update bind of serial failed.");
  res = sqlite3_bind_int64(statement, 1, completed);
  if (res != SQLITE_OK)
    sqlite_error(res, db, "event_log update bind of completed failed.");
  res = sqlite3_step(statement); 
  if (res != SQLITE_DONE)
    sqlite_error(res, db, "insert into event_log failed.");
  evlog(LOG_SOMETIMES, "Marked in event_log: %lu events", completed);
  finalizeStatement(db, statement);
}
Beispiel #8
0
BOOL CSQLite::Connect(const char * db)
{
    int rc;
    rc = sqlite3_open(db, &sdb);
    if (rc) {
        sprintf(lastError, "Can't open database: %s", sqlite3_errmsg(sdb));
        sqlite3_close(sdb);
        return false;
    }

    // begin implicit transaction
    rc = sqlite3_prepare(sdb, "BEGIN", -1, &pStmt, NULL);
    if (rc != SQLITE_OK)
        strcpy(lastError, sqlite3_errmsg(sdb));
    else {
        rc = sqlite3_step(pStmt);
        if (rc != SQLITE_DONE)
            strcpy(lastError, sqlite3_errmsg(sdb));
        finalizeStatement();
    }

    return true;
}
Beispiel #9
0
char * CSQLite::Fetch(char * buffer, unsigned int buffersize)
{
    // Move a database cursor to the next (requested) row.
    int rc;
    if (firstFetch) {
        firstFetch = false;
        rc = SQLITE_ROW;
    } else
        rc = sqlite3_step(pStmt);

    switch (rc) {
        case SQLITE_BUSY:
            strncpy(lastError, "CSQLite::Fetch: The database file is locked!", lastErrorSize);
            finalizeStatement();
            return NULL;

        case SQLITE_ERROR:
            strncpy(lastError, sqlite3_errmsg(sdb), lastErrorSize);
            finalizeStatement();
            return NULL;

        case SQLITE_MISUSE:
            strncpy(lastError, "CSQLite::Fetch: Library used incorrectly.", lastErrorSize);
            finalizeStatement();
            return NULL;

        case SQLITE_ROW:
            break;

        default:
            finalizeStatement();
            return NULL;
    };

    // Calculate length of the row.
    int NumCol = sqlite3_column_count(pStmt);
    unsigned long row_length = 0;
    for (int i = 0; i < NumCol; ++i) {
        const char * column = (const char *)sqlite3_column_text(pStmt, i);
        if (column) {
            // We will need one more character per a column to act as a column separator and a NULL terminating
            // character.
            row_length += strlen(column) + 1;
        }
    }
    if (row_length == 0)
        return NULL;

    // If the row length exceeds size of the SPACER buffer, allocate a new one.
    // The SPACER will be deleted automatically in nwnx2lib.cpp.
    char * result = row_length <= buffersize ? buffer : (char *)malloc(row_length);

    // Copy content of the columns to the buffer.
    for (int i = 0, pos = 0; i < NumCol; ++i) {
        const char * column = (const char *)sqlite3_column_text(pStmt, i);
        if (column) {
            int length = strlen(column);
            strncpy(&result[pos], column, length);
            pos += length;
            result[pos++] = '¬';
        }
    }
    result[row_length - 1] = '\0';

    return result;
}
Beispiel #10
0
static void registerLogFile(sqlite3 *db,
                            const char *filename)
{
  sqlite3_stmt *statement;
  int res;
  const unsigned char *name;
  int64 completed;
  int64 file_size;
  int64 file_modtime;

  if (filename) {
    struct stat st;
    res = stat(filename, &st);
    if (res != 0)
      error("Couldn't stat() %s", filename);
    file_size = st.st_size;
    file_modtime = st.st_mtime;
                        
    statement = prepareStatement(db,
                                 "SELECT name, serial, completed FROM event_log"
                                 " WHERE size = ? AND modtime = ?");
    res = sqlite3_bind_int64(statement, 1, file_size);
    if (res != SQLITE_OK)
      sqlite_error(res, db, "event_log bind of size failed.");
    res = sqlite3_bind_int64(statement, 2, file_modtime);
    if (res != SQLITE_OK)
      sqlite_error(res, db, "event_log bind of modtime failed.");
    res = sqlite3_step(statement); 
    switch(res) {
    case SQLITE_DONE:
      evlog(LOG_SOMETIMES, "No log file matching '%s' found in database.", filename);
      break;
    case SQLITE_ROW:
      name = sqlite3_column_text(statement, 0);
      logSerial = sqlite3_column_int64(statement, 1);
      completed = sqlite3_column_int64(statement, 2);
      evlog(force ? LOG_OFTEN : LOG_ALWAYS, "Log file matching '%s' already in event_log, named \"%s\" (serial %lu, completed %lu).",
            filename, name, logSerial, completed);
      if (force) {
        evlog(LOG_OFTEN, "Continuing anyway because -f specified.");
      } else {
        evlog(LOG_ALWAYS, "Exiting.  Specify -f to force events into SQL anyway.");
        exit(0);
      }
      break;
    default:
      sqlite_error(res, db, "select from event_log failed.");
    }
    finalizeStatement(db, statement);
  } else { /* stdin */
    filename = "<stdin>";
    file_size = 0;
    file_modtime = 0;
  }
  statement = prepareStatement(db,
                               "INSERT into event_log (name, size, modtime, completed)"
                               " VALUES (?, ?, ?, 0)");
  res = sqlite3_bind_text(statement, 1, filename, -1, SQLITE_STATIC);
  if (res != SQLITE_OK)
    sqlite_error(res, db, "event_log insert bind of name failed.");
  res = sqlite3_bind_int64(statement, 2, file_size);
  if (res != SQLITE_OK)
    sqlite_error(res, db, "event_log insert bind of size failed.");
  res = sqlite3_bind_int64(statement, 3, file_modtime);
  if (res != SQLITE_OK)
    sqlite_error(res, db, "event_log insert bind of modtime failed.");
  res = sqlite3_step(statement); 
  if (res != SQLITE_DONE)
    sqlite_error(res, db, "insert into event_log failed.");
  logSerial = sqlite3_last_insert_rowid(db);
  evlog(LOG_SOMETIMES, "Log file %s added to event_log with serial %lu",
        filename, logSerial);
  finalizeStatement(db, statement);
}