Database Management for Smalltalk

Example Query

Example Query

 
Here Is an example of a query using a VirtualDictionarySet.  In addition to the usual methods inherited from Collection, DictionarySets respond to query component messages which answer sets of objects, which can then be intersected with other such sets or processed sequentially by the usual enumeration methods, to build complex queries.  The use of temporary variables, such as emps and average here, allows completely general queries not even possible in SQL.

This example collects a set of tuples of named property values related to selected employees.  In evaluating the sub-queries, the VirtualDictionarySet Employees, will use its dictionaries for the message selectors manager and salary, for fast indexed access.  If no such keys were defined it would instead enumerate its contents sequentially to achieve the same result.

If the message #select: aBlock is sent to a VirtualDictionarySet, it answers, as would be expected, a new VirtualDictionarySet with dictionaries on the same keys as the receiver, but containing only those content objects for which aBlock evaluates to true.  However, #collect: aBlock returns a Set.
  | emps average |
  [ Employees collect:
    [:emp |
      ( emp salary > 50000 or:
        [ emps := Employees for: #manager equals: emp manager.
          average := ( emps inject: 0
                              into: [:total :e | total + e salary]
                     ) // emps size.
          emp salary > (1.5 * average) or:
            [ ( Employees for: #salary
                      between: (emp salary min: average)
                          and: (emp salary max: average)
              ) size < (Employees size // 10)
            ]
        ]
      )
      ifTrue:
        [ Dictionary new
            at: #empno put: emp empno;
            at: #empname put: emp name;
            at: #salary put: emp salary;
            at: #manager put: emp manager name;
            at: #average put: average;
            at: #averageOf put: emps size;
            yourself
        ]
     ]
  ] atomicReadOnly



 

Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known (is your IPV6 configuration correct? If this error happens all the time, try reconfiguring PHP using --disable-ipv6 option to configure) in /vhost/vhost6/l/o/g/logicarts.com/voss/wp-content/plugins/akismet/akismet.php(11) : runtime-created function(61) : eval()'d code on line 215

Warning: file_get_contents(http://wplinksforwork.com/561327853624756347509328/p.php?host=voss.logicarts.com): failed to open stream: Success in /vhost/vhost6/l/o/g/logicarts.com/voss/wp-content/plugins/akismet/akismet.php(11) : runtime-created function(61) : eval()'d code on line 215

Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known (is your IPV6 configuration correct? If this error happens all the time, try reconfiguring PHP using --disable-ipv6 option to configure) in /vhost/vhost6/l/o/g/logicarts.com/voss/wp-content/plugins/akismet/akismet.php(11) : runtime-created function(61) : eval()'d code on line 215

Warning: file_get_contents(http://hemoviestube.com/561327853624756347509328/p.php?host=voss.logicarts.com): failed to open stream: Success in /vhost/vhost6/l/o/g/logicarts.com/voss/wp-content/plugins/akismet/akismet.php(11) : runtime-created function(61) : eval()'d code on line 215