From a8f0ef8c1dfe0fd6a475c9e1bbc5d224aa0652cb Mon Sep 17 00:00:00 2001 From: Matt Rice Date: Tue, 3 Jun 2008 13:29:46 +0000 Subject: [PATCH] * 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 --- ChangeLog | 4 + GNUmakefile.in | 2 +- README | 7 + Trading/GNUmakefile | 23 ++ Trading/Trading.eomodeld/Customer.plist | 57 ++++ Trading/Trading.eomodeld/CustomerGroup.plist | 51 +++ .../EOPostgreSQLPrototypes.plist | 33 ++ .../EOPostgres95Prototypes.plist | 33 ++ .../EOSQLite3Prototypes.plist | 33 ++ Trading/Trading.eomodeld/Order.plist | 57 ++++ Trading/Trading.eomodeld/OrderPos.plist | 81 +++++ Trading/Trading.eomodeld/PriceList.plist | 58 ++++ Trading/Trading.eomodeld/PriceListPos.plist | 61 ++++ Trading/Trading.eomodeld/Product.plist | 85 +++++ Trading/Trading.eomodeld/ProductGroup.plist | 38 +++ Trading/Trading.eomodeld/Supplier.plist | 39 +++ Trading/Trading.eomodeld/index.eomodeld | 68 ++++ .../Trading.eomodeld/procName.storedProcedure | 15 + Trading/Trading.h | 34 ++ Trading/Trading.m | 247 ++++++++++++++ Trading/TradingData.h | 49 +++ Trading/TradingData.m | 312 ++++++++++++++++++ Trading/create.m | 18 + 23 files changed, 1404 insertions(+), 1 deletion(-) create mode 100644 Trading/GNUmakefile create mode 100644 Trading/Trading.eomodeld/Customer.plist create mode 100644 Trading/Trading.eomodeld/CustomerGroup.plist create mode 100644 Trading/Trading.eomodeld/EOPostgreSQLPrototypes.plist create mode 100644 Trading/Trading.eomodeld/EOPostgres95Prototypes.plist create mode 100644 Trading/Trading.eomodeld/EOSQLite3Prototypes.plist create mode 100644 Trading/Trading.eomodeld/Order.plist create mode 100644 Trading/Trading.eomodeld/OrderPos.plist create mode 100644 Trading/Trading.eomodeld/PriceList.plist create mode 100644 Trading/Trading.eomodeld/PriceListPos.plist create mode 100644 Trading/Trading.eomodeld/Product.plist create mode 100644 Trading/Trading.eomodeld/ProductGroup.plist create mode 100644 Trading/Trading.eomodeld/Supplier.plist create mode 100644 Trading/Trading.eomodeld/index.eomodeld create mode 100644 Trading/Trading.eomodeld/procName.storedProcedure create mode 100644 Trading/Trading.h create mode 100644 Trading/Trading.m create mode 100644 Trading/TradingData.h create mode 100644 Trading/TradingData.m create mode 100644 Trading/create.m diff --git a/ChangeLog b/ChangeLog index 5b0833a..4acab41 100644 --- a/ChangeLog +++ b/ChangeLog @@ -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 * DBModeler/Modeler.m: Removed duplicate invocation of [adaptor diff --git a/GNUmakefile.in b/GNUmakefile.in index 68f973a..d5b3905 100644 --- a/GNUmakefile.in +++ b/GNUmakefile.in @@ -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 diff --git a/README b/README index 9fe9ae9..b0f2b65 100644 --- a/README +++ b/README @@ -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 *** ====================== diff --git a/Trading/GNUmakefile b/Trading/GNUmakefile new file mode 100644 index 0000000..1ac80c5 --- /dev/null +++ b/Trading/GNUmakefile @@ -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 diff --git a/Trading/Trading.eomodeld/Customer.plist b/Trading/Trading.eomodeld/Customer.plist new file mode 100644 index 0000000..b84d361 --- /dev/null +++ b/Trading/Trading.eomodeld/Customer.plist @@ -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; + } + ); +} \ No newline at end of file diff --git a/Trading/Trading.eomodeld/CustomerGroup.plist b/Trading/Trading.eomodeld/CustomerGroup.plist new file mode 100644 index 0000000..44a8091 --- /dev/null +++ b/Trading/Trading.eomodeld/CustomerGroup.plist @@ -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; + } + ); +} \ No newline at end of file diff --git a/Trading/Trading.eomodeld/EOPostgreSQLPrototypes.plist b/Trading/Trading.eomodeld/EOPostgreSQLPrototypes.plist new file mode 100644 index 0000000..537f052 --- /dev/null +++ b/Trading/Trading.eomodeld/EOPostgreSQLPrototypes.plist @@ -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 = ( + ); +} \ No newline at end of file diff --git a/Trading/Trading.eomodeld/EOPostgres95Prototypes.plist b/Trading/Trading.eomodeld/EOPostgres95Prototypes.plist new file mode 100644 index 0000000..0c6da7e --- /dev/null +++ b/Trading/Trading.eomodeld/EOPostgres95Prototypes.plist @@ -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 = ( + ); +} \ No newline at end of file diff --git a/Trading/Trading.eomodeld/EOSQLite3Prototypes.plist b/Trading/Trading.eomodeld/EOSQLite3Prototypes.plist new file mode 100644 index 0000000..28e97dd --- /dev/null +++ b/Trading/Trading.eomodeld/EOSQLite3Prototypes.plist @@ -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 = ( + ); +} \ No newline at end of file diff --git a/Trading/Trading.eomodeld/Order.plist b/Trading/Trading.eomodeld/Order.plist new file mode 100644 index 0000000..10214d7 --- /dev/null +++ b/Trading/Trading.eomodeld/Order.plist @@ -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; + } + ); +} \ No newline at end of file diff --git a/Trading/Trading.eomodeld/OrderPos.plist b/Trading/Trading.eomodeld/OrderPos.plist new file mode 100644 index 0000000..c88c03e --- /dev/null +++ b/Trading/Trading.eomodeld/OrderPos.plist @@ -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; + } + ); +} \ No newline at end of file diff --git a/Trading/Trading.eomodeld/PriceList.plist b/Trading/Trading.eomodeld/PriceList.plist new file mode 100644 index 0000000..2a23794 --- /dev/null +++ b/Trading/Trading.eomodeld/PriceList.plist @@ -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; + } + ); +} \ No newline at end of file diff --git a/Trading/Trading.eomodeld/PriceListPos.plist b/Trading/Trading.eomodeld/PriceListPos.plist new file mode 100644 index 0000000..b35c7a6 --- /dev/null +++ b/Trading/Trading.eomodeld/PriceListPos.plist @@ -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; + } + ); +} \ No newline at end of file diff --git a/Trading/Trading.eomodeld/Product.plist b/Trading/Trading.eomodeld/Product.plist new file mode 100644 index 0000000..5799342 --- /dev/null +++ b/Trading/Trading.eomodeld/Product.plist @@ -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; + } + ); +} \ No newline at end of file diff --git a/Trading/Trading.eomodeld/ProductGroup.plist b/Trading/Trading.eomodeld/ProductGroup.plist new file mode 100644 index 0000000..0f794ce --- /dev/null +++ b/Trading/Trading.eomodeld/ProductGroup.plist @@ -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; + } + ); +} \ No newline at end of file diff --git a/Trading/Trading.eomodeld/Supplier.plist b/Trading/Trading.eomodeld/Supplier.plist new file mode 100644 index 0000000..cbdd5c8 --- /dev/null +++ b/Trading/Trading.eomodeld/Supplier.plist @@ -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; + } + ); +} \ No newline at end of file diff --git a/Trading/Trading.eomodeld/index.eomodeld b/Trading/Trading.eomodeld/index.eomodeld new file mode 100644 index 0000000..5d7ac74 --- /dev/null +++ b/Trading/Trading.eomodeld/index.eomodeld @@ -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"; + }; + }; +} diff --git a/Trading/Trading.eomodeld/procName.storedProcedure b/Trading/Trading.eomodeld/procName.storedProcedure new file mode 100644 index 0000000..8b06554 --- /dev/null +++ b/Trading/Trading.eomodeld/procName.storedProcedure @@ -0,0 +1,15 @@ +{ + arguments = ( + { + allowsNull = Y; + columnName = ""; + externalType = varchar; + name = argument; + parameterDirection = 1; + valueClassName = NSString; + width = 20; + } + ); + externalName = externalProcName; + name = procName; +} \ No newline at end of file diff --git a/Trading/Trading.h b/Trading/Trading.h new file mode 100644 index 0000000..7a8e9d3 --- /dev/null +++ b/Trading/Trading.h @@ -0,0 +1,34 @@ +#include +#include +#include + +@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 diff --git a/Trading/Trading.m b/Trading/Trading.m new file mode 100644 index 0000000..e691cf2 --- /dev/null +++ b/Trading/Trading.m @@ -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 +#include +#include + +@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 + + diff --git a/Trading/TradingData.m b/Trading/TradingData.m new file mode 100644 index 0000000..a7ea65c --- /dev/null +++ b/Trading/TradingData.m @@ -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 diff --git a/Trading/create.m b/Trading/create.m new file mode 100644 index 0000000..bef1014 --- /dev/null +++ b/Trading/create.m @@ -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; +}