Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception for ZIMSqlUpsertStatement with one indexed field #31

Closed
chapayGhub opened this issue Jul 24, 2014 · 1 comment
Closed

Exception for ZIMSqlUpsertStatement with one indexed field #31

chapayGhub opened this issue Jul 24, 2014 · 1 comment

Comments

@chapayGhub
Copy link

Table 'Installed" has only one indexea key "app_id"
Exception when I trying to do next

        ZIMSqlUpsertStatement *sql = [[ZIMSqlUpsertStatement alloc] init];
        [sql into: [Installed table]];
        [sql column: @"app_id" value: [app_id valueForKey:@"app_id"]];
        [sql matching:[NSArray arrayWithObject:@"app_id"]];

        NSString *statement = [sql statement];
        [self.connection execute:statement];

I make some fix in file ZIMSqlUpsertStatement.m

- (NSString *) statement {
    // Note: Because "INSERT OR REPLACE" requires prior knowledge of the table's columns to properly update a record,
    // this method for mimicing an upsert statement was chosen.  Therefore, always match against either the primary key
    // or a unique key.

    NSMutableString *sql = [[NSMutableString alloc] init];

    [sql appendFormat: @"UPDATE OR IGNORE %@ SET ", _table];

    NSMutableArray *set = [[NSMutableArray alloc] init];
    NSMutableArray *where = [[NSMutableArray alloc] init];

    for (NSString *column in _column) {
        if ([_compositeKey containsObject: column]) {
/////// MY FIX START /////// 
            if( [_column count] == 1 )
            {
                [set addObject: [NSString stringWithFormat: @"%@ = %@", column, [_column objectForKey: column]]];
            }            
/////// MY FIX END /////// 
            [where addObject: [NSString stringWithFormat: @"%@ = %@", column, [_column objectForKey: column]]];
        }
        else {
            [set addObject: [NSString stringWithFormat: @"%@ = %@", column, [_column objectForKey: column]]];
        }
    }

    [sql appendString: [set componentsJoinedByString: @", "]];
    [sql appendString: @" WHERE "];
    [sql appendString: [where componentsJoinedByString: @" AND "]];

    [sql appendString: @"; "];

    [sql appendFormat: @"INSERT OR IGNORE INTO %@ ", _table];

    [sql appendFormat: @"(%@) VALUES (%@)", [[_column allKeys] componentsJoinedByString: @", "], [[_column allValues] componentsJoinedByString: @", "]];

    [sql appendString: @";"];

    return sql;
}
ziminji added a commit that referenced this issue Sep 27, 2014
@ziminji
Copy link
Owner

ziminji commented Sep 27, 2014

Thanks!

@ziminji ziminji closed this as completed Sep 27, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants