文本处理学习笔记1

环境情况如下:
python-2.5.2
ibatis-2.3.4.726
jdk1.6.0_07
Oracle9i Enterprise Edition 9.2.0.1.0

一个简单的ibatis配置文件生成工具:
测试文件如下(test.sql):

-----------------------------------------------------
-- Export file for user PS --
-- Created by administrator on 2010/1/26, 10:56:19 --
-----------------------------------------------------

spool test.log

prompt
prompt Creating table TAB_BSC
prompt =====================
prompt
create table PS.TAB_BSC
(
TB_ID NUMBER(10, 0),
TB_NAME VARCHAR2(50),
TB_TIME DATE,
TB_VALUE NUMBER(10, 5)
)
tablespace PSTBS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);

prompt
prompt Creating table TAB_CELL
prompt ======================
prompt
create table PS.TAB_CELL
(
TC_ID NUMBER(10, 0),
TC_NAME VARCHAR2(50),
TC_TIME DATE,
TC_VALUE NUMBER(10, 5)
)
tablespace PSTBS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);

spool off

生成工具的Python代码如下(ibatisGen.py):

# coding:gbk

import os
import re

def ibatisxml(rawsqls):
tabs = re.findall("(create table PS.(\w+).+?;)", rawsqls, re.DOTALL)

for i in tabs:
tabU = i[1]
cols = re.findall(" ([_A-Z0-9]+)\s+([_A-Z0-9]+)", i[0])

txml = ["""<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">"""]
columnStr = ",\n".join([""" %s"""%j[0] for j in cols])
valueStr = ",\n".join([""" #%s#"""%j[0] for j in cols])

txml.append("""<sqlMap namespace="%(tabU)s">
<typeAlias alias="%(tabU)s" type="com.test.beans.%(tabU)s"/>
<select id="selectSource%(tabU)s" parameterClass="%(tabU)s" resultClass="%(tabU)s">
<![CDATA[
select
%(columnStr)s
from %(tabU)s
]]>
</select>
<insert id="insertTarget%(tabU)s" parameterClass="%(tabU)s">
<![CDATA[
insert into %(tabU)s (
%(columnStr)s
) values (
%(valueStr)s
)
]]>
</insert>
<delete id="deleteTarget%(tabU)s" parameterClass="%(tabU)s">
<![CDATA[
delete from %(tabU)s where 1=1
]]>
</delete>
</sqlMap>
"""%locals())

if not os.path.exists("ibatisxml"):
os.makedirs("ibatisxml")
ofile = file("ibatisxml/%s.xml"%i[1], "w")
ofile.write("\n".join(txml))
ofile.close()

def javaclass(rawsqls):
tabs = re.findall("(create table PS.(\w+).+?;)", rawsqls, re.DOTALL)

for vv in tabs:
tabU = vv[1]

tclass = []
cols = re.findall(" ([_A-Z0-9]+)\s+([_A-Z0-9]+)", vv[0])
tclass.append("""package com.test.beans;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;

public class %(tabU)s implements Serializable {

private static final long serialVersionUID = 1L;
"""%locals())

for iv, cv in enumerate(cols):
colU = cv[0]
colL = cv[0].capitalize()
colT = "String"
if cv[1]=="TIMESTAMP" or cv[1]=="DATE":
colT = "Timestamp"
elif cv[1]=="INTEGER" or cv[1]=="FLOAT" or cv[1]=="NUMBER":
colT = "BigDecimal"
tclass.append(""" public %(colT)s get%(colU)s() {
return %(colU)s;
}

public void set%(colU)s(%(colT)s %(colL)s) {
%(colU)s = %(colL)s;
}
"""%locals())

for iv, cv in enumerate(cols):
colU = cv[0]
colL = cv[0].capitalize()
colT = "String"
if cv[1]=="TIMESTAMP" or cv[1]=="DATE":
colT = "Timestamp"
elif cv[1]=="INTEGER" or cv[1]=="FLOAT" or cv[1]=="NUMBER":
colT = "BigDecimal"
tclass.append(""" public %(colT)s %(colU)s;"""%locals())

tclass.append("}")

if not os.path.exists("javaclass"):
os.makedirs("javaclass")
ofile = file("javaclass/%s.java"%tabU, "w")
ofile.write("\n".join(tclass))
ofile.close()

ifile = file("test.sql", "r")
rsql = ifile.read()
ifile.close()
ibatisxml(rsql)
javaclass(rsql)



来看看测试结果吧,
ibatisxml文件夹中生成两个文件:

(TAB_BSC.xml):

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="TAB_BSC">
<typeAlias alias="TAB_BSC" type="com.test.beans.TAB_BSC"/>
<select id="selectSourceTAB_BSC" parameterClass="TAB_BSC" resultClass="TAB_BSC">
<![CDATA[
select
TB_ID,
TB_NAME,
TB_TIME,
TB_VALUE
from TAB_BSC
]]>
</select>
<insert id="insertTargetTAB_BSC" parameterClass="TAB_BSC">
<![CDATA[
insert into TAB_BSC (
TB_ID,
TB_NAME,
TB_TIME,
TB_VALUE
) values (
#TB_ID#,
#TB_NAME#,
#TB_TIME#,
#TB_VALUE#
)
]]>
</insert>
<delete id="deleteTargetTAB_BSC" parameterClass="TAB_BSC">
<![CDATA[
delete from TAB_BSC where 1=1
]]>
</delete>
</sqlMap>


(TAB_CELL.xml):

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="TAB_CELL">
<typeAlias alias="TAB_CELL" type="com.test.beans.TAB_CELL"/>
<select id="selectSourceTAB_CELL" parameterClass="TAB_CELL" resultClass="TAB_CELL">
<![CDATA[
select
TC_ID,
TC_NAME,
TC_TIME,
TC_VALUE
from TAB_CELL
]]>
</select>
<insert id="insertTargetTAB_CELL" parameterClass="TAB_CELL">
<![CDATA[
insert into TAB_CELL (
TC_ID,
TC_NAME,
TC_TIME,
TC_VALUE
) values (
#TC_ID#,
#TC_NAME#,
#TC_TIME#,
#TC_VALUE#
)
]]>
</insert>
<delete id="deleteTargetTAB_CELL" parameterClass="TAB_CELL">
<![CDATA[
delete from TAB_CELL where 1=1
]]>
</delete>
</sqlMap>


javaclass文件夹中也生成了两个文件:

(TAB_BSC.java):

package com.test.beans;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;

public class TAB_BSC implements Serializable {

private static final long serialVersionUID = 1L;

public BigDecimal getTB_ID() {
return TB_ID;
}

public void setTB_ID(BigDecimal Tb_id) {
TB_ID = Tb_id;
}

public String getTB_NAME() {
return TB_NAME;
}

public void setTB_NAME(String Tb_name) {
TB_NAME = Tb_name;
}

public Timestamp getTB_TIME() {
return TB_TIME;
}

public void setTB_TIME(Timestamp Tb_time) {
TB_TIME = Tb_time;
}

public BigDecimal getTB_VALUE() {
return TB_VALUE;
}

public void setTB_VALUE(BigDecimal Tb_value) {
TB_VALUE = Tb_value;
}

public BigDecimal TB_ID;
public String TB_NAME;
public Timestamp TB_TIME;
public BigDecimal TB_VALUE;
}


(TAB_CELL.java):

package com.test.beans;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;

public class TAB_CELL implements Serializable {

private static final long serialVersionUID = 1L;

public BigDecimal getTC_ID() {
return TC_ID;
}

public void setTC_ID(BigDecimal Tc_id) {
TC_ID = Tc_id;
}

public String getTC_NAME() {
return TC_NAME;
}

public void setTC_NAME(String Tc_name) {
TC_NAME = Tc_name;
}

public Timestamp getTC_TIME() {
return TC_TIME;
}

public void setTC_TIME(Timestamp Tc_time) {
TC_TIME = Tc_time;
}

public BigDecimal getTC_VALUE() {
return TC_VALUE;
}

public void setTC_VALUE(BigDecimal Tc_value) {
TC_VALUE = Tc_value;
}

public BigDecimal TC_ID;
public String TC_NAME;
public Timestamp TC_TIME;
public BigDecimal TC_VALUE;
}


JavaBean的生成稍微偷了下懒,
使用 Timestamp 接所有的时间,
使用 BigDecimal 接所有的数字,
幸好 java支持这样做,jdbc支持得也不错。
以后用ibatis时可以先导一份表结构,
根据表结构可以生成配置文件,生成JavaBean,
方便了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值