Welcome!

You will be redirected in 30 seconds or close now.

ColdFusion Authors: Yakov Fain, Jeremy Geelan, Maureen O'Gara, Nancy Y. Nee, Tad Anderson

Related Topics: ColdFusion

ColdFusion: Article

ODBC Escape Sequences

ODBC Escape Sequences

Within the field of computer science, competing products often implement standards differently. Database management systems (DBMSs), with their different syntaxes for SQL queries, are no exception. How can we write queries within our applications that will be compatible with several DBMSs, especially when we need to use functionality that different DBMSs have implemented differently?

Recently, Technomedia Training, Inc., was awarded a contract to deliver our core product, a Web-based human resources management software application, to a new client. The client would be hosting the application, and the IT department's infrastructure and expertise were strictly Oracle-based. However, our application was designed to work with SQL Server 2000. Our challenge was to transform our Web application to make it compatible with an Oracle8i database.

The first step was to convert the database and all its objects from SQL Server 2000 to Oracle8i. (We employed some interesting conversion techniques, but they're a whole subject themselves and we won't cover them in this article.) Next, we discovered that many of the queries within the application didn't work anymore with the Oracle8i database because of differences in implementation of the SQL standard. For example, the following outer join query in Transact-SQL works with SQL Server 2000 but is incompatible with Oracle8i:

SELECT EMP.ID, EMP.NAME, DEP.NAME
FROM EMPLOYEES EMP
LEFT OUTER JOIN DEPARTMENTS DEP ON
DEP.DEPARTMENT_ID = EMP.ID
In Oracle8i, the syntax is:

SELECT EMP.ID,EMP.NAME.DEP.NAME
FROM EMPLOYEES EMP, DEPARTMENTS DEP
WHERE EMP.DEPARTMENT_ID = DEP.ID(+)
We looked at several alternatives for resolving this problem, and concluded that ODBC escape sequences are the best way to make our application's queries compatible with several DBMSs.

Technological Architecture
Our core product is a Web application that uses a standard, three-tier architecture. A ColdFusion Server works with an IIS Web server to run the application code. The ColdFusion Server uses an ODBC driver to connect to the database. Users' Web browsers call up pages from the ColdFusion/IIS servers using the HTTP protocol, and the application works with a SQL Server 2000 database - we need to make it compatible with Oracle8i (see Figure1).

Dealing with DBMS-Specific Queries
To deal with syntax differences among DBMSs, we considered three possible solutions. We could:

1.   Change the application's queries to remove the DBMS-specific elements.
2.   Put conditional statements around all queries with DBMS-specific terms and have both syntaxes present in the code. Then, depending on which DBMS the application is connected to, the appropriate syntax would be used.
3.   Use ODBC escape sequences and let the ODBC driver map to the DBMS-specific syntax.

We'll examine these three possibilities next.

Simple SQL
The first option for dealing with DBMS-specific queries is to remove all terms that are DBMS-specific and to use only SQL syntax that is common to all DBMSs. For example, the SQL Server 2000 syntax for concatenating a first and a last name looks like this:

<cfquery name = "qsEmployees" datasource="Sigal">
SELECT first_name + ' ' + last_name AS name
FROM employees
</cfquery>
Instead, the concatenation could be done outside of the query like this:

<cfquery name = "qsEmployees" datasource="Sigal">
SELECT first_name, last_name, NULL AS name
FROM employees
</cfquery>
<cfloop query = "qsEmployees">
<cfset tmp = QuerySetCell (qsEmployees, "name",
qsEmployees.first_name & " " &

qsEmployees.last_name,
qsEmployees.CurrentRow)
</cfloop>
The advantage of this solution is that the query will work with all types of DBMSs and with all types of drivers, including non-ODBC drivers. However, we discarded this solution because of its disadvantages. If we limit our queries to a lowest-common-denominator SQL, we can no longer use some functionality that is essential to our application. There's no way to execute outer joins, and most date and time functions are excluded. All queries have to be relatively simple, which implies having to restructure certain queries or even sub-dividing them to make them more compatible. In addition, a lot of the processing is transferred from the database to the application code. This incurs a performance penalty, since the database server is optimized for many of the data operations, while the application server is not.

Different Versions of Incompatible Queries
The second option for making DBMS-specific queries compatible with several DBMSs is to create different versions of the query, each version containing DBMS-specific syntax. Using this method, our concatenation example would look like this:

<cfquery name = "qsEmployees" datasource="Sigal">
<cfif application. DBType eq "ORA">
SELECT first_name || ' ' || last_name AS name
FROM employees
<cfelse>
SELECT first_name + ' ' + last_name AS name
FROM employees
</cfif>
</cfquery>
The advantage of this solution is that it's relatively easy to implement. A new, DBMS-specific version of each incompatible query is written and the application executes the appropriate version for the database it's connected to. However, doubling queries makes the application code bigger. Modifying and maintaining the code becomes more complex because changes to a query need to be repeated for each DBMS-specific version. Moreover, this solution doesn't scale with other DBMSs. If one day the need arose to make the application compatible with a DBMS other than Oracle or SQL Server, the whole process would need to be repeated. The code would become even more complex and difficult to modify and to maintain.

ODBC Escape Sequences
Using ODBC escape sequences for the DBMS-specific queries is the third option for making queries compatible with different DBMSs. ODBC escape sequences are a way of coding database functionalities that have different syntaxes in different DBMSs. As we have seen, the syntax for concatenations in Oracle8i is different from that used in SQL Server 2000, but we can use an escape sequence that represents the concatenation functionality provided by the DBMSs instead of the DBMS syntax. The concatenation example now looks like this:

<cfquery name = "qsEmployees" datasource="Sigal">
SELECT {fn concat (first_name, {fn concat (' ', last_name)})}
AS name
FROM employees
</cfquery>

ODBC escape sequences allow queries to be compatible with several DBMSs while giving access to the functionalities that have been implemented differently by different DBMSs. It's important to note that an escape sequence will only work with a DBMS if the DBMS supports the escape sequence functionality. Escape sequences are not implementations of database functionalities. Rather, they are an alternative way of writing a functionality, an alternative syntax that is translated by the ODBC driver into the DBMS syntax. It's possible to have one version of the queries using this technique. This solution also scales much better than the previous one because the queries are not only compatible with Oracle8i and SQL Server 2000, but also with any other DBMS that has an ODBC driver, in the measure that the DBMS supports the functionality represented by the escape sequences. The learning process is simplified because we don't need to learn the DBMS syntax of different DBMSs once we have learned the ODBC escape sequences. However, using ODBC escape sequences can make the code more complex because of the somewhat ornery syntax. You'll need to plan for a learning period for everyone to become comfortable with the new syntax and when to use it. Despite these mild difficulties, ODBC escape sequences are, in our opinion, the best way to deal with the different ways different DBMSs implement certain functionalities.

Using ODBC Escape Sequences
Let's look at how ODBC escape sequences are used. The functionalities that are implemented differently by different DBMSs are coded using a special syntax, called "escape sequences". Escape sequences have the following structure:

  • They are placed within curly braces so the ODBC driver recognizes them.
  • A code that represents the escape sequence type follows the opening curly brace.
  • The escape sequence content is written.

    The ODBC driver reads the escape sequence and translates it into the DBMS-specific syntax before sending the query to the database. ODBC escape sequences can be used for functions, outer joins, LIKE escape characters, GUIDs, intervals, procedure calls, and date, time, and timestamp literals. For example, an escape sequence function looks like this:

    SELECT {fn MonthName (date_membership)}
    AS month_membership
    FROM employees
    An outer join escape sequence has this syntax:

    SELECT emp.id, ent.name
    FROM {oj employees emp
    LEFT OUTER JOIN entities ent
    ON emp.entity_id = ent.id}
    A date literal has this syntax:

    SELECT COUNT(id) AS num
    FROM employees
    WHERE date_membership > {d '2003-01-01'}
    Challenges
    When we started implementing and testing ODBC escape sequences in our application's queries, we discovered that not all ODBC drivers fully implement the ODBC standard. Some of the functions in the ODBC escape sequence specification were not implemented by our Oracle ODBC driver. For example, we weren't able to use the TimeStampDifference() function with the Oracle ODBC driver. In such cases, we used one of the other two alternatives, conditional statements around different versions of the query, or simple SQL with more processing outside the query.

    Another concern was that by using ODBC escape sequences, we were limiting our database connectivity to ODBC drivers. Our company also develops Java objects to extend the functionality of ColdFusion, and we questioned whether the ODBC escape sequences were a good long-term solution, as Java objects use the JDBC protocol. Research revealed that certain JDBC drivers, known as ODBC bridges, use the ODBC protocol. Also, the JDBC protocol specification includes an escape syntax that is very similar to the ODBC escape sequences. Testing with a pure JDBC driver showed that it was compliant with several escape sequences. We strongly recommend testing with the JDBC driver you're planning to use.

    Conclusion
    Although the process of converting an application's queries to be compatible with a new DBMS can be lengthy, using ODBC escape sequences ensures that most of the work will be done only once. It also preserves the application's performance by allowing access to the database functionality, which has different syntaxes in different DBMSs.

    References

  • ODBC escape sequences: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/ odbcescape_sequences_in_odbc.asp
  • JDBC API: http://java.sun.com/products/jdbc/download.html
  • ColdFusion JRun JDBC driversdocumentation: www.macromedia.com/v1/documents/jr31/jdbcref.pdf
  • Comments (0)

    Share your thoughts on this story.

    Add your comment
    You must be signed in to add a comment. Sign-in | Register

    In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


    @ThingsExpo Stories
    "Cloud Academy is an enterprise training platform for the cloud, specifically public clouds. We offer guided learning experiences on AWS, Azure, Google Cloud and all the surrounding methodologies and technologies that you need to know and your teams need to know in order to leverage the full benefits of the cloud," explained Alex Brower, VP of Marketing at Cloud Academy, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clar...
    Large industrial manufacturing organizations are adopting the agile principles of cloud software companies. The industrial manufacturing development process has not scaled over time. Now that design CAD teams are geographically distributed, centralizing their work is key. With large multi-gigabyte projects, outdated tools have stifled industrial team agility, time-to-market milestones, and impacted P&L stakeholders.
    Gemini is Yahoo’s native and search advertising platform. To ensure the quality of a complex distributed system that spans multiple products and components and across various desktop websites and mobile app and web experiences – both Yahoo owned and operated and third-party syndication (supply), with complex interaction with more than a billion users and numerous advertisers globally (demand) – it becomes imperative to automate a set of end-to-end tests 24x7 to detect bugs and regression. In th...
    "Akvelon is a software development company and we also provide consultancy services to folks who are looking to scale or accelerate their engineering roadmaps," explained Jeremiah Mothersell, Marketing Manager at Akvelon, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
    "MobiDev is a software development company and we do complex, custom software development for everybody from entrepreneurs to large enterprises," explained Alan Winters, U.S. Head of Business Development at MobiDev, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
    SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5–7, 2018, at the Javits Center in New York City, NY. CrowdReviews.com is a transparent online platform for determining which products and services are the best based on the opinion of the crowd. The crowd consists of Internet users that have experienced products and services first-hand and have an interest in letting other potential buye...
    "IBM is really all in on blockchain. We take a look at sort of the history of blockchain ledger technologies. It started out with bitcoin, Ethereum, and IBM evaluated these particular blockchain technologies and found they were anonymous and permissionless and that many companies were looking for permissioned blockchain," stated René Bostic, Technical VP of the IBM Cloud Unit in North America, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Conventi...
    SYS-CON Events announced today that Telecom Reseller has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5-7, 2018, at the Javits Center in New York, NY. Telecom Reseller reports on Unified Communications, UCaaS, BPaaS for enterprise and SMBs. They report extensively on both customer premises based solutions such as IP-PBX as well as cloud based and hosted platforms.
    "Space Monkey by Vivent Smart Home is a product that is a distributed cloud-based edge storage network. Vivent Smart Home, our parent company, is a smart home provider that places a lot of hard drives across homes in North America," explained JT Olds, Director of Engineering, and Brandon Crowfeather, Product Manager, at Vivint Smart Home, in this SYS-CON.tv interview at @ThingsExpo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
    Coca-Cola’s Google powered digital signage system lays the groundwork for a more valuable connection between Coke and its customers. Digital signs pair software with high-resolution displays so that a message can be changed instantly based on what the operator wants to communicate or sell. In their Day 3 Keynote at 21st Cloud Expo, Greg Chambers, Global Group Director, Digital Innovation, Coca-Cola, and Vidya Nagarajan, a Senior Product Manager at Google, discussed how from store operations and ...
    In his session at 21st Cloud Expo, Carl J. Levine, Senior Technical Evangelist for NS1, will objectively discuss how DNS is used to solve Digital Transformation challenges in large SaaS applications, CDNs, AdTech platforms, and other demanding use cases. Carl J. Levine is the Senior Technical Evangelist for NS1. A veteran of the Internet Infrastructure space, he has over a decade of experience with startups, networking protocols and Internet infrastructure, combined with the unique ability to it...
    It is of utmost importance for the future success of WebRTC to ensure that interoperability is operational between web browsers and any WebRTC-compliant client. To be guaranteed as operational and effective, interoperability must be tested extensively by establishing WebRTC data and media connections between different web browsers running on different devices and operating systems. In his session at WebRTC Summit at @ThingsExpo, Dr. Alex Gouaillard, CEO and Founder of CoSMo Software, presented ...
    "There's plenty of bandwidth out there but it's never in the right place. So what Cedexis does is uses data to work out the best pathways to get data from the origin to the person who wants to get it," explained Simon Jones, Evangelist and Head of Marketing at Cedexis, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
    WebRTC is great technology to build your own communication tools. It will be even more exciting experience it with advanced devices, such as a 360 Camera, 360 microphone, and a depth sensor camera. In his session at @ThingsExpo, Masashi Ganeko, a manager at INFOCOM Corporation, introduced two experimental projects from his team and what they learned from them. "Shotoku Tamago" uses the robot audition software HARK to track speakers in 360 video of a remote party. "Virtual Teleport" uses a multip...
    A strange thing is happening along the way to the Internet of Things, namely far too many devices to work with and manage. It has become clear that we'll need much higher efficiency user experiences that can allow us to more easily and scalably work with the thousands of devices that will soon be in each of our lives. Enter the conversational interface revolution, combining bots we can literally talk with, gesture to, and even direct with our thoughts, with embedded artificial intelligence, whic...
    SYS-CON Events announced today that Evatronix will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Evatronix SA offers comprehensive solutions in the design and implementation of electronic systems, in CAD / CAM deployment, and also is a designer and manufacturer of advanced 3D scanners for professional applications.
    Leading companies, from the Global Fortune 500 to the smallest companies, are adopting hybrid cloud as the path to business advantage. Hybrid cloud depends on cloud services and on-premises infrastructure working in unison. Successful implementations require new levels of data mobility, enabled by an automated and seamless flow across on-premises and cloud resources. In his general session at 21st Cloud Expo, Greg Tevis, an IBM Storage Software Technical Strategist and Customer Solution Architec...
    To get the most out of their data, successful companies are not focusing on queries and data lakes, they are actively integrating analytics into their operations with a data-first application development approach. Real-time adjustments to improve revenues, reduce costs, or mitigate risk rely on applications that minimize latency on a variety of data sources. In his session at @BigDataExpo, Jack Norris, Senior Vice President, Data and Applications at MapR Technologies, reviewed best practices to ...
    An increasing number of companies are creating products that combine data with analytical capabilities. Running interactive queries on Big Data requires complex architectures to store and query data effectively, typically involving data streams, an choosing efficient file format/database and multiple independent systems that are tied together through custom-engineered pipelines. In his session at @BigDataExpo at @ThingsExpo, Tomer Levi, a senior software engineer at Intel’s Advanced Analytics gr...
    When talking IoT we often focus on the devices, the sensors, the hardware itself. The new smart appliances, the new smart or self-driving cars (which are amalgamations of many ‘things’). When we are looking at the world of IoT, we should take a step back, look at the big picture. What value are these devices providing? IoT is not about the devices, it’s about the data consumed and generated. The devices are tools, mechanisms, conduits. In his session at Internet of Things at Cloud Expo | DXWor...