Send to Printer


ST 4U 162: Using a Glorp Descriptor

November 23, 2011 9:54:25.655

Today's Smalltalk 4 You starts making use of of the Glorp code we wrote here. 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 Use.

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 start using our basic Glorp setup to insert and query data from Oracle. To start out, we need to login and get a session (this uses code we created in the last screencast).

login := Login new
database: OracleODBCPlatform new;
username: username;
password: password;
connectString: 'orcl11g';

"login and logout"
accessor := DatabaseAccessor forLogin: login.
accessor login.

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

Now we can create an instance and have it inserted into the database. In the code below, ou would normally use #register: - I used #registerAsNew: to ensure that Glorp would treat my object as a new one bound of the database:

"Now insert one"
emp := Emp new.
emp firstName: 'Fred'.
emp lastName: 'Flintstone'.

session beginUnitOfWork.
session registerAsNew: emp.
session commitUnitOfWork.

Note the beginning and ending of the transaction. Now, let's use a query to get the data back:

accessor := DatabaseAccessor forLogin: login.
accessor login.

emps := session readManyOf: Emp.

It's worth looking at class GlorpSession for the full API range of what you can do; we'll be exploring some of that in future screencasts. Below are two images - a screencap of the inspector on the queried data, and a sqlplus wind showing the same thing:



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

[st4u162-iPhone.m4v ( Size: 3998020 )]

posted by James Robertson

 Share Tweet This