一次编写,随处运行(3)

If the above does not work for you there is always the option of getting the package directly from the PEAR MDB homepage. The URL is listed at the bottom of the article.
如果前面的过程对你来说不管用,总是有从 PEAR MDB 主页中直接获得包的选项。URL 列于文章的最后。

Making use of data type abstraction
利用数据类型抽象

Since most databases tend to have some specialities or quirks it is important for MDB to hide these differences from the developer. MDB achieves this by defining its own internal data types: text, boolean, integer, decimal, float, date, time, time stamp, large objects (files). All data that is passed to and from the database may be converted from MDB's internal format to the databases internal format. The accompanying example scripts to this section can be found in the datatype directory. Let us look at the following query:
因为大部分数据库倾向于有一些个性或者怪癖,对于MDB来说把这些不同之处给开发者隐藏起来非常重要。MDB 通过定义自己的内部数据类型来达到这点:text,boolean,integer,decimal,float,date,time,time stamp,large objects(文件)。所有传递给数据库和从数据库获取的数据都能转换成 MDB 的内部格式或者从数据库的内部格式转化回来。本节相关的例子脚本能够再 datatype 目录中找到。让我们看看下面的查询:

$session = '098f6bcd4621d373cade4e832627b4f6';
// set time out to 30 minutes
$timeout = time()+60*30;
// SELECT query showing how the datatype conversion works
$query = 'SELECT createtime, user_id FROM sessions';
$query .= ' WHERE session = '.$session;
$query .= ' AND lastaccess < '.$timeout;

This query will most likely fail if it were send to a database. The reason being that the value stored in $name would need to be converted to the correct string format. This would mean the contents of $name would have to have special characters escaped and quotes placed around. PEAR DB provides the method DB:.quote() for this. In MDB the method is called MDB::getTextValue(). The difference is that MDB offers such a method for every data type listed above. So we can also convert $timeout to the correct format.
这个查询如果发送给数据库的话八成要失败。原因是存储在 $name 中的值需要转换为正确的字符串格式。这也许意味着 $name 的内容可能有特殊的转义字符或者被引号包围。PEAR DB 为此提供了方法 DB:.quote()。在 MDB 中这个方法叫 MDB::getTextValue()。不同之处是 MDB 给每种前面所列的数据类型都提供了这样的函数。因而我们也能够把 $timeout 转换为正确的格式。

// convert $timeout to the MDB timestamp format
$timeout = MDB_date::unix2Mdbstamp($timeout);
// SELECT query showing how the datatype conversion works
$query = 'SELECT createtime, user_id FROM sessions';
$query .= ' WHERE session = '.$mdb->getTextValue($session);
$query .= ' AND lastaccess < '.$mdb->getTimestampValue($timeout);

For the sake of the example let us assume that we only want to retrieve the first row. MDB::queryRow() fetches the first row, he frees the result set and returns the content, so it is exactly what we want.
为了作个演示,让我们假定我仅仅想要获取第一行。MDB::queryRow() 获得第一行,它释放结果集并且返回其内容,因而它正是我们所要的。

$result = $mdb->queryRow($query);

But different RDBMS return data like dates in different formats. So, if I then want to do some date arithmetic it is important that data is always returned in the same format regardless of the RDBMS chosen. This can be done semi-automatically by MDB. All you need to do is tell MDB what type your result columns will have and MDB handles the conversion. The easiest way is to pass such information with the query method call:
但是不同的 RDBMS 返回像日期这样的数据时用的格式是不同的。因此,如果我们然后要对一些数据进行计算,不管选择的 RDBMS 是什么,把数据以相同的格式返回是重要的。这个可以由 MDB 半自动地完成。你所有需要做的是告诉你的结果列将是什么样的类型,MDB将处理转换的工作。最简单的办法是把这样的信息传递给查询函数。

$types = array('timestamp', 'integer');
$result = $mdb->queryRow($query, $types);

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/03bdd9fbbb1e43b845f41708130fdd16.html