Your browser may have trouble rendering this page. See supported browsers for more information.

This page shows the source for this entry, with WebCore formatting language tags and attributes highlighted.

Title

Mixing your own SQL into Quino queries: part 1 of 2

Description

The Quino ORM<fn> manages all CrUD---Create, Update, Delete---operations for your application. This basic behavior is generally more than enough for standard user interfaces. When a user works with a single object in a window and saves it, there really isn't that much to optimize. <h>Modeled methods</h> A more complex editing process may include several objects at once and perhaps trigger events that create additional auditing objects. Even in these cases, there are still only a handful of save operations to execute. To keep the architecture clean, an application is encouraged to model these higher-level operations with methods in the metadata (modeled methods). The advantage to using modeled methods is that they can be executed in an application server as well as locally in the client. When an application uses a remote application server rather than a direct connection to a database, modeled methods are executed in the service layer and therefore have much less latency to the database. <h>When Quino's query language isn't enough</h> If an application needs even more optimization, then it may be necessary to write custom SQL---or even to use stored procedures to move the query into the database. Mixing SQL with an ORM can be a tricky business. It's even more of a challenge with an ORM like that in Quino, which generates the database schema and shields the user from tables, fields and SQL syntax almost entirely. What are the potential pitfalls when using custom query text (e.g. SQL) with Quino? <ul> <b>Schema element names</b>: An application needs to figure out the names of database objects like table and columns. It would be best not to hard-code them so that when the model changes, the custom code will be automatically updated.<ul> If the query is in a stored procedure, then the database may ensure that the code is updated or at least checked when the schema changes.<fn> If the query is in application code, then care can be taken to keep that query in-sync with the model </ul> <b>Materialization</b>: In particular, the selected fields in a projection must match the expectations of the ORM exactly so that it can materialize the objects properly. We'll see how to ensure this in examples below. </ul> There are two approaches to executing custom code: <ul> <b>ADO</b>: Get a reference to the underlying ADO infrastructure to execute queries directly without using Quino at all. With this approach, Quino can still help an application retrieve properly configured connections and commands. <b>CustomCommandText</b>: An application commonly adds restrictions and sorts to the <c>IQuery</c> object using expressions, but can also add text directly to enhance or replace sections of the generated query. </ul> <n>All of the examples below are taken directly from the Quino test suite. Some variables---like <c>DefaultDatabase</c>---are provided by the Quino base testing classes but their purpose, types and implementation should be relatively obvious.</n> <h>Using ADO directly</h> You can use the <c>AdoDataConnectionTools</c> to get the underlying ADO connection for a given <c>Session</c> so that any commands you execute are guaranteed to be executed in the same transactions as are already active on that session. If you use these tools, your ADO code will also automatically use the same connection parameters as the rest of your application without having to use hard-coded connection strings. The first example shows a test from the Quino framework that shows how easy it is to combine results returned from another method into a standard Quino query. <code> [Test] public virtual void TestExecuteAdoDirectly() { var ids = GetIds().ToList(); var people = Session.GetList<person>(); people.Query.Where(Person.MetaProperties.Id, ExpressionOperator.In, ids); Assert.That(people.Count, Is.EqualTo(9)); } </code> The ADO-access code is hidden inside the call to <c>GetIds()</c>, the implementation for which is shown below. Your application can get the connection for a session as described above and then create commands using the same helper class. If you call <c>CreateCommand()</c> directly on the ADO connection, you'll have a problem when running inside a transaction on SQL Server. The SQL Server ADO implementation requires that you assign the active transaction object to each command. Quino takes care of this bookkeeping for you if you use the helper method. <code> private IEnumerable<int> GetIds() { using (var helper = AdoDataConnectionTools.GetAdoConnection(Session, "Name")) { using (var command = helper.CreateCommand()) { command.AdoCommand.CommandText = @"SELECT id FROM punchclock__person WHERE lastname = 'Rogers'"; using (var reader = command.AdoCommand.ExecuteReader()) { while (reader.Read()) { yield return reader.GetInt32(0); } } } } } </code> There are a few drawbacks to this approach: <ul> Your application will make two queries instead of one. The hard-coded SQL will break if you make model changes that affect those tables and fields. The ADO approach only works if the application has a direct connection to the database. An application that uses ADO will not be able to switch to an application-server driver without modification. </ul> In the second part, we will improve on this approach by using the <c>CustomCommandText</c> property of a Quino query. This will allow us to use only a single query. We will also improve maintainability by reducing the amount of code that isn't checked by the compiler (e.g. the SQL text above). Stay tuned for part 2, coming soon! <n>Many thanks to <a href="http.//blogs.encodo.ch/news/view_user.php?name=Urs">Urs</a> for proofreading and suggestions on overall structure.</n> <hr> <ft>This article uses features of Quino that will only become available in version 1.12. Almost all of the examples will also work in earlier versions but the <c>AdoDataConnectionTools</c> is not available until 1.12. The functionality of this class can, however, be back-ported if necessary.</ft> <ft>More likely, though, is that the Quino schema migration will be prevented from applying updates if there are custom stored procedures that use tables and columns that need to be changed.</ft>Mixing your own SQL into Quino queries: part 1 of 2