dtl


variant_row

Category: utilities Component type: type

Description

The variant_row class provides a mechanism to hold an arbitrary number of fields with values of arbitrary types. This class is used primarily by dynamic queries where the number and the types of the fields returned by a query are unknown.

Definition

Defined in the variant_row.h header file.

Refinement of

None.

Associated types

variant_field, TypeTranslation.

Example:

// Manipulating fields in a variant_row

void variant_row_example(void) {

	TIMESTAMP_STRUCT test_date = {1999, 9, 29, 0, 0, 0, 0};

	vector<TypeTranslation> types;
	vector<string> names;
	int i;
	string s;
	TypeTranslation vt0=TypeTranslation(typeid(int).name(), C_INT, SQL_INTEGER, SQL_C_SLONG,
				TypeTranslation::TYPE_PRIMITIVE, sizeof(int)), 
			vt1=TypeTranslation(typeid(string).name(), C_STRING, SQL_VARCHAR, SQL_C_CHAR,
				TypeTranslation::TYPE_COMPLEX, sizeof(string));

	types.push_back(vt0);
	names.push_back("int");

	types.push_back(vt1);
	names.push_back("string");

	variant_row r(types, names);
	
	
	r["int"] = (int)r["int"] + 5;
	i = (int)r["int"];
	
	s = (string) r["int"];

	r["int"] = test_date;
	s = (string) r["int"];

	// Print out the column names
	vector<string> colNames = r.GetNames();
	for (vector<string>::iterator name_it = colNames.begin(); name_it != colNames.end(); name_it++)
	{
		cout << (*name_it) << " ";
	}
	cout << endl;

	// Print out all column values
	for (i = 0; i < r.size(); i++)
		cout << r[i] << " ";
	cout << endl;


};

Public base classes

None.

Notation

X A type that is a model of variant_row
a Object of type X

Expression semantics

Name Expression Precondition Semantics Postcondition
Default constructor
X a()
  Creates an empty variant_row. The type and number of fields is not set so an empty variant_row cannot be used until is has been copy or assignment constructed from another row. The row is empty.
Alterante constructor
X a(vector<TypeTranslation> 
  &types, vector<string> &names)
  Creates a variant_row to hold an array of types as specified in types with field names given by names. In practice, you should never need to use this. Instead we recommend that you get any raw rowbuf object that you need by calling the DataObj() method of DynamicDBView or DynamicIndexedDBView to get a variant_row constructed for you that matches the columns in your query. See [1] for details about TypeTranslation. The row is initialized to be able to hold the given types and access these fields using the specified names.
Copy constructor
X a(const X &b)
  Creates a variant_row to hold an array of types as specified in b. The types, field names and values stored in b are copied into a. The row is initialized to be able to hold the types and fields given in b. The data held in b is copied to a.
Get field types
vector<TypeTranslation> GetTypes
  Returns a vector listing the type of each field in variant_row in the order that these fields occur. See [1] for details about TypeTranslation.  
Get field names
vector<string> GetFields()
  Returns a vector listing the name of each field in variant_row in the order that these fields occur.  
Get number of fields
size_t size()
  Returns the number of fields in the variant_row.  
Get field by number
variant_field operator[](int i)
  Retrieves field number i from the variant_row object. Fields are numbered starting at 0. The field is returned as a variant_field object.  
Get field by name
variant_field operator[](const string &f)
  Retrieves the field with the name given by the string f from the variant_row object. Fields are numbered starting at 0. The field is returned as a variant_field object.  

Notes

[1] TypeTranslation is an internal structure that is used by the library to translate between C types and SQL types. Details of the mapping that we use may be found in bind_basics.h and bind_basics.cpp. Typically, one would not read types directly off of this vector but would instead use type information functions exposed by the variant_field object.

See also

variant_field, DynamicDBView, DynamicIndexedDBView


[DTL Home]

Copyright © 2001, Michael Gradman and Corwin Joy.

Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appears in all copies and that both that copyright notice and this permission notice appear in supporting documentation. Corwin Joy and Michael Gradman make no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.