Permalink
Browse files

Return NULL / -1 where possible in lieu of asserts.

  • Loading branch information...
1 parent 1ea52f8 commit 3b14ea911a67bab153174852c9bb01dba93f5445 @ciaranj committed Apr 9, 2012
Showing with 206 additions and 174 deletions.
  1. +6 −2 MySql.h
  2. +49 −31 MySql.m
  3. +4 −4 MySqlProtocol.h
  4. +147 −137 MySqlProtocol.m
View
@@ -22,8 +22,12 @@
-(id) initWithHost:(NSString *)host port:(UInt16)port user:(NSString *)user password:(NSString *)password;
-(id) initWithProtocol:(MySqlProtocol*) __attribute__((ns_consumed)) protocol user:(NSString *)user password:(NSString *)password;
--(void) selectDatabase:(NSString*)database;
+
+// 'Non-Blocking' functions.
-(void) performQuery:(NSString*)query continueWithBlock:(void (^)(MySqlResults *))block;
--(void) quit;
+
+// 'Blocking' functions.
+-(int) selectDatabase:(NSString*)database;
+-(int) quit;
@end
#endif
View
80 MySql.m
@@ -18,10 +18,17 @@ @implementation MySql
-(id) initWithProtocol:(MySqlProtocol*) __attribute__((ns_consumed)) protocol user:(NSString *)user password:(NSString *)password {
if ( self = [super init] ) {
protocolImpl= protocol;
- [protocolImpl connect];
- [protocolImpl handshakeForUserName:user
- password:password];
+ if( [protocolImpl connect] ) {
+ if( ![protocolImpl handshakeForUserName:user
+ password:password] ) {
+ return NULL;
+ }
+ }
+ else {
+ return NULL;
+ }
}
+
return self;
}
@@ -38,44 +45,52 @@ -(void)dealloc {
[super dealloc];
}
--(void) selectDatabase:(NSString*)database {
+-(int) selectDatabase:(NSString*)database {
volatile __block bool blockCalled= false;
+ volatile __block int selectDBResult= 0;
- NSLog(@"Select Database");
NSData* data=[database dataUsingEncoding:NSUTF8StringEncoding];
- [protocolImpl sendCommand:2 data:data continueWithBlock:^(){
+ [protocolImpl sendCommand:2 data:data continueWithBlock:^(int result){
+ if( result ) {
NSData* okOrErrorPacket= [protocolImpl readPacket];
- UInt8* resultPacketData= (UInt8*)[okOrErrorPacket bytes];
- if( resultPacketData[0] == 0xFF ) {
- uint16_t errorNumber= resultPacketData[1] + (resultPacketData[2]<<8);
- // sqlstate is chars 3-> 8
-
- NSString* errorMessage= [[NSString alloc] initWithCString: (const char*)(resultPacketData+9) encoding:NSASCIIStringEncoding];
-
- NSLog(@"ERROR: %@ (%u)", errorMessage, errorNumber);
- for(int i=0;i< [okOrErrorPacket length]; i++ ) {
- fprintf(stderr, "%x ", resultPacketData[i]);
+ if( okOrErrorPacket ) {
+ UInt8* resultPacketData= (UInt8*)[okOrErrorPacket bytes];
+ if( resultPacketData[0] == 0xFF ) {
+ uint16_t errorNumber= resultPacketData[1] + (resultPacketData[2]<<8);
+ // sqlstate is chars 3-> 8
+
+ NSString* errorMessage= [[NSString alloc] initWithCString: (const char*)(resultPacketData+9) encoding:NSASCIIStringEncoding];
+
+ NSLog(@"ERROR: %@ (%u)", errorMessage, errorNumber);
+ for(int i=0;i< [okOrErrorPacket length]; i++ ) {
+ fprintf(stderr, "%x ", resultPacketData[i]);
+ }
+ [errorMessage release];
+
+ selectDBResult= -1;
}
- [errorMessage release];
- }
- else {
- NSLog(@"HAPPPY PACKET");
+ else {
+ selectDBResult= 1;
+ }
+ [okOrErrorPacket release];
}
- [okOrErrorPacket release];
- blockCalled= true;
- }];
+ } else {
+ selectDBResult= result;
+ }
+ blockCalled= true;
+ }];
// As an attempt to simplify the external API, we
// simulate a blocking API by blocking the calling thread, until
// the command's callback block has executed.
while(!blockCalled) {
- [NSThread sleepForTimeInterval:0.01];
+ [NSThread sleepForTimeInterval:0.25];
}
+ return selectDBResult;
}
-(void) performQuery:(NSString*)query continueWithBlock:(void (^)(MySqlResults* results))block {
- NSLog(@"Execute Query");
NSData* data=[query dataUsingEncoding:NSUTF8StringEncoding];
- [protocolImpl sendCommand:3 data:data continueWithBlock:^(){
+ [protocolImpl sendCommand:3 data:data continueWithBlock:^(int result){
NSData* initialServerResponsePacket= [protocolImpl readPacket];
UInt8* resultPacketData= (UInt8*)[initialServerResponsePacket bytes];
MySqlResults* results= [[MySqlResults alloc] init];
@@ -84,7 +99,8 @@ -(void) performQuery:(NSString*)query continueWithBlock:(void (^)(MySqlResults*
// sqlstate is chars 3-> 8
NSString* errorMessage= [NSString stringWithCString: (const char*)(resultPacketData+9) encoding:NSASCIIStringEncoding];
-
+ //TODO: this is confusing as it will display older packet's data as it doesn't check the termination boundary
+ //of the string..dumb...
NSLog(@"ERROR: %@ (%u)", errorMessage, errorNumber);
}
else {
@@ -163,21 +179,23 @@ -(void) performQuery:(NSString*)query continueWithBlock:(void (^)(MySqlResults*
}];
}
--(void) quit {
- NSLog(@"Quit");
+-(int) quit {
volatile __block bool blockCalled= false;
- [protocolImpl sendCommand:1 data:NULL continueWithBlock:^(){
+ volatile __block int quitResult= 0;
+ [protocolImpl sendCommand:1 data:NULL continueWithBlock:^(int result){
blockCalled= true;
+ quitResult= result;
}];
// As an attempt to simplify the external API, we
// simulate a blocking API by blocking the calling thread, until
// the command's callback block has executed.
while(!blockCalled) {
- [NSThread sleepForTimeInterval:0.01];
+ [NSThread sleepForTimeInterval:0.25];
}
[protocolImpl dealloc];
protocolImpl= NULL;
+ return quitResult;
}
@end
View
@@ -47,15 +47,15 @@ dispatch_queue_t queue;
-(NSData *) readPacket __attribute((ns_returns_retained));
--(void) sendPacket:(NSData*)packet;
--(void) sendUint32:(UInt32)value;
+-(int) sendPacket:(NSData*)packet;
+-(int) sendUint32:(UInt32)value;
-(NSNumber*) readLengthCodedLength:(UInt8**) byteData;
-(NSString*) readLengthCodedString:(UInt8**) byteData __attribute((ns_returns_retained));
-(bool) isEOFPacket:(NSData*)data;
--(void) sendCommand:(UInt8)command data:(NSData*)data continueWithBlock:(void (^)(void))block;
+-(void) sendCommand:(UInt8)command data:(NSData*)data continueWithBlock:(void (^)(int))block;
--(void) handshakeForUserName:(NSString*)user password:(NSString*)password;
+-(int) handshakeForUserName:(NSString*)user password:(NSString*)password;
@end
#endif
Oops, something went wrong.

0 comments on commit 3b14ea9

Please sign in to comment.