mercredi 16 décembre 2015

sqlite passing comma separated string as sqlite3_result_text

I have a custom function that needs to return a comma-separated list of IDs that I then need to use in a statement. However, I can't seem to get it to work as sqlite3_result_text seems to be unhappy about the single quotes I've used for individual strings. Here's what I have:

void sqliteExtractNames(sqlite3_context *context, int argc, sqlite3_value **argv) {
  assert(argc == 1);

  if (sqlite3_value_type(argv[0]) == SQLITE_TEXT) {
    unsigned const char *stringC = sqlite3_value_text(argv[0]);

    NSString *stringOrig = [[NSString alloc] initWithUTF8String:(char *) stringC];

    // This returns something like: 'Name 1', 'Name 2'
    NSString *nameString = [self getCommaSeparatedNames: stringOrig];


    if ([nameString length] > 0) {
      sqlite3_result_text(context, [nameString UTF8String], -1, SQLITE_TRANSIENT);
    } else {
      sqlite3_result_null(context);
    }
  } else {
    sqlite3_result_null(context);
  }
}

And then later I'm doing:

SELECT count(*) FROM mytable WHERE name IN (sqliteExtractNames(somecolumn))

However this does not seem to work. If I change the getCommaSeparatedNames method to instead return a single word without single quotes, it works. The moment I use more than one word separated by a comma, it stops working. How can I pass a text result back which can be used in this statement?

Aucun commentaire:

Enregistrer un commentaire