. .

st4u

ST 4U 170: Using Sequence Numbers with Glorp

December 14, 2011 9:59:21.327

Today's Smalltalk 4 You looks at how to use sequence numbers as part of your table description with Glorp; in the process of doing that, we modify our simple table description, drop the table from Oracle, and then add it back. Finally, we add new data and check that the sequence number was used as expected. 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:

Sequence Numbers.

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 make some changes to our simple example - adding a real primary key. That key will be a sequence number in the database, so we'll have to do the following:

  • Drop the old table
  • Modify the descriptor and mapping class in Smalltalk
  • Add the table back to the database
  • We'll then add some data to the table again to see whether our sequence generator works as expected. First, add an "id" variable to class Emp, and then make the following three changes to class EmpDescriptor:

    
    classModelForEMP: aClassModel
    	aClassModel newAttributeNamed: #id.
    	aClassModel newAttributeNamed: #firstName.
    	aClassModel newAttributeNamed: #lastName.
    
    
    
    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').
    	(aDescriptor newMapping: DirectMapping) 
    		from: #id
    		to: (table fieldNamed: 'ID'). 
    
    tableForEMP: aTable 
    	(aTable createFieldNamed: 'ID' type: platform sequence) bePrimaryKey.
    	(aTable createFieldNamed: 'first_name' type: (platform varChar: 50)).
    	aTable createFieldNamed: 'last_name' type: (platform varChar: 50).
    
    

    Note two things: the type of our id variable is "sequence", and we made sure that it's the primary key. Now let's drop the table (in a separate transaction from where we'll add it back)

    
    "we have updated the table description - need to drop the old table and re-add. 
    If that's not practical, mod it with db tools"
    accessor dropTables: session system allTables.
    
    "now create with the sequence"
    session inTransactionDo: 
    [
    	session system platform areSequencesExplicitlyCreated 
    	ifTrue: 
    	[session system allSequences do: 
    		[:each | 
    		accessor createSequence: each
    		ifError: [:error | Transcript show: error messageText]]].	
    session system allTables do: 
    	[:each | 
    	accessor createTable: each
    	ifError: [:error | Transcript show: error messageText]]]
    
    
    

    If you turned SQL logging on, you'll see the transactions in the Transcript. Now let's add some data - note that we do not set the id - Glorp handles that for us using Oracle facilities:

    
    "Now insert one"
    emp := Emp new.
    emp firstName: 'Fred'.
    emp lastName: 'Flintstone'.
    
    
    "insert"
    session beginUnitOfWork.
    session registerAsNew: emp.
    session commitUnitOfWork.
    
    

    Using SQLPlus, let's have a look:

    New data with PK

    And that about wraps it up for today

    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: , ,

Enclosures:
[st4u170-iPhone.m4v ( Size: 7226845 )]

posted by James Robertson

 Share Tweet This