]> err.no Git - sope/blob - sope-gdl1/GDLContentStore/GCSFieldInfo.m
fixed for OGo bug #1906
[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     NSDictionary *plist = _plist;
50     
51     self->columnName = [[plist objectForKey:@"columnName"] copy];
52     self->sqlType    = [[plist objectForKey:@"sqlType"]    copy];
53     
54     self->allowsNull   = [[plist objectForKey:@"allowsNull"]   boolValue];
55     self->isPrimaryKey = [[plist objectForKey:@"isPrimaryKey"] boolValue];
56     
57     if (![self->columnName isNotNull] || ![self->sqlType isNotNull]) {
58       [self release];
59       return nil;
60     }
61   }
62   return self;
63 }
64
65 - (void)dealloc {
66   [self->columnName release];
67   [self->sqlType    release];
68   [super dealloc];
69 }
70
71 /* accessors */
72
73 - (NSString *)columnName {
74   return self->columnName;
75 }
76 - (NSString *)sqlType {
77   return self->sqlType;
78 }
79
80 - (BOOL)doesAllowNull {
81   return self->allowsNull;
82 }
83 - (BOOL)isPrimaryKey {
84   return self->isPrimaryKey;
85 }
86
87 /* generating SQL */
88
89 - (NSString *)sqlCreateSection {
90   NSMutableString *ms;
91   
92   ms = [NSMutableString stringWithCapacity:32];
93   [ms appendString:[self columnName]];
94   [ms appendString:@" "];
95   [ms appendString:[self sqlType]];
96   
97   [ms appendString:@" "];
98   if (![self doesAllowNull]) [ms appendString:@"NOT "];
99   [ms appendString:@"NULL"];
100   
101   if ([self isPrimaryKey]) [ms appendString:@" PRIMARY KEY"];
102   return ms;
103 }
104
105 /* description */
106
107 - (void)appendAttributesToDescription:(NSMutableString *)ms {
108   id tmp;
109   
110   if ((tmp = [self columnName]) != nil) [ms appendFormat:@" column=%@", tmp];
111   if ((tmp = [self sqlType])    != nil) [ms appendFormat:@" sql=%@",    tmp];
112   
113   if ([self doesAllowNull]) [ms appendString:@" allows-null"];
114   if ([self isPrimaryKey])  [ms appendString:@" pkey"];
115 }
116
117 - (NSString *)description {
118   NSMutableString *ms;
119   
120   ms = [NSMutableString stringWithCapacity:256];
121   [ms appendFormat:@"<0x%p[%@]:", self, NSStringFromClass([self class])];
122   [self appendAttributesToDescription:ms];
123   [ms appendString:@">"];
124   return ms;
125 }
126
127 @end /* GCSFieldInfo */