MYSQL.query_to_temp_class — maps query results to instance variables of a temporary class.
MYSQL.query_to_temp_class (superclass, query_string)
The superclass for the newly created class, or nil.
A string containing a well-formed SQL query.
This method of the MYSQL class emits a query query_string, to the database represented by MYSQL, collects the results, and constructs a temporary Gamma class whose instance variables correspond to the columns of the resulting query. This function is useful when the query is unique, and when the class that describes the rows does not need to persist once this query has been handled. The resulting class may be named the same as previous temporary classes, and it is possible that instances of more than one temporary class can be in use at the same time. It is not possible to accurately determine the class of an instance resulting from this function by name. To determine the class of an instance, use the Gamma function class_of.
The return value is an array of instances, one instance per row produced by the query. These instances are the only instances of their class. It is possible to determine whether two instances were generated from the same query by comparing their classes:
class_of(instance1) == class_of(instance2)
The superclass argument specifies whether the temporary class will be derived from an existing class. This can be useful to automatically attach methods to the temporary class through inheritance.
This method is defined in the Gamma library /usr/cogent/require/MySQLSupport.g. It is made available to a Gamma program with the statement: require("MySQLSupport");.
This example is taken from Section 2.2, “Tutorial 2: create_pets.g”
... /* Test the MYSQL.query_to_temp_class method. */ query = mysql.query_to_temp_class (mypetclass, "select * from pets where sex = 'm'"); pretty_princ ("\nQUERY The .query_to_temp_class method applied ", "to males in mypetclass returns:\n", query, "\n"); pretty_princ ("The name of the first pet is: ", query[0].name, "\n"); ...
The output for this section of the code is as follows:
QUERY The .query_to_temp_class method applied to males in mypetclass returns: [{TempQuery (birth . 1994-03-17) (death) (id . 2) (name . Claws) (owner . Gwen) (sex . m) (species . cat)} {TempQuery (birth . 1990-08-27) (death) (id . 4) (name . Fang) (owner . Benny) (sex . m) (species . dog)} {TempQuery (birth . 1995-08-31) (death . 2002-07-29) (id . 5) (name . Bowser) (owner . Diane) (sex . m) (species . dog)} {TempQuery (birth . 1996-04-29) (death) (id . 8) (name . Slim) (owner . Benny) (sex . m) (species . snake)}] The name of the first pet is: Claws
Copyright © 1995-2010 by Cogent Real-Time Systems, Inc. All rights reserved.