Skip to content

Instantly share code, notes, and snippets.

@ccgus
Created August 23, 2013 21:28
Show Gist options
  • Save ccgus/6324222 to your computer and use it in GitHub Desktop.
Save ccgus/6324222 to your computer and use it in GitHub Desktop.
Custom SQLite Functions
[db makeFunctionNamed:@"UTTypeConformsTo" maximumArguments:2 withBlock:^(sqlite3_context *context, int argc, sqlite3_value **argv) {
if (sqlite3_value_type(argv[0]) == SQLITE_TEXT) {
const unsigned char *a = sqlite3_value_text(argv[0]);
const unsigned char *b = sqlite3_value_text(argv[1]);
CFStringRef as = CFStringCreateWithCString(nil, (const char*)a, kCFStringEncodingUTF8);
CFStringRef bs = CFStringCreateWithCString(nil, (const char*)b, kCFStringEncodingUTF8);
sqlite3_result_int(context, UTTypeConformsTo(as, bs));
CFRelease(as);
CFRelease(bs);
}
else {
NSLog(@"Unknown formart for UTTypeConformsToSQLiteCallBackFunction (%d) %s:%d", sqlite3_value_type(argv[0]), __FUNCTION__, __LINE__);
sqlite3_result_null(context);
}
}];
- (NSArray*)orderedDisplayNamesWithUTIConformingTo:(NSString*)uti {
NSMutableArray *ret = [NSMutableArray array];
[[self q] inDatabase:^(FMDatabase *db) {
FMResultSet *rs = [db executeQuery:@"select displayName, key from items where UTTypeConformsTo(uti, ?) order by 2", uti];
while ([rs next]) {
[ret addObject:[rs stringForColumnIndex:0]];
}
}];
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment