Exemple #1
0
const char *
dbop3_next(DBOP *dbop) {
	int rc;
	char *key, *dat;

	/*
	 *	0: rowid
	 *	1: key
	 *	2: dat
	 *	3: flags
	 */
	for (;;) {
		/* Once it receives SQLITE_DONE, do not never return value */
		if (dbop->done)
			return NULL;
		rc = sqlite3_step(dbop->stmt);
		if (rc == SQLITE_DONE)
			goto finish;
		else if (rc == SQLITE_ROW) {
			dbop->readcount++;
			dbop->lastrowid = sqlite3_column_int64(dbop->stmt, 0);
			key = (char *)sqlite3_column_text(dbop->stmt, 1);
			dat = (char *)sqlite3_column_text(dbop->stmt, 2);
			/* skip meta records */
			if (!(dbop->openflags & DBOP_RAW)) {
				if (dbop->ioflags & DBOP_KEY && ismeta(key))
					continue;
				else if (ismeta(dat))
					continue;
			}
			if (dbop->ioflags & DBOP_KEY) {
				if (!strcmp(dbop->prev, key))
					continue;
				if (strlen(key) > MAXKEYLEN)
					die("primary key too long.");
				strlimcpy(dbop->prev, key, sizeof(dbop->prev));
			}
			if (dbop->ioflags & DBOP_PREFIX) {
				if (strncmp(key, dbop->key, dbop->keylen))
					goto finish;
			} else if (dbop->keylen) {
				if (strcmp(key, dbop->key)) 
					goto finish;
			}
			if (dbop->preg && regexec(dbop->preg, key, 0, 0, 0) != 0)
				continue;
			break;
		} else {
			die("dbop3_next: something is wrong (rc = %d).", rc);
		}
	}
	strbuf_clear(dbop->sb);
	strbuf_puts0(dbop->sb, (char *)sqlite3_column_text(dbop->stmt, 2));
	dbop->lastsize = strbuf_getlen(dbop->sb) - 1;
	dbop->lastflag = (char *)sqlite3_column_text(dbop->stmt, 3);
	if (dbop->lastflag)
		strbuf_puts(dbop->sb, dbop->lastflag);
	dbop->lastdat = strbuf_value(dbop->sb);
	if (dbop->lastflag)
		dbop->lastflag = dbop->lastdat + dbop->lastsize + 1;
	dbop->lastkey = key;
	dbop->lastkeysize = strlen(dbop->lastkey);
	if (dbop->ioflags & DBOP_KEY) {
		strlimcpy(dbop->prev, key, sizeof(dbop->prev));
		return key;
	}
	return dbop->lastdat;
finish:
	dbop->done = 1;
	dbop->lastdat = NULL;
	dbop->lastsize = 0;
	return dbop->lastdat;
}
Exemple #2
0
void Database_SQLite3::endSave() {
	verifyDatabase();
	SQLRES(sqlite3_step(m_stmt_end), SQLITE_DONE,
		"Failed to commit SQLite3 transaction");
	sqlite3_reset(m_stmt_end);
}
Exemple #3
0
/*
** Execute SQL code.  Return one of the SQLITE_ success/failure
** codes.  Also write an error message into memory obtained from
** malloc() and make *pzErrMsg point to that message.
**
** If the SQL is a query, then for each row in the query result
** the xCallback() function is called.  pArg becomes the first
** argument to xCallback().  If xCallback=NULL then no callback
** is invoked, even for queries.
*/
SQLITE_EXPORT int sqlite3_exec(
  sqlite3 *db,                /* The database on which the SQL executes */
  const char *zSql,           /* The SQL to be executed */
  sqlite3_callback xCallback, /* Invoke this callback routine */
  void *pArg,                 /* First argument to xCallback() */
  char **pzErrMsg             /* Write error messages here */
){
  int rc = SQLITE_OK;
  const char *zLeftover;
  sqlite3_stmt *pStmt = 0;
  char **azCols = 0;

  int nRetry = 0;
  int nCallback;

  if( zSql==0 ) return SQLITE_OK;
  while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
    int nCol;
    char **azVals = 0;

    pStmt = 0;
    rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
    assert( rc==SQLITE_OK || pStmt==0 );
    if( rc!=SQLITE_OK ){
      continue;
    }
    if( !pStmt ){
      /* this happens for a comment or white-space */
      zSql = zLeftover;
      continue;
    }

    nCallback = 0;

    nCol = sqlite3_column_count(pStmt);
    azCols = sqliteMalloc(2*nCol*sizeof(const char *) + 1);
    if( azCols==0 ){
      goto exec_out;
    }

    while( 1 ){
      int i;
      rc = sqlite3_step(pStmt);

      /* Invoke the callback function if required */
      if( xCallback && (SQLITE_ROW==rc || 
          (SQLITE_DONE==rc && !nCallback && db->flags&SQLITE_NullCallback)) ){
        if( 0==nCallback ){
          for(i=0; i<nCol; i++){
            azCols[i] = (char *)sqlite3_column_name(pStmt, i);
          }
          nCallback++;
        }
        if( rc==SQLITE_ROW ){
          azVals = &azCols[nCol];
          for(i=0; i<nCol; i++){
            azVals[i] = (char *)sqlite3_column_text(pStmt, i);
            if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
              rc = SQLITE_NOMEM;
              goto exec_out;
            }
          }
        }
        if( xCallback(pArg, nCol, azVals, azCols) ){
          rc = SQLITE_ABORT;
          goto exec_out;
        }
      }

      if( rc!=SQLITE_ROW ){
        rc = sqlite3_finalize(pStmt);
        pStmt = 0;
        if( rc!=SQLITE_SCHEMA ){
          nRetry = 0;
          zSql = zLeftover;
          while( isspace((unsigned char)zSql[0]) ) zSql++;
        }
        break;
      }
    }

    sqliteFree(azCols);
    azCols = 0;
  }

exec_out:
  if( pStmt ) sqlite3_finalize(pStmt);
  if( azCols ) sqliteFree(azCols);

  rc = sqlite3ApiExit(0, rc);
  if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){
    *pzErrMsg = sqlite3_malloc(1+strlen(sqlite3_errmsg(db)));
    if( *pzErrMsg ){
      strcpy(*pzErrMsg, sqlite3_errmsg(db));
    }
  }else if( pzErrMsg ){
    *pzErrMsg = 0;
  }

  assert( (rc&db->errMask)==rc );
  return rc;
}
Exemple #4
0
bool QgsStyleV2::load( const QString& filename )
{
  mErrorString.clear();

  // Open the sqlite database
  if ( !openDB( filename ) )
  {
    mErrorString = "Unable to open database file specified";
    QgsDebugMsg( mErrorString );
    return false;
  }

  // Make sure there are no Null fields in parenting symbols ang groups
  char *query = sqlite3_mprintf( "UPDATE symbol SET groupid=0 WHERE groupid IS NULL;"
                                 "UPDATE colorramp SET groupid=0 WHERE groupid IS NULL;"
                                 "UPDATE symgroup SET parent=0 WHERE parent IS NULL;" );
  runEmptyQuery( query );

  // First create all the main symbols
  query = sqlite3_mprintf( "SELECT * FROM symbol" );

  sqlite3_stmt *ppStmt;
  int nError = sqlite3_prepare_v2( mCurrentDB, query, -1, &ppStmt, nullptr );
  while ( nError == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
  {
    QDomDocument doc;
    QString symbol_name = QString::fromUtf8( reinterpret_cast< const char * >( sqlite3_column_text( ppStmt, SymbolName ) ) );
    QString xmlstring = QString::fromUtf8( reinterpret_cast< const char * >( sqlite3_column_text( ppStmt, SymbolXML ) ) );
    if ( !doc.setContent( xmlstring ) )
    {
      QgsDebugMsg( "Cannot open symbol " + symbol_name );
      continue;
    }

    QDomElement symElement = doc.documentElement();
    QgsSymbolV2 *symbol = QgsSymbolLayerV2Utils::loadSymbol( symElement );
    if ( symbol )
      mSymbols.insert( symbol_name, symbol );
  }

  sqlite3_finalize( ppStmt );

  query = sqlite3_mprintf( "SELECT * FROM colorramp" );
  nError = sqlite3_prepare_v2( mCurrentDB, query, -1, &ppStmt, nullptr );
  while ( nError == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
  {
    QDomDocument doc;
    QString ramp_name = QString::fromUtf8( reinterpret_cast< const char * >( sqlite3_column_text( ppStmt, ColorrampName ) ) );
    QString xmlstring = QString::fromUtf8( reinterpret_cast< const char * >( sqlite3_column_text( ppStmt, ColorrampXML ) ) );
    if ( !doc.setContent( xmlstring ) )
    {
      QgsDebugMsg( "Cannot open symbol " + ramp_name );
      continue;
    }
    QDomElement rampElement = doc.documentElement();
    QgsVectorColorRampV2 *ramp = QgsSymbolLayerV2Utils::loadColorRamp( rampElement );
    if ( ramp )
      mColorRamps.insert( ramp_name, ramp );
  }

  mFileName = filename;
  return true;
}
bool rec_sqlite_loadTable ( char *tableName )
{
	traceLastFunc( "rec_sqlite_loadTable()" );

	sqlite3 *rec_db;
	sqlite3_stmt *prep_stmt;
	int prep_step_ret;
	char sql_cmd[1024];

	if ( sqlite3_open( REC_DB_NAME, &rec_db ) != SQLITE_OK )
	{
		Log( "SQLite - Error while connecting: %s", sqlite3_errmsg(rec_db) );
		sqlite3_close( rec_db );
		return false;
	}

	// return false, if error happens, or table doesn't exist
	if ( sqliteDB_checkTableExists( rec_db, tableName ) != 1 )
	{
		sqlite3_close( rec_db );
		cheat_state_text( "table doesn't exist" );
		return false;
	}

	// stop playing/recording when loading a new route
	rec_state = RECORDING_OFF;

	_snprintf_s( sql_cmd, sizeof(sql_cmd)-1, "SELECT * FROM '%s';", tableName );
	if ( sqlite3_prepare_v2( rec_db, sql_cmd, sizeof(sql_cmd), &prep_stmt, NULL ) != SQLITE_OK )
	{
		Log( "SQLite - Error (prepare statement to load from table '%s'): %s", tableName, sqlite3_errmsg(rec_db) );
		sqlite3_close( rec_db );
		return false;
	}

	// jump to first row and set the maxNum
	prep_step_ret = sqlite3_step( prep_stmt );
	rec_maxNum = sqlite3_column_int(prep_stmt,1);

	if ( rec_maxNum > (REC_ARRAYSIZE-1) || rec_maxNum <= 0 )
	{
		Log( "Recording - load table '%s': rec_maxNum(%i) is <= 0 or greater than maximum array size!", 
			tableName, rec_maxNum );
		sqlite3_finalize( prep_stmt );
		sqlite3_close( rec_db );

		cheat_state_text( "failed to load" );
		// we changed a variable, so set maxNum to 0, so it can't be 
		// causing problems when trying to play record
		rec_maxNum = 0;
		return false;
	}

	for ( int i = 0; i < rec_maxNum; i++ )
	{
		// load our data from the table
		// do not forget to adjust these offsets when changing table design
		for ( int j = 0; j < 6; j++ )
			rec_angle[i][j] = sqlite3_column_double( prep_stmt, j+2 );
		for ( int j = 0; j < 3; j++ )
		{
			rec_spin[i][j] = sqlite3_column_double( prep_stmt, j+8 );
			rec_speed[i][j] = sqlite3_column_double( prep_stmt, j+11 );
			rec_pos[i][j] = sqlite3_column_double( prep_stmt, j+14 );
		}

		prep_step_ret = sqlite3_step( prep_stmt );
		
		// step() returned some error/unexpected value?
		if ( prep_step_ret != SQLITE_ROW && prep_step_ret != SQLITE_DONE )
		{
			Log( "SQLite - Error (prepare statement to load from table '%s' - cycle %i): %s", tableName, i, sqlite3_errmsg(rec_db) );
			sqlite3_finalize( prep_stmt );
			sqlite3_close( rec_db );
			
			cheat_state_text( "failed to load" );
			// data has been changed.. destroy record for playback
			rec_maxNum = 0;
			return false;
		}

		// we somehow reached the end (end of rows/end of loop)
		if ( i == (rec_maxNum-1) || prep_step_ret == SQLITE_DONE )
		{
			// check if its only end of one = error while loading
			if ( i != (rec_maxNum-1) || prep_step_ret != SQLITE_DONE )
			{
				Log( "Problem while loading Recording '%s': %s - MaxNum %i - cycleNum %i",
					tableName,
					prep_step_ret == SQLITE_DONE ? "End of rows" : "Not at end of rows",
					rec_maxNum, i );
				sqlite3_finalize( prep_stmt );
				sqlite3_close( rec_db );

				cheat_state_text( "failed to load" );
				// we probably got incorrect data in the recording? - set maxNum to 0
				rec_maxNum = 0;
				return false;
			}

			// we reached the end of rows at expected time (when the loop reaches maxNum-1)
			cheat_state_text( "successfully loaded route" );
		}		
	}
	sqlite3_finalize( prep_stmt );
	sqlite3_close( rec_db );
	return true;
}
Exemple #6
0
static int
cdb_get(struct custdb_handle *ch, const ChopstixPhone *phone,
		ChopstixCustomer *cust)
{
	char *phonestr;
	int64_t cust_ID = 0;
	sqlite3_stmt *q = NULL;
	ChopstixCredit *cc;
	int dupcount = 0;

	CHECKSQL(ch);

	/* get phonestr before zeroing, in case user sends same structure in */
	if ((phonestr = phone2str(phone)) == NULL)
		goto fail;

	bzero(cust, sizeof(*cust));

	/*
	 * CUSTOMER
	 */

	q = SQLARG(ch)->q.cdb.get_cust;
	/* 1 = first parameter in prepared query */
	if (sqlite3_bind_text(q, 1, phonestr, strlen(phonestr), SQLITE_STATIC)) {
		sql_err(SQLARG(ch), "cannot bind phone number to query");
		goto fail;
	}
	while (sqlite3_step(q) == SQLITE_ROW) {
		dupcount++;

		/* start by freeing, incase multiple customers match the same phone */
		free_ChopstixCustomer(cust);

		/* ID, Phone, PhoneExt, Address, Intersection, Special */
		cust_ID = sqlite3_column_int64(q, 0);
		cust->name = sql_strdup(sqlite3_column_text(q, 1));
		str2phone(sqlite3_column_text(q, 2), &cust->phone);
		str2phoneext(sqlite3_column_text(q, 3), &cust->phone);
		cust->addr.addr = sql_strdup(sqlite3_column_text(q, 4));
		cust->addr.apt = sql_strdup(sqlite3_column_text(q, 5));
		cust->addr.entry = sql_strdup(sqlite3_column_text(q, 6));
		cust->isect.cross = sql_strdup(sqlite3_column_text(q, 7));
		if ((cust->special = calloc(1, sizeof(cust->special))))
			*cust->special = sql_strdup(sqlite3_column_text(q, 8));
		cust->reps = sqlite3_column_int(q, 9);
	}
	sqlite3_reset(q);

	if (dupcount > 1)
		sql_warn(SQLARG(ch), "%d duplicate entries", dupcount);

	if (cust_ID == 0) {
		errno = ENOENT;
		return -1;
	}

	/*
	 * CREDITS
	 */
	q = SQLARG(ch)->q.cdb.get_cred;
	/* 1 = first parameter in prepared query */
	if (sqlite3_bind_int(q, 1, cust_ID)) {
		sql_err(SQLARG(ch), "cannot bind customer ID to query");
		goto fail;
	}
	if ((cust->credit = calloc(1, sizeof(*cust->credit))) == NULL)
		goto fail;
	cust->credit->len = 0;
	while (sqlite3_step(q) == SQLITE_ROW) {
		/* increase buffer */
		if ((cc = realloc(cust->credit->val, (cust->credit->len + 1) *
						sizeof(ChopstixCredit))) == NULL)
			goto fail;
		cust->credit->len++;
		cust->credit->val = cc;

		/* Credit, Remain, Reason */
		cust->credit->val[cust->credit->len - 1].credit
			= sqlite3_column_int(q, 0);
		cust->credit->val[cust->credit->len - 1].remain
			= sqlite3_column_int(q, 1);
		cust->credit->val[cust->credit->len - 1].reason
			= sql_strdup(sqlite3_column_text(q, 2));
	}
	sqlite3_reset(SQLARG(ch)->q.cdb.get_cred);

	return 0;

fail:
	free_ChopstixCustomer(cust);
	if (q)
		sqlite3_reset(q);
	return -1;
}
Exemple #7
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);
}
Exemple #8
0
static int
vknn_check_view_rtree (sqlite3 * sqlite, const char *table_name,
		       const char *geom_column, char **real_table,
		       char **real_geom, int *is_geographic)
{
/* checks if the required RTree is actually defined - SpatialView */
    sqlite3_stmt *stmt;
    char *sql_statement;
    int ret;
    int count = 0;
    char *rt = NULL;
    char *rg = NULL;
    int is_longlat = 0;

/* testing if views_geometry_columns exists */
    sql_statement = sqlite3_mprintf ("SELECT tbl_name FROM sqlite_master "
				     "WHERE type = 'table' AND tbl_name = 'views_geometry_columns'");
    ret =
	sqlite3_prepare_v2 (sqlite, sql_statement, strlen (sql_statement),
			    &stmt, NULL);
    sqlite3_free (sql_statement);
    if (ret != SQLITE_OK)
	return 0;
    while (1)
      {
	  /* scrolling the result set rows */
	  ret = sqlite3_step (stmt);
	  if (ret == SQLITE_DONE)
	      break;		/* end of result set */
	  if (ret == SQLITE_ROW)
	      count++;
      }
    sqlite3_finalize (stmt);
    if (count != 1)
	return 0;
    count = 0;

/* attempting to find the RTree Geometry Column */
    sql_statement =
	sqlite3_mprintf
	("SELECT a.f_table_name, a.f_geometry_column, SridIsGeographic(b.srid) "
	 "FROM views_geometry_columns AS a " "JOIN geometry_columns AS b ON ("
	 "Upper(a.f_table_name) = Upper(b.f_table_name) AND "
	 "Upper(a.f_geometry_column) = Upper(b.f_geometry_column)) "
	 "WHERE Upper(a.view_name) = Upper(%Q) "
	 "AND Upper(a.view_geometry) = Upper(%Q) AND b.spatial_index_enabled = 1",
	 table_name, geom_column);
    ret =
	sqlite3_prepare_v2 (sqlite, sql_statement, strlen (sql_statement),
			    &stmt, NULL);
    sqlite3_free (sql_statement);
    if (ret != SQLITE_OK)
	return 0;
    while (1)
      {
	  /* scrolling the result set rows */
	  ret = sqlite3_step (stmt);
	  if (ret == SQLITE_DONE)
	      break;		/* end of result set */
	  if (ret == SQLITE_ROW)
	    {
		const char *v = (const char *) sqlite3_column_text (stmt, 0);
		int len = sqlite3_column_bytes (stmt, 0);
		if (rt)
		    free (rt);
		rt = malloc (len + 1);
		strcpy (rt, v);
		v = (const char *) sqlite3_column_text (stmt, 1);
		len = sqlite3_column_bytes (stmt, 1);
		if (rg)
		    free (rg);
		rg = malloc (len + 1);
		strcpy (rg, v);
		is_longlat = sqlite3_column_int (stmt, 2);
		count++;
	    }
      }
    sqlite3_finalize (stmt);
    if (count != 1)
	return 0;
    if (!validateRowid (sqlite, rt))
      {
	  free (rt);
	  free (rg);
	  return 0;
      }
    *real_table = rt;
    *real_geom = rg;
    *is_geographic = is_longlat;
    return 1;
}
Exemple #9
0
static int
vknn_find_view_rtree (sqlite3 * sqlite, const char *db_prefix,
		      const char *table_name, char **real_table,
		      char **real_geom, int *is_geographic)
{
/* attempts to find the corresponding RTree Geometry Column - SpatialView */
    sqlite3_stmt *stmt;
    char *sql_statement;
    int ret;
    int count = 0;
    char *rt = NULL;
    char *rg = NULL;
    int is_longlat = 0;

/* testing if views_geometry_columns exists */
    if (db_prefix == NULL)
      {
	  sql_statement = sqlite3_mprintf ("SELECT tbl_name FROM sqlite_master "
					   "WHERE type = 'table' AND tbl_name = 'views_geometry_columns'");
      }
    else
      {
	  char *quoted_db = gaiaDoubleQuotedSql (db_prefix);
	  sql_statement =
	      sqlite3_mprintf ("SELECT tbl_name FROM \"%s\".sqlite_master "
			       "WHERE type = 'table' AND tbl_name = 'views_geometry_columns'",
			       quoted_db);
	  free (quoted_db);
      }
    ret =
	sqlite3_prepare_v2 (sqlite, sql_statement, strlen (sql_statement),
			    &stmt, NULL);
    sqlite3_free (sql_statement);
    if (ret != SQLITE_OK)
	return 0;
    while (1)
      {
	  /* scrolling the result set rows */
	  ret = sqlite3_step (stmt);
	  if (ret == SQLITE_DONE)
	      break;		/* end of result set */
	  if (ret == SQLITE_ROW)
	      count++;
      }
    sqlite3_finalize (stmt);
    if (count != 1)
	return 0;
    count = 0;

/* attempting to find the RTree Geometry Column */
    if (db_prefix == NULL)
      {
	  sql_statement =
	      sqlite3_mprintf
	      ("SELECT a.f_table_name, a.f_geometry_column, SridIsGeographic(b.srid) "
	       "FROM views_geometry_columns AS a "
	       "JOIN geometry_columns AS b ON ("
	       "Upper(a.f_table_name) = Upper(b.f_table_name) AND "
	       "Upper(a.f_geometry_column) = Upper(b.f_geometry_column)) "
	       "WHERE Upper(a.view_name) = Upper(%Q) AND b.spatial_index_enabled = 1",
	       table_name);
      }
    else
      {
	  char *quoted_db = gaiaDoubleQuotedSql (db_prefix);
	  sql_statement =
	      sqlite3_mprintf
	      ("SELECT a.f_table_name, a.f_geometry_column, SridIsGeographic(b.srid) "
	       "FROM \"%s\".views_geometry_columns AS a "
	       "JOIN \"%s\".geometry_columns AS b ON ("
	       "Upper(a.f_table_name) = Upper(b.f_table_name) AND "
	       "Upper(a.f_geometry_column) = Upper(b.f_geometry_column)) "
	       "WHERE Upper(a.view_name) = Upper(%Q) AND b.spatial_index_enabled = 1",
	       quoted_db, quoted_db, table_name);
	  free (quoted_db);
      }
    ret =
	sqlite3_prepare_v2 (sqlite, sql_statement, strlen (sql_statement),
			    &stmt, NULL);
    sqlite3_free (sql_statement);
    if (ret != SQLITE_OK)
	return 0;
    while (1)
      {
	  /* scrolling the result set rows */
	  ret = sqlite3_step (stmt);
	  if (ret == SQLITE_DONE)
	      break;		/* end of result set */
	  if (ret == SQLITE_ROW)
	    {
		const char *v = (const char *) sqlite3_column_text (stmt, 0);
		int len = sqlite3_column_bytes (stmt, 0);
		if (rt)
		    free (rt);
		rt = malloc (len + 1);
		strcpy (rt, v);
		v = (const char *) sqlite3_column_text (stmt, 1);
		len = sqlite3_column_bytes (stmt, 1);
		if (rg)
		    free (rg);
		rg = malloc (len + 1);
		strcpy (rg, v);
		is_longlat = sqlite3_column_int (stmt, 2);
		count++;
	    }
      }
    sqlite3_finalize (stmt);
    if (count != 1)
	return 0;
    *real_table = rt;
    *real_geom = rg;
    *is_geographic = is_longlat;
    return 1;
}
Exemple #10
0
/* Gets a reply from the cache */
static int
cache_daap_query_get(struct cache_command *cmd)
{
#define Q_TMPL "SELECT reply FROM replies WHERE query = ?;"
  sqlite3_stmt *stmt;
  char *query;
  int datalen;
  int ret;

  query = cmd->arg.query;
  remove_tag(query, "session-id");
  remove_tag(query, "revision-number");

  // Look in the DB
  ret = sqlite3_prepare_v2(g_db_hdl, Q_TMPL, -1, &stmt, 0);
  if (ret != SQLITE_OK)
    {
      DPRINTF(E_LOG, L_CACHE, "Error preparing query for cache update: %s\n", sqlite3_errmsg(g_db_hdl));
      free(query);
      return -1;
    }

  sqlite3_bind_text(stmt, 1, query, -1, SQLITE_STATIC);

  ret = sqlite3_step(stmt);
  if (ret != SQLITE_ROW)  
    {
      if (ret != SQLITE_DONE)
	DPRINTF(E_LOG, L_CACHE, "Error stepping query for cache update: %s\n", sqlite3_errmsg(g_db_hdl));
      goto error_get;
    }

  datalen = sqlite3_column_bytes(stmt, 0);

  if (!cmd->arg.evbuf)
    {
      DPRINTF(E_LOG, L_CACHE, "Error: DAAP reply evbuffer is NULL\n");
      goto error_get;
    }

  ret = evbuffer_add(cmd->arg.evbuf, sqlite3_column_blob(stmt, 0), datalen);
  if (ret < 0)
    {
      DPRINTF(E_LOG, L_CACHE, "Out of memory for DAAP reply evbuffer\n");
      goto error_get;
    }

  ret = sqlite3_finalize(stmt);
  if (ret != SQLITE_OK)
    DPRINTF(E_LOG, L_CACHE, "Error finalizing query for getting cache: %s\n", sqlite3_errmsg(g_db_hdl));

  DPRINTF(E_INFO, L_CACHE, "Cache hit: %s\n", query);

  free(query);

  return 0;

 error_get:
  sqlite3_finalize(stmt);
  free(query);
  return -1;  
#undef Q_TMPL
}
Exemple #11
0
static int
vknn_filter (sqlite3_vtab_cursor * pCursor, int idxNum, const char *idxStr,
	     int argc, sqlite3_value ** argv)
{
/* setting up a cursor filter */
    char *db_prefix = NULL;
    char *table_name = NULL;
    char *geom_column = NULL;
    char *xtable = NULL;
    char *xgeom = NULL;
    char *xgeomQ;
    char *xtableQ;
    char *idx_name;
    char *idx_nameQ;
    char *sql_statement;
    gaiaGeomCollPtr geom = NULL;
    int ok_table = 0;
    int ok_geom = 0;
    int ok_max = 0;
    int max_items = 3;
    int is_geographic;
    const unsigned char *blob;
    int size;
    int exists;
    int ret;
    sqlite3_stmt *stmt = NULL;
    sqlite3_stmt *stmt_dist = NULL;
    sqlite3_stmt *stmt_rect = NULL;
    VirtualKnnCursorPtr cursor = (VirtualKnnCursorPtr) pCursor;
    VirtualKnnPtr knn = (VirtualKnnPtr) cursor->pVtab;
    VKnnContextPtr vknn_context = knn->knn_ctx;
    if (idxStr)
	idxStr = idxStr;	/* unused arg warning suppression */
    cursor->eof = 1;
    if (idxStr)
	idxStr = idxStr;	/* unused arg warning suppression */
    cursor->eof = 1;
    if (idxNum == 1 && argc == 3)
      {
	  /* retrieving the Table/Column/Geometry params */
	  if (sqlite3_value_type (argv[0]) == SQLITE_TEXT)
	    {
		char *tn = (char *) sqlite3_value_text (argv[0]);
		vknn_parse_table_name (tn, &db_prefix, &table_name);
		ok_table = 1;
	    }
	  if (sqlite3_value_type (argv[1]) == SQLITE_TEXT)
	    {
		geom_column = (char *) sqlite3_value_text (argv[1]);
		ok_geom = 1;
	    }
	  if (sqlite3_value_type (argv[2]) == SQLITE_BLOB)
	    {
		blob = sqlite3_value_blob (argv[2]);
		size = sqlite3_value_bytes (argv[2]);
		geom = gaiaFromSpatiaLiteBlobWkb (blob, size);
	    }
	  if (ok_table && ok_geom && geom)
	      ;
	  else
	    {
		/* invalid args */
		goto stop;
	    }
      }
    if (idxNum == 2 && argc == 2)
      {
	  /* retrieving the Table/Geometry params */
	  if (sqlite3_value_type (argv[0]) == SQLITE_TEXT)
	    {
		char *tn = (char *) sqlite3_value_text (argv[0]);
		vknn_parse_table_name (tn, &db_prefix, &table_name);
		ok_table = 1;
	    }
	  if (sqlite3_value_type (argv[1]) == SQLITE_BLOB)
	    {
		blob = sqlite3_value_blob (argv[1]);
		size = sqlite3_value_bytes (argv[1]);
		geom = gaiaFromSpatiaLiteBlobWkb (blob, size);
	    }
	  if (ok_table && geom)
	      ;
	  else
	    {
		/* invalid args */
		goto stop;
	    }
      }
    if (idxNum == 3 && argc == 4)
      {
	  /* retrieving the Table/Column/Geometry/MaxItems params */
	  if (sqlite3_value_type (argv[0]) == SQLITE_TEXT)
	    {
		char *tn = (char *) sqlite3_value_text (argv[0]);
		vknn_parse_table_name (tn, &db_prefix, &table_name);
		ok_table = 1;
	    }
	  if (sqlite3_value_type (argv[1]) == SQLITE_TEXT)
	    {
		geom_column = (char *) sqlite3_value_text (argv[1]);
		ok_geom = 1;
	    }
	  if (sqlite3_value_type (argv[2]) == SQLITE_BLOB)
	    {
		blob = sqlite3_value_blob (argv[2]);
		size = sqlite3_value_bytes (argv[2]);
		geom = gaiaFromSpatiaLiteBlobWkb (blob, size);
	    }
	  if (sqlite3_value_type (argv[3]) == SQLITE_INTEGER)
	    {
		max_items = sqlite3_value_int (argv[3]);
		if (max_items > 1024)
		    max_items = 1024;
		if (max_items < 1)
		    max_items = 1;
		ok_max = 1;
	    }
	  if (ok_table && ok_geom && geom && ok_max)
	      ;
	  else
	    {
		/* invalid args */
		goto stop;
	    }
      }
    if (idxNum == 4 && argc == 3)
      {
	  /* retrieving the Table/Geometry/MaxItems params */
	  if (sqlite3_value_type (argv[0]) == SQLITE_TEXT)
	    {
		char *tn = (char *) sqlite3_value_text (argv[0]);
		vknn_parse_table_name (tn, &db_prefix, &table_name);
		ok_table = 1;
	    }
	  if (sqlite3_value_type (argv[1]) == SQLITE_BLOB)
	    {
		blob = sqlite3_value_blob (argv[1]);
		size = sqlite3_value_bytes (argv[1]);
		geom = gaiaFromSpatiaLiteBlobWkb (blob, size);
	    }
	  if (sqlite3_value_type (argv[2]) == SQLITE_INTEGER)
	    {
		max_items = sqlite3_value_int (argv[2]);
		if (max_items > 1024)
		    max_items = 1024;
		if (max_items < 1)
		    max_items = 1;
		ok_max = 1;
	    }
	  if (ok_table && geom && ok_max)
	      ;
	  else
	    {
		/* invalid args */
		goto stop;
	    }
      }

/* checking if the corresponding R*Tree exists */
    if (ok_geom)
	exists =
	    vknn_check_rtree (knn->db, db_prefix, table_name, geom_column,
			      &xtable, &xgeom, &is_geographic);
    else
	exists =
	    vknn_find_rtree (knn->db, db_prefix, table_name, &xtable,
			     &xgeom, &is_geographic);
    if (!exists)
	goto stop;

/* building the Distance query */
    xgeomQ = gaiaDoubleQuotedSql (xgeom);
    xtableQ = gaiaDoubleQuotedSql (xtable);
    if (is_geographic)
	sql_statement =
	    sqlite3_mprintf
	    ("SELECT ST_Distance(?, \"%s\", 1) FROM \"%s\" WHERE rowid = ?",
	     xgeomQ, xtableQ);
    else
	sql_statement =
	    sqlite3_mprintf
	    ("SELECT ST_Distance(?, \"%s\") FROM \"%s\" WHERE rowid = ?",
	     xgeomQ, xtableQ);
    free (xgeomQ);
    free (xtableQ);
    ret =
	sqlite3_prepare_v2 (knn->db, sql_statement, strlen (sql_statement),
			    &stmt_dist, NULL);
    sqlite3_free (sql_statement);
    if (ret != SQLITE_OK)
	goto stop;

/* building the RTree MBR Distance query */
    sql_statement = "SELECT ST_Distance(?, BuildMbr(?, ?, ?, ?))";
    ret =
	sqlite3_prepare_v2 (knn->db, sql_statement, strlen (sql_statement),
			    &stmt_rect, NULL);
    if (ret != SQLITE_OK)
	goto stop;

/* installing the R*Tree query callback */
    gaiaMbrGeometry (geom);
    vknn_init_context (vknn_context, xtable, xgeom, geom, max_items, stmt_dist,
		       stmt_rect);
    gaiaFreeGeomColl (geom);
    geom = NULL;		/* releasing ownership on geom */
    stmt_dist = NULL;		/* releasing ownership on stmt_dist */
    stmt_rect = NULL;		/* releasing ownership on stmt_rect */
    sqlite3_rtree_query_callback (knn->db, "knn_position", vknn_query_callback,
				  vknn_context, NULL);

/* building the RTree query */
    idx_name = sqlite3_mprintf ("idx_%s_%s", xtable, xgeom);
    idx_nameQ = gaiaDoubleQuotedSql (idx_name);
    if (db_prefix == NULL)
      {
	  sql_statement =
	      sqlite3_mprintf
	      ("SELECT pkid FROM \"%s\" WHERE pkid MATCH knn_position(1)",
	       idx_nameQ);
      }
    else
      {
	  char *quoted_db = gaiaDoubleQuotedSql (db_prefix);
	  sql_statement =
	      sqlite3_mprintf
	      ("SELECT pkid FROM \"%s\".\"%s\" WHERE pkid MATCH knn_position(1)",
	       quoted_db, idx_nameQ);
	  free (quoted_db);
      }
    free (idx_nameQ);
    sqlite3_free (idx_name);
    ret =
	sqlite3_prepare_v2 (knn->db, sql_statement, strlen (sql_statement),
			    &stmt, NULL);
    sqlite3_free (sql_statement);
    if (ret != SQLITE_OK)
	goto stop;

    vknn_context->curr_level = -1;
    while (vknn_context->curr_level != 0)
      {
	  /* querying the R*Tree: step #1 tree MBRs */
	  sqlite3_step (stmt);
	  if (vknn_context->curr_level == -1)
	      break;		/* found an empty R*Tree, preasumably */
	  vknn_context->curr_level -= 1;
      }
    if (vknn_context->curr_level == 0)
      {
	  /* querying the R*Tree: step #2 features */
	  sqlite3_step (stmt);
      }

    if (vknn_context->curr_items == 0)
	cursor->eof = 1;
    else
	cursor->eof = 0;
    cursor->CurrentIndex = 0;
  stop:
    if (geom)
	gaiaFreeGeomColl (geom);
    if (xtable)
	free (xtable);
    if (xgeom)
	free (xgeom);
    if (db_prefix)
	free (db_prefix);
    if (table_name)
	free (table_name);
    if (stmt != NULL)
	sqlite3_finalize (stmt);
    if (stmt_dist != NULL)
	sqlite3_finalize (stmt_dist);
    if (stmt_rect != NULL)
	sqlite3_finalize (stmt_rect);
    return SQLITE_OK;
}
Exemple #12
0
/*
 * Get the cached artwork image for the given persistentid and maximum width/height
 *
 * If there is a cached entry for the given id and width/height, the parameter cached is set to 1.
 * In this case format and data contain the cached values.
 *
 * @param cmd->arg.persistentid persistent songalbumid or songartistid
 * @param cmd->arg.max_w maximum image width
 * @param cmd->arg.max_h maximum image height
 * @param cmd->arg.cached set by this function to 0 if no cache entry exists, otherwise 1
 * @param cmd->arg.format set by this function to the format of the cache entry
 * @param cmd->arg.evbuf event buffer filled by this function with the scaled image
 * @return 0 if successful, -1 if an error occurred
 */
static int
cache_artwork_get_impl(struct cache_command *cmd)
{
#define Q_TMPL "SELECT a.format, a.data FROM artwork a WHERE a.persistentid = '%" PRIi64 "' AND a.max_w = %d AND a.max_h = %d;"
  sqlite3_stmt *stmt;
  char *query;
  int datalen;
  int ret;

  query = sqlite3_mprintf(Q_TMPL, cmd->arg.peristentid, cmd->arg.max_w, cmd->arg.max_h);
  if (!query)
    {
      DPRINTF(E_LOG, L_CACHE, "Out of memory for query string\n");
      return -1;
    }

  DPRINTF(E_DBG, L_CACHE, "Running query '%s'\n", query);
  ret = sqlite3_prepare_v2(g_db_hdl, query, -1, &stmt, 0);
  if (ret != SQLITE_OK)
    {
      DPRINTF(E_LOG, L_CACHE, "Could not prepare statement: %s\n", sqlite3_errmsg(g_db_hdl));
      ret = -1;
      goto error_get;
    }

  ret = sqlite3_step(stmt);
  if (ret != SQLITE_ROW)
    {
      cmd->arg.cached = 0;

      if (ret == SQLITE_DONE)
	{
	  ret = 0;
	  DPRINTF(E_DBG, L_CACHE, "No results\n");
	}
      else
	{
	  ret = -1;
	  DPRINTF(E_LOG, L_CACHE, "Could not step: %s\n", sqlite3_errmsg(g_db_hdl));
	}

      goto error_get;
    }

  cmd->arg.format = sqlite3_column_int(stmt, 0);
  datalen = sqlite3_column_bytes(stmt, 1);
  if (!cmd->arg.evbuf)
    {
      DPRINTF(E_LOG, L_CACHE, "Error: Artwork evbuffer is NULL\n");
      goto error_get;
    }

  ret = evbuffer_add(cmd->arg.evbuf, sqlite3_column_blob(stmt, 1), datalen);
  if (ret < 0)
    {
      DPRINTF(E_LOG, L_CACHE, "Out of memory for artwork evbuffer\n");
      goto error_get;
    }

  cmd->arg.cached = 1;

  ret = sqlite3_finalize(stmt);
  if (ret != SQLITE_OK)
    DPRINTF(E_LOG, L_CACHE, "Error finalizing query for getting cache: %s\n", sqlite3_errmsg(g_db_hdl));

  DPRINTF(E_DBG, L_CACHE, "Cache hit: %s\n", query);

  return 0;

 error_get:
  sqlite3_finalize(stmt);
  return -1;
#undef Q_TMPL
}
Exemple #13
0
/* update all values to reflect mouse over image id or no data at all */
static void _metadata_view_update_values(dt_lib_module_t *self)
{
  dt_lib_metadata_view_t *d = (dt_lib_metadata_view_t *)self->data;
  int32_t mouse_over_id = -1;
  DT_CTL_GET_GLOBAL(mouse_over_id, lib_image_mouse_over_id);

  if (mouse_over_id == -1)
  {
    const dt_view_t *cv = dt_view_manager_get_current_view(darktable.view_manager);
    if(cv->view((dt_view_t*)cv) == DT_VIEW_DARKROOM)
    {
      mouse_over_id = darktable.develop->image_storage.id;
    }
    else
    {
      sqlite3_stmt *stmt;
      DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), "select imgid from selected_images limit 1", -1, &stmt, NULL);
      if(sqlite3_step(stmt) == SQLITE_ROW)
        mouse_over_id = sqlite3_column_int(stmt, 0);
      sqlite3_finalize(stmt);
    }
  }

  if(mouse_over_id >= 0)
  {
    const int vl = 512;
    char value[vl];
    const dt_image_t *img = dt_image_cache_read_get(darktable.image_cache, mouse_over_id);
    if(!img) goto fill_minuses;
    if(img->film_id == -1)
    {
      dt_image_cache_read_release(darktable.image_cache, img);
      goto fill_minuses;
    }

    /* update all metadata */

    dt_image_film_roll(img, value, vl);
    _metadata_update_value(d->metadata[md_internal_filmroll], value);
    const int tp = 512;
    char tooltip[tp];
    snprintf(tooltip, tp, _("double click to jump to film roll\n%s"), value);
    g_object_set(G_OBJECT(d->metadata[md_internal_filmroll]), "tooltip-text", tooltip, (char *)NULL);

    snprintf(value,vl,"%d", img->id);
    _metadata_update_value(d->metadata[md_internal_imgid], value);

    _metadata_update_value(d->metadata[md_internal_filename], img->filename);

    dt_image_full_path(img->id, value, MAXPATHLEN);
    _metadata_update_value(d->metadata[md_internal_fullpath], value);

    /* EXIF */
    _metadata_update_value_end(d->metadata[md_exif_model], img->exif_model);
    _metadata_update_value_end(d->metadata[md_exif_lens], img->exif_lens);
    _metadata_update_value_end(d->metadata[md_exif_maker], img->exif_maker);

    snprintf(value, vl, "F/%.1f", img->exif_aperture);
    _metadata_update_value(d->metadata[md_exif_aperture], value);

    if(img->exif_exposure <= 0.5) snprintf(value, vl, "1/%.0f", 1.0/img->exif_exposure);
    else                          snprintf(value, vl, "%.1f''", img->exif_exposure);
    _metadata_update_value(d->metadata[md_exif_exposure], value);

    snprintf(value, vl, "%.0f", img->exif_focal_length);
    _metadata_update_value(d->metadata[md_exif_focal_length], value);

    snprintf(value, vl, "%.2f m", img->exif_focus_distance);
    _metadata_update_value(d->metadata[md_exif_focus_distance], value);

    snprintf(value, vl, "%.0f", img->exif_iso);
    _metadata_update_value(d->metadata[md_exif_iso], value);

    _metadata_update_value(d->metadata[md_exif_datetime], img->exif_datetime_taken);

    snprintf(value, vl, "%d", img->height);
    _metadata_update_value(d->metadata[md_exif_height], value);
    snprintf(value, vl, "%d", img->width);
    _metadata_update_value(d->metadata[md_exif_width], value);

    /* XMP */
    GList *res;
    if((res = dt_metadata_get(img->id, "Xmp.dc.title", NULL))!=NULL)
    {
      snprintf(value, vl, "%s", (char*)res->data);
      _filter_non_printable(value, vl);
      g_free(res->data);
      g_list_free(res);
    }
    else
      snprintf(value, vl, NODATA_STRING);
    _metadata_update_value(d->metadata[md_xmp_title], value);

    if((res = dt_metadata_get(img->id, "Xmp.dc.creator", NULL))!=NULL)
    {
      snprintf(value, vl, "%s", (char*)res->data);
      _filter_non_printable(value, vl);
      g_free(res->data);
      g_list_free(res);
    }
    else
      snprintf(value, vl, NODATA_STRING);
    _metadata_update_value(d->metadata[md_xmp_creator], value);

    if((res = dt_metadata_get(img->id, "Xmp.dc.rights", NULL))!=NULL)
    {
      snprintf(value, vl, "%s", (char*)res->data);
      _filter_non_printable(value, vl);
      g_free(res->data);
      g_list_free(res);
    }
    else
      snprintf(value, vl, NODATA_STRING);
    _metadata_update_value(d->metadata[md_xmp_rights], value);

    /* geotagging */
    /* latitude */
    if(isnan(img->latitude))
    {
      _metadata_update_value(d->metadata[md_geotagging_lat], NODATA_STRING);
    }
    else
    {
#ifdef HAVE_MAP
      if(dt_conf_get_bool("plugins/lighttable/metadata_view/pretty_location"))
      {
        gchar *latitude = osd_latitude_str(img->latitude);
        _metadata_update_value(d->metadata[md_geotagging_lat], latitude);
        g_free(latitude);
      }
      else
      {
#endif
        gchar NS = img->latitude<0?'S':'N';
        snprintf(value, vl, "%c %09.6f", NS, fabs(img->latitude));
        _metadata_update_value(d->metadata[md_geotagging_lat], value);
#ifdef HAVE_MAP
      }
#endif
    }
    /* longitude */
    if(isnan(img->longitude))
    {
      _metadata_update_value(d->metadata[md_geotagging_lon], NODATA_STRING);
    }
    else
    {
#ifdef HAVE_MAP
      if(dt_conf_get_bool("plugins/lighttable/metadata_view/pretty_location"))
      {
        gchar *longitude = osd_longitude_str(img->longitude);
        _metadata_update_value(d->metadata[md_geotagging_lon], longitude);
        g_free(longitude);
      }
      else
      {
#endif
        gchar EW = img->longitude<0?'W':'E';
        snprintf(value, vl, "%c %010.6f", EW, fabs(img->longitude));
        _metadata_update_value(d->metadata[md_geotagging_lon], value);
#ifdef HAVE_MAP
      }
#endif
    }

    /* release img */
    dt_image_cache_read_release(darktable.image_cache, img);

  }

  return;

  /* reset */
fill_minuses:
  for(int k=0; k<md_size; k++)
    _metadata_update_value(d->metadata[k],NODATA_STRING);

}
Exemple #14
0
static void _lib_geotagging_calculate_offset_callback(GtkWidget *widget, dt_lib_module_t *self)
{
  dt_lib_geotagging_t *d = (dt_lib_geotagging_t *)self->data;
  const gchar *gps_time = gtk_entry_get_text(GTK_ENTRY(d->floating_window_entry));
  if(gps_time)
  {
    gchar **tokens = g_strsplit(gps_time, ":", 0);
    if(tokens[0] != '\0' && tokens[1] != '\0' && tokens[2] != '\0')
    {
      if(g_ascii_isdigit(tokens[0][0]) && g_ascii_isdigit(tokens[0][1]) && tokens[0][2] == '\0'
         && g_ascii_isdigit(tokens[1][0]) && g_ascii_isdigit(tokens[1][1]) && tokens[1][2] == '\0'
         && g_ascii_isdigit(tokens[2][0]) && g_ascii_isdigit(tokens[2][1]) && tokens[2][2] == '\0')
      {
        int h, m, s;
        h = (tokens[0][0] - '0') * 10 + tokens[0][1] - '0';
        m = (tokens[1][0] - '0') * 10 + tokens[1][1] - '0';
        s = (tokens[2][0] - '0') * 10 + tokens[2][1] - '0';
        if(h < 24 && m < 60 && s < 60)
        {
          // finally a valid time
          // get imgid
          int32_t imgid = -1;
          sqlite3_stmt *stmt;
          DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),
                                      "select imgid from selected_images order by imgid asc limit 1", -1,
                                      &stmt, NULL);
          if(sqlite3_step(stmt) == SQLITE_ROW)
            imgid = sqlite3_column_int(stmt, 0);
          else // no selection is used, use mouse over id
            imgid = dt_control_get_mouse_over_id();
          sqlite3_finalize(stmt);

          if(imgid > 0)
          {
            const dt_image_t *cimg = dt_image_cache_get(darktable.image_cache, imgid, 'r');
            // get the exif_datetime_taken and parse it
            gint year;
            gint month;
            gint day;
            gint hour;
            gint minute;
            gint second;

            if(sscanf(cimg->exif_datetime_taken, "%d:%d:%d %d:%d:%d", (int *)&year, (int *)&month,
                      (int *)&day, (int *)&hour, (int *)&minute, (int *)&second) == 6)
            {
              // calculate the offset
              long int exif_seconds = hour * 60 * 60 + minute * 60 + second;
              long int gps_seconds = h * 60 * 60 + m * 60 + s;
              long int offset = gps_seconds - exif_seconds;
              // transform the offset back into a string
              gchar sign = (offset < 0) ? '-' : '+';
              offset = labs(offset);
              gint offset_h = offset / (60 * 60);
              offset -= offset_h * 60 * 60;
              gint offset_m = offset / 60;
              offset -= offset_m * 60;
              gchar *offset_str = g_strdup_printf("%c%02d:%02d:%02ld", sign, offset_h, offset_m, offset);
              // write the offset into d->offset_entry
              gtk_entry_set_text(GTK_ENTRY(d->offset_entry), offset_str);
              g_free(offset_str);
            }

            dt_image_cache_read_release(darktable.image_cache, cimg);
          }
        }
      }
    }
    g_strfreev(tokens);
  }
  gtk_widget_destroy(d->floating_window);
}
Exemple #15
0
int main(int argc,char* argv[]){
  sqlite3 *db;
  char *zErrMsg=0;
  char file[100];
  char *sql;
  int i;
  sqlite3_stmt *prepared_insert,*prepared_delete,*prepared_replace,*prepared_source_redirect;
  listNode* root=calloc(1,sizeof(listNode));
  strcat(file,getenv("HOME"));
  strncat(file,"/freshen.db",13);
  int rc;
  rc=sqlite3_open(file,&db);
  if(rc){
    fprintf(stderr,"Can't open database:%s\n",sqlite3_errmsg(db));
    exit(0);
  }else{
    fprintf(stderr,"database opened successfully\n");
  }
  sql=CREATE_TABLE;
  rc=sqlite3_exec(db,sql,callback,0,&zErrMsg);
  if(rc!=SQLITE_OK){
    fprintf(stderr,"SQL Error:%s\n",zErrMsg);
    sqlite3_free(zErrMsg);
  }else{
    fprintf(stderr,"Table created sucessfully\n");
  }
  rc=sqlite3_prepare_v2(db,"insert into FILES (DESTINATION,SOURCE,DANGEROUS) values (?,?,?);",-1,&prepared_insert,NULL);
  if(rc!=SQLITE_OK){
    //write this sometime
  }
  rc=sqlite3_prepare_v2(db,"delete from FILES where DESTINATION = ?",-1,&prepared_delete,NULL);
  if(rc!=SQLITE_OK){
    //do something here sometime.
  }
  rc=sqlite3_prepare_v2(db,"update FILES set SOURCE = ? where DESTINATION = ?",-1,&prepared_replace,NULL);
  if(rc!=SQLITE_OK){
    //Needs to be written
  }
  rc=sqlite3_prepare_v2(db,"UPDATE FILES SET SOURCE = ? WHERE SOURCE = ?",-1,&prepared_source_redirect,NULL);
  if(rc!=SQLITE_OK){
    //This sort of thing might not need to actually be written, after all, they
    //should compile to the same thing each time.
    fprintf(stderr,"Something broke!\n");
  }
  for(i=1;i<argc;i++){
    if(strcmp(argv[i],"-insert")==0){
      char *dest=argv[i+1];
      char *src=argv[i+2];
      char destpath[PATH_MAX+1],
        srcpath[PATH_MAX+1];
      if(!isArgFile(dest)){
        fprintf(stderr,"-insert requires two arguments <destination file> <source file> [-dangerous]?\n%s is not visible to this as a file\n",dest);
        exit(1);
      }
      if(!isArgFile(src)){
        fprintf(stderr,"-insert requires two arguments <destination file> <source file> [-dangerous]?\n%s is not visible to this as a file\n",src);
        exit(1);
      }
      if(!fileExists(src))
        {
          fprintf(stderr,"%s does not exist, source files must exist\n",src);
          exit(1);
        }
      realpath(dest,destpath);
      realpath(src,srcpath);
      i+=2;
      short dangerous=0;
      if(argc>=i+2){
        if(strcmp(argv[i+1],"-dangerous")==0)
          {
            dangerous=1;
            i++;
          }
      }
      if(sqlite3_bind_text(prepared_insert,1,destpath,-1,SQLITE_STATIC)!=SQLITE_OK)
        fprintf(stderr,"Failed to bind destination\n");

      if(sqlite3_bind_text(prepared_insert,2,srcpath,-1,SQLITE_STATIC)!=SQLITE_OK)
        fprintf(stderr,"Failed to bind source\n");
      if(sqlite3_bind_int(prepared_insert,3,dangerous)!=SQLITE_OK)
        fprintf(stderr,"Failed to bind dangerous\n");
      rc=sqlite3_step(prepared_insert);
      if(rc!=SQLITE_DONE){
        fprintf(stderr,"Didn't run right: %s\n",sqlite3_errstr(rc));
      }
      sqlite3_reset(prepared_insert);//Reset prepared statement

    }else if(strcmp(argv[i],"-freshen")==0){
      sqlite3_exec(db,"select * from FILES;",freshen,(void*)root,&zErrMsg);
      listNode* r=root;
      struct stat srcbuf,dstbuf;
      short destination_exists=1,skip_danger=0;
      char can_replace=1;
      struct utimbuf replacement_time;
      if(argc>i+1&&strcmp(argv[i+1],"-safe-only")){
        skip_danger=1;
        i++;
      }
      while(r){
        rc=stat(r->destination,&dstbuf);
        if(rc==-1){
          if(errno!=ENOENT){
            fprintf(stderr,"It seems that the destination is inaccessible for some reason\n");
            exit(1);
          }else
            destination_exists=0;
        }else
          destination_exists=1;
        rc=stat(r->source,&srcbuf);
        if(rc==-1){
            fprintf(stderr,"It seems that the source file is inaccessible for some reason\n");
            exit(1);
        }
        if(r->dangerous)
          {
            if(skip_danger)
              {
                r=r->next;
                continue;
              }
            printf("%s is marked as being dangerous, is %s ready to be used?\n",r->destination,r->source);
            scanf("%c",&can_replace);
          }else
          can_replace=1;
        if((can_replace!=0&&can_replace!='n')||!destination_exists){
          printf(srcbuf.st_mtime>dstbuf.st_mtime?
                 "Replacing %s with %s\n":"%s up to date with %s\n",r->destination,r->source);
          cp(r->destination,r->source);
          replacement_time.modtime=srcbuf.st_mtim.tv_sec;
          replacement_time.actime=srcbuf.st_atim.tv_sec;
          utime(r->destination,&replacement_time);//replace with the source file's time
        }
        r=r->next;
      }
      freeList(root);
      root=calloc(1,sizeof(listNode));
    }else if (strcmp(argv[i],"-remove")==0){//Remove destination file from database, doesn't really matter if it succeeds.
      char rpath[PATH_MAX+1];
      realpath(argv[i+1],rpath);
      i++;
      sqlite3_bind_text(prepared_delete,1,rpath,-1,SQLITE_STATIC);
      sqlite3_step(prepared_delete);
      sqlite3_reset(prepared_delete);
    }else if(strcmp(argv[i],"-replace")==0){
      char srcpath[PATH_MAX+1],dstpath[PATH_MAX+1];
      realpath(argv[i+1],dstpath);
      realpath(argv[i+2],srcpath);
      sqlite3_bind_text(prepared_replace,1,srcpath,-1,SQLITE_STATIC);
      sqlite3_bind_text(prepared_replace,2,dstpath,-1,SQLITE_STATIC);
      sqlite3_step(prepared_replace);
      sqlite3_reset(prepared_replace);
    }else if(strcmp(argv[i],"-redirect")==0){
      char srcpath[PATH_MAX+1],newpath[PATH_MAX+1];
      if(argc<=i+2){
        fprintf(stderr,"The proper invocation of -redirect requires two arguments <original source> <new source>\n");
        exit(1);
      }
      realpath(argv[i+1],srcpath);//Get the full path of the file.
      realpath(argv[i+2],newpath);
      printf("%s\n",srcpath);
      sqlite3_bind_text(prepared_source_redirect,1,newpath,-1,SQLITE_STATIC);
      sqlite3_bind_text(prepared_source_redirect,2,srcpath,-1,SQLITE_STATIC);
      rc=sqlite3_step(prepared_source_redirect);
      if(rc!=SQLITE_DONE){
        fprintf(stderr,"%s\n",sqlite3_errmsg(db));
      }
      sqlite3_reset(prepared_source_redirect);
      i+=2;
    }
  }
  sqlite3_close(db);
}
Exemple #16
0
static int
vknn_find_rtree (sqlite3 * sqlite, const char *db_prefix,
		 const char *table_name, char **real_table, char **real_geom,
		 int *is_geographic)
{
/* attempts to find the corresponding RTree Geometry Column */
    sqlite3_stmt *stmt;
    char *sql_statement;
    int ret;
    int count = 0;
    char *rt = NULL;
    char *rg = NULL;
    int is_longlat = 0;

    if (db_prefix == NULL)
      {
	  sql_statement =
	      sqlite3_mprintf
	      ("SELECT f_table_name, f_geometry_column, SridIsGeographic(srid) "
	       " FROM geometry_columns WHERE Upper(f_table_name) = Upper(%Q) "
	       "AND spatial_index_enabled = 1", table_name);
      }
    else
      {
	  char *quoted_db = gaiaDoubleQuotedSql (db_prefix);
	  sql_statement =
	      sqlite3_mprintf
	      ("SELECT f_table_name, f_geometry_column, SridIsGeographic(srid) "
	       " FROM \"%s\".geometry_columns WHERE Upper(f_table_name) = Upper(%Q) "
	       "AND spatial_index_enabled = 1", quoted_db, table_name);
	  free (quoted_db);
      }
    ret =
	sqlite3_prepare_v2 (sqlite, sql_statement, strlen (sql_statement),
			    &stmt, NULL);
    sqlite3_free (sql_statement);
    if (ret != SQLITE_OK)
	return 0;
    while (1)
      {
	  /* scrolling the result set rows */
	  ret = sqlite3_step (stmt);
	  if (ret == SQLITE_DONE)
	      break;		/* end of result set */
	  if (ret == SQLITE_ROW)
	    {
		const char *v = (const char *) sqlite3_column_text (stmt, 0);
		int len = sqlite3_column_bytes (stmt, 0);
		if (rt)
		    free (rt);
		rt = malloc (len + 1);
		strcpy (rt, v);
		v = (const char *) sqlite3_column_text (stmt, 1);
		len = sqlite3_column_bytes (stmt, 1);
		if (rg)
		    free (rg);
		rg = malloc (len + 1);
		strcpy (rg, v);
		is_longlat = sqlite3_column_int (stmt, 2);
		count++;
	    }
      }
    sqlite3_finalize (stmt);
    if (count != 1)
	return vknn_find_view_rtree (sqlite, db_prefix, table_name,
				     real_table, real_geom, is_geographic);
    else
      {
	  *real_table = rt;
	  *real_geom = rg;
	  *is_geographic = is_longlat;
      }
    return 1;
}
Exemple #17
0
static int
cdb_update(struct custdb_handle *ch, const ChopstixCustomer *cust)
{
	char *phonestr, *phoneext, *special;
	sqlite3_stmt *q = NULL, *u = NULL;
	size_t cpos = 0;

	CHECKSQL(ch);

	if ((phonestr = phone2str(&cust->phone)) == NULL)
		goto fail;
	if ((phoneext = phoneext2str(&cust->phone)) == NULL)
		goto fail;

	/*
	 * CUSTOMER
	 */

	q = SQLARG(ch)->q.cdb.upd_cust;
	/* 1 = first parameter in prepared query */
	if (sqlite3_bind_text(q, 1, cust->name, strlen(cust->name),
				SQLITE_STATIC))
		goto fail;
	if (sqlite3_bind_text(q, 2, phoneext, strlen(phoneext), SQLITE_STATIC))
		goto fail;
	if (sqlite3_bind_text(q, 3, cust->addr.addr, strlen(cust->addr.addr),
				SQLITE_STATIC))
		goto fail;
	if (sqlite3_bind_text(q, 4, cust->addr.apt, strlen(cust->addr.apt),
				SQLITE_STATIC))
		goto fail;
	if (sqlite3_bind_text(q, 5, cust->addr.entry, strlen(cust->addr.entry),
				SQLITE_STATIC))
		goto fail;
	if (sqlite3_bind_text(q, 6, cust->isect.cross, strlen(cust->isect.cross),
				SQLITE_STATIC))
		goto fail;
	if (cust->special)
		special = *cust->special;
	else
		special = "";
	if (sqlite3_bind_text(q, 7, special, strlen(special), SQLITE_STATIC))
		goto fail;
	if (sqlite3_bind_text(q, 8, phonestr, strlen(phonestr), SQLITE_STATIC))
		goto fail;

	if (sqlite3_step(q) != SQLITE_DONE) {
		sql_err(SQLARG(ch), "cannot update customer");
		goto fail;
	}
	sqlite3_reset(q);

	/* no need to do this if no credits are attached */
	if (cust->credit == NULL || cust->credit->len == 0)
		return 0;

	/*
	 * CREDITS
	 *
	 * Have to SELECT, compare against the passed array and then UPDATE any
	 * that have changed.
	 */

	q = SQLARG(ch)->q.cdb.get_cred_phone;
	/* 1 = first parameter in prepared query */
	if (sqlite3_bind_text(q, 1, phonestr, strlen(phonestr), SQLITE_STATIC)) {
		sql_err(SQLARG(ch), "cannot bind phone number to query");
		goto fail;
	}
	while (sqlite3_step(q) == SQLITE_ROW) {
		/* limit credit positional */
		if (cpos >= cust->credit->len)
			break;

		/* ensure we are still in order */
		if (cust->credit->val[cpos].reason == NULL ||
				sqlite3_column_int(q, 1) != cust->credit->val[cpos].credit) {
			sql_err(SQLARG(ch), "cannot update customer credit");
			break;
		}

		/* if nothing needs to change, continue */
		if (cust->credit->val[cpos].remain == sqlite3_column_int(q, 2) &&
				strcasecmp(cust->credit->val[cpos].reason,
					sqlite3_column_text(q, 3)) == 0) {
			cpos++;
			continue;
		}

		u = SQLARG(ch)->q.cdb.upd_cred;
		/* 1 = first parameter in prepared query */
		if (sqlite3_bind_int(u, 1, cust->credit->val[cpos].remain))
			goto fail;
		if (sqlite3_bind_text(u, 2, cust->credit->val[cpos].reason,
					strlen(cust->credit->val[cpos].reason), SQLITE_STATIC))
			goto fail;
		if (sqlite3_bind_int(u, 3, sqlite3_column_int(q, 0)))
			goto fail;

		if (sqlite3_step(u) != SQLITE_DONE) {
			sql_err(SQLARG(ch), "cannot update credit %zd", cpos + 1);
			goto fail;
		}
		sqlite3_reset(u);

		cpos++;
	}
	sqlite3_reset(q);

	for (; cpos < cust->credit->len; cpos++) {
		u = SQLARG(ch)->q.cdb.put_cred;
		/* 1 = first parameter in prepared query */
		if (sqlite3_bind_text(u, 4, phonestr, strlen(phonestr), SQLITE_STATIC))
			goto fail;
		if (sqlite3_bind_int(u, 1, cust->credit->val[cpos].credit))
			goto fail;
		if (sqlite3_bind_int(u, 2, cust->credit->val[cpos].remain))
			goto fail;
		if (sqlite3_bind_text(u, 3, cust->credit->val[cpos].reason,
					strlen(cust->credit->val[cpos].reason), SQLITE_STATIC))
			goto fail;

		if (sqlite3_step(u) != SQLITE_DONE) {
			sql_err(SQLARG(ch), "cannot insert credit %zd", cpos + 1);
			goto fail;
		}
		sqlite3_reset(u);
	}

	return 0;

fail:
	sql_err(SQLARG(ch), "cannot bind data to query");
	if (u)
		sqlite3_reset(u);
	if (q)
		sqlite3_reset(q);
	return -1;
}
Exemple #18
0
int sql_query ( dbref player, char *q_string, char *buff, char **bufc, const Delim *row_delim, const Delim *field_delim )
{
    sqlite3 *sqlite;
    const unsigned char *col_data;
    int num_rows, got_rows, got_fields;
    int i, j;
    int retries;
    int retval;
    sqlite3_stmt *stmt;
    const char *rest;
    /*
     * If we have no connection, and we don't have auto-reconnect on (or
     * we try to auto-reconnect and we fail), this is an error generating
     * a #-1. Notify the player, too, and set the return code.
     */
    sqlite = sqlite3_struct;

    if ( ( !sqlite ) && ( mod_db_sql_config.reconnect != 0 ) ) {
        /*
         * Try to reconnect.
         */
        retries = 0;

        while ( ( retries < SQLITE_RETRY_TIMES ) && !sqlite ) {
            sleep ( 1 );
            sql_init ( 0, 0, NULL, NULL );
            sqlite = sqlite3_struct;
            retries++;
        }
    }

    if ( !sqlite ) {
        notify_quiet ( player, "No SQL database connection." );

        if ( buff )
            safe_str ( "#-1", buff, bufc );

        return -1;
    }

    if ( !q_string || !*q_string )
        return 0;

    /*
     * Prepare the query.
     */
    retval = sqlite3_prepare_v2 ( sqlite, q_string, -1, &stmt, &rest );

    if ( retval != SQLITE_OK ) {
        notify_quiet ( player, sqlite3_errmsg ( sqlite ) );

        if ( buff )
            safe_str ( "#-1", buff, bufc );

        sqlite3_finalize ( stmt );
        return -1;
    }

    /*
     * Construct properly-delimited data.
     */
    if ( buff ) {
        i = 0;

        while ( sqlite3_step ( stmt ) == SQLITE_ROW ) {
            if ( i++ > 0 ) {
                print_sep ( row_delim, buff, bufc );
            }

            got_fields = sqlite3_column_count ( stmt );

            if ( got_fields ) {
                for ( j = 0; j < got_fields; j++ ) {
                    col_data =
                        sqlite3_column_text ( stmt, j );

                    if ( j > 0 ) {
                        print_sep ( field_delim, buff,
                                    bufc );
                    }

                    if ( col_data && *col_data )
                        safe_str ( ( char * ) col_data,
                                   buff, bufc );
                }
            }
        }
    } else {
        i = 0;

        while ( sqlite3_step ( stmt ) == SQLITE_ROW ) {
            if ( i++ > 0 ) {
                print_sep ( row_delim, buff, bufc );
            }

            got_fields = sqlite3_column_count ( stmt );

            if ( got_fields ) {
                for ( j = 0; j < got_fields; j++ ) {
                    col_data =
                        sqlite3_column_text ( stmt, j );

                    if ( j > 0 ) {
                        notify_check ( player, player, MSG_PUP_ALWAYS | MSG_ME_ALL | MSG_F_DOWN, "Row %d, Field %d: %s", i, j + 1, col_data );
                    }

                    if ( col_data && *col_data ) {
                        notify_check ( player, player, MSG_PUP_ALWAYS | MSG_ME_ALL | MSG_F_DOWN, "Row %d, Field %d: NULL", i, j + 1 );
                    }
                }
            }
        }
    }

    if ( i == 0 ) {
        num_rows = sqlite3_changes ( sqlite );

        if ( num_rows > 0 ) {
            notify_check ( player, player, MSG_PUP_ALWAYS | MSG_ME_ALL | MSG_F_DOWN, "SQL query touched %d %s.", num_rows, ( num_rows == 1 ) ? "row" : "rows" );
        }
    }

    sqlite3_finalize ( stmt );
    return 0;
}
Exemple #19
0
static int image_index(lua_State *L)
{
  const char* membername = lua_tostring(L, -1);
  const dt_image_t * my_image=checkreadimage(L,-2);
  if(luaA_struct_has_member_name(L,dt_image_t,membername))
  {
    const int result = luaA_struct_push_member_name(L, dt_image_t, my_image, membername);
    releasereadimage(L,my_image);
    return result;
  }
  switch(luaL_checkoption(L,-1,NULL,image_fields_name))
  {
    case PATH:
      {
        sqlite3_stmt *stmt;
        DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),
            "select folder from images, film_rolls where "
            "images.film_id = film_rolls.id and images.id = ?1", -1, &stmt, NULL);
        DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, my_image->id);
        if(sqlite3_step(stmt) == SQLITE_ROW)
        {
          lua_pushstring(L,(char *)sqlite3_column_text(stmt, 0));
        }
        else
        {
          sqlite3_finalize(stmt);
          releasereadimage(L,my_image);
          return luaL_error(L,"should never happen");
        }
        sqlite3_finalize(stmt);
        break;
      }
    case DUP_INDEX:
      {
        // get duplicate suffix
        int version = 0;
        sqlite3_stmt *stmt;
        DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),
            "select count(id) from images where filename in "
            "(select filename from images where id = ?1) and film_id in "
            "(select film_id from images where id = ?1) and id < ?1",
            -1, &stmt, NULL);
        DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, my_image->id);
        if(sqlite3_step(stmt) == SQLITE_ROW)
          version = sqlite3_column_int(stmt, 0);
        sqlite3_finalize(stmt);
        lua_pushinteger(L,version);
        break;
      }
    case IS_LDR:
      lua_pushboolean(L,dt_image_is_ldr(my_image));
      break;
    case IS_HDR:
      lua_pushboolean(L,dt_image_is_hdr(my_image));
      break;
    case IS_RAW:
      lua_pushboolean(L,dt_image_is_raw(my_image));
      break;
    case RATING:
      {
        int score = my_image->flags & 0x7;
        if(score >6) score=5;
        if(score ==6) score=-1;

        lua_pushinteger(L,score);
        break;
      }
    case ID:
      lua_pushinteger(L,my_image->id);
      break;
    case FILM:
      luaA_push(L,dt_lua_film_t,&my_image->film_id);
      break;
    case CREATOR:
      {
        sqlite3_stmt *stmt;
        DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),"select value from meta_data where id = ?1 and key = ?2", -1, &stmt, NULL);
        DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, my_image->id);
        DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, DT_METADATA_XMP_DC_CREATOR);
        if(sqlite3_step(stmt) != SQLITE_ROW)
        {
          lua_pushstring(L,"");
        }
        else
        {
          lua_pushstring(L,(char *)sqlite3_column_text(stmt, 0));
        }
        sqlite3_finalize(stmt);
        break;

      }
    case PUBLISHER:
      {
        sqlite3_stmt *stmt;
        DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),"select value from meta_data where id = ?1 and key = ?2", -1, &stmt, NULL);
        DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, my_image->id);
        DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, DT_METADATA_XMP_DC_PUBLISHER);
        if(sqlite3_step(stmt) != SQLITE_ROW)
        {
          lua_pushstring(L,"");
        }
        else
        {
          lua_pushstring(L,(char *)sqlite3_column_text(stmt, 0));
        }
        sqlite3_finalize(stmt);
        break;

      }
    case TITLE:
      {
        sqlite3_stmt *stmt;
        DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),"select value from meta_data where id = ?1 and key = ?2", -1, &stmt, NULL);
        DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, my_image->id);
        DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, DT_METADATA_XMP_DC_TITLE);
        if(sqlite3_step(stmt) != SQLITE_ROW)
        {
          lua_pushstring(L,"");
        }
        else
        {
          lua_pushstring(L,(char *)sqlite3_column_text(stmt, 0));
        }
        sqlite3_finalize(stmt);
        break;

      }
    case DESCRIPTION:
      {
        sqlite3_stmt *stmt;
        DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),"select value from meta_data where id = ?1 and key = ?2", -1, &stmt, NULL);
        DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, my_image->id);
        DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, DT_METADATA_XMP_DC_DESCRIPTION);
        if(sqlite3_step(stmt) != SQLITE_ROW)
        {
          lua_pushstring(L,"");
        }
        else
        {
          lua_pushstring(L,(char *)sqlite3_column_text(stmt, 0));
        }
        sqlite3_finalize(stmt);
        break;

      }
    case RIGHTS:
      {
        sqlite3_stmt *stmt;
        DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),"select value from meta_data where id = ?1 and key = ?2", -1, &stmt, NULL);
        DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, my_image->id);
        DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, DT_METADATA_XMP_DC_RIGHTS);
        if(sqlite3_step(stmt) != SQLITE_ROW)
        {
          lua_pushstring(L,"");
        }
        else
        {
          lua_pushstring(L,(char *)sqlite3_column_text(stmt, 0));
        }
        sqlite3_finalize(stmt);
        break;

      }
    case GROUP_LEADER:
      {
        luaA_push(L,dt_lua_image_t,&(my_image->group_id));
        break;
      }
    case APPLY_STYLE:
      {
        lua_pushcfunction(L,dt_lua_style_apply);
        break;
      }
    case CREATE_STYLE:
      {
        lua_pushcfunction(L,dt_lua_style_create_from_image);
        break;
      }
    case RESET:
      {
        lua_pushcfunction(L,history_delete);
        break;
      }
    case MOVE:
      {
        lua_pushcfunction(L,dt_lua_move_image);
        break;
      }
    case COPY:
      {
        lua_pushcfunction(L,dt_lua_copy_image);
        break;
      }
    case LOCAL_COPY:
      {
        lua_pushboolean(L,my_image->flags &DT_IMAGE_LOCAL_COPY);
        break;
      }
    default:
      releasereadimage(L,my_image);
      return luaL_error(L,"should never happen %s",lua_tostring(L,-1));

  }
releasereadimage(L,my_image);
return 1;
}
Exemple #20
0
  /* raise signal of tags change to refresh keywords module */
  dt_control_signal_raise(darktable.signals, DT_SIGNAL_TAG_CHANGED);

  return TRUE;
}

guint dt_tag_remove(const guint tagid, gboolean final)
{
  int rv, count=-1;
  sqlite3_stmt *stmt;

  DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),
                              "SELECT count() FROM tagged_images WHERE tagid=?1", -1, &stmt, NULL);
  DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, tagid);
  rv = sqlite3_step(stmt);
  if( rv == SQLITE_ROW)
    count = sqlite3_column_int(stmt,0);
  sqlite3_finalize(stmt);

  if (final == TRUE )
  {
    // let's actually remove the tag
    DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),
                                "DELETE FROM tags WHERE id=?1", -1, &stmt, NULL);
    DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, tagid);
    sqlite3_step(stmt);
    sqlite3_finalize(stmt);
    DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),
                                "DELETE FROM tagxtag WHERE id1=?1 OR ID2=?1", -1, &stmt, NULL);
    DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, tagid);
Exemple #21
0
static int64 readLog(FILE *input,
                     sqlite3 *db)
{
  int64 eventCount = 0;

  /* declare statements for every event type */
  EVENT_LIST(EVENT_TYPE_DECLARE_STATEMENT, X);

  /* prepare statements for every event type */
  EVENT_LIST(EVENT_TYPE_PREPARE_STATEMENT, X);

  runStatement(db, "BEGIN", "Transaction start");

  while (TRUE) { /* loop for each event */
    char line[MAX_LOG_LINE_LENGTH];
    char *p;
    char *q;
    int last_index=0;
    sqlite3_stmt *statement = NULL;
    int res;
    int64 clock_field;
    long code;

    p = fgets(line, MAX_LOG_LINE_LENGTH, input);
    if (!p) {
      if (feof(input))
        break;
      else
        error("Couldn't read line after event %llu", eventCount);
    }

    eventCount++;

    clock_field = strtoll(p, &q, 16);

    if (q == p)
      error("event %llu clock field not a hex integer: %s",
            eventCount, p);

    if (*q != ' ')
      error("event %llu code field not preceded by ' ': %s",
            eventCount, q);
    while(*q == ' ')
      ++q;

    p = q;
    code = strtol(p, &q, 16);
    if (q == p)
      error("event %llu code field not an integer: %s",
            eventCount, p);
    p = q;

    /* Write event to SQLite. */
    switch (code) {
      /* this macro sets statement and last_index */
      EVENT_LIST(EVENT_TYPE_WRITE_SQL, X);
    default:
      error("Event %llu has Unknown event code %d", eventCount, code);
    }
    /* bind the fields we store for every event */ \
    res = sqlite3_bind_int64(statement, last_index+1, logSerial);
    if (res != SQLITE_OK)
      sqlite_error(res, db, "Event %llu bind of log_serial failed.", eventCount);
    res = sqlite3_bind_int64(statement, last_index+2, clock_field);
    if (res != SQLITE_OK)
      sqlite_error(res, db, "Event %llu bind of clock failed.", eventCount);
    res = sqlite3_step(statement);
    if (res != SQLITE_DONE)
      sqlite_error(res, db, "insert of event %llu failed.", eventCount);
    res = sqlite3_reset(statement);
    if (res != SQLITE_OK)
      sqlite_error(res, db, "Couldn't reset insert statement of event %llu", eventCount);

    if (progress) {
      if ((eventCount % SMALL_TICK) == 0) {
        printf(".");
        fflush(stdout);
        if (((eventCount / SMALL_TICK) % BIG_TICK) == 0) {
          printf("\n");
          fflush(stdout);
          evlog(LOG_SOMETIMES, "%lu events.", (unsigned long)eventCount);
        }
      }
    }
  }
  if (progress) {
    printf("\n");
    fflush(stdout);
  }
  runStatement(db, "COMMIT", "Transaction finish");
  logFileCompleted(db, eventCount);

  /* finalize all the statements */
  EVENT_LIST(EVENT_TYPE_FINALIZE_STATEMENT, X);

  return eventCount;
}
Exemple #22
0
static const char *dict_sqlite_lookup(DICT *dict, const char *name)
{
    const char *myname = "dict_sqlite_lookup";
    DICT_SQLITE *dict_sqlite = (DICT_SQLITE *) dict;
    sqlite3_stmt *sql_stmt;
    const char *query_remainder;
    static VSTRING *query;
    static VSTRING *result;
    const char *retval;
    int     expansion = 0;
    int     status;
    int     domain_rc;

    /*
     * In case of return without lookup (skipped key, etc.).
     */
    dict->error = 0;

    /*
     * Don't frustrate future attempts to make Postfix UTF-8 transparent.
     */
    if (!valid_utf_8(name, strlen(name))) {
	if (msg_verbose)
	    msg_info("%s: %s: Skipping lookup of non-UTF-8 key '%s'",
		     myname, dict_sqlite->parser->name, name);
	return (0);
    }

    /*
     * Optionally fold the key. Folding may be enabled on on-the-fly.
     */
    if (dict->flags & DICT_FLAG_FOLD_FIX) {
	if (dict->fold_buf == 0)
	    dict->fold_buf = vstring_alloc(100);
	vstring_strcpy(dict->fold_buf, name);
	name = lowercase(vstring_str(dict->fold_buf));
    }

    /*
     * Apply the optional domain filter for email address lookups.
     */
    if ((domain_rc = db_common_check_domain(dict_sqlite->ctx, name)) == 0) {
	if (msg_verbose)
	    msg_info("%s: %s: Skipping lookup of '%s'",
		     myname, dict_sqlite->parser->name, name);
	return (0);
    }
    if (domain_rc < 0)
	DICT_ERR_VAL_RETURN(dict, domain_rc, (char *) 0);

    /*
     * Expand the query and query the database.
     */
#define INIT_VSTR(buf, len) do { \
	if (buf == 0) \
		buf = vstring_alloc(len); \
	VSTRING_RESET(buf); \
	VSTRING_TERMINATE(buf); \
    } while (0)

    INIT_VSTR(query, 10);

    if (!db_common_expand(dict_sqlite->ctx, dict_sqlite->query,
			  name, 0, query, dict_sqlite_quote))
	return (0);

    if (msg_verbose)
	msg_info("%s: %s: Searching with query %s",
		 myname, dict_sqlite->parser->name, vstring_str(query));

    if (sqlite3_prepare_v2(dict_sqlite->db, vstring_str(query), -1,
			   &sql_stmt, &query_remainder) != SQLITE_OK)
	msg_fatal("%s: %s: SQL prepare failed: %s\n",
		  myname, dict_sqlite->parser->name,
		  sqlite3_errmsg(dict_sqlite->db));

    if (*query_remainder && msg_verbose)
	msg_info("%s: %s: Ignoring text at end of query: %s",
		 myname, dict_sqlite->parser->name, query_remainder);

    /*
     * Retrieve and expand the result(s).
     */
    INIT_VSTR(result, 10);
    while ((status = sqlite3_step(sql_stmt)) != SQLITE_DONE) {
	if (status == SQLITE_ROW) {
	    if (db_common_expand(dict_sqlite->ctx, dict_sqlite->result_format,
				 (char *) sqlite3_column_text(sql_stmt, 0),
				 name, result, 0)
		&& dict_sqlite->expansion_limit > 0
		&& ++expansion > dict_sqlite->expansion_limit) {
		msg_warn("%s: %s: Expansion limit exceeded for key '%s'",
			 myname, dict_sqlite->parser->name, name);
		dict->error = DICT_ERR_RETRY;
		break;
	    }
	}
	/* Fix 20100616 */
	else {
	    msg_warn("%s: %s: SQL step failed for query '%s': %s\n",
		     myname, dict_sqlite->parser->name,
		     vstring_str(query), sqlite3_errmsg(dict_sqlite->db));
	    dict->error = DICT_ERR_RETRY;
	    break;
	}
    }

    /*
     * Clean up.
     */
    if (sqlite3_finalize(sql_stmt))
	msg_fatal("%s: %s: SQL finalize failed for query '%s': %s\n",
		  myname, dict_sqlite->parser->name,
		  vstring_str(query), sqlite3_errmsg(dict_sqlite->db));

    return ((dict->error == 0 && *(retval = vstring_str(result)) != 0) ?
	    retval : 0);
}
Exemple #23
0
QStringList QgsStyleV2::findSymbols( StyleEntity type, const QString& qword )
{
  if ( !mCurrentDB )
  {
    QgsDebugMsg( "Sorry! Cannot open database to search" );
    return QStringList();
  }

  // first find symbols with matching name
  QString item = ( type == SymbolEntity ) ? "symbol" : "colorramp";
  char *query = sqlite3_mprintf( "SELECT name FROM %q WHERE name LIKE '%%%q%%'",
                                 item.toUtf8().constData(), qword.toUtf8().constData() );

  sqlite3_stmt *ppStmt;
  int nErr = sqlite3_prepare_v2( mCurrentDB, query, -1, &ppStmt, nullptr );

  QSet< QString > symbols;
  while ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
  {
    symbols << QString::fromUtf8( reinterpret_cast< const char * >( sqlite3_column_text( ppStmt, 0 ) ) );
  }

  sqlite3_finalize( ppStmt );

  // next add symbols with matching tags
  query = sqlite3_mprintf( "SELECT id FROM tag WHERE name LIKE '%%%q%%'", qword.toUtf8().constData() );
  nErr = sqlite3_prepare_v2( mCurrentDB, query, -1, &ppStmt, NULL );

  QStringList tagids;
  while ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
  {
    tagids << QString::fromUtf8(( const char * ) sqlite3_column_text( ppStmt, 0 ) );
  }

  sqlite3_finalize( ppStmt );


  QString dummy = tagids.join( ", " );

  if ( type == SymbolEntity )
  {
    query = sqlite3_mprintf( "SELECT symbol_id FROM tagmap WHERE tag_id IN (%q)",
                             dummy.toUtf8().constData() );
  }
  else
  {
    query = sqlite3_mprintf( "SELECT colorramp_id FROM ctagmap WHERE tag_id IN (%q)",
                             dummy.toUtf8().constData() );
  }
  nErr = sqlite3_prepare_v2( mCurrentDB, query, -1, &ppStmt, NULL );

  QStringList symbolids;
  while ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
  {
    symbolids << QString::fromUtf8(( const char * ) sqlite3_column_text( ppStmt, 0 ) );
  }

  sqlite3_finalize( ppStmt );


  dummy = symbolids.join( ", " );
  query = sqlite3_mprintf( "SELECT name FROM %q  WHERE id IN (%q)",
                           item.toUtf8().constData(), dummy.toUtf8().constData() );
  nErr = sqlite3_prepare_v2( mCurrentDB, query, -1, &ppStmt, NULL );
  while ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
  {
    symbols << QString::fromUtf8(( const char * ) sqlite3_column_text( ppStmt, 0 ) );
  }

  sqlite3_finalize( ppStmt );

  return symbols.toList();
}
Exemple #24
0
int
sqlite3_stepx (sqlite3 *db,
               sqlite3_stmt **stmtp,
               sqlite3_stepx_callback callback,
               void *cookie,
               char **errmsg,
               const char *sql, ...)
{
  int rc;
  int err = 0;
  sqlite3_stmt *stmt = NULL;

  va_list va;
  int args;
  enum sqlite_arg_type t;
  int i;

  int cols;
  /* Names of the columns.  We initialize this lazily to avoid the
     overhead in case the query doesn't return any results.  */
  const char **azColName = 0;
  int callback_initialized = 0;

  const char **azVals = 0;

  callback_initialized = 0;

  if (stmtp && *stmtp)
    {
      stmt = *stmtp;

      /* Make sure this statement is associated with the supplied db.  */
      assert (db == sqlite3_db_handle (stmt));

#if DEBUG_TOFU_CACHE
      prepares_saved ++;
#endif
    }
  else
    {
      const char *tail = NULL;

      rc = sqlite3_prepare_v2 (db, sql, -1, &stmt, &tail);
      if (rc)
        log_fatal ("failed to prepare SQL: %s", sql);

      /* We can only process a single statement.  */
      if (tail)
        {
          while (*tail == ' ' || *tail == ';' || *tail == '\n')
            tail ++;

          if (*tail)
            log_fatal
              ("sqlite3_stepx can only process a single SQL statement."
               "  Second statement starts with: '%s'\n",
               tail);
        }

      if (stmtp)
        *stmtp = stmt;
    }

#if DEBUG_TOFU_CACHE
  queries ++;
#endif

  args = sqlite3_bind_parameter_count (stmt);
  va_start (va, sql);
  if (args)
    {
      for (i = 1; i <= args; i ++)
        {
          t = va_arg (va, enum sqlite_arg_type);
          switch (t)
            {
            case SQLITE_ARG_INT:
              {
                int value = va_arg (va, int);
                err = sqlite3_bind_int (stmt, i, value);
                break;
              }
            case SQLITE_ARG_LONG_LONG:
              {
                long long value = va_arg (va, long long);
                err = sqlite3_bind_int64 (stmt, i, value);
                break;
              }
            case SQLITE_ARG_STRING:
              {
                char *text = va_arg (va, char *);
                err = sqlite3_bind_text (stmt, i, text, -1, SQLITE_STATIC);
                break;
              }
            case SQLITE_ARG_BLOB:
              {
                char *blob = va_arg (va, void *);
                long long length = va_arg (va, long long);
                err = sqlite3_bind_blob (stmt, i, blob, length, SQLITE_STATIC);
                break;
              }
            default:
              /* Internal error.  Likely corruption.  */
              log_fatal ("Bad value for parameter type %d.\n", t);
            }

          if (err)
            {
              log_fatal ("Error binding parameter %d\n", i);
              goto out;
            }
        }

    }
  t = va_arg (va, enum sqlite_arg_type);
  assert (t == SQLITE_ARG_END);
  va_end (va);

  for (;;)
    {
      rc = sqlite3_step (stmt);

      if (rc != SQLITE_ROW)
        /* No more data (SQLITE_DONE) or an error occurred.  */
        break;

      if (! callback)
        continue;

      if (! callback_initialized)
        {
          cols = sqlite3_column_count (stmt);
          azColName = xmalloc (2 * cols * sizeof (const char *) + 1);

          for (i = 0; i < cols; i ++)
            azColName[i] = sqlite3_column_name (stmt, i);

          callback_initialized = 1;
        }

      azVals = &azColName[cols];
      for (i = 0; i < cols; i ++)
        {
          azVals[i] = sqlite3_column_text (stmt, i);
          if (! azVals[i] && sqlite3_column_type (stmt, i) != SQLITE_NULL)
            /* Out of memory.  */
            {
              err = SQLITE_NOMEM;
              break;
            }
        }

      if (callback (cookie, cols, (char **) azVals, (char **) azColName, stmt))
        /* A non-zero result means to abort.  */
        {
          err = SQLITE_ABORT;
          break;
        }
    }

 out:
  xfree (azColName);

  if (stmtp)
    rc = sqlite3_reset (stmt);
  else
    rc = sqlite3_finalize (stmt);
  if (rc == SQLITE_OK && err)
    /* Local error.  */
    {
      rc = err;
      if (errmsg)
        {
          const char *e = sqlite3_errstr (err);
          size_t l = strlen (e) + 1;
          *errmsg = sqlite3_malloc (l);
          if (! *errmsg)
            log_fatal ("Out of memory.\n");
          memcpy (*errmsg, e, l);
        }
    }
  else if (rc != SQLITE_OK && errmsg)
    /* Error reported by sqlite.  */
    {
      const char * e = sqlite3_errmsg (db);
      size_t l = strlen (e) + 1;
      *errmsg = sqlite3_malloc (l);
      if (! *errmsg)
        log_fatal ("Out of memory.\n");
      memcpy (*errmsg, e, l);
    }

  return rc;
}
Exemple #25
0
void Database_SQLite3::beginSave() {
	verifyDatabase();
	SQLRES(sqlite3_step(m_stmt_begin), SQLITE_DONE,
		"Failed to start SQLite3 transaction");
	sqlite3_reset(m_stmt_begin);
}
Exemple #26
0
static int FindLine(sqlite3 *db, char *file, int line, char *name, char *returnedFileName, int *startLine)
{
    // this query to attempt to find it in the current file
    static char *query = {
        "SELECT FileNames.Name, LineNumbers.Startline FROM LineNumbers "
        "    JOIN FileNames ON FileNames.id = LineNumbers.fileId "
        "    JOIN Names ON Names.id = LineNumbers.symbolId"
        "    WHERE FileNames.name = ?"
        "        AND Names.name = ?"
        "        AND LineNumbers.startLine <= ?"
        "    ORDER BY LineNumbers.startLine DESC;"
    };
    // this query to find it at file scope in any file the current main file includes
    static char *query2 = {
        "SELECT FileNames.name, LineNumbers.Startline FROM LineNumbers "
        "    JOIN FileNames ON FileNames.id = LineNumbers.fileId"
        "    JOIN Names ON Names.id = LineNumbers.symbolId"
        "    WHERE FileNames.name != ?"
        "        AND Names.name = ?;"
    };
    char fileName[260];
    int i, l = strlen(file);
    int rc = SQLITE_OK;
    sqlite3_stmt *handle;
    for (i=0; i < l; i++)
        fileName[i] = tolower(file[i]);
    fileName[i] = 0;
    rc = sqlite3_prepare_v2(db, query, strlen(query)+1, &handle, NULL);
    if (rc == SQLITE_OK)
    {
        int done = FALSE;
        rc = SQLITE_DONE;
        sqlite3_reset(handle);
        sqlite3_bind_text(handle, 1, fileName, strlen(fileName), SQLITE_STATIC);
        sqlite3_bind_text(handle, 2, name, strlen(name), SQLITE_STATIC);
        sqlite3_bind_int(handle, 3, line);
        while (!done)
        {
            switch(rc = sqlite3_step(handle))
            {
                case SQLITE_BUSY:
                    done = TRUE;
                    break;
                case SQLITE_DONE:
                    done = TRUE;
                    break;
                case SQLITE_ROW:
                    strcpy(returnedFileName, sqlite3_column_text(handle, 0));
                    *startLine = sqlite3_column_int(handle, 1);
                    rc = SQLITE_OK;
                    done = TRUE;
                    break;
                default:
                    done = TRUE;
                    break;
            }
        }
        sqlite3_finalize(handle);
        if (rc != SQLITE_OK)
        {
            rc = sqlite3_prepare_v2(db, query2, strlen(query2)+1, &handle, NULL);
            if (rc == SQLITE_OK)
            {
                int done = FALSE;
                rc = SQLITE_DONE;
                sqlite3_reset(handle);
                sqlite3_bind_text(handle, 1, fileName, strlen(fileName), SQLITE_STATIC);
                sqlite3_bind_text(handle, 2, name, strlen(name), SQLITE_STATIC);
                while (!done)
                {
                    switch(rc = sqlite3_step(handle))
                    {
                        case SQLITE_BUSY:
                            done = TRUE;
                            break;
                        case SQLITE_DONE:
                            done = TRUE;
                            break;
                        case SQLITE_ROW:
                            strcpy(returnedFileName, sqlite3_column_text(handle, 0));
                            *startLine = sqlite3_column_int(handle, 1);
                            rc = SQLITE_OK;
                            done = TRUE;
                            break;
                        default:
                            done = TRUE;
                            break;
                    }
                }
            }
            sqlite3_finalize(handle);
        }
    }
    return rc == SQLITE_OK;
}
int main(int argc, char* argv[])
{
	sqlite3 *db;
	int return_code;

	const char *db_name;
	if (argc == 2) {
		db_name = argv[1];
	} else {
		db_name = "test.db";
	}

	return_code = sqlite3_open(db_name, &db);

	if (return_code != SQLITE_OK) {
		fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
		sqlite3_close(db);
		return 1;
	}


	const char *sql = add_record;
	sqlite3_stmt *statement;
	return_code = sqlite3_prepare_v2(db, sql, -1, &statement, 0);
	if (return_code != SQLITE_OK) {
		printf("Problem with SQL statement - can't prepare it?\n");
		sqlite3_close_v2(db);
	}

	int done = 0;
	char str[25];
	int inumber = 0;
	double fnumber = 0;
	int ans=0;
	char user_input[256];

	while (!done) {

		printf("Enter Record number: ");
		fgets(user_input, 255, stdin);
		sscanf(user_input, "%d ", &inumber);
		printf("Enter string: ");
		fgets(user_input, 20, stdin);
		sscanf(user_input, "%[^\n\r]", str);
		printf("Enter number: ");
		fgets(user_input, 255, stdin);
		sscanf(user_input, "%lf ", &fnumber);
		printf("%d, %s, %lf\n", inumber, str, fnumber);

		//"(T_Index INTEGER PRIMARY KEY, My_String VARCHAR(20), My_Number FLOAT);";
		return_code = sqlite3_bind_int(statement, 1, inumber);
		if (return_code != SQLITE_OK)
			printf("Trouble binding 1");
		return_code = sqlite3_bind_text(statement, 2, str, -1, SQLITE_STATIC);
		if (return_code != SQLITE_OK)
			printf("Trouble binding 2");
		return_code = sqlite3_bind_double(statement, 3, fnumber);
		if (return_code != SQLITE_OK)
			printf("Trouble binding 3");

		return_code = sqlite3_step(statement);
		if (return_code != SQLITE_DONE) {
			printf("Something went wrong: error code %d\n", return_code);
			return -1;
		} else
			printf("Record added\n");


		return_code = sqlite3_reset(statement);// have to reset before binding new stuff.
		printf("Another record? (y/n)");

		ans = getchar();
		if (ans != 'y')
			done = 1;
		printf("\n");

	}

	//finished with statement - free memory associated with the DQL
	sqlite3_finalize(statement);

	sqlite3_close_v2(db);
	return 0;
}
/**
 * 住所リスト表示
 * @param
 * @return void
 */
void __fastcall TForm1::ShowAddress()
{
	// メソッド内変数宣言
	sqlite3 *dbObj = NULL;
	sqlite3_stmt *stmt = NULL;
	char *dbName = "practice4.sqlite3";
	char *sql;
	int result;

	try
	{
		//**************************************************
		//【DB接続】
		//**************************************************
		result = sqlite3_open(dbName, &dbObj);

		if (result != SQLITE_OK)
		{
			// NG
			ShowMessage("DB接続中に異常が発生しました。");
			return;
		}

		//**************************************************
		//【SQL生成】
		//**************************************************
		sql =
			"SELECT "
				"* "
			"FROM "
				"p4_address "
			"ORDER BY "
				"id"
		;

		//**************************************************
		//【SQL実行】
		//**************************************************
		// クリア
		sqlite3_reset(stmt);

		// 準備
		result = sqlite3_prepare_v2(dbObj, sql, strlen(sql), &stmt, NULL);

		if (result != SQLITE_OK)
		{
			// NG
			ShowMessage("SQL実行中に異常が発生しました。");
		}

		//**************************************************
		//【SQL実行結果取得】
		//**************************************************
		while ((result = sqlite3_step(stmt)) == SQLITE_ROW)
		{
			//==================================================
			//【住所リスト】
			//  行追加
			//==================================================
			this->grd_Address->RowCount++;

			// 行インデックス取得
			int rowIndex = this->grd_Address->RowCount - 1;

			//==================================================
			//【住所リスト】
			//  スタイル設定
			//==================================================
			// 固定行
			this->grd_Address->FixedRows = 1;

			// 高さ
			this->grd_Address->RowHeights[rowIndex] = 24;

			//==================================================
			//【データ抽出】
			//==================================================
			int id = sqlite3_column_int(stmt, 0);
			UnicodeString name = (WCHAR*)sqlite3_column_text16(stmt, 1);
			UnicodeString address = (WCHAR*)sqlite3_column_text16(stmt, 2);
			UnicodeString memo = (WCHAR*)sqlite3_column_text16(stmt, 3);
			int valid = sqlite3_column_int(stmt, 4);

			//==================================================
			//【データ設定】
			//==================================================
			// ID
			this->grd_Address->Cells[0][rowIndex] = id;

			// 名前
			this->grd_Address->Cells[1][rowIndex] = name;

			// 住所
			this->grd_Address->Cells[2][rowIndex] = address;

			// メモ
			this->grd_Address->Cells[3][rowIndex] = memo;

			// 有効
			if (valid == 1)
			{
				// ○:有効
				this->grd_Address->Cells[4][rowIndex] = L"○";
			}
			else
			{
				// ×:無効
				this->grd_Address->Cells[4][rowIndex] = L"×";
			}
		}

		if (result != SQLITE_DONE)
		{
			// NG
			ShowMessage("SQL実行結果取得中に異常が発生しました。");
		}

		// オブジェクト解放
		sqlite3_finalize(stmt);
		sqlite3_free(sql);

		//**************************************************
		//【DB切断】
		//**************************************************
		sqlite3_close(dbObj);
	}
	catch (int e)
	{
		throw e;
	}
}
/**
 * This function is invoked as:
 *
 *  _TOKENIZE('<token_table>', <data_row_id>, <data>, <delimiter>,
 *             <use_token_index>, <data_tag>)
 *
 * If <use_token_index> is omitted, it is treated as 0.
 * If <data_tag> is omitted, it is treated as NULL.
 *
 * It will split <data> on each instance of <delimiter> and insert each token
 * into <token_table>. The following columns in <token_table> are used:
 * token TEXT, source INTEGER, token_index INTEGER, tag (any type)
 * The token_index column is not required if <use_token_index> is 0.
 * The tag column is not required if <data_tag> is NULL.
 *
 * One row is inserted for each token in <data>.
 * In each inserted row, 'source' is <data_row_id>.
 * In the first inserted row, 'token' is the hex collation key of
 * the entire <data> string, and 'token_index' is 0.
 * In each row I (where 1 <= I < N, and N is the number of tokens in <data>)
 * 'token' will be set to the hex collation key of the I:th token (0-based).
 * If <use_token_index> != 0, 'token_index' is set to I.
 * If <data_tag> is not NULL, 'tag' is set to <data_tag>.
 *
 * In other words, there will be one row for the entire string,
 * and one row for each token except the first one.
 *
 * The function returns the number of tokens generated.
 */
static void tokenize(sqlite3_context * context, int argc, sqlite3_value ** argv)
{
    //LOGD("enter tokenize");
    int err;
    int useTokenIndex = 0;
    int useDataTag = 0;

    if (!(argc >= 4 || argc <= 6)) {
        LOGE("Tokenize requires 4 to 6 arguments");
        sqlite3_result_null(context);
        return;
    }

    if (argc > 4) {
        useTokenIndex = sqlite3_value_int(argv[4]);
    }

    if (argc > 5) {
        useDataTag = (sqlite3_value_type(argv[5]) != SQLITE_NULL);
    }

    sqlite3 * handle = sqlite3_context_db_handle(context);
    UCollator* collator = (UCollator*)sqlite3_user_data(context);
    char const * tokenTable = (char const *)sqlite3_value_text(argv[0]);
    if (tokenTable == NULL) {
        LOGE("tokenTable null");
        sqlite3_result_null(context);
        return;
    }

    // Get or create the prepared statement for the insertions
    sqlite3_stmt * statement = (sqlite3_stmt *)sqlite3_get_auxdata(context, 0);
    if (!statement) {
        char const * tokenIndexCol = useTokenIndex ? ", token_index" : "";
        char const * tokenIndexParam = useTokenIndex ? ", ?" : "";
        char const * dataTagCol = useDataTag ? ", tag" : "";
        char const * dataTagParam = useDataTag ? ", ?" : "";
        char * sql = sqlite3_mprintf("INSERT INTO %s (token, source%s%s) VALUES (?, ?%s%s);",
                tokenTable, tokenIndexCol, dataTagCol, tokenIndexParam, dataTagParam);
        err = sqlite3_prepare_v2(handle, sql, -1, &statement, NULL);
        sqlite3_free(sql);
        if (err) {
            LOGE("prepare failed");
            sqlite3_result_null(context);
            return;
        }
        // This binds the statement to the table it was compiled against, which is argv[0].
        // If this function is ever called with a different table the finalizer will be called
        // and sqlite3_get_auxdata() will return null above, forcing a recompile for the new table.
        sqlite3_set_auxdata(context, 0, statement, tokenize_auxdata_delete);
    } else {
        // Reset the cached statement so that binding the row ID will work properly
        sqlite3_reset(statement);
    }

    // Bind the row ID of the source row
    int64_t rowID = sqlite3_value_int64(argv[1]);
    err = sqlite3_bind_int64(statement, 2, rowID);
    if (err != SQLITE_OK) {
        LOGE("bind failed");
        sqlite3_result_null(context);
        return;
    }

    // Bind <data_tag> to the tag column
    if (useDataTag) {
        int dataTagParamIndex = useTokenIndex ? 4 : 3;
        err = sqlite3_bind_value(statement, dataTagParamIndex, argv[5]);
        if (err != SQLITE_OK) {
            LOGE("bind failed");
            sqlite3_result_null(context);
            return;
        }
    }

    // Get the raw bytes for the string to tokenize
    // the string will be modified by following code
    // however, sqlite did not reuse the string, so it is safe to not dup it
    UChar * origData = (UChar *)sqlite3_value_text16(argv[2]);
    if (origData == NULL) {
        sqlite3_result_null(context);
        return;
    }

    // Get the raw bytes for the delimiter
    const UChar * delim = (const UChar *)sqlite3_value_text16(argv[3]);
    if (delim == NULL) {
        LOGE("can't get delimiter");
        sqlite3_result_null(context);
        return;
    }

    UChar * token = NULL;
    UChar *state;
    int numTokens = 0;

    do {
        if (numTokens == 0) {
            token = origData;
        }

        // Reset the program so we can use it to perform the insert
        sqlite3_reset(statement);
        UErrorCode status = U_ZERO_ERROR;
        char keybuf[1024];
        uint32_t result = ucol_getSortKey(collator, token, -1, (uint8_t*)keybuf, sizeof(keybuf)-1);
        if (result > sizeof(keybuf)) {
            // TODO allocate memory for this super big string
            LOGE("ucol_getSortKey needs bigger buffer %d", result);
            break;
        }
        uint32_t keysize = result-1;
        uint32_t base16Size = keysize*2;
        char *base16buf = (char*)malloc(base16Size);
        base16Encode(base16buf, keybuf, keysize);
        err = sqlite3_bind_text(statement, 1, base16buf, base16Size, SQLITE_STATIC);

        if (err != SQLITE_OK) {
            LOGE(" sqlite3_bind_text16 error %d", err);
            free(base16buf);
            break;
        }

        if (useTokenIndex) {
            err = sqlite3_bind_int(statement, 3, numTokens);
            if (err != SQLITE_OK) {
                LOGE(" sqlite3_bind_int error %d", err);
                free(base16buf);
                break;
            }
        }

        err = sqlite3_step(statement);
        free(base16buf);

        if (err != SQLITE_DONE) {
            LOGE(" sqlite3_step error %d", err);
            break;
        }
        numTokens++;
        if (numTokens == 1) {
            // first call
            u_strtok_r(origData, delim, &state);
        }
    } while ((token = u_strtok_r(NULL, delim, &state)) != NULL);
    sqlite3_result_int(context, numTokens);
}
Exemple #30
0
const char *
dbop3_first(DBOP *dbop, const char *name, regex_t *preg, int flags) {
	int rc;
	char *key;
	STRBUF *sql = strbuf_open_tempbuf();

	dbop->done = 0; 	/* This is turned on when it receives SQLITE_DONE. */
	strbuf_puts(sql, "select rowid, * from ");
	strbuf_puts(sql, dbop->tblname);
	if (name) {
		strbuf_puts(sql, " where key ");
		if (dbop->ioflags & DBOP_PREFIX) {
			/*
			 * In sqlite3, 'like' ignores case. 'glob' does not ignore case.
			 */
			strbuf_puts(sql, "glob '");
			strbuf_puts(sql, name);
			strbuf_puts(sql, "*'");
		} else {
			strbuf_puts(sql, "= '");
			strbuf_puts(sql, name);
			strbuf_puts(sql, "'");
		}
		strlimcpy(dbop->key, name, sizeof(dbop->key));
		dbop->keylen = strlen(name);
	}
	strbuf_puts(sql, " order by key");
	if (dbop->stmt) {
		rc = sqlite3_finalize(dbop->stmt);
		if (rc != SQLITE_OK)
			die("dbop3_finalize failed. (rc = %d)", rc);
		dbop->stmt = NULL;
	}
	rc = sqlite3_prepare_v2(dbop->db3, strbuf_value(sql), -1, &dbop->stmt, NULL);
	if (rc != SQLITE_OK)
		die("dbop3_first: sqlite3_prepare_v2 failed. (rc = %d)", rc);
	/*
	 *	0: rowid
	 *	1: key
	 *	2: dat
	 *	3: flags
	 */
	for (;;) {
		/* Once it receives SQLITE_DONE, do not never return value */
		if (dbop->done)
			return NULL;
		rc = sqlite3_step(dbop->stmt);
		if (rc == SQLITE_DONE)
			goto finish;
		else if (rc == SQLITE_ROW) {
			dbop->readcount++;
			dbop->lastrowid = sqlite3_column_int64(dbop->stmt, 0);
			key = (char *)sqlite3_column_text(dbop->stmt, 1);
			if (name) {
				if (dbop->ioflags & DBOP_PREFIX) {
					if (strncmp(key, dbop->key, dbop->keylen))
						goto finish;
				} else {
					if (strcmp(key, dbop->key)) 
						goto finish;
				}
				if (dbop->preg && regexec(dbop->preg, key, 0, 0, 0) != 0)
					continue;
			} else {
				/* skip meta records */
				if (ismeta(key) && !(dbop->openflags & DBOP_RAW))
					continue;
				if (dbop->preg && regexec(dbop->preg, key, 0, 0, 0) != 0)
					continue;
			}
			break;
		} else {
			die("dbop3_first: something is wrong (rc = %d).", rc);
		}
	}
	strbuf_clear(dbop->sb);
	strbuf_puts0(dbop->sb, (char *)sqlite3_column_text(dbop->stmt, 2));
	dbop->lastsize = strbuf_getlen(dbop->sb) - 1;
	dbop->lastflag = (char *)sqlite3_column_text(dbop->stmt, 3);
	if (dbop->lastflag)
		strbuf_puts(dbop->sb, dbop->lastflag);
	dbop->lastdat = strbuf_value(dbop->sb);
	if (dbop->lastflag)
		dbop->lastflag = dbop->lastdat + dbop->lastsize + 1;
	dbop->lastkey = key;
	dbop->lastkeysize = strlen(dbop->lastkey);
	strbuf_release_tempbuf(sql);
	if (flags & DBOP_KEY) {
		strlimcpy(dbop->prev, key, sizeof(dbop->prev));
		return key;
	}
	return dbop->lastdat;
finish:
	strbuf_release_tempbuf(sql);
	dbop->done = 1;
	dbop->lastdat = NULL;
	dbop->lastsize = 0;
	dbop->lastflag = NULL;
	return dbop->lastdat;
}