|
| 1 | +// |
| 2 | +// LPDatabase.m |
| 3 | +// Leanplum |
| 4 | +// |
| 5 | +// Created by Alexis Oyama on 6/9/17. |
| 6 | +// Copyright (c) 2017 Leanplum, Inc. All rights reserved. |
| 7 | +// |
| 8 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 9 | +// or more contributor license agreements. See the NOTICE file |
| 10 | +// distributed with this work for additional information |
| 11 | +// regarding copyright ownership. The ASF licenses this file |
| 12 | +// to you under the Apache License, Version 2.0 (the "License"); |
| 13 | +// you may not use this file except in compliance with the License. |
| 14 | +// You may obtain a copy of the License at |
| 15 | +// |
| 16 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | +// |
| 18 | +// Unless required by applicable law or agreed to in writing, |
| 19 | +// software distributed under the License is distributed on an |
| 20 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 21 | +// KIND, either express or implied. See the License for the |
| 22 | +// specific language governing permissions and limitations |
| 23 | +// under the License. |
| 24 | + |
| 25 | +#import "LPDatabase.h" |
| 26 | +#import "LPFileManager.h" |
| 27 | +#import "LeanplumInternal.h" |
| 28 | +#import "Constants.h" |
| 29 | +#import <sqlite3.h> |
| 30 | + |
| 31 | +@implementation LPDatabase |
| 32 | + |
| 33 | +/** |
| 34 | + * Create/Open SQLite database. |
| 35 | + */ |
| 36 | +- (id)init |
| 37 | +{ |
| 38 | + if (self = [super init]) { |
| 39 | + const char *sqliteFilePath = [[LPDatabase sqliteFilePath] UTF8String]; |
| 40 | + sqlite3 *sqlite; |
| 41 | + int result = sqlite3_open(sqliteFilePath, &sqlite); |
| 42 | + if (result != SQLITE_OK) { |
| 43 | + LPLog(LPError, @"Fail to open SQLite with result of %d", result); |
| 44 | + return self; |
| 45 | + } |
| 46 | + |
| 47 | + // Create tables. |
| 48 | + [self runQuery:@"CREATE TABLE IF NOT EXISTS event (" |
| 49 | + "id INTEGER PRIMARY KEY," |
| 50 | + "data TEXT" |
| 51 | + ");"]; |
| 52 | + NSLog(@"This is okay?"); |
| 53 | + } |
| 54 | + return self; |
| 55 | +} |
| 56 | + |
| 57 | ++ (LPDatabase *)database |
| 58 | +{ |
| 59 | + static id _database = nil; |
| 60 | + static dispatch_once_t databaseToken; |
| 61 | + dispatch_once(&databaseToken, ^{ |
| 62 | + _database = [self new]; |
| 63 | + }); |
| 64 | + return _database; |
| 65 | +} |
| 66 | + |
| 67 | ++ (NSString *)sqliteFilePath |
| 68 | +{ |
| 69 | + return [[LPFileManager documentsDirectory] stringByAppendingPathComponent:LEANPLUM_SQLITE_NAME]; |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Helper method that returns sqlite statement from query. |
| 74 | + * Used by both runQuery: and rowsFromQuery. |
| 75 | + */ |
| 76 | +- (sqlite3_stmt *)sqliteStatementFromQuery:(NSString *)query |
| 77 | +{ |
| 78 | + const char *sqliteFilePath = [[LPDatabase sqliteFilePath] UTF8String]; |
| 79 | + sqlite3 *sqlite; |
| 80 | + int result = sqlite3_open(sqliteFilePath, &sqlite); |
| 81 | + if (result != SQLITE_OK) { |
| 82 | + LPLog(LPError, @"Fail to open SQLite with result of %d", result); |
| 83 | + return nil; |
| 84 | + } |
| 85 | + |
| 86 | + sqlite3_stmt *statement; |
| 87 | + result = sqlite3_prepare_v2(sqlite, [query UTF8String], -1, &statement, NULL); |
| 88 | + if (result != SQLITE_OK) { |
| 89 | + LPLog(LPError, @"Preparing '%@': %s (%d)", query, sqlite3_errmsg(sqlite), |
| 90 | + result); |
| 91 | + return nil; |
| 92 | + } |
| 93 | + |
| 94 | + return statement; |
| 95 | +} |
| 96 | + |
| 97 | +- (void)runQuery:(NSString *)query |
| 98 | +{ |
| 99 | + sqlite3_stmt *statement = [self sqliteStatementFromQuery:query]; |
| 100 | + if (!statement) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + int result = sqlite3_step(statement); |
| 105 | + if (result != SQLITE_DONE) { |
| 106 | + LPLog(LPError, @"Fail to runQuery with result of %d", result); |
| 107 | + } |
| 108 | + sqlite3_finalize(statement); |
| 109 | +} |
| 110 | + |
| 111 | +- (NSArray *)rowsFromQuery:(NSString *)query |
| 112 | +{ |
| 113 | + NSMutableArray *rows = [NSMutableArray new]; |
| 114 | + sqlite3_stmt *statement = [self sqliteStatementFromQuery:query]; |
| 115 | + if (!statement) { |
| 116 | + return @[]; |
| 117 | + } |
| 118 | + |
| 119 | + // Iterate through rows. |
| 120 | + while (sqlite3_step(statement) == SQLITE_ROW) { |
| 121 | + // Get column data as dictionary where column name is the key |
| 122 | + // and value will be a string. This is a safe conversion. |
| 123 | + // For more details: http://www.sqlite.org/c3ref/column_blob.html |
| 124 | + NSMutableDictionary *columnData = [NSMutableDictionary new]; |
| 125 | + int columnsCount = sqlite3_column_count(statement); |
| 126 | + for (int i=0; i<columnsCount; i++){ |
| 127 | + char *columnKeyUTF8 = (char *)sqlite3_column_name(statement, i); |
| 128 | + NSString *columnKey = [NSString stringWithUTF8String:columnKeyUTF8]; |
| 129 | + |
| 130 | + char *columnValueUTF8 = (char *)sqlite3_column_text(statement, i); |
| 131 | + NSString *columnValue = [NSString stringWithUTF8String:columnValueUTF8]; |
| 132 | + |
| 133 | + columnData[columnKey] = columnValue; |
| 134 | + } |
| 135 | + [rows addObject:columnData]; |
| 136 | + } |
| 137 | + sqlite3_finalize(statement); |
| 138 | + |
| 139 | + return rows; |
| 140 | +} |
| 141 | + |
| 142 | +@end |
0 commit comments