强大的跨数据库访问组件 UniDAC使用教程:统一SQL

Universal Data Access Components (UniDAC)是一款通用数据库访问组件,提供了多个数据库的直接访问,如针对Windows的Delphi,C++Builder, Lazarus (以及 Free Pascal), Mac OS X, iOS,Android,Linux和64和32位的FreeBSD等等。我们将长期的经验集于这个小组件,提供统一的数据库连接访问(如oracle、微软SQL等等)。这意味着你可以在你的项目之间轻松地切换不同的数据库,以及创建跨数据库应用程序接口。

                                          【下载UniDAC最新版本

在几个数据库的编程应用程序中最关键的问题之一是SQL语法在许多情况下可能不同。本文将演示UniDAC如何帮助大家解决此问题。

数据库应用程序使用SQL语句操作数据。除非用户直接输入,否则语句可以采用两种方式构建,在开发期间进行硬编码,或在运行时构建。第一种方式对开发人员来说非常方便,而第二种方式则更灵活。UniDAC可以结合这两种方法,从而达到最佳效果:你可以对在运行时转换为适当语法的SQL语句进行硬编码。

本篇文章主要包含以下内容:

  • 基本信息
  • 条件执行(IF)
  • 文字和标识符
  • 评论
  • SQL函数
  • 宏参考

基本信息

UniDAC的通用功能基于以下功能:

  • 具有特定于不同数据库(提供者)的值的宏。
  • 一组自动映射的功能。
  • 统一的文字标准。

了解这一点后,你可以编写在运行时真正与数据库无关的SQL代码。

UniDAC提供了两种处理宏的方法:Connection Macros(连接宏)和 DataSet Macros(数据集宏)。它们的定义方式和SQL查询文本中的指示方式不同。

DataSet Macros由“&MacroName”定义,仅影响指定的数据集。

Connection Macros由“{MacroName}”定义,并影响所有关联的数据集。

让我们对TUniConnection.Macros进行更详细的分析。以传统方式使用它:

if  UniConnection.ProviderName = 'Oracle' then
  UniConnection.MacroByName('tablename').Value := 'dept'
else
if  UniConnection.ProviderName = 'MySql' then
  UniConnection.MacroByName('tablename').Value := 'test.dept';复制代码

或者你可以使用预定义的方法。宏是一组名称,条件和值。如果启用了潜在条件,宏将计算其值,如果未启用条件,则计算为空字符串。根据TUniConnection组件使用的提供程序启用或禁用条件。例如,如果使用Oracle提供程序,则将启用ORACLE宏。

因此,基于Oracle条件的所有宏在SQL语句中使用时都会返回它们的值;所有其他宏返回空字符串。有关可用条件的列表(或者说,预定义的宏),请参阅“宏参考”(“宏参考”将会在后续文章中与大家分享)。

从API的角度来看,宏表示为TUniMacro类。宏的集合被组织成TUniMacros,可以通过TUniConnection的宏属性访问它。每个连接都有一组独立的宏。

以下示例演示了宏的用法:

UniConnection.Provider = 'MySQL';
...
UniConnection.Open;
UniConnection.Macros.Add('tablename', 'test.dept', 'MySQL');
UniQuery.SQL.Text := 'SELECT Count(*) FROM {tablename}';
UniQuery.Open;复制代码

现在假设我们需要在Oracle服务器上执行相同的操作。 由于使用了UniSQL,唯一要添加的是另一个宏:

UniConnection.Provider = 'Oracle';
...
UniConnection.Open;
UniConnection.Macros.Add('tablename', 'test.dept', 'MySQL');
UniConnection.Macros.Add('tablename', 'dept', 'Oracle');
UniQuery.SQL.Text := 'SELECT Count(*) FROM {tablename}';
UniQuery.Open;复制代码

如你所见,控制SQL语句转换非常容易。现在让我们看一下演示一大堆重要功能的另一个示例:

UniConnection.Macros.Add('tablename', 'emp', '');
//For MySQL, prepend database name
UniConnection.Macros.Add('tablename', 'test.emp', 'MySQL');

//Limit records count where it is easy (MySQL and PostgreSQL)
UniConnection.Macros.Add('limit', 'LIMIT 0,5', 'MySQL');
UniConnection.Macros.Add('limit', 'LIMIT 5 OFFSET 0', 'PostgreSQL');

//Define default FROM clause
UniConnection.Macros.Add('from', 'FROM {tablename}', '');
//If the limit macro is defined, add extra clause
UniConnection.Macros.Add('from', 'FROM {tablename} {limit}', 'limit');

//Define query that uses the macro
UniQuery.SQL.Text := 'SELECT EName, Job, Sal {from}';
UniQuery.Open;复制代码

假设在此示例中连接到MySQL服务器,执行的语句将是:

SELECT EName, Job, Sal FROM emp LIMIT 0,5

注意:你可以使用DBMonitor应用程序查看执行时查询的内容。对样本的逐步分析揭示了以下重要说明:

  1. 如果宏具有空白条件,则始终对其进行评估。
  2. 启用条件的宏将覆盖具有空白条件的宏。
  3. 条件不区分大小写。
  4. 你可以使用自己的宏作为条件。
  5. 你可以将宏用作其他宏值的一部分。

你可以在大括号内的宏名称后添加任何文本。如果启用了宏的条件,则将此文本添加到最终的SQL语句中。例如:

UniConnection.Macros.Add('schema', 'test', 'MySQL');
UniQuery.SQL.Text := 'SELECT * FROM {schema .}emp';
UniQuery.Open;复制代码

在此示例中,仅在启用SCHEMA宏时才添加点。UniDAC提供了一组有用的预定义宏,可以帮助你编写通用语句。有关更多信息,请参阅“宏参考”(“宏参考”将会在后续文章中与大家分享)。




转载于:https://juejin.im/post/5c9b3b1551882520a4120b51

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Universal Data Access Components (UniDAC) is a powerful library of nonvisual cross-database data access components for Delphi, Delphi for .NET, C++Builder, and Free Pascal. The UniDAC library is designed to help programmers develop faster and cleaner cross-database applications. UniDAC is a complete replacement for standard database connectivity solutions and presents an efficient native alternative to the Borland Database Engine and dbExpress for access to Oracle, SQL Server, MySQL, InterBase, Firebird, SQLite, DB2, Microsoft Access, Advantage Database Server, Adaptive Server Enterprise, and other databases (using ODBC provider). UniDAC is based on the well-known Data Access Components from Devart such as ODAC, SDAC, MyDAC, IBDAC, and PgDAC. We have joined the experience of long-term successful development into one great product which provides unified access to popular databases such as Oracle, Microsoft SQL Server, MySQL, InterBase, Firebird, SQLite, DB2, Microsoft Access, Advantage Database Server, Adaptive Server Enterprise, and other databases (using ODBC provider). The UniDAC library is actively developed and supported by Devart Team. If you have questions about UniDAC, email the developers at unidac@devart.com or visit UniDAC online at http://www.devart.com/unidac/. Advantages of UniDAC Technology UniDAC is very convenient in setup and usage. It provides transparent server-independent interface for working with different databases. Selected database provider ensures the best way to perform operations on the server. Universal Data Access UniDAC provides transparent server-independent interfaces for working with different databases, and lets you change the client engine for specific server type just by changing single connection option. It means that you can easily switch between database servers in your cross-database UniDAC-based application. Server-Aware Providers UniDAC chooses the best way specific to the server to perform most operations. Every UniDAC data provider uses server-specific native connectivity. All operations with data are performed by providers automatically considering peculiarities of the selected database server. Optimized Code The goal of UniDAC is to enable developers to write efficient and flexible database applications. The UniDAC library is implemented using advanced data access algorithms and optimization techniques. Classes and components undergo comprehensive performance tests and are designed to help you write high-performance, lightweight data access layers. Compatibility with Other Connectivity Methods The UniDAC interface retains compatibility with standard VCL data access components like BDE. Existing BDE-based applications can be easily migrated to UniDAC and enhanced to take advantage of server-specific features. Development and Support UniDAC is a cross-database connectivity solution that has been actively developed and supported. UniDAC comes with full documentation, demo projects, and fast (usually within one business day) technical support by the UniDAC development team. Find out more about how to get help or submit feedback and suggestions to the UniDAC development team in the Getting Support topic. A description of the UniDAC components is provided in the Component List.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值