Skip to content

Instantly share code, notes, and snippets.

@little-brother
Created April 26, 2021 12:18
Show Gist options
  • Select an option

  • Save little-brother/074047adcca02a0845fdc4d7db0491dc to your computer and use it in GitHub Desktop.

Select an option

Save little-brother/074047adcca02a0845fdc4d7db0491dc to your computer and use it in GitHub Desktop.
SQLite scalar function example
static void square (sqlite3_context *ctx, int argc, sqlite3_value **argv) {
int type = sqlite3_value_type(argv[0]);
double d = sqlite3_value_double(argv[0]);
switch (type) {
case SQLITE_NULL:
sqlite3_result_null(ctx);
break;
case SQLITE_INTEGER:
case SQLITE_FLOAT:
sqlite3_result_double(ctx, d * d);
break;
case SQLITE_TEXT:
case SQLITE_BLOB:
sqlite3_result_error(ctx, "Unsupported", -1);
break;
default:
sqlite3_result_error(ctx, "Impossible", -1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment