This document provides examples of how to use the Object Translator Visual Basic runtime component to perform the following tasks:
Dim conn As New ORMapperConnection
To connect using ODBC, use this code:
conn.DBMS = "ODBC"
conn.DataSource = "your_datasource_name"
conn.Database = "your_database_name"
conn.UID = "your_user_ID"
conn.Password = "your_password"
conn.AutoCommit = True
' transaction type
conn.Connect
To connect using OLEDB Provider for Informix, use this code:
conn.Provider = "Ifxoledbc.2"
conn.DataSource = your_database@your_server
conn.UID = "your_user_ID"
conn.Password = "your_password"
conn.AutoCommit = True
' transaction type
conn.connect
' Instantiate object of type Customer
Dim oCust as new Customer
'Instantiate object of type CustomerOID
Dim oCustOID as new CustomerOID
' Set the CustomerOID object's customer_num property
to 100
oCustOID.customer_num = 100
' Assign the CustomerOID object to the Customer
object's OID property
oCust.oid = oCustOID
oCust.connection = conn
oCustOID.customer_num = 100
oCust.oid = oCustOID
oCust.Restore
To retrieve the data for individual properties (for example, to populate user-interface items), use the Customer object's getter functions:
Text1.text = oCust.customer_num
Text2.text = oCust.name
Dim oCustColl as CustomerCollection
oCustColl.Restore(conn, "customer_num > 100")
Use the FirstObject,
NextObject,
PreviousObject,
and LastObject functions to navigate among the individual Customer
objects in the CustomerCollection object.
oCust.customer_num = 100
oCust.name = "Robert"
oCust.Store
oCustOID.customer_num = 100
oCust.oid = oCustOID
oCust.Restore
oCust.name = "Sam"
oCust.Store
You can achieve the same result by calling the MarkChanged function with a value of True. Note that when you use the MarkChanged function, you do not use an instance of the CustomerOID object.
oCust.customer_num = 100
oCust.MarkChanged(True)
oCust.name = "Sam"
oCust.Store
oCust.destroy
If you prefer to perform manual transactions, then you must set the connection object's AutoCommit property to False and provide the database-server transaction logic, as shown in the following example:
' Instantiate a transaction object
Dim oTrans As ORMapperTransaction
' Instantiate a connection object
Dim conn As New ORMapperConnection
' Connect to the database using ODBC
conn.DBMS = "ODBC"
conn.DataSource = "books"
conn.Database = "books"
conn.UID = "informix"
conn.Password = "informix"
' Set the transaction type to False, for manual
conn.AutoCommit = False
conn.Connect
' Begin the manual transaction
Set oTrans = conn.CreateTransaction
oTrans.Begin
oCust.Connection = conn
' Insert data into the database
oCust.customer_num = 100
oCust.name = "Robert"
oCust.store
' Commit the transaction
oTrans.Commit
' Disconnect from the database
conn.Disconnect
Set conn = Nothing
You use the Object Translator GUI to generate the objects, and to specify that they are in a collection relationship. While defining each parent (Customer and Order), you can set their RestoreType, StoreType, and DestroyType properties to Deep or Shallow. If you choose Deep for both parent objects, then when your application calls the Customer object's Restore, Store, or Destroy methods, Object Translator also performs those operations on the Order and OrderLine objects. (For information on using the Object Translator GUI, see the online help provided with the GUI.)
The examples in this section show that you can override the GUI property settings (RestoreType, and so on) to fit your application's needs, by setting the DeepRestore, DeepStore, and DeepDestroy properties to True or False.
This section describes how to work with collection objects in the following ways:
Dim oCust As new Customer
Dim oCustOID As New CustomerOID
Dim oOrders as new Orders
Dim oOrdColl as new OrdersCollection
Dim oOrdLns as new OrderLines
Dim oOrdLnsColl as new OrderLinesCollection
oCust.connection = conn
In the following code, setting the Customer object's DeepRestore property to True means that data is also retrieved for the Order objects when you call the Customer object's Restore method. If you set the property to False, then data is retrieved for a particular object only when you call the getter function of that object.
oCustOID.customer_num = 26
oCust.oid = oCustOID
oCust.DeepRestore = True
oCust.Restore
To retrieve the data for individual properties (for example, to populate user-interface items), use the getter functions:
Text1.text = oCust.name
This code retrieves an OrdersCollection object:
Set oOrdColl = oCust.Orders ' Returns OrdersCollection object
Use the FirstObject, NextObject, PreviousObject, and LastObject functions to navigate among the individual objects contained in the Orders collection object. For example, to retrieve the first Orders object, use this code:
Set oOrders = oOrdColl.FirstObject
Then you can retrieve data from that Orders object using a getter function:
Text1.text = oOrders.order_num
oCust.Customer_num = 100
oCust.name = "Robert"
oOrders.Order_num = 1000
oOrders.customer_num = 100
oOrders.order_date = "03-03-1999"
oOrders.paid_date = "04-04-1999"
oOrdLns.ol_pk = 2000
oOrdLns.Order_num = 1000
oOrdLns.Qty_Ordered = 89
oOrdLns.Qty_Shipped = 78
You can use the AddObject function to add any number of OrderLines objects to an OrderLinesCollection object, as follows:
oOrdLnsColl.AddObject(oOrdLns)
The following code adds Orders objects to an OrdersCollection object and assigns an OrdersCollection object to a Customer object. Because the DeepStore properties of the Customer object and the Orders object are set to True by default, calling the Customer object's Store method inserts the data for the Customer object, its Orders objects, and their OrderLines objects into the database:
oOrdColl.addObject(oOrders)
oCust.orders = oOrdColl
oCust.store
To insert the data into only the Customer object (and not any collection objects it contains), use this code:
oCust.deepStore = False
oCust.store
oCustOID.customer_num = 26
oCust.DeepRestore = True
oCust.Restore
oCust.name = "Pete"
The following code shows how to retrieve an OrdersCollection object, update the first Orders object, update the first OrderLines object, and write all the updated data for these objects to the database:
Set oOrdColl = oCust.Orders
Set oOrders = oOrdColl.FirstObject
If Not oOrders Is Nothing Then
oOrders.order_date =
"04-09-1999"
oOrders.paid_date =
"05-10-1999"
End If
Set oOrdLnsColl = oOrders.OrderLines
Set oOrdLns = oOrdLnsColl.FirstObject
If Not oOrdLns Is Nothing Then
oOrdLns.Order_num =
1000
oOrdLns.Qty_Ordered
= 89
oOrdLns.Qty_Shipped
= 78
End if
oCust.store
' update the database
oCustOID.customer_num =
26
oCust.oid = oCustOID
oCust.DeepRestore = True
oCust.Restore
Set oOrdColl = oCust.Orders
Set oOrders = oOrdColl.FirstObject
While Not oOrders Is Nothing
oOrders.DeepDestroy = True
Set oOrdLnsColl = oOrders.Items
Set oOrdLns = oOrdLnsColl.FirstObject
While Not oOrdLns Is Nothing
Set oOrdLns = oOrdLnsColl.NextObject
Wend
Set oOrders = oOrdColl.NextObject
Wend
oCust.DeepDestroy = True
oCust.Destroy
This section describes how to perform the following tasks:
Dim oCust As new Customer
Dim oCustOID As New CustomerOID
Dim oAddress as Address '
Embedded object of oCust
oCust.connection = conn 'Assigning
the connection object
The following code shows how to retrieve the data for the Customer object with a customer_num property of 26:
oCustOID.customer_num = 26
oCust.oid = oCustOID
oCust.Restore
To retrieve the data for individual properties (for example, to populate user-interface items), use the object's getter functions:
text1.text = oCust.customer_num
Use this code to retrieve the embedded object's data:
set oAddress = oCust.Address
text2.text = oAddress.city
text3.text = oAddress.state
If Address is Nothing Then
set oAddress = new Address
oCust.Customer_num =
100
oCust.name = "Robert"
oAddress.city = "Oakland"
oAddress.state = "CA"
oCust.Address = oAddress
oCust.store
End If
oCustOID.customer_num = 26
oCust.oid = oCustOID
oCust.Restore
oCust.name = "Pete"
oAddress = oCust.Address
oAddress.city = "New York"
oAddress.state = "NY"
oCust.store
oCustOID.customer_num = 26
oCust.oid = oCustOID
oCust.destroy
Last updated June 19, 2000.