PATH |
Explains how to determine table and column information from the database server using the adaptor sublayer.
You can read schema information from the database server with the EOAdaptorChannel "describe" methods:
The describeTableNames method reads and returns an array of table names from the database. The describeModelWithTableNames method constructs an EOModel out of the database's metadata, and assigns the adaptor name and connection dictionary to the new EOModel. You generally invoke describeModelWithTableNames with the array of table names (or a subset) returned from describeTableNames . The describeStoredProcedureNames method reads an array of stored procedure names from the database. You can then use addStoredProceduresNamed (addStoredProceduresNamed:toModel: in Objective-C) to add the corresponding stored procedures to a model. The following code shows how to open an adaptor and get the database schema from it.
EOAdaptor myAdaptor = EOAdaptor.adaptorWithName("OpenBaseLite"); EOAdaptorContext myContext = myAdaptor.createAdaptorContext(); EOAdaptorChannel myChannel = myContext.createAdaptorChannel(); /* The connection dictionary is adaptor and database dependent */ NSMutableDictionary connDict = new NSMutableDictionary(); connDict.setObjectForKey("Movies","DATABASENAME"); connDict.setObjectForKey("/Local/Library/Databases","PATH"); myAdaptor.setConnectionDictionary(connDict); try { myAdaptor.assertConnectionDictionaryIsValid(); } catch (NSException exception) { /* handle bad connection dictionary */ } myChannel.openChannel(); NSArray tableNames = myChannel.describeTableNames(); EOModel myModel = myChannel.describeModelWithTableNames(tableNames); NSArray storedProcNames = myChannel.describeStoredProcedureNames(); myChannel.addStoredProceduresNamed(storedProcNames,myModel); myChannel.closeChannel();
The code first builds a connection dictionary to connect to the database with. Then it attempts to verify the connection dictionary using assertConnectionDictionaryIsValid . If the connection attempt fails, the method raises an exception. Otherwise the code opens the connection to the database and gets the table names and stored procedures.
Note that the keys for the connection dictionary depend on the particular adaptor and database server you are using. You can view the connection dictionary for a database using EOModeler in the following way.
Build a model for your database in EOModeler by invoking the File->New menu item.