-->

虫虫的技术博客 技术 生活

Tuesday, June 12, 2012

Torque源码之二

在使用torque的过程中,generator的自动生成代码很是方便,有点遗憾的是每次生成代码之后要手动的替换一些东西,数据库名,和主键自动生成的时候设置主键生成策略,然后才能使用。
为什么框架不能实现的更好一点,一次性生成可用的代码呢?
问题1,generator在执行jdbc这个target过程中,生成的schema文件中database元素的name属性是root,并不是数据库的名,跟踪代码发现这个值是根据配置中的dbUser的值来赋值的,怪不得是root因为数据库连接的用户名是root。
没明白为什么database的name属性是根据dbUser来给数据库name赋值,把它改成dbSchema,然后在作配置的时候,指定schema为数据库名就可以了
201行改为
databaseNode.setAttribute( "name", dbSchema);
问题2,同样是在generator在执行jdbc这个target过程中,数据库中表的主键为自增长的时候,生成的schema文件的database元素要有idMethod=“native”属性值,否则在使用过程中实体的insert方法执行完成后并没有把这条数据的ID带回来,很是不方便。
继续跟踪源代码发现在实现过程中并没有考虑主键生成策略这个因素,无奈自己顺藤摸瓜把这个属性给加上吧。
大致过程是在从数据库中把表结构取出来的时候记录下来表的主键生成策略,如果为自动生成,
在表下增加属性table.setAttribute(“idMethod”, “native”);
getColumns方法中
String is_autoincrement = columnSet.getString(23);
//。。。
col.add(decimalDigits);
col.add(is_autoincrement);
columns.add(col);
//。。。
剩下就是在组织xml结构的时候把这个属性加上
generateXML方法中增加
boolean isautoincrement = false;
if (isautoincrement) {
table.setAttribute( "idMethod", "native");
}
由于代码比较简单,也没有复杂的逻辑,但是改动的代码不是很集中,所以剩下的就直接看附件的类文件
TorqueJDBCTransformTask
torque源码下载地址:
http://svn.apache.org/repos/asf/db/torque/generator/tags/TORQUE_3_3
补充一下torque的知识
torque of apache
Torque项目是Apache的公开源代码项目,主要用于生成访问数据库的资源和java代码、提供使用这些代码访问数据库的运行时 (runtime)环境。通过使用Torque,你可以使用面向对象方式访问数据库,不再需要编写任何SQL语句。目前Torque支持的数据库包括 mysql、oracle、sqlserver、db2等,还包括对weblogic的数据源的支持。
引自百科
http://baike.baidu.com/view/2089710.htm
torque主页
http://db.apache.org/torque/

2 comments:

  1. Then you're going to love this quick 1xbet paced variant of an already exciting game. One of an important rules in Spanish 21, that extremely differs from blackjack, is the rule that a player’s 21 all the time beats a dealer’s 21. Embers is a smoke-friendly casino, features a state-of-the-art air air flow system to make your visit pleasant. You don’t have to fold until you see all the group cards. Win the Bonus Jackpot on Emperor’s Challenge with a further $1 Wager. You can play the Emperor’s Treasure and qualify for the Envy with $5 or extra.

    ReplyDelete
  2. It is managed 다파벳 by Betsoft and Nucleus Gaming with in extra of four hundred games. When things go wrong, it’s necessary to know somebody has your back. Game developer Microgaming supplies this slot onSpin Casino.

    ReplyDelete

Popular Posts

Copyright © 虫虫的成长历程 | Powered by Blogger Design by PWT | Blogger Theme by NewBloggerThemes.com