Skip to content

Commit

Permalink
Bug fix for issue #104
Browse files Browse the repository at this point in the history
  • Loading branch information
robbiehanson committed Aug 12, 2014
1 parent 1d848f4 commit fb96066
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 24 deletions.
87 changes: 87 additions & 0 deletions Testing/UnitTesting/TestYapDatabaseSecondaryIndex.m
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,91 @@ - (void)test
}];
}

/**
* https://github.com/yaptv/YapDatabase/issues/104
**/
- (void)testIssue104
{
NSString *databasePath = [self databasePath:NSStringFromSelector(_cmd)];

[[NSFileManager defaultManager] removeItemAtPath:databasePath error:NULL];
YapDatabase *database = [[YapDatabase alloc] initWithPath:databasePath];

XCTAssertNotNil(database, @"Oops");

YapDatabaseConnection *connection = [database newConnection];

YapDatabaseSecondaryIndexSetup *setup = [[YapDatabaseSecondaryIndexSetup alloc] init];
[setup addColumn:@"str" withType:YapDatabaseSecondaryIndexTypeText];

YapDatabaseSecondaryIndexBlockType blockType = YapDatabaseSecondaryIndexBlockTypeWithObject;
YapDatabaseSecondaryIndexWithObjectBlock block =
^(NSMutableDictionary *dict, NSString *collection, NSString *key, id object){

// If we're storing other types of objects in our database,
// then we should check the object before presuming we can cast it.
if ([object isKindOfClass:[NSString class]])
{
__unsafe_unretained NSString *str = (NSString *)object;

if ([str hasPrefix:@"like "]) {
[dict setObject:str forKey:@"str"];
}
}
};

YapDatabaseSecondaryIndex *secondaryIndex =
[[YapDatabaseSecondaryIndex alloc] initWithSetup:setup block:block blockType:blockType];

[database registerExtension:secondaryIndex withName:@"idx"];

//
// Test initial population
//

[connection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {

[transaction setObject:@"like whatever" forKey:@"1" inCollection:nil];
[transaction setObject:@"as if" forKey:@"2" inCollection:nil];


__block NSUInteger count = 0;

YapDatabaseQuery *query = [YapDatabaseQuery queryMatchingAll];
[[transaction ext:@"idx"] enumerateKeysAndObjectsMatchingQuery:query usingBlock:
^(NSString *collection, NSString *key, id object, BOOL *stop)
{
count++;

XCTAssert([object isEqual:@"like whatever"], @"like whatever, guess the code sucks");
}];

XCTAssertTrue(count == 1, @"Incorrect count: %lu", (unsigned long)count);
}];

//
// Test update
//

[connection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {

[transaction setObject:@"whatever" forKey:@"1" inCollection:nil];
[transaction setObject:@"like as if" forKey:@"2" inCollection:nil];


__block NSUInteger count = 0;

YapDatabaseQuery *query = [YapDatabaseQuery queryMatchingAll];
[[transaction ext:@"idx"] enumerateKeysAndObjectsMatchingQuery:query usingBlock:
^(NSString *collection, NSString *key, id object, BOOL *stop)
{
count++;

XCTAssert([object isEqual:@"like as if"], @"like as if, the code was going to work");
}];

XCTAssertTrue(count == 1, @"Incorrect count: %lu", (unsigned long)count);
}];
}

@end
115 changes: 111 additions & 4 deletions Testing/Xcode-mobile/YapDatabase.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,69 @@
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
DC431595199A6DCD00600AFE /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = DCAE51DF1673FE2600395076 /* Project object */;
proxyType = 1;
remoteGlobalIDString = DCAE51E61673FE2600395076;
remoteInfo = YapDatabase;
};
DC431597199A6DD100600AFE /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = DCAE51DF1673FE2600395076 /* Project object */;
proxyType = 1;
remoteGlobalIDString = DCAE51E61673FE2600395076;
remoteInfo = YapDatabase;
};
DC431599199A6DDC00600AFE /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = DCAE51DF1673FE2600395076 /* Project object */;
proxyType = 1;
remoteGlobalIDString = DCAE51E61673FE2600395076;
remoteInfo = YapDatabase;
};
DC43159B199A6DDD00600AFE /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = DCAE51DF1673FE2600395076 /* Project object */;
proxyType = 1;
remoteGlobalIDString = DCAE51E61673FE2600395076;
remoteInfo = YapDatabase;
};
DCAE52151673FE2600395076 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = DCAE51DF1673FE2600395076 /* Project object */;
proxyType = 1;
remoteGlobalIDString = DCAE51E61673FE2600395076;
remoteInfo = YapDatabase;
};
DCE633B2199A6EED00D1A19B /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = DCAE51DF1673FE2600395076 /* Project object */;
proxyType = 1;
remoteGlobalIDString = DCAE51E61673FE2600395076;
remoteInfo = YapDatabase;
};
DCE633B4199A6EEE00D1A19B /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = DCAE51DF1673FE2600395076 /* Project object */;
proxyType = 1;
remoteGlobalIDString = DCAE51E61673FE2600395076;
remoteInfo = YapDatabase;
};
DCE633B6199A6EF000D1A19B /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = DCAE51DF1673FE2600395076 /* Project object */;
proxyType = 1;
remoteGlobalIDString = DCAE51E61673FE2600395076;
remoteInfo = YapDatabase;
};
DCE633B8199A6EF400D1A19B /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = DCAE51DF1673FE2600395076 /* Project object */;
proxyType = 1;
remoteGlobalIDString = DCAE51E61673FE2600395076;
remoteInfo = YapDatabase;
};
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -871,6 +927,14 @@
);
dependencies = (
DCAE52161673FE2600395076 /* PBXTargetDependency */,
DC431596199A6DCD00600AFE /* PBXTargetDependency */,
DC431598199A6DD100600AFE /* PBXTargetDependency */,
DC43159A199A6DDC00600AFE /* PBXTargetDependency */,
DC43159C199A6DDD00600AFE /* PBXTargetDependency */,
DCE633B3199A6EED00D1A19B /* PBXTargetDependency */,
DCE633B5199A6EEE00D1A19B /* PBXTargetDependency */,
DCE633B7199A6EF000D1A19B /* PBXTargetDependency */,
DCE633B9199A6EF400D1A19B /* PBXTargetDependency */,
);
name = YapDatabaseTests;
productName = YapDatabaseTests;
Expand All @@ -890,6 +954,9 @@
DCAE51E61673FE2600395076 = {
DevelopmentTeam = 4D98PK6HWS;
};
DCAE520F1673FE2600395076 = {
TestTargetID = DCAE51E61673FE2600395076;
};
};
};
buildConfigurationList = DCAE51E21673FE2600395076 /* Build configuration list for PBXProject "YapDatabase" */;
Expand Down Expand Up @@ -1047,11 +1114,51 @@
/* End PBXSourcesBuildPhase section */

/* Begin PBXTargetDependency section */
DC431596199A6DCD00600AFE /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = DCAE51E61673FE2600395076 /* YapDatabase */;
targetProxy = DC431595199A6DCD00600AFE /* PBXContainerItemProxy */;
};
DC431598199A6DD100600AFE /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = DCAE51E61673FE2600395076 /* YapDatabase */;
targetProxy = DC431597199A6DD100600AFE /* PBXContainerItemProxy */;
};
DC43159A199A6DDC00600AFE /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = DCAE51E61673FE2600395076 /* YapDatabase */;
targetProxy = DC431599199A6DDC00600AFE /* PBXContainerItemProxy */;
};
DC43159C199A6DDD00600AFE /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = DCAE51E61673FE2600395076 /* YapDatabase */;
targetProxy = DC43159B199A6DDD00600AFE /* PBXContainerItemProxy */;
};
DCAE52161673FE2600395076 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = DCAE51E61673FE2600395076 /* YapDatabase */;
targetProxy = DCAE52151673FE2600395076 /* PBXContainerItemProxy */;
};
DCE633B3199A6EED00D1A19B /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = DCAE51E61673FE2600395076 /* YapDatabase */;
targetProxy = DCE633B2199A6EED00D1A19B /* PBXContainerItemProxy */;
};
DCE633B5199A6EEE00D1A19B /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = DCAE51E61673FE2600395076 /* YapDatabase */;
targetProxy = DCE633B4199A6EEE00D1A19B /* PBXContainerItemProxy */;
};
DCE633B7199A6EF000D1A19B /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = DCAE51E61673FE2600395076 /* YapDatabase */;
targetProxy = DCE633B6199A6EF000D1A19B /* PBXContainerItemProxy */;
};
DCE633B9199A6EF400D1A19B /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = DCAE51E61673FE2600395076 /* YapDatabase */;
targetProxy = DCE633B8199A6EF400D1A19B /* PBXContainerItemProxy */;
};
/* End PBXTargetDependency section */

/* Begin PBXVariantGroup section */
Expand Down Expand Up @@ -1182,7 +1289,7 @@
DCAE52261673FE2600395076 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/YapDatabase.app/YapDatabase";
BUNDLE_LOADER = "$(TEST_HOST)";
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
Expand All @@ -1192,14 +1299,14 @@
GCC_PREFIX_HEADER = "YapDatabase/YapDatabase-Prefix.pch";
INFOPLIST_FILE = "YapDatabaseTests/YapDatabaseTests-Info.plist";
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUNDLE_LOADER)";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/YapDatabase.app/YapDatabase";
};
name = Debug;
};
DCAE52271673FE2600395076 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/YapDatabase.app/YapDatabase";
BUNDLE_LOADER = "$(TEST_HOST)";
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
Expand All @@ -1209,7 +1316,7 @@
GCC_PREFIX_HEADER = "YapDatabase/YapDatabase-Prefix.pch";
INFOPLIST_FILE = "YapDatabaseTests/YapDatabaseTests-Info.plist";
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUNDLE_LOADER)";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/YapDatabase.app/YapDatabase";
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,20 +266,23 @@ - (sqlite3_stmt *)updateStatement
if (*statement == NULL)
{
NSMutableString *string = [NSMutableString stringWithCapacity:100];
[string appendFormat:@"UPDATE \"%@\" SET ", [secondaryIndex tableName]];
[string appendFormat:@"INSERT OR REPLACE INTO \"%@\" (\"rowid\"", [secondaryIndex tableName]];

NSUInteger i = 0;
for (YapDatabaseSecondaryIndexColumn *column in secondaryIndex->setup)
{
if (i == 0)
[string appendFormat:@"\"%@\" = ?", column.name];
else
[string appendFormat:@", \"%@\" = ?", column.name];

i++;
[string appendFormat:@", \"%@\"", column.name];
}

[string appendString:@" WHERE rowid = ?;"];
[string appendString:@") VALUES (?"];

NSUInteger count = [secondaryIndex->setup count];
NSUInteger i;
for (i = 0; i < count; i++)
{
[string appendString:@", ?"];
}

[string appendString:@");"];

sqlite3 *db = databaseConnection->db;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,15 +439,13 @@ - (void)addRowid:(int64_t)rowid isNew:(BOOL)isNew
if (statement == NULL)
return;

// isNew : INSERT INTO "tableName" ("rowid", "column1", "column2", ...) VALUES (?, ?, ? ...);
// !isNew : UPDATE "tableName" SET "column1" = ?, "column2" = ?, ... WHERE "rowid" = ?;
// isNew : INSERT INTO "tableName" ("rowid", "column1", "column2", ...) VALUES (?, ?, ? ...);
// !isNew : INSERT OR REPLACE INTO "tableName" ("rowid", "column1", "column2", ...) VALUES (?, ?, ? ...);

int i = 1;

if (isNew) {
sqlite3_bind_int64(statement, i, rowid);
i++;
}
sqlite3_bind_int64(statement, i, rowid);
i++;

for (YapDatabaseSecondaryIndexColumn *column in secondaryIndexConnection->secondaryIndex->setup)
{
Expand Down Expand Up @@ -513,11 +511,6 @@ - (void)addRowid:(int64_t)rowid isNew:(BOOL)isNew
i++;
}

if (!isNew) {
sqlite3_bind_int64(statement, i, rowid);
i++;
}

int status = sqlite3_step(statement);
if (status != SQLITE_DONE)
{
Expand Down

0 comments on commit fb96066

Please sign in to comment.