MSBuild Integration
SqlCodeAnalyzeFilesTask
The task runs static analysis over specified script files and creates a report.
Task parameters:
Name | Description |
|---|---|
| Quiet | Suppresses all except error output messages. |
| InputPath | Required. Input script files location. Example: "c:\my files\*.sql". |
| Rules | List of analysis rules or analysis groups which to be applied. The rules can be separated with ',',';' or '|'. Example: sa0001,sa0002,ex0018,Performace,Design |
| ParametersFile | Path to a XML file containing the values for the analysis rules parameters. Check the Xml\AnalysisParameters.xml for example and the Schemas\AnalysisParameters.xsd for the parameters file XML schema. |
| ContextFile | Path to an analysis context file which to be used for analyzing the target script files. Analysis context file for specific database can be created using the command line tool with the analysiscontext command. |
| TemplateFile | Optional parameter for setting external analysis template. The external template will be used as a source for the analysis rules instead of the default template. |
| ReportOutputPath | Output location of the analysis XML report. Example: "c:\my reports\analysis1.xml" |
| FailOnRuleViolation | If TRUE, the task will fail if a single rule violation is found, otherwise the build script can decide whether to fail the build or not. Default value is FALSE. |
| ViolationsCount | Output parameter returning the total number of rule violations. The number includes all rule violations not considering their type. |
| ObjectsCount | Output parameter returning the total number analyzed objects. |
Example MSBuild project SqlCodeAnalyzeFilesTask:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="Ubitsoft.SqlEnlight.Tasks.SqlCodeAnalyzeFilesTask"
AssemblyFile="C:\Program Files (x86)\Ubitsoft\SQL Enlight\Ubitsoft.SqlEnlight.Tasks.dll" />
<PropertyGroup>
<SqlEnlight_AnalysisInputPath>E:\Projects\MyProject\SQL Script\AdventureWorks2008R2.sql</SqlEnlight_AnalysisInputPath>
<SqlEnlight_AnalysisRules>ex0018,Design,Performance</SqlEnlight_AnalysisRules>
<SqlEnlight_AnalysisReportOutputPath>E:\Projects\MyProject\Build\Reports\AdventureWorks2008R2_Script_AnalysisReport.xml</SqlEnlight_AnalysisReportOutputPath>
<SqlEnlight_AnalysisRulesParametersFile>C:\Program Files (x86)\Ubitsoft\SQL Enlight\Xml\AnalysisParameters.xml</SqlEnlight_AnalysisRulesParametersFile>
<SqlEnlight_AnalysisContextFile>E:\Projects\MyProject\Build\Context\AdventureWorks2008R2.xml</SqlEnlight_AnalysisContextFile>
<SqlEnlight_AnalysisTemplateFile>C:\Program Files (x86)\Ubitsoft\SQL Enlight\Templates\Analysis\DefaultAnalysisTemplate.xml</SqlEnlight_AnalysisTemplateFile>
<SqlEnlight_AnalyzedObjectsCount>0</SqlEnlight_AnalyzedObjectsCount>
<SqlEnlight_AnalysisViolationsCount>0</SqlEnlight_AnalysisViolationsCount>
</PropertyGroup>
<Target Name="AnalyzeSqlScriptsTarget">
<Message Text="Starting SQL script analysis..." />
<Message Text="Running analysis on SQL script '$(SqlEnlight_AnalysisInputPath)'..." />
<SqlCodeAnalyzeFilesTask
FailOnRuleViolation="false"
Quiet="true"
Rules="$(SqlEnlight_AnalysisRules)"
InputPath="$(SqlEnlight_AnalysisInputPath)"
ReportOutputPath="$(SqlEnlight_AnalysisReportOutputPath)"
ParametersFile="$(SqlEnlight_AnalysisRulesParametersFile)"
ContextFile="$(SqlEnlight_AnalysisContextFile)"
TemplateFile="$(SqlEnlight_AnalysisTemplateFile)">
<Output TaskParameter="ViolationsCount" PropertyName="SqlEnlight_AnalysisViolationsCount" />
<Output TaskParameter="ObjectsCount" PropertyName="SqlEnlight_AnalyzedObjectsCount" />
</SqlCodeAnalyzeFilesTask>
<Error Text="SQL Enlight analysis reported $(SqlEnlight_AnalysisViolationsCount) rule violations, see $(SqlEnlight_AnalysisReportOutputPath) for details."
Condition="$(SqlEnlight_AnalysisViolationsCount) > 0" />
<Message Text="Completed database script files analysis!" />
</Target>
</Project>
SqlCodeAnalyzeServerTask
The task runs static analysis over specified SQL Server objects and creates a report.
Task parameters:
Name | Description | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Quiet | Suppresses all except error output messages. | ||||||||||||||||||||
ServerName | Required. Target SQL Server name. | ||||||||||||||||||||
DatabaseName | Target database name. Default is "master". | ||||||||||||||||||||
Username | Username for accessing the database. If omitted integrated security will be used. | ||||||||||||||||||||
Password | User password for accessing the database. If omitted integrated security will be used. | ||||||||||||||||||||
ObjectType | Required. Type of the objects that will be analyzed.
|
||||||||||||||||||||
ObjectName | Single-part delimited name for servers,databases and triggers ([object_name]). | ||||||||||||||||||||
TemplateFile | Optional parameter for setting external analysis template. The external template will be used as a source for the analysis rules instead of the default template. | ||||||||||||||||||||
Rules | List of analysis rules or analysis group names separated with ',',';' or '|'. | ||||||||||||||||||||
ParametersFile | Path to a XML file containing the values for the analysis rules parameters. Check Schemas\AnalysisParameters.xsd for the parameters file XML schema. | ||||||||||||||||||||
ContextFile | Path to an analysis context file which to be used for analyzing the tagret script files. | ||||||||||||||||||||
ReportOutputPath | Output location of the analysis XML report. | ||||||||||||||||||||
| FailOnRuleViolation | If TRUE, the task will fail if a single rule violation is found, otherwise the build script can decide whether to fail the build or not. Default value is FALSE. | ||||||||||||||||||||
| ViolationsCount | Output parameter returning the total number of rule violations. The number includes all rule violations not considering their type. | ||||||||||||||||||||
| ObjectsCount | Output parameter returning the total number analyzed objects. |
Example MSBuild project SqlCodeAnalyzeServerTask:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="Ubitsoft.SqlEnlight.Tasks.SqlCodeAnalyzeServerTask"
AssemblyFile="C:\Program Files (x86)\Ubitsoft\SQL Enlight\Ubitsoft.SqlEnlight.Tasks.dll" />
<PropertyGroup>
<SqlEnlight_AnalysisServerName>.</SqlEnlight_AnalysisServerName>
<SqlEnlight_AnalysisDatabaseName>AdventureWorks2008R2</SqlEnlight_AnalysisDatabaseName>
<SqlEnlight_AnalysisUsername></SqlEnlight_AnalysisUsername>
<SqlEnlight_AnalysisPassword></SqlEnlight_AnalysisPassword>
<SqlEnlight_AnalysisObjectType>database</SqlEnlight_AnalysisObjectType>
<SqlEnlight_AnalysisObjectName>AdventureWorks2008R2</SqlEnlight_AnalysisObjectName>
<SqlEnlight_AnalysisRules>ex0018,Design,Performance</SqlEnlight_AnalysisRules>
<SqlEnlight_AnalysisReportOutputPath>E:\Projects\MyProject\Build\Reports\AdventureWorks2008R2_Database_AnalysisReport.xml</SqlEnlight_AnalysisReportOutputPath>
<SqlEnlight_AnalysisRulesParametersFile>C:\Program Files (x86)\Ubitsoft\SQL Enlight\Xml\AnalysisParameters.xml</SqlEnlight_AnalysisRulesParametersFile>
<SqlEnlight_AnalysisTemplateFile>C:\Program Files (x86)\Ubitsoft\SQL Enlight\Templates\Analysis\DefaultAnalysisTemplate.xml</SqlEnlight_AnalysisTemplateFile>
<SqlEnlight_AnalyzedObjectsCount>0</SqlEnlight_AnalyzedObjectsCount>
<SqlEnlight_AnalysisViolationsCount>0</SqlEnlight_AnalysisViolationsCount>
</PropertyGroup>
<Target Name="AnalyzeDatabaseTarget">
<Message Text="Starting database analysis..." />
<Message Text="Running analysis on SQL Server '$(SqlEnlight_AnalysisServerName)' database '$(SqlEnlight_AnalysisDatabaseName)'..." />
<SqlCodeAnalyzeServerTask
FailOnRuleViolation="false"
Quiet="true"
ServerName="$(SqlEnlight_AnalysisServerName)"
DatabaseName="$(SqlEnlight_AnalysisDatabaseName)"
ObjectType="$(SqlEnlight_AnalysisObjectType)"
ObjectName="$(SqlEnlight_AnalysisObjectName)"
Rules="$(SqlEnlight_AnalysisRules)"
ReportOutputPath="$(SqlEnlight_AnalysisReportOutputPath)"
ParametersFile="$(SqlEnlight_AnalysisRulesParametersFile)"
TemplateFile="$(SqlEnlight_AnalysisTemplateFile)">
<Output TaskParameter="ViolationsCount" PropertyName="SqlEnlight_AnalysisViolationsCount" />
<Output TaskParameter="ObjectsCount" PropertyName="SqlEnlight_AnalyzedObjectsCount" />
</SqlCodeAnalyzeServerTask>
<Error Text="SQL Enlight analysis reported $(SqlEnlight_AnalysisViolationsCount) rule violations, see $(SqlEnlight_AnalysisReportOutputPath) for details."
Condition="$(SqlEnlight_AnalysisViolationsCount) > 0" />
<Message Text="Completed database analysis!" />
</Target>
</Project>
SqlCodeLayoutFilesTask
The task reformats T-SQL script files using given layout template.
Task parameters:
Name | Description |
|---|---|
Quiet | Suppresses all except error output messages. |
InputPath | Required. Source T-SQL files location. |
OutputPath | Required. Output location for the reformatted T-SQL script files. |
TemplateName | Exact name of the layout template or external layout template file which to be used. The default layout template will be used in case this argument is not provided. Example: "My Favorite Layout Template" Or "D:\My Layout Templates\My Favorite Layout Template.layouttemplate" |
Example MSBuild project using SqlCodeLayoutFilesTask:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="Ubitsoft.SqlEnlight.Tasks.SqlCodeLayoutFilesTask"
AssemblyFile="C:\Program Files (x86)\Ubitsoft\SQL Enlight\Ubitsoft.SqlEnlight.Tasks.dll" />
<PropertyGroup>
<SqlEnlight_LayoutInputPath>E:\Projects\MyProject\SQL Script\*.sql</SqlEnlight_LayoutInputPath>
<SqlEnlight_LayoutOutputPath>E:\Projects\MyProject\SQL Script\Reformatted\</SqlEnlight_LayoutOutputPath>
<SqlEnlight_LayoutTemplate>C:\Program Files (x86)\Ubitsoft\SQL Enlight\Templates\Layout\Ubitsoft Favorite 1.layouttemplate</SqlEnlight_LayoutTemplate>
</PropertyGroup>
<Target Name="LayoutScriptsTarget">
<Message Text="Starting SQL script layout..." />
<Message Text="Running SQL script layout '$(SqlEnlight_LayoutInputPath)'..." />
<SqlCodeLayoutFilesTask ContinueOnError="false"
TemplateName="$(SqlEnlight_LayoutTemplate)"
InputPath="$(SqlEnlight_LayoutInputPath)"
OutputPath="$(SqlEnlight_LayoutOutputPath)" />
<Message Text="Completed SQL script layout!" />
</Target>
</Project>
NAnt Integration
Before using SQL Enlight tasks with NAnt a modification to NAnt�s configuration have to be made. The change is required , because the following error may appear when a SQL Enlight task is executed:
Mixed mode assembly is built against version 'v2.0.50727' of the runtime and can not be loaded in the 4.0 runtime without additional configuration information.
To resolve the error message and enable SQL Enlight tasks, an attribute useLegacyV2RuntimeActivationPolicy have to be added to the startup element in the NAnt.exe.config file.
<startup useLegacyV2RuntimeActivationPolicy="true">
<!-- .NET Framework 4.0 -->
<supportedRuntime version="v4.0.30319" />
<!-- .NET Framework 2.0 -->
<supportedRuntime version="v2.0.50727" />
<!-- .NET Framework 1.1 -->
<supportedRuntime version="v1.1.4322" />
<!-- .NET Framework 1.0 -->
<supportedRuntime version="v1.0.3705" />
</startup>
sqlCodeAnalyzeFiles
The task runs static analysis over specified script files and creates a report.
Task parameters:
Name | Description |
|---|---|
| quiet | Suppresses all except error output messages. |
| inputPath | Required. Input script files location. Example: "c:\my files\*.sql". |
| rules | List of analysis rules or analysis groups which to be applied. The rules can be separated with ',',';' or '|'. Example: sa0001,sa0002,ex0018,Performace,Design |
| parametersFile | Path to a XML file containing the values for the analysis rules parameters. Check the Xml\AnalysisParameters.xml for example and the Schemas\AnalysisParameters.xsd for the parameters file XML schema. |
| contextFile | Path to an analysis context file which to be used for analyzing the target script files. Analysis context file for specific database can be created using the command line tool with the analysiscontext command. |
| templateFile | Optional parameter for setting external analysis template. The external template will be used as a source for the analysis rules instead of the default template. |
| reportOutputPath | Output location of the analysis XML report. Example: "c:\my reports\analysis1.xml" |
| failOnRuleViolation | If TRUE, the task will fail if a single rule violation is found, otherwise the build script can decide whether to fail the build or not. Default value is FALSE. |
Example NAnt project sqlCodeAnalyzeFiles:
<?xml version="1.0" encoding="utf-8"?>
<project name="SQL Enlight Analyze SQL Files Example" default="runSqlEnlightAnalyzeScriptFiles" basedir="." >
<description>This is an example build file of using SQL Enlight's sqlCodeAnalyzeFiles task.</description>
<loadtasks assembly="C:\Program Files (x86)\Ubitsoft\SQL Enlight\Ubitsoft.SqlEnlight.Tasks.NAnt.dll" />
<property name="SqlEnlight_AnalysisInputPath" value="E:\Projects\MyProject\SQL Script\AdventureWorks2008R2.sql" />
<property name="SqlEnlight_AnalysisTemplateFile" value="C:\Program Files (x86)\Ubitsoft\SQL Enlight\Templates\Analysis\DefaultAnalysisTemplate.xml" />
<property name="SqlEnlight_AnalysisRules" value="design,performance,ex0018" />
<property name="SqlEnlight_AnalysisContextFile" value="E:\Projects\MyProject\Build\Context\AdventureWorks2008R2.xml" />
<property name="SqlEnlight_AnalysisReportOutputPath" value="E:\Projects\MyProject\Build\Reports\AdventureWorks2008R2_Script_AnalysisReport.xml" />
<property name="SqlEnlight_AnalysisRulesParametersFile" value="C:\Program Files (x86)\Ubitsoft\SQL Enlight\Xml\AnalysisParameters.xml"/>
<target name="runSqlEnlightAnalyzeScriptFiles" description="Analyze database script files with SQL Enlight.">
<echo message="${datetime::now()}: Starting database script files analysis..." />
<echo message="Running analysis on SQL script ${SqlEnlight_AnalysisInputPath}..." />
<sqlCodeAnalyzeFiles failOnRuleViolation="false"
quiet="false"
inputPath="${SqlEnlight_AnalysisInputPath}"
templateFile="${SqlEnlight_AnalysisContextFile}"
rules="${SqlEnlight_AnalysisRules}"
reportOutputPath="${SqlEnlight_AnalysisReportOutputPath}"
contextFile="${SqlEnlight_AnalysisContextFile}"
parametersFile="${SqlEnlight_AnalysisRulesParametersFile}"
/>
<xmlpeek
file="${SqlEnlight_AnalysisReportOutputPath}"
xpath="/SqlEnlightReport/ExtendedProperties/ExtendedProperty[@Name='IssuesCount']/@Value"
property="SqlEnlight_AnalysisViolationsCount"
failonerror="false">
</xmlpeek>
<xmlpeek
file="${SqlEnlight_AnalysisReportOutputPath}"
xpath="/SqlEnlightReport/ExtendedProperties/ExtendedProperty[@Name='ObjectsCount']/@Value"
property="SqlEnlight_AnalyzedObjectsCount"
failonerror="false">
</xmlpeek>
<fail if="${int::parse(SqlEnlight_AnalysisViolationsCount) > 0}" verbose="true" message="SQL Enlight analysis reported ${SqlEnlight_AnalysisViolationsCount} rule violations. See ${SqlEnlight_AnalysisReportOutputPath} for details."/>
<echo message="${datetime::now()}: Completed database script files analysis!" />
</target>
</project>
sqlCodeAnalyzeServer
The task runs static analysis over specified SQL Server objects and creates a report.
Task parameters:
Name | Description | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
quiet | Suppresses all except error output messages. | ||||||||||||||||||||
serverName | Required. Target SQL Server name. | ||||||||||||||||||||
databaseName | Target database name. Default is "master". | ||||||||||||||||||||
username | Username for accessing the database. If omitted integrated security will be used. | ||||||||||||||||||||
password | User password for accessing the database. If omitted integrated security will be used. | ||||||||||||||||||||
objectType | Required. Type of the objects that will be analyzed.
|
||||||||||||||||||||
objectName | Single-part delimited name for servers,databases and triggers ([object_name]). | ||||||||||||||||||||
templateFile | Optional parameter for setting external analysis template. The external template will be used as a source for the analysis rules instead of the default template. | ||||||||||||||||||||
rules | List of analysis rules or analysis group names separated with ',',';' or '|'. | ||||||||||||||||||||
parametersFile | Path to a XML file containing the values for the analysis rules parameters. Check Schemas\AnalysisParameters.xsd for the parameters file XML schema. | ||||||||||||||||||||
contextFile | Path to an analysis context file which to be used for analyzing the tagret script files. | ||||||||||||||||||||
reportOutputPath | Output location of the analysis XML report. | ||||||||||||||||||||
| failOnRuleViolation | If TRUE, the task will fail if a single rule violation is found, otherwise the build script can decide whether to fail the build or not. Default value is FALSE. |
Example NAnt project sqlCodeAnalyzeServer:
<?xml version="1.0" encoding="utf-8"?>
<project name="SQL Enlight Analyze SQL Server Example" default="runSqlCodeAnalyzeServer" basedir=".">
<description>This is an example build file of using SQL Enlight's sqlCodeAnalyzeServer task.</description>
<loadtasks assembly="C:\Program Files (x86)\Ubitsoft\SQL Enlight\Ubitsoft.SqlEnlight.Tasks.NAnt.dll" />
<property name="SqlEnlight_AnalysisTemplateFile" value="C:\Program Files (x86)\Ubitsoft\SQL Enlight\Templates\Analysis\DefaultAnalysisTemplate.xml" />
<property name="SqlEnlight_AnalysisRules" value="design,performance,ex0018" />
<property name="SqlEnlight_AnalysisReportOutputPath" value="E:\Projects\MyProject\Build\Reports\AdventureWorks2008R2_Database_AnalysisReport.xml" />
<property name="SqlEnlight_AnalysisRulesParametersFile" value="C:\Program Files (x86)\Ubitsoft\SQL Enlight\Xml\AnalysisParameters.xml"/>
<property name="SqlEnlight_AnalysisServerName" value="(local)"/>
<property name="SqlEnlight_AnalysisDatabaseName" value="AdventureWorks2008R2"/>
<property name="SqlEnlight_AnalysisViolationsCount" value="0" />
<property name="SqlEnlight_AnalyzedObjectsCount" value="0" />
<!-- Windows Authentication Mode assumed when the username and password properties are empty. -->
<property name="SqlEnlight_AnalysisUsername" value=""/>
<property name="SqlEnlight_AnalysisPassword" value=""/>
<!--
-->
<target name="runSqlCodeAnalyzeServer" description="Analyze database with SQL Enlight.">
<echo message="${datetime::now()}: Starting database analysis..." />
<echo message="Running analysis on SQL Server '${SqlEnlight_AnalysisServerName}' database '${SqlEnlight_AnalysisDatabaseName}'..." />
<sqlCodeAnalyzeServer failOnRuleViolation="false"
quiet="false"
serverName="${SqlEnlight_AnalysisServerName}"
databaseName="${SqlEnlight_AnalysisDatabaseName}"
userName="${SqlEnlight_AnalysisUsername}"
password="${SqlEnlight_AnalysisPassword}"
objectType="database"
objectName="${SqlEnlight_AnalysisDatabaseName}"
templateFile="${SqlEnlight_AnalysisTemplateFile}"
rules="${SqlEnlight_AnalysisRules}"
reportOutputPath="${SqlEnlight_AnalysisReportOutputPath}"
parametersFile="${SqlEnlight_AnalysisRulesParametersFile}"
/>
<xmlpeek
file="${SqlEnlight_AnalysisReportOutputPath}"
xpath="/SqlEnlightReport/ExtendedProperties/ExtendedProperty[@Name='IssuesCount']/@Value"
property="SqlEnlight_AnalysisViolationsCount"
failonerror="false">
</xmlpeek>
<xmlpeek
file="${SqlEnlight_AnalysisReportOutputPath}"
xpath="/SqlEnlightReport/ExtendedProperties/ExtendedProperty[@Name='ObjectsCount']/@Value"
property="SqlEnlight_AnalyzedObjectsCount"
failonerror="false">
</xmlpeek>
<fail if="${int::parse(SqlEnlight_AnalysisViolationsCount) > 0}" verbose="true" message="SQL Enlight analysis reported ${SqlEnlight_AnalysisViolationsCount} rule violations. See ${SqlEnlight_AnalysisReportOutputPath} for details."/>
<echo message="${datetime::now()}: Completed database analysis!" />
</target>
</project>
sqlCodeLayoutFiles
The task reformats T-SQL script files using given layout template.
Task parameters:
Name | Description |
|---|---|
quiet | Suppresses all except error output messages. |
inputPath | Required. Source T-SQL files location. |
outputPath | Required. Output location for the reformatted T-SQL script files. |
templateName | Exact name of the layout template or external layout template file which to be used. The default layout template will be used in case this argument is not provided. Example: "My Favorite Layout Template" Or "D:\My Layout Templates\My Favorite Layout Template.layouttemplate" |
Example NAnt project using sqlCodeLayoutFiles:
<?xml version="1.0" encoding="utf-8"?>
<project name="SQL Enlight Layout SQL Script Files Example" default="runSqlCodeLayoutFiles" basedir=".">
<description>This is an example build file of using SQL Enlight's sqlCodeLayoutFiles task.</description>
<loadtasks assembly="C:\Program Files (x86)\Ubitsoft\SQL Enlight\Ubitsoft.SqlEnlight.Tasks.NAnt.dll" />
<property name="SqlEnlight_LayoutTemplate" value="C:\Program Files (x86)\Ubitsoft\SQL Enlight\Templates\Layout\Ubitsoft Favorite 1.layouttemplate" />
<property name="SqlEnlight_LayoutInputPath" value="E:\Projects\MyProject\SQL Script\AdventureWorks2008R2.sql" />
<property name="SqlEnlight_LayoutOutputPath" value="E:\Projects\MyProject\SQL Script\Reformatted\" />
<target name="runSqlCodeLayoutFiles" description="Layout script files SQL Enlight.">
<echo message="${datetime::now()}: Starting script layout..." />
<echo message="Applying '${SqlEnlight_LayoutTemplate}' layout template to the '${SqlEnlight_LayoutInputPath}' files ..." />
<sqlCodeLayoutFiles quiet="true"
inputPath="${SqlEnlight_LayoutInputPath}"
outputPath="${SqlEnlight_LayoutOutputPath}"
layoutTemplate="${SqlEnlight_LayoutTemplate}"
/>
<echo message="${datetime::now()}: Completed script layout!" />
</target>
</project>

