Set up a SQL property

To generate code from a database table, the template needs metadata about that table, supplied through a property that refers to it. CodeSmith Generator includes the SchemaExplorer library, a rich set of types for working with databases. One of them, TableSchema, lets the user pick a table from a database. You can then use the SchemaExplorer object model to get almost any information you need about the table and its database.

  1. Add this Property directive:

    <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="The table the procedure returns." %>
    
  2. Generator has no built-in knowledge of the SchemaExplorer types, so tell it to load the SchemaExplorer assembly. Importing the namespace as well saves typing:

    <%@ Assembly Name="SchemaExplorer" %>
    <%@ Import Namespace="SchemaExplorer" %>
    

When the user selects a table, Generator fills in the TableSchema object. Most of this template only needs the table's name and its owner (the schema, such as Person), both easy to reach through the SchemaExplorer object model:

<%= SourceTable.Name %>
<%= SourceTable.Owner %>

Put those expressions in the right places and the template is nearly done. One task still needs a little code: building the column list.

Next: Write the database code