forked from netkiller/netkiller.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgsql.py
More file actions
executable file
·216 lines (189 loc) · 7.08 KB
/
pgsql.py
File metadata and controls
executable file
·216 lines (189 loc) · 7.08 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/python
#############################################################
# Created 2003-12-6
# Modified 2003-12-8
# Project PostgreSQL Converse
# Model Create Java Entity Bean (not EJB CMP)
# Company Organizations
# Author 陈景峰
# Nickname netkiller
# Email netkiller@9812.net
# Version 1.1
# Database PostgreSQL 7.3.4
#############################################################
import pg
import string
#db = pg.connect(dbname='netkiller', host='127.0.0.1', user='netkiller',passwd='chen')
db = pg.DB(dbname='netkiller', host='127.0.0.1', user='netkiller',passwd='chen')
# tab = db.get_tables()
# print tab
#print "====================================================="
table = raw_input("Please enter a table: ")
#table = "users"
#classname = "workfile"
classname = string.capwords(table)
query = "select * from "+table
table_attnames = db.query(query).listfields()
db.close()
#table_attnames = db.get_attnames(table)
buffer = "\n"
buffer += "public class "+classname+"{\n"
buffer += "\n"
for att in table_attnames:
buffer += " private String "+ att +" = null;\n"
buffer += "\n"
buffer += " DBConnect odb = null; \n\n"
buffer += " private ResultSet rs = null; \n\n"
buffer += " private Vector vError = new Vector(); \n\n"
buffer += " public "+ classname+ "(){\n }\n"
buffer += "\n"
for att in table_attnames:
buffer += " public void set"+ string.capwords(att) +"(String value){\n"
buffer += " this."+ att +" = value; \n"
buffer += " }\n"
buffer += " public String get"+ string.capwords(att) +"(){\n"
buffer += " return this."+ att +"; \n"
buffer += " }\n"
buffer += " public void setPrimaryKey(int id){ \n"
buffer += " this.id = id; \n"
buffer += " } \n"
finallys = " finally{ \n"
finallys +=" try{ \n"
finallys +=" if(rs != null)rs.close(); \n"
finallys +=" odb.close(); \n"
finallys +=" }catch(Exception e){ \n"
finallys +=" vError.add(e.toString()); \n"
finallys +=" } \n"
finallys +=" } \n"
buffer += " public void findByPrimaryKey(){ \n"
buffer += " String sql = null; \n"
buffer += " try{ \n"
buffer += " } \n"
buffer += " catch(Exception e){ \n"
buffer += " vError.add(e.toString()); \n"
buffer += " } \n"
buffer += finallys
buffer += " } \n"
buffer += " public void findAll(){} \n"
buffer += " public void findByMethod(){} \n"
sql_insert = "insert into " + table + "("
symbol = "?"
prepare = ""
i = 1
for field in table_attnames:
sql_insert += (field + ",")
symbol += ",?"
prepare += " odb.setString("+ str(i) +",this."+ field +"); \n"
i += 1
sql_insert += ") values(" + prepare + ")"
buffer += " public boolean create"+ string.capwords(classname) +"() { \n"
buffer += " String sql = \""+ sql_insert +"\"; \n"
buffer += " boolean isSuccess = false; \n"
buffer += " try{ \n"
buffer += " odb = new DBConnect();\n"
buffer += " odb.prepareStatement(sql); \n"
buffer += prepare
buffer += " if(odb.executeUpdate()>0) \n"
buffer += " isSuccess = true; \n"
buffer += " } \n "
buffer += " catch(Exception e){ \n"
buffer += " vError.add(e.toString()); \n"
buffer += " } \n"
buffer += finallys
buffer += " return isSuccess; \n"
buffer += " } \n"
buffer += " public boolean remove"+ string.capwords(classname) +"() { \n"
buffer += " String sql = \"delete from "+table+" where id = ? \"; \n";
buffer += " try{ \n"
buffer += " odb = new DBConnect(); \n"
buffer += " odb.prepareStatement(sql); \n"
buffer += " odb.setInt(1,this.id); \n"
buffer += " if(odb.executeUpdate()>0) \n"
buffer += " isSuccess = true; \n"
buffer += " } \n "
buffer += " catch(Exception e){ \n"
buffer += " vError.add(e.toString()); \n"
buffer += " } \n"
buffer += finallys
buffer += " return isSuccess; \n"
buffer += " } \n"
sql_update = "update "+ table +" set "
for field in table_attnames:
sql_update += (field + "=?,")
sql_update += " where id = ?"
buffer += " public boolean update"+ string.capwords(classname) +"() { \n"
buffer += " String sql = \""+ sql_update +"\"; \n"
buffer += " boolean isSuccess = false; \n"
buffer += " try{ \n"
buffer += " isSuccess = true; \n"
buffer += " } \n "
buffer += " catch(Exception e){ \n"
buffer += " vError.add(e.toString()); \n"
buffer += " } \n"
buffer += finallys
buffer += " return isSuccess; \n"
buffer += " } \n"
flush = ""
for field in table_attnames:
flush += (" this."+field + " = null; \n")
buffer += " public void flush(){ \n"
buffer += flush
buffer += " } \n"
buffer += " public static void main(String[] args) { \n"
buffer += " " + classname + " test = new " +classname+ "(); \n"
buffer += " }\n"
buffer +="}\n"
package = "package ebusiness; \n\n"
imports = "import java.sql.*; \n"
imports += "import java.util.*; \n"
imports += "import netkiller.database.*; \n"
imports += "import netkiller.security.*; \n"
f=open(classname+'.java', 'w')
f.write(package);
f.write(imports);
f.write(buffer)
f.close()
#for r in db.query("select * from oa.meeting").dictresult():
# print '%(id)s %(subject)s' % r
def MakeHtml(html):
tag = "<!-- db==>html form author:netkiller --> \n"
tag +="<form name=\"form\" action=\"\" > \n"
tag += html
tag += "</form>"
return tag
def MakeJsp(jsp):
tag = "<% \n"
tag += jsp
tag += "%> \n"
return tag
html = "<table border=0> \n"
jsp = "jspParameter para = new jspParameter(request); \n"
jsp += " String op = request.getParameter(\"op\") \n"
jsp += " String status = ""; \n"
for att in table_attnames:
jsp += " String "+ att +" = para.getString(\""+ att +"\",\"\"); \n"
html += " <tr><td>"+ att+":</td><td><input name=\""+ att +"\" type=\"text\" value=\"<%= "+att+" %>\"></td></tr> \n"
jsp += " if(op != null)){ \n"
jsp += " if(op.equals(\"create\")){ \n"
jsp += " status = "<script> alert('添加成功!') </script>" \n"
jsp += " } \n"
jsp += " if(op.equals(\"remove\")){} \n"
jsp += " if(op.equals(\"remove\")){} \n"
jsp += " if(op.equals(\"update\")){} \n"
jsp += " } \n"
html += "</table> \n"
html += "<%= status %>"
details = "<% \n"
details += "jspParameter para = new jspParameter(request); \n"
details += " int id = para.getInt(\"id\"); \n"
details += " setPrimaryKey(id) \n"
details += " findByPrimaryKey() \n"
details += "<table border=0> \n"
for att in table_attnames:
details += " <tr><td>"+att+":</td><td><%= "+att+" %></td></tr> \n"
details +="</table> \n"
f=open('html/'+classname+'.html','w')
f.write(MakeJsp(jsp))
f.write(details)
f.write(MakeHtml(html))
f.close