-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibatis-xml-generator.py
158 lines (129 loc) · 4.98 KB
/
ibatis-xml-generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
filename="ServerMount"
tablename="server_mount"
databaseList=['id','server_id','mount_name','mount_point']
def underline_to_camel(underline_format):
camel_format = ''
if isinstance(underline_format,str):
if underline_format.find("_")!=-1:
i=0
for _s_ in underline_format.split('_'):
if i!=0:
_s_=_s_[0].upper()+_s_[1:]
camel_format += _s_
i+=1
else:
camel_format=underline_format
return camel_format
propertyList=[]
for field in databaseList:
camel_field = underline_to_camel(field)
propertyList.append(camel_field)
print propertyList
def generateResultMapByPropeties(filename,databaseList,propetyList):
str="<resultMap id=\""+filename+"Map\""+" class=\""+filename+"\">\n";
i=0
for property in propertyList:
line="<result property=\""+property+"\""+" column=\""+databaseList[i]+"\"/>\n"
str=str+line
i=i+1
str=str+"</resultMap>\n"
return str
def generateAllColumn(databaseList):
str="<sql id=\"allColumn\">\n"
for property in databaseList:
str=str+property+",\n"
str=str+"</sql>\n"
str=str.replace(",\n</sql>","\n</sql>")
return str
def generateRetrieve(filename,databaseList,propertyList):
str="<select id=\"retrieve\" parameterClass=\"java.util.HashMap\" resultMap=\""+filename+"Map"+"\">\n"
str=str+"select\n"
str=str+"<include refid=\"allColumn\" />\n"
str=str+"from "+tablename+"\n"
str=str+"<dynamic prepend=\"where\">\n"
i=0
for property in propertyList:
str=str+"<isNotEmpty prepend=\"and\" property=\""+property+"\">\n"
str=str+"<![CDATA["+databaseList[i]+"=#"+property+"#]]>\n"
str=str+"</isNotEmpty>\n"
i=i+1
str=str+"</dynamic>\n"
str=str+"</select>\n"
return str
def generateList(filename,databaseList,propertyList):
str="<select id=\"list\" parameterClass=\"java.util.HashMap\" resultMap=\""+filename+"Map"+"\">\n"
str=str+"select\n"
str=str+"<include refid=\"allColumn\" />\n"
str=str+"from "+tablename+"\n"
str=str+"<dynamic prepend=\"where\">\n"
i=0
for property in propertyList:
str=str+"<isNotEmpty prepend=\"and\" property=\""+property+"\">\n"
str=str+"<![CDATA["+databaseList[i]+"=#"+property+"#]]>\n"
str=str+"</isNotEmpty>\n"
i=i+1
str=str+"</dynamic>\n"
str=str+"</select>\n"
return str
def generateInsert(filename,propertyList):
str="<insert id=\"create\" parameterClass=\""+filename+"\">\n"
str=str+"insert into "+tablename+"\n"
str=str+"(\n"
str=str+"<include refid=\"allColumn\" />\n"
str=str+")\n"
str=str+"values(\n"
for property in propertyList:
str=str+"#"+property+"#,"
str=str+")"
str=str.replace("#,)","#)")
str=str+";\n"
str=str+"<selectKey resultClass=\"java.lang.Integer\" keyProperty=\"id\">\n"
str=str+"select last_insert_id() as id from "+tablename+" limit 1\n"
str=str+"</selectKey>\n"
str=str+"</insert>"
return str
def generateDelete(filename):
str="<delete id=\"delete\" parameterClass=\"java.lang.Integer\">\n"
str=str+"delete from "+tablename+"\n"
str=str+"where\n"
str=str+"id=#id#\n"
str=str+"</delete>\n"
return str
def generateUpdate(filename,databaseList,propertyList):
str="<update id=\"update\" parameterClass=\""+filename+"\">\n"
str=str+"update "+tablename+"\n"
str=str+"<dynamic prepend=\"set\">\n"
i=0
for property in propertyList:
str=str+"<isNotEmpty prepend=\",\" property=\""+property+"\">\n"
str=str+"<![CDATA["+databaseList[i]+"=#"+property+"#]]>\n"
str=str+"</isNotEmpty>\n"
i=i+1
str=str+"</dynamic>\n"
str=str+"<dynamic prepend=\"where\">\n"
str=str+"<isNotEmpty prepend=\"and\" property=\"id\">\n"
str=str+"<![CDATA[id=#id#]]>\n"
str=str+"</isNotEmpty>\n"
str=str+"</dynamic>\n"
str=str+"</update>\n"
return str
def generateTotal(filename,databaseList,propertyList):
str="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
str=str+"<!DOCTYPE sqlMap PUBLIC \n"
str=str+" \"-//ibatis.apache.org//DTD SQL Map 2.0//EN\" \n"
str=str+"\"http://ibatis.apache.org/dtd/sql-map-2.dtd\">\n"
str=str+"<sqlMap namespace=\""+filename+"\">\n"
str=str+"<typeAlias alias=\""+filename+"\" "+"type=\"com.netease.sentry.biz.domain."+filename+"\"></typeAlias>\n"
str=str+generateResultMapByPropeties(filename,databaseList,propertyList)+"\n"
str=str+generateAllColumn(databaseList)+"\n"
str=str+generateRetrieve(filename,databaseList,propertyList)+"\n"
str=str+generateInsert(filename,propertyList)+"\n"
str=str+generateUpdate(filename,databaseList,propertyList)+"\n"
str=str+generateDelete(filename)+"\n"
str=str+generateList(filename,databaseList,propertyList)+"\n"
str=str+"</sqlMap>"
return str
str=generateTotal(filename,databaseList,propertyList)
print str
file_w=open(filename+".xml","a")
file_w.write(str);