From Dabo Wiki
Revision as of 14:15, 11 May 2020 by Ed (talk | contribs) (Created page with "Dabo cursor objects hold their data in DataSet objects. Internally, the class dabo.db.dDataSet is a tuple of dictionaries, where each element of the tuple represents a row in...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Dabo cursor objects hold their data in DataSet objects. Internally, the class dabo.db.dDataSet is a tuple of dictionaries, where each element of the tuple represents a row in the data; each row is represented by a dictionary with each key-value pair being a column name and its value in that row. You can get a DataSet by calling a bizobj's getDataSet() method. For those of you coming from a VisualFoxPro background, you can think of them as you would a Fox cursor.

The cool thing about DataSets is that they understand SQL commands. You can run a SQL Select statement against the DataSet object, and a new DataSet containing the result of executing that statement is returned. The data in the original DataSet is unchanged.

Since you aren't querying an actual table, what do you use for the FROM clause? The convention we've adopted is to simply use dataset in the FROM clause as the name of the source table. Like all SQL, this is not case-sensitive.

You aren't limited to a single DataSet, by the way. You can do multi-DataSet joins quite easily. The only catch is that you have to decide what alias names to use for each of the additional DataSets. Then pass a dictionary containing your alias:DataSet as the key:value pairs.

 1 custDS = customerBizobj.getDataSet()
 2 ordersDS = ordersBizobj.getDataSet()
 3 ordItemsDS = orditemsBizobj.getDataSet()
 6 # If you are in an onHit() method of a GUI control:
 7 # custDS = self.Form.getBizobj(dataSource='name_of_table').getDataSet()
10 # Get all customers whose company name begins with 'ST'
11 ds = custDS.execute("select * from dataset where company like 'ST%' ")
14 # Get several columns from the Orders table 
15 ds = ordersDS.execute("""select order_net, shipped_on as shipDate 
16         from dataset
17         where order_net >= 1000
18         order by shipDate desc""")
21 # Join all three DataSets together
22 aliasDict = {"ord" : ordersDS, "orditem" : ordItemsDS}
23 ds = custDS.execute("""select,, 
24         ord.shipped_on, ord.order_net, orditem.unit_price, orditem.quantity, 
25         (orditem.unit_price * orditem.quantity) as itemTotal
26         from dataset join ord
27                 on dataset.pkid = ord.cust_fk
28         join orditem 
29                 on ord.pkid = orditem.order_fk
30         where = 'Franchi S.p.A.'
31         order by ord.order_date""", aliasDict)


You can replace the value of a field in a DataSet with a given value or expression by calling the replace() method of the DataSet, passing in the field name, the value/expression to replace that field with, and optionally a scope expression that will limit the affected records. The scope should evaluate to a boolean value for each row in the DataSet, and only those for which scope=True will be modified. The DataSet is modified in place; there is no return value from the replace() method.


You can obtain a sorted copy of a DataSet by calling its sort() method and passing in the field name to sort on. By default, the sort is done ascending and case-insensitive; you can modify those defaults by passing in either of two additional parameters: asdesc (ASC or DESC) and caseSensitive (bool).


DataSets have a filter() method that allows you to easily control the rows in the data without having to create and execute queries. Additionally, filters can be removed using the removeFilter() method; this will "unfilter" in the reverse order that the filters were applied to return to your original data. Example:

 1  ds = bizobj.getDataSet()
 2  print len(ds)
 3  ## prints 100
 4  ds = ds.filter("lastname", "S", "startswith")
 5  print len(ds)
 6  ## prints 24
 7  ds = ds.filter("income", 80000, ">")
 8  print len(ds)
 9  ## prints 6
10  ds = ds.removeFilter()
11  print len(ds)
12  ## prints 24
13  ds = ds.removeFilter()
14  print len(ds)
15  ## prints 100

There is also a removeFilters() (note the plural) that will remove all filters on a data set at once.

To apply a filter, pass in the field name to filter on, a matching expression, and (optionally) an operator. If no operator is supplied, the default of = will be used. You may also pass one of the following strings in, and the corresponding operator will be used:

Filter String Operator
eq =
equals =
gt >
gte >=
lt <
lte <=
startswith LIKE%
beginswith LIKE%
endswith  %LIKE
contains  %LIKE%