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
|
/* SOGoSQLUserProfile.m - this file is part of SOGo
*
* Copyright (C) 2009-2017 Inverse inc.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This file 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#import <NGExtensions/NSObject+Logs.h>
#import <NGExtensions/NSNull+misc.h>
#import <GDLContentStore/GCSChannelManager.h>
#import <GDLContentStore/NSURL+GCS.h>
#import <GDLAccess/EOAdaptor.h>
#import <GDLAccess/EOAdaptorChannel.h>
#import <GDLAccess/EOAdaptorContext.h>
#import <GDLAccess/EOAttribute.h>
#import <SOGo/SOGoCache.h>
#import "SOGoSystemDefaults.h"
#import "SOGoSQLUserProfile.h"
static NSURL *tableURL = nil;
static NSString *uidColumnName = @"c_uid";
static EOAttribute *textColumn = nil;
static const NSString *kCDefaultsLenKey = @"kCDefaultsLenKey";
@implementation SOGoSQLUserProfile
+ (void) initialize
{
NSDictionary *description;
NSString *profileURL;
SOGoSystemDefaults *sd;
if (!tableURL)
{
sd = [SOGoSystemDefaults sharedSystemDefaults];
profileURL = [sd profileURL];
if (profileURL)
tableURL = [[NSURL alloc] initWithString: profileURL];
}
if (!textColumn)
{
#warning This is a hack for providing an EOAttribute definition \
that is compatible with all the backends that we support
/* TODO: ... We should make use of EOModel for the profile tables */
description = [NSDictionary dictionaryWithObjectsAndKeys:
@"c_textfield", @"columnName",
@"VARCHAR", @"externalType",
nil];
textColumn = [EOAttribute attributeFromPropertyList: description];
[textColumn retain];
}
}
- (id) init
{
if ((self = [super init]))
{
fieldName = nil;
}
return self;
}
- (void) dealloc
{
[fieldName release];
[super dealloc];
}
- (void) setProfileType: (SOGoUserProfileType) newProfileType
{
if (newProfileType == SOGoUserProfileTypeDefaults)
ASSIGN (fieldName, @"c_defaults");
else if (newProfileType == SOGoUserProfileTypeSettings)
ASSIGN (fieldName, @"c_settings");
else
[self errorWithFormat: @"unknown profile value: %d", newProfileType];
[super setProfileType: newProfileType];
}
- (NSString *) fetchJSONProfileFromDB
{
GCSChannelManager *cm;
EOAdaptorChannel *channel;
NSDictionary *row;
NSException *ex;
NSString *sql, *result;
id value;
NSArray *attrs;
value = nil;
result = nil;
cm = [GCSChannelManager defaultChannelManager];
channel = [cm acquireOpenChannelForURL: tableURL];
if (channel)
{
/* generate SQL */
defFlags.ready = YES;
sql = [NSString stringWithFormat: @"SELECT %@ FROM %@ WHERE %@ = '%@'",
fieldName, [tableURL gcsTableName],
uidColumnName, [self uid]];
/* run SQL */
ex = [channel evaluateExpressionX: sql];
if (ex)
[self errorWithFormat:@"could not run SQL '%@': %@", sql, ex];
else
{
/* fetch schema */
attrs = [channel describeResults: NO /* don't beautify */];
/* fetch values */
row = [channel fetchAttributes: attrs withZone: NULL];
[channel cancelFetch];
/* the isNew flag depends on the presence of the row in the
database rather than on the existence of the value. */
defFlags.isNew = (row == nil);
value = [row objectForKey: fieldName];
if (![value isNotNull])
value = nil; /* we discard any NSNull instance */
if (value && [value isKindOfClass: [NSData class]]) {
result = [NSString stringWithUTF8String: [value bytes]];
} else if (value && [value isKindOfClass: [NSString class]]) {
result = value;
}
}
[cm releaseChannel: channel];
}
else
{
defFlags.ready = NO;
[self errorWithFormat:@"failed to acquire channel for URL: %@",
tableURL];
}
return result;
}
- (NSString *) generateSQLForInsert: (NSString *) jsonRepresentation
{
NSString *sql;
if ([jsonRepresentation length])
sql = [NSString stringWithFormat: (@"INSERT INTO %@"
@" (%@, %@)"
@" VALUES ('%@', %@)"),
[tableURL gcsTableName], uidColumnName, fieldName,
[self uid],
jsonRepresentation];
else
sql = nil;
return sql;
}
- (NSString *) generateSQLForUpdate: (NSString *) jsonRepresentation
{
NSString *sql;
if ([jsonRepresentation length])
sql = [NSString stringWithFormat: (@"UPDATE %@"
@" SET %@ = %@"
@" WHERE %@ = '%@'"),
[tableURL gcsTableName],
fieldName,
jsonRepresentation,
uidColumnName, [self uid]];
else
sql = nil;
return sql;
}
- (BOOL) storeJSONProfileInDB: (NSString *) jsonRepresentation
{
GCSChannelManager *cm;
EOAdaptorChannel *channel;
EOAdaptorContext *context;
NSException *ex;
NSString *sql, *formattedValue;
BOOL rc;
rc = NO;
cm = [GCSChannelManager defaultChannelManager];
channel = [cm acquireOpenChannelForURL: tableURL];
if (channel)
{
context = [channel adaptorContext];
if ([context beginTransaction])
{
formattedValue = [[context adaptor] formatValue: jsonRepresentation
forAttribute: textColumn];
sql = ((defFlags.isNew)
? [self generateSQLForInsert: formattedValue]
: [self generateSQLForUpdate: formattedValue]);
defFlags.ready = YES;
ex = [channel evaluateExpressionX:sql];
if (ex)
{
[self errorWithFormat: @"could not run SQL '%@': %@", sql, ex];
[context rollbackTransaction];
}
else
{
rc = YES;
defFlags.modified = NO;
defFlags.isNew = NO;
[context commitTransaction];
}
[cm releaseChannel: channel];
}
else
{
defFlags.ready = NO;
[cm releaseChannel: channel immediately: YES];
}
}
else
{
defFlags.ready = NO;
[self errorWithFormat: @"failed to acquire channel for URL: %@",
tableURL];
}
return rc;
}
- (unsigned long long) getCDefaultsSize {
unsigned long long r;
NSString *sql;
NSException *ex;
GCSChannelManager *cm;
EOAdaptorChannel *channel;
EOAdaptorContext *context;
NSArray *attrs;
NSDictionary *infos;
SOGoCache *cache;
NSNumberFormatter *formatter;
r = 65535;
cache = [SOGoCache sharedCache];
if ([cache valueForKey:kCDefaultsLenKey]) {
formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
r = [[formatter numberFromString: [cache valueForKey:kCDefaultsLenKey]] unsignedLongLongValue];
[formatter release];
} else {
cm = [GCSChannelManager defaultChannelManager];
channel = [cm acquireOpenChannelForURL: tableURL];
sql = [NSString stringWithFormat: @"select character_octet_length as CHARACTER_MAXIMUM_LENGTH from information_schema.columns where table_name = '%@' AND column_name = 'c_defaults'", [tableURL gcsTableName]];
ex = [channel evaluateExpressionX: sql];
if (!ex) {
attrs = [channel describeResults: NO];
infos = [channel fetchAttributes: attrs withZone: NULL];
[cm releaseChannel: channel immediately: YES];
if (infos && [infos objectForKey:@"CHARACTER_MAXIMUM_LENGTH"]) {
r = [[infos objectForKey:@"CHARACTER_MAXIMUM_LENGTH"] longLongValue];
} else if (infos && [infos objectForKey:@"character_maximum_length"]) { // PGSQL case
r = [[infos objectForKey:@"character_maximum_length"] longLongValue];
}
}
[cache setValue: [[NSNumber numberWithUnsignedLongLong: r] stringValue] forKey: kCDefaultsLenKey];
}
return r;
}
@end
|