/*
        Copyright (c) 1996, NeXT Software, Inc.
        All rights reserved.

        You may freely copy, distribute and reuse the code in this example.
        NeXT disclaims any warranty of any kind, expressed or implied,
        as to its fitness for any particular use.
*/

#import "FlatFileAdaptor.h"
#import "FlatFileSQLExpression.h"

@interface FlatFileSQLExpression (FlatFileSQLExpressionPrivate)

+ (NSString *)newHeaderForAttribute:(EOAttribute *)attribute;

@end

@implementation FlatFileSQLExpression

+ (NSArray *)createTableStatementsForEntityGroup:(NSArray *)entityGroup {
    NSEnumerator *attributeEnumerator, *entityEnumerator = [entityGroup objectEnumerator];
    NSMutableSet *columnNames = [NSMutableSet set];
    NSMutableString *listString = nil;
    NSString *columnName, *tableName = nil;
    EOEntity *entity;
    EOAttribute *attribute;
    FlatFileSQLExpression *expression = nil;
    
    while (entity = [entityEnumerator nextObject]) {
        attributeEnumerator = [[entity attributes] objectEnumerator];
        while (attribute = [attributeEnumerator nextObject]) {
            columnName = [attribute columnName];
            if (columnName) {
                if (![columnNames containsObject:columnName]) {
                    if (!expression) {
                        expression = [[[self alloc] initWithEntity:entity] autorelease];
                        listString = [expression listString];
                        tableName =  [entity externalName];
                    }
                    [expression appendItem:[self newHeaderForAttribute:attribute] toListString:listString];
                    [columnNames addObject:columnName];
                }
            }
        }
    }

    [expression setStatement:[NSString stringWithFormat:@"create %@ %@", tableName, listString]];
    return [NSArray arrayWithObject:expression];
}

+ (NSArray *)dropTableStatementsForEntityGroup:(NSArray *)entityGroup {
    EOEntity *entity;
    FlatFileSQLExpression *expression = nil;

    if ([entityGroup count]) {
        entity = [entityGroup lastObject];
        expression = [[[self alloc] initWithEntity:entity] autorelease];
        [expression setStatement:[NSString stringWithFormat:@"drop %@", [entity externalName]]];
    }

    return [NSArray arrayWithObject:expression];
}

+ (NSArray *)primaryKeyConstraintStatementsForEntityGroup:(NSArray *)entityGroup {
    return nil;
}

+ (NSArray *)primaryKeySupportStatementsForEntityGroup:(NSArray *)entityGroup {
    return nil;
}

+ (NSArray *)foreignKeyConstraintStatementsForEntityGroup:(NSArray *)entityGroup {
    return nil;
}

+ (NSString *)newHeaderForAttribute:(EOAttribute *)attribute {
    return [NSString stringWithFormat:@"%@%@%@", [attribute columnName], [FlatFileAdaptor columnHeaderSeparator], [attribute externalType]];
}

+ (NSArray *)dropPrimaryKeySupportStatementsForEntityGroup:(NSArray *)entityGroup {
    return nil;
}

+ (NSArray *)foreignKeyConstraintStatementsForRelationship:(EORelationship *)relationship {
    return nil;
}


@end
