]> err.no Git - sope/blob - sope-gdl1/GDLContentStore/GCSFieldInfo.m
properly use system-lib-dir
[sope] / sope-gdl1 / GDLContentStore / GCSFieldInfo.m
1 /*
2   Copyright (C) 2004-2005 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "GCSFieldInfo.h"
23 #include "common.h"
24
25 @implementation GCSFieldInfo
26
27 + (NSArray *)fieldsForPropertyList:(NSArray *)_plist {
28   NSMutableArray *fields;
29   unsigned i, count;
30   
31   if (_plist == nil)
32     return nil;
33   
34   count = [_plist count];
35   fields = [NSMutableArray arrayWithCapacity:count];
36   for (i = 0; i < count; i++) {
37     GCSFieldInfo *field;
38
39     field = [[GCSFieldInfo alloc] initWithPropertyList:
40                                     [_plist objectAtIndex:i]];
41     if (field != nil) [fields addObject:field];
42     [field release];
43   }
44   return fields;
45 }
46
47 - (id)initWithPropertyList:(id)_plist {
48   if ((self = [super init])) {
49     self->columnName = [[_plist objectForKey:@"columnName"] copy];
50     self->sqlType    = [[_plist objectForKey:@"sqlType"]    copy];
51     
52     self->allowsNull   = [[_plist objectForKey:@"allowsNull"]   boolValue];
53     self->isPrimaryKey = [[_plist objectForKey:@"isPrimaryKey"] boolValue];
54     
55     if (![self->columnName isNotNull] || ![self->sqlType isNotNull]) {
56       [self release];
57       return nil;
58     }
59   }
60   return self;
61 }
62
63 - (void)dealloc {
64   [self->columnName release];
65   [self->sqlType    release];
66   [super dealloc];
67 }
68
69 /* accessors */
70
71 - (NSString *)columnName {
72   return self->columnName;
73 }
74 - (NSString *)sqlType {
75   return self->sqlType;
76 }
77
78 - (BOOL)doesAllowNull {
79   return self->allowsNull;
80 }
81 - (BOOL)isPrimaryKey {
82   return self->isPrimaryKey;
83 }
84
85 /* generating SQL */
86
87 - (NSString *)sqlCreateSection {
88   NSMutableString *ms;
89   
90   ms = [NSMutableString stringWithCapacity:32];
91   [ms appendString:[self columnName]];
92   [ms appendString:@" "];
93   [ms appendString:[self sqlType]];
94   
95   [ms appendString:@" "];
96   if (![self doesAllowNull]) [ms appendString:@"NOT "];
97   [ms appendString:@"NULL"];
98   
99   if ([self isPrimaryKey]) [ms appendString:@" PRIMARY KEY"];
100   return ms;
101 }
102
103 /* description */
104
105 - (void)appendAttributesToDescription:(NSMutableString *)ms {
106   id tmp;
107   
108   if ((tmp = [self columnName]) != nil) [ms appendFormat:@" column=%@", tmp];
109   if ((tmp = [self sqlType])    != nil) [ms appendFormat:@" sql=%@",    tmp];
110   
111   if ([self doesAllowNull]) [ms appendString:@" allows-null"];
112   if ([self isPrimaryKey])  [ms appendString:@" pkey"];
113 }
114
115 - (NSString *)description {
116   NSMutableString *ms;
117   
118   ms = [NSMutableString stringWithCapacity:256];
119   [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
120   [self appendAttributesToDescription:ms];
121   [ms appendString:@">"];
122   return ms;
123 }
124
125 @end /* GCSFieldInfo */