* README: Add Trading framework.

* GNUmakefile: Ditto.
* Trading/*: Initial import.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gdl2/trunk@26593 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Matt Rice 2008-06-03 13:29:46 +00:00
parent d89df1082e
commit a8f0ef8c1d
23 changed files with 1404 additions and 1 deletions

View file

@ -3,6 +3,10 @@
* README: Reflect name change of PostgreSQL adaptor, and addition of
SQLite adaptor. Bump supported version of Postgres Adaptor.
* README: Add Trading framework.
* GNUmakefile: Ditto.
* Trading/*: Initial import.
2008-05-29 Blake Nicholson <blaken@umich.edu>
* DBModeler/Modeler.m: Removed duplicate invocation of [adaptor

View file

@ -42,7 +42,7 @@ ifeq ($(GUI_MAKE_LOADED),yes)
SUBPROJECTS += EOInterface EOModeler DBModeler @GUI_SUBPROJECTS@
endif
ifeq ($(GUI_LIB),apple)
SUBPROJECTS += EOInterface EOModeler DBModeler @GUI_SUBPROJECTS@
SUBPROJECTS += EOInterface EOModeler DBModeler Trading @GUI_SUBPROJECTS@
endif
DOCUMENT_NAME = GDL2

7
README
View file

@ -46,6 +46,13 @@ the PostgreSQL and SQLite databases.
GDL2 will offers a UI application to create and maintain .eomodel(d)
files. This is the first release of a preliminary version.
- Trading
The Trading framework, provides an example model, and some routines to
create a database from the model, and populate the database with data,
while not an example itself it is indented for use by example applications
using GDL2 alone, EOInterface or GSWeb, and for higher level tests.
======================
*** Important Note ***
======================

23
Trading/GNUmakefile Normal file
View file

@ -0,0 +1,23 @@
include ../common.make
include $(GNUSTEP_MAKEFILES)/common.make
include ../Version
include ../common.make
NATIVE_LIBRARY_NAME=Trading
TOOL_NAME=createTradingDB
Trading_RESOURCE_FILES=Trading.eomodeld
Trading_HAS_RESOURCE_BUNDLE=yes
Trading_OBJC_FILES=Trading.m TradingData.m
Trading_HEADER_FILES=Trading.h TradingData.h
createTradingDB_OBJC_FILES=create.m
ADDITIONAL_INCLUDE_DIRS+=-I..
ADDITIONAL_NATIVE_LIBS+=EOAccess EOControl Trading
ADDITIONAL_NATIVE_LIB_DIRS+=../EOAccess ../EOControl ../Trading
include $(GNUSTEP_MAKEFILES)/native-library.make
include $(GNUSTEP_MAKEFILES)/tool.make

View file

@ -0,0 +1,57 @@
{
attributes = (
{
allowsNull = Y;
columnName = GRPID;
name = grpid;
prototypeName = integer;
},
{
columnName = NAME;
name = name;
prototypeName = char;
},
{
columnName = PID;
name = pid;
prototypeName = integer;
}
);
className = EOGenericRecord;
classProperties = (
customerGroup,
name,
orders
);
externalName = TST_CUSTOMER;
name = Customer;
primaryKeyAttributes = (
pid
);
relationships = (
{
destination = CustomerGroup;
isToMany = N;
joinSemantic = EOInnerJoin;
joins = (
{
destinationAttribute = pid;
sourceAttribute = grpid;
}
);
name = customerGroup;
},
{
destination = Order;
isToMany = Y;
joinSemantic = EOInnerJoin;
joins = (
{
destinationAttribute = cstid;
sourceAttribute = pid;
}
);
name = orders;
}
);
}

View file

@ -0,0 +1,51 @@
{
attributes = (
{
columnName = NAME;
name = name;
prototypeName = char;
},
{
columnName = PID;
name = pid;
prototypeName = integer;
}
);
className = EOGenericRecord;
classProperties = (
customers,
name,
priceList
);
externalName = TST_CUSTOMERGROUP;
name = CustomerGroup;
primaryKeyAttributes = (
pid
);
relationships = (
{
destination = Customer;
isToMany = Y;
joinSemantic = EOInnerJoin;
joins = (
{
destinationAttribute = grpid;
sourceAttribute = pid;
}
);
name = customers;
},
{
destination = PriceList;
isToMany = Y;
joinSemantic = EOInnerJoin;
joins = (
{
destinationAttribute = cstgrpid;
sourceAttribute = pid;
}
);
name = priceList;
}
);
}

View file

@ -0,0 +1,33 @@
{
attributes = (
{
externalType = char;
name = char;
valueClassName = NSString;
width = 30;
},
{
externalType = date;
name = date;
valueClassName = NSCalendarDate;
},
{
externalType = decimal;
name = decimal;
precision = 12;
scale = 2;
valueClassName = NSDecimalNumber;
},
{
externalType = integer;
name = integer;
valueClassName = NSNumber;
valueType = i;
}
);
className = EOGenericRecord;
isAbstractEntity = Y;
name = EOPostgreSQLPrototypes;
relationships = (
);
}

View file

@ -0,0 +1,33 @@
{
attributes = (
{
externalType = char;
name = char;
valueClassName = NSString;
width = 30;
},
{
externalType = date;
name = date;
valueClassName = NSCalendarDate;
},
{
externalType = decimal;
name = decimal;
precision = 12;
scale = 2;
valueClassName = NSDecimalNumber;
},
{
externalType = integer;
name = integer;
valueClassName = NSNumber;
valueType = i;
}
);
className = EOGenericRecord;
isAbstractEntity = Y;
name = EOPostgres95Prototypes;
relationships = (
);
}

View file

@ -0,0 +1,33 @@
{
attributes = (
{
externalType = TEXT;
name = char;
valueClassName = NSString;
width = 30;
},
{
externalType = DATE;
name = date;
valueClassName = NSCalendarDate;
},
{
externalType = decimal;
name = decimal;
precision = 12;
scale = 2;
valueClassName = NSDecimalNumber;
},
{
externalType = INTEGER;
name = integer;
valueClassName = NSNumber;
valueType = i;
}
);
className = EOGenericRecord;
isAbstractEntity = Y;
name = EOSQLite3Prototypes;
relationships = (
);
}

View file

@ -0,0 +1,57 @@
{
attributes = (
{
allowsNull = Y;
columnName = CSTID;
name = cstid;
prototypeName = integer;
},
{
columnName = DATE;
name = date;
prototypeName = date;
},
{
columnName = PID;
name = pid;
prototypeName = integer;
}
);
className = EOGenericRecord;
classProperties = (
customer,
date,
orderPos
);
externalName = TST_ORDER;
name = Order;
primaryKeyAttributes = (
pid
);
relationships = (
{
destination = OrderPos;
isToMany = Y;
joinSemantic = EOInnerJoin;
joins = (
{
destinationAttribute = ordid;
sourceAttribute = pid;
}
);
name = orderPos;
},
{
destination = Customer;
isToMany = N;
joinSemantic = EOInnerJoin;
joins = (
{
destinationAttribute = pid;
sourceAttribute = cstid;
}
);
name = customer;
}
);
}

View file

@ -0,0 +1,81 @@
{
attributes = (
{
columnName = ORDID;
name = ordid;
prototypeName = integer;
},
{
columnName = PID;
name = pid;
prototypeName = integer;
},
{
columnName = POSNR;
name = posnr;
prototypeName = integer;
},
{
columnName = PRDID;
name = prdid;
prototypeName = integer;
},
{
columnName = PRICE;
name = price;
prototypeName = decimal;
},
{
columnName = QUANTITY;
name = quantity;
prototypeName = decimal;
},
{
columnName = VALUE;
name = value;
prototypeName = decimal;
}
);
className = EOGenericRecord;
classProperties = (
order,
posnr,
price,
product,
quantity,
value
);
externalName = TST_ORDERPOS;
name = OrderPos;
primaryKeyAttributes = (
pid
);
relationships = (
{
destination = Product;
isMandatory = Y;
isToMany = N;
joinSemantic = EOInnerJoin;
joins = (
{
destinationAttribute = pid;
sourceAttribute = prdid;
}
);
name = product;
},
{
destination = Order;
isMandatory = Y;
isToMany = N;
joinSemantic = EOInnerJoin;
joins = (
{
destinationAttribute = pid;
sourceAttribute = ordid;
}
);
name = order;
}
);
}

View file

@ -0,0 +1,58 @@
{
attributes = (
{
columnName = CSTGRPID;
externalType = integer;
name = cstgrpid;
valueClassName = NSNumber;
},
{
columnName = NAME;
externalType = char;
name = name;
prototypeName = char;
},
{
columnName = PID;
name = pid;
prototypeName = integer;
}
);
className = EOGenericRecord;
classProperties = (
customerGroup,
name,
priceListPos
);
externalName = TST_PRICELIST;
name = PriceList;
primaryKeyAttributes = (
pid
);
relationships = (
{
destination = CustomerGroup;
isToMany = N;
joinSemantic = EOInnerJoin;
joins = (
{
destinationAttribute = pid;
sourceAttribute = cstgrpid;
}
);
name = customerGroup;
},
{
destination = PriceListPos;
isToMany = Y;
joinSemantic = EOInnerJoin;
joins = (
{
destinationAttribute = prlid;
sourceAttribute = pid;
}
);
name = priceListPos;
}
);
}

View file

@ -0,0 +1,61 @@
{
attributes = (
{
columnName = PID;
name = pid;
prototypeName = integer;
},
{
columnName = PRDID;
name = prdid;
prototypeName = integer;
},
{
columnName = PRICE;
name = price;
prototypeName = decimal;
},
{
columnName = PRLID;
name = prlid;
prototypeName = integer;
}
);
className = EOGenericRecord;
classProperties = (
price,
priceList,
product
);
externalName = TST_PRICELISTPOS;
name = PriceListPos;
primaryKeyAttributes = (
pid
);
relationships = (
{
destination = Product;
isToMany = N;
joinSemantic = EOInnerJoin;
joins = (
{
destinationAttribute = pid;
sourceAttribute = prdid;
}
);
name = product;
},
{
destination = PriceList;
isToMany = N;
joinSemantic = EOInnerJoin;
joins = (
{
destinationAttribute = pid;
sourceAttribute = prlid;
}
);
name = priceList;
}
);
}

View file

@ -0,0 +1,85 @@
{
attributes = (
{
columnName = GRPID;
externalType = integer;
name = grpid;
prototypeName = integer;
},
{
columnName = NAME;
name = name;
prototypeName = char;
},
{
columnName = PID;
name = pid;
prototypeName = integer;
},
{
allowsNull = Y;
columnName = PRICE;
externalType = decimal;
name = price;
valueClassName = NSDecimalNumber;
},
{
allowsNull = Y;
columnName = SUPID;
externalType = integer;
name = supid;
valueClassName = NSNumber;
}
);
className = EOGenericRecord;
classProperties = (
name,
orderPos,
price,
productGroup,
supplier
);
externalName = TST_PRODUCT;
name = Product;
primaryKeyAttributes = (
pid
);
relationships = (
{
destination = ProductGroup;
isToMany = N;
joinSemantic = EOInnerJoin;
joins = (
{
destinationAttribute = pid;
sourceAttribute = grpid;
}
);
name = productGroup;
},
{
destination = Supplier;
isToMany = N;
joinSemantic = EOInnerJoin;
joins = (
{
destinationAttribute = pid;
sourceAttribute = supid;
}
);
name = supplier;
},
{
destination = OrderPos;
isToMany = Y;
joinSemantic = EOInnerJoin;
joins = (
{
destinationAttribute = prdid;
sourceAttribute = pid;
}
);
name = orderPos;
}
);
}

View file

@ -0,0 +1,38 @@
{
attributes = (
{
columnName = NAME;
name = name;
prototypeName = char;
},
{
columnName = PID;
name = pid;
prototypeName = integer;
}
);
className = EOGenericRecord;
classProperties = (
name,
products
);
externalName = TST_PRODUCTGROUP;
name = ProductGroup;
primaryKeyAttributes = (
pid
);
relationships = (
{
destination = Product;
isToMany = Y;
joinSemantic = EOInnerJoin;
joins = (
{
destinationAttribute = grpid;
sourceAttribute = pid;
}
);
name = products;
}
);
}

View file

@ -0,0 +1,39 @@
{
attributes = (
{
columnName = NAME;
name = name;
prototypeName = char;
width = 90;
},
{
columnName = PID;
name = pid;
prototypeName = integer;
}
);
className = EOGenericRecord;
classProperties = (
name,
products
);
externalName = TST_SUPPLIER;
name = Supplier;
primaryKeyAttributes = (
pid
);
relationships = (
{
destination = Product;
isToMany = Y;
joinSemantic = EOInnerJoin;
joins = (
{
destinationAttribute = supid;
sourceAttribute = pid;
}
);
name = products;
}
);
}

View file

@ -0,0 +1,68 @@
{
EOModelVersion = 2;
adaptorName = PostgreSQL;
entities = (
{
className = EOGenericRecord;
name = Customer;
},
{
className = EOGenericRecord;
name = CustomerGroup;
},
{
className = EOGenericRecord;
name = EOPostgreSQLPrototypes;
},
{
className = EOGenericRecord;
name = EOPostgres95Prototypes;
},
{
className = EOGenericRecord;
name = EOSQLite3Prototypes;
},
{
className = EOGenericRecord;
name = Order;
},
{
className = EOGenericRecord;
name = OrderPos;
},
{
className = EOGenericRecord;
name = PriceList;
},
{
className = EOGenericRecord;
name = PriceListPos;
},
{
className = EOGenericRecord;
name = Product;
},
{
className = EOGenericRecord;
name = ProductGroup;
},
{
className = EOGenericRecord;
name = Supplier;
}
);
storedProcedures = (
procName
);
userInfo = {
PostgreSQLConnectionDictionary = {
databaseName = gdl2trading;
};
Postgres95ConnectionDictionary = {
databaseName = gdl2trading;
};
SQLite3ConnectionDictionary = {
databasePath = "/tmp/gdl2trading";
};
};
}

View file

@ -0,0 +1,15 @@
{
arguments = (
{
allowsNull = Y;
columnName = "";
externalType = varchar;
name = argument;
parameterDirection = 1;
valueClassName = NSString;
width = 20;
}
);
externalName = externalProcName;
name = procName;
}

34
Trading/Trading.h Normal file
View file

@ -0,0 +1,34 @@
#include <EOAccess/EOAccess.h>
#include <EOControl/EOControl.h>
#include <Foundation/Foundation.h>
@interface Trading : NSObject
{
EOModel *_model;
EOAdaptor *_adaptor;
EOAdaptorChannel *_channel;
EOAdaptorContext *_context;
Class _exprClass;
}
+ (EOModel *)defaultModel;
- (id) initWithModel:(EOModel *)model;
- (id) initWithModel:(EOModel *)model adaptorName:(NSString *)adaptorName;
- (id) initWithModel:(EOModel *)model
adaptorName:(NSString*)adaptorName
connectionDictionary:(NSDictionary *)connDict;
- (BOOL) isOpen;
- (void) open;
- (void) close;
- (void) recreateTables;
- (void) dropTables;
- (void) createTables;
- (EOAdaptor *)adaptor;
- (EOAdaptorChannel *)channel;
- (EOAdaptorContext *)context;
- (Class)exprClass;
- (EOModel *)model;
@end

247
Trading/Trading.m Normal file
View file

@ -0,0 +1,247 @@
#include "Trading.h"
/* Convience class for working with models supporting multiple adaptors */
@implementation Trading : NSObject
+ (EOModel *) defaultModel
{
EOModelGroup *globalGrp = [EOModelGroup globalModelGroup];
EOModel *model = [[EOModelGroup globalModelGroup] modelNamed:@"Trading"];
/*
* globalModelGroup doesn't currently work with non-framework native-libs
*/
if (!model)
{
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *modelPath = [bundle pathForResource:@"Trading"
ofType:@"eomodeld"];
[globalGrp addModelWithFile:modelPath];
model = [globalGrp modelNamed:@"Trading"];
}
return model;
}
/* calls initWithModel: with the default model */
- (id) init
{
return [self initWithModel:[[self class] defaultModel]];
}
- (BOOL) hasAdaptorNamed:(NSString *)adaptorName;
{
NSArray *adaptorNames;
if (adaptorName == nil)
return NO;
adaptorNames = [EOAdaptor availableAdaptorNames];
return [adaptorNames containsObject:adaptorName];
}
/* calls initWithModel:adaptorName:
* looking for an adaptor name in the following order:
* first it looks at the TEST_ADAPTOR environment variable
* then it looks for the GDL2TestAdaptor key
* then it looks for the models -adaptorName.
* finally if all else fails, it uses the first adaptor in the
* +availableAdaptorNames array.
*/
- (id) initWithModel:(EOModel *)model
{
NSArray *adaptorNames;
NSString *adaptorName;
BOOL flag;
adaptorNames = [EOAdaptor availableAdaptorNames];
adaptorName = [[[NSProcessInfo processInfo] environment]
objectForKey:@"TEST_ADAPTOR"];
if (flag == NO && ((flag = [self hasAdaptorNamed:adaptorName]) == NO))
adaptorName = [[NSUserDefaults standardUserDefaults]
stringForKey:@"GDL2TestAdaptorName"];
if (flag == NO && ((flag = [self hasAdaptorNamed:adaptorName]) == NO))
{
adaptorName = [model adaptorName];
}
if (flag == NO && ((flag = [self hasAdaptorNamed:adaptorName]) == NO))
{
adaptorName = [adaptorNames count] ? [adaptorNames objectAtIndex:0]
: nil;
}
return [self initWithModel:model adaptorName:adaptorName];
}
/*
* uses the default model and an 'adaptorName'ConnectionDictionary
* in the models userInfo.
*/
- (id) initWithModel:(EOModel *)model adaptorName:(NSString *)adaptorName
{
NSAssert(adaptorName, @"nil adaptor name");
{
NSString *dictName = [adaptorName stringByAppendingString:
@"ConnectionDictionary"];
NSDictionary *connDict = [[model userInfo] objectForKey: dictName];
return [self initWithModel:model
adaptorName:adaptorName
connectionDictionary:connDict];
}
}
/* designated initializer assigns the models adaptor name, and connection
* dictionary */
- (id) initWithModel:(EOModel *)model
adaptorName:(NSString*)adaptorName
connectionDictionary:(NSDictionary *)connDict
{
self = [super init];
if (self)
{
_model = [model retain];
[model setConnectionDictionary:connDict];
[model setAdaptorName:adaptorName];
_adaptor = [[EOAdaptor adaptorWithModel:_model] retain];
_exprClass = [_adaptor expressionClass];
_context = [[_adaptor createAdaptorContext] retain];
_channel = [[_context createAdaptorChannel] retain];
}
return self;
}
- (void) dealloc
{
[self close];
[_model release];
[_adaptor release];
[_context release];
[_channel release];
[super dealloc];
}
/*
* drops the tables (ignoring any exceptions)
* as the tables may not exist to be dropped.
* then returns the value of [self createTables];
*/
- (void) recreateTables
{
NS_DURING
[self dropTables];
NS_HANDLER
NS_ENDHANDLER
return [self createTables];
}
- (void) dropTables
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSArray *entities = [_model entities];
NSDictionary *dropOptDict
= [NSDictionary dictionaryWithObjectsAndKeys:
@"NO", @"EOPrimaryKeyConstraintsKey",
@"NO", @"EOCreatePrimaryKeySupportKey",
@"NO", @"EOCreateTablesKey",
nil];
NSArray *exprs;
EOSQLExpression *expr;
unsigned i,c;
exprs = [_exprClass schemaCreationStatementsForEntities: entities
options: dropOptDict];
[self open];
for (i=0, c=[exprs count]; i<c; i++)
{
expr = [exprs objectAtIndex: i];
[_channel evaluateExpression: expr];
}
[self close];
[pool release];
}
- (void) createTables
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSArray *entities = [_model entities];
NSDictionary *createOptDict
= [NSDictionary dictionaryWithObjectsAndKeys:
@"NO", @"EODropTablesKey",
@"NO", @"EODropPrimaryKeySupportKey", nil];
NSArray *exprs;
EOSQLExpression *expr;
unsigned i,c;
[self open];
exprs = [_exprClass schemaCreationStatementsForEntities: entities
options: createOptDict];
for (i=0, c=[exprs count]; i<c; i++)
{
expr = [exprs objectAtIndex: i];
[_channel evaluateExpression: expr];
}
[self close];
[pool release];
}
- (void) open
{
if (![self isOpen])
[_channel openChannel];
}
- (void) close
{
if ([_channel isOpen])
[_channel closeChannel];
}
- (BOOL) isOpen
{
return [_channel isOpen];
}
- (Class) exprClass
{
return _exprClass;
}
- (EOAdaptorChannel *)channel
{
return _channel;
}
- (EOAdaptorContext *)context
{
return _context;
}
- (EOModel *)model
{
return _model;
}
- (EOAdaptor *)adaptor
{
return _adaptor;
}
@end

49
Trading/TradingData.h Normal file
View file

@ -0,0 +1,49 @@
#import <Foundation/Foundation.h>
#include <EOAccess/EOAccess.h>
#include <EOControl/EOControl.h>
@interface TradingData : NSObject
{
EOEditingContext *ec;
EODatabaseDataSource *productGroupDS;
EODatabaseDataSource *productDS;
EODatabaseDataSource *suppliersDS;
EODatabaseDataSource *priceListDS;
EODatabaseDataSource *priceListPosDS;
EODatabaseDataSource *orderDS;
EODatabaseDataSource *orderPosDS;
EODatabaseDataSource *customerDS;
EODatabaseDataSource *customerGroupDS;
}
- (void) saveChanges;
- (void) fillTables;
- (id) addSupplierNamed:(NSString *)name;
- (id) addProductGroup:(NSString *)name;
- (id) addProduct:(NSString*)name price:(NSDecimalNumber *)price
supplier:(id)supplier
group:(id)group;
- (id) addProduct:(id)product
price:(NSDecimalNumber *)price
toPriceList:(id)priceList;
- (id) addPriceList:(NSString *)name
forGroup:(id)group;
- (id) addCustomer:(NSString *)customer;
- (id) addCustomer:(NSString *)customer group:(id)group;
- (id) addCustomerGroup:(NSString *)name;
- (id) customer:(id)customer
order:(id)product;
- (id) customer:(id)customer
order:(id)product
quantity:(int)quantity;
- (id) customer:(id)customer
order:(id)product
quantity:(int)quantity
posNr:(int)posNr;
- (id) createOrderForCustomer:(id)customer;
- (id) order:(id)order
product:(id)product
quantity:(int)quantity;
@end

312
Trading/TradingData.m Normal file
View file

@ -0,0 +1,312 @@
#include "TradingData.h"
#define PDEC2(x) \
[NSDecimalNumber decimalNumberWithMantissa:x \
exponent:-2 \
isNegative:NO]
@implementation TradingData
- (id) init
{
self = [super init];
if (self)
{
ec = [[EOEditingContext alloc] init];
customerDS = [[EODatabaseDataSource alloc] initWithEditingContext:ec
entityName:@"Customer"];
customerGroupDS = [[EODatabaseDataSource alloc] initWithEditingContext:ec
entityName:@"CustomerGroup"];
productGroupDS = [[EODatabaseDataSource alloc] initWithEditingContext:ec
entityName:@"ProductGroup"];
productDS = [[EODatabaseDataSource alloc] initWithEditingContext:ec
entityName:@"Product"];
suppliersDS = [[EODatabaseDataSource alloc] initWithEditingContext:ec
entityName:@"Supplier"];
priceListDS = [[EODatabaseDataSource alloc] initWithEditingContext:ec
entityName:@"PriceList"];
priceListPosDS = [[EODatabaseDataSource alloc] initWithEditingContext:ec
entityName:@"PriceListPos"];
orderDS = [[EODatabaseDataSource alloc] initWithEditingContext:ec
entityName:@"Order"];
orderPosDS = [[EODatabaseDataSource alloc] initWithEditingContext:ec
entityName:@"OrderPos"];
}
return self;
}
- (void) dealloc
{
[ec release];
[customerDS release];
[customerGroupDS release];
[productGroupDS release];
[productDS release];
[suppliersDS release];
[priceListDS release];
[priceListPosDS release];
[orderDS release];
[orderPosDS release];
}
- (void) fillTables
{
id fsfSup, mysqlSup, postgreSup, publicSup, acmeSup;
id implGrp, softGrp;
id folkGrp;
id rake, shovel, pitchfork, mysql, postgresql, sqlite, gnustep, gcc;
id salePriceList;
id customer;
id order;
implGrp = [self addProductGroup:@"Implements of destruction"];
softGrp = [self addProductGroup:@"Software"];
fsfSup = [self addSupplierNamed:@"The Free Software Foundation"];
gnustep = [self addProduct:@"GNUstep"
price:PDEC2(2595)
supplier:fsfSup
group:softGrp];
gcc = [self addProduct:@"gcc"
price:PDEC2(4995)
supplier:fsfSup
group:softGrp];
mysqlSup = [self addSupplierNamed:@"MySQL AB"];
mysql = [self addProduct:@"MySQL"
price:PDEC2(3000)
supplier:fsfSup
group:softGrp];
postgreSup = [self addSupplierNamed:@"The PostgreSQL Global Development Group"];
postgresql = [self addProduct:@"PostgreSQL"
price:PDEC2(3000)
supplier:postgreSup
group:softGrp];
publicSup = [self addSupplierNamed:@"sqlite.org"];
sqlite = [self addProduct:@"SQLite"
price:PDEC2(1500)
supplier:publicSup
group:softGrp];
acmeSup = [self addSupplierNamed:@"ACME"];
shovel = [self addProduct:@"Shovel"
price:PDEC2(1400)
supplier:acmeSup
group:implGrp];
rake = [self addProduct:@"Rake"
price:PDEC2(1295)
supplier:acmeSup
group:implGrp];
pitchfork = [self addProduct:@"Pitchfork"
price:PDEC2(1800)
supplier:acmeSup
group:implGrp];
folkGrp = [self addCustomerGroup:@"Angry Townfolk"];
[self customer:[self addCustomer:@"Angry Townsperson1" group:folkGrp]
order:rake
quantity:3];
[self customer:[self addCustomer:@"Angry Townsperson2" group:folkGrp]
order:shovel];
[self customer:[self addCustomer:@"Angry Townsperson3" group:folkGrp]
order:rake
quantity:2];
customer = [self addCustomer:@"Test Customer1"];
order = [self createOrderForCustomer:customer];
[self order:order product:mysql quantity:5];
[self order:order product:postgresql quantity:1];
[self order:order product:rake quantity:2];
[self customer:[self addCustomer:@"Test Customer2"]
order:postgresql];
[self customer:[self addCustomer:@"Test Customer3"]
order:sqlite];
salePriceList = [self addPriceList:@"Farm Supply Sale"
forGroup:folkGrp];
[self addProduct:pitchfork
price:PDEC2(1675)
toPriceList:salePriceList];
[self addProduct:rake
price:PDEC2(800)
toPriceList:salePriceList];
[self addProduct:shovel
price:PDEC2(995)
toPriceList:salePriceList];
[self saveChanges];
}
- (id) addPriceList:(NSString *)name
forGroup:(id)grp
{
id record = [priceListDS createObject];
[record takeValue:name forKey:@"name"];
[record addObject:grp toBothSidesOfRelationshipWithKey:@"customerGroup"];
return record;
}
- (id) addProduct:(id)product
price:(NSDecimalNumber *)price
toPriceList:(id)priceList
{
id record = [priceListPosDS createObject];
[record addObject:product toBothSidesOfRelationshipWithKey:@"product"];
[record takeValue:price forKey:@"price"];
[record addObject:priceList toBothSidesOfRelationshipWithKey:@"priceList"];
[priceListPosDS insertObject:record];
return record;
}
- (id) addProductGroup:(NSString *)productGroupName
{
id record = [productGroupDS createObject];
[record takeValue:productGroupName forKey:@"name"];
return record;
}
- (id) addSupplierNamed:(NSString *)name
{
id record = [suppliersDS createObject];
[record takeValue:name forKey:@"name"];
[suppliersDS insertObject:record];
return record;
}
- (id) addProduct:(NSString *)productName
price:(NSDecimalNumber *)price
supplier:(id)supplier
group:(id)productGroup
{
id record = [productDS createObject];
[record takeValue:productName forKey:@"name"];
[record takeValue:price forKey:@"price"];
[record addObject:supplier toBothSidesOfRelationshipWithKey:@"supplier"];
[record addObject:productGroup toBothSidesOfRelationshipWithKey:@"productGroup"];
[productDS insertObject:record];
return record;
}
- (id) addCustomerGroup:(NSString *)name
{
id record = [customerGroupDS createObject];
[record takeValue:name forKey:@"name"];
[customerGroupDS insertObject:record];
return record;
}
- (id) addCustomer:(NSString *)name
{
return [self addCustomer:name group:nil];
}
- (id) addCustomer:(NSString *)name
group:(id)group
{
id record = [customerDS createObject];
[record takeValue:name forKey:@"name"];
[record addObject:group toBothSidesOfRelationshipWithKey:@"customerGroup"];
[customerDS insertObject:record];
return record;
}
- (id) createOrderForCustomer:(id)customer
{
id order = [orderDS createObject];
[order takeValue:[NSDate date] forKey:@"date"];
[order addObject:customer toBothSidesOfRelationshipWithKey:@"customer"];
[orderDS insertObject:order];
return order;
}
- (id) order:(id)order
product:(id)product
quantity:(int)quantity
{
id orderPos = [orderPosDS createObject];
NSNumber *price = [product valueForKey:@"price"];
NSNumber *qNum = [NSNumber numberWithInt:quantity];
/* this should really not be in the database but in some business logic */
NSNumber *value = [NSNumber numberWithDouble:((double)quantity) * [price doubleValue]];
[orderPos addObject:product toBothSidesOfRelationshipWithKey:@"product"];
[orderPos addObject:order toBothSidesOfRelationshipWithKey:@"order"];
/* FIXME customer group pricing */
[orderPos takeValue:price forKey:@"price"];
[orderPos takeValue:qNum forKey:@"quantity"];
[orderPos takeValue:value forKey:@"value"];
/* FIXME this should be a relationship to a pos table */
[orderPos takeValue:[NSNumber numberWithInt:1] forKey:@"posnr"];
return orderPos;
}
- (id) customer:(id)customer
order:(id)product
{
return [self customer:customer order:product quantity:1];
}
- (id) customer:(id)customer
order:(id)product
quantity:(int)quantity
{
return [self customer:customer order:product quantity:quantity posNr:1];
}
- (id) customer:(id)customer
order:(id)product
quantity:(int)quantity
posNr:(int)posNr
{
id order = [orderDS createObject];
id orderPos = [orderPosDS createObject];
NSNumber *price = [product valueForKey:@"price"];
NSNumber *qNum = [NSNumber numberWithInt:quantity];
NSNumber *value = [NSNumber numberWithDouble:((double)quantity) * [price doubleValue]];
[order takeValue:[NSDate date] forKey:@"date"];
[order addObject:customer toBothSidesOfRelationshipWithKey:@"customer"];
[orderPos addObject:order toBothSidesOfRelationshipWithKey:@"order"];
/* FIXME customer group pricing */
[orderPos takeValue:price forKey:@"price"];
[orderPos takeValue:qNum forKey:@"quantity"];
[orderPos takeValue:value forKey:@"value"];
/* fixme this should be a relationship to a pos table */
[orderPos takeValue:[NSNumber numberWithInt:posNr] forKey:@"posnr"];
[orderPos addObject:product toBothSidesOfRelationshipWithKey:@"product"];
return order;
}
- (void) saveChanges
{
[ec saveChanges];
}
@end

18
Trading/create.m Normal file
View file

@ -0,0 +1,18 @@
#include "Trading.h"
#include "TradingData.h"
int main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
id trading = [[Trading alloc] init];
id data = [[TradingData alloc] init];
[trading recreateTables];
[data fillTables];
[data saveChanges];
[trading release];
[data release];
[pool release];
return 0;
}