1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
|
/* -*-objc-*- */
/** Implementation of SQLClientOracle for GNUStep
Copyright (C) 2004 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <rfm@gnu.org>
Written by: Nicola Pero <nicola@brainsrtorm.co.uk>
Date: April 2004
This file is part of the SQLClient Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
$Date$ $Revision$
*/
#include <Foundation/NSString.h>
#include <Foundation/NSData.h>
#include <Foundation/NSDate.h>
#include <Foundation/NSCalendarDate.h>
#include <Foundation/NSException.h>
#include <Foundation/NSProcessInfo.h>
#include <Foundation/NSNotification.h>
#include <Foundation/NSUserDefaults.h>
#include <Foundation/NSMapTable.h>
#include <Foundation/NSLock.h>
#include "SQLClient.h"
/*
* Example configuration for an Oracle database:
*
* oracle-test = {
* ServerType = "Oracle"
* SQLDatabase = "nicola";
* SQLPassword = "mbrand";
* SQLUser = "mbrand";
* };
*
* Where SQLDatabase is the Unique database Identifier.
*
*/
@interface SQLClientOracle : SQLClient
@end
@interface SQLClientOracle(Embedded)
- (const char *) blobFromData: (NSData*)data;
- (NSData *) dataFromBlob: (const char *)blob;
- (BOOL) dbFromDate: (NSDate*)d toBuffer: (char*)b length: (int)l;
- (BOOL) dbFromString: (NSString*)s toBuffer: (char*)b length: (int)l;
- (NSDate*) dbToDateFromBuffer: (char*)b length: (int)l;
- (NSString*) dbToStringFromBuffer: (char*)b length: (int)l;
@end
EXEC SQL INCLUDE sqlca;
EXEC SQL WHENEVER SQLERROR DO SQLClientOracleErrorHandler();
/**
* Return YES of the last SQL error indicated we are out of data,
* NO otherwise.
*/
BOOL SQLClientOracleOutOfData()
{
if (sqlca.sqlcode == 100)
{
return YES;
}
else
{
return NO;
}
}
/**
* This error handler is called for most errors ... so we can get it to
* raise an exception for us.
*/
void SQLClientOracleErrorHandler()
{
int code = sqlca.sqlcode;
const char *ptr = sqlca.sqlerrm.sqlerrmc;
const char *e0 = "'no connection to the server'";
const char *e1 = "Error in transaction processing";
sqlca.sqlcode = 0; // Reset error code
NSLog (@"(Oracle) Raising an exception, %ld, %s",
code, sqlca.sqlerrm.sqlerrmc);
if (strncmp(ptr, e0, strlen(e0)) == 0
|| strncmp(ptr, e1, strlen(e1)) == 0)
{
[NSException raise: SQLConnectionException
format: @"(Oracle) SQL Error: SQLCODE=(%ld): %s", code, ptr];
}
else
{
[NSException raise: SQLException
format: @"(Oracle) SQL Error: SQLCODE=(%ld): %s", code, ptr];
}
}
@implementation SQLClientOracle
- (BOOL) backendConnect
{
if (connected == NO)
{
if ([self database] != nil
&& [self user] != nil
&& [self password] != nil)
{
Class c = NSClassFromString(@"CmdClient");
[[self class] purgeConnections: nil];
NS_DURING
{
EXEC SQL BEGIN DECLARE SECTION;
const char *database_c;
const char *user_c;
const char *password_c;
const char *client_c;
EXEC SQL END DECLARE SECTION;
/* Database is the Oracle Net identifier for the database. */
database_c = [[self database] UTF8String];
/* User and password are used to connect to the database. */
user_c = [[self user] UTF8String];
password_c = [[self password] UTF8String];
/* Client is only used to give this connection a name
* and distinguish it from other connections.
*/
client_c = [[self clientName] UTF8String];
if (c != 0)
{
[self debug: @"(Oracle) Connect to database %s user %s as %s",
database_c, user_c, client_c];
}
EXEC SQL CONNECT :user_c IDENTIFIED BY :password_c
AT :client_c USING :database_c;
if (c != 0)
{
[self debug: @"(Oracle) Connected (%s)", client_c];
}
connected = YES;
}
NS_HANDLER
{
[self error: @"(Oracle) Error connecting to database: %@",
localException];
}
NS_ENDHANDLER
}
else
{
[self error:
@"(Oracle) Connect with no user/password/database configured"];
}
}
return connected;
}
- (void) backendDisconnect
{
if (connected == YES)
{
NS_DURING
{
EXEC SQL BEGIN DECLARE SECTION;
const char *client_c;
EXEC SQL END DECLARE SECTION;
if ([self isInTransaction] == YES)
{
[self rollback];
}
client_c = [[self clientName] UTF8String];
[self debug: @"(Oracle) Disconnecting client %@", [self clientName]];
/* To disconnect from the database, we issuse a COMMIT
* statement with the RELEASE option. The RELEASE option
* causes it to disconnect after the COMMIT.
*/
EXEC SQL AT :client_c COMMIT WORK RELEASE;
[self debug: @"(Oracle) Disconnected client %@", [self clientName]];
}
NS_HANDLER
{
[self error: @"(Oracle) Error disconnecting from database (%@): %@",
[self clientName], localException];
}
NS_ENDHANDLER
connected = NO;
}
}
- (NSInteger) backendExecute: (NSArray*)info
{
EXEC SQL BEGIN DECLARE SECTION;
char *statement;
char *handle;
EXEC SQL END DECLARE SECTION;
CREATE_AUTORELEASE_POOL(arp);
NSString *stmt = [info objectAtIndex: 0];
unsigned int length;
BOOL manuallyAutoCommit = NO;
length = [stmt length];
if (length == 0)
{
[NSException raise: NSInternalInconsistencyException
format: @"(Oracle) Statement produced null string"];
}
statement = (char*)[stmt UTF8String];
handle = (char*)[[self clientName] UTF8String];
/*
* Ensure we have a working connection.
*/
if ([self connect] == NO)
{
[NSException raise: SQLException
format: @"(Oracle) Unable to connect to database"];
}
NS_DURING
{
if ([self isInTransaction] == NO)
{
manuallyAutoCommit = YES;
}
EXEC SQL AT :handle PREPARE command FROM :statement;
EXEC SQL AT :handle EXECUTE command;
if (manuallyAutoCommit)
{
EXEC SQL AT :handle COMMIT;
}
}
NS_HANDLER
{
NSString *n = [localException name];
NSString *msg = [localException reason];
if (manuallyAutoCommit)
{
EXEC SQL AT :handle ROLLBACK;
}
if ([n isEqual: SQLConnectionException] == YES)
{
[self disconnect];
}
/*
* remove line number information from database exception message
* since it's meaningless to the developer as it's the line number
* in this file rather than the code which is calling us.
*/
if ([n isEqual: SQLException] == YES
|| [n isEqual: SQLConnectionException] == YES)
{
NSRange r;
r = [msg rangeOfString: @" in line " options: NSBackwardsSearch];
if (r.length > 0)
{
msg = [msg substringToIndex: r.location];
localException = [NSException exceptionWithName: n
reason: msg
userInfo: nil];
}
}
[self error: @"(Oracle) Error executing statement:\n%@\n%@",
stmt, localException];
[localException raise];
}
NS_ENDHANDLER
DESTROY(arp);
return -1;
}
static unsigned int trim(char *str)
{
char *start = str;
while (isspace(*str))
{
str++;
}
if (str != start)
{
strcpy(start, str);
}
str = start;
while (*str != '\0')
{
str++;
}
while (str > start && isspace(str[-1]))
{
*--str = '\0';
}
return (str - start);
}
- (NSMutableArray*) backendQuery: (NSString*)stmt recordClass: (Class)rClass
{
EXEC SQL BEGIN DECLARE SECTION;
int count;
int index;
short int indicator;
int type;
int length;
int octetLength;
short int returnedOctetLength;
char fieldName[120];
char *aString;
/* This holds a string representation of numbers returned by Oracle.
* 128 seems a safe bound - else they'll be truncated. */
char aNumber[128];
char *query;
char *handle;
EXEC SQL END DECLARE SECTION;
CREATE_AUTORELEASE_POOL(arp);
NSMutableArray *records;
BOOL isOpen = NO;
BOOL wasInTransaction = [self isInTransaction];
BOOL allocatedDescriptor = NO;
length = [stmt length];
if (length == 0)
{
[NSException raise: NSInternalInconsistencyException
format: @"(Oracle) Statement produced null string"];
}
handle = (char*)[[self clientName] UTF8String];
query = (char*)[stmt UTF8String];
records = [[NSMutableArray alloc] initWithCapacity: 32];
/*
* Ensure we have a working connection.
*/
if ([self connect] == NO)
{
[NSException raise: SQLException
format: @"(Oracle) Unable to connect to database"];
}
NS_DURING
{
/* This is really the output descriptor. We do not use input
* descriptors; all the input is in the SQL statement.
*/
EXEC SQL ALLOCATE DESCRIPTOR 'myDesc';
allocatedDescriptor = YES;
EXEC SQL AT :handle PREPARE myQuery from :query;
if ([self isInTransaction] == NO)
{
/* EXEC SQL AT :handle BEGIN; */
_inTransaction = YES;
}
EXEC SQL AT :handle DECLARE myCursor CURSOR FOR myQuery;
EXEC SQL AT :handle OPEN myCursor;
isOpen = YES;
EXEC SQL AT :handle DESCRIBE OUTPUT myQuery USING DESCRIPTOR 'myDesc';
EXEC SQL GET DESCRIPTOR 'myDesc' :count = COUNT;
if (count > 0)
{
/* Now we do what the Oracle examples do, which is we forcefully
* require to the library to convert everything into types
* chosen by us (mostly strings). The reason we do it is that
* managing the 'internal' Oracle datatypes is a daunting task
* (for example numbers are returned in a 22 byte representation
* used internally by Oracle ...) and apparently it's now how
* they expect you to use it - they provide no examples or
* explanations of how to do it btw! They expect you to choose
* which 'external' Oracle representation you want, by using SET
* DESCRIPTOR as we do here, and then FETCH comfortably data
* which is returned in the representation you chose. So we do
* that way.
*/
int originalType[count];
for (index = 1; index <= count; index++)
{
EXEC SQL GET DESCRIPTOR 'myDesc' VALUE :index
:length = LENGTH,
:octetLength = OCTET_LENGTH,
:type = TYPE;
/* Save the original type so that we know later what's
* inside each returned value. */
originalType[index - 1] = type;
switch (type)
{
/* Negative values of 'type' are used for Oracle
* proprietary extensions; positive values for ANSI
* types. */
/* We get character types as they are. */
case 1 /* CHARACTER */:
case 12 /* CHARACTER_VARYING */:
case -1 /* Oracle VARCHAR2 */:
type = -1; /* Oracle VARCHAR2 */
EXEC SQL SET DESCRIPTOR 'myDesc' VALUE :index
TYPE = :type;
break;
/* We get a string representation (128 bytes long)
* of any number. */
case 2 /* NUMERIC */:
case 3 /* DECIMAL */:
case 4 /* INTEGER */:
case 5 /* SMALLINT*/:
case 6 /* FLOAT */:
case 7 /* REAL */:
case 8 /* DOUBLE_PRECISION */:
type = 12; /* ANSI CHARACTER_VARYING */
octetLength = 128;
EXEC SQL SET DESCRIPTOR 'myDesc' VALUE :index
LENGTH = :octetLength,
TYPE = :type;
break;
}
}
while (1)
{
SQLRecord *record;
id keys[count];
id values[count];
EXEC SQL AT :handle FETCH myCursor INTO SQL DESCRIPTOR 'myDesc';
if (sqlca.sqlcode)
{
break;
}
for (index = 1; index <= count; ++index)
{
id v;
EXEC SQL GET DESCRIPTOR 'myDesc' VALUE :index
:indicator = INDICATOR,
:length = LENGTH,
:fieldName = NAME,
:octetLength = OCTET_LENGTH,
:returnedOctetLength = RETURNED_OCTET_LENGTH,
:type = TYPE;
if (indicator == -1)
{
v = [NSNull null];
}
else
{
switch (originalType[index - 1])
{
case 3 /* DECIMAL */:
case 4 /* INTEGER */:
case 5 /* SMALLINT*/:
{
int aInt;
EXEC SQL GET DESCRIPTOR 'myDesc' VALUE :index
:aNumber = DATA;
aInt = [[NSString stringWithUTF8String: aNumber] intValue];
v = [NSNumber numberWithInt: aInt];
break;
}
case 2 /* NUMERIC */:
case 6 /* FLOAT */:
case 7 /* REAL */:
case 8 /* DOUBLE_PRECISION */:
{
float aFloat;
EXEC SQL GET DESCRIPTOR 'myDesc' VALUE :index
:aNumber = DATA;
aFloat = [[NSString stringWithUTF8String: aNumber] floatValue];
v = [NSNumber numberWithFloat: aFloat];
break;
}
case 1 /* CHARACTER */:
case 12 /* CHARACTER_VARYING */:
case -1 /* Oracle VARCHAR2 */:
/* For unclear reasons, returnedOctetLength is always 0. */
/* This code (patchy and experimentally
* determined) really works if the database
* field contains something like UTF-8,
* returned as UTF-8 (such as for CHAR(20)
* fields). If UNICODE stuff is returned,
* then it's not the right way. We might
* need to make a different depending on the
* originalField type.
*/
/* Add 1 byte to \0-pad the string. */
aString = malloc (octetLength + 1);
if (aString == NULL)
{
[NSException
raise: @"OutOfMemoryException"
format: @"(Oracle) could not malloc %d bytes",
octetLength];
}
EXEC SQL GET DESCRIPTOR 'myDesc' VALUE :index
:aString = DATA;
/* \0-pad the string. */
aString[octetLength] = '\0';
if (YES == _shouldTrim)
{
trim (aString);
}
v = [NSString stringWithUTF8String: aString];
free(aString);
break;
/* TODO: DATES */
/*
TODO TODO
case BLOB:
EXEC SQL GET DESCRIPTOR 'myDesc' VALUE :index
:aString = DATA;
v = [self dataFromBlob: aString];
free(aString);
break;
*/
default:
aString = malloc (octetLength + 1);
if (aString == NULL)
{
[NSException
raise: @"OutOfMemoryException"
format: @"(Oracle) could not malloc %d bytes",
octetLength];
}
EXEC SQL GET DESCRIPTOR 'myDesc' VALUE :index
:aString = DATA;
aString[octetLength] = '\0';
if (YES == _shouldTrim)
{
trim (aString);
}
v = [NSString stringWithUTF8String: aString];
free (aString);
NSLog(@"(Oracle) Unknown data type (%d) for '%s': '%@'",
type, fieldName, v);
break;
}
}
values[index - 1] = v;
keys[index - 1] = [NSString stringWithUTF8String:
fieldName];
}
record = [rClass newWithValues: values
keys: keys
count: count];
[records addObject: record];
RELEASE(record);
}
}
isOpen = NO;
EXEC SQL AT :handle CLOSE myCursor;
if (wasInTransaction == NO && [self isInTransaction] == YES)
{
EXEC SQL AT :handle COMMIT;
_inTransaction = NO;
}
EXEC SQL DEALLOCATE DESCRIPTOR 'myDesc';
allocatedDescriptor = NO;
}
NS_HANDLER
{
NSString *n = [localException name];
NSString *msg = [localException reason];
DESTROY(records);
NS_DURING
{
if (isOpen == YES)
{
EXEC SQL AT :handle CLOSE myCursor;
}
if (wasInTransaction == NO && [self isInTransaction] == YES)
{
EXEC SQL AT :handle ROLLBACK;
_inTransaction = NO;
}
}
NS_HANDLER
{
NSString *e = [localException name];
if (wasInTransaction == NO && [self isInTransaction] == YES)
{
_inTransaction = NO;
}
if ([e isEqual: SQLConnectionException] == YES)
{
[self disconnect];
}
}
NS_ENDHANDLER
NS_DURING
{
if (allocatedDescriptor)
{
EXEC SQL DEALLOCATE DESCRIPTOR 'myDesc';
allocatedDescriptor = NO;
}
}
NS_HANDLER
{
NSLog (@"Can't deallocate descriptor ... serious problem.");
}
NS_ENDHANDLER
if ([n isEqual: SQLConnectionException] == YES)
{
_inTransaction = NO;
[self disconnect];
}
/*
* remove line number information from database exception message
* since it's meaningless to the developer as it's the line number
* in this file rather than the code which is calling us.
*/
if ([n isEqual: SQLException] == YES
|| [n isEqual: SQLConnectionException] == YES)
{
NSRange r;
r = [msg rangeOfString: @" in line " options: NSBackwardsSearch];
if (r.length > 0)
{
msg = [msg substringToIndex: r.location];
localException = [NSException exceptionWithName: n
reason: msg
userInfo: nil];
}
}
RETAIN(localException);
RELEASE(arp);
AUTORELEASE(localException);
[localException raise];
}
NS_ENDHANDLER
DESTROY(arp);
return AUTORELEASE(records);
}
/**
* Convert NSData object with raw binary data into escaped sequence
*/
- (const char *) blobFromData: (NSData*)data
{
NSMutableData *md;
unsigned sLen = [data length];
unsigned char *src = (unsigned char*)[data bytes];
unsigned dLen = 0;
unsigned char *dst;
unsigned i;
for (i = 0; i < sLen; i++)
{
unsigned char c = src[i];
if (c < 32 || c > 126)
{
dLen += 4;
}
else if (c == 92)
{
dLen += 2;
}
else
{
dLen += 1;
}
}
md = [NSMutableData dataWithLength: dLen + 1];
dst = (unsigned char*)[md mutableBytes];
dLen = 0;
for (i = 0; i < sLen; i++)
{
unsigned char c = src[i];
if (c < 32 || c > 126)
{
dst[dLen] = '\\';
dst[dLen + 3] = (c & 7) + '0';
c >>= 3;
dst[dLen + 2] = (c & 7) + '0';
c >>= 3;
dst[dLen + 1] = (c & 7) + '0';
dLen += 4;
}
else if (c == 92)
{
dst[dLen++] = '\\';
dst[dLen++] = '\\';
}
else
{
dst[dLen++] = c;
}
}
dst[dLen] = '\0';
return dst; // Owned by autoreleased NSMutableData
}
/**
* Convert escaped sequence to raw binary data in NSData object
*/
- (NSData *) dataFromBlob: (const char *)blob
{
NSMutableData *md;
unsigned sLen = strlen(blob == 0 ? "" : blob);
unsigned dLen = 0;
unsigned char *dst;
unsigned i;
for (i = 0; i < sLen; i++)
{
unsigned c = blob[i];
dLen++;
if (c == '\\')
{
c = blob[++i];
if (c != '\\')
{
i += 2; // Skip 2 digits octal
}
}
}
md = [NSMutableData dataWithLength: dLen];
dst = (unsigned char*)[md mutableBytes];
dLen = 0;
for (i = 0; i < sLen; i++)
{
unsigned c = blob[i];
if (c == '\\')
{
c = blob[++i];
if (c != '\\')
{
c = c - '0';
c <<= 3;
c += blob[++i] - '0';
c <<= 3;
c += blob[++i] - '0';
}
}
dst[dLen++] = c;
}
return md;
}
/**
* Convert an NSdate into a buffer for sending to the database.
* Return YES if the conversion fitted, NO if it was truncated.
* The value of l is expected to be one less than the size of the buffer.
* A nul character is appended to the bytes in the buffer.
*/
- (BOOL) dbFromDate: (NSDate*)d toBuffer: (char*)b length: (int)l
{
NSString *s;
s = [d descriptionWithCalendarFormat: @"%Y-%m-%d %H:%M:%S %z"
timeZone: nil
locale: nil];
return [self dbFromString: s toBuffer: b length: l];
}
/**
* Convert an NSString into a buffer for sending to the database.<br />
* Return YES if the conversion fitted, NO if it was truncated.<br />
* If s is nil, it is treated as an empty string.<br />
* The value of l is expected to be one less than the size of the buffer
* and must be at least 1.<br />
* The pointer b must not be null.<br />
* A nul character is appended to the bytes in the buffer.<br />
* Raises an exception when passed invalid arguments.
*/
- (BOOL) dbFromString: (NSString*)s toBuffer: (char*)b length: (int)l
{
NSData *d;
BOOL ok = YES;
unsigned size = l;
if (l <= 0)
{
[NSException raise: NSInvalidArgumentException
format: @"(Oracle) -%@: length too small (%d)",
NSStringFromSelector(_cmd), l];
}
if (b == 0)
{
[NSException raise: NSInvalidArgumentException
format: @"(Oracle) -%@: buffer is null",
NSStringFromSelector(_cmd)];
}
if (s == nil)
{
s = @"";
}
d = [s dataUsingEncoding: NSUTF8StringEncoding];
if (l < (int)[d length])
{
/*
* As the data is UTF8, we need to avoid truncating in the
* middle of a multibyte character, so we shorten the
* original string and reconvert to UTF8 until we find a
* string that fits.
*/
if ((int)[s length] > l)
{
s = [s substringToIndex: l];
d = [s dataUsingEncoding: NSUTF8StringEncoding];
}
while ((int)[d length] > l)
{
s = [s substringToIndex: [s length] - 1];
d = [s dataUsingEncoding: NSUTF8StringEncoding];
}
ok = NO;
}
size = [d length];
memcpy(b, (const char*)[d bytes], size);
/*
* Pad with nuls and ensure there is a nul terminator.
*/
while ((int)size <= l)
{
b[size++] = '\0';
}
return ok;
}
- (NSDate*) dbToDateFromBuffer: (char*)b length: (int)l
{
char buf[l+32]; /* Allow space to expand buffer. */
NSCalendarDate *d;
BOOL milliseconds = NO;
BOOL timezone = NO;
NSString *s;
int i;
int e;
memcpy(buf, b, l);
b = buf;
/*
* Find end of string.
*/
for (i = 0; i < l; i++)
{
if (b[i] == '\0')
{
l = i;
break;
}
}
while (l > 0 && isspace(b[l-1]))
{
l--;
}
b[l] = '\0';
if (l == 10)
{
s = [NSString stringWithUTF8String: b];
return [NSCalendarDate dateWithString: s
calendarFormat: @"%Y-%m-%d"
locale: nil];
}
i = l;
/* Convert +/-HH:SS timezone to +/-HHSS
*/
if (i > 5 && b[i-3] == ':' && (b[i-6] == '+' || b[i-6] == '-'))
{
b[i-3] = b[i-2];
b[i-2] = b[i-1];
b[--i] = '\0';
}
while (i-- > 0)
{
if (b[i] == '+' || b[i] == '-')
{
break;
}
if (b[i] == ':' || b[i] == ' ')
{
i = 0;
break; /* No time zone found */
}
}
if (i == 0)
{
e = l;
}
else
{
timezone = YES;
e = i;
if (isdigit(b[i-1]))
{
/*
* Make space between seconds and timezone.
*/
memmove(&b[i+1], &b[i], l - i);
b[i++] = ' ';
b[++l] = '\0';
}
/*
* Ensure we have a four digit timezone value.
*/
if (isdigit(b[i+1]) && isdigit(b[i+2]))
{
if (b[i+3] == '\0')
{
// Two digit time zone ... append zero minutes
b[l++] = '0';
b[l++] = '0';
b[l] = '\0';
}
else if (b[i+3] == ':')
{
// Zone with colon before minutes ... remove it
b[i+3] = b[i+4];
b[i+4] = b[i+5];
b[--l] = '\0';
}
}
}
/* kludge for timestamps with fractional second information.
* Force it to 3 digit millisecond */
while (i-- > 0)
{
if (b[i] == '.')
{
milliseconds = YES;
i++;
if (!isdigit(b[i]))
{
memmove(&b[i+3], &b[i], e-i);
l += 3;
memcpy(&b[i], "000", 3);
}
i++;
if (!isdigit(b[i]))
{
memmove(&b[i+2], &b[i], e-i);
l += 2;
memcpy(&b[i], "00", 2);
}
i++;
if (!isdigit(b[i]))
{
memmove(&b[i+1], &b[i], e-i);
l += 1;
memcpy(&b[i], "0", 1);
}
i++;
break;
}
}
if (i > 0 && i < e)
{
memmove(&b[i], &b[e], l - e);
l -= (e - i);
}
b[l] = '\0';
if (l == 0)
{
return nil;
}
s = [NSString stringWithUTF8String: b];
if (YES == timezone)
{
if (milliseconds == YES)
{
d = [NSCalendarDate dateWithString: s
calendarFormat: @"%Y-%m-%d %H:%M:%S.%F %z"
locale: nil];
}
else
{
d = [NSCalendarDate dateWithString: s
calendarFormat: @"%Y-%m-%d %H:%M:%S %z"
locale: nil];
}
}
else
{
if (milliseconds == YES)
{
d = [NSCalendarDate dateWithString: s
calendarFormat: @"%Y-%m-%d %H:%M:%S.%F"
locale: nil];
}
else
{
d = [NSCalendarDate dateWithString: s
calendarFormat: @"%Y-%m-%d %H:%M:%S"
locale: nil];
}
}
[d setCalendarFormat: @"%Y-%m-%d %H:%M:%S %z"];
return d;
}
/**
* Convert from a database character buffer to an NSString.
*/
- (NSString*) dbToStringFromBuffer: (char*)b length: (int)l
{
NSData *d;
NSString *s;
/*
* Database fields are padded to the full field size with spaces or nuls ...
* we need to remove that padding before placing in a string.
*/
while (l > 0 && b[l-1] <= ' ')
{
l--;
}
d = [[NSData alloc] initWithBytes: b length: l];
s = [[NSString alloc] initWithData: d encoding: NSUTF8StringEncoding];
RELEASE(d);
return AUTORELEASE(s);
}
@end
|