Learn How - Code Generation

1. Be sure you've installed SQL-Hero server components. The document to consult on this one can be found here. Double check that you have a server name specified on the Settings tool:

Furthermore, this assumes you've previously set up some global connections as described here.

2. Start Visual Studio. Create a new project, of type:

3. Double-click on the .sqlherogen file to open the SQL-Hero code gen designer. Fill in a project name and the SQL-Hero application server name.

4. Next, click on "Manage Data Sources".

We'll add our global RDDev database connection.

5. Next, we'll add a new template.

6. Enter details about which template we want to run. Here we're running the standard SQL template against tables to build standard audit features which can be controlled declaratively via user-defined properties.

7. Be sure we're satisfied with the UDP settings on our database objects. In this example, we're running the CodeX audit and procedures template, so this is what we might see on a table, for example:

Note: if you don't see all the properties listed above, you might have to run the template for a first time - something that can be done right within the Editor as seen here:

8. Now, go ahead and "build" the sqlheroproj project we previously created, right within Visual Studio. Refresh your view of the RDDev database and now observe that in addition to Visual Studio telling us our build succeeded, there has been changes applied to our Customer table.

This is a quick example of just one kind of template that can be invoked as part of the code generation process. Multiple templates can be sequenced, source control can be tied in automatically, and more. Future documentation found in the library will cover this in greater detail.

9. Note: as was implied above, you can run templates manually as well. This has become a part of the development process, too, whereby if a developer needs to generate a stored procedure wrapper, they don't need to rebuild the entire .sqlheroproj file using Visual Studio. You can run a template against a desired object, get the output and paste it into the desired location (or execute it). The next time a full code gen is executed, the change the developer made gets overwritten, yes - but it should be overwritten with a new file that includes their change. We typically identify individual outputs of the code gen process using .CodeGen in the target file name, but this is just one way to do it.

Note that command-line tool support (using SHCommand) is available and this supports code gen capabilities without requiring invoking a build using Visual Studio at all!