Skip to content

Commit

Permalink
Improve FMDBVersion
Browse files Browse the repository at this point in the history
- Previously, `FMDBVersion` would break if any component exceeded 9.
- With this change, it doesn't break until any component exceeds 15, at which point it will max out at 0xf for that component and log an error.
- Added documentation suggesting FMDBUserVersion with NSNumericSearch option.
- Added unit tests for proper version number checking.
- While unit testing, I noticed that `testBoolInsert` was attempting `[NSNumber numberWithBool:12]`, which is not valid. Replaced with `YES`.
  • Loading branch information
robertmryan committed May 5, 2020
1 parent fab5c73 commit f4ddeb5
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 34 deletions.
17 changes: 13 additions & 4 deletions Tests/FMDatabaseTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ - (void)testCaseSensitiveResultDictionary
{
// case sensitive result dictionary test
[self.db executeUpdate:@"create table cs (aRowName integer, bRowName text)"];
[self.db executeUpdate:@"insert into cs (aRowName, bRowName) values (?, ?)", [NSNumber numberWithBool:1], @"hello"];
[self.db executeUpdate:@"insert into cs (aRowName, bRowName) values (?, ?)", [NSNumber numberWithInt:1], @"hello"];

XCTAssertFalse([self.db hadError], @"Shouldn't have any errors");

Expand All @@ -278,13 +278,12 @@ - (void)testCaseSensitiveResultDictionary
- (void)testBoolInsert
{
[self.db executeUpdate:@"create table btest (aRowName integer)"];
[self.db executeUpdate:@"insert into btest (aRowName) values (?)", [NSNumber numberWithBool:12]];
[self.db executeUpdate:@"insert into btest (aRowName) values (?)", [NSNumber numberWithBool:YES]];

XCTAssertFalse([self.db hadError], @"Shouldn't have any errors");

FMResultSet *rs = [self.db executeQuery:@"select * from btest"];
while ([rs next]) {

XCTAssertTrue([rs boolForColumnIndex:0], @"first column should be true.");
XCTAssertTrue([rs intForColumnIndex:0] == 1, @"first column should be equal to 1 - it was %d.", [rs intForColumnIndex:0]);
}
Expand Down Expand Up @@ -1125,7 +1124,17 @@ - (void)createCustomFunctions {
}

- (void)testVersionNumber {
XCTAssertTrue([FMDatabase FMDBVersion] == 0x0275); // this is going to break everytime we bump it.
XCTAssertEqual([FMDatabase FMDBVersion], 0x0276); // this is going to break everytime we bump it.
}

- (void)testVersionStringAboveRequired {
NSComparisonResult result = [[FMDatabase FMDBUserVersion] compare:@"1.100.42" options:NSNumericSearch];
XCTAssertEqual(result, NSOrderedDescending);
}

- (void)testVersionStringBelowRequired {
NSComparisonResult result = [[FMDatabase FMDBUserVersion] compare:@"10.0.42" options:NSNumericSearch];
XCTAssertEqual(result, NSOrderedAscending);
}

- (void)testExecuteStatements {
Expand Down
4 changes: 3 additions & 1 deletion fmdb.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@
08FB7793FE84155DC02AAC07 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1000;
LastUpgradeCheck = 1140;
TargetAttributes = {
83C73EFD1C326AB000FFC730 = {
CreatedOnToolsVersion = 7.2;
Expand Down Expand Up @@ -805,6 +805,7 @@
CLANG_WARN_OBJC_IMPLICIT_ATOMIC_PROPERTIES = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_SUSPICIOUS_IMPLICIT_CONVERSION = YES;
CODE_SIGN_IDENTITY = "-";
COPY_PHASE_STRIP = NO;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
Expand All @@ -828,6 +829,7 @@
CLANG_WARN_OBJC_IMPLICIT_ATOMIC_PROPERTIES = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_SUSPICIOUS_IMPLICIT_CONVERSION = YES;
CODE_SIGN_IDENTITY = "-";
GCC_GENERATE_DEBUGGING_SYMBOLS = NO;
GCC_MODEL_TUNING = G5;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
Expand Down
6 changes: 1 addition & 5 deletions fmdb.xcodeproj/xcshareddata/xcschemes/FMDB MacOS.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1000"
LastUpgradeVersion = "1140"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -29,8 +29,6 @@
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
Expand All @@ -51,8 +49,6 @@
ReferencedContainer = "container:fmdb.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
6 changes: 1 addition & 5 deletions fmdb.xcodeproj/xcshareddata/xcschemes/FMDB iOS.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1000"
LastUpgradeVersion = "1140"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -29,8 +29,6 @@
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
Expand All @@ -51,8 +49,6 @@
ReferencedContainer = "container:fmdb.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
10 changes: 3 additions & 7 deletions fmdb.xcodeproj/xcshareddata/xcschemes/FMDB watchOS.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1000"
LastUpgradeVersion = "1140"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -27,8 +27,6 @@
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
Expand All @@ -38,8 +36,8 @@
ReferencedContainer = "container:fmdb.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
Expand All @@ -60,8 +58,6 @@
ReferencedContainer = "container:fmdb.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
28 changes: 27 additions & 1 deletion src/fmdb/FMDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -1085,11 +1085,37 @@ typedef NS_ENUM(int, FMDBCheckpointMode) {

+ (NSString*)sqliteLibVersion;

/**
The FMDB version number as a string in the form of `"2.7.7"`.
If you want to compare version number strings, you can use NSNumericSearch option:
@code
NSComparisonResult result = [[FMDatabase FMDBUserVersion] compare:@"2.11.0" options:NSNumericSearch];
@endcode
@returns The version number string.
*/

+ (NSString*)FMDBUserVersion;

+ (SInt32)FMDBVersion;
/** The FMDB version
This returns the FMDB as hexadecimal value, e.g., 0x0243 for version 2.4.3.
@warning This routine will not work if any component of the version number exceeds 15.
For example, if it is version 2.17.3, this will max out at 0x2f3.
For this reason, we would recommend using `FMDBUserVersion` and with `NSNumericSearch` option, e.g.
@code
NSComparisonResult result = [[FMDatabase FMDBUserVersion] compare:@"2.11.0" options:NSNumericSearch];
@endcode
@returns The version number in hexadecimal, e.g., 0x0243 for version 2.4.3. If any component exceeds what can be
can be represented in four bits, we'll max it out at 0xf.
*/

+ (SInt32)FMDBVersion;

///------------------------
/// @name Make SQL function
Expand Down
22 changes: 11 additions & 11 deletions src/fmdb/FMDatabase.m
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ + (NSString*)FMDBUserVersion {
return @"2.7.6";
}

// returns 0x0240 for version 2.4. This makes it super easy to do things like:
// /* need to make sure to do X with FMDB version 2.4 or later */
// if ([FMDatabase FMDBVersion] >= 0x0240) { … }

+ (SInt32)FMDBVersion {

// we go through these hoops so that we only have to change the version number in a single spot.
Expand All @@ -115,15 +111,19 @@ + (SInt32)FMDBVersion {
dispatch_once(&once, ^{
NSString *prodVersion = [self FMDBUserVersion];

if ([[prodVersion componentsSeparatedByString:@"."] count] < 3) {
while ([[prodVersion componentsSeparatedByString:@"."] count] < 3) {
prodVersion = [prodVersion stringByAppendingString:@".0"];
}

NSString *junk = [prodVersion stringByReplacingOccurrencesOfString:@"." withString:@""];

char *e = nil;
FMDBVersionVal = (int) strtoul([junk UTF8String], &e, 16);


NSArray *components = [prodVersion componentsSeparatedByString:@"."];
for (NSInteger i = 0; i < 3; i++) {
SInt32 component = [components[i] intValue];
if (component > 15) {
NSLog(@"FMDBVersion is invalid: Please use FMDBUserVersion instead.");
component = 15;
}
FMDBVersionVal = FMDBVersionVal << 4 | component;
}
});

return FMDBVersionVal;
Expand Down

0 comments on commit f4ddeb5

Please sign in to comment.