-
Notifications
You must be signed in to change notification settings - Fork 2
What happens when you save
bbsimonbb edited this page Mar 2, 2020
·
3 revisions
QueryFirst swings into action every time you save a QueryFirst .sql file.*
It analyses your query and (re)generates the nested .gen.cs file, which gives you a rich C# interface to your query.
Generating the wrapper code requires several steps...
- We muster all the info: the query text obviously, but also...
- the config from qfconfig.json (we take the first file we find, starting in the folder of the .sql file and working up).
- config overrides from the query itself.
- the namespace and the result class name from the nested results.cs file. You can change these to your liking and they will be reflected in the generated code.
- We get the DB to report the parameters used in the query (sp_describe_undeclared_parameters for Sql Server). For Sql Server, this works well so long as your parameter is used only once. A good habit is to save your .sql each time you create a parameter, so that if you reuse it later, it's already found and declared.
- We declare parameters as local variables in the design time section. This happens once, and these declarations become part of your .sql file. Parameters will appear in method signatures in the order in which they are declared in the
--designTimesection. You can edit these declarations to, for example, assign design-time values or add comments, but you must leave the structure intact, each declaration on it's own line. We will parse this section to determine the type and direction of parameters to supply at runtime. - We "run" the query with the SCHEMA_ONLY flag set, to retrieve the result-set schema in a provider-independent fashion.
- If this returns nothing, there's a provider-specific second attempt. For Sql Server, the second attempt calls sp_describe_first_result_set. This notably deals better with table-valued parameters, but statements that create temp tables still pose a problem.
- We generate the wrapper. See the next page for details.
QueryFirst will ignore .sql files that don't have "managed by QueryFirst" in them. This text is present in the template. If you want QueryFirst to keep doing it's thing, don't delete this text.