-
Notifications
You must be signed in to change notification settings - Fork 55
/
mysqlLib.py
235 lines (223 loc) · 8.69 KB
/
mysqlLib.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env python
#-*- coding: utf-8 -*-
from logLib import *
import datetime
import MySQLdb
host='XXX'
user='XXX'
passwd='XXX
db='db_pb'
port=33306
from dynsqlLib import *
class mysqlLib():
def __init__(self,host=host,user=user,passwd=passwd,db=db,port=port,charset='utf8'):
try:
self.host=host
self.user=user
self.passwd=passwd
self.db=db
self.port=port
self.charset=charset
self.conn=MySQLdb.connect(host=self.host,user=self.user,passwd=self.passwd,db=self.db,port=self.port,charset=self.charset)
self.cursor=self.conn.cursor()
except Exception as e:
logging.error(str(e))
def close(self):
try:
self.cursor.close()
self.conn.close()
except Exception as e:
logging.error(str(e))
##################################################################server操作####################################################
def add_server(self,param):
try:
sql="""insert into tbl_server(id,name,exenum,workspace,ip,username,password,belong,descpt) values(NULL,%s,%s,%s,%s,%s,%s,%s,%s)"""
n=self.cursor.execute(sql,param)
last_id=int(self.cursor.lastrowid)
self.conn.commit()
return n,last_id
except Exception as e:
logging.error(str(e))
return 0,0
def query_server(self,param):
try:
s=DynSql("""select id,name,exenum,workspace,ip,username,password,belong,descpt from tbl_server where 1=1
{ and id=$id} { and belong=$belong} { and name=$name}
ORDER BY id DESC {limit {$offset,} $limit}""")
sql=s(param)
cnt=self.cursor.execute(sql[0],sql[1])
ret=self.cursor.fetchall()
return ret
except Exception as e:
logging.error(str(e))
def query_server_totalcnt(self,param):
try:
s=DynSql("""select count(*) from tbl_server where 1=1
{ and belong=$belong}
""")
sql=s(param)
cnt=self.cursor.execute(sql[0],sql[1])
ret=self.cursor.fetchall()
totalcnt=ret[0][0]
return ret
except Exception as e:
logging.error(str(e))
def del_server(self,param):
try:
sql="delete from tbl_server where id=%s"
n=self.cursor.execute(sql,param)
self.conn.commit()
return n
except Exception as e:
logging.error(str(e))
##################################################################testdata操作####################################################
def add_testdata(self,param):
try:
sql="""insert into tbl_testdata(id,type,name,belong,filenum,descpt) values(NULL,%s,%s,%s,%s,%s)"""
n=self.cursor.execute(sql,param)
last_id=int(self.cursor.lastrowid)
self.conn.commit()
return n,last_id
except Exception as e:
logging.error(str(e))
return 0,0
def query_testdata(self,param):
try:
s=DynSql("""select id,name,type,belong,filenum,descpt from tbl_testdata where 1=1
{ and id=$id} { and belong=$belong} { and type=$datatype}
ORDER BY id DESC {limit {$offset,} $limit}""")
sql=s(param)
cnt=self.cursor.execute(sql[0],sql[1])
ret=self.cursor.fetchall()
return ret
except Exception as e:
logging.error(str(e))
def query_testdata_totalcnt(self,param):
try:
s=DynSql("""select count(*) from tbl_testdata where 1=1
{ and belong=$belong} { and type=$datatype}
""")
sql=s(param)
cnt=self.cursor.execute(sql[0],sql[1])
ret=self.cursor.fetchall()
totalcnt=ret[0][0]
return ret
except Exception as e:
logging.error(str(e))
def add_datastr(self,param):
try:
sql="""insert into tbl_datafile(id,testdata_id,filename,filestr) values(NULL,%s,%s,%s)"""
n=self.cursor.execute(sql,param)
last_id=int(self.cursor.lastrowid)
self.conn.commit()
return n,last_id
except Exception as e:
logging.error(str(e))
return 0,0
def del_testdata(self,param):
try:
sql="delete from tbl_testdata where id=%s"
n=self.cursor.execute(sql,param)
self.conn.commit()
return n
except Exception as e:
logging.error(str(e))
def del_datafile(self,param):
try:
sql="delete from tbl_datafile where testdata_id=%s"
n=self.cursor.execute(sql,param)
self.conn.commit()
return n
except Exception as e:
logging.error(str(e))
def query_datafile(self,param):
try:
s=DynSql("""select filename,filestr from tbl_datafile where 1=1
{ and testdata_id=$testdata_id}
ORDER BY id DESC {limit {$offset,} $limit}""")
sql=s(param)
cnt=self.cursor.execute(sql[0],sql[1])
ret=self.cursor.fetchall()
return ret
except Exception as e:
logging.error(str(e))
##################################################################download stat操作####################################################
def add_downloadstat(self,param):
try:
sql="""insert into tbl_downloadstat(id,type,user,time) values(NULL,%s,%s,%s)"""
n=self.cursor.execute(sql,param)
last_id=int(self.cursor.lastrowid)
self.conn.commit()
return n,last_id
except Exception as e:
logging.error(str(e))
return 0,0
def query_downloadstat(self,period,param):
try:
if(period == 1):
s=DynSql("""select type,count(*) from tbl_downloadstat where 1=1
{ and id=$id} and DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(time)
group by type order by type""")
else:
s=DynSql("""select type,count(*) from tbl_downloadstat where 1=1
{ and id=$id}
group by type order by type""")
sql=s(param)
#print sql[0] %sql[1]
cnt=self.cursor.execute(sql[0],sql[1])
ret=self.cursor.fetchall()
return ret
except Exception as e:
logging.error(str(e))
##################################################################task操作####################################################
def add_task(self, param):
try:
sql="""insert into tbl_task(id,submit,jenkinsurl,jobname,build_params,status) values(NULL,%s,%s,%s,%s,%s)"""
n=self.cursor.execute(sql,param)
last_id=int(self.cursor.lastrowid)
self.conn.commit()
return n,last_id
except Exception as e:
logging.error(str(e))
return 0,0
def update_task_status(self,param):
try:
sql="""update tbl_task set status=%s where id=%s"""
n=self.cursor.execute(sql,param)
self.conn.commit()
return n
except Exception as e:
logging.error(str(e))
def update_task_build_number(self,param):
try:
sql="""update tbl_task set build_number=%s where id=%s"""
n=self.cursor.execute(sql,param)
self.conn.commit()
return n
except Exception as e:
logging.error(str(e))
def query_task(self, param):
try:
s=DynSql("""select id,submit,jenkinsurl,jobname,build_params,status,build_number from tbl_task where 1=1
{ and id=$id} { and submit=$submit}
ORDER BY id DESC {limit {$offset,} $limit}""")
sql=s(param)
cnt=self.cursor.execute(sql[0],sql[1])
ret=self.cursor.fetchall()
return ret
except Exception as e:
logging.error(str(e))
def query_task_totalcnt(self,param):
try:
s=DynSql("""select count(*) from tbl_task where 1=1
{ and belong=$belong}
""")
sql=s(param)
cnt=self.cursor.execute(sql[0],sql[1])
ret=self.cursor.fetchall()
totalcnt=ret[0][0]
return ret
except Exception as e:
logging.error(str(e))
if __name__ == "__main__":
pass