aboutsummaryrefslogtreecommitdiffstats
path: root/Frameworks/PostgresKit/Source/PGPostgresTypeStringHandler.m
diff options
context:
space:
mode:
authorstuconnolly <stuart02@gmail.com>2012-09-26 07:23:08 +0000
committerstuconnolly <stuart02@gmail.com>2012-09-26 07:23:08 +0000
commit88263061eb326e58fe882bc91d09f52d3d49628d (patch)
treef16453cd3bc0afc429b7592a46a84196e775933d /Frameworks/PostgresKit/Source/PGPostgresTypeStringHandler.m
parent9e0da1ad71ea178867a8d3419f80a46b42c32d4c (diff)
downloadsequelpro-88263061eb326e58fe882bc91d09f52d3d49628d.tar.gz
sequelpro-88263061eb326e58fe882bc91d09f52d3d49628d.tar.bz2
sequelpro-88263061eb326e58fe882bc91d09f52d3d49628d.zip
Change project prefix.
Diffstat (limited to 'Frameworks/PostgresKit/Source/PGPostgresTypeStringHandler.m')
-rw-r--r--Frameworks/PostgresKit/Source/PGPostgresTypeStringHandler.m163
1 files changed, 163 insertions, 0 deletions
diff --git a/Frameworks/PostgresKit/Source/PGPostgresTypeStringHandler.m b/Frameworks/PostgresKit/Source/PGPostgresTypeStringHandler.m
new file mode 100644
index 00000000..f638eb4e
--- /dev/null
+++ b/Frameworks/PostgresKit/Source/PGPostgresTypeStringHandler.m
@@ -0,0 +1,163 @@
+//
+// $Id: PGPostgresTypeStringHandler.m 3866 2012-09-26 01:30:28Z stuart02 $
+//
+// PGPostgresTypeStringHandler.m
+// PostgresKit
+//
+// Copyright (c) 2008-2009 David Thorpe, djt@mutablelogic.com
+//
+// Forked by the Sequel Pro Team on July 22, 2012.
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#import "PGPostgresTypeStringHandler.h"
+#import "PGPostgresConnection.h"
+
+#import <netdb.h>
+
+static PGPostgresOid PGPostgresTypeStringTypes[] =
+{
+ PGPostgresOidText,
+ PGPostgresOidChar,
+ PGPostgresOidName,
+ PGPostgresOidVarChar,
+ PGPostgresOidXML,
+ PGPostgresOidUUID,
+ PGPostgresOidBit,
+ PGPostgresOidVarBit,
+ PGPostgresOidInetAddr,
+ PGPostgresOidCidrAddr,
+ PGPostgresOidMacAddr,
+ PGPostgresOidUnknown,
+ 0
+};
+
+@interface PGPostgresTypeStringHandler ()
+
+- (id)_stringFromResult;
+- (id)_macAddressFromResult;
+- (id)_inetAddressFromResult;
+
+@end
+
+@implementation PGPostgresTypeStringHandler
+
+@synthesize row = _row;
+@synthesize type = _type;
+@synthesize column = _column;
+@synthesize result = _result;
+
+#pragma mark -
+#pragma mark Protocol Implementation
+
+- (PGPostgresOid *)remoteTypes
+{
+ return PGPostgresTypeStringTypes;
+}
+
+- (Class)nativeClass
+{
+ return [NSString class];
+}
+
+- (NSArray *)classAliases
+{
+ return [NSArray arrayWithObject:@"NSCFString"];
+}
+
+- (id)objectFromResult
+{
+ if (!_result || !_type) return [NSNull null];
+
+ switch (_type)
+ {
+ case PGPostgresOidText:
+ case PGPostgresOidChar:
+ case PGPostgresOidName:
+ case PGPostgresOidVarChar:
+ case PGPostgresOidXML:
+ case PGPostgresOidUUID:
+ case PGPostgresOidBit:
+ case PGPostgresOidVarBit:
+ case PGPostgresOidUnknown:
+ return [self _stringFromResult];
+ case PGPostgresOidMacAddr:
+ return [self _macAddressFromResult];
+ case PGPostgresOidInetAddr:
+ case PGPostgresOidCidrAddr:
+ return [self _inetAddressFromResult];
+ }
+
+ return [NSNull null];
+}
+
+#pragma mark -
+#pragma mark Private API
+
+/**
+ * Converts a char value to a string.
+ *
+ * @return A string representation of the value.
+ */
+- (id)_stringFromResult
+{
+ const void *bytes = PQgetvalue(_result, (int)_row, (int)_column);
+ NSUInteger length = PQgetlength(_result, (int)_row, (int)_column);
+
+ if (!bytes || !length) return [NSNull null];
+
+ return [[[NSString alloc] initWithBytes:bytes length:length encoding:[_connection stringEncoding]] autorelease];
+}
+
+/**
+ * Converts a MAC address value to a string.
+ *
+ * @return A string representation of the MAC address.
+ */
+- (id)_macAddressFromResult
+{
+ PGmacaddr address;
+
+ if (!PQgetf(_result, (int)_row, PGPostgresResultValueMacAddr, (int)_column, &address)) return [NSNull null];
+
+ return [NSString stringWithFormat:@"%02d:%02d:%02d:%02d:%02d:%02d", address.a, address.b, address.c, address.d, address.e, address.f];
+}
+
+/**
+ * Converts a network address value to a string.
+ *
+ * @return A string representation of the network address.
+ */
+- (id)_inetAddressFromResult
+{
+ PGinet inet;
+
+ if (!PQgetf(_result, (int)_row, _type == PGPostgresOidInetAddr ? PGPostgresResultValueInet : PGPostgresResultValueCidr, (int)_column, &inet)) return [NSNull null];
+
+ char ip[80];
+ struct sockaddr *sa = (struct sockaddr *)inet.sa_buf;
+
+ int success = getnameinfo(sa, inet.sa_buf_len, ip, sizeof(ip), NULL, 0, NI_NUMERICHOST);
+
+ if (success != 0) {
+ const char *error = gai_strerror(success);
+
+ NSLog(@"PostgresKit: Error: Failed to convert IP address to string representation (%s)", error);
+
+ return [NSNull null];
+ }
+
+ return [NSString stringWithUTF8String:ip];
+}
+
+@end