ActiveGrid Applications can access a variety of different databases, including MySQL, PostgreSQL, Oracle, DB2 and MS SQL Server. A SQLite database is installed by default with the ActiveGrid Studio for testing purposes. For a complete list of supported databases, refer to the
ActiveGrid Installation and Deployment Guide.
In order to interact with this wide spectrum of databases seamlessly, the ActiveGrid Studio provides the
Data Model. The
Data Model is based on XML Schema and you work with it in the Data Model Editor. The Data Model Editor provides a standard interface for creating and editing database tables, columns, keys and so on.
The Data Model provides a logical description of the data in a particular
data source. The data source is a named connection to a specific database. ActiveGrid Applications use a
Data Model based on the XML schema standard. The
Data Model files in your application end with the
.xsd suffix.
You can export your Data Model to your database and you can also import your database structures to your
Data Model. So you can make changes in both the
Data Model and the database, and then export or import them.
Each Data Model has an associated
data source, that identifies the specific database referenced by the data model. When you create a new
Data Model, you can use any data source that is currently registered in the ActiveGrid Studio or you can add a new data source through the
Data Sources configuration tab (
Tools > Options > Data Sources).
ActiveGrid’s Data Model uses complex types to represent the tables in a relational database . A complex type is a data structure that can contain multiple elements, each with its own data type. Complex types are part of the XML Schema standard. Each element in a complex type can itself be a simple type (representing a column in the table) or a complex type (representing a relationship to another complex type).
In addition to complex types, the Data Model uses elements, keys and key references from the
XML schema standard to represent the database structure. The following table shows how the database structure is represented in the
Data Model.L Schema, provides a logical description of the data in a database. This allows ActiveGrid Applications to access the data from different databases generically. The
Data Model represents a database as shown in the following table.
In general, we will refer to items in the Data Model according to database, rather than schema terminology. However, where this might be misleading, we will use schema terminology.
The ActiveGrid Data Service is a special service for making database calls. The
Data Service is different from other services in ActiveGrid Application because it does not have a
Service Definition or defined input and output messages.
The Data Service provides a variety of operations for manipulating data, including
save,
commit,
rollback,
delete and
insert. The
Data Service also includes the
getObject and
getObjects operations. The
getObject and
getObjects operations return a single complex type (
getObject) or a list of complex types (
getObjects). For a full reference to the
Data Service API, refer to
“API to the Data Layer: The DataService Class” on page 99.
You can call the Data Service in one of these ways:
ActiveGrid applications use data sources to access databases. A data source is simply a named reference to a particular database connection. To define a new data source, follow these steps:
2.
|
Click the Data Sources tab. The Data Sources panel appears. Here you can view and edit existing data sources or define new ones.
|
3.
|
Click the Add button. A new data source, named Untitled, appears in the Data Sources list.
|
•
|
Data Source Name: Type in a name for the data source. Note that this is not the server hostname, but simply an arbitrary name that you use to access the data source from the ActiveGrid Studio.
|
•
|
Database Type: Select the type of database from the pull-down list. The list contains all supported databases. Some of the other fields in the Selected Data Source area might be disabled once you select your database type. This is because different database types require different connection information. Similarly, the value you type in the Connection String field is different for different databases.
|
•
|
User Name: For all databases except SQLite, you should type in the user name that you use to access this database. SQLite databases do not require authentication so this field is disabled for SQLite.
|
•
|
Password: For all databases except SQLite, you should type in the password that you use to access this database. SQLite databases do not require authentication so this field is disabled for SQLite.
|
•
|
Connection String and Database Name: These fields identify the database instance. The exact values for these two fields depend on the type of database to which you’re connecting. The following table describes the connection string and database name for each database type.
|
The Data Model Editor provides a uniform interface for working with all the databases that your application accesses. To see the Data Model Editor, open an ActiveGrid Application and double-click on the
Data Model file (
.xsd) for the application.
In addition to the Project Panel, which displays all the files for the project, the Data Model Editor contains four major panels that allow you to work with different aspects of the
Data Model:
•
|
Tree View Panel: Shows and allows you to select items in the Data Model, such as tables, columns, keys and so on. When you select an element in the Tree View, its properties are displayed in the Property Editor. You can select primary and foreign keys in the Tree View and then edit their properties in theProperty Editor. ( The Tree View Panel).
|
•
|
Data Model Editor: Displays the tables and columns. Shows relationships as lines between items. If you select a table or an element, the properties for that item appear in the Property Editor ( The Data Model Editor).
|
•
|
Property Editor: Allows you to view and edit the properties for the selected item.
|
Each main box in the Data Model Editor represents a table. The smaller boxes within the table represent the columns for that table. The following items are represented with icons in the Data Model Editor:
The Tree View provides a hierarchical view of the Data Model. Click the plus icons (+) to expand an item and the minus icons (-) to collapse an item. All the keys for a particular table are listed under
keys.
The Property Editor shows the properties for the currently selected item. You can select items either in the Tree View Panel or in the Data Model Editor.
To see the properties for the Data Model itself, select the
Data Model (
.xsd) file in the Tree View Panel.
The Data Model has the following properties:
You can create a Data Model from scratch or you can use the ActiveGrid Studio to automatically generate a
Data Model. Once you have a
Data Model, you can modify it and then can export your modifications back to the database. You can also update the
Data Model with changes in the database.
To generate a Data Model from a data source (or to update an existing
Data Model from the data source), select
Tools > Import Database... from the ActiveGrid Studio main menu. This launches the
Import Database wizard, which walks you through the steps of naming the
Data Model, selecting a data source, and selecting which tables to import.
The Import Database wizard allows you to import different database schemas (for databases that support schemas) such as Oracle, DB2 and PostgreSQL. By default, the Import Database wizard imports the database schema associated with the connection information you specified when you defined the data source (
Defining a Data Source).
If you want to import a different schema, or multiple schemas, click the Change Database Schema button on the first screen of the Import Database Wizard. Enter a comma-delimited list of the schemas you want to import. Complete the Import Database wizard.
ActiveGrid Applications use standard WSDL documents to describe the stored procedures available in the database. Each stored procedure is represented by a web service operation. The input parameters are defined by a request message. The output parameters and cursors are defined by a response message.
You can use the ActiveGrid Studio to update the database with changes to the
Data Model. To update the database with changes from the
Data Model, select
Tools > Export Database... from the ActiveGrid Studio main menu. This launches the Select Data Source wizard, which walks you through the steps of naming the
Data Model, selecting a data source, and selecting which types to import.
The Data Model Editor allows you to create, delete and edit tables. You can then export your changes back to the database. Tables in the
Data Model can have a different name from the corresponding table in the database.
To create a table in the Data Model, right-click in the Data Model Editor and select
Insert Table... from the resulting pop-up menu. The Table Properties screen appears. Name the table and type in Plural and Singular labels for it, then click
Finish. The table appears in the
Data Model.
Data Model tables have the following properties, which you can set in the Property Editor.
To create a key on a table, right-click in the Data Model Editor and select Insert Key... from the resulting pop-up menu. The Key Description screen appears:
To see the properties for a key, select the key in Tree View of the Data Model Editor. You can set the following properties for keys in the Property Editor:
To see the properties for a foreign key, select the key in Tree View of the Data Model Editor. You can set the following properties for foreign keys in the Property Editor:
The columns that you can create in the Data Model do not have to be database columns. The
Data Model allows you to create pseudo-columns that are not written back to the database. When you create a new column, you choose what
kind of column you want:
•
|
Database Columns: A column in the Data Model that gets its value from a column in the database or from a computation involving an column or columns from the underlying table. Select: Database column or expression.
|
•
|
Computed columns: A column that is populated by a performing a calculation on data in another column. Computed Columns can be read-write or read only. Computed Columns are stored in the Data Model but are not part of the database. There are three types of computed column.
|
•
|
Computed with an XPath expression: A Computed Column that derives its value from an XPath expression. Computed Columns are stored in the Data Model but are not part of the database.
|
•
|
Computed with Custom Code: A Computed Column that uses a Python or PHP method to compute its value. Computed Columns are stored in the Data Model but are not part of the database.
|
•
|
Computed relationship to Another Table: This computed column uses a custom method to produce the result. It is also a relationship type column, but you don’t need a foreign key to link the tables for this type of column.
|
•
|
Relationship columns: A column that is populated with data from another table. In the Data Model Editor, relationship columns are indicated by a green double-arrow icon. Relationship Columns are stored in the Data Model but are not part of the database.
|
Database columns are columns in your Data Model that correspond directly to either a column in your database column or to an expression involving columns in your database.
In the Data Model, a Database Column represents a column in the associated database. The column in the
Data Model does not have to have the same name as the column in the database. Name the column anything you want in the
Data Model and then use the
Binding Property to bind the column to a specific column in the database.
The Binding property specifies the column in the database to which this (
Data Model) corresponds. When you set the Binding property, be sure to type in the name of the column exactly as it appears in the database.
2.
|
In the Column Name field, type in the name for this column in the Data Model. This name does not have to match the name of the corresponding database column.
|
3.
|
From the Table pull-down menu, select the Data Model table to which this column belongs.
|
4.
|
For Column Kind, click the Database Column or Expression radio button.
|
5.
|
From the Column Type pull-down menu, select the type of data that this Data Model column contains.
|
6.
|
Click Next. The Column Properties screen appears. Here you set the properties for this Data Model Column. These properties are described in Database Column Properties. At a minimum, you need to set the SQL Type, Length and Binding properties.
|
For Binding, type in the name of the column (as it appears
in the database) to which this
Data Model column corresponds.
In the Data Model, a Database Expression Column represents a calculation based on one or more columns in the associated database. Use the
Binding Property to specify the
database columns and operations.
To create a new Database Expression Column, follow thes steps for creating a Database Column in the
Data Model. Then, for the
Binding type your expression, including the names of any column that appear
in the database.
For example, suppose you have a database table called itemDB that is represented in the
Data Model by a table, called
itemDM. The itemDB table contains two columns, called
price and
cost. But the itemDM table actually contains three elements:
ListPrice (bound to the price column)
UnitCost (bound to the cost column) and
Margin (bound to the price and cost columns).
The Margin column in the Data Model is actually a calculation on the two database columns, price and cost.
This type of Data Model column derives
its
value from an expression involving other columns in the
Data Model, (rather than database columns). XPath-computed columns are stored in the
Data Model only. You cannot export them to the database. Use the
Binding Property to specify the XPath expression.
2.
|
In the Column Name field, type in the name for this column in the Data Model.
|
3.
|
From the Table pull-down menu, select the Data Model table to which this column belongs.
|
4.
|
For Column Kind, click the Computed with an XPath Expression radio button.
|
5.
|
From the Column Type pull-down menu, select the type of data that this Data Model column contains.
|
6.
|
Click Next. The Column Properties screen appears. Here you set the properties for this Data Model Column. These properties are described in Database Column Properties. At a minimum, you need to set the SQL Type, Length and Binding properties.
|
For Binding, you can type in
Data Model columns from the current table, XPath to columns from a related table, and XPath expressions.
For example, suppose you have a Data Model that contains two tables called
item and
product. Each of these tables has a column that represents a column in the database:
Now you want to add an column called ListPrice to the item table. The ListPrice item is the product of the UnitMarkup column from the product table and the UnitCost column from the item table. Since the calculation involves a column from another table, you need to use XPath to represent that column.
Notice that the binding for ListPrice references the markup column of the related product table. In order for this to work, the item table must have a column named
product that is a
relationship type (
Creating Keys in the Data Model).
Finally, if you want to add a calculated column that includes a binding to your XPath column, that new column must also be an XPath column. For example, suppose you create an column called
Margin, equal to the difference between the ListCost and the UnitMarkup. The Margin column must be bound to an XPath expression. This is because Margin must now be calculated with the ListPrice column, which is itself bound to an XPath expression.
The XPath expression is evaluated outside of the database and can make use of one-to-one relationships defined on the table containing the column. XPath columns are read-only.
This type of column is computed by a Python or PHP method defined in a separate file associated with the
Data Model. When you create a computed column the, Data Model Editor creates a method prototype for you in a Python or PHP file.
In the Dependent Columns: field, list all the elements that are used to compute the value. This is a comma-separated list of XPath expressions. For example, suppose the computation uses the UnitCost element from this complex type and the UnitMarkup element, from the prod complex type. In the
Dependent Columns field, you would type the following:
•
|
The code file has the same name as the Data Model file, but with the suffix xsd.py or xsd.php. So, for example, if the name of the Data Model is MyDataModel.xsd, then the code file is either MyDataModelxsd.py or MyDataModelxsd.php, depending on which language you selected. (Python files have the .py suffix and PHP files have the .php suffix).
|
Fill in the method implementation and save the code file. For example, your Python method might look something like this:
This is like a computed column as it has a custom method you implement to produce the result. The difference is that the type can be another table type, like a relationship.
2.
|
In the Column Name field, type in the name for this column in the Data Model.
|
3.
|
From the Table pull-down menu, select the Data Model table to which this column belongs.
|
4.
|
For Column Kind, click the Computed Relationship to Another Table radio button.
|
5.
|
From the Column Type pull-down menu, select the type of the table you’re referencing.
|
6.
|
Click Next. The Computed Relationship Properties screen appears. Here you set the properties for this Data Model Column:
|
•
|
In the Dependent Columns: field, list all the elements that are used to compute the value. This is a comma-separated list of XPath expressions. See Custom-Code Computed Columns.
|
•
|
In the Multiplicity: field, select the multiplicity of the relationship you’re creating.
|
7.
|
Click Finish. A code file opens with a skeleton method for the column. Complete the method and save the file.
|
cheapestPrice = sys.maxint
for item in product.items:
if item.listprice < cheapestPrice:
cheapestPrice = item.listprice
The Relationship kind of column references another table in the
Data Model. In this case the
type of the element is that of the referenced complex type.
In order to create a relationship element, there must already exist a foreign key on the table. The choices of multiplicity depend on which side the foreign key is placed. See
Creating a Relationship Between Tables.
2.
|
In the Column Name field, type in the name for this column in the Data Model.
|
3.
|
From the Table pull-down menu, select the Data Model table to which this column belongs.
|
4.
|
For Column Kind, click the Relationship to Another Table radio button.
|
5.
|
From the Column Type pull-down menu, select the table you’re referencing.
|
6.
|
Click Next. The Relationship Properties screen appears. Here you set the properties for this Data Model Column:
|
•
|
In the Foreign Key: field, select the foreign key you’re using for this relationship.
|
•
|
In the Multiplicity: field, select the multiplicity of the relationship you’re creating.
|
To establish a relationship between tables, both tables must have keys and there must be a foreign key establishing a relationship between the two tables (
Creating Foreign Keys). You can then create a relationship column (
Working with Relationship Columns) in one table that references the other table.
For clarity, we’ll describe this process by example. Assume that we have a
Data Model that contains two tables:
Product and
ProductCategory. Further, assume that the Product table contains the following columns and primary key:
We want to create a column called productsInCategory, in the
productCategory table. The productsInCategory column contains the list of products that belong to this category. In order to populate this list, we establish a relationship between the two tables,
productCategory and
product:
This creates the products column in the ProductCategory table. The products column is actually a relationship column referencing the
product table.
The Inspecting:Fields screen appears.
•
|
Random number: The random algorithm generates a random number between the configured “minRandom” and “maxRandom” values. The number sequence can be initialized by specifying “randomSeed”.
|
•
|
Timestamp: The timestamp algorithm generates a value from the current UTC date/time using a format specification defined by the implementation language. For Python this is defined by the time.strftime function. For PHP this is defined by the date function.
|
An extension to the timestamp formatting directives is %q which formats the fractional seconds (microseconds) of the current time. The default format returns a number with year, Julian day, hour, minute, second, and microsecond.
•
|
Sequence: This is a database-generated unique sequence. The sequence algorithm retrieves the next value generated by the database sequence generator named by the “sequenceName” for the field. Databases supporting sequences include: Oracle, DB2, and PostgreSQL. For MySQL, you need to use AUTO_INCREMENT columns ( “Generating New Key Values Using MySQL AUTO_INCREMENT Columns”).
|
The key generation service can be configured at deployment time, by editing the deployment file. Configuration parameters include the following:
MySQL does not support database sequences to generate new key values. It does support AUTO_INCREMENT columns however. To generate key values using the AUTO_INCREMENT column, follow these steps: