aboutsummaryrefslogtreecommitdiff
path: root/QSThingsPlugin/Helpers/SQLHelper.m
blob: c26dfc54c99221591172474861ea881a4b5a450c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#import "SQLHelper.h"
#import <QSFoundation/FMDatabase.h>

@implementation SQLHelper

+ (NSArray *) executeSql:(NSString *)query onFile:(NSString *)path {
  
  NSMutableArray *objects = [NSMutableArray arrayWithCapacity:0];
  
  FMDatabase *database = [FMDatabase databaseWithPath:path];
  if (![database openWithFlags:0x00000001]) { // 1 = SQLITE_OPEN_READONLY
    NSLog(@"Could not open Things database.");
    return @[];
  }
  [database goodConnection];

  // execute SQL query
  FMResultSet *resultSet = [database executeQuery:query];
  if ([database hadError]) {
    NSLog(@"Error while reading Things database. (%d): %@", [database lastErrorCode], [database lastErrorMessage]);
    return @[];
  }
  
  while ([resultSet next]) {
          // Create a completely independent dictionary with string copies
          NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
          
          // Manually copy each field to ensure we have independent string objects
          NSDictionary *originalDict = resultSet.resultDictionary;
          for (NSString *key in originalDict.allKeys) {
              id value = [originalDict objectForKey:key];
              if (value && value != [NSNull null]) {
                  // Make sure we have independent copies of strings
                  if ([value isKindOfClass:[NSString class]]) {
                      resultDictionary[key] = [NSString stringWithString:value];
                  } else {
                      resultDictionary[key] = value;
                  }
              }
          }
          
          [objects addObject:[resultDictionary copy]]; // Make immutable copy
      }

  [resultSet close];
  [database close];
  
  return objects;
}

+ (BOOL)appendString:(NSString *)string toFile:(NSString *)filePath {
  // Create file manager
  NSFileManager *fileManager = [NSFileManager defaultManager];
  
  // Check if file exists, create it if it doesn't
  if (![fileManager fileExistsAtPath:filePath]) {
      // Create empty file
      [@"" writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  }
  
  // Create file handle for appending
  NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
  
  if (!fileHandle) {
      NSLog(@"Failed to open file for writing: %@", filePath);
      return NO;
  }
  
  // Move to end of file
  [fileHandle seekToEndOfFile];
  
  // Convert string to data and write
  NSData *stringData = [string dataUsingEncoding:NSUTF8StringEncoding];
  [fileHandle writeData:stringData];
  
  // Close file handle
  [fileHandle closeFile];
  
  NSLog(@"Successfully appended string to: %@", filePath);
  return YES;
}

@end