├── .gitignore ├── Example ├── Podfile ├── Podfile.lock ├── Pods │ ├── FMDB │ │ ├── LICENSE.txt │ │ ├── README.markdown │ │ └── src │ │ │ └── fmdb │ │ │ ├── FMDB.h │ │ │ ├── FMDatabase.h │ │ │ ├── FMDatabase.m │ │ │ ├── FMDatabaseAdditions.h │ │ │ ├── FMDatabaseAdditions.m │ │ │ ├── FMDatabasePool.h │ │ │ ├── FMDatabasePool.m │ │ │ ├── FMDatabaseQueue.h │ │ │ ├── FMDatabaseQueue.m │ │ │ ├── FMResultSet.h │ │ │ └── FMResultSet.m │ ├── Local Podspecs │ │ └── XWDatabase.podspec.json │ ├── Manifest.lock │ ├── Pods.xcodeproj │ │ └── project.pbxproj │ └── Target Support Files │ │ ├── FMDB │ │ ├── FMDB-Info.plist │ │ ├── FMDB-dummy.m │ │ ├── FMDB-prefix.pch │ │ ├── FMDB-umbrella.h │ │ ├── FMDB.modulemap │ │ └── FMDB.xcconfig │ │ ├── Pods-XWDatabase_Example │ │ ├── Pods-XWDatabase_Example-Info.plist │ │ ├── Pods-XWDatabase_Example-acknowledgements.markdown │ │ ├── Pods-XWDatabase_Example-acknowledgements.plist │ │ ├── Pods-XWDatabase_Example-dummy.m │ │ ├── Pods-XWDatabase_Example-frameworks.sh │ │ ├── Pods-XWDatabase_Example-umbrella.h │ │ ├── Pods-XWDatabase_Example.debug.xcconfig │ │ ├── Pods-XWDatabase_Example.modulemap │ │ └── Pods-XWDatabase_Example.release.xcconfig │ │ ├── Pods-XWDatabase_Tests │ │ ├── Pods-XWDatabase_Tests-Info.plist │ │ ├── Pods-XWDatabase_Tests-acknowledgements.markdown │ │ ├── Pods-XWDatabase_Tests-acknowledgements.plist │ │ ├── Pods-XWDatabase_Tests-dummy.m │ │ ├── Pods-XWDatabase_Tests-frameworks.sh │ │ ├── Pods-XWDatabase_Tests-umbrella.h │ │ ├── Pods-XWDatabase_Tests.debug.xcconfig │ │ ├── Pods-XWDatabase_Tests.modulemap │ │ └── Pods-XWDatabase_Tests.release.xcconfig │ │ └── XWDatabase │ │ ├── XWDatabase-Info.plist │ │ ├── XWDatabase-dummy.m │ │ ├── XWDatabase-prefix.pch │ │ ├── XWDatabase-umbrella.h │ │ ├── XWDatabase.modulemap │ │ └── XWDatabase.xcconfig ├── Tests │ ├── Tests-Info.plist │ ├── Tests-Prefix.pch │ ├── Tests.m │ └── en.lproj │ │ └── InfoPlist.strings ├── XWDatabase.xcodeproj │ ├── project.pbxproj │ ├── project.xcworkspace │ │ └── contents.xcworkspacedata │ └── xcshareddata │ │ └── xcschemes │ │ └── XWDatabase-Example.xcscheme ├── XWDatabase.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── XWDatabase │ ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard │ ├── Images.xcassets │ ├── AppIcon.appiconset │ │ ├── Contents.json │ │ ├── icon-1024.png │ │ ├── icon-20-ipad.png │ │ ├── icon-20@2x-ipad.png │ │ ├── icon-20@2x.png │ │ ├── icon-20@3x.png │ │ ├── icon-29-ipad.png │ │ ├── icon-29.png │ │ ├── icon-29@2x-ipad.png │ │ ├── icon-29@2x.png │ │ ├── icon-29@3x.png │ │ ├── icon-40.png │ │ ├── icon-40@2x.png │ │ ├── icon-40@3x.png │ │ ├── icon-50.png │ │ ├── icon-50@2x.png │ │ ├── icon-57.png │ │ ├── icon-57@2x.png │ │ ├── icon-60@2x.png │ │ ├── icon-60@3x.png │ │ ├── icon-72.png │ │ ├── icon-72@2x.png │ │ ├── icon-76.png │ │ ├── icon-76@2x.png │ │ └── icon-83.5@2x.png │ ├── Contents.json │ ├── icon.imageset │ │ ├── Contents.json │ │ └── icon.jpg │ └── icon_comment_owner.imageset │ │ ├── Contents.json │ │ └── icon_comment_owner@2x.png │ ├── Model │ ├── XWBook.h │ ├── XWBook.m │ ├── XWImage.h │ ├── XWImage.m │ ├── XWPerson.h │ ├── XWPerson.m │ ├── XWTestModel.h │ ├── XWTestModel.m │ ├── XWTestSubModel.h │ └── XWTestSubModel.m │ ├── XWAppDelegate.h │ ├── XWAppDelegate.m │ ├── XWDatabase-Info.plist │ ├── XWDatabase-Prefix.pch │ ├── XWViewController.h │ ├── XWViewController.m │ ├── en.lproj │ └── InfoPlist.strings │ └── main.m ├── LICENSE ├── README.md ├── XWDatabase.podspec ├── XWDatabase ├── Assets │ └── .gitkeep └── Classes │ ├── .gitkeep │ ├── NSObject+XWModel.h │ ├── NSObject+XWModel.m │ ├── XWDatabase.h │ ├── XWDatabase.m │ ├── XWDatabaseDataModel.h │ ├── XWDatabaseDataModel.m │ ├── XWDatabaseModel.h │ ├── XWDatabaseModel.m │ ├── XWDatabaseModelProtocol.h │ ├── XWDatabaseQueue.h │ ├── XWDatabaseQueue.m │ ├── XWDatabaseSQL.h │ ├── XWDatabaseSQL.m │ ├── XWLivingThread.h │ └── XWLivingThread.m └── _Pods.xcodeproj /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | # CocoaPods 32 | # 33 | # We recommend against adding the Pods directory to your .gitignore. However 34 | # you should judge for yourself, the pros and cons are mentioned at: 35 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 36 | # 37 | # Pods/ 38 | 39 | # Carthage 40 | # 41 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 42 | # Carthage/Checkouts 43 | 44 | Carthage/Build 45 | 46 | # fastlane 47 | # 48 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 49 | # screenshots whenever they are needed. 50 | # For more information about the recommended setup visit: 51 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 52 | 53 | fastlane/report.xml 54 | fastlane/Preview.html 55 | fastlane/screenshots/**/*.png 56 | fastlane/test_output 57 | 58 | # Code Injection 59 | # 60 | # After new code Injection tools there's a generated folder /iOSInjectionProject 61 | # https://github.com/johnno1962/injectionforxcode 62 | 63 | iOSInjectionProject/ 64 | -------------------------------------------------------------------------------- /Example/Podfile: -------------------------------------------------------------------------------- 1 | use_frameworks! 2 | 3 | platform :ios, '8.0' 4 | 5 | target 'XWDatabase_Example' do 6 | 7 | pod 'XWDatabase', :path => '../' 8 | 9 | # pod 'FMDB' 10 | 11 | target 'XWDatabase_Tests' do 12 | inherit! :search_paths 13 | 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /Example/Podfile.lock: -------------------------------------------------------------------------------- 1 | PODS: 2 | - FMDB (2.7.5): 3 | - FMDB/standard (= 2.7.5) 4 | - FMDB/standard (2.7.5) 5 | - XWDatabase (1.2.2): 6 | - FMDB 7 | 8 | DEPENDENCIES: 9 | - XWDatabase (from `../`) 10 | 11 | SPEC REPOS: 12 | https://github.com/cocoapods/specs.git: 13 | - FMDB 14 | 15 | EXTERNAL SOURCES: 16 | XWDatabase: 17 | :path: "../" 18 | 19 | SPEC CHECKSUMS: 20 | FMDB: 2ce00b547f966261cd18927a3ddb07cb6f3db82a 21 | XWDatabase: 538a3c6c0905c0c5cbe52897fa003bb689c5f286 22 | 23 | PODFILE CHECKSUM: b70233fb56a4a47f18e103190428d4c38af38bfb 24 | 25 | COCOAPODS: 1.7.0.rc.1 26 | -------------------------------------------------------------------------------- /Example/Pods/FMDB/LICENSE.txt: -------------------------------------------------------------------------------- 1 | If you are using FMDB in your project, I'd love to hear about it. Let Gus know 2 | by sending an email to gus@flyingmeat.com. 3 | 4 | And if you happen to come across either Gus Mueller or Rob Ryan in a bar, you 5 | might consider purchasing a drink of their choosing if FMDB has been useful to 6 | you. 7 | 8 | Finally, and shortly, this is the MIT License. 9 | 10 | Copyright (c) 2008-2014 Flying Meat Inc. 11 | 12 | Permission is hereby granted, free of charge, to any person obtaining a copy 13 | of this software and associated documentation files (the "Software"), to deal 14 | in the Software without restriction, including without limitation the rights 15 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 | copies of the Software, and to permit persons to whom the Software is 17 | furnished to do so, subject to the following conditions: 18 | 19 | The above copyright notice and this permission notice shall be included in 20 | all copies or substantial portions of the Software. 21 | 22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 28 | THE SOFTWARE. -------------------------------------------------------------------------------- /Example/Pods/FMDB/src/fmdb/FMDB.h: -------------------------------------------------------------------------------- 1 | #import 2 | 3 | FOUNDATION_EXPORT double FMDBVersionNumber; 4 | FOUNDATION_EXPORT const unsigned char FMDBVersionString[]; 5 | 6 | #import "FMDatabase.h" 7 | #import "FMResultSet.h" 8 | #import "FMDatabaseAdditions.h" 9 | #import "FMDatabaseQueue.h" 10 | #import "FMDatabasePool.h" 11 | -------------------------------------------------------------------------------- /Example/Pods/FMDB/src/fmdb/FMDatabaseAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // FMDatabaseAdditions.h 3 | // fmdb 4 | // 5 | // Created by August Mueller on 10/30/05. 6 | // Copyright 2005 Flying Meat Inc.. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "FMDatabase.h" 11 | 12 | NS_ASSUME_NONNULL_BEGIN 13 | 14 | /** Category of additions for `` class. 15 | 16 | ### See also 17 | 18 | - `` 19 | */ 20 | 21 | @interface FMDatabase (FMDatabaseAdditions) 22 | 23 | ///---------------------------------------- 24 | /// @name Return results of SQL to variable 25 | ///---------------------------------------- 26 | 27 | /** Return `int` value for query 28 | 29 | @param query The SQL query to be performed. 30 | @param ... A list of parameters that will be bound to the `?` placeholders in the SQL query. 31 | 32 | @return `int` value. 33 | 34 | @note This is not available from Swift. 35 | */ 36 | 37 | - (int)intForQuery:(NSString*)query, ...; 38 | 39 | /** Return `long` value for query 40 | 41 | @param query The SQL query to be performed. 42 | @param ... A list of parameters that will be bound to the `?` placeholders in the SQL query. 43 | 44 | @return `long` value. 45 | 46 | @note This is not available from Swift. 47 | */ 48 | 49 | - (long)longForQuery:(NSString*)query, ...; 50 | 51 | /** Return `BOOL` value for query 52 | 53 | @param query The SQL query to be performed. 54 | @param ... A list of parameters that will be bound to the `?` placeholders in the SQL query. 55 | 56 | @return `BOOL` value. 57 | 58 | @note This is not available from Swift. 59 | */ 60 | 61 | - (BOOL)boolForQuery:(NSString*)query, ...; 62 | 63 | /** Return `double` value for query 64 | 65 | @param query The SQL query to be performed. 66 | @param ... A list of parameters that will be bound to the `?` placeholders in the SQL query. 67 | 68 | @return `double` value. 69 | 70 | @note This is not available from Swift. 71 | */ 72 | 73 | - (double)doubleForQuery:(NSString*)query, ...; 74 | 75 | /** Return `NSString` value for query 76 | 77 | @param query The SQL query to be performed. 78 | @param ... A list of parameters that will be bound to the `?` placeholders in the SQL query. 79 | 80 | @return `NSString` value. 81 | 82 | @note This is not available from Swift. 83 | */ 84 | 85 | - (NSString * _Nullable)stringForQuery:(NSString*)query, ...; 86 | 87 | /** Return `NSData` value for query 88 | 89 | @param query The SQL query to be performed. 90 | @param ... A list of parameters that will be bound to the `?` placeholders in the SQL query. 91 | 92 | @return `NSData` value. 93 | 94 | @note This is not available from Swift. 95 | */ 96 | 97 | - (NSData * _Nullable)dataForQuery:(NSString*)query, ...; 98 | 99 | /** Return `NSDate` value for query 100 | 101 | @param query The SQL query to be performed. 102 | @param ... A list of parameters that will be bound to the `?` placeholders in the SQL query. 103 | 104 | @return `NSDate` value. 105 | 106 | @note This is not available from Swift. 107 | */ 108 | 109 | - (NSDate * _Nullable)dateForQuery:(NSString*)query, ...; 110 | 111 | 112 | // Notice that there's no dataNoCopyForQuery:. 113 | // That would be a bad idea, because we close out the result set, and then what 114 | // happens to the data that we just didn't copy? Who knows, not I. 115 | 116 | 117 | ///-------------------------------- 118 | /// @name Schema related operations 119 | ///-------------------------------- 120 | 121 | /** Does table exist in database? 122 | 123 | @param tableName The name of the table being looked for. 124 | 125 | @return `YES` if table found; `NO` if not found. 126 | */ 127 | 128 | - (BOOL)tableExists:(NSString*)tableName; 129 | 130 | /** The schema of the database. 131 | 132 | This will be the schema for the entire database. For each entity, each row of the result set will include the following fields: 133 | 134 | - `type` - The type of entity (e.g. table, index, view, or trigger) 135 | - `name` - The name of the object 136 | - `tbl_name` - The name of the table to which the object references 137 | - `rootpage` - The page number of the root b-tree page for tables and indices 138 | - `sql` - The SQL that created the entity 139 | 140 | @return `FMResultSet` of schema; `nil` on error. 141 | 142 | @see [SQLite File Format](http://www.sqlite.org/fileformat.html) 143 | */ 144 | 145 | - (FMResultSet * _Nullable)getSchema; 146 | 147 | /** The schema of the database. 148 | 149 | This will be the schema for a particular table as report by SQLite `PRAGMA`, for example: 150 | 151 | PRAGMA table_info('employees') 152 | 153 | This will report: 154 | 155 | - `cid` - The column ID number 156 | - `name` - The name of the column 157 | - `type` - The data type specified for the column 158 | - `notnull` - whether the field is defined as NOT NULL (i.e. values required) 159 | - `dflt_value` - The default value for the column 160 | - `pk` - Whether the field is part of the primary key of the table 161 | 162 | @param tableName The name of the table for whom the schema will be returned. 163 | 164 | @return `FMResultSet` of schema; `nil` on error. 165 | 166 | @see [table_info](http://www.sqlite.org/pragma.html#pragma_table_info) 167 | */ 168 | 169 | - (FMResultSet * _Nullable)getTableSchema:(NSString*)tableName; 170 | 171 | /** Test to see if particular column exists for particular table in database 172 | 173 | @param columnName The name of the column. 174 | 175 | @param tableName The name of the table. 176 | 177 | @return `YES` if column exists in table in question; `NO` otherwise. 178 | */ 179 | 180 | - (BOOL)columnExists:(NSString*)columnName inTableWithName:(NSString*)tableName; 181 | 182 | /** Test to see if particular column exists for particular table in database 183 | 184 | @param columnName The name of the column. 185 | 186 | @param tableName The name of the table. 187 | 188 | @return `YES` if column exists in table in question; `NO` otherwise. 189 | 190 | @see columnExists:inTableWithName: 191 | 192 | @warning Deprecated - use `` instead. 193 | */ 194 | 195 | - (BOOL)columnExists:(NSString*)tableName columnName:(NSString*)columnName __deprecated_msg("Use columnExists:inTableWithName: instead"); 196 | 197 | 198 | /** Validate SQL statement 199 | 200 | This validates SQL statement by performing `sqlite3_prepare_v2`, but not returning the results, but instead immediately calling `sqlite3_finalize`. 201 | 202 | @param sql The SQL statement being validated. 203 | 204 | @param error This is a pointer to a `NSError` object that will receive the autoreleased `NSError` object if there was any error. If this is `nil`, no `NSError` result will be returned. 205 | 206 | @return `YES` if validation succeeded without incident; `NO` otherwise. 207 | 208 | */ 209 | 210 | - (BOOL)validateSQL:(NSString*)sql error:(NSError * _Nullable *)error; 211 | 212 | 213 | ///----------------------------------- 214 | /// @name Application identifier tasks 215 | ///----------------------------------- 216 | 217 | /** Retrieve application ID 218 | 219 | @return The `uint32_t` numeric value of the application ID. 220 | 221 | @see setApplicationID: 222 | */ 223 | 224 | @property (nonatomic) uint32_t applicationID; 225 | 226 | #if TARGET_OS_MAC && !TARGET_OS_IPHONE 227 | 228 | /** Retrieve application ID string 229 | 230 | @see setApplicationIDString: 231 | */ 232 | 233 | @property (nonatomic, retain) NSString *applicationIDString; 234 | 235 | #endif 236 | 237 | ///----------------------------------- 238 | /// @name user version identifier tasks 239 | ///----------------------------------- 240 | 241 | /** Retrieve user version 242 | 243 | @see setUserVersion: 244 | */ 245 | 246 | @property (nonatomic) uint32_t userVersion; 247 | 248 | @end 249 | 250 | NS_ASSUME_NONNULL_END 251 | -------------------------------------------------------------------------------- /Example/Pods/FMDB/src/fmdb/FMDatabaseAdditions.m: -------------------------------------------------------------------------------- 1 | // 2 | // FMDatabaseAdditions.m 3 | // fmdb 4 | // 5 | // Created by August Mueller on 10/30/05. 6 | // Copyright 2005 Flying Meat Inc.. All rights reserved. 7 | // 8 | 9 | #import "FMDatabase.h" 10 | #import "FMDatabaseAdditions.h" 11 | #import "TargetConditionals.h" 12 | 13 | #if FMDB_SQLITE_STANDALONE 14 | #import 15 | #else 16 | #import 17 | #endif 18 | 19 | @interface FMDatabase (PrivateStuff) 20 | - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray * _Nullable)arrayArgs orDictionary:(NSDictionary * _Nullable)dictionaryArgs orVAList:(va_list)args; 21 | @end 22 | 23 | @implementation FMDatabase (FMDatabaseAdditions) 24 | 25 | #define RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(type, sel) \ 26 | va_list args; \ 27 | va_start(args, query); \ 28 | FMResultSet *resultSet = [self executeQuery:query withArgumentsInArray:0x00 orDictionary:0x00 orVAList:args]; \ 29 | va_end(args); \ 30 | if (![resultSet next]) { return (type)0; } \ 31 | type ret = [resultSet sel:0]; \ 32 | [resultSet close]; \ 33 | [resultSet setParentDB:nil]; \ 34 | return ret; 35 | 36 | 37 | - (NSString *)stringForQuery:(NSString*)query, ... { 38 | RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(NSString *, stringForColumnIndex); 39 | } 40 | 41 | - (int)intForQuery:(NSString*)query, ... { 42 | RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(int, intForColumnIndex); 43 | } 44 | 45 | - (long)longForQuery:(NSString*)query, ... { 46 | RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(long, longForColumnIndex); 47 | } 48 | 49 | - (BOOL)boolForQuery:(NSString*)query, ... { 50 | RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(BOOL, boolForColumnIndex); 51 | } 52 | 53 | - (double)doubleForQuery:(NSString*)query, ... { 54 | RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(double, doubleForColumnIndex); 55 | } 56 | 57 | - (NSData*)dataForQuery:(NSString*)query, ... { 58 | RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(NSData *, dataForColumnIndex); 59 | } 60 | 61 | - (NSDate*)dateForQuery:(NSString*)query, ... { 62 | RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(NSDate *, dateForColumnIndex); 63 | } 64 | 65 | 66 | - (BOOL)tableExists:(NSString*)tableName { 67 | 68 | tableName = [tableName lowercaseString]; 69 | 70 | FMResultSet *rs = [self executeQuery:@"select [sql] from sqlite_master where [type] = 'table' and lower(name) = ?", tableName]; 71 | 72 | //if at least one next exists, table exists 73 | BOOL returnBool = [rs next]; 74 | 75 | //close and free object 76 | [rs close]; 77 | 78 | return returnBool; 79 | } 80 | 81 | /* 82 | get table with list of tables: result colums: type[STRING], name[STRING],tbl_name[STRING],rootpage[INTEGER],sql[STRING] 83 | check if table exist in database (patch from OZLB) 84 | */ 85 | - (FMResultSet * _Nullable)getSchema { 86 | 87 | //result colums: type[STRING], name[STRING],tbl_name[STRING],rootpage[INTEGER],sql[STRING] 88 | FMResultSet *rs = [self executeQuery:@"SELECT type, name, tbl_name, rootpage, sql FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type != 'meta' AND name NOT LIKE 'sqlite_%' ORDER BY tbl_name, type DESC, name"]; 89 | 90 | return rs; 91 | } 92 | 93 | /* 94 | get table schema: result colums: cid[INTEGER], name,type [STRING], notnull[INTEGER], dflt_value[],pk[INTEGER] 95 | */ 96 | - (FMResultSet * _Nullable)getTableSchema:(NSString*)tableName { 97 | 98 | //result colums: cid[INTEGER], name,type [STRING], notnull[INTEGER], dflt_value[],pk[INTEGER] 99 | FMResultSet *rs = [self executeQuery:[NSString stringWithFormat: @"pragma table_info('%@')", tableName]]; 100 | 101 | return rs; 102 | } 103 | 104 | - (BOOL)columnExists:(NSString*)columnName inTableWithName:(NSString*)tableName { 105 | 106 | BOOL returnBool = NO; 107 | 108 | tableName = [tableName lowercaseString]; 109 | columnName = [columnName lowercaseString]; 110 | 111 | FMResultSet *rs = [self getTableSchema:tableName]; 112 | 113 | //check if column is present in table schema 114 | while ([rs next]) { 115 | if ([[[rs stringForColumn:@"name"] lowercaseString] isEqualToString:columnName]) { 116 | returnBool = YES; 117 | break; 118 | } 119 | } 120 | 121 | //If this is not done FMDatabase instance stays out of pool 122 | [rs close]; 123 | 124 | return returnBool; 125 | } 126 | 127 | 128 | 129 | - (uint32_t)applicationID { 130 | #if SQLITE_VERSION_NUMBER >= 3007017 131 | uint32_t r = 0; 132 | 133 | FMResultSet *rs = [self executeQuery:@"pragma application_id"]; 134 | 135 | if ([rs next]) { 136 | r = (uint32_t)[rs longLongIntForColumnIndex:0]; 137 | } 138 | 139 | [rs close]; 140 | 141 | return r; 142 | #else 143 | NSString *errorMessage = NSLocalizedStringFromTable(@"Application ID functions require SQLite 3.7.17", @"FMDB", nil); 144 | if (self.logsErrors) NSLog(@"%@", errorMessage); 145 | return 0; 146 | #endif 147 | } 148 | 149 | - (void)setApplicationID:(uint32_t)appID { 150 | #if SQLITE_VERSION_NUMBER >= 3007017 151 | NSString *query = [NSString stringWithFormat:@"pragma application_id=%d", appID]; 152 | FMResultSet *rs = [self executeQuery:query]; 153 | [rs next]; 154 | [rs close]; 155 | #else 156 | NSString *errorMessage = NSLocalizedStringFromTable(@"Application ID functions require SQLite 3.7.17", @"FMDB", nil); 157 | if (self.logsErrors) NSLog(@"%@", errorMessage); 158 | #endif 159 | } 160 | 161 | 162 | #if TARGET_OS_MAC && !TARGET_OS_IPHONE 163 | 164 | - (NSString*)applicationIDString { 165 | #if SQLITE_VERSION_NUMBER >= 3007017 166 | NSString *s = NSFileTypeForHFSTypeCode([self applicationID]); 167 | 168 | assert([s length] == 6); 169 | 170 | s = [s substringWithRange:NSMakeRange(1, 4)]; 171 | 172 | 173 | return s; 174 | #else 175 | NSString *errorMessage = NSLocalizedStringFromTable(@"Application ID functions require SQLite 3.7.17", @"FMDB", nil); 176 | if (self.logsErrors) NSLog(@"%@", errorMessage); 177 | return nil; 178 | #endif 179 | } 180 | 181 | - (void)setApplicationIDString:(NSString*)s { 182 | #if SQLITE_VERSION_NUMBER >= 3007017 183 | if ([s length] != 4) { 184 | NSLog(@"setApplicationIDString: string passed is not exactly 4 chars long. (was %ld)", [s length]); 185 | } 186 | 187 | [self setApplicationID:NSHFSTypeCodeFromFileType([NSString stringWithFormat:@"'%@'", s])]; 188 | #else 189 | NSString *errorMessage = NSLocalizedStringFromTable(@"Application ID functions require SQLite 3.7.17", @"FMDB", nil); 190 | if (self.logsErrors) NSLog(@"%@", errorMessage); 191 | #endif 192 | } 193 | 194 | #endif 195 | 196 | - (uint32_t)userVersion { 197 | uint32_t r = 0; 198 | 199 | FMResultSet *rs = [self executeQuery:@"pragma user_version"]; 200 | 201 | if ([rs next]) { 202 | r = (uint32_t)[rs longLongIntForColumnIndex:0]; 203 | } 204 | 205 | [rs close]; 206 | return r; 207 | } 208 | 209 | - (void)setUserVersion:(uint32_t)version { 210 | NSString *query = [NSString stringWithFormat:@"pragma user_version = %d", version]; 211 | FMResultSet *rs = [self executeQuery:query]; 212 | [rs next]; 213 | [rs close]; 214 | } 215 | 216 | #pragma clang diagnostic push 217 | #pragma clang diagnostic ignored "-Wdeprecated-implementations" 218 | 219 | - (BOOL)columnExists:(NSString*)tableName columnName:(NSString*)columnName __attribute__ ((deprecated)) { 220 | return [self columnExists:columnName inTableWithName:tableName]; 221 | } 222 | 223 | #pragma clang diagnostic pop 224 | 225 | - (BOOL)validateSQL:(NSString*)sql error:(NSError**)error { 226 | sqlite3_stmt *pStmt = NULL; 227 | BOOL validationSucceeded = YES; 228 | 229 | int rc = sqlite3_prepare_v2([self sqliteHandle], [sql UTF8String], -1, &pStmt, 0); 230 | if (rc != SQLITE_OK) { 231 | validationSucceeded = NO; 232 | if (error) { 233 | *error = [NSError errorWithDomain:NSCocoaErrorDomain 234 | code:[self lastErrorCode] 235 | userInfo:[NSDictionary dictionaryWithObject:[self lastErrorMessage] 236 | forKey:NSLocalizedDescriptionKey]]; 237 | } 238 | } 239 | 240 | sqlite3_finalize(pStmt); 241 | 242 | return validationSucceeded; 243 | } 244 | 245 | @end 246 | -------------------------------------------------------------------------------- /Example/Pods/FMDB/src/fmdb/FMDatabasePool.h: -------------------------------------------------------------------------------- 1 | // 2 | // FMDatabasePool.h 3 | // fmdb 4 | // 5 | // Created by August Mueller on 6/22/11. 6 | // Copyright 2011 Flying Meat Inc. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @class FMDatabase; 14 | 15 | /** Pool of `` objects. 16 | 17 | ### See also 18 | 19 | - `` 20 | - `` 21 | 22 | @warning Before using `FMDatabasePool`, please consider using `` instead. 23 | 24 | If you really really really know what you're doing and `FMDatabasePool` is what 25 | you really really need (ie, you're using a read only database), OK you can use 26 | it. But just be careful not to deadlock! 27 | 28 | For an example on deadlocking, search for: 29 | `ONLY_USE_THE_POOL_IF_YOU_ARE_DOING_READS_OTHERWISE_YOULL_DEADLOCK_USE_FMDATABASEQUEUE_INSTEAD` 30 | in the main.m file. 31 | */ 32 | 33 | @interface FMDatabasePool : NSObject 34 | 35 | /** Database path */ 36 | 37 | @property (atomic, copy, nullable) NSString *path; 38 | 39 | /** Delegate object */ 40 | 41 | @property (atomic, assign, nullable) id delegate; 42 | 43 | /** Maximum number of databases to create */ 44 | 45 | @property (atomic, assign) NSUInteger maximumNumberOfDatabasesToCreate; 46 | 47 | /** Open flags */ 48 | 49 | @property (atomic, readonly) int openFlags; 50 | 51 | /** Custom virtual file system name */ 52 | 53 | @property (atomic, copy, nullable) NSString *vfsName; 54 | 55 | 56 | ///--------------------- 57 | /// @name Initialization 58 | ///--------------------- 59 | 60 | /** Create pool using path. 61 | 62 | @param aPath The file path of the database. 63 | 64 | @return The `FMDatabasePool` object. `nil` on error. 65 | */ 66 | 67 | + (instancetype)databasePoolWithPath:(NSString * _Nullable)aPath; 68 | 69 | /** Create pool using file URL. 70 | 71 | @param url The file `NSURL` of the database. 72 | 73 | @return The `FMDatabasePool` object. `nil` on error. 74 | */ 75 | 76 | + (instancetype)databasePoolWithURL:(NSURL * _Nullable)url; 77 | 78 | /** Create pool using path and specified flags 79 | 80 | @param aPath The file path of the database. 81 | @param openFlags Flags passed to the openWithFlags method of the database. 82 | 83 | @return The `FMDatabasePool` object. `nil` on error. 84 | */ 85 | 86 | + (instancetype)databasePoolWithPath:(NSString * _Nullable)aPath flags:(int)openFlags; 87 | 88 | /** Create pool using file URL and specified flags 89 | 90 | @param url The file `NSURL` of the database. 91 | @param openFlags Flags passed to the openWithFlags method of the database. 92 | 93 | @return The `FMDatabasePool` object. `nil` on error. 94 | */ 95 | 96 | + (instancetype)databasePoolWithURL:(NSURL * _Nullable)url flags:(int)openFlags; 97 | 98 | /** Create pool using path. 99 | 100 | @param aPath The file path of the database. 101 | 102 | @return The `FMDatabasePool` object. `nil` on error. 103 | */ 104 | 105 | - (instancetype)initWithPath:(NSString * _Nullable)aPath; 106 | 107 | /** Create pool using file URL. 108 | 109 | @param url The file `NSURL of the database. 110 | 111 | @return The `FMDatabasePool` object. `nil` on error. 112 | */ 113 | 114 | - (instancetype)initWithURL:(NSURL * _Nullable)url; 115 | 116 | /** Create pool using path and specified flags. 117 | 118 | @param aPath The file path of the database. 119 | @param openFlags Flags passed to the openWithFlags method of the database 120 | 121 | @return The `FMDatabasePool` object. `nil` on error. 122 | */ 123 | 124 | - (instancetype)initWithPath:(NSString * _Nullable)aPath flags:(int)openFlags; 125 | 126 | /** Create pool using file URL and specified flags. 127 | 128 | @param url The file `NSURL` of the database. 129 | @param openFlags Flags passed to the openWithFlags method of the database 130 | 131 | @return The `FMDatabasePool` object. `nil` on error. 132 | */ 133 | 134 | - (instancetype)initWithURL:(NSURL * _Nullable)url flags:(int)openFlags; 135 | 136 | /** Create pool using path and specified flags. 137 | 138 | @param aPath The file path of the database. 139 | @param openFlags Flags passed to the openWithFlags method of the database 140 | @param vfsName The name of a custom virtual file system 141 | 142 | @return The `FMDatabasePool` object. `nil` on error. 143 | */ 144 | 145 | - (instancetype)initWithPath:(NSString * _Nullable)aPath flags:(int)openFlags vfs:(NSString * _Nullable)vfsName; 146 | 147 | /** Create pool using file URL and specified flags. 148 | 149 | @param url The file `NSURL` of the database. 150 | @param openFlags Flags passed to the openWithFlags method of the database 151 | @param vfsName The name of a custom virtual file system 152 | 153 | @return The `FMDatabasePool` object. `nil` on error. 154 | */ 155 | 156 | - (instancetype)initWithURL:(NSURL * _Nullable)url flags:(int)openFlags vfs:(NSString * _Nullable)vfsName; 157 | 158 | /** Returns the Class of 'FMDatabase' subclass, that will be used to instantiate database object. 159 | 160 | Subclasses can override this method to return specified Class of 'FMDatabase' subclass. 161 | 162 | @return The Class of 'FMDatabase' subclass, that will be used to instantiate database object. 163 | */ 164 | 165 | + (Class)databaseClass; 166 | 167 | ///------------------------------------------------ 168 | /// @name Keeping track of checked in/out databases 169 | ///------------------------------------------------ 170 | 171 | /** Number of checked-in databases in pool 172 | */ 173 | 174 | @property (nonatomic, readonly) NSUInteger countOfCheckedInDatabases; 175 | 176 | /** Number of checked-out databases in pool 177 | */ 178 | 179 | @property (nonatomic, readonly) NSUInteger countOfCheckedOutDatabases; 180 | 181 | /** Total number of databases in pool 182 | */ 183 | 184 | @property (nonatomic, readonly) NSUInteger countOfOpenDatabases; 185 | 186 | /** Release all databases in pool */ 187 | 188 | - (void)releaseAllDatabases; 189 | 190 | ///------------------------------------------ 191 | /// @name Perform database operations in pool 192 | ///------------------------------------------ 193 | 194 | /** Synchronously perform database operations in pool. 195 | 196 | @param block The code to be run on the `FMDatabasePool` pool. 197 | */ 198 | 199 | - (void)inDatabase:(__attribute__((noescape)) void (^)(FMDatabase *db))block; 200 | 201 | /** Synchronously perform database operations in pool using transaction. 202 | 203 | @param block The code to be run on the `FMDatabasePool` pool. 204 | 205 | @warning Unlike SQLite's `BEGIN TRANSACTION`, this method currently performs 206 | an exclusive transaction, not a deferred transaction. This behavior 207 | is likely to change in future versions of FMDB, whereby this method 208 | will likely eventually adopt standard SQLite behavior and perform 209 | deferred transactions. If you really need exclusive tranaction, it is 210 | recommended that you use `inExclusiveTransaction`, instead, not only 211 | to make your intent explicit, but also to future-proof your code. 212 | */ 213 | 214 | - (void)inTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block; 215 | 216 | /** Synchronously perform database operations in pool using exclusive transaction. 217 | 218 | @param block The code to be run on the `FMDatabasePool` pool. 219 | */ 220 | 221 | - (void)inExclusiveTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block; 222 | 223 | /** Synchronously perform database operations in pool using deferred transaction. 224 | 225 | @param block The code to be run on the `FMDatabasePool` pool. 226 | */ 227 | 228 | - (void)inDeferredTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block; 229 | 230 | /** Synchronously perform database operations on queue, using immediate transactions. 231 | 232 | @param block The code to be run on the queue of `FMDatabaseQueue` 233 | */ 234 | 235 | - (void)inImmediateTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block; 236 | 237 | /** Synchronously perform database operations in pool using save point. 238 | 239 | @param block The code to be run on the `FMDatabasePool` pool. 240 | 241 | @return `NSError` object if error; `nil` if successful. 242 | 243 | @warning You can not nest these, since calling it will pull another database out of the pool and you'll get a deadlock. If you need to nest, use `<[FMDatabase startSavePointWithName:error:]>` instead. 244 | */ 245 | 246 | - (NSError * _Nullable)inSavePoint:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block; 247 | 248 | @end 249 | 250 | 251 | /** FMDatabasePool delegate category 252 | 253 | This is a category that defines the protocol for the FMDatabasePool delegate 254 | */ 255 | 256 | @interface NSObject (FMDatabasePoolDelegate) 257 | 258 | /** Asks the delegate whether database should be added to the pool. 259 | 260 | @param pool The `FMDatabasePool` object. 261 | @param database The `FMDatabase` object. 262 | 263 | @return `YES` if it should add database to pool; `NO` if not. 264 | 265 | */ 266 | 267 | - (BOOL)databasePool:(FMDatabasePool*)pool shouldAddDatabaseToPool:(FMDatabase*)database; 268 | 269 | /** Tells the delegate that database was added to the pool. 270 | 271 | @param pool The `FMDatabasePool` object. 272 | @param database The `FMDatabase` object. 273 | 274 | */ 275 | 276 | - (void)databasePool:(FMDatabasePool*)pool didAddDatabase:(FMDatabase*)database; 277 | 278 | @end 279 | 280 | NS_ASSUME_NONNULL_END 281 | -------------------------------------------------------------------------------- /Example/Pods/FMDB/src/fmdb/FMDatabasePool.m: -------------------------------------------------------------------------------- 1 | // 2 | // FMDatabasePool.m 3 | // fmdb 4 | // 5 | // Created by August Mueller on 6/22/11. 6 | // Copyright 2011 Flying Meat Inc. All rights reserved. 7 | // 8 | 9 | #if FMDB_SQLITE_STANDALONE 10 | #import 11 | #else 12 | #import 13 | #endif 14 | 15 | #import "FMDatabasePool.h" 16 | #import "FMDatabase.h" 17 | 18 | typedef NS_ENUM(NSInteger, FMDBTransaction) { 19 | FMDBTransactionExclusive, 20 | FMDBTransactionDeferred, 21 | FMDBTransactionImmediate, 22 | }; 23 | 24 | @interface FMDatabasePool () { 25 | dispatch_queue_t _lockQueue; 26 | 27 | NSMutableArray *_databaseInPool; 28 | NSMutableArray *_databaseOutPool; 29 | } 30 | 31 | - (void)pushDatabaseBackInPool:(FMDatabase*)db; 32 | - (FMDatabase*)db; 33 | 34 | @end 35 | 36 | 37 | @implementation FMDatabasePool 38 | @synthesize path=_path; 39 | @synthesize delegate=_delegate; 40 | @synthesize maximumNumberOfDatabasesToCreate=_maximumNumberOfDatabasesToCreate; 41 | @synthesize openFlags=_openFlags; 42 | 43 | 44 | + (instancetype)databasePoolWithPath:(NSString *)aPath { 45 | return FMDBReturnAutoreleased([[self alloc] initWithPath:aPath]); 46 | } 47 | 48 | + (instancetype)databasePoolWithURL:(NSURL *)url { 49 | return FMDBReturnAutoreleased([[self alloc] initWithPath:url.path]); 50 | } 51 | 52 | + (instancetype)databasePoolWithPath:(NSString *)aPath flags:(int)openFlags { 53 | return FMDBReturnAutoreleased([[self alloc] initWithPath:aPath flags:openFlags]); 54 | } 55 | 56 | + (instancetype)databasePoolWithURL:(NSURL *)url flags:(int)openFlags { 57 | return FMDBReturnAutoreleased([[self alloc] initWithPath:url.path flags:openFlags]); 58 | } 59 | 60 | - (instancetype)initWithURL:(NSURL *)url flags:(int)openFlags vfs:(NSString *)vfsName { 61 | return [self initWithPath:url.path flags:openFlags vfs:vfsName]; 62 | } 63 | 64 | - (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags vfs:(NSString *)vfsName { 65 | 66 | self = [super init]; 67 | 68 | if (self != nil) { 69 | _path = [aPath copy]; 70 | _lockQueue = dispatch_queue_create([[NSString stringWithFormat:@"fmdb.%@", self] UTF8String], NULL); 71 | _databaseInPool = FMDBReturnRetained([NSMutableArray array]); 72 | _databaseOutPool = FMDBReturnRetained([NSMutableArray array]); 73 | _openFlags = openFlags; 74 | _vfsName = [vfsName copy]; 75 | } 76 | 77 | return self; 78 | } 79 | 80 | - (instancetype)initWithPath:(NSString *)aPath flags:(int)openFlags { 81 | return [self initWithPath:aPath flags:openFlags vfs:nil]; 82 | } 83 | 84 | - (instancetype)initWithURL:(NSURL *)url flags:(int)openFlags { 85 | return [self initWithPath:url.path flags:openFlags vfs:nil]; 86 | } 87 | 88 | - (instancetype)initWithPath:(NSString*)aPath { 89 | // default flags for sqlite3_open 90 | return [self initWithPath:aPath flags:SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE]; 91 | } 92 | 93 | - (instancetype)initWithURL:(NSURL *)url { 94 | return [self initWithPath:url.path]; 95 | } 96 | 97 | - (instancetype)init { 98 | return [self initWithPath:nil]; 99 | } 100 | 101 | + (Class)databaseClass { 102 | return [FMDatabase class]; 103 | } 104 | 105 | - (void)dealloc { 106 | 107 | _delegate = 0x00; 108 | FMDBRelease(_path); 109 | FMDBRelease(_databaseInPool); 110 | FMDBRelease(_databaseOutPool); 111 | FMDBRelease(_vfsName); 112 | 113 | if (_lockQueue) { 114 | FMDBDispatchQueueRelease(_lockQueue); 115 | _lockQueue = 0x00; 116 | } 117 | #if ! __has_feature(objc_arc) 118 | [super dealloc]; 119 | #endif 120 | } 121 | 122 | 123 | - (void)executeLocked:(void (^)(void))aBlock { 124 | dispatch_sync(_lockQueue, aBlock); 125 | } 126 | 127 | - (void)pushDatabaseBackInPool:(FMDatabase*)db { 128 | 129 | if (!db) { // db can be null if we set an upper bound on the # of databases to create. 130 | return; 131 | } 132 | 133 | [self executeLocked:^() { 134 | 135 | if ([self->_databaseInPool containsObject:db]) { 136 | [[NSException exceptionWithName:@"Database already in pool" reason:@"The FMDatabase being put back into the pool is already present in the pool" userInfo:nil] raise]; 137 | } 138 | 139 | [self->_databaseInPool addObject:db]; 140 | [self->_databaseOutPool removeObject:db]; 141 | 142 | }]; 143 | } 144 | 145 | - (FMDatabase*)db { 146 | 147 | __block FMDatabase *db; 148 | 149 | 150 | [self executeLocked:^() { 151 | db = [self->_databaseInPool lastObject]; 152 | 153 | BOOL shouldNotifyDelegate = NO; 154 | 155 | if (db) { 156 | [self->_databaseOutPool addObject:db]; 157 | [self->_databaseInPool removeLastObject]; 158 | } 159 | else { 160 | 161 | if (self->_maximumNumberOfDatabasesToCreate) { 162 | NSUInteger currentCount = [self->_databaseOutPool count] + [self->_databaseInPool count]; 163 | 164 | if (currentCount >= self->_maximumNumberOfDatabasesToCreate) { 165 | NSLog(@"Maximum number of databases (%ld) has already been reached!", (long)currentCount); 166 | return; 167 | } 168 | } 169 | 170 | db = [[[self class] databaseClass] databaseWithPath:self->_path]; 171 | shouldNotifyDelegate = YES; 172 | } 173 | 174 | //This ensures that the db is opened before returning 175 | #if SQLITE_VERSION_NUMBER >= 3005000 176 | BOOL success = [db openWithFlags:self->_openFlags vfs:self->_vfsName]; 177 | #else 178 | BOOL success = [db open]; 179 | #endif 180 | if (success) { 181 | if ([self->_delegate respondsToSelector:@selector(databasePool:shouldAddDatabaseToPool:)] && ![self->_delegate databasePool:self shouldAddDatabaseToPool:db]) { 182 | [db close]; 183 | db = 0x00; 184 | } 185 | else { 186 | //It should not get added in the pool twice if lastObject was found 187 | if (![self->_databaseOutPool containsObject:db]) { 188 | [self->_databaseOutPool addObject:db]; 189 | 190 | if (shouldNotifyDelegate && [self->_delegate respondsToSelector:@selector(databasePool:didAddDatabase:)]) { 191 | [self->_delegate databasePool:self didAddDatabase:db]; 192 | } 193 | } 194 | } 195 | } 196 | else { 197 | NSLog(@"Could not open up the database at path %@", self->_path); 198 | db = 0x00; 199 | } 200 | }]; 201 | 202 | return db; 203 | } 204 | 205 | - (NSUInteger)countOfCheckedInDatabases { 206 | 207 | __block NSUInteger count; 208 | 209 | [self executeLocked:^() { 210 | count = [self->_databaseInPool count]; 211 | }]; 212 | 213 | return count; 214 | } 215 | 216 | - (NSUInteger)countOfCheckedOutDatabases { 217 | 218 | __block NSUInteger count; 219 | 220 | [self executeLocked:^() { 221 | count = [self->_databaseOutPool count]; 222 | }]; 223 | 224 | return count; 225 | } 226 | 227 | - (NSUInteger)countOfOpenDatabases { 228 | __block NSUInteger count; 229 | 230 | [self executeLocked:^() { 231 | count = [self->_databaseOutPool count] + [self->_databaseInPool count]; 232 | }]; 233 | 234 | return count; 235 | } 236 | 237 | - (void)releaseAllDatabases { 238 | [self executeLocked:^() { 239 | [self->_databaseOutPool removeAllObjects]; 240 | [self->_databaseInPool removeAllObjects]; 241 | }]; 242 | } 243 | 244 | - (void)inDatabase:(__attribute__((noescape)) void (^)(FMDatabase *db))block { 245 | 246 | FMDatabase *db = [self db]; 247 | 248 | block(db); 249 | 250 | [self pushDatabaseBackInPool:db]; 251 | } 252 | 253 | - (void)beginTransaction:(FMDBTransaction)transaction withBlock:(void (^)(FMDatabase *db, BOOL *rollback))block { 254 | 255 | BOOL shouldRollback = NO; 256 | 257 | FMDatabase *db = [self db]; 258 | 259 | switch (transaction) { 260 | case FMDBTransactionExclusive: 261 | [db beginTransaction]; 262 | break; 263 | case FMDBTransactionDeferred: 264 | [db beginDeferredTransaction]; 265 | break; 266 | case FMDBTransactionImmediate: 267 | [db beginImmediateTransaction]; 268 | break; 269 | } 270 | 271 | 272 | block(db, &shouldRollback); 273 | 274 | if (shouldRollback) { 275 | [db rollback]; 276 | } 277 | else { 278 | [db commit]; 279 | } 280 | 281 | [self pushDatabaseBackInPool:db]; 282 | } 283 | 284 | - (void)inTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block { 285 | [self beginTransaction:FMDBTransactionExclusive withBlock:block]; 286 | } 287 | 288 | - (void)inDeferredTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block { 289 | [self beginTransaction:FMDBTransactionDeferred withBlock:block]; 290 | } 291 | 292 | - (void)inExclusiveTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block { 293 | [self beginTransaction:FMDBTransactionExclusive withBlock:block]; 294 | } 295 | 296 | - (void)inImmediateTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block { 297 | [self beginTransaction:FMDBTransactionImmediate withBlock:block]; 298 | } 299 | 300 | - (NSError*)inSavePoint:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block { 301 | #if SQLITE_VERSION_NUMBER >= 3007000 302 | static unsigned long savePointIdx = 0; 303 | 304 | NSString *name = [NSString stringWithFormat:@"savePoint%ld", savePointIdx++]; 305 | 306 | BOOL shouldRollback = NO; 307 | 308 | FMDatabase *db = [self db]; 309 | 310 | NSError *err = 0x00; 311 | 312 | if (![db startSavePointWithName:name error:&err]) { 313 | [self pushDatabaseBackInPool:db]; 314 | return err; 315 | } 316 | 317 | block(db, &shouldRollback); 318 | 319 | if (shouldRollback) { 320 | // We need to rollback and release this savepoint to remove it 321 | [db rollbackToSavePointWithName:name error:&err]; 322 | } 323 | [db releaseSavePointWithName:name error:&err]; 324 | 325 | [self pushDatabaseBackInPool:db]; 326 | 327 | return err; 328 | #else 329 | NSString *errorMessage = NSLocalizedStringFromTable(@"Save point functions require SQLite 3.7", @"FMDB", nil); 330 | if (self.logsErrors) NSLog(@"%@", errorMessage); 331 | return [NSError errorWithDomain:@"FMDatabase" code:0 userInfo:@{NSLocalizedDescriptionKey : errorMessage}]; 332 | #endif 333 | } 334 | 335 | @end 336 | -------------------------------------------------------------------------------- /Example/Pods/FMDB/src/fmdb/FMDatabaseQueue.h: -------------------------------------------------------------------------------- 1 | // 2 | // FMDatabaseQueue.h 3 | // fmdb 4 | // 5 | // Created by August Mueller on 6/22/11. 6 | // Copyright 2011 Flying Meat Inc. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "FMDatabase.h" 11 | 12 | NS_ASSUME_NONNULL_BEGIN 13 | 14 | /** To perform queries and updates on multiple threads, you'll want to use `FMDatabaseQueue`. 15 | 16 | Using a single instance of `` from multiple threads at once is a bad idea. It has always been OK to make a `` object *per thread*. Just don't share a single instance across threads, and definitely not across multiple threads at the same time. 17 | 18 | Instead, use `FMDatabaseQueue`. Here's how to use it: 19 | 20 | First, make your queue. 21 | 22 | FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:aPath]; 23 | 24 | Then use it like so: 25 | 26 | [queue inDatabase:^(FMDatabase *db) { 27 | [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]]; 28 | [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]]; 29 | [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]]; 30 | 31 | FMResultSet *rs = [db executeQuery:@"select * from foo"]; 32 | while ([rs next]) { 33 | //… 34 | } 35 | }]; 36 | 37 | An easy way to wrap things up in a transaction can be done like this: 38 | 39 | [queue inTransaction:^(FMDatabase *db, BOOL *rollback) { 40 | [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]]; 41 | [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]]; 42 | [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]]; 43 | 44 | if (whoopsSomethingWrongHappened) { 45 | *rollback = YES; 46 | return; 47 | } 48 | // etc… 49 | [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:4]]; 50 | }]; 51 | 52 | `FMDatabaseQueue` will run the blocks on a serialized queue (hence the name of the class). So if you call `FMDatabaseQueue`'s methods from multiple threads at the same time, they will be executed in the order they are received. This way queries and updates won't step on each other's toes, and every one is happy. 53 | 54 | ### See also 55 | 56 | - `` 57 | 58 | @warning Do not instantiate a single `` object and use it across multiple threads. Use `FMDatabaseQueue` instead. 59 | 60 | @warning The calls to `FMDatabaseQueue`'s methods are blocking. So even though you are passing along blocks, they will **not** be run on another thread. 61 | 62 | */ 63 | 64 | @interface FMDatabaseQueue : NSObject 65 | /** Path of database */ 66 | 67 | @property (atomic, retain, nullable) NSString *path; 68 | 69 | /** Open flags */ 70 | 71 | @property (atomic, readonly) int openFlags; 72 | 73 | /** Custom virtual file system name */ 74 | 75 | @property (atomic, copy, nullable) NSString *vfsName; 76 | 77 | ///---------------------------------------------------- 78 | /// @name Initialization, opening, and closing of queue 79 | ///---------------------------------------------------- 80 | 81 | /** Create queue using path. 82 | 83 | @param aPath The file path of the database. 84 | 85 | @return The `FMDatabaseQueue` object. `nil` on error. 86 | */ 87 | 88 | + (nullable instancetype)databaseQueueWithPath:(NSString * _Nullable)aPath; 89 | 90 | /** Create queue using file URL. 91 | 92 | @param url The file `NSURL` of the database. 93 | 94 | @return The `FMDatabaseQueue` object. `nil` on error. 95 | */ 96 | 97 | + (nullable instancetype)databaseQueueWithURL:(NSURL * _Nullable)url; 98 | 99 | /** Create queue using path and specified flags. 100 | 101 | @param aPath The file path of the database. 102 | @param openFlags Flags passed to the openWithFlags method of the database. 103 | 104 | @return The `FMDatabaseQueue` object. `nil` on error. 105 | */ 106 | + (nullable instancetype)databaseQueueWithPath:(NSString * _Nullable)aPath flags:(int)openFlags; 107 | 108 | /** Create queue using file URL and specified flags. 109 | 110 | @param url The file `NSURL` of the database. 111 | @param openFlags Flags passed to the openWithFlags method of the database. 112 | 113 | @return The `FMDatabaseQueue` object. `nil` on error. 114 | */ 115 | + (nullable instancetype)databaseQueueWithURL:(NSURL * _Nullable)url flags:(int)openFlags; 116 | 117 | /** Create queue using path. 118 | 119 | @param aPath The file path of the database. 120 | 121 | @return The `FMDatabaseQueue` object. `nil` on error. 122 | */ 123 | 124 | - (nullable instancetype)initWithPath:(NSString * _Nullable)aPath; 125 | 126 | /** Create queue using file URL. 127 | 128 | @param url The file `NSURL of the database. 129 | 130 | @return The `FMDatabaseQueue` object. `nil` on error. 131 | */ 132 | 133 | - (nullable instancetype)initWithURL:(NSURL * _Nullable)url; 134 | 135 | /** Create queue using path and specified flags. 136 | 137 | @param aPath The file path of the database. 138 | @param openFlags Flags passed to the openWithFlags method of the database. 139 | 140 | @return The `FMDatabaseQueue` object. `nil` on error. 141 | */ 142 | 143 | - (nullable instancetype)initWithPath:(NSString * _Nullable)aPath flags:(int)openFlags; 144 | 145 | /** Create queue using file URL and specified flags. 146 | 147 | @param url The file path of the database. 148 | @param openFlags Flags passed to the openWithFlags method of the database. 149 | 150 | @return The `FMDatabaseQueue` object. `nil` on error. 151 | */ 152 | 153 | - (nullable instancetype)initWithURL:(NSURL * _Nullable)url flags:(int)openFlags; 154 | 155 | /** Create queue using path and specified flags. 156 | 157 | @param aPath The file path of the database. 158 | @param openFlags Flags passed to the openWithFlags method of the database 159 | @param vfsName The name of a custom virtual file system 160 | 161 | @return The `FMDatabaseQueue` object. `nil` on error. 162 | */ 163 | 164 | - (nullable instancetype)initWithPath:(NSString * _Nullable)aPath flags:(int)openFlags vfs:(NSString * _Nullable)vfsName; 165 | 166 | /** Create queue using file URL and specified flags. 167 | 168 | @param url The file `NSURL of the database. 169 | @param openFlags Flags passed to the openWithFlags method of the database 170 | @param vfsName The name of a custom virtual file system 171 | 172 | @return The `FMDatabaseQueue` object. `nil` on error. 173 | */ 174 | 175 | - (nullable instancetype)initWithURL:(NSURL * _Nullable)url flags:(int)openFlags vfs:(NSString * _Nullable)vfsName; 176 | 177 | /** Returns the Class of 'FMDatabase' subclass, that will be used to instantiate database object. 178 | 179 | Subclasses can override this method to return specified Class of 'FMDatabase' subclass. 180 | 181 | @return The Class of 'FMDatabase' subclass, that will be used to instantiate database object. 182 | */ 183 | 184 | + (Class)databaseClass; 185 | 186 | /** Close database used by queue. */ 187 | 188 | - (void)close; 189 | 190 | /** Interupt pending database operation. */ 191 | 192 | - (void)interrupt; 193 | 194 | ///----------------------------------------------- 195 | /// @name Dispatching database operations to queue 196 | ///----------------------------------------------- 197 | 198 | /** Synchronously perform database operations on queue. 199 | 200 | @param block The code to be run on the queue of `FMDatabaseQueue` 201 | */ 202 | 203 | - (void)inDatabase:(__attribute__((noescape)) void (^)(FMDatabase *db))block; 204 | 205 | /** Synchronously perform database operations on queue, using transactions. 206 | 207 | @param block The code to be run on the queue of `FMDatabaseQueue` 208 | 209 | @warning Unlike SQLite's `BEGIN TRANSACTION`, this method currently performs 210 | an exclusive transaction, not a deferred transaction. This behavior 211 | is likely to change in future versions of FMDB, whereby this method 212 | will likely eventually adopt standard SQLite behavior and perform 213 | deferred transactions. If you really need exclusive tranaction, it is 214 | recommended that you use `inExclusiveTransaction`, instead, not only 215 | to make your intent explicit, but also to future-proof your code. 216 | 217 | */ 218 | 219 | - (void)inTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block; 220 | 221 | /** Synchronously perform database operations on queue, using deferred transactions. 222 | 223 | @param block The code to be run on the queue of `FMDatabaseQueue` 224 | */ 225 | 226 | - (void)inDeferredTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block; 227 | 228 | /** Synchronously perform database operations on queue, using exclusive transactions. 229 | 230 | @param block The code to be run on the queue of `FMDatabaseQueue` 231 | */ 232 | 233 | - (void)inExclusiveTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block; 234 | 235 | /** Synchronously perform database operations on queue, using immediate transactions. 236 | 237 | @param block The code to be run on the queue of `FMDatabaseQueue` 238 | */ 239 | 240 | - (void)inImmediateTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block; 241 | 242 | ///----------------------------------------------- 243 | /// @name Dispatching database operations to queue 244 | ///----------------------------------------------- 245 | 246 | /** Synchronously perform database operations using save point. 247 | 248 | @param block The code to be run on the queue of `FMDatabaseQueue` 249 | */ 250 | 251 | // NOTE: you can not nest these, since calling it will pull another database out of the pool and you'll get a deadlock. 252 | // If you need to nest, use FMDatabase's startSavePointWithName:error: instead. 253 | - (NSError * _Nullable)inSavePoint:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block; 254 | 255 | ///----------------- 256 | /// @name Checkpoint 257 | ///----------------- 258 | 259 | /** Performs a WAL checkpoint 260 | 261 | @param checkpointMode The checkpoint mode for sqlite3_wal_checkpoint_v2 262 | @param error The NSError corresponding to the error, if any. 263 | @return YES on success, otherwise NO. 264 | */ 265 | - (BOOL)checkpoint:(FMDBCheckpointMode)checkpointMode error:(NSError * _Nullable *)error; 266 | 267 | /** Performs a WAL checkpoint 268 | 269 | @param checkpointMode The checkpoint mode for sqlite3_wal_checkpoint_v2 270 | @param name The db name for sqlite3_wal_checkpoint_v2 271 | @param error The NSError corresponding to the error, if any. 272 | @return YES on success, otherwise NO. 273 | */ 274 | - (BOOL)checkpoint:(FMDBCheckpointMode)checkpointMode name:(NSString * _Nullable)name error:(NSError * _Nullable *)error; 275 | 276 | /** Performs a WAL checkpoint 277 | 278 | @param checkpointMode The checkpoint mode for sqlite3_wal_checkpoint_v2 279 | @param name The db name for sqlite3_wal_checkpoint_v2 280 | @param error The NSError corresponding to the error, if any. 281 | @param logFrameCount If not NULL, then this is set to the total number of frames in the log file or to -1 if the checkpoint could not run because of an error or because the database is not in WAL mode. 282 | @param checkpointCount If not NULL, then this is set to the total number of checkpointed frames in the log file (including any that were already checkpointed before the function was called) or to -1 if the checkpoint could not run due to an error or because the database is not in WAL mode. 283 | @return YES on success, otherwise NO. 284 | */ 285 | - (BOOL)checkpoint:(FMDBCheckpointMode)checkpointMode name:(NSString * _Nullable)name logFrameCount:(int * _Nullable)logFrameCount checkpointCount:(int * _Nullable)checkpointCount error:(NSError * _Nullable *)error; 286 | 287 | @end 288 | 289 | NS_ASSUME_NONNULL_END 290 | -------------------------------------------------------------------------------- /Example/Pods/FMDB/src/fmdb/FMDatabaseQueue.m: -------------------------------------------------------------------------------- 1 | // 2 | // FMDatabaseQueue.m 3 | // fmdb 4 | // 5 | // Created by August Mueller on 6/22/11. 6 | // Copyright 2011 Flying Meat Inc. All rights reserved. 7 | // 8 | 9 | #import "FMDatabaseQueue.h" 10 | #import "FMDatabase.h" 11 | 12 | #if FMDB_SQLITE_STANDALONE 13 | #import 14 | #else 15 | #import 16 | #endif 17 | 18 | typedef NS_ENUM(NSInteger, FMDBTransaction) { 19 | FMDBTransactionExclusive, 20 | FMDBTransactionDeferred, 21 | FMDBTransactionImmediate, 22 | }; 23 | 24 | /* 25 | 26 | Note: we call [self retain]; before using dispatch_sync, just incase 27 | FMDatabaseQueue is released on another thread and we're in the middle of doing 28 | something in dispatch_sync 29 | 30 | */ 31 | 32 | /* 33 | * A key used to associate the FMDatabaseQueue object with the dispatch_queue_t it uses. 34 | * This in turn is used for deadlock detection by seeing if inDatabase: is called on 35 | * the queue's dispatch queue, which should not happen and causes a deadlock. 36 | */ 37 | static const void * const kDispatchQueueSpecificKey = &kDispatchQueueSpecificKey; 38 | 39 | @interface FMDatabaseQueue () { 40 | dispatch_queue_t _queue; 41 | FMDatabase *_db; 42 | } 43 | @end 44 | 45 | @implementation FMDatabaseQueue 46 | 47 | + (instancetype)databaseQueueWithPath:(NSString *)aPath { 48 | FMDatabaseQueue *q = [[self alloc] initWithPath:aPath]; 49 | 50 | FMDBAutorelease(q); 51 | 52 | return q; 53 | } 54 | 55 | + (instancetype)databaseQueueWithURL:(NSURL *)url { 56 | return [self databaseQueueWithPath:url.path]; 57 | } 58 | 59 | + (instancetype)databaseQueueWithPath:(NSString *)aPath flags:(int)openFlags { 60 | FMDatabaseQueue *q = [[self alloc] initWithPath:aPath flags:openFlags]; 61 | 62 | FMDBAutorelease(q); 63 | 64 | return q; 65 | } 66 | 67 | + (instancetype)databaseQueueWithURL:(NSURL *)url flags:(int)openFlags { 68 | return [self databaseQueueWithPath:url.path flags:openFlags]; 69 | } 70 | 71 | + (Class)databaseClass { 72 | return [FMDatabase class]; 73 | } 74 | 75 | - (instancetype)initWithURL:(NSURL *)url flags:(int)openFlags vfs:(NSString *)vfsName { 76 | return [self initWithPath:url.path flags:openFlags vfs:vfsName]; 77 | } 78 | 79 | - (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags vfs:(NSString *)vfsName { 80 | self = [super init]; 81 | 82 | if (self != nil) { 83 | 84 | _db = [[[self class] databaseClass] databaseWithPath:aPath]; 85 | FMDBRetain(_db); 86 | 87 | #if SQLITE_VERSION_NUMBER >= 3005000 88 | BOOL success = [_db openWithFlags:openFlags vfs:vfsName]; 89 | #else 90 | BOOL success = [_db open]; 91 | #endif 92 | if (!success) { 93 | NSLog(@"Could not create database queue for path %@", aPath); 94 | FMDBRelease(self); 95 | return 0x00; 96 | } 97 | 98 | _path = FMDBReturnRetained(aPath); 99 | 100 | _queue = dispatch_queue_create([[NSString stringWithFormat:@"fmdb.%@", self] UTF8String], NULL); 101 | dispatch_queue_set_specific(_queue, kDispatchQueueSpecificKey, (__bridge void *)self, NULL); 102 | _openFlags = openFlags; 103 | _vfsName = [vfsName copy]; 104 | } 105 | 106 | return self; 107 | } 108 | 109 | - (instancetype)initWithPath:(NSString *)aPath flags:(int)openFlags { 110 | return [self initWithPath:aPath flags:openFlags vfs:nil]; 111 | } 112 | 113 | - (instancetype)initWithURL:(NSURL *)url flags:(int)openFlags { 114 | return [self initWithPath:url.path flags:openFlags vfs:nil]; 115 | } 116 | 117 | - (instancetype)initWithURL:(NSURL *)url { 118 | return [self initWithPath:url.path]; 119 | } 120 | 121 | - (instancetype)initWithPath:(NSString *)aPath { 122 | // default flags for sqlite3_open 123 | return [self initWithPath:aPath flags:SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE vfs:nil]; 124 | } 125 | 126 | - (instancetype)init { 127 | return [self initWithPath:nil]; 128 | } 129 | 130 | - (void)dealloc { 131 | FMDBRelease(_db); 132 | FMDBRelease(_path); 133 | FMDBRelease(_vfsName); 134 | 135 | if (_queue) { 136 | FMDBDispatchQueueRelease(_queue); 137 | _queue = 0x00; 138 | } 139 | #if ! __has_feature(objc_arc) 140 | [super dealloc]; 141 | #endif 142 | } 143 | 144 | - (void)close { 145 | FMDBRetain(self); 146 | dispatch_sync(_queue, ^() { 147 | [self->_db close]; 148 | FMDBRelease(_db); 149 | self->_db = 0x00; 150 | }); 151 | FMDBRelease(self); 152 | } 153 | 154 | - (void)interrupt { 155 | [[self database] interrupt]; 156 | } 157 | 158 | - (FMDatabase*)database { 159 | if (![_db isOpen]) { 160 | if (!_db) { 161 | _db = FMDBReturnRetained([[[self class] databaseClass] databaseWithPath:_path]); 162 | } 163 | 164 | #if SQLITE_VERSION_NUMBER >= 3005000 165 | BOOL success = [_db openWithFlags:_openFlags vfs:_vfsName]; 166 | #else 167 | BOOL success = [_db open]; 168 | #endif 169 | if (!success) { 170 | NSLog(@"FMDatabaseQueue could not reopen database for path %@", _path); 171 | FMDBRelease(_db); 172 | _db = 0x00; 173 | return 0x00; 174 | } 175 | } 176 | 177 | return _db; 178 | } 179 | 180 | - (void)inDatabase:(__attribute__((noescape)) void (^)(FMDatabase *db))block { 181 | #ifndef NDEBUG 182 | /* Get the currently executing queue (which should probably be nil, but in theory could be another DB queue 183 | * and then check it against self to make sure we're not about to deadlock. */ 184 | FMDatabaseQueue *currentSyncQueue = (__bridge id)dispatch_get_specific(kDispatchQueueSpecificKey); 185 | assert(currentSyncQueue != self && "inDatabase: was called reentrantly on the same queue, which would lead to a deadlock"); 186 | #endif 187 | 188 | FMDBRetain(self); 189 | 190 | dispatch_sync(_queue, ^() { 191 | 192 | FMDatabase *db = [self database]; 193 | 194 | block(db); 195 | 196 | if ([db hasOpenResultSets]) { 197 | NSLog(@"Warning: there is at least one open result set around after performing [FMDatabaseQueue inDatabase:]"); 198 | 199 | #if defined(DEBUG) && DEBUG 200 | NSSet *openSetCopy = FMDBReturnAutoreleased([[db valueForKey:@"_openResultSets"] copy]); 201 | for (NSValue *rsInWrappedInATastyValueMeal in openSetCopy) { 202 | FMResultSet *rs = (FMResultSet *)[rsInWrappedInATastyValueMeal pointerValue]; 203 | NSLog(@"query: '%@'", [rs query]); 204 | } 205 | #endif 206 | } 207 | }); 208 | 209 | FMDBRelease(self); 210 | } 211 | 212 | - (void)beginTransaction:(FMDBTransaction)transaction withBlock:(void (^)(FMDatabase *db, BOOL *rollback))block { 213 | FMDBRetain(self); 214 | dispatch_sync(_queue, ^() { 215 | 216 | BOOL shouldRollback = NO; 217 | 218 | switch (transaction) { 219 | case FMDBTransactionExclusive: 220 | [[self database] beginTransaction]; 221 | break; 222 | case FMDBTransactionDeferred: 223 | [[self database] beginDeferredTransaction]; 224 | break; 225 | case FMDBTransactionImmediate: 226 | [[self database] beginImmediateTransaction]; 227 | break; 228 | } 229 | 230 | block([self database], &shouldRollback); 231 | 232 | if (shouldRollback) { 233 | [[self database] rollback]; 234 | } 235 | else { 236 | [[self database] commit]; 237 | } 238 | }); 239 | 240 | FMDBRelease(self); 241 | } 242 | 243 | - (void)inTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block { 244 | [self beginTransaction:FMDBTransactionExclusive withBlock:block]; 245 | } 246 | 247 | - (void)inDeferredTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block { 248 | [self beginTransaction:FMDBTransactionDeferred withBlock:block]; 249 | } 250 | 251 | - (void)inExclusiveTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block { 252 | [self beginTransaction:FMDBTransactionExclusive withBlock:block]; 253 | } 254 | 255 | - (void)inImmediateTransaction:(__attribute__((noescape)) void (^)(FMDatabase * _Nonnull, BOOL * _Nonnull))block { 256 | [self beginTransaction:FMDBTransactionImmediate withBlock:block]; 257 | } 258 | 259 | - (NSError*)inSavePoint:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block { 260 | #if SQLITE_VERSION_NUMBER >= 3007000 261 | static unsigned long savePointIdx = 0; 262 | __block NSError *err = 0x00; 263 | FMDBRetain(self); 264 | dispatch_sync(_queue, ^() { 265 | 266 | NSString *name = [NSString stringWithFormat:@"savePoint%ld", savePointIdx++]; 267 | 268 | BOOL shouldRollback = NO; 269 | 270 | if ([[self database] startSavePointWithName:name error:&err]) { 271 | 272 | block([self database], &shouldRollback); 273 | 274 | if (shouldRollback) { 275 | // We need to rollback and release this savepoint to remove it 276 | [[self database] rollbackToSavePointWithName:name error:&err]; 277 | } 278 | [[self database] releaseSavePointWithName:name error:&err]; 279 | 280 | } 281 | }); 282 | FMDBRelease(self); 283 | return err; 284 | #else 285 | NSString *errorMessage = NSLocalizedStringFromTable(@"Save point functions require SQLite 3.7", @"FMDB", nil); 286 | if (_db.logsErrors) NSLog(@"%@", errorMessage); 287 | return [NSError errorWithDomain:@"FMDatabase" code:0 userInfo:@{NSLocalizedDescriptionKey : errorMessage}]; 288 | #endif 289 | } 290 | 291 | - (BOOL)checkpoint:(FMDBCheckpointMode)mode error:(NSError * __autoreleasing *)error 292 | { 293 | return [self checkpoint:mode name:nil logFrameCount:NULL checkpointCount:NULL error:error]; 294 | } 295 | 296 | - (BOOL)checkpoint:(FMDBCheckpointMode)mode name:(NSString *)name error:(NSError * __autoreleasing *)error 297 | { 298 | return [self checkpoint:mode name:name logFrameCount:NULL checkpointCount:NULL error:error]; 299 | } 300 | 301 | - (BOOL)checkpoint:(FMDBCheckpointMode)mode name:(NSString *)name logFrameCount:(int * _Nullable)logFrameCount checkpointCount:(int * _Nullable)checkpointCount error:(NSError * __autoreleasing _Nullable * _Nullable)error 302 | { 303 | __block BOOL result; 304 | __block NSError *blockError; 305 | 306 | FMDBRetain(self); 307 | dispatch_sync(_queue, ^() { 308 | result = [self.database checkpoint:mode name:name logFrameCount:NULL checkpointCount:NULL error:&blockError]; 309 | }); 310 | FMDBRelease(self); 311 | 312 | if (error) { 313 | *error = blockError; 314 | } 315 | return result; 316 | } 317 | 318 | @end 319 | -------------------------------------------------------------------------------- /Example/Pods/Local Podspecs/XWDatabase.podspec.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "XWDatabase", 3 | "version": "1.2.2", 4 | "summary": "数据库工具类,直接操作模型读写数据库,FMDB封装", 5 | "description": "数据库工具类,直接操作模型读写数据库,FMDB封装, 二进制文件重用, 支持自定义主键/联合主键, 支持SQL拓展操作, 支持自定义对象存储...", 6 | "homepage": "https://github.com/qxuewei/XWDatabase", 7 | "license": { 8 | "type": "MIT", 9 | "file": "LICENSE" 10 | }, 11 | "authors": { 12 | "qxuewei@yeah.net": "qiuxuewei@peiwo.cn" 13 | }, 14 | "source": { 15 | "git": "https://github.com/qxuewei/XWDatabase.git", 16 | "tag": "1.2.2" 17 | }, 18 | "platforms": { 19 | "ios": "8.0" 20 | }, 21 | "source_files": "XWDatabase/Classes/**/*", 22 | "dependencies": { 23 | "FMDB": [ 24 | 25 | ] 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Example/Pods/Manifest.lock: -------------------------------------------------------------------------------- 1 | PODS: 2 | - FMDB (2.7.5): 3 | - FMDB/standard (= 2.7.5) 4 | - FMDB/standard (2.7.5) 5 | - XWDatabase (1.2.2): 6 | - FMDB 7 | 8 | DEPENDENCIES: 9 | - XWDatabase (from `../`) 10 | 11 | SPEC REPOS: 12 | https://github.com/cocoapods/specs.git: 13 | - FMDB 14 | 15 | EXTERNAL SOURCES: 16 | XWDatabase: 17 | :path: "../" 18 | 19 | SPEC CHECKSUMS: 20 | FMDB: 2ce00b547f966261cd18927a3ddb07cb6f3db82a 21 | XWDatabase: 538a3c6c0905c0c5cbe52897fa003bb689c5f286 22 | 23 | PODFILE CHECKSUM: b70233fb56a4a47f18e103190428d4c38af38bfb 24 | 25 | COCOAPODS: 1.7.0.rc.1 26 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/FMDB/FMDB-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | ${PRODUCT_BUNDLE_IDENTIFIER} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 2.7.5 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | ${CURRENT_PROJECT_VERSION} 23 | NSPrincipalClass 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/FMDB/FMDB-dummy.m: -------------------------------------------------------------------------------- 1 | #import 2 | @interface PodsDummy_FMDB : NSObject 3 | @end 4 | @implementation PodsDummy_FMDB 5 | @end 6 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/FMDB/FMDB-prefix.pch: -------------------------------------------------------------------------------- 1 | #ifdef __OBJC__ 2 | #import 3 | #else 4 | #ifndef FOUNDATION_EXPORT 5 | #if defined(__cplusplus) 6 | #define FOUNDATION_EXPORT extern "C" 7 | #else 8 | #define FOUNDATION_EXPORT extern 9 | #endif 10 | #endif 11 | #endif 12 | 13 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/FMDB/FMDB-umbrella.h: -------------------------------------------------------------------------------- 1 | #ifdef __OBJC__ 2 | #import 3 | #else 4 | #ifndef FOUNDATION_EXPORT 5 | #if defined(__cplusplus) 6 | #define FOUNDATION_EXPORT extern "C" 7 | #else 8 | #define FOUNDATION_EXPORT extern 9 | #endif 10 | #endif 11 | #endif 12 | 13 | #import "FMDatabase.h" 14 | #import "FMDatabaseAdditions.h" 15 | #import "FMDatabasePool.h" 16 | #import "FMDatabaseQueue.h" 17 | #import "FMDB.h" 18 | #import "FMResultSet.h" 19 | 20 | FOUNDATION_EXPORT double FMDBVersionNumber; 21 | FOUNDATION_EXPORT const unsigned char FMDBVersionString[]; 22 | 23 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/FMDB/FMDB.modulemap: -------------------------------------------------------------------------------- 1 | framework module FMDB { 2 | umbrella header "FMDB-umbrella.h" 3 | 4 | export * 5 | module * { export * } 6 | } 7 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/FMDB/FMDB.xcconfig: -------------------------------------------------------------------------------- 1 | CONFIGURATION_BUILD_DIR = ${PODS_CONFIGURATION_BUILD_DIR}/FMDB 2 | GCC_PREPROCESSOR_DEFINITIONS = $(inherited) COCOAPODS=1 3 | OTHER_LDFLAGS = $(inherited) -l"sqlite3" 4 | PODS_BUILD_DIR = ${BUILD_DIR} 5 | PODS_CONFIGURATION_BUILD_DIR = ${PODS_BUILD_DIR}/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME) 6 | PODS_ROOT = ${SRCROOT} 7 | PODS_TARGET_SRCROOT = ${PODS_ROOT}/FMDB 8 | PRODUCT_BUNDLE_IDENTIFIER = org.cocoapods.${PRODUCT_NAME:rfc1034identifier} 9 | SKIP_INSTALL = YES 10 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/Pods-XWDatabase_Example/Pods-XWDatabase_Example-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | ${PRODUCT_BUNDLE_IDENTIFIER} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | ${CURRENT_PROJECT_VERSION} 23 | NSPrincipalClass 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/Pods-XWDatabase_Example/Pods-XWDatabase_Example-acknowledgements.markdown: -------------------------------------------------------------------------------- 1 | # Acknowledgements 2 | This application makes use of the following third party libraries: 3 | 4 | ## FMDB 5 | 6 | If you are using FMDB in your project, I'd love to hear about it. Let Gus know 7 | by sending an email to gus@flyingmeat.com. 8 | 9 | And if you happen to come across either Gus Mueller or Rob Ryan in a bar, you 10 | might consider purchasing a drink of their choosing if FMDB has been useful to 11 | you. 12 | 13 | Finally, and shortly, this is the MIT License. 14 | 15 | Copyright (c) 2008-2014 Flying Meat Inc. 16 | 17 | Permission is hereby granted, free of charge, to any person obtaining a copy 18 | of this software and associated documentation files (the "Software"), to deal 19 | in the Software without restriction, including without limitation the rights 20 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 21 | copies of the Software, and to permit persons to whom the Software is 22 | furnished to do so, subject to the following conditions: 23 | 24 | The above copyright notice and this permission notice shall be included in 25 | all copies or substantial portions of the Software. 26 | 27 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 28 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 29 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 30 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 31 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 32 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 33 | THE SOFTWARE. 34 | 35 | ## XWDatabase 36 | 37 | Copyright (c) 2018 qxuewei@yeah.net 38 | 39 | Permission is hereby granted, free of charge, to any person obtaining a copy 40 | of this software and associated documentation files (the "Software"), to deal 41 | in the Software without restriction, including without limitation the rights 42 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 43 | copies of the Software, and to permit persons to whom the Software is 44 | furnished to do so, subject to the following conditions: 45 | 46 | The above copyright notice and this permission notice shall be included in 47 | all copies or substantial portions of the Software. 48 | 49 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 50 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 51 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 52 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 53 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 54 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 55 | THE SOFTWARE. 56 | 57 | Generated by CocoaPods - https://cocoapods.org 58 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/Pods-XWDatabase_Example/Pods-XWDatabase_Example-acknowledgements.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | PreferenceSpecifiers 6 | 7 | 8 | FooterText 9 | This application makes use of the following third party libraries: 10 | Title 11 | Acknowledgements 12 | Type 13 | PSGroupSpecifier 14 | 15 | 16 | FooterText 17 | If you are using FMDB in your project, I'd love to hear about it. Let Gus know 18 | by sending an email to gus@flyingmeat.com. 19 | 20 | And if you happen to come across either Gus Mueller or Rob Ryan in a bar, you 21 | might consider purchasing a drink of their choosing if FMDB has been useful to 22 | you. 23 | 24 | Finally, and shortly, this is the MIT License. 25 | 26 | Copyright (c) 2008-2014 Flying Meat Inc. 27 | 28 | Permission is hereby granted, free of charge, to any person obtaining a copy 29 | of this software and associated documentation files (the "Software"), to deal 30 | in the Software without restriction, including without limitation the rights 31 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 32 | copies of the Software, and to permit persons to whom the Software is 33 | furnished to do so, subject to the following conditions: 34 | 35 | The above copyright notice and this permission notice shall be included in 36 | all copies or substantial portions of the Software. 37 | 38 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 39 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 40 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 41 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 42 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 43 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 44 | THE SOFTWARE. 45 | License 46 | MIT 47 | Title 48 | FMDB 49 | Type 50 | PSGroupSpecifier 51 | 52 | 53 | FooterText 54 | Copyright (c) 2018 qxuewei@yeah.net <qiuxuewei@peiwo.cn> 55 | 56 | Permission is hereby granted, free of charge, to any person obtaining a copy 57 | of this software and associated documentation files (the "Software"), to deal 58 | in the Software without restriction, including without limitation the rights 59 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 60 | copies of the Software, and to permit persons to whom the Software is 61 | furnished to do so, subject to the following conditions: 62 | 63 | The above copyright notice and this permission notice shall be included in 64 | all copies or substantial portions of the Software. 65 | 66 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 67 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 68 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 69 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 70 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 71 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 72 | THE SOFTWARE. 73 | 74 | License 75 | MIT 76 | Title 77 | XWDatabase 78 | Type 79 | PSGroupSpecifier 80 | 81 | 82 | FooterText 83 | Generated by CocoaPods - https://cocoapods.org 84 | Title 85 | 86 | Type 87 | PSGroupSpecifier 88 | 89 | 90 | StringsTable 91 | Acknowledgements 92 | Title 93 | Acknowledgements 94 | 95 | 96 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/Pods-XWDatabase_Example/Pods-XWDatabase_Example-dummy.m: -------------------------------------------------------------------------------- 1 | #import 2 | @interface PodsDummy_Pods_XWDatabase_Example : NSObject 3 | @end 4 | @implementation PodsDummy_Pods_XWDatabase_Example 5 | @end 6 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/Pods-XWDatabase_Example/Pods-XWDatabase_Example-frameworks.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | set -u 4 | set -o pipefail 5 | 6 | function on_error { 7 | echo "$(realpath -mq "${0}"):$1: error: Unexpected failure" 8 | } 9 | trap 'on_error $LINENO' ERR 10 | 11 | if [ -z ${FRAMEWORKS_FOLDER_PATH+x} ]; then 12 | # If FRAMEWORKS_FOLDER_PATH is not set, then there's nowhere for us to copy 13 | # frameworks to, so exit 0 (signalling the script phase was successful). 14 | exit 0 15 | fi 16 | 17 | echo "mkdir -p ${CONFIGURATION_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}" 18 | mkdir -p "${CONFIGURATION_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}" 19 | 20 | COCOAPODS_PARALLEL_CODE_SIGN="${COCOAPODS_PARALLEL_CODE_SIGN:-false}" 21 | SWIFT_STDLIB_PATH="${DT_TOOLCHAIN_DIR}/usr/lib/swift/${PLATFORM_NAME}" 22 | 23 | # Used as a return value for each invocation of `strip_invalid_archs` function. 24 | STRIP_BINARY_RETVAL=0 25 | 26 | # This protects against multiple targets copying the same framework dependency at the same time. The solution 27 | # was originally proposed here: https://lists.samba.org/archive/rsync/2008-February/020158.html 28 | RSYNC_PROTECT_TMP_FILES=(--filter "P .*.??????") 29 | 30 | # Copies and strips a vendored framework 31 | install_framework() 32 | { 33 | if [ -r "${BUILT_PRODUCTS_DIR}/$1" ]; then 34 | local source="${BUILT_PRODUCTS_DIR}/$1" 35 | elif [ -r "${BUILT_PRODUCTS_DIR}/$(basename "$1")" ]; then 36 | local source="${BUILT_PRODUCTS_DIR}/$(basename "$1")" 37 | elif [ -r "$1" ]; then 38 | local source="$1" 39 | fi 40 | 41 | local destination="${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}" 42 | 43 | if [ -L "${source}" ]; then 44 | echo "Symlinked..." 45 | source="$(readlink "${source}")" 46 | fi 47 | 48 | # Use filter instead of exclude so missing patterns don't throw errors. 49 | echo "rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --filter \"- CVS/\" --filter \"- .svn/\" --filter \"- .git/\" --filter \"- .hg/\" --filter \"- Headers\" --filter \"- PrivateHeaders\" --filter \"- Modules\" \"${source}\" \"${destination}\"" 50 | rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" --filter "- Headers" --filter "- PrivateHeaders" --filter "- Modules" "${source}" "${destination}" 51 | 52 | local basename 53 | basename="$(basename -s .framework "$1")" 54 | binary="${destination}/${basename}.framework/${basename}" 55 | 56 | if ! [ -r "$binary" ]; then 57 | binary="${destination}/${basename}" 58 | elif [ -L "${binary}" ]; then 59 | echo "Destination binary is symlinked..." 60 | dirname="$(dirname "${binary}")" 61 | binary="${dirname}/$(readlink "${binary}")" 62 | fi 63 | 64 | # Strip invalid architectures so "fat" simulator / device frameworks work on device 65 | if [[ "$(file "$binary")" == *"dynamically linked shared library"* ]]; then 66 | strip_invalid_archs "$binary" 67 | fi 68 | 69 | # Resign the code if required by the build settings to avoid unstable apps 70 | code_sign_if_enabled "${destination}/$(basename "$1")" 71 | 72 | # Embed linked Swift runtime libraries. No longer necessary as of Xcode 7. 73 | if [ "${XCODE_VERSION_MAJOR}" -lt 7 ]; then 74 | local swift_runtime_libs 75 | swift_runtime_libs=$(xcrun otool -LX "$binary" | grep --color=never @rpath/libswift | sed -E s/@rpath\\/\(.+dylib\).*/\\1/g | uniq -u) 76 | for lib in $swift_runtime_libs; do 77 | echo "rsync -auv \"${SWIFT_STDLIB_PATH}/${lib}\" \"${destination}\"" 78 | rsync -auv "${SWIFT_STDLIB_PATH}/${lib}" "${destination}" 79 | code_sign_if_enabled "${destination}/${lib}" 80 | done 81 | fi 82 | } 83 | 84 | # Copies and strips a vendored dSYM 85 | install_dsym() { 86 | local source="$1" 87 | if [ -r "$source" ]; then 88 | # Copy the dSYM into a the targets temp dir. 89 | echo "rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --filter \"- CVS/\" --filter \"- .svn/\" --filter \"- .git/\" --filter \"- .hg/\" --filter \"- Headers\" --filter \"- PrivateHeaders\" --filter \"- Modules\" \"${source}\" \"${DERIVED_FILES_DIR}\"" 90 | rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" --filter "- Headers" --filter "- PrivateHeaders" --filter "- Modules" "${source}" "${DERIVED_FILES_DIR}" 91 | 92 | local basename 93 | basename="$(basename -s .framework.dSYM "$source")" 94 | binary="${DERIVED_FILES_DIR}/${basename}.framework.dSYM/Contents/Resources/DWARF/${basename}" 95 | 96 | # Strip invalid architectures so "fat" simulator / device frameworks work on device 97 | if [[ "$(file "$binary")" == *"Mach-O dSYM companion"* ]]; then 98 | strip_invalid_archs "$binary" 99 | fi 100 | 101 | if [[ $STRIP_BINARY_RETVAL == 1 ]]; then 102 | # Move the stripped file into its final destination. 103 | echo "rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --filter \"- CVS/\" --filter \"- .svn/\" --filter \"- .git/\" --filter \"- .hg/\" --filter \"- Headers\" --filter \"- PrivateHeaders\" --filter \"- Modules\" \"${DERIVED_FILES_DIR}/${basename}.framework.dSYM\" \"${DWARF_DSYM_FOLDER_PATH}\"" 104 | rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" --filter "- Headers" --filter "- PrivateHeaders" --filter "- Modules" "${DERIVED_FILES_DIR}/${basename}.framework.dSYM" "${DWARF_DSYM_FOLDER_PATH}" 105 | else 106 | # The dSYM was not stripped at all, in this case touch a fake folder so the input/output paths from Xcode do not reexecute this script because the file is missing. 107 | touch "${DWARF_DSYM_FOLDER_PATH}/${basename}.framework.dSYM" 108 | fi 109 | fi 110 | } 111 | 112 | # Copies the bcsymbolmap files of a vendored framework 113 | install_bcsymbolmap() { 114 | local bcsymbolmap_path="$1" 115 | local destination="${BUILT_PRODUCTS_DIR}" 116 | echo "rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" --filter "- Headers" --filter "- PrivateHeaders" --filter "- Modules" "${bcsymbolmap_path}" "${destination}"" 117 | rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" --filter "- Headers" --filter "- PrivateHeaders" --filter "- Modules" "${bcsymbolmap_path}" "${destination}" 118 | } 119 | 120 | # Signs a framework with the provided identity 121 | code_sign_if_enabled() { 122 | if [ -n "${EXPANDED_CODE_SIGN_IDENTITY:-}" -a "${CODE_SIGNING_REQUIRED:-}" != "NO" -a "${CODE_SIGNING_ALLOWED}" != "NO" ]; then 123 | # Use the current code_sign_identity 124 | echo "Code Signing $1 with Identity ${EXPANDED_CODE_SIGN_IDENTITY_NAME}" 125 | local code_sign_cmd="/usr/bin/codesign --force --sign ${EXPANDED_CODE_SIGN_IDENTITY} ${OTHER_CODE_SIGN_FLAGS:-} --preserve-metadata=identifier,entitlements '$1'" 126 | 127 | if [ "${COCOAPODS_PARALLEL_CODE_SIGN}" == "true" ]; then 128 | code_sign_cmd="$code_sign_cmd &" 129 | fi 130 | echo "$code_sign_cmd" 131 | eval "$code_sign_cmd" 132 | fi 133 | } 134 | 135 | # Strip invalid architectures 136 | strip_invalid_archs() { 137 | binary="$1" 138 | # Get architectures for current target binary 139 | binary_archs="$(lipo -info "$binary" | rev | cut -d ':' -f1 | awk '{$1=$1;print}' | rev)" 140 | # Intersect them with the architectures we are building for 141 | intersected_archs="$(echo ${ARCHS[@]} ${binary_archs[@]} | tr ' ' '\n' | sort | uniq -d)" 142 | # If there are no archs supported by this binary then warn the user 143 | if [[ -z "$intersected_archs" ]]; then 144 | echo "warning: [CP] Vendored binary '$binary' contains architectures ($binary_archs) none of which match the current build architectures ($ARCHS)." 145 | STRIP_BINARY_RETVAL=0 146 | return 147 | fi 148 | stripped="" 149 | for arch in $binary_archs; do 150 | if ! [[ "${ARCHS}" == *"$arch"* ]]; then 151 | # Strip non-valid architectures in-place 152 | lipo -remove "$arch" -output "$binary" "$binary" 153 | stripped="$stripped $arch" 154 | fi 155 | done 156 | if [[ "$stripped" ]]; then 157 | echo "Stripped $binary of architectures:$stripped" 158 | fi 159 | STRIP_BINARY_RETVAL=1 160 | } 161 | 162 | 163 | if [[ "$CONFIGURATION" == "Debug" ]]; then 164 | install_framework "${BUILT_PRODUCTS_DIR}/FMDB/FMDB.framework" 165 | install_framework "${BUILT_PRODUCTS_DIR}/XWDatabase/XWDatabase.framework" 166 | fi 167 | if [[ "$CONFIGURATION" == "Release" ]]; then 168 | install_framework "${BUILT_PRODUCTS_DIR}/FMDB/FMDB.framework" 169 | install_framework "${BUILT_PRODUCTS_DIR}/XWDatabase/XWDatabase.framework" 170 | fi 171 | if [ "${COCOAPODS_PARALLEL_CODE_SIGN}" == "true" ]; then 172 | wait 173 | fi 174 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/Pods-XWDatabase_Example/Pods-XWDatabase_Example-umbrella.h: -------------------------------------------------------------------------------- 1 | #ifdef __OBJC__ 2 | #import 3 | #else 4 | #ifndef FOUNDATION_EXPORT 5 | #if defined(__cplusplus) 6 | #define FOUNDATION_EXPORT extern "C" 7 | #else 8 | #define FOUNDATION_EXPORT extern 9 | #endif 10 | #endif 11 | #endif 12 | 13 | 14 | FOUNDATION_EXPORT double Pods_XWDatabase_ExampleVersionNumber; 15 | FOUNDATION_EXPORT const unsigned char Pods_XWDatabase_ExampleVersionString[]; 16 | 17 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/Pods-XWDatabase_Example/Pods-XWDatabase_Example.debug.xcconfig: -------------------------------------------------------------------------------- 1 | FRAMEWORK_SEARCH_PATHS = $(inherited) "${PODS_CONFIGURATION_BUILD_DIR}/FMDB" "${PODS_CONFIGURATION_BUILD_DIR}/XWDatabase" 2 | GCC_PREPROCESSOR_DEFINITIONS = $(inherited) COCOAPODS=1 3 | HEADER_SEARCH_PATHS = $(inherited) "${PODS_CONFIGURATION_BUILD_DIR}/FMDB/FMDB.framework/Headers" "${PODS_CONFIGURATION_BUILD_DIR}/XWDatabase/XWDatabase.framework/Headers" 4 | LD_RUNPATH_SEARCH_PATHS = $(inherited) '@executable_path/Frameworks' '@loader_path/Frameworks' 5 | OTHER_LDFLAGS = $(inherited) -l"sqlite3" -framework "FMDB" -framework "XWDatabase" 6 | PODS_BUILD_DIR = ${BUILD_DIR} 7 | PODS_CONFIGURATION_BUILD_DIR = ${PODS_BUILD_DIR}/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME) 8 | PODS_PODFILE_DIR_PATH = ${SRCROOT}/. 9 | PODS_ROOT = ${SRCROOT}/Pods 10 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/Pods-XWDatabase_Example/Pods-XWDatabase_Example.modulemap: -------------------------------------------------------------------------------- 1 | framework module Pods_XWDatabase_Example { 2 | umbrella header "Pods-XWDatabase_Example-umbrella.h" 3 | 4 | export * 5 | module * { export * } 6 | } 7 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/Pods-XWDatabase_Example/Pods-XWDatabase_Example.release.xcconfig: -------------------------------------------------------------------------------- 1 | FRAMEWORK_SEARCH_PATHS = $(inherited) "${PODS_CONFIGURATION_BUILD_DIR}/FMDB" "${PODS_CONFIGURATION_BUILD_DIR}/XWDatabase" 2 | GCC_PREPROCESSOR_DEFINITIONS = $(inherited) COCOAPODS=1 3 | HEADER_SEARCH_PATHS = $(inherited) "${PODS_CONFIGURATION_BUILD_DIR}/FMDB/FMDB.framework/Headers" "${PODS_CONFIGURATION_BUILD_DIR}/XWDatabase/XWDatabase.framework/Headers" 4 | LD_RUNPATH_SEARCH_PATHS = $(inherited) '@executable_path/Frameworks' '@loader_path/Frameworks' 5 | OTHER_LDFLAGS = $(inherited) -l"sqlite3" -framework "FMDB" -framework "XWDatabase" 6 | PODS_BUILD_DIR = ${BUILD_DIR} 7 | PODS_CONFIGURATION_BUILD_DIR = ${PODS_BUILD_DIR}/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME) 8 | PODS_PODFILE_DIR_PATH = ${SRCROOT}/. 9 | PODS_ROOT = ${SRCROOT}/Pods 10 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/Pods-XWDatabase_Tests/Pods-XWDatabase_Tests-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | ${PRODUCT_BUNDLE_IDENTIFIER} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | ${CURRENT_PROJECT_VERSION} 23 | NSPrincipalClass 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/Pods-XWDatabase_Tests/Pods-XWDatabase_Tests-acknowledgements.markdown: -------------------------------------------------------------------------------- 1 | # Acknowledgements 2 | This application makes use of the following third party libraries: 3 | Generated by CocoaPods - https://cocoapods.org 4 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/Pods-XWDatabase_Tests/Pods-XWDatabase_Tests-acknowledgements.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | PreferenceSpecifiers 6 | 7 | 8 | FooterText 9 | This application makes use of the following third party libraries: 10 | Title 11 | Acknowledgements 12 | Type 13 | PSGroupSpecifier 14 | 15 | 16 | FooterText 17 | Generated by CocoaPods - https://cocoapods.org 18 | Title 19 | 20 | Type 21 | PSGroupSpecifier 22 | 23 | 24 | StringsTable 25 | Acknowledgements 26 | Title 27 | Acknowledgements 28 | 29 | 30 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/Pods-XWDatabase_Tests/Pods-XWDatabase_Tests-dummy.m: -------------------------------------------------------------------------------- 1 | #import 2 | @interface PodsDummy_Pods_XWDatabase_Tests : NSObject 3 | @end 4 | @implementation PodsDummy_Pods_XWDatabase_Tests 5 | @end 6 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/Pods-XWDatabase_Tests/Pods-XWDatabase_Tests-frameworks.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | set -u 4 | set -o pipefail 5 | 6 | function on_error { 7 | echo "$(realpath -mq "${0}"):$1: error: Unexpected failure" 8 | } 9 | trap 'on_error $LINENO' ERR 10 | 11 | if [ -z ${FRAMEWORKS_FOLDER_PATH+x} ]; then 12 | # If FRAMEWORKS_FOLDER_PATH is not set, then there's nowhere for us to copy 13 | # frameworks to, so exit 0 (signalling the script phase was successful). 14 | exit 0 15 | fi 16 | 17 | echo "mkdir -p ${CONFIGURATION_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}" 18 | mkdir -p "${CONFIGURATION_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}" 19 | 20 | COCOAPODS_PARALLEL_CODE_SIGN="${COCOAPODS_PARALLEL_CODE_SIGN:-false}" 21 | SWIFT_STDLIB_PATH="${DT_TOOLCHAIN_DIR}/usr/lib/swift/${PLATFORM_NAME}" 22 | 23 | # Used as a return value for each invocation of `strip_invalid_archs` function. 24 | STRIP_BINARY_RETVAL=0 25 | 26 | # This protects against multiple targets copying the same framework dependency at the same time. The solution 27 | # was originally proposed here: https://lists.samba.org/archive/rsync/2008-February/020158.html 28 | RSYNC_PROTECT_TMP_FILES=(--filter "P .*.??????") 29 | 30 | # Copies and strips a vendored framework 31 | install_framework() 32 | { 33 | if [ -r "${BUILT_PRODUCTS_DIR}/$1" ]; then 34 | local source="${BUILT_PRODUCTS_DIR}/$1" 35 | elif [ -r "${BUILT_PRODUCTS_DIR}/$(basename "$1")" ]; then 36 | local source="${BUILT_PRODUCTS_DIR}/$(basename "$1")" 37 | elif [ -r "$1" ]; then 38 | local source="$1" 39 | fi 40 | 41 | local destination="${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}" 42 | 43 | if [ -L "${source}" ]; then 44 | echo "Symlinked..." 45 | source="$(readlink "${source}")" 46 | fi 47 | 48 | # Use filter instead of exclude so missing patterns don't throw errors. 49 | echo "rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --filter \"- CVS/\" --filter \"- .svn/\" --filter \"- .git/\" --filter \"- .hg/\" --filter \"- Headers\" --filter \"- PrivateHeaders\" --filter \"- Modules\" \"${source}\" \"${destination}\"" 50 | rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" --filter "- Headers" --filter "- PrivateHeaders" --filter "- Modules" "${source}" "${destination}" 51 | 52 | local basename 53 | basename="$(basename -s .framework "$1")" 54 | binary="${destination}/${basename}.framework/${basename}" 55 | 56 | if ! [ -r "$binary" ]; then 57 | binary="${destination}/${basename}" 58 | elif [ -L "${binary}" ]; then 59 | echo "Destination binary is symlinked..." 60 | dirname="$(dirname "${binary}")" 61 | binary="${dirname}/$(readlink "${binary}")" 62 | fi 63 | 64 | # Strip invalid architectures so "fat" simulator / device frameworks work on device 65 | if [[ "$(file "$binary")" == *"dynamically linked shared library"* ]]; then 66 | strip_invalid_archs "$binary" 67 | fi 68 | 69 | # Resign the code if required by the build settings to avoid unstable apps 70 | code_sign_if_enabled "${destination}/$(basename "$1")" 71 | 72 | # Embed linked Swift runtime libraries. No longer necessary as of Xcode 7. 73 | if [ "${XCODE_VERSION_MAJOR}" -lt 7 ]; then 74 | local swift_runtime_libs 75 | swift_runtime_libs=$(xcrun otool -LX "$binary" | grep --color=never @rpath/libswift | sed -E s/@rpath\\/\(.+dylib\).*/\\1/g | uniq -u) 76 | for lib in $swift_runtime_libs; do 77 | echo "rsync -auv \"${SWIFT_STDLIB_PATH}/${lib}\" \"${destination}\"" 78 | rsync -auv "${SWIFT_STDLIB_PATH}/${lib}" "${destination}" 79 | code_sign_if_enabled "${destination}/${lib}" 80 | done 81 | fi 82 | } 83 | 84 | # Copies and strips a vendored dSYM 85 | install_dsym() { 86 | local source="$1" 87 | if [ -r "$source" ]; then 88 | # Copy the dSYM into a the targets temp dir. 89 | echo "rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --filter \"- CVS/\" --filter \"- .svn/\" --filter \"- .git/\" --filter \"- .hg/\" --filter \"- Headers\" --filter \"- PrivateHeaders\" --filter \"- Modules\" \"${source}\" \"${DERIVED_FILES_DIR}\"" 90 | rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" --filter "- Headers" --filter "- PrivateHeaders" --filter "- Modules" "${source}" "${DERIVED_FILES_DIR}" 91 | 92 | local basename 93 | basename="$(basename -s .framework.dSYM "$source")" 94 | binary="${DERIVED_FILES_DIR}/${basename}.framework.dSYM/Contents/Resources/DWARF/${basename}" 95 | 96 | # Strip invalid architectures so "fat" simulator / device frameworks work on device 97 | if [[ "$(file "$binary")" == *"Mach-O dSYM companion"* ]]; then 98 | strip_invalid_archs "$binary" 99 | fi 100 | 101 | if [[ $STRIP_BINARY_RETVAL == 1 ]]; then 102 | # Move the stripped file into its final destination. 103 | echo "rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --filter \"- CVS/\" --filter \"- .svn/\" --filter \"- .git/\" --filter \"- .hg/\" --filter \"- Headers\" --filter \"- PrivateHeaders\" --filter \"- Modules\" \"${DERIVED_FILES_DIR}/${basename}.framework.dSYM\" \"${DWARF_DSYM_FOLDER_PATH}\"" 104 | rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" --filter "- Headers" --filter "- PrivateHeaders" --filter "- Modules" "${DERIVED_FILES_DIR}/${basename}.framework.dSYM" "${DWARF_DSYM_FOLDER_PATH}" 105 | else 106 | # The dSYM was not stripped at all, in this case touch a fake folder so the input/output paths from Xcode do not reexecute this script because the file is missing. 107 | touch "${DWARF_DSYM_FOLDER_PATH}/${basename}.framework.dSYM" 108 | fi 109 | fi 110 | } 111 | 112 | # Copies the bcsymbolmap files of a vendored framework 113 | install_bcsymbolmap() { 114 | local bcsymbolmap_path = "$1" 115 | local destination="${TARGET_BUILD_DIR}" 116 | echo "rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" --filter "- Headers" --filter "- PrivateHeaders" --filter "- Modules" "${bcsymbolmap_path}" "${destination}"" 117 | rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" --filter "- Headers" --filter "- PrivateHeaders" --filter "- Modules" "${bcsymbolmap_path}" "${destination}" 118 | } 119 | 120 | # Signs a framework with the provided identity 121 | code_sign_if_enabled() { 122 | if [ -n "${EXPANDED_CODE_SIGN_IDENTITY:-}" -a "${CODE_SIGNING_REQUIRED:-}" != "NO" -a "${CODE_SIGNING_ALLOWED}" != "NO" ]; then 123 | # Use the current code_sign_identity 124 | echo "Code Signing $1 with Identity ${EXPANDED_CODE_SIGN_IDENTITY_NAME}" 125 | local code_sign_cmd="/usr/bin/codesign --force --sign ${EXPANDED_CODE_SIGN_IDENTITY} ${OTHER_CODE_SIGN_FLAGS:-} --preserve-metadata=identifier,entitlements '$1'" 126 | 127 | if [ "${COCOAPODS_PARALLEL_CODE_SIGN}" == "true" ]; then 128 | code_sign_cmd="$code_sign_cmd &" 129 | fi 130 | echo "$code_sign_cmd" 131 | eval "$code_sign_cmd" 132 | fi 133 | } 134 | 135 | # Strip invalid architectures 136 | strip_invalid_archs() { 137 | binary="$1" 138 | # Get architectures for current target binary 139 | binary_archs="$(lipo -info "$binary" | rev | cut -d ':' -f1 | awk '{$1=$1;print}' | rev)" 140 | # Intersect them with the architectures we are building for 141 | intersected_archs="$(echo ${ARCHS[@]} ${binary_archs[@]} | tr ' ' '\n' | sort | uniq -d)" 142 | # If there are no archs supported by this binary then warn the user 143 | if [[ -z "$intersected_archs" ]]; then 144 | echo "warning: [CP] Vendored binary '$binary' contains architectures ($binary_archs) none of which match the current build architectures ($ARCHS)." 145 | STRIP_BINARY_RETVAL=0 146 | return 147 | fi 148 | stripped="" 149 | for arch in $binary_archs; do 150 | if ! [[ "${ARCHS}" == *"$arch"* ]]; then 151 | # Strip non-valid architectures in-place 152 | lipo -remove "$arch" -output "$binary" "$binary" 153 | stripped="$stripped $arch" 154 | fi 155 | done 156 | if [[ "$stripped" ]]; then 157 | echo "Stripped $binary of architectures:$stripped" 158 | fi 159 | STRIP_BINARY_RETVAL=1 160 | } 161 | 162 | 163 | if [[ "$CONFIGURATION" == "Debug" ]]; then 164 | install_framework "${BUILT_PRODUCTS_DIR}/FMDB/FMDB.framework" 165 | install_framework "${BUILT_PRODUCTS_DIR}/XWDatabase/XWDatabase.framework" 166 | fi 167 | if [[ "$CONFIGURATION" == "Release" ]]; then 168 | install_framework "${BUILT_PRODUCTS_DIR}/FMDB/FMDB.framework" 169 | install_framework "${BUILT_PRODUCTS_DIR}/XWDatabase/XWDatabase.framework" 170 | fi 171 | if [ "${COCOAPODS_PARALLEL_CODE_SIGN}" == "true" ]; then 172 | wait 173 | fi 174 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/Pods-XWDatabase_Tests/Pods-XWDatabase_Tests-umbrella.h: -------------------------------------------------------------------------------- 1 | #ifdef __OBJC__ 2 | #import 3 | #else 4 | #ifndef FOUNDATION_EXPORT 5 | #if defined(__cplusplus) 6 | #define FOUNDATION_EXPORT extern "C" 7 | #else 8 | #define FOUNDATION_EXPORT extern 9 | #endif 10 | #endif 11 | #endif 12 | 13 | 14 | FOUNDATION_EXPORT double Pods_XWDatabase_TestsVersionNumber; 15 | FOUNDATION_EXPORT const unsigned char Pods_XWDatabase_TestsVersionString[]; 16 | 17 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/Pods-XWDatabase_Tests/Pods-XWDatabase_Tests.debug.xcconfig: -------------------------------------------------------------------------------- 1 | FRAMEWORK_SEARCH_PATHS = $(inherited) "${PODS_CONFIGURATION_BUILD_DIR}/FMDB" "${PODS_CONFIGURATION_BUILD_DIR}/XWDatabase" 2 | GCC_PREPROCESSOR_DEFINITIONS = $(inherited) COCOAPODS=1 3 | HEADER_SEARCH_PATHS = $(inherited) "${PODS_CONFIGURATION_BUILD_DIR}/FMDB/FMDB.framework/Headers" "${PODS_CONFIGURATION_BUILD_DIR}/XWDatabase/XWDatabase.framework/Headers" 4 | OTHER_LDFLAGS = $(inherited) -l"sqlite3" -framework "FMDB" -framework "XWDatabase" 5 | PODS_BUILD_DIR = ${BUILD_DIR} 6 | PODS_CONFIGURATION_BUILD_DIR = ${PODS_BUILD_DIR}/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME) 7 | PODS_PODFILE_DIR_PATH = ${SRCROOT}/. 8 | PODS_ROOT = ${SRCROOT}/Pods 9 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/Pods-XWDatabase_Tests/Pods-XWDatabase_Tests.modulemap: -------------------------------------------------------------------------------- 1 | framework module Pods_XWDatabase_Tests { 2 | umbrella header "Pods-XWDatabase_Tests-umbrella.h" 3 | 4 | export * 5 | module * { export * } 6 | } 7 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/Pods-XWDatabase_Tests/Pods-XWDatabase_Tests.release.xcconfig: -------------------------------------------------------------------------------- 1 | FRAMEWORK_SEARCH_PATHS = $(inherited) "${PODS_CONFIGURATION_BUILD_DIR}/FMDB" "${PODS_CONFIGURATION_BUILD_DIR}/XWDatabase" 2 | GCC_PREPROCESSOR_DEFINITIONS = $(inherited) COCOAPODS=1 3 | HEADER_SEARCH_PATHS = $(inherited) "${PODS_CONFIGURATION_BUILD_DIR}/FMDB/FMDB.framework/Headers" "${PODS_CONFIGURATION_BUILD_DIR}/XWDatabase/XWDatabase.framework/Headers" 4 | OTHER_LDFLAGS = $(inherited) -l"sqlite3" -framework "FMDB" -framework "XWDatabase" 5 | PODS_BUILD_DIR = ${BUILD_DIR} 6 | PODS_CONFIGURATION_BUILD_DIR = ${PODS_BUILD_DIR}/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME) 7 | PODS_PODFILE_DIR_PATH = ${SRCROOT}/. 8 | PODS_ROOT = ${SRCROOT}/Pods 9 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/XWDatabase/XWDatabase-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | ${PRODUCT_BUNDLE_IDENTIFIER} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.2.2 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | ${CURRENT_PROJECT_VERSION} 23 | NSPrincipalClass 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/XWDatabase/XWDatabase-dummy.m: -------------------------------------------------------------------------------- 1 | #import 2 | @interface PodsDummy_XWDatabase : NSObject 3 | @end 4 | @implementation PodsDummy_XWDatabase 5 | @end 6 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/XWDatabase/XWDatabase-prefix.pch: -------------------------------------------------------------------------------- 1 | #ifdef __OBJC__ 2 | #import 3 | #else 4 | #ifndef FOUNDATION_EXPORT 5 | #if defined(__cplusplus) 6 | #define FOUNDATION_EXPORT extern "C" 7 | #else 8 | #define FOUNDATION_EXPORT extern 9 | #endif 10 | #endif 11 | #endif 12 | 13 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/XWDatabase/XWDatabase-umbrella.h: -------------------------------------------------------------------------------- 1 | #ifdef __OBJC__ 2 | #import 3 | #else 4 | #ifndef FOUNDATION_EXPORT 5 | #if defined(__cplusplus) 6 | #define FOUNDATION_EXPORT extern "C" 7 | #else 8 | #define FOUNDATION_EXPORT extern 9 | #endif 10 | #endif 11 | #endif 12 | 13 | #import "NSObject+XWModel.h" 14 | #import "XWDatabase.h" 15 | #import "XWDatabaseDataModel.h" 16 | #import "XWDatabaseModel.h" 17 | #import "XWDatabaseModelProtocol.h" 18 | #import "XWDatabaseQueue.h" 19 | #import "XWDatabaseSQL.h" 20 | #import "XWLivingThread.h" 21 | 22 | FOUNDATION_EXPORT double XWDatabaseVersionNumber; 23 | FOUNDATION_EXPORT const unsigned char XWDatabaseVersionString[]; 24 | 25 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/XWDatabase/XWDatabase.modulemap: -------------------------------------------------------------------------------- 1 | framework module XWDatabase { 2 | umbrella header "XWDatabase-umbrella.h" 3 | 4 | export * 5 | module * { export * } 6 | } 7 | -------------------------------------------------------------------------------- /Example/Pods/Target Support Files/XWDatabase/XWDatabase.xcconfig: -------------------------------------------------------------------------------- 1 | CONFIGURATION_BUILD_DIR = ${PODS_CONFIGURATION_BUILD_DIR}/XWDatabase 2 | FRAMEWORK_SEARCH_PATHS = $(inherited) "${PODS_CONFIGURATION_BUILD_DIR}/FMDB" 3 | GCC_PREPROCESSOR_DEFINITIONS = $(inherited) COCOAPODS=1 4 | PODS_BUILD_DIR = ${BUILD_DIR} 5 | PODS_CONFIGURATION_BUILD_DIR = ${PODS_BUILD_DIR}/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME) 6 | PODS_ROOT = ${SRCROOT} 7 | PODS_TARGET_SRCROOT = ${PODS_ROOT}/../.. 8 | PRODUCT_BUNDLE_IDENTIFIER = org.cocoapods.${PRODUCT_NAME:rfc1034identifier} 9 | SKIP_INSTALL = YES 10 | -------------------------------------------------------------------------------- /Example/Tests/Tests-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundlePackageType 14 | BNDL 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleSignature 18 | ???? 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /Example/Tests/Tests-Prefix.pch: -------------------------------------------------------------------------------- 1 | // The contents of this file are implicitly included at the beginning of every test case source file. 2 | 3 | #ifdef __OBJC__ 4 | 5 | 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /Example/Tests/Tests.m: -------------------------------------------------------------------------------- 1 | // 2 | // XWDatabaseTests.m 3 | // XWDatabaseTests 4 | // 5 | // Created by qxuewei@yeah.net on 12/06/2018. 6 | // Copyright (c) 2018 qxuewei@yeah.net. All rights reserved. 7 | // 8 | 9 | @import XCTest; 10 | 11 | @interface Tests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation Tests 16 | 17 | - (void)setUp 18 | { 19 | [super setUp]; 20 | // Put setup code here. This method is called before the invocation of each test method in the class. 21 | } 22 | 23 | - (void)tearDown 24 | { 25 | // Put teardown code here. This method is called after the invocation of each test method in the class. 26 | [super tearDown]; 27 | } 28 | 29 | - (void)testExample 30 | { 31 | XCTFail(@"No implementation for \"%s\"", __PRETTY_FUNCTION__); 32 | } 33 | 34 | 35 | @end 36 | 37 | -------------------------------------------------------------------------------- /Example/Tests/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /Example/XWDatabase.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Example/XWDatabase.xcodeproj/xcshareddata/xcschemes/XWDatabase-Example.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 42 | 43 | 49 | 50 | 51 | 52 | 53 | 54 | 64 | 66 | 72 | 73 | 74 | 75 | 76 | 77 | 83 | 85 | 91 | 92 | 93 | 94 | 96 | 97 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /Example/XWDatabase.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Example/XWDatabase.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Example/XWDatabase/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "size" : "20x20", 5 | "idiom" : "iphone", 6 | "filename" : "icon-20@2x.png", 7 | "scale" : "2x" 8 | }, 9 | { 10 | "size" : "20x20", 11 | "idiom" : "iphone", 12 | "filename" : "icon-20@3x.png", 13 | "scale" : "3x" 14 | }, 15 | { 16 | "size" : "29x29", 17 | "idiom" : "iphone", 18 | "filename" : "icon-29.png", 19 | "scale" : "1x" 20 | }, 21 | { 22 | "size" : "29x29", 23 | "idiom" : "iphone", 24 | "filename" : "icon-29@2x.png", 25 | "scale" : "2x" 26 | }, 27 | { 28 | "size" : "29x29", 29 | "idiom" : "iphone", 30 | "filename" : "icon-29@3x.png", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "size" : "40x40", 35 | "idiom" : "iphone", 36 | "filename" : "icon-40@2x.png", 37 | "scale" : "2x" 38 | }, 39 | { 40 | "size" : "40x40", 41 | "idiom" : "iphone", 42 | "filename" : "icon-40@3x.png", 43 | "scale" : "3x" 44 | }, 45 | { 46 | "size" : "57x57", 47 | "idiom" : "iphone", 48 | "filename" : "icon-57.png", 49 | "scale" : "1x" 50 | }, 51 | { 52 | "size" : "57x57", 53 | "idiom" : "iphone", 54 | "filename" : "icon-57@2x.png", 55 | "scale" : "2x" 56 | }, 57 | { 58 | "size" : "60x60", 59 | "idiom" : "iphone", 60 | "filename" : "icon-60@2x.png", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "size" : "60x60", 65 | "idiom" : "iphone", 66 | "filename" : "icon-60@3x.png", 67 | "scale" : "3x" 68 | }, 69 | { 70 | "size" : "20x20", 71 | "idiom" : "ipad", 72 | "filename" : "icon-20-ipad.png", 73 | "scale" : "1x" 74 | }, 75 | { 76 | "size" : "20x20", 77 | "idiom" : "ipad", 78 | "filename" : "icon-20@2x-ipad.png", 79 | "scale" : "2x" 80 | }, 81 | { 82 | "size" : "29x29", 83 | "idiom" : "ipad", 84 | "filename" : "icon-29-ipad.png", 85 | "scale" : "1x" 86 | }, 87 | { 88 | "size" : "29x29", 89 | "idiom" : "ipad", 90 | "filename" : "icon-29@2x-ipad.png", 91 | "scale" : "2x" 92 | }, 93 | { 94 | "size" : "40x40", 95 | "idiom" : "ipad", 96 | "filename" : "icon-40.png", 97 | "scale" : "1x" 98 | }, 99 | { 100 | "size" : "40x40", 101 | "idiom" : "ipad", 102 | "filename" : "icon-40@2x.png", 103 | "scale" : "2x" 104 | }, 105 | { 106 | "size" : "50x50", 107 | "idiom" : "ipad", 108 | "filename" : "icon-50.png", 109 | "scale" : "1x" 110 | }, 111 | { 112 | "size" : "50x50", 113 | "idiom" : "ipad", 114 | "filename" : "icon-50@2x.png", 115 | "scale" : "2x" 116 | }, 117 | { 118 | "size" : "72x72", 119 | "idiom" : "ipad", 120 | "filename" : "icon-72.png", 121 | "scale" : "1x" 122 | }, 123 | { 124 | "size" : "72x72", 125 | "idiom" : "ipad", 126 | "filename" : "icon-72@2x.png", 127 | "scale" : "2x" 128 | }, 129 | { 130 | "size" : "76x76", 131 | "idiom" : "ipad", 132 | "filename" : "icon-76.png", 133 | "scale" : "1x" 134 | }, 135 | { 136 | "size" : "76x76", 137 | "idiom" : "ipad", 138 | "filename" : "icon-76@2x.png", 139 | "scale" : "2x" 140 | }, 141 | { 142 | "size" : "83.5x83.5", 143 | "idiom" : "ipad", 144 | "filename" : "icon-83.5@2x.png", 145 | "scale" : "2x" 146 | }, 147 | { 148 | "size" : "1024x1024", 149 | "idiom" : "ios-marketing", 150 | "filename" : "icon-1024.png", 151 | "scale" : "1x" 152 | } 153 | ], 154 | "info" : { 155 | "version" : 1, 156 | "author" : "xcode" 157 | } 158 | } -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-1024.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-1024.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-20-ipad.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-20-ipad.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-20@2x-ipad.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-20@2x-ipad.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-20@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-20@2x.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-20@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-20@3x.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-29-ipad.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-29-ipad.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-29.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-29.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-29@2x-ipad.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-29@2x-ipad.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-29@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-29@2x.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-29@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-29@3x.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-40.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-40.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-40@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-40@2x.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-40@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-40@3x.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-50.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-50.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-50@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-50@2x.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-57.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-57.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-57@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-57@2x.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-60@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-60@2x.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-60@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-60@3x.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-72.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-72.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-72@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-72@2x.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-76.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-76.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-76@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-76@2x.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-83.5@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/AppIcon.appiconset/icon-83.5@2x.png -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/icon.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "icon.jpg", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/icon.imageset/icon.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/icon.imageset/icon.jpg -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/icon_comment_owner.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "icon_comment_owner@2x.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Example/XWDatabase/Images.xcassets/icon_comment_owner.imageset/icon_comment_owner@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/Example/XWDatabase/Images.xcassets/icon_comment_owner.imageset/icon_comment_owner@2x.png -------------------------------------------------------------------------------- /Example/XWDatabase/Model/XWBook.h: -------------------------------------------------------------------------------- 1 | // 2 | // XWBook.h 3 | // XWDatabase_Example 4 | // 5 | // Created by 邱学伟 on 2018/12/11. 6 | // Copyright © 2018 qxuewei@yeah.net. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "XWDatabaseModelProtocol.h" 11 | 12 | @class XWTestSubModel; 13 | 14 | NS_ASSUME_NONNULL_BEGIN 15 | 16 | @interface XWBook : NSObject 17 | 18 | @property (nonatomic, assign) int bookId; 19 | @property (nonatomic, copy) NSMutableString *name; 20 | @property (nonatomic, copy) NSString *author; 21 | @property (nonatomic, copy) NSString *bookConcern; 22 | 23 | @property (nonatomic, strong) NSDictionary *dictionary; 24 | @property (nonatomic, strong) NSMutableDictionary *dictionaryM; 25 | 26 | @property (nonatomic, strong) NSArray *array; 27 | @property (nonatomic, strong) NSMutableArray *arrayM; 28 | 29 | 30 | @property (nonatomic, strong) XWTestSubModel *subModel; 31 | 32 | //@property (nonatomic, copy) NSString *temp; 33 | //@property (nonatomic, copy) NSString *temp2; 34 | 35 | 36 | @end 37 | 38 | NS_ASSUME_NONNULL_END 39 | -------------------------------------------------------------------------------- /Example/XWDatabase/Model/XWBook.m: -------------------------------------------------------------------------------- 1 | // 2 | // XWBook.m 3 | // XWDatabase_Example 4 | // 5 | // Created by 邱学伟 on 2018/12/11. 6 | // Copyright © 2018 qxuewei@yeah.net. All rights reserved. 7 | // 8 | 9 | #import "XWBook.h" 10 | #import "XWDatabase.h" 11 | #import "XWTestSubModel.h" 12 | 13 | @implementation XWBook 14 | 15 | #pragma mark - Life Cycle 16 | + (void)initialize { 17 | // static dispatch_once_t onceToken; 18 | // dispatch_once(&onceToken, ^{ 19 | // /// 数据迁移 20 | // [XWDatabase updateTable:self completion:^(BOOL isSuccess) { 21 | // NSLog(@" updateTable (%@)",isSuccess?@"成功":@"失败"); 22 | // }]; 23 | // }); 24 | } 25 | 26 | /// 快速归解档的宏 27 | XWCodingImplementation 28 | 29 | + (NSDictionary *)xw_customModelMapping { 30 | return @{ 31 | @"subModel":[XWTestSubModel class] 32 | }; 33 | } 34 | 35 | + (NSString *)xw_primaryKey { 36 | return @"bookId"; 37 | } 38 | 39 | //+ (NSSet *)xw_ignoreColumnNames { 40 | // return [NSSet setWithObject:@"author"]; 41 | //} 42 | 43 | //+ (NSSet *)xw_specificSaveColumnNames { 44 | // return [NSSet setWithObjects:@"name",@"bookId",@"author",@"subModel", nil]; 45 | //} 46 | 47 | 48 | @end 49 | -------------------------------------------------------------------------------- /Example/XWDatabase/Model/XWImage.h: -------------------------------------------------------------------------------- 1 | // 2 | // XWImage.h 3 | // XWDatabase_Example 4 | // 5 | // Created by 邱学伟 on 2019/1/13. 6 | // Copyright © 2019 qxuewei@yeah.net. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface XWImage : NSObject 14 | 15 | @property (nonatomic, copy) NSString *name; 16 | @property (nonatomic, copy) NSString *filePath; 17 | 18 | @property (nonatomic, copy) NSString *temp; 19 | 20 | @end 21 | 22 | NS_ASSUME_NONNULL_END 23 | -------------------------------------------------------------------------------- /Example/XWDatabase/Model/XWImage.m: -------------------------------------------------------------------------------- 1 | // 2 | // XWImage.m 3 | // XWDatabase_Example 4 | // 5 | // Created by 邱学伟 on 2019/1/13. 6 | // Copyright © 2019 qxuewei@yeah.net. All rights reserved. 7 | // 8 | 9 | #import "XWImage.h" 10 | #import "XWDatabase.h" 11 | 12 | @implementation XWImage 13 | 14 | + (void)initialize { 15 | /// 数据迁移 16 | static dispatch_once_t onceToken; 17 | dispatch_once(&onceToken, ^{ 18 | [XWDatabase updateTable:self completion:^(BOOL isSuccess) { 19 | NSLog(@" updateTable (%@)",isSuccess?@"成功":@"失败"); 20 | }]; 21 | }); 22 | } 23 | 24 | 25 | @end 26 | -------------------------------------------------------------------------------- /Example/XWDatabase/Model/XWPerson.h: -------------------------------------------------------------------------------- 1 | // 2 | // XWPerson.h 3 | // XWDatabase_Example 4 | // 5 | // Created by 邱学伟 on 2018/12/6. 6 | // Copyright © 2018 qxuewei@yeah.net. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "XWDatabaseModelProtocol.h" 11 | @class XWBook; 12 | 13 | NS_ASSUME_NONNULL_BEGIN 14 | 15 | @interface XWPerson : NSObject 16 | 17 | @property (nonatomic, copy) NSString *cardID; 18 | @property (nonatomic, copy) NSString *name; 19 | @property (nonatomic, copy) NSString *sex; 20 | @property (nonatomic, assign) NSInteger age; 21 | @property (nonatomic, strong) NSDate *birthday; 22 | @property (nonatomic, strong) NSMutableData *icon; 23 | @property (nonatomic, strong) NSMutableArray *girls; 24 | @property (nonatomic, strong) NSMutableDictionary *books; 25 | @property (nonatomic, strong) NSNumber *number; 26 | @property (nonatomic, strong) NSNumber *floatNumber; 27 | @property (nonatomic, strong) NSSet *pSet; 28 | @property (nonatomic, strong) NSMutableSet *pSetM; 29 | @property (nonatomic, strong) NSAttributedString *pAttributedString; 30 | @property (nonatomic, strong) NSMutableAttributedString *pMutableAttributedString; 31 | @property (nonatomic, strong) NSIndexPath *indexPath; 32 | @property (nonatomic, assign) CGFloat pCGFloat; 33 | @property (nonatomic, assign) float pFloat; 34 | @property (nonatomic, assign) int pInt; 35 | @property (nonatomic, assign) double pDouble; 36 | @property (nonatomic, assign) long pLong; 37 | @property (nonatomic, assign) BOOL pBOOL; 38 | @property (nonatomic, assign) bool pBooll; 39 | @property (nonatomic, assign) long long pLongLong; 40 | @property (nonatomic, assign) NSInteger pInteger; 41 | @property (nonatomic, assign) NSUInteger pUInteger; 42 | @property (nonatomic, assign) CGPoint pPoint; 43 | @property (nonatomic, assign) CGRect pRect; 44 | @property (nonatomic, assign) CGSize pSize; 45 | 46 | //@property (nonatomic, copy) NSString *temp; 47 | 48 | @property (nonatomic, strong) UIImage *image; 49 | @property (nonatomic, strong) NSURL *URL; 50 | @property (nonatomic, assign) NSRange pRange; 51 | 52 | @property (nonatomic, strong) XWBook *favoriteBook; 53 | 54 | + (XWPerson *)testPerson:(int)index; 55 | 56 | @end 57 | 58 | NS_ASSUME_NONNULL_END 59 | -------------------------------------------------------------------------------- /Example/XWDatabase/Model/XWPerson.m: -------------------------------------------------------------------------------- 1 | // 2 | // XWPerson.m 3 | // XWDatabase_Example 4 | // 5 | // Created by 邱学伟 on 2018/12/6. 6 | // Copyright © 2018 qxuewei@yeah.net. All rights reserved. 7 | // 8 | 9 | #import "XWPerson.h" 10 | #import "XWDatabase.h" 11 | #import "XWBook.h" 12 | 13 | @interface XWPerson () 14 | 15 | @property (nonatomic, assign) NSUInteger girlFirendsCount; 16 | @property (nonatomic, copy) NSString *favoriteGirl; 17 | 18 | @end 19 | 20 | @implementation XWPerson 21 | 22 | #pragma mark - public 23 | /// 测试数据 24 | + (XWPerson *)testPerson:(int)index { 25 | XWPerson *person = [[XWPerson alloc] init]; 26 | 27 | NSString *testString = @"Abe's Crazy English \\ ' & $ ^ ' ' * & & | } {' : > ? < ,@ ! ~ ` HAHAHA"; 28 | 29 | person.cardID = [NSString stringWithFormat:@"%d",index]; 30 | person.age = 18 + arc4random_uniform(100); 31 | person.name = person.age % 2 == 0 ? @"极客学伟" : testString; 32 | person.sex = arc4random_uniform(2) == 1 ? @"Male" : @"male"; 33 | person.birthday = [NSDate date]; 34 | person.girls = @[@"小妹",@"校花",@"小baby",testString].mutableCopy; 35 | person.books = @{@"name":@"iOS 从入门到掉头发",@"test":testString}.mutableCopy; 36 | person.number = [NSNumber numberWithBool:YES]; 37 | person.floatNumber = [NSNumber numberWithFloat:3.1415926]; 38 | 39 | /// 存储二进制文件 40 | UIImage *image = [UIImage imageNamed:@"icon"]; 41 | person.image = image; 42 | // person.icon = UIImageJPEGRepresentation(image, 0.5).mutableCopy; 43 | 44 | person.pFloat = 1.1111; 45 | person.pInt = 3; 46 | person.pDouble = 2.2222; 47 | person.pLong = 88; 48 | person.pLongLong = 999999999999; 49 | person.pBOOL = YES; 50 | person.pBooll = false; 51 | person.pInteger = -10086; 52 | person.pUInteger = 10010; 53 | person.pCGFloat = 3.33; 54 | person.pPoint = CGPointMake(10, 10); 55 | person.pRect = CGRectMake(0, 0, 100, 100); 56 | person.pSize = CGSizeMake(200, 300); 57 | person.pSet = [NSSet setWithObjects:@"Set",@(123),nil]; 58 | person.pSetM = [NSMutableSet setWithObjects:@"MutableSet",@(456), nil]; 59 | 60 | NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:testString]; 61 | NSTextAttachment *imageAttach = [[NSTextAttachment alloc] init]; 62 | imageAttach.image = [UIImage imageNamed:@"icon_comment_owner"]; 63 | imageAttach.bounds = CGRectMake(4, -2, 10, 5); 64 | NSAttributedString *imageAttri = [NSAttributedString attributedStringWithAttachment:imageAttach]; 65 | [attri appendAttributedString:imageAttri]; 66 | person.pAttributedString = attri; 67 | person.pMutableAttributedString = attri.mutableCopy; 68 | 69 | person.URL = [NSURL URLWithString:@"www.qiuxuewei.com"]; 70 | person.pRange = NSMakeRange(0, 99); 71 | person.indexPath = [NSIndexPath indexPathForItem:1 inSection:2]; 72 | person.girlFirendsCount = 108; 73 | person.favoriteGirl = @"小王"; 74 | 75 | /// 存储自定义对象 76 | // XWBook *book = [XWBook new]; 77 | // book.name = @"name"; 78 | // book.author = @"JK"; 79 | // person.favoriteBook = book; 80 | 81 | return person; 82 | } 83 | 84 | 85 | #pragma mark - Life Cycle 86 | + (void)initialize { 87 | /// 数据迁移 88 | // static dispatch_once_t onceToken; 89 | // dispatch_once(&onceToken, ^{ 90 | // [XWDatabase updateTable:self completion:^(BOOL isSuccess) { 91 | // NSLog(@" updateTable (%@)",isSuccess?@"成功":@"失败"); 92 | // }]; 93 | // }); 94 | } 95 | 96 | #pragma mark - XWDatabaseModelProtocol 97 | 98 | /// 主键 99 | + (NSString *)xw_primaryKey { 100 | return @"cardID"; 101 | } 102 | 103 | /// 联合主键成员变量数组 104 | //+ (NSArray *)xw_unionPrimaryKey { 105 | // return @[@"cardID",@"age"]; 106 | //} 107 | 108 | /// 自定义对象映射` 109 | + (NSDictionary *)xw_customModelMapping { 110 | return @{ 111 | @"favoriteBook" : [XWBook class] 112 | }; 113 | } 114 | 115 | /// 自定义表名 116 | + (NSString *)xw_customTableName { 117 | return @"Person"; 118 | } 119 | 120 | /// 忽略的成员变量 121 | + (NSSet *)xw_ignoreColumnNames { 122 | return [NSSet setWithObject:@"floatNumber"]; 123 | } 124 | 125 | /// 自定义字段名映射表 (默认成员变量即变量名, 可自定义字段名 key: 成员变量(属性)名称 value: 自定义数据库表字段名) 126 | + (NSDictionary *)xw_customColumnMapping { 127 | return @{ 128 | @"sex" : @"gender", 129 | @"girls" : @"sweethearts" 130 | }; 131 | } 132 | 133 | 134 | @end 135 | -------------------------------------------------------------------------------- /Example/XWDatabase/Model/XWTestModel.h: -------------------------------------------------------------------------------- 1 | // 2 | // XWTestModel.h 3 | // XWDatabase_Example 4 | // 5 | // Created by 邱学伟 on 2019/3/11. 6 | // Copyright © 2019 qxuewei@yeah.net. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "XWDatabaseModelProtocol.h" 11 | 12 | @class XWTestSubModel; 13 | 14 | NS_ASSUME_NONNULL_BEGIN 15 | 16 | @interface XWTestModel : NSObject 17 | 18 | @property (nonatomic, copy) NSString *cardID; 19 | @property (nonatomic, copy) NSString *name; 20 | @property (nonatomic, copy) NSString *sex; 21 | @property (nonatomic, assign) NSInteger age; 22 | @property (nonatomic, strong) NSDate *birthday; 23 | @property (nonatomic, strong) NSMutableData *icon; 24 | @property (nonatomic, strong) NSMutableArray *girls; 25 | @property (nonatomic, strong) NSMutableDictionary *books; 26 | @property (nonatomic, strong) NSNumber *number; 27 | @property (nonatomic, strong) NSNumber *floatNumber; 28 | @property (nonatomic, strong) NSSet *pSet; 29 | @property (nonatomic, strong) NSMutableSet *pSetM; 30 | @property (nonatomic, strong) NSAttributedString *pAttributedString; 31 | @property (nonatomic, strong) NSMutableAttributedString *pMutableAttributedString; 32 | @property (nonatomic, strong) NSIndexPath *indexPath; 33 | @property (nonatomic, assign) CGFloat pCGFloat; 34 | @property (nonatomic, assign) float pFloat; 35 | @property (nonatomic, assign) int pInt; 36 | @property (nonatomic, assign) double pDouble; 37 | @property (nonatomic, assign) long pLong; 38 | @property (nonatomic, assign) BOOL pBOOL; 39 | @property (nonatomic, assign) bool pBooll; 40 | @property (nonatomic, assign) long long pLongLong; 41 | @property (nonatomic, assign) NSInteger pInteger; 42 | @property (nonatomic, assign) NSUInteger pUInteger; 43 | @property (nonatomic, assign) CGPoint pPoint; 44 | @property (nonatomic, assign) CGRect pRect; 45 | @property (nonatomic, assign) CGSize pSize; 46 | 47 | //@property (nonatomic, copy) NSString *temp; 48 | 49 | @property (nonatomic, strong) UIImage *image; 50 | @property (nonatomic, strong) NSURL *URL; 51 | @property (nonatomic, assign) NSRange pRange; 52 | 53 | @property (nonatomic, strong) XWTestSubModel *favoriteBook; 54 | 55 | + (XWTestModel *)testModel:(int)index; 56 | 57 | @end 58 | 59 | NS_ASSUME_NONNULL_END 60 | -------------------------------------------------------------------------------- /Example/XWDatabase/Model/XWTestModel.m: -------------------------------------------------------------------------------- 1 | // 2 | // XWTestModel.m 3 | // XWDatabase_Example 4 | // 5 | // Created by 邱学伟 on 2019/3/11. 6 | // Copyright © 2019 qxuewei@yeah.net. All rights reserved. 7 | // 8 | 9 | #import "XWTestModel.h" 10 | #import "XWDatabase.h" 11 | #import "XWTestSubModel.h" 12 | 13 | @interface XWTestModel () 14 | 15 | @property (nonatomic, assign) NSInteger private; 16 | 17 | @end 18 | 19 | @implementation XWTestModel 20 | 21 | #pragma mark - public 22 | /// 测试数据 23 | + (XWTestModel *)testModel:(int)index { 24 | 25 | XWTestModel *model = [[XWTestModel alloc] init]; 26 | model.cardID = [NSString stringWithFormat:@"%d",index]; 27 | model.age = 18 + arc4random_uniform(100); 28 | model.name = model.age % 2 == 0 ? @"极客学伟" : @"www.qiuxuewei.com"; 29 | model.sex = arc4random_uniform(2) == 1 ? @"Male" : @"male"; 30 | model.birthday = [NSDate date]; 31 | model.girls = @[@"小妹",@"校花",@"小baby"].mutableCopy; 32 | model.books = @{@"name":@"iOS 从入门到掉头发"}.mutableCopy; 33 | model.number = [NSNumber numberWithBool:YES]; 34 | model.floatNumber = [NSNumber numberWithFloat:3.1415926]; 35 | 36 | /// 存储二进制文件 37 | UIImage *image = [UIImage imageNamed:@"icon"]; 38 | model.image = image; 39 | // person.icon = UIImageJPEGRepresentation(image, 0.5).mutableCopy; 40 | 41 | model.pFloat = 1.1111; 42 | model.pInt = 3; 43 | model.pDouble = 2.2222; 44 | model.pLong = 88; 45 | model.pLongLong = 999999999999; 46 | model.pBOOL = YES; 47 | model.pBooll = false; 48 | model.pInteger = -10086; 49 | model.pUInteger = 10010; 50 | model.pCGFloat = 3.33; 51 | model.pPoint = CGPointMake(10, 10); 52 | model.pRect = CGRectMake(0, 0, 100, 100); 53 | model.pSize = CGSizeMake(200, 300); 54 | model.pSet = [NSSet setWithObjects:@"Set",@(123),nil]; 55 | model.pSetM = [NSMutableSet setWithObjects:@"MutableSet",@(456), nil]; 56 | model.pAttributedString = [[NSAttributedString alloc] initWithString:@"NSAttributedString"]; 57 | model.pMutableAttributedString = [[NSMutableAttributedString alloc] initWithString:@"NSMutableAttributedString"]; 58 | model.URL = [NSURL URLWithString:@"www.qiuxuewei.com"]; 59 | model.pRange = NSMakeRange(0, 99); 60 | model.indexPath = [NSIndexPath indexPathForItem:1 inSection:2]; 61 | 62 | model.private = 111; 63 | 64 | /// 存储自定义对象 65 | XWTestSubModel *subM = [XWTestSubModel testTestSubModel]; 66 | model.favoriteBook = subM; 67 | 68 | return model; 69 | } 70 | 71 | 72 | #pragma mark - Life Cycle 73 | + (void)initialize { 74 | /// 数据迁移 75 | static dispatch_once_t onceToken; 76 | dispatch_once(&onceToken, ^{ 77 | [XWDatabase updateTable:self completion:^(BOOL isSuccess) { 78 | NSLog(@" updateTable (%@)",isSuccess?@"成功":@"失败"); 79 | }]; 80 | }); 81 | } 82 | 83 | #pragma mark - XWDatabaseModelProtocol 84 | 85 | /// 主键 86 | + (NSString *)xw_primaryKey { 87 | return @"cardID"; 88 | } 89 | 90 | /// 联合主键成员变量数组 91 | //+ (NSArray *)xw_unionPrimaryKey { 92 | // return @[@"cardID",@"age"]; 93 | //} 94 | 95 | /// 自定义对象映射 96 | + (NSDictionary *)xw_customModelMapping { 97 | return @{ 98 | @"favoriteBook" : [XWTestSubModel class] 99 | }; 100 | } 101 | 102 | 103 | /// 自定义字段名映射表 (默认成员变量即变量名, 可自定义字段名 key: 成员变量(属性)名称 value: 自定义数据库表字段名) 104 | + (NSDictionary *)xw_customColumnMapping { 105 | return @{ 106 | @"sex" : @"gender", 107 | @"girls" : @"sweethearts" 108 | }; 109 | } 110 | 111 | 112 | @end 113 | -------------------------------------------------------------------------------- /Example/XWDatabase/Model/XWTestSubModel.h: -------------------------------------------------------------------------------- 1 | // 2 | // XWTestSubModel.h 3 | // XWDatabase_Example 4 | // 5 | // Created by 邱学伟 on 2019/3/11. 6 | // Copyright © 2019 qxuewei@yeah.net. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface XWTestSubModel : NSObject 14 | 15 | @property (nonatomic, assign) NSInteger uuid; 16 | 17 | @property (nonatomic, copy) NSString *name; 18 | 19 | @property (nonatomic, strong) UIImage *icon; 20 | 21 | + (instancetype)testTestSubModel; 22 | @end 23 | 24 | NS_ASSUME_NONNULL_END 25 | -------------------------------------------------------------------------------- /Example/XWDatabase/Model/XWTestSubModel.m: -------------------------------------------------------------------------------- 1 | // 2 | // XWTestSubModel.m 3 | // XWDatabase_Example 4 | // 5 | // Created by 邱学伟 on 2019/3/11. 6 | // Copyright © 2019 qxuewei@yeah.net. All rights reserved. 7 | // 8 | 9 | #import "XWTestSubModel.h" 10 | #import "NSObject+XWModel.h" 11 | 12 | @implementation XWTestSubModel 13 | 14 | /// 快速归解档的宏 15 | XWCodingImplementation 16 | 17 | + (instancetype)testTestSubModel { 18 | XWTestSubModel *model = [XWTestSubModel new]; 19 | model.uuid = 123; 20 | model.name = @"name"; 21 | model.icon = [UIImage imageNamed:@"icon"]; 22 | return model; 23 | } 24 | 25 | @end 26 | -------------------------------------------------------------------------------- /Example/XWDatabase/XWAppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // XWAppDelegate.h 3 | // XWDatabase 4 | // 5 | // Created by qxuewei@yeah.net on 12/06/2018. 6 | // Copyright (c) 2018 qxuewei@yeah.net. All rights reserved. 7 | // 8 | 9 | @import UIKit; 10 | 11 | @interface XWAppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /Example/XWDatabase/XWAppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // XWAppDelegate.m 3 | // XWDatabase 4 | // 5 | // Created by qxuewei@yeah.net on 12/06/2018. 6 | // Copyright (c) 2018 qxuewei@yeah.net. All rights reserved. 7 | // 8 | 9 | #import "XWAppDelegate.h" 10 | 11 | @implementation XWAppDelegate 12 | 13 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 14 | { 15 | NSLog(@"%@",NSHomeDirectory()); 16 | 17 | return YES; 18 | } 19 | 20 | - (void)applicationWillResignActive:(UIApplication *)application 21 | { 22 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 23 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 24 | } 25 | 26 | - (void)applicationDidEnterBackground:(UIApplication *)application 27 | { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | - (void)applicationWillEnterForeground:(UIApplication *)application 33 | { 34 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 35 | } 36 | 37 | - (void)applicationDidBecomeActive:(UIApplication *)application 38 | { 39 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 40 | } 41 | 42 | - (void)applicationWillTerminate:(UIApplication *)application 43 | { 44 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 45 | } 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /Example/XWDatabase/XWDatabase-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleDisplayName 8 | ${PRODUCT_NAME} 9 | CFBundleExecutable 10 | ${EXECUTABLE_NAME} 11 | CFBundleIdentifier 12 | $(PRODUCT_BUNDLE_IDENTIFIER) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1.0 25 | LSRequiresIPhoneOS 26 | 27 | UILaunchStoryboardName 28 | LaunchScreen 29 | UIMainStoryboardFile 30 | Main 31 | UIRequiredDeviceCapabilities 32 | 33 | armv7 34 | 35 | UISupportedInterfaceOrientations 36 | 37 | UIInterfaceOrientationPortrait 38 | UIInterfaceOrientationLandscapeLeft 39 | UIInterfaceOrientationLandscapeRight 40 | 41 | UISupportedInterfaceOrientations~ipad 42 | 43 | UIInterfaceOrientationPortrait 44 | UIInterfaceOrientationPortraitUpsideDown 45 | UIInterfaceOrientationLandscapeLeft 46 | UIInterfaceOrientationLandscapeRight 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /Example/XWDatabase/XWDatabase-Prefix.pch: -------------------------------------------------------------------------------- 1 | // 2 | // Prefix header 3 | // 4 | // The contents of this file are implicitly included at the beginning of every source file. 5 | // 6 | 7 | #import 8 | 9 | #ifndef __IPHONE_5_0 10 | #warning "This project uses features only available in iOS SDK 5.0 and later." 11 | #endif 12 | 13 | #ifdef __OBJC__ 14 | @import UIKit; 15 | @import Foundation; 16 | #endif 17 | -------------------------------------------------------------------------------- /Example/XWDatabase/XWViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // XWViewController.h 3 | // XWDatabase 4 | // 5 | // Created by qxuewei@yeah.net on 12/06/2018. 6 | // Copyright (c) 2018 qxuewei@yeah.net. All rights reserved. 7 | // 8 | 9 | @import UIKit; 10 | 11 | @interface XWViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /Example/XWDatabase/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /Example/XWDatabase/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // XWDatabase 4 | // 5 | // Created by qxuewei@yeah.net on 12/06/2018. 6 | // Copyright (c) 2018 qxuewei@yeah.net. All rights reserved. 7 | // 8 | 9 | @import UIKit; 10 | #import "XWAppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) 13 | { 14 | @autoreleasepool { 15 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([XWAppDelegate class])); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2018 qxuewei@yeah.net 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # XWDatabase 2 | 3 | [![CI Status](https://img.shields.io/travis/qxuewei@yeah.net/XWDatabase.svg?style=flat)](https://travis-ci.org/qxuewei@yeah.net/XWDatabase) 4 | [![Version](https://img.shields.io/cocoapods/v/XWDatabase.svg?style=flat)](https://cocoapods.org/pods/XWDatabase) 5 | [![License](https://img.shields.io/cocoapods/l/XWDatabase.svg?style=flat)](https://cocoapods.org/pods/XWDatabase) 6 | [![Platform](https://img.shields.io/cocoapods/p/XWDatabase.svg?style=flat)](https://cocoapods.org/pods/XWDatabase) 7 | 8 | ## Example 9 | 10 | To run the example project, clone the repo, and run `pod install` from the Example directory first. 11 | 12 | ## Requirements 13 | 14 | ## Installation 15 | 16 | XWDatabase is available through [CocoaPods](https://cocoapods.org). To install 17 | it, simply add the following line to your Podfile: 18 | 19 | ```ruby 20 | pod 'XWDatabase' 21 | ``` 22 | 23 | ## Author 24 | 25 | qxuewei@yeah.net, qiuxuewei@peiwo.cn 26 | 27 | 28 | ## Introduce 29 | 30 | #### XWDatabase 的亮点 31 | 32 | - 将数据库操作简化到难以想象的程度,你甚至不需要知道数据库的存在,当然更不需要写 SQL 语句,你只需要直接操作模型即可对模型进行增删改查的操作,她会根据模型动态在数据库中创建以当前模型类名为名称的数据库表,当然你也可以自定义表名; 33 | - 她会根据模型的成员变量和成员变量的类型动态进行字段的设计,有多少成员变量,表中自然就会有多少字段与其对应,当然,你也可以忽略其中的某些你不想存储的成员变量,也可以自定义字段的名称; 34 | - 如果哪天模型的字段变化了,她会自动进行表中原有字段的更新,而且无论原表中有多少数据,都会一条不落的迁移到新表中; 35 | - 她的API简单到只有一行代码,你无需关注数据库的开启和关闭,一行代码实现增删改查和数据迁移; 36 | - 你甚至可以在任何线程中调用她的API,她一定是线程安全的,不会出现多线程访问同一个数据库和死锁的问题; 37 | - 数据操作是耗时操作,所以你无需手动开启异步线程操作数据库操作,她会统一在一个保活的异步线程中执行; 38 | - 她支持存储常见的数据类型(int、long、signed、float、double、NSInteger、CGFloat、BOOL、NSString、NSMutableString、NSNumber、NSArray、NSMutableArray、NSDictionary、NSMutableDictionary、NSData、NSMutableData、UIImage、NSDate、NSURL、NSRange、CGRect、CGSize、CGPoint、自定义对象等的存储); 39 | - 她还对二进制文件的存储做了优化,比如同一张图片表中所有数据都持有这张图片对象,她在数据库中只会有一份拷贝,竭尽她所能优化存储空间。 40 | - 若保存的模型中含有自定义模型的属性, 注意模型需要实现 'xw_customModelMapping' 协议声明所指向的自定义模型 并且 自定义模型需要遵循 协议, 对其进行归解档!!!! -> 可使用 'XWCodingImplementation' 宏 快速实现 或 自行实现 '- (instancetype)initWithCoder:(NSCoder *)aDecoder' 和 '- (void)encodeWithCoder:(NSCoder *)aCoder' 方法 41 | 42 | 笔锋一转,V1.0 版本会存在很多不足,希望各位前辈和大牛多多指正,多提 `issues` 43 | 44 | 下面简述一下此库的一些设计思路和使用方法 45 | 46 | ### 使用 47 | 48 | #### 一、增 49 | 50 | ##### 1.保存一个模型 (Save One Model) 51 | 52 | ``` 53 | - (void)saveOnePerson 54 | { 55 | XWPerson *person = [XWPerson testPerson:2]; 56 | [XWDatabase saveModel:person completion:^(BOOL isSuccess) { 57 | 58 | }]; 59 | } 60 | ``` 61 | 实例化一个对象, 调用 `saveModel` 方法。 62 | 63 | ##### 2.保存多个模型 (Save Many Models) 64 | 65 | ``` 66 | - (void)saveModels 67 | { 68 | NSMutableArray *persons = [[NSMutableArray alloc] init]; 69 | for (int i = 0; i < 1000; i++) { 70 | [persons addObject:[XWPerson testPerson:i]]; 71 | } 72 | [XWDatabase saveModels:persons completion:^(BOOL isSuccess) { 73 | 74 | }]; 75 | } 76 | ``` 77 | 实例化一堆对象, 调用 `saveModels` 方法。 78 | 79 | #### 二、删 80 | 81 | ##### 1.删除一个模型 (Delete One Model) 82 | 83 | ``` 84 | - (void)deleteModel 85 | { 86 | XWPerson *person = [XWPerson new]; 87 | person.cardID = @"1"; /// 指定想删除的主键(或联合主键) 88 | [XWDatabase deleteModel:person completion:^(BOOL isSuccess) { 89 | 90 | }]; 91 | } 92 | ``` 93 | 实例化一个对象,为主键赋值(得知道删的是哪个,让她猜,臣妾做不到), 调用 `deleteModel` 方法。 94 | 95 | ##### 2.删除此模型存储的所有数据 (Delete All Models) 96 | 97 | ``` 98 | - (void)clearModel 99 | { 100 | [XWDatabase clearModel:XWPerson.class completion:^(BOOL isSuccess) { 101 | 102 | }]; 103 | } 104 | ``` 105 | 调用 `clearModel` 方法,传入想删除的模型类 106 | 107 | ##### 3.选择性删除此模型存储的数据 (Delete Models With Condition) 108 | 109 | ``` 110 | /// 删除 age > 50 的数据 111 | - (void)clearModel 112 | { 113 | [XWDatabase clearModel:XWPerson.class condition:@"age > '50'" completion:^(BOOL isSuccess) { 114 | 115 | }]; 116 | } 117 | ``` 118 | 调用 `clearModel` 方法,传入想删除的模型类和条件 119 | 120 | 121 | #### 三、改 122 | 123 | ##### 1.更新某模型某个成员变量 (选择性更新) (Update some properties in Model) 124 | 125 | ``` 126 | /// 改名 127 | - (void)updateModel 128 | { 129 | XWPerson *person = [XWPerson new]; 130 | person.cardID = @"2"; 131 | person.name = @"新名字"; 132 | 133 | /// 自定义成员变量更新 134 | [XWDatabase updateModel:person updatePropertys:@[@"name"] completion:^(BOOL isSuccess) { 135 | 136 | }]; 137 | 138 | } 139 | ``` 140 | 实例化一个对象,为主键和有变化的成员变量赋值, 调用 `updateModel` 方法,传入想更新的成员变量名称。 141 | 142 | ##### 2.更新某模型所有数据 (全量更新) (Update all properties in Model) 143 | 144 | ``` 145 | /// 根据传入的模型整体更新 146 | - (void)updateModel 147 | { 148 | XWPerson *person = [XWPerson new]; 149 | person.cardID = @"2"; 150 | person.name = @"新名字"; 151 | person.girls = @[@"小妹",@"校花",@"小baby"]; 152 | 153 | /// 整个模型更新 154 | [XWDatabase saveModel:person completion:^(BOOL isSuccess) { 155 | 156 | }]; 157 | 158 | } 159 | ``` 160 | 实例化一个对象, 调用 `updateModel` 方法,传入想更新的模型。 161 | 162 | #### 四、查 163 | 164 | ##### 1.根据主键查询模型 (Search One Model with primary Key) 165 | 166 | ``` 167 | - (void)getOnePerson 168 | { 169 | XWPerson *person = [XWPerson new]; 170 | person.cardID = @"81"; 171 | [XWDatabase getModel:person completion:^(XWPerson * obj) { 172 | 173 | }]; 174 | } 175 | ``` 176 | 实例化一个对象,为主键赋值, 调用 `getModel` 方法。 177 | 178 | ##### 2.查询数据库中所有该模型存储的数据 (Search all Models in the database) 179 | 180 | ``` 181 | - (void)getModels 182 | { 183 | [XWDatabase getModels:XWPerson.class completion:^(NSArray * _Nullable objs) { 184 | 185 | 186 | }]; 187 | } 188 | ``` 189 | 调用 `getModels` 方法,传入模型类 190 | 191 | ##### 3.查询数据库中所有该模型存储的数据 - 按某成员变量排序 (Search all the Models in the database to sort the model) 192 | 193 | ``` 194 | /// 获取数据库中所有该模型存储的数据 - 按 age 字段降序排列 195 | - (void)getModelsSortAge 196 | { 197 | [XWDatabase getModels:XWPerson.class sortColumn:@"age" isOrderDesc:YES completion:^(NSArray * _Nullable objs) { 198 | 199 | }]; 200 | } 201 | ``` 202 | 调用 `getModels` 方法,传入模型类和要排序的字段 203 | 204 | ##### 4.查询数据库中所有该模型存储的数据 - 自定义查询条件 (Search all the Models with condition) 205 | 206 | 207 | ``` 208 | /// 获取数据库中所有该模型存储的数据 - 自定义查找条件 (例如模糊查询 name 含 学伟 的数据) 209 | - (void)getModelsCondition 210 | { 211 | [XWDatabase getModels:XWPerson.class condition:@"name like '%学伟'" completion:^(NSArray * _Nullable objs) { 212 | 213 | }]; 214 | } 215 | ``` 216 | 调用 `getModels` 方法,传入模型类和查询的条件 217 | 218 | ##### 5.查询数据库中所有该模型存储的数据 - 自定义查询条件并且可按照某字段排序 (Search all the Models in the database to sort the model) 219 | 220 | ``` 221 | /// 获取数据库中所有该模型存储的数据 - 自定义查找条件可排序 (例如模糊查询 name 含 学伟 的数据, 并且按 age 升序排序) 222 | - (void)getModelsConditionSort 223 | { 224 | [XWDatabase getModels:XWPerson.class sortColumn:@"age" isOrderDesc:NO condition:@"name like '%学伟'" completion:^(NSArray * _Nullable objs) { 225 | 226 | }]; 227 | } 228 | ``` 229 | 调用 `getModels` 方法,传入模型类和查询的条件和排序的成员变量名称 230 | 231 | #### 五、数据迁移 (Data Migration) 232 | 233 | ##### 模型中成员变量发生变化,动态进行数据迁移 234 | 235 | ``` 236 | + (void)initialize 237 | + { 238 | [XWDatabase updateTable:self completion:^(BOOL isSuccess) { 239 | 240 | }]; 241 | ``` 242 | 在模型对象的 `initialize` 方法中 调用 `updateTable` 方法。之所以在 `initialize` 方法中调用是保证用户无感知的情况下在操作此模型进行数据操作时自动更新。 243 | 244 | 245 | 以上就是 `XWDatabase` V1.0 版本的所有功能示例。谢谢! 246 | 247 | 下面介绍一些使用规范和功能扩展。 248 | 249 | #### 六 、`XWDatabaseModelProtocol` 协议 250 | 251 | ``` 252 | /** 253 | 主键 不可更改/唯一性 254 | 255 | @return 主键的属性名 256 | */ 257 | + (NSString *)xw_primaryKey; 258 | 259 | /** 260 | 联合主键成员变量数组 (多个属性共同定义主键) - 优先级大于 'xw_primaryKey' 261 | 262 | @return 联合主键成员变量数组 263 | */ 264 | + (NSArray < NSString * > *)xw_unionPrimaryKey; 265 | 266 | /** 267 | 自定义对象映射 (key: 成员变量名称 value: 对象类) 268 | 269 | @return 自定义对象映射 270 | */ 271 | + (NSDictionary *)xw_customModelMapping; 272 | 273 | /** 274 | 忽略不保存数据库的属性 275 | 276 | @return 忽略的属性名数组 277 | */ 278 | + (NSSet *)xw_ignoreColumnNames; 279 | 280 | /** 281 | 自定义字段名映射表 (默认成员变量即变量名, 可自定义字段名 key: 成员变量(属性)名称 value: 自定义数据库表字段名) 282 | 283 | @return 自定义字段名映射表 284 | */ 285 | + (NSDictionary *)xw_customColumnMapping; 286 | 287 | /** 288 | 自定义表名 (默认属性类名) 289 | 290 | @return 自定义表名 291 | */ 292 | + (NSString *)xw_customTableName; 293 | 294 | ``` 295 | 296 | 当模型遵守 `XWDatabaseModelProtocol` 协议并选择性实现其中某些方法时她便会更好的为您服务。当然 主键 `xw_primaryKey`(或联合主键 `xw_unionPrimaryKey` )是查询和更新必须要实现的方法。 297 | 298 | 如果模型中成员变量存在其他的自定义模型,那其他的自定义模型需要遵从 `NSCoding` 协议并实现 `initWithCoder` 和 `encodeWithCoder` 方法。 [XWDatabase](https://github.com/qxuewei/XWDatabase) 中的 `NSObject+XWModel` 提供了一个宏可以快速使自定义对象具备归解档的功能 `XWCodingImplementation` 299 | 300 | ### 设计思路 301 | 1. 根据 `runtime` 获取对象成员变量名称和类型生成 建表 `SQL` 语句 302 | 2. 根据当前对象成员变量名称和原有数据库表中字段排序后进行比较,有差异进行数据迁移 303 | 3. 根据模型动态生成 `SQL` 语句 304 | 4. 利用事务进行大数据量的操作 305 | 5. 创建一个保活的子线程(使异步线程的 `Runloop` 保持活跃)进行数据库操作,使用主线程队列保证数据操作的同步 306 | 6. 数据库底层封装自 [FMDB](https://github.com/ccgus/fmdb/) 307 | 7. 创建一个单独存放图片二进制的库存储二进制文件。真实表中存储 二进制 文件的 `hash` 值已达到数据重用。 308 | 309 | 310 | 此库支持 `CocoaPod` 集成: 311 | 312 | ``` 313 | pod 'XWDatabase' 314 | ``` 315 | 316 | 317 | 作者:极客学伟 318 | 博客: 319 | 320 | 321 | ## License 322 | 323 | XWDatabase is available under the MIT license. See the LICENSE file for more info. 324 | 325 | 326 | -------------------------------------------------------------------------------- /XWDatabase.podspec: -------------------------------------------------------------------------------- 1 | # 2 | # Be sure to run `pod lib lint XWDatabase.podspec' to ensure this is a 3 | # valid spec before submitting. 4 | # 5 | # Any lines starting with a # are optional, but their use is encouraged 6 | # To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html 7 | # 8 | 9 | Pod::Spec.new do |s| 10 | s.name = 'XWDatabase' 11 | s.version = '1.2.2' 12 | s.summary = '数据库工具类,直接操作模型读写数据库,FMDB封装' 13 | 14 | # This description is used to generate tags and improve search results. 15 | # * Think: What does it do? Why did you write it? What is the focus? 16 | # * Try to keep it short, snappy and to the point. 17 | # * Write the description between the DESC delimiters below. 18 | # * Finally, don't worry about the indent, CocoaPods strips it! 19 | 20 | s.description = <<-DESC 21 | 数据库工具类,直接操作模型读写数据库,FMDB封装, 二进制文件重用, 支持自定义主键/联合主键, 支持SQL拓展操作, 支持自定义对象存储... 22 | DESC 23 | 24 | s.homepage = 'https://github.com/qxuewei/XWDatabase' 25 | # s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2' 26 | s.license = { :type => 'MIT', :file => 'LICENSE' } 27 | s.author = { 'qxuewei@yeah.net' => 'qiuxuewei@peiwo.cn' } 28 | s.source = { :git => 'https://github.com/qxuewei/XWDatabase.git', :tag => s.version.to_s } 29 | # s.social_media_url = 'https://twitter.com/' 30 | 31 | s.ios.deployment_target = '8.0' 32 | 33 | s.source_files = 'XWDatabase/Classes/**/*' 34 | 35 | # s.resource_bundles = { 36 | # 'XWDatabase' => ['XWDatabase/Assets/*.png'] 37 | # } 38 | 39 | # s.public_header_files = 'Pod/Classes/**/*.h' 40 | # s.frameworks = 'UIKit', 'MapKit' 41 | s.dependency 'FMDB' 42 | end 43 | -------------------------------------------------------------------------------- /XWDatabase/Assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/XWDatabase/Assets/.gitkeep -------------------------------------------------------------------------------- /XWDatabase/Classes/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxuewei/XWDatabase/85b067c917c98235a55dd3bfbf4db920a0556c7f/XWDatabase/Classes/.gitkeep -------------------------------------------------------------------------------- /XWDatabase/Classes/NSObject+XWModel.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+XWModel.h 3 | // XWDatabase_Example 4 | // 5 | // Created by 邱学伟 on 2018/12/11. 6 | // Copyright © 2018 qxuewei@yeah.net. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "XWDatabaseModelProtocol.h" 11 | 12 | #pragma mark - 智能归解档 13 | #define XWCodingImplementation \ 14 | - (instancetype)initWithCoder:(NSCoder *)aDecoder{\ 15 | if(self == [super init]){\ 16 | [self xw_decode:aDecoder];\ 17 | }\ 18 | return self;\ 19 | }\ 20 | \ 21 | - (void)encodeWithCoder:(NSCoder *)aCoder{\ 22 | [self xw_encode:aCoder];\ 23 | }\ 24 | + (BOOL)supportsSecureCoding {\ 25 | return YES;\ 26 | } 27 | 28 | NS_ASSUME_NONNULL_BEGIN 29 | 30 | #pragma mark 常量 31 | #define kXWDB_IDENTIFIER_COLUMNNAME @"xw_identifier" //唯一标识字段名称 32 | #define kXWDB_IDENTIFIER_VALUE @"xw_null" //唯一标识字段值 33 | #define kXWDB_PRIMARYKEY_COLUMNNAME @"xw_id" //默认自增主键字段名称 34 | 35 | @interface NSObject (XWModel) 36 | 37 | /** 38 | 是否具备数据可 更新/查询 条件 39 | 40 | @return 是否具备数据可 更新/查询 条件 41 | */ 42 | - (BOOL)xwdb_isUpdateQueryingCondition; 43 | 44 | /** 45 | 默认主键 (自增) 46 | */ 47 | //@property (nonatomic, strong) NSNumber *xw_id; 48 | 49 | /** 50 | 上次存储主键值 51 | */ 52 | //@property (nonatomic, strong) NSNumber *xw_lastPrimaryKeyId; 53 | 54 | /** 55 | 模型中所有成员变量 (key: 成员变量名称 value: 成员变量类型) 56 | */ 57 | @property (nonatomic, strong) NSDictionary *xw_classIvarNameTypeDict; 58 | 59 | /** 60 | 模型中成员变量集合 61 | */ 62 | @property (nonatomic, strong) NSSet *xw_IvarSet; 63 | 64 | /** 65 | 模型中自定义对象 66 | */ 67 | @property (nonatomic, strong) NSSet *xw_CustomModelSet; 68 | 69 | 70 | #pragma mark - 智能归解档 71 | /// 归档 72 | - (void)xw_decode:(NSCoder*)aDecoder; 73 | 74 | /// 解档 75 | - (void)xw_encode:(NSCoder*)aCoder; 76 | 77 | #pragma mark - 以下方法根据 XWDatabaseModelProtocol 协议中实现获取相应值 78 | 79 | /** 80 | 主键 不可更改/唯一性 81 | 82 | @return 主键的属性名 83 | */ 84 | - (NSString * _Nullable)xwdb_primaryKey; 85 | 86 | /** 87 | 联合主键成员变量数组 (多个属性共同定义主键) - 优先级大于 'xwdb_primaryKey' 88 | 89 | @return 联合主键成员变量数组 90 | */ 91 | - (NSArray < NSString * > * _Nullable)xwdb_unionPrimaryKey; 92 | 93 | /** 94 | 忽略不保存数据库的属性 95 | 96 | @return 忽略的属性名数组 97 | */ 98 | - (NSSet * _Nullable)xwdb_ignoreColumnNames; 99 | 100 | /** 101 | 自定义字段名映射表 (默认成员变量即变量名, 可自定义字段名 key: 成员变量(属性)名称 value: 自定义数据库表字段名) 102 | 103 | @return 自定义字段名映射表 104 | */ 105 | - (NSDictionary * _Nullable)xwdb_customColumnMapping; 106 | 107 | /** 108 | 自定义表名 (默认属性类名) 109 | 110 | @return 自定义表名 111 | */ 112 | - (NSString * _Nullable)xwdb_customTableName; 113 | 114 | /** 115 | 自定义存储的属性数组, 实现此协议在存储时将自动忽略其他属性( 'xw_ignoreColumnNames' 将无效), 116 | 117 | @return 自定义存储的属性数组 118 | */ 119 | - (NSSet < NSString * > * _Nullable)xwdb_specificSaveColumnNames; 120 | 121 | @end 122 | 123 | NS_ASSUME_NONNULL_END 124 | -------------------------------------------------------------------------------- /XWDatabase/Classes/NSObject+XWModel.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+XWModel.m 3 | // XWDatabase_Example 4 | // 5 | // Created by 邱学伟 on 2018/12/11. 6 | // Copyright © 2018 qxuewei@yeah.net. All rights reserved. 7 | // 8 | 9 | #import "NSObject+XWModel.h" 10 | #import 11 | #import "XWDatabaseModel.h" 12 | 13 | @interface NSObject () 14 | @end 15 | 16 | @implementation NSObject (XWModel) 17 | 18 | #pragma mark - Public 19 | /** 20 | 是否具备数据可 更新/查询 条件 21 | 22 | @return 是否具备数据可 更新/查询 条件 23 | */ 24 | - (BOOL)xwdb_isUpdateQueryingCondition { 25 | if (!self.xwdb_primaryKey && !self.xwdb_unionPrimaryKey) { 26 | return NO; 27 | } 28 | return YES; 29 | } 30 | 31 | #pragma mark - 分类新增属性 32 | /// 默认自增主键 33 | - (void)setXw_id:(NSNumber *)xw_id { 34 | objc_setAssociatedObject(self, @selector(xw_id), xw_id, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 35 | } 36 | - (NSNumber *)xw_id { 37 | return objc_getAssociatedObject(self, _cmd); 38 | } 39 | 40 | /// 上次存储的ID 41 | - (void)setXw_lastPrimaryKeyId:(NSNumber *)xw_lastPrimaryKeyId { 42 | objc_setAssociatedObject(self, @selector(xw_lastPrimaryKeyId), xw_lastPrimaryKeyId, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 43 | } 44 | - (NSNumber *)xw_lastPrimaryKeyId { 45 | return objc_getAssociatedObject(self, _cmd); 46 | } 47 | 48 | /// 模型中所有成员变量 (key: 成员变量名称 value: 成员变量类型) 49 | - (void)setXw_classIvarNameTypeDict:(NSDictionary *)xw_classIvarNameTypeDict { 50 | objc_setAssociatedObject(self, @selector(xw_classIvarNameTypeDict), xw_classIvarNameTypeDict, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 51 | } 52 | - (NSDictionary *)xw_classIvarNameTypeDict { 53 | return objc_getAssociatedObject(self, _cmd); 54 | } 55 | 56 | /// 模型中真实成员变量类型 57 | - (void)setXw_IvarSet:(NSSet *)xw_IvarSet { 58 | objc_setAssociatedObject(self, @selector(xw_IvarSet), xw_IvarSet, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 59 | } 60 | - (NSSet *)xw_IvarSet { 61 | return objc_getAssociatedObject(self, _cmd); 62 | } 63 | 64 | /// 自定义对象 65 | - (void)setXw_CustomModelSet:(NSSet *)xw_CustomModelSet { 66 | objc_setAssociatedObject(self, @selector(xw_CustomModelSet), xw_CustomModelSet, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 67 | } 68 | - (NSSet *)xw_CustomModelSet { 69 | return objc_getAssociatedObject(self, _cmd); 70 | } 71 | 72 | #pragma mark - 智能归解档 73 | - (void)xw_decode:(NSCoder*)aDecoder { 74 | Class cls = [self class]; 75 | while (cls && cls != [NSObject class]) { 76 | unsigned int count = 0; 77 | Ivar *ivars = class_copyIvarList(cls, &count); 78 | for (int i = 0; i < count; i++) { 79 | Ivar ivar = ivars[i]; 80 | NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)]; 81 | id value = [aDecoder decodeObjectForKey:key]; 82 | if (value) { 83 | [self setValue:value forKey:key]; 84 | } 85 | } 86 | free(ivars); 87 | cls = [cls superclass]; 88 | } 89 | } 90 | 91 | - (void)xw_encode:(NSCoder*)aCoder { 92 | Class cls = [self class]; 93 | while (cls && cls != [NSObject class]) { 94 | unsigned int count = 0; 95 | Ivar *ivars = class_copyIvarList(cls, &count); 96 | for (int i = 0; i < count; i++) { 97 | Ivar ivar = ivars[i]; 98 | NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)]; 99 | id value = [self valueForKey:key]; 100 | if (value) { 101 | [aCoder encodeObject:value forKey:key]; 102 | } 103 | } 104 | free(ivars); 105 | cls = [cls superclass]; 106 | } 107 | } 108 | 109 | #pragma mark - 以下方法根据 XWDatabaseModelProtocol 协议中实现获取相应值 110 | 111 | /** 112 | 主键 不可更改/唯一性 113 | 114 | @return 主键的属性名 115 | */ 116 | - (NSString * _Nullable)xwdb_primaryKey { 117 | if ([self.class respondsToSelector:@selector(xw_primaryKey)]) { 118 | NSString *primaryKey = [self.class xw_primaryKey]; 119 | if (primaryKey && primaryKey.length > 0) { 120 | NSDictionary *columns = [XWDatabaseModel classColumnIvarNameTypeDict:self.class]; 121 | if (![columns.allKeys containsObject:primaryKey]) { 122 | /// 表字段中无该主键 123 | return nil; 124 | } 125 | return primaryKey; 126 | } 127 | } 128 | return nil; 129 | } 130 | 131 | /** 132 | 联合主键成员变量数组 (多个属性共同定义主键) - 优先级大于 'xwdb_primaryKey' 133 | 134 | @return 联合主键成员变量数组 135 | */ 136 | - (NSArray < NSString * > * _Nullable)xwdb_unionPrimaryKey { 137 | if ([self.class respondsToSelector:@selector(xw_unionPrimaryKey)]) { 138 | NSArray *unionPrimaryKey = [self.class xw_unionPrimaryKey]; 139 | if (unionPrimaryKey && unionPrimaryKey.count > 0) { 140 | __block BOOL isError = NO; 141 | [unionPrimaryKey enumerateObjectsUsingBlock:^(NSString * obj, NSUInteger idx, BOOL * _Nonnull stop) { 142 | NSDictionary *columns = [XWDatabaseModel classColumnIvarNameTypeDict:self.class]; 143 | /// 表字段中无该主键 144 | isError = ![columns.allKeys containsObject:obj]; 145 | if (isError) { 146 | *stop = YES; 147 | } 148 | }]; 149 | if (isError) { 150 | return nil; 151 | } 152 | return unionPrimaryKey; 153 | } 154 | } 155 | return nil; 156 | } 157 | 158 | /** 159 | 忽略不保存数据库的属性 160 | 161 | @return 忽略的属性名数组 162 | */ 163 | - (NSSet * _Nullable)xwdb_ignoreColumnNames { 164 | if ([self.class respondsToSelector:@selector(xw_ignoreColumnNames)]) { 165 | NSSet *ignoreColumnNames = [self.class xw_ignoreColumnNames]; 166 | if (ignoreColumnNames && ignoreColumnNames.count) { 167 | return ignoreColumnNames; 168 | } 169 | } 170 | return nil; 171 | } 172 | 173 | /** 174 | 自定义字段名映射表 (默认成员变量即变量名, 可自定义字段名 key: 成员变量(属性)名称 value: 自定义数据库表字段名) 175 | 176 | @return 自定义字段名映射表 177 | */ 178 | - (NSDictionary * _Nullable)xwdb_customColumnMapping { 179 | if ([self.class respondsToSelector:@selector(xw_customColumnMapping)]) { 180 | NSMutableDictionary *customColumnMapping = [self.class xw_customColumnMapping].mutableCopy; 181 | if (customColumnMapping && customColumnMapping.count > 0) { 182 | [customColumnMapping enumerateKeysAndObjectsUsingBlock:^(NSString * key, NSString * obj, BOOL * _Nonnull stop) { 183 | obj = [key stringByReplacingOccurrencesOfString:@" " withString:@""]; 184 | }]; 185 | return customColumnMapping.copy; 186 | } 187 | } 188 | return nil; 189 | } 190 | 191 | /** 192 | 自定义表名 (默认属性类名) 193 | 194 | @return 自定义表名 195 | */ 196 | - (NSString * _Nullable)xwdb_customTableName { 197 | if ([self.class respondsToSelector:@selector(xw_customTableName)]) { 198 | NSString *customTableName = [self.class xw_customTableName]; 199 | if (customTableName && customTableName.length > 0) { 200 | return customTableName; 201 | } 202 | } 203 | return nil; 204 | } 205 | 206 | /** 207 | 自定义存储的属性数组, 实现此协议在存储时将自动忽略其他属性( 'xw_ignoreColumnNames' 将无效), 208 | 209 | @return 自定义存储的属性数组 210 | */ 211 | - (NSSet < NSString * > * _Nullable)xwdb_specificSaveColumnNames { 212 | if ([self.class respondsToSelector:@selector(xw_specificSaveColumnNames)]) { 213 | NSMutableSet *specificSaveColumnNames = [self.class xw_specificSaveColumnNames].mutableCopy; 214 | if (specificSaveColumnNames && specificSaveColumnNames.count) { 215 | return specificSaveColumnNames.copy; 216 | } 217 | } 218 | return nil; 219 | } 220 | 221 | @end 222 | -------------------------------------------------------------------------------- /XWDatabase/Classes/XWDatabase.h: -------------------------------------------------------------------------------- 1 | // 2 | // XWDatabase.h 3 | // XWDatabase 4 | // 5 | // Created by 邱学伟 on 2018/11/29. 6 | // Copyright © 2018 邱学伟. All rights reserved. 7 | // 8 | // 若保存的模型中含有自定义模型的属性, 注意模型需要实现 'xw_customModelMapping' 协议声明所指向的自定义模型 并且 自定义模型需要遵循 协议, 对其进行归解档!!!! -> 可使用 'XWCodingImplementation' 宏 快速实现或自行实现 '- (instancetype)initWithCoder:(NSCoder *)aDecoder' 和 '- (void)encodeWithCoder:(NSCoder *)aCoder' 方法 9 | 10 | #import 11 | #import "XWDatabaseModelProtocol.h" 12 | #import "NSObject+XWModel.h" 13 | 14 | @class FMResultSet; 15 | 16 | NS_ASSUME_NONNULL_BEGIN 17 | 18 | typedef void(^XWDatabaseCompletion)(BOOL isSuccess); /// 操作回调 19 | typedef void(^XWDatabaseReturnObject)(id _Nullable obj); /// 返回对象 20 | typedef void(^XWDatabaseReturnObjects)(NSArray * _Nullable objs); /// 返回对象数组 21 | typedef void(^XWDatabaseReturnResultSet)(FMResultSet * _Nullable resultSet);/// 返回 FMResultSet 原生查询结果-需自己解析 22 | 23 | @interface XWDatabase : NSObject 24 | 25 | #pragma mark - 增 26 | /** 27 | 保存模型 (表中不存在插入, 已存在更新) 28 | 29 | @param obj 模型 30 | @param completion 保存 成功/失败 31 | */ 32 | + (void)saveModel:(NSObject *)obj completion:(XWDatabaseCompletion _Nullable)completion; 33 | 34 | /** 35 | 保存模型 (表中不存在插入, 已存在更新) - 标示符区分 36 | 37 | @param obj 模型 38 | @param identifier 唯一标识,用于区分不同数据组 (如: userID), 若模型无主键或联合主键则会统一插入数据 39 | @param completion 保存 成功/失败 40 | */ 41 | + (void)saveModel:(NSObject *)obj identifier:(NSString * _Nullable)identifier completion:(XWDatabaseCompletion _Nullable)completion; 42 | 43 | /** 44 | 保存模型数组 45 | 46 | @param objs 模型数组 47 | @param completion 保存 成功/失败 48 | */ 49 | + (void)saveModels:(NSArray *)objs completion:(XWDatabaseCompletion _Nullable)completion; 50 | 51 | /** 52 | 保存模型数组 - 标示符区分 53 | 54 | @param objs 模型数组 55 | @param identifier 唯一标识,用于区分不同数据组 (如: userID) 56 | @param completion 保存 成功/失败 57 | */ 58 | + (void)saveModels:(NSArray *)objs identifier:(NSString * _Nullable)identifier completion:(XWDatabaseCompletion _Nullable)completion; 59 | 60 | 61 | #pragma mark - 删 62 | /** 63 | 删除指定某个模型, 主键不能为空 64 | 65 | @param obj 模型 (主键不能为空) 66 | @param completion 成功/失败 67 | */ 68 | + (void)deleteModel:(NSObject *)obj completion:(XWDatabaseCompletion _Nullable)completion; 69 | 70 | /** 71 | 删除指定某个模型, 主键不能为空 - 标示符区分 72 | 73 | @param obj 模型 (主键不能为空) 74 | @param identifier 唯一标识,用于区分不同数据组 (如: userID) 75 | @param completion 成功/失败 76 | */ 77 | + (void)deleteModel:(NSObject *)obj identifier:(NSString * _Nullable)identifier completion:(XWDatabaseCompletion _Nullable)completion; 78 | 79 | /** 80 | 删除指定模型所有数据 81 | 82 | @param cls 模型类 83 | @param completion 成功/失败 84 | */ 85 | + (void)clearModel:(Class)cls completion:(XWDatabaseCompletion _Nullable)completion; 86 | 87 | /** 88 | 删除指定模型所有数据 - 标示符区分 89 | 90 | @param cls 模型类 91 | @param identifier 唯一标识,用于区分不同数据组 (如: userID) 92 | @param completion 成功/失败 93 | */ 94 | + (void)clearModel:(Class)cls identifier:(NSString * _Nullable)identifier completion:(XWDatabaseCompletion _Nullable)completion; 95 | 96 | /** 97 | 删除指定模型所有数据 - 自定义条件 98 | 99 | @param cls 模型类 100 | @param condition 自定义条件 (为空删除所有数据,有值根据自定义的条件删除 eg: 条件 (age > 60) ) 101 | @param completion 成功/失败 102 | */ 103 | + (void)clearModel:(Class)cls condition:(NSString * _Nullable)condition completion:(XWDatabaseCompletion _Nullable)completion; 104 | 105 | /** 106 | 删除指定模型所有数据 - 自定义条件 - 标示符区分 107 | 108 | @param cls 模型类 109 | @param identifier 唯一标识,用于区分不同数据组 (如: userID) 110 | @param condition 自定义条件 (为空删除所有数据,有值根据自定义的条件删除) 111 | @param completion 成功/失败 112 | */ 113 | + (void)clearModel:(Class)cls identifier:(NSString * _Nullable)identifier condition:(NSString * _Nullable)condition completion:(XWDatabaseCompletion _Nullable)completion; 114 | 115 | #pragma mark - 改 116 | /** 117 | 更新现有表结构 (增删字段,数据迁移) 118 | 119 | @param cls 模型类 120 | @param completion 是否更新成功 121 | */ 122 | + (void)updateTable:(Class)cls completion:(XWDatabaseCompletion _Nullable)completion; 123 | 124 | /** 125 | 更新模型 126 | 127 | @param obj 模型 (主键(或联合主键)不能为空) 128 | @param updatePropertys 所更新的字段数组 (若为 nil -> 全量更新) 129 | @param completion 保存 成功/失败 130 | */ 131 | + (void)updateModel:(NSObject *)obj updatePropertys:(NSArray * _Nullable)updatePropertys completion:(XWDatabaseCompletion _Nullable)completion; 132 | 133 | /** 134 | 更新模型 - 标示符区分 135 | 136 | @param obj 模型 (主键(或联合主键)不能为空) 137 | @param identifier 唯一标识,用于区分不同数据组 (如: userID) 138 | @param updatePropertys 所更新的字段数组 (无自定义全量更新) 139 | @param completion 保存 成功/失败 140 | */ 141 | + (void)updateModel:(NSObject *)obj identifier:(NSString * _Nullable)identifier updatePropertys:(NSArray * _Nullable)updatePropertys completion:(XWDatabaseCompletion _Nullable)completion; 142 | 143 | /** 144 | 更新模型数组 145 | 146 | @param objs 模型数组 (主键(或联合主键)不能为空) 147 | @param updatePropertys 所更新的字段数组 (若为 nil -> 全量更新) 148 | @param completion 保存 成功/失败 149 | */ 150 | + (void)updateModels:(NSArray < NSObject * > *)objs updatePropertys:(NSArray * _Nullable)updatePropertys completion:(XWDatabaseCompletion _Nullable)completion; 151 | 152 | /** 153 | 更新模型数组 - 标示符区分 154 | 155 | @param objs 模型数组 (主键(或联合主键)不能为空) 156 | @param identifier 唯一标识,用于区分不同数据组 (如: userID) 157 | @param updatePropertys 所更新的字段数组 (若为 nil -> 全量更新) 158 | @param completion 保存 成功/失败 159 | */ 160 | + (void)updateModels:(NSArray < NSObject * > *)objs identifier:(NSString * _Nullable)identifier updatePropertys:(NSArray * _Nullable)updatePropertys completion:(XWDatabaseCompletion _Nullable)completion; 161 | 162 | /** 163 | 更新模型数组,自定义更新条件 164 | 165 | @param obj 模型 166 | @param updatePropertys 所更新的字段数组 (若为 nil -> 全量更新) 167 | @param condition 条件 (自定义条件不能为空!) 168 | @param completion 保存 成功/失败 169 | */ 170 | + (void)updateModels:(NSObject *)obj updatePropertys:(NSArray * _Nullable)updatePropertys condition:(NSString * _Nullable)condition completion:(XWDatabaseCompletion _Nullable)completion; 171 | 172 | /** 173 | 更新模型数组,自定义更新条件 - 标示符区分 174 | 175 | @param obj 模型 176 | @param identifier 唯一标识,用于区分不同数据组 (如: userID) 177 | @param updatePropertys 所更新的字段数组 (若为 nil -> 全量更新) 178 | @param condition 条件 (自定义条件不能为空!) 179 | @param completion 保存 成功/失败 180 | */ 181 | + (void)updateModels:(NSObject *)obj identifier:(NSString * _Nullable)identifier updatePropertys:(NSArray * _Nullable)updatePropertys condition:(NSString * _Nullable)condition completion:(XWDatabaseCompletion _Nullable)completion; 182 | 183 | #pragma mark - 查 184 | /** 185 | 查询模型 186 | 187 | @param obj 查询对象(必须保证主键 不为空/或唯一标识属性 不为空) 188 | @param completion 结果 189 | */ 190 | + (void)getModel:(NSObject *)obj completion:(XWDatabaseReturnObject _Nullable)completion; 191 | 192 | /** 193 | 查询模型 - 标示符区分 (主键不可为空) 194 | 195 | @param obj 查询对象(必须保证主键 不为空) 196 | @param identifier 唯一标识,用于区分不同数据组 (如: userID), 若模型无 主键或联合主键 不会获取到任何结果 可使用 ' 197 | @selector(getModels:identifier:completion:)' 获取模型数组 198 | @param completion 结果 199 | */ 200 | + (void)getModel:(NSObject *)obj identifier:(NSString * _Nullable)identifier completion:(XWDatabaseReturnObject _Nullable)completion; 201 | 202 | /** 203 | 查询模型数组 204 | 205 | @param cls 模型类 206 | @param completion 结果 207 | */ 208 | + (void)getModels:(Class)cls completion:(XWDatabaseReturnObjects _Nullable)completion; 209 | 210 | /** 211 | 查询模型数组 - 标示符区分 212 | 213 | @param cls 模型类 214 | @param identifier 唯一标识,用于区分不同数据组 (如: userID) 215 | @param completion 结果 216 | */ 217 | + (void)getModels:(Class)cls identifier:(NSString * _Nullable)identifier completion:(XWDatabaseReturnObjects _Nullable)completion; 218 | 219 | /** 220 | 查询模型数组 - 自定义条件 221 | 222 | @param cls 模型类 223 | @param condition 条件 (name like '%学伟') 224 | @param completion 结果 225 | */ 226 | + (void)getModels:(Class)cls condition:(NSString * _Nullable)condition completion:(XWDatabaseReturnObjects _Nullable)completion; 227 | 228 | /** 229 | 查询模型数组 - 自定义条件 - 标示符区分 230 | 231 | @param cls 模型类 232 | @param identifier 唯一标识,用于区分不同数据组 (如: userID) 233 | @param condition 条件 234 | @param completion 结果 235 | */ 236 | + (void)getModels:(Class)cls identifier:(NSString * _Nullable)identifier condition:(NSString * _Nullable)condition completion:(XWDatabaseReturnObjects _Nullable)completion; 237 | 238 | 239 | /** 240 | 查询模型数组 - 按某字段排序 241 | 242 | @param cls 模型类 243 | @param sortColumn 排序字段 244 | @param isOrderDesc 是否降序 (YES: 降序 NO: 升序) 245 | @param completion 结果 246 | */ 247 | + (void)getModels:(Class)cls sortColumn:(NSString * _Nullable)sortColumn isOrderDesc:(BOOL)isOrderDesc completion:(XWDatabaseReturnObjects _Nullable)completion; 248 | 249 | /** 250 | 查询模型数组 - 按某字段排序 - 标示符区分 251 | 252 | @param cls 模型类 253 | @param identifier 唯一标识,用于区分不同数据组 (如: userID) 254 | @param sortColumn 排序字段 255 | @param isOrderDesc 是否降序 (YES: 降序 NO: 升序) 256 | @param completion 结果 257 | */ 258 | + (void)getModels:(Class)cls identifier:(NSString * _Nullable)identifier sortColumn:(NSString * _Nullable)sortColumn isOrderDesc:(BOOL)isOrderDesc completion:(XWDatabaseReturnObjects _Nullable)completion; 259 | 260 | /** 261 | 查询模型数组 - 自定义条件 + 按某字段排序 262 | 263 | @param cls 模型类 264 | @param sortColumn 排序字段 265 | @param isOrderDesc 是否降序 (YES: 降序 NO: 升序) 266 | @param condition 条件 267 | @param completion 结果 268 | */ 269 | + (void)getModels:(Class)cls sortColumn:(NSString * _Nullable)sortColumn isOrderDesc:(BOOL)isOrderDesc condition:(NSString * _Nullable)condition completion:(XWDatabaseReturnObjects _Nullable)completion; 270 | 271 | /** 272 | 查询模型数组 - 自定义条件 + 按某字段排序 - 标示符区分 273 | 274 | @param cls 模型类 275 | @param identifier 唯一标识,用于区分不同数据组 (如: userID) 276 | @param sortColumn 排序字段 277 | @param isOrderDesc 是否降序 278 | @param condition 条件 279 | @param completion 结果 280 | */ 281 | + (void)getModels:(Class)cls identifier:(NSString * _Nullable)identifier sortColumn:(NSString * _Nullable)sortColumn isOrderDesc:(BOOL)isOrderDesc condition:(NSString * _Nullable)condition completion:(XWDatabaseReturnObjects _Nullable)completion; 282 | 283 | /** 284 | 查询模型数组 - 自定义条件 + 按某字段排序 + 限制个数 - 标示符区分 285 | 286 | @param cls 模型类 287 | @param identifier 唯一标识,用于区分不同数据组 (如: userID) 288 | @param sortColumn 排序字段 289 | @param isOrderDesc 是否降序 290 | @param condition 条件 291 | @param limitCount 个数 292 | @param completion 结果 293 | */ 294 | + (void)getModels:(Class)cls identifier:(NSString * _Nullable)identifier sortColumn:(NSString * _Nullable)sortColumn isOrderDesc:(BOOL)isOrderDesc condition:(NSString * _Nullable)condition limitCount:(NSUInteger)limitCount completion:(XWDatabaseReturnObjects _Nullable)completion; 295 | 296 | #pragma mark - 执行自定义SQL语句 297 | 298 | /** 299 | 执行单条自定义 SQL 更新语句 300 | 301 | @param sql 自定义 SQL 更新语句 302 | @param completion 完成回调 303 | */ 304 | + (void)executeUpdateSql:(NSString *)sql completion:(XWDatabaseCompletion _Nullable)completion; 305 | 306 | /** 307 | 执行多条自定义 SQL 更新语句 308 | 309 | @param sqls 多条自定义 SQL 更新语句 310 | @param completion 完成回调 311 | */ 312 | + (void)executeUpdateSqls:(NSArray *)sqls completion:(XWDatabaseCompletion _Nullable)completion; 313 | 314 | /** 315 | 执行单条自定义 SQL 查询语句 316 | 317 | @param sql 自定义 SQL 查询语句 318 | @param completion 完成回调 319 | */ 320 | + (void)executeQuerySql:(NSString *)sql completion:(XWDatabaseReturnResultSet _Nullable)completion; 321 | 322 | @end 323 | 324 | NS_ASSUME_NONNULL_END 325 | -------------------------------------------------------------------------------- /XWDatabase/Classes/XWDatabaseDataModel.h: -------------------------------------------------------------------------------- 1 | // 2 | // XWDatabaseDataModel.h 3 | // XWDatabase_Example 4 | // 5 | // Created by 邱学伟 on 2018/12/12. 6 | // Copyright © 2018 qxuewei@yeah.net. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface XWDatabaseDataModel : NSObject 14 | 15 | /** 16 | 存储二进制到二进制数据库中 17 | 18 | @param data 二进制文件 19 | @param completion 回调 20 | */ 21 | + (void)saveData:(NSData *)data completion:(void(^)(BOOL, NSUInteger))completion; 22 | 23 | /** 24 | 获取二进制文件 25 | 26 | @param hashString hash 值 27 | @param completion 回调 28 | */ 29 | + (void)dataWithHash:(NSString *)hashString completion:(void(^)(NSData *))completion; 30 | 31 | @end 32 | 33 | NS_ASSUME_NONNULL_END 34 | -------------------------------------------------------------------------------- /XWDatabase/Classes/XWDatabaseDataModel.m: -------------------------------------------------------------------------------- 1 | // 2 | // XWDatabaseDataModel.m 3 | // XWDatabase_Example 4 | // 5 | // Created by 邱学伟 on 2018/12/12. 6 | // Copyright © 2018 qxuewei@yeah.net. All rights reserved. 7 | // 8 | 9 | #import "XWDatabaseDataModel.h" 10 | #import "XWDatabaseQueue.h" 11 | #import 12 | 13 | @interface XWDatabaseDataModel () 14 | @end 15 | 16 | @implementation XWDatabaseDataModel 17 | static NSString * const cTableName = @"XWDatabaseDataModelTable"; 18 | 19 | + (void)load { 20 | [[XWDatabaseQueue shareInstance] inDataDatabase:^(FMDatabase * _Nonnull database) { 21 | /// hashID : 二进制唯一标识 data : 二进制文件(bas64字符串形式存储) referenceCount : 引用计数 (每次insert + 1 delete - 1) 22 | NSString *creatTableSql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@(hashID INTEGER PRIMARY KEY ,data BLOB, referenceCount INTEGER)",cTableName]; 23 | [database executeUpdate:creatTableSql]; 24 | }]; 25 | } 26 | 27 | + (void)saveData:(NSData *)data completion:(void(^)(BOOL, NSUInteger))completion { 28 | [[XWDatabaseQueue shareInstance] inDataDatabase:^(FMDatabase * _Nonnull database) { 29 | NSUInteger hash = data.hash; 30 | NSString *searchDataSql = [NSString stringWithFormat:@"SELECT COUNT(*) FROM %@ WHERE hashID = '%lu'",cTableName,(unsigned long)hash]; 31 | BOOL searchSucess = [database executeStatements:searchDataSql withResultBlock:^int(NSDictionary * _Nonnull resultsDictionary) { 32 | int count = [[resultsDictionary.allValues lastObject] intValue]; 33 | if (count == 0) { 34 | NSString *base64 = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; 35 | NSString *save = [NSString stringWithFormat:@"INSERT INTO %@(hashID, data) VALUES('%lu', '%@')",cTableName,(unsigned long)hash,base64]; 36 | BOOL isSuccess = [database executeUpdate:save]; 37 | completion ? completion(isSuccess, hash) : nil; 38 | } else { 39 | completion ? completion(YES, hash) : nil; 40 | } 41 | return 0; 42 | }]; 43 | if (!searchSucess) { 44 | completion ? completion(NO, 0) : nil; 45 | } 46 | }]; 47 | } 48 | 49 | + (void)dataWithHash:(NSString *)hashString completion:(void(^)(NSData *))completion { 50 | [[XWDatabaseQueue shareInstance] inDataDatabase:^(FMDatabase * _Nonnull database) { 51 | NSUInteger hash = hashString.integerValue; 52 | NSString *searchDataSql = [NSString stringWithFormat:@"SELECT COUNT(*) FROM %@ WHERE hashID = '%lu'",cTableName,(unsigned long)hash]; 53 | BOOL searchSucess = [database executeStatements:searchDataSql withResultBlock:^int(NSDictionary * _Nonnull resultsDictionary) { 54 | int count = [[resultsDictionary.allValues lastObject] intValue]; 55 | if (count == 0) { 56 | completion ? completion(nil) : nil; 57 | } else { 58 | // select name from XWStuModel where 59 | NSString *querySql = [NSString stringWithFormat:@"SELECT data FROM %@ WHERE hashID = '%lu'",cTableName,(unsigned long)hash]; 60 | FMResultSet *set = [database executeQuery:querySql]; 61 | while (set.next) { 62 | NSString *base64 = [set objectForColumn:@"data"]; 63 | NSData *data = [[NSData alloc] initWithBase64EncodedString:base64 options:NSDataBase64DecodingIgnoreUnknownCharacters]; 64 | completion ? completion(data) : nil; 65 | } 66 | } 67 | return 0; 68 | }]; 69 | if (!searchSucess) { 70 | completion ? completion(nil) : nil; 71 | } 72 | }]; 73 | } 74 | 75 | @end 76 | -------------------------------------------------------------------------------- /XWDatabase/Classes/XWDatabaseModel.h: -------------------------------------------------------------------------------- 1 | // 2 | // XWDatabaseModel.h 3 | // XWDatabase 4 | // 5 | // Created by 邱学伟 on 2018/11/29. 6 | // Copyright © 2018 邱学伟. All rights reserved. 7 | // 对于模型的操作 8 | 9 | #import 10 | #import 11 | #import "XWDatabaseModelProtocol.h" 12 | 13 | NS_ASSUME_NONNULL_BEGIN 14 | 15 | @interface XWDatabaseModel : NSObject 16 | 17 | /** 18 | 表名 19 | 20 | @param cls 成员变量类 21 | @return 表名 22 | */ 23 | + (NSString *)tableName:(Class)cls; 24 | 25 | /** 26 | 临时表名 27 | 28 | @param cls 模型类 29 | @return 临时表名 30 | */ 31 | + (NSString *)tempTableName:(Class)cls; 32 | 33 | /** 34 | 模型中所有成员变量 (key: 成员变量名称(映射后字段名) value: 成员变量类型) 35 | 36 | @param cls 类 37 | @return 模型中所有成员变量 (key: 成员变量名称(映射后字段名) value: 成员变量类型) 38 | */ 39 | + (NSDictionary *)classColumnIvarNameTypeDict:(Class)cls; 40 | 41 | /** 42 | 模型中所有成员变量在Sqlite 数据库中对应的类型 (成员变量:Sqlite Type) 43 | 44 | @param cls 模型类 45 | @return 模型中所有成员变量在Sqlite 数据库中对应的类型 46 | */ 47 | + (NSDictionary *)classIvarNameSqliteTypeDict:(Class)cls; 48 | 49 | /** 50 | 模型建表的Sql语句 51 | 52 | @param cls 模型类 53 | @return 模型建表的Sql语句 54 | */ 55 | + (NSString *)sqlWithCreatTable:(Class)cls; 56 | 57 | /** 58 | 模型根据字母排序的成员变量数组 59 | 60 | @param cls 模型类 61 | @return 模型根据字母排序的成员变量数组 62 | */ 63 | + (NSArray *)sortedIvarNames:(Class)cls; 64 | 65 | /** 66 | 根据字段名获取模型真实成员变量名 67 | 68 | @param column 字段名 69 | @param cls 类 70 | @return 模型真实成员变量名 71 | */ 72 | + (NSString *)ivarNameWithColumn:(NSString *)column cls:(Class)cls; 73 | 74 | /** 75 | 模型中成员变量集合 76 | 77 | @return 模型中成员变量集合 78 | */ 79 | + (NSSet *)classIvarNamesSet:(Class)cls; 80 | 81 | /** 82 | 模型中自定义对象 83 | 84 | @param cls 模型类 85 | @return 模型中自定义对象 86 | */ 87 | + (NSSet *)customModelSet:(Class)cls; 88 | 89 | /** 90 | 自定义对象映射 (key: 成员变量名称 value: 对象类) 91 | 92 | @return 自定义对象映射 93 | */ 94 | + (NSDictionary * _Nullable)xwdb_customModelMappingCls:(Class)cls; 95 | 96 | #pragma mark - 模型转换 97 | /// NSAarray -> NSString 98 | + (NSString *)stringWithArray:(NSArray *)array; 99 | /// NSString -> NSArray 100 | + (NSArray *)arrayWithString:(NSString *)string; 101 | 102 | /// NSDictionary -> NSString 103 | + (NSString *)stringWithDict:(NSDictionary *)dict; 104 | /// NSDictionary -> NSArray 105 | + (NSDictionary *)dictWithString:(NSString *)string; 106 | 107 | /// NSData -> NSString 108 | + (NSString *)stringWithData:(NSData *)data; 109 | /// NSString -> NSData 110 | + (NSData *)dataWithString:(NSString *)string; 111 | 112 | /// NSDate -> NSString 113 | + (NSString *)stringWithDate:(NSDate *)date; 114 | /// NSString -> NSDate 115 | + (NSDate *)dateWithString:(NSString *)string; 116 | 117 | /// NSNumber -> NSString 118 | + (NSString *)stringWithNumber:(NSNumber *)number; 119 | /// NSString -> NSNumber 120 | + (NSNumber *)numberWithString:(NSString *)string; 121 | 122 | /// NSSet -> NSString 123 | + (NSString *)stringWithSet:(NSSet *)set; 124 | /// NSString -> NSSet 125 | + (NSSet *)setWithString:(NSString *)string; 126 | 127 | /// NSAttributedString -> NSString 128 | + (NSString *)stringWithAttributedString:(NSAttributedString *)attributedString; 129 | /// NSString -> NSAttributedString 130 | + (NSAttributedString *)attributedStringWithString:(NSString *)string; 131 | 132 | /// NSIndexPath -> NSString 133 | + (NSString *)stringWithIndexPath:(NSIndexPath *)indexPath; 134 | /// NSString -> NSIndexPath 135 | + (NSIndexPath *)indexPathWithString:(NSString *)string; 136 | 137 | /// UIImage -> NSString 138 | + (NSString *)stringWithImage:(UIImage *)image; 139 | /// NSString -> UIImage 140 | + (UIImage *)imageWithString:(NSString *)string; 141 | 142 | /// NSURL -> NSString 143 | + (NSString *)stringWithURL:(NSURL *)URL; 144 | /// NSString -> NSURL 145 | + (NSURL *)URLWithString:(NSString *)string; 146 | 147 | /// CustomModel -> NSString 148 | + (NSString *)stringWithCustomModel:(id)customModel; 149 | /// NSString -> CustomModel 150 | + (id)customModelWithString:(NSString *)string; 151 | 152 | @end 153 | 154 | NS_ASSUME_NONNULL_END 155 | -------------------------------------------------------------------------------- /XWDatabase/Classes/XWDatabaseModelProtocol.h: -------------------------------------------------------------------------------- 1 | // 2 | // XWDatabaseModelProtocol.h 3 | // XWDatabase 4 | // 5 | // Created by 邱学伟 on 2018/11/29. 6 | // Copyright © 2018 邱学伟. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /// 模型操作数据库所遵循的协议 12 | @protocol XWDatabaseModelProtocol 13 | 14 | @optional 15 | 16 | /** 17 | 主键 不可更改/唯一性 18 | 19 | @return 主键的属性名 20 | */ 21 | + (NSString *)xw_primaryKey; 22 | 23 | /** 24 | 联合主键成员变量数组 (多个属性共同定义主键) - 优先级大于 'xw_primaryKey' 25 | 26 | @return 联合主键成员变量数组 27 | */ 28 | + (NSArray < NSString * > *)xw_unionPrimaryKey; 29 | 30 | /** 31 | 自定义对象映射 (key: 成员变量名称 value: 对象类) 32 | 33 | @return 自定义对象映射 34 | */ 35 | + (NSDictionary *)xw_customModelMapping; 36 | 37 | /** 38 | 忽略不保存数据库的属性 39 | 40 | @return 忽略的属性名数组 41 | */ 42 | + (NSSet *)xw_ignoreColumnNames; 43 | 44 | /** 45 | 自定义字段名映射表 (默认成员变量即变量名, 可自定义字段名 key: 成员变量(属性)名称 value: 自定义数据库表字段名) 46 | 47 | @return 自定义字段名映射表 48 | */ 49 | + (NSDictionary *)xw_customColumnMapping; 50 | 51 | /** 52 | 自定义表名 (默认属性类名) 53 | 54 | @return 自定义表名 55 | */ 56 | + (NSString *)xw_customTableName; 57 | 58 | /** 59 | 自定义存储的属性数组, 实现此协议在存储时将自动忽略其他属性 60 | 61 | @return 自定义存储的属性数组 62 | */ 63 | + (NSSet < NSString * > *)xw_specificSaveColumnNames; 64 | 65 | @end 66 | -------------------------------------------------------------------------------- /XWDatabase/Classes/XWDatabaseQueue.h: -------------------------------------------------------------------------------- 1 | // 2 | // XWDatabaseQueue.h 3 | // XWDatabase 4 | // 5 | // Created by 邱学伟 on 2018/12/1. 6 | // Copyright © 2018 邱学伟. All rights reserved. 7 | // FMDB 线程安全操作类 8 | 9 | #import 10 | @class FMResultSet,FMDatabase; 11 | 12 | NS_ASSUME_NONNULL_BEGIN 13 | 14 | typedef void(^XWDatabaseQueueUpdateResult)(BOOL isSuccess); 15 | typedef void(^XWDatabaseQueueQueryCountResult)(int count); 16 | typedef void(^XWDatabaseQueueDatabaseHandle)(FMDatabase * _Nonnull database); 17 | typedef void(^XWDatabaseQueueTransactionHandle)(FMDatabase * _Nonnull database, BOOL * _Nonnull rollback); 18 | typedef void(^XWDatabaseQueueQueryResult)(FMResultSet * _Nullable resultSet); 19 | typedef void(^XWDatabaseQueueQueryResults)(NSArray < FMResultSet *> * _Nullable resultSets); 20 | 21 | @interface XWDatabaseQueue : NSObject 22 | 23 | /** 24 | 单例 25 | 26 | @return 单例对象 27 | */ 28 | + (instancetype)shareInstance; 29 | 30 | /** 31 | 数据库操作队列 32 | 33 | @param block 队列内操作 34 | */ 35 | - (void)inDatabase:(XWDatabaseQueueDatabaseHandle)block; 36 | 37 | /** 38 | 数据存储数据库操作队列 39 | 40 | @param block 队列内操作 41 | */ 42 | - (void)inDataDatabase:(XWDatabaseQueueDatabaseHandle)block; 43 | 44 | /** 45 | 数据库事务操作队列 46 | 47 | @param block 队列内操作 48 | */ 49 | - (void)inTransaction:(XWDatabaseQueueTransactionHandle)block; 50 | 51 | /** 52 | 执行更新操作 (单语句) 53 | 54 | @param sql 所执行的 SQL 55 | @param database 数据库 56 | */ 57 | + (BOOL)executeUpdateSql:(NSString *)sql database:(FMDatabase *)database; 58 | 59 | /** 60 | 执行查询操作 (单语句) 61 | 62 | @param sql 所执行的 SQL 63 | @param database 数据库 64 | */ 65 | + (FMResultSet *)executeQuerySql:(NSString *)sql database:(FMDatabase *)database; 66 | 67 | /** 68 | 执行查询操作 (单语句) 69 | 70 | @param sql 所执行的 SQL 71 | @param database 数据库 72 | */ 73 | + (void)executeStatementQuerySql:(NSString *)sql database:(FMDatabase *)database completion:(XWDatabaseQueueQueryCountResult)completion; 74 | 75 | @end 76 | 77 | NS_ASSUME_NONNULL_END 78 | -------------------------------------------------------------------------------- /XWDatabase/Classes/XWDatabaseQueue.m: -------------------------------------------------------------------------------- 1 | // 2 | // XWDatabaseQueue.m 3 | // XWDatabase 4 | // 5 | // Created by 邱学伟 on 2018/12/1. 6 | // Copyright © 2018 邱学伟. All rights reserved. 7 | // 8 | 9 | #import "XWDatabaseQueue.h" 10 | #import 11 | 12 | #define WS(weakSelf) __weak __typeof(self) weakSelf = self; 13 | #define TS(strongSelf) __strong __typeof(weakSelf) strongSelf = weakSelf; 14 | 15 | @interface XWDatabaseQueue () { 16 | FMDatabase *_db; 17 | FMDatabase *_dataDB; 18 | FMDatabaseQueue *_queue; 19 | FMDatabaseQueue *_dataDBQueue; 20 | } 21 | @property (nonatomic, copy) NSString *databasePath; 22 | @property (nonatomic, copy) NSString *dataDatabasePath; 23 | @property (nonatomic, strong) FMDatabase *dataBase; 24 | @property (nonatomic, strong) FMDatabase *dataDataBase; 25 | 26 | @end 27 | 28 | @implementation XWDatabaseQueue 29 | 30 | #pragma mark - Public 31 | static XWDatabaseQueue *_defaultManager; 32 | 33 | /** 34 | 单例 35 | 36 | @return 单例对象 37 | */ 38 | + (instancetype)shareInstance { 39 | if (!_defaultManager) { 40 | _defaultManager = [[self alloc] init]; 41 | } 42 | return _defaultManager; 43 | } 44 | 45 | /** 46 | 数据库操作队列 47 | 48 | @param block 队列内操作 49 | */ 50 | - (void)inDatabase:(XWDatabaseQueueDatabaseHandle)block { 51 | if (!block) { 52 | return; 53 | } 54 | if (self.dataBase) { 55 | block(self.dataBase); 56 | return; 57 | } 58 | WS(weakSelf); 59 | [_queue inDatabase:^(FMDatabase *db){ 60 | TS(strongSelf); 61 | strongSelf.dataBase = db; 62 | block(db); 63 | strongSelf.dataBase = nil; 64 | }]; 65 | } 66 | 67 | /** 68 | 数据存储数据库操作队列 69 | 70 | @param block 队列内操作 71 | */ 72 | - (void)inDataDatabase:(XWDatabaseQueueDatabaseHandle)block { 73 | if (!block) { 74 | return; 75 | } 76 | if (self.dataDataBase) { 77 | block(self.dataDataBase); 78 | return; 79 | } 80 | WS(weakSelf); 81 | [_dataDBQueue inDatabase:^(FMDatabase *db){ 82 | TS(strongSelf); 83 | strongSelf.dataDataBase = db; 84 | block(db); 85 | strongSelf.dataDataBase = nil; 86 | }]; 87 | } 88 | 89 | /** 90 | 数据库事务操作队列 91 | 92 | @param block 队列内操作 93 | */ 94 | - (void)inTransaction:(XWDatabaseQueueTransactionHandle)block { 95 | if (!block) { 96 | return; 97 | } 98 | [_queue inTransaction:^(FMDatabase * _Nonnull db, BOOL * _Nonnull rollback) { 99 | block(db,rollback); 100 | }]; 101 | } 102 | 103 | /** 104 | 执行更新操作 (单语句) 105 | 106 | @param sql 所执行的 SQL 107 | @param database 数据库 108 | */ 109 | + (BOOL)executeUpdateSql:(NSString *)sql database:(FMDatabase *)database { 110 | if (!sql || !database) { 111 | return NO; 112 | } 113 | return [database executeUpdate:sql]; 114 | } 115 | 116 | /** 117 | 执行查询操作 (单语句) 118 | 119 | @param sql 所执行的 SQL 120 | @param database 数据库 121 | */ 122 | + (FMResultSet *)executeQuerySql:(NSString *)sql database:(FMDatabase *)database { 123 | if (!sql || !database) { 124 | return nil; 125 | } 126 | FMResultSet *set = [database executeQuery:sql]; 127 | return set; 128 | } 129 | 130 | /** 131 | 执行查询操作 (单语句) 132 | 133 | @param sql 所执行的 SQL 134 | @param database 数据库 135 | */ 136 | + (void)executeStatementQuerySql:(NSString *)sql database:(FMDatabase *)database completion:(XWDatabaseQueueQueryCountResult)completion { 137 | if (!completion) { 138 | return; 139 | } 140 | if (!sql || !database) { 141 | completion(-1); 142 | return; 143 | } 144 | [database executeStatements:sql withResultBlock:^int(NSDictionary * _Nonnull resultsDictionary) { 145 | int count = [[resultsDictionary.allValues lastObject] intValue]; 146 | completion(count); 147 | return 0; 148 | }]; 149 | } 150 | 151 | #pragma mark - Life Cycle 152 | + (instancetype)allocWithZone:(struct _NSZone *)zone { 153 | if (!_defaultManager) { 154 | static dispatch_once_t onceToken; 155 | dispatch_once(&onceToken, ^{ 156 | _defaultManager = [super allocWithZone:zone]; 157 | }); 158 | } 159 | return _defaultManager; 160 | } 161 | 162 | - (id)copyWithZone:(NSZone *)zone{ 163 | return _defaultManager; 164 | } 165 | 166 | - (id)mutableCopyWithZone:(NSZone *)zone{ 167 | return _defaultManager; 168 | } 169 | 170 | - (instancetype)init { 171 | if (self = [super init]) { 172 | _db = [FMDatabase databaseWithPath:self.databasePath]; 173 | _dataDB = [FMDatabase databaseWithPath:self.dataDatabasePath]; 174 | _queue = [FMDatabaseQueue databaseQueueWithPath:self.databasePath]; 175 | _dataDBQueue = [FMDatabaseQueue databaseQueueWithPath:self.dataDatabasePath]; 176 | } 177 | return self; 178 | } 179 | 180 | #pragma mark - Private 181 | 182 | #pragma mark - Getter 183 | - (NSString *)databasePath { 184 | if (!_databasePath) { 185 | NSString *document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject; 186 | _databasePath = [document stringByAppendingPathComponent:@"XWCommonDatabase.sqlite"]; 187 | } 188 | return _databasePath; 189 | } 190 | - (NSString *)dataDatabasePath { 191 | if(!_dataDatabasePath){ 192 | NSString *document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject; 193 | _dataDatabasePath = [document stringByAppendingPathComponent:@"XWDataDatabase.sqlite"]; 194 | } 195 | return _dataDatabasePath; 196 | } 197 | @end 198 | -------------------------------------------------------------------------------- /XWDatabase/Classes/XWDatabaseSQL.h: -------------------------------------------------------------------------------- 1 | // 2 | // XWDatabaseSQL.h 3 | // XWDatabase 4 | // 5 | // Created by 邱学伟 on 2018/12/1. 6 | // Copyright © 2018 邱学伟. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "XWDatabaseModelProtocol.h" 11 | 12 | NS_ASSUME_NONNULL_BEGIN 13 | 14 | @interface XWDatabaseSQL : NSObject 15 | #pragma mark - 增 16 | 17 | /** 18 | 建表SQL 19 | 20 | @param cls 类 21 | @param isTtemporary 临时表 22 | 23 | @return 建表 sql (CREATE TABLE IF NOT EXISTS Person(xw_id INTEGER PRIMARY KEY AUTOINCREMENT,cardID text,gender text,age integer,name text)) 24 | */ 25 | + (NSString *)createTableSql:(Class)cls isTtemporary:(BOOL)isTtemporary; 26 | 27 | /** 28 | 保存单个对象SQL 29 | 30 | @param obj 模型 31 | @param identifier 标示符 32 | @return 保存单个对象SQL (insert into Person(cardID,age,gender,name) values('1','50','male','极客学伟')) 33 | */ 34 | + (NSString *)insertOneObjSql:(NSObject *)obj identifier:(NSString * _Nullable)identifier; 35 | 36 | /** 37 | 批量更新主键SQLS 38 | 39 | @param cls 模型 40 | @return 批量更新主键SQLS 41 | */ 42 | + (NSArray *)insertPrimarys:(Class)cls; 43 | 44 | #pragma mark - 删 45 | /** 46 | 删除表中某条数据 47 | 48 | @param obj 模型 49 | @param identifier 标示符 50 | @return 是否删除成功 51 | */ 52 | + (NSString *)deleteColumn:(NSObject *)obj identifier:(NSString * _Nullable)identifier; 53 | 54 | /** 55 | 清空表中所有字段 56 | 57 | @param cls 模型类 58 | @param condition 条件 59 | @return 是否删除成功 60 | */ 61 | + (NSString *)clearColumn:(Class)cls identifier:(NSString * _Nullable)identifier condition:(NSString *)condition; 62 | 63 | /** 64 | 删除表 SQL 65 | 66 | @param cls 类 67 | @return 删除表 SQL 68 | */ 69 | + (NSString *)dropTable:(Class)cls; 70 | 71 | #pragma mark - 改 72 | /** 73 | 更新单个对象SQL 74 | 75 | @param obj 模型 76 | @return 保存单个对象SQL 77 | */ 78 | + (NSString *)updateOneObjSql:(NSObject *)obj identifier:(NSString * _Nullable)identifier; 79 | 80 | /** 81 | 更新单个对象SQL 82 | 83 | @param obj 模型 84 | @param identifier 唯一标识 85 | @param condition 自定义更新条件 86 | @param isCustomCondition 是否自定义更新条件 87 | @param updatePropertys 所更新的属性 s 88 | @return 更新单个对象SQL语句 89 | */ 90 | + (NSString *)updateOneObjSql:(NSObject *)obj identifier:(NSString * _Nullable)identifier condition:(NSString * _Nullable)condition isCustomCondition:(BOOL)isCustomCondition updatePropertys:(NSArray *)updatePropertys; 91 | 92 | 93 | /** 94 | 更新指定约束对象SQL 95 | 96 | @param obj 对象 97 | @param identifier 唯一标识 98 | @param condition 更新条件 99 | @param customIvarNames 所更新的属性 s 100 | @return 更新指定约束对象SQL 101 | */ 102 | + (NSString *)updateConditionObjsSql:(NSObject *)obj identifier:(NSString * _Nullable)identifier condition:(NSString * _Nullable)condition customIvarNames:(NSArray *)customIvarNames; 103 | 104 | /** 105 | 更新字段值 SQL 106 | 107 | @param cls 类 108 | @param columName 字段名 109 | @return 更新字段值 SQL 110 | */ 111 | + (NSString *)updateColumn:(Class)cls columName:(NSString *)columName; 112 | 113 | /** 114 | 表重命名 SQL 115 | 116 | @param cls 类 117 | @return 表重命名 SQL 118 | */ 119 | + (NSString *)renameTable:(Class)cls; 120 | 121 | #pragma mark - 查 122 | 123 | 124 | /** 125 | 查询当前类对应的表是否存在 126 | 127 | @param cls 类 128 | @return 查询语句 129 | */ 130 | + (NSString *)isExistTableCls:(Class)cls; 131 | 132 | /** 133 | 查找某主键对象 134 | 135 | @param obj 模型 136 | @param identifier 标示符 137 | @return 查找语句 138 | */ 139 | + (NSString *)searchSql:(NSObject *)obj identifier:(NSString * _Nullable)identifier; 140 | 141 | /** 142 | 查找某条数据是否存在 143 | 144 | @param obj 模型 145 | @return 是否存在 (SELECT COUNT(*) FROM Person WHERE age = '42' AND cardID = '1') 146 | */ 147 | + (NSString *)isExistSql:(NSObject *)obj identifier:(NSString * _Nullable)identifier; 148 | 149 | /** 150 | 查询表内所有数据 (可按照某字段排序) 151 | 152 | @param cls 模型 153 | @param sortColumn 排序字段 154 | @param isOrderDesc 是否降序 155 | @param condition 自定义条件 156 | @param limitCount 限制个数 157 | @return 符合条件的表内所有数据 158 | */ 159 | + (NSString *)searchSql:(Class)cls identifier:(NSString * _Nullable)identifier sortColumn:(NSString *)sortColumn isOrderDesc:(BOOL)isOrderDesc condition:(NSString *)condition limitCount:(NSUInteger)limitCount; 160 | 161 | /** 162 | 现有表 建表语句 163 | 164 | @param cls 类 165 | @return 表 建表语句 (SELECT sql FROM sqlite_master WHERE type = 'table' AND name = '表名') 166 | 获取当前表的建表SQL -> (CREATE TABLE XWPerson(pRect text,birthday text,pFloat real,pLong integer,sex text,icon blob,floatNumber text,pCGFloat real,pBooll integer,books text,name text,cardID text,pBOOL integer,pUInteger integer,pSize text,number text,pPoint text,pDouble real,pLongLong integer,girls text,age integer,pInt integer,pInteger integer,primary key(cardID))) 167 | */ 168 | + (NSString *)queryCreateTableSql:(Class)cls; 169 | 170 | @end 171 | 172 | NS_ASSUME_NONNULL_END 173 | -------------------------------------------------------------------------------- /XWDatabase/Classes/XWLivingThread.h: -------------------------------------------------------------------------------- 1 | // 2 | // XWLivingThread.h 3 | // XWDatabase 4 | // 5 | // Created by 邱学伟 on 2018/12/5. 6 | // Copyright © 2018 邱学伟. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | typedef void(^XWLivingThreadTask)(void); 14 | 15 | @interface XWLivingThread : NSObject 16 | /** 17 | 主线程触发某操作 18 | 19 | @param task 任务 20 | */ 21 | + (void)executeTaskInMain:(XWLivingThreadTask)task; 22 | 23 | /** 24 | 在默认全局常驻线程中执行操作 (只要调用,默认线程即创建且不会销毁) 25 | 26 | @param task 操作 27 | */ 28 | + (void)executeTask:(XWLivingThreadTask)task; 29 | 30 | /** 31 | 在自定义全局常驻线程中执行操作 (根据 identity 创建自定义线程,且创建后不会销毁) 32 | 33 | @param task 操作 34 | @param identity 自定义线程唯一标识 35 | */ 36 | + (void)executeTask:(XWLivingThreadTask)task identity:(NSString *)identity; 37 | 38 | /** 39 | 在默认常驻线程中执行操作 (线程需随当前对象创建或销魂) 40 | 41 | @param task 操作 42 | */ 43 | - (void)executeTask:(XWLivingThreadTask)task; 44 | 45 | @end 46 | 47 | NS_ASSUME_NONNULL_END 48 | -------------------------------------------------------------------------------- /XWDatabase/Classes/XWLivingThread.m: -------------------------------------------------------------------------------- 1 | // 2 | // XWLivingThread.m 3 | // XWDatabase 4 | // 5 | // Created by 邱学伟 on 2018/12/5. 6 | // Copyright © 2018 邱学伟. All rights reserved. 7 | // 8 | 9 | #import "XWLivingThread.h" 10 | 11 | @interface XWLivingThread() 12 | @property (nonatomic, weak) NSThread *p_thread; 13 | @property (nonatomic, assign, getter=isShouldKeepRunning) BOOL shouldKeepRunning; 14 | @end 15 | 16 | @implementation XWLivingThread 17 | static NSThread *xw_defaultThread; 18 | static NSMutableDictionary *xw_threadDictM; 19 | 20 | #pragma mark - public 21 | 22 | /** 23 | 主线程触发某操作 24 | 25 | @param task 任务 26 | */ 27 | + (void)executeTaskInMain:(XWLivingThreadTask)task { 28 | if (!task) { 29 | return; 30 | } 31 | 32 | void(^block)(void) = ^ { 33 | [XWLivingThread executeTask:task]; 34 | }; 35 | if ([NSThread currentThread].isMainThread) { 36 | block(); 37 | } else { 38 | dispatch_async(dispatch_get_main_queue(), block); 39 | } 40 | } 41 | 42 | /** 43 | 在默认全局常驻线程中执行操作 (只要调用,默认线程即创建且不会销毁) 44 | 45 | @param task 操作 46 | */ 47 | + (void)executeTask:(XWLivingThreadTask)task { 48 | if (!task) { 49 | return; 50 | } 51 | 52 | if (!xw_defaultThread) { 53 | void (^creatThreadBlock)(void) = ^ { 54 | NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop]; 55 | [currentRunLoop addPort:[NSPort new] forMode:NSDefaultRunLoopMode]; 56 | [[NSRunLoop currentRunLoop] run]; 57 | while (1) { 58 | [currentRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; 59 | } 60 | }; 61 | 62 | if (@available(iOS 10.0, *)) { 63 | xw_defaultThread = [[NSThread alloc] initWithBlock:creatThreadBlock]; 64 | } else { 65 | xw_defaultThread = [[NSThread alloc] initWithTarget:self selector:@selector(class_creatThreadMethod:) object:creatThreadBlock]; 66 | } 67 | [xw_defaultThread start]; 68 | } 69 | [self performSelector:@selector(class_taskMethod:) onThread:xw_defaultThread withObject:task waitUntilDone:NO]; 70 | } 71 | 72 | + (void)class_creatThreadMethod:(void (^)(void))block { 73 | block(); 74 | } 75 | 76 | + (void)class_taskMethod:(XWLivingThreadTask)task { 77 | task(); 78 | } 79 | 80 | /** 81 | 在自定义全局常驻线程中执行操作 (根据 identity 创建自定义线程,且创建后不会销毁) 82 | 83 | @param task 操作 84 | @param identity 自定义线程唯一标识 85 | */ 86 | + (void)executeTask:(XWLivingThreadTask)task identity:(NSString *)identity { 87 | if (!task || !identity || identity.length == 0) { 88 | return; 89 | } 90 | 91 | if (!xw_threadDictM) { 92 | xw_threadDictM = [NSMutableDictionary dictionary]; 93 | } 94 | 95 | NSThread *threadByIdentity = [xw_threadDictM objectForKey:identity]; 96 | 97 | if (!threadByIdentity) { 98 | void (^creatThreadBlock)(void) = ^ { 99 | CFRunLoopSourceContext content = {0}; 100 | CFRunLoopSourceRef source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &content); 101 | CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode); 102 | CFRelease(source); 103 | CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0e10, false); 104 | }; 105 | 106 | if (@available(iOS 10.0, *)) { 107 | threadByIdentity = [[NSThread alloc] initWithBlock:creatThreadBlock]; 108 | } else { 109 | threadByIdentity = [[NSThread alloc] initWithTarget:self selector:@selector(class_creatThreadMethod:) object:creatThreadBlock]; 110 | } 111 | [threadByIdentity start]; 112 | 113 | if (threadByIdentity) { 114 | [xw_threadDictM setObject:threadByIdentity forKey:identity]; 115 | } 116 | } 117 | 118 | [self performSelector:@selector(class_taskMethod:) onThread:threadByIdentity withObject:task waitUntilDone:NO]; 119 | } 120 | 121 | /** 122 | 在默认常驻线程中执行操作 (线程需随当前对象创建或销毁) 123 | 124 | @param task 操作 125 | */ 126 | - (void)executeTask:(XWLivingThreadTask)task { 127 | if (!task || !self.p_thread) return; 128 | [self performSelector:@selector(threakTaskMethod:) onThread:self.p_thread withObject:task waitUntilDone:NO]; 129 | } 130 | 131 | #pragma mark - system 132 | - (instancetype)init { 133 | if (self = [super init]) { 134 | self.p_thread = [self thread]; 135 | } 136 | return self; 137 | } 138 | 139 | - (void)dealloc { 140 | [self performSelector:@selector(clearThreadMethod) onThread:self.p_thread withObject:nil waitUntilDone:YES]; 141 | } 142 | 143 | #pragma mark - private 144 | - (NSThread *)thread { 145 | NSThread *thread = nil; 146 | __weak typeof(self) weakSelf = self; 147 | void (^creatThreadBlock)(void) = ^ { 148 | NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop]; 149 | [currentRunLoop addPort:[NSPort new] forMode:NSDefaultRunLoopMode]; 150 | while (weakSelf && weakSelf.isShouldKeepRunning) { 151 | [currentRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; 152 | } 153 | }; 154 | 155 | self.shouldKeepRunning = YES; 156 | if (@available(iOS 10.0, *)) { 157 | thread = [[NSThread alloc] initWithBlock:creatThreadBlock]; 158 | } else { 159 | thread = [[NSThread alloc] initWithTarget:weakSelf selector:@selector(creatThreadMethod:) object:creatThreadBlock]; 160 | } 161 | [thread start]; 162 | return thread; 163 | } 164 | 165 | - (void)creatThreadMethod:(void (^)(void))creatThreadBlock { 166 | creatThreadBlock(); 167 | } 168 | 169 | - (void)threakTaskMethod:(void (^)(void))task { 170 | task ? task() : nil; 171 | } 172 | 173 | - (void)clearThreadMethod { 174 | self.shouldKeepRunning = NO; 175 | CFRunLoopStop(CFRunLoopGetCurrent()); 176 | } 177 | 178 | @end 179 | 180 | -------------------------------------------------------------------------------- /_Pods.xcodeproj: -------------------------------------------------------------------------------- 1 | Example/Pods/Pods.xcodeproj --------------------------------------------------------------------------------