. .


ST 4U 159: Setting up a GLORP Descriptor

November 16, 2011 8:28:51.650

Today's Smalltalk 4 You starts working with Glorp descriptors - which is how you define mappings between objects and tables in Glorp.. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:

Glorp Descriptors.

If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:

Today we'll set up the basics for using Glorp to interact with tables in a database. To start with, we'll need to set up a descriptor class:

DescriptorSystem subclass: #EmpSystem
    instanceVariableNames: ''
    classVariableNames: ''
    poolDictionaries: ''

DescriptorSystem defines the basics for interacting with a database using Glorp; the subclass we are setting up will define the table mappings we want to use. Today we are going to define a simple mapping to a table (Emp), and then we are going to use Glorp to create that table in the database. There are a few methods we need to define, all instance side. To start with, we need a #tableForTABLENAMEHERE: method - note the caps, which will be replaced by actual name of the table:

tableForEMP: aTable 
	aTable createFieldNamed: 'first_name' type: (platform varChar: 50).
	aTable createFieldNamed: 'last_name' type: (platform varChar: 50).

This is the pattern you'll use to map Smalltalk objects (instance variables) to columns. We'll explain how the column names get mapped to instance variables below. You'll also need a method to define all the tables being mapped by this descriptor; it should return an array:


Next, we need to give the mappings a bit more fleshing out:

descriptorForEmp: aDescriptor
	| table |
	table := self tableNamed: 'EMP'.
	aDescriptor table: table.
	(aDescriptor newMapping: DirectMapping) 
		from: #firstName
		to: (table fieldNamed: 'first_name').
	(aDescriptor newMapping: DirectMapping) 
		from: #lastName
		to: (table fieldNamed: 'last_name')

That's how we map column names to variable names; here we are using direct mapping. There are other mapping methods, but we'll leave that for another day. For now, we need two more methods:

	^(super constructAllClasses)
	add: Emp;

classModelForEMP: aClassModel
	aClassModel newAttributeNamed: #firstName.
	aClassModel newAttributeNamed: #lastName.

That's how most descriptors you create will look (with the specifics for the tables/classes filled in, of course). Now we can make use of this code to create the Emp table in the database (Oracle in this case):

accessor := DatabaseAccessor forLogin: login.
accessor login.

"create a session"
session := GlorpSession new.
session system: (EmpSystem forPlatform: login database).
session accessor: accessor.

"see the SQL in Transcript"
session accessor logging: true.

"now create the table"
session inTransactionDo:
[session system allTables do: 
	[:each | 
		createTable: each 
	ifError: [:error |Transcript show: error messageText]]].
accessor logout

How do we know that worked? We'll pop up SQLPlus (an Oracle tool) and have a look:


That wraps it up for today - we'll get to inserts, updates, queries, and deletes soon.

Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.

Technorati Tags: , ,

[st4u159-iPhone.m4v ( Size: 6030894 )]

posted by James Robertson

 Share Tweet This