Skip to content

Commit cf7beff

Browse files
committed
Solved Issue martinrybak#17: Fetching image data
1 parent 3dc87ea commit cf7beff

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

SQLClient/SQLClient/SQLClient/SQLClient.m

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,20 @@ - (void)execute:(NSString*)sql completion:(void (^)(NSArray* results))completion
191191
//Get column metadata
192192
pcol->name = dbcolname(connection, c);
193193
pcol->type = dbcoltype(connection, c);
194-
pcol->size = dbcollen(connection, c);
194+
195+
//For IMAGE data, we need to multiply by 2, because dbbind() will convert each byte to a hexadecimal pair.
196+
//http://www.freetds.org/userguide/samplecode.htm#SAMPLECODE.RESULTS
197+
if(pcol->type == SYBIMAGE){
198+
pcol->size = dbcollen(connection, c) * 2;
199+
}else{
200+
pcol->size = dbcollen(connection, c);
201+
}
202+
195203

196204
//If the column is [VAR]CHAR or TEXT, we want the column's defined size, otherwise we want
197205
//its maximum size when represented as a string, which FreeTDS's dbwillconvert()
198-
//returns (for fixed-length datatypes).
199-
if (pcol->type != SYBCHAR && pcol->type != SYBTEXT)
206+
//returns (for fixed-length datatypes). We also do not need to convert IMAGE data type
207+
if (pcol->type != SYBCHAR && pcol->type != SYBTEXT && pcol->type != SYBIMAGE)
200208
pcol->size = dbwillconvert(pcol->type, SYBCHAR);
201209

202210
//Allocate memory in the current pcol struct for a buffer
@@ -237,7 +245,24 @@ - (void)execute:(NSString*)sql completion:(void (^)(NSArray* results))completion
237245
id value;
238246
if (pcol->status == -1) { //null value
239247
value = [NSNull null];
240-
} else {
248+
249+
//Converting hexadecimal buffer into UIImage
250+
}else if (pcol ->type == SYBIMAGE){
251+
NSString *hexString = [[NSString stringWithUTF8String:pcol->buffer] stringByReplacingOccurrencesOfString:@" " withString:@""];
252+
NSMutableData *hexData = [[NSMutableData alloc] init];
253+
254+
//Converting hex string to NSData
255+
unsigned char whole_byte;
256+
char byte_chars[3] = {'\0','\0','\0'};
257+
int i;
258+
for (i=0; i < [hexString length]/2; i++) {
259+
byte_chars[0] = [hexString characterAtIndex:i*2];
260+
byte_chars[1] = [hexString characterAtIndex:i*2+1];
261+
whole_byte = strtol(byte_chars, NULL, 16);
262+
[hexData appendBytes:&whole_byte length:1];
263+
}
264+
value = [UIImage imageWithData:hexData];
265+
}else {
241266
value = [NSString stringWithUTF8String:pcol->buffer];
242267
}
243268
//id value = [NSString stringWithUTF8String:pcol->buffer] ?: [NSNull null];

0 commit comments

Comments
 (0)