PBDDTable.m


max21 Unternehmensgruppe
#import "Aprica.h"
//	Aprica2
//	copyright Pirmin Braun 1997-2007 - pirmin@pirmin.de
//	all Rights reserved;
@implementation PBDDTable
- init;
{
    if(!(self = [super init]))return nil;
    attributes = [[NSMutableArray alloc]initWithCapacity:30];
    [self setGuiName:SC_NewTable];
    [self setDbName:SC_NewTable];
    [self setType:SC_Abstract];
    hasPidimage = NO;
    attributesReferencingMe = nil;
    return self;
}
- (id)initWithCoder:(NSCoder *)coder;
{
    if(!([self init]))return nil;
    [self setMyDD: [coder decodeObject]];
    [attributes addObjectsFromArray:[coder decodeObject]];
    [self setPlainAttributes: [coder decodeObject]];
    [self setPlainAttributesLD: [coder decodeObject]];
    [self setGuiName: [coder decodeObject]];
    [self setDbName: [coder decodeObject]];
    [self setBereich: [coder decodeObject]];
    [self setBemerkung: [coder decodeObject]];
    [self setType: [coder decodeObject]];
    [self setPrimaryKeyName: [coder decodeObject]];
    [coder decodeValueOfObjCType:@encode(BOOL) at:&isRealTable];
    [coder decodeValueOfObjCType:@encode(BOOL) at:&hasPidimage];
    [self setPrimaryKeyAttr: [coder decodeObject]];
    [self setDescriAttributes: [coder decodeObject]];
    [self setCsAttrNames: [coder decodeObject]];
    return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
    [coder encodeObject:myDD];
    [coder encodeObject:attributes];
    [coder encodeObject:plainAttributes];
    [coder encodeObject:plainAttributesLD];
    [coder encodeObject:guiName];
    [coder encodeObject:dbName];
    [coder encodeObject:bereich];
    [coder encodeObject:bemerkung];
    [coder encodeObject:type];
    [coder encodeObject:primaryKeyName];
    [coder encodeValueOfObjCType:@encode(BOOL) at:&isRealTable];
    [coder encodeValueOfObjCType:@encode(BOOL) at:&hasPidimage];
    [coder encodeObject:primaryKeyAttr];
    [coder encodeObject:descriAttributes];
    [coder encodeObject:csAttrNames];
}
+ (PBDDTable *)minimalTableNamed:(NSString *)s;
{
    PBDDTable *t = [[PBDDTable alloc]init];
    [t setGuiName:s];
    [t setDbName:s];
    [t setType:SC_Real];
    return [t autorelease];
}
- (void)dealloc
{
    [attributes makeObjectsPerformSelector:@selector(setMyTable:) withObject: nil];
    [plainAttributes makeObjectsPerformSelector:@selector(setMyTable:) withObject: nil];
    [self clean];
    [primaryKeyAttr release];
    [guiName release];
    [dbName release];
    [type release];
    [attributes release];
    [bemerkung release];
    [bereich release];
    [attributesFK release];
    [attributesDB release];
    [attributesDBFetch release];
    [attributesDescri release];
    [dbNamesDB release];
    [dbNamesNotVisible release];
    [plainAttributes release];
    [plainAttributesLD release];
    [descriAttributes release];
    [csAttrNames release];
    [attributesReferencingMe release];
    [super dealloc];
}
- (void)clean;	//gepufferte Variablen loeschen, um Neuberechnung zu erwzingen.
{
    [attributesFK removeAllObjects];
    [attributesDB removeAllObjects];
    [attributesDBFetch removeAllObjects];
    [attributesDescri removeAllObjects];
    [dbNamesDB removeAllObjects];
    [dbNamesNotVisible removeAllObjects];
    [plainAttributes release];
    [plainAttributesLD release];
    [attributesReferencingMe release];
    plainAttributes = nil;
    plainAttributesLD = nil;
    attributesReferencingMe = nil;
    [myDD setIsConsistent:NO];
}
ACCESSClassm(descriAttributes,setDescriAttributes,NSArray)
ACCESSClassm(csAttrNames,setCsAttrNames,NSArray)
ACCESSm(bereich, setBereich)
ACCESSm(bemerkung, setBemerkung)
ACCESSm(dbName, setDbName)
ACCESSm(primaryKeyName, setPrimaryKeyName)
ACCESSm(guiName, setGuiName)
ACCESSClassm(primaryKeyAttr, setPrimaryKeyAttr, PBDDAttribute)
ACCESSClassmNR(myDD, setMyDD, PBDD)
+ (NSArray *)ivarNames;
{
    static NSArray *a;
    if(!a){
        a = [[[PBDDTable ivarNamesShort] arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"attributes",nil]]retain];
    }
    return a;
}
+ (NSArray *)ivarNamesShort;
{
    static NSArray *a;
    if(!a){
        a = [[NSArray arrayWithObjects:SC_dbName,SC_GuiName,SC_tableType,@"bereich",@"bemerkung",nil]retain];
    }
    return a;
}
- (NSDictionary *)valuesForKeys:(NSArray *)a;
{
    int i,j;
    LMD;
    for(i=0,j=[a count];i<j;i++){
        NSString *k = [a oai:i];
        NSObject *o = [self vfk:k];
        if(o)[lmd setObject:o forKey:k];
    }
    return lmd;
}
- (NSString *)primaryKey;
{
    return dbName;
}
- (NSMutableDictionary *)values;
{
    return (id)[NSMutableDictionary dictionaryWithDictionary:[self valuesForKeys:[PBDDTable ivarNames]]];
}
- (NSString *)description;			//um es in ein File zu schreiben
{
    return [[self valuesForKeys:[PBDDTable ivarNames]] description];
}
//f. keyValueCoding
- (void)setAttributes:(NSMutableArray *)value;
{
   [attributes removeAllObjects];
   [attributes addObjectsFromArray:value];
}
- (NSMutableArray *)attributes;{return attributes;}
- (void)addAttributes:(NSArray *)a;
{
    int i,j=[a count];
    for(i=0;i<j;i++){
        [self addAttribute:[a oai:i]];
    }
}
- (void)addAttribute:(PBDDAttribute *)pba;
{
    if(pba){
        [attributes addObject:pba];
        [pba setMyTable:self];
        [pba setMyDD:myDD];
    }
}
- (void)convertDictionaries;	//nach takeValues beim Laden
{
    int i,j;
    int i1,j1;
    NSArray *attrs  = [NSArray arrayWithArray:attributes];
    NSArray *a = [PBDDAttribute ivarNames];
    [attributes removeAllObjects];
    for(i=0,j=[attrs count];i<j;i++){ //dictionaries durch echte attributes ers.
        NSDictionary *d = [attrs oai:i];
        PBDDAttribute *pbdda = [[PBDDAttribute alloc]init];
        for(i1=0,j1=[a count];i1<j1;i1++){
            NSString *k = [a oai:i1];
            NSString *s = [d vfk:k];
            if(s)[pbdda tvfk(s,k)];
        }
        if([[pbdda refdTableName] hasSecurePrefix:@"@"])[pbdda setRefdTableName:[[pbdda refdTableName] substringFromIndex:1]];
        [pbdda setMyDD:[self myDD]];
        [pbdda setMyTable:self];
        [pbdda validate];
        [attributes addObject:pbdda];
        [pbdda release];
    }
}
- (void)refreshAttributesFrom:(NSArray *)attrs;
{
// funktioniert nur fuer topLevel Attribute, nicht fuer solche, die ueber abstract tables reinkommen!
    int i,j;
    NSArray *a = [PBDDAttribute ivarNames];
    int i1,j1=[a count];
    for(i=0,j=[attrs count];i<j;i++){ // attrs sind dictionaries
        NSDictionary *d = [attrs oai:i];
        PBDDAttribute *pbdda = [self plainAttrNamed:[d ofk:@"dbName"]];
        if(pbdda){
            [pbdda initIvars];
            for(i1=0;i1<j1;i1++){
                NSString *k = [a oai:i1];
                NSString *s = [d vfk:k];
                if(s)[pbdda tvfk(s,k)];
            }
            [pbdda buildVLArrays];
            [pbdda validate];
        }
    }
}
//Accessor
- (BOOL)isRealTable;{return isRealTable;}
- (void)setIsRealTable:(BOOL)yn;{isRealTable = yn;}
- (BOOL)hasPidimage;{return  hasPidimage;}
- (NSString *)type;{return  type;}
- (void)setType:(NSString *)s;
{
   if(s != type){
       [type release];
       type = [s retain];
   }
   isRealTable = ![type iE:SC_Abstract];
}
//Aufbereitung
- (void)getPKN;
{
    NSArray *a;
    PBDDAttribute	*pba;
    int	i,j;
    
        // darf erst erfolgen, wenn alle tables geladen!
    [self setPrimaryKeyName:nil];
    [self setPrimaryKeyAttr:nil];
    a = [self plainAttributes];
    for(i=0,j=[a count];i<j;i++){
        pba = [a oai:i];
        if([pba isPK]){
            [self setPrimaryKeyName:[pba dbName]];
            [self setPrimaryKeyAttr:pba];
            break;
        }
    }
}
- (int)pkLen;
{
    //Laenge des pk
    return [primaryKeyAttr length];
}
- (void)consolidate;
{
    //den fk Feldern die jeweilige pkLen setzen
    //vor sql-gen und nach laden in Anwendung;
    NSArray *a = [self attributesFK];
    int i,j;
    PBDDAttribute *at;
    PBDDTable *rt;
    hasPidimage = NO;
    if((j=[a count])){
        for(i=0;i<j;i++){
            at = [a oai:i];
            if((rt=[at relationTable])){
                [at setLength:[rt pkLen]];
                [at setDataTyp:[[rt primaryKeyAttr]dataTyp]];
            }
        }
    }
    [primaryKeyAttr setLength:[self pkLen]];
    [self determineDescri];
    [self determineCS];
    if([self plainAttrNamed:@"pidimagefn"])hasPidimage = YES;
}
- (void)determineDescri;
{
    //alle descri-attr. sammeln und nach isDescri sortieren; Namen davon in array
    LMA;
    NSArray *a = [self attributesDescri],*soa;
    int i,j;
    soa = [NSArray soaFrom:@"isDescriS"];
    a = [a sortedArrayUsingKeyOrderArray:soa];
    for(i=0,j=[a count];i<j;i++){
        PBDDAttribute *pbat = [a oai:i];
        // das hier geht schief, weil Association selbst mit SQL die Daten holt: if([pbat targetTyp]==ATTVCSELOBJ)[lma addObject:[pbat dbName]];
        if([pbat isDB])[lma addObject:pbat];
    }
    [self setDescriAttributes:lma];
}
- (void)determineCS;
{
    // alle CS-attr. sammeln und nach isCombiSuchS sortieren; Namen davon in array
    LMA;
    NSArray *a = [self plainAttributes],*soa;
    int i,j;
    soa = [NSArray soaFrom:@"isCombiSuchS"];
    a = [a sortedArrayUsingKeyOrderArray:soa];
    for(i=0,j=[a count];i<j;i++){
        PBDDAttribute *pbat = [a oai:i];
        if([pbat isCombiSuch] && [pbat isDB])[lma addObject:[pbat dbName]];
    }
// falls kein CS Attribut, zumindest PK nehmen
    if(![lma count] && [self primaryKeyName])[lma addObject:[self primaryKeyName]];
    [self setCsAttrNames:lma];
}
//plainAttributes handling
- (NSArray *)plainAttributes;		//expandiert alle Macros;
{
    NSMutableArray	*ma;
    PBDDAttribute *a;
    int i,j;
    if(plainAttributes) return plainAttributes;
    ma = [NSMutableArray arrayWithCapacity:30];
    j= [attributes count];
    for(i=0;i<j;i++){
        a = [attributes oai:i];
        [ma addObjectsFromArray:[a plainAttributes]];
    }
    [ma sortUsingKeyOrderArray:[NSArray soaFrom:@"dbName"]]; //plain Attributes sortiert zurueckgeben, damit z.B. die autom. Indexnamen immer in der gleichen Reihenfolge hochgezaehlt werden;
    [self setPlainAttributes:ma];
    [self setPlainAttributesLD:[NSDictionary lookupDictForArray:ma key:@"dbName"]];
    return plainAttributes;
}
- (void)setPlainAttributes:(NSArray *)a;
{
    if(plainAttributes == a)return;
    [plainAttributes release];
    plainAttributes = [a retain];
}
- (NSDictionary *)plainAttributesLD;
{
    if(!plainAttributesLD){
        [self plainAttributes];
    }
    return plainAttributesLD;
}
- (void)setPlainAttributesLD:(NSDictionary *)d;
{
    if(plainAttributesLD == d)return;
    [plainAttributesLD release];
    plainAttributesLD = [d retain];
}
- (PBDDAttribute *)plainAttrNamed:(NSString *)name;
{
    return [[self plainAttributesLD]ofk:name];
}
- (NSArray *)plainAttributesKeys;
{
    return [[self plainAttributesLD]allKeys];
}
//ungeparste Attribute
- (PBDDAttribute *)attrNamed:(NSString *)name;
{
    int i,j;
    PBDDAttribute	*a;
    if(!name)return nil;
    for(i=0,j=[attributes count];i<j;i++){
        a = [attributes oai:i];
        if([[a dbName]iE:name])return a;
    }
    return nil;
}
//Attribute nach Eigenschaften: s muss ein BOOL returning selector f. ein PBDDAttribute sein; wird SC_DbNames daran anghaengt, kriegt man statt der Attributes nur deren dbNames;
- (NSArray *)attributesFK;
{
    if(!attributesFK){
        PBDDAttribute	*pba;
        int	i,j;
        NSArray *a = [self plainAttributes];
        MA(attributesFK);
        for(i=0,j=[a count];i<j;i++){
            pba = [a oai:i];
            if([pba isFK])[attributesFK addObject:pba];
        }
    }
    return attributesFK;
}
- (NSArray *)attributesDB;
{
    if(!attributesDB){
        PBDDAttribute	*pba;
        int	i,j;
        NSArray *a = [self plainAttributes];
        MA(attributesDB);
        for(i=0,j=[a count];i<j;i++){
            pba = [a oai:i];
            if([pba isDB])[attributesDB addObject:pba];
        }
    }
    return attributesDB;
}
- (NSArray *)attributesDBFetch;
{
// SQL-Expression Felder; sind aber nicht updateable
    if(!attributesDBFetch){
        PBDDAttribute	*pba;
        int	i,j;
        NSArray *a = [self plainAttributes];
        MA(attributesDBFetch);
        for(i=0,j=[a count];i<j;i++){
            pba = [a oai:i];
            if(![pba isDB] && [[pba expression]hasSecurePrefix:@"SQL:"])[attributesDBFetch addObject:pba];
        }
    }
    return attributesDBFetch;
}
- (NSArray *)attributesDescri;
{
    if(!attributesDescri){
        PBDDAttribute	*pba;
        int	i,j;
        NSArray *a = [self plainAttributes];
        MA(attributesDescri);
        for(i=0,j=[a count];i<j;i++){
            pba = [a oai:i];
            if([pba isDescri])[attributesDescri addObject:pba];
        }
    }
    return attributesDescri;
}
- (NSArray *)dbNamesDB;
{
    if(!dbNamesDB){
        PBDDAttribute	*pba;
        int	i,j;
        NSArray *a = [self plainAttributes];
        MA(dbNamesDB);
        for(i=0,j=[a count];i<j;i++){
            pba = [a oai:i];
            if([pba isDB])[dbNamesDB addObject:[pba dbName]];
        }
    }
    return dbNamesDB;
}
- (NSArray *)dbNamesNotVisible;
{
    if(!dbNamesNotVisible){
        PBDDAttribute	*pba;
        int	i,j;
        NSArray *a = [self plainAttributes];
        MA(dbNamesNotVisible);
        for(i=0,j=[a count];i<j;i++){
            pba = [a oai:i];
            if(![pba isVisible])[dbNamesNotVisible addObject:[pba dbName]];
        }
    }
    return dbNamesNotVisible;
}
- (BOOL)hasStatisticFields;
{
    if(![self plainAttrNamed:@"ldate"])return NO;
    if(![self plainAttrNamed:@"cdate"])return NO;
    if(![self plainAttrNamed:@"luser"])return NO;
    if(![self plainAttrNamed:@"cuser"])return NO;
    return YES;
}
- (NSArray *)attributesReferencingMe;
{
    if(attributesReferencingMe)return attributesReferencingMe;
    {
        NSArray *a = [MYDD plainAttributes];
        int i,j;
        LMA;
        for(i=0,j = [a count];i<j;i++){
            PBDDAttribute *pba = [a objectAtIndex:i];
            PBDDTable *t = [pba myTable];
            if(![t isRealTable])continue;
            if(![pba isDB])continue;
            if([[pba refdTableName]iE:dbName]){
                [lma addObject:pba];
            }
        }
        attributesReferencingMe = [lma retain];
    }
    return attributesReferencingMe;
}
@end
Foto