Skip to content

Commit 63bfdec

Browse files
committed
更新postgres工具类
1 parent cb8e4e0 commit 63bfdec

1 file changed

Lines changed: 56 additions & 52 deletions

File tree

tools/postgres.py

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import json
66
from datetime import date, datetime
77

8+
from log import Logger
9+
logger = Logger('postgres', 'postgres.log')
10+
# logger.get_memory_usage()
11+
812

913
# 本地环境
1014
db_config_local = {
@@ -44,7 +48,7 @@ def __init__(self, db_config, db_name=None):
4448
port=self.db_config['port']
4549
)
4650
except Exception, e:
47-
print e
51+
logger.error(e)
4852

4953
@staticmethod
5054
def __default(obj):
@@ -86,39 +90,39 @@ def get_columns_name(self, table_name):
8690
:return:
8791
"""
8892
if self.is_conn_open() is False:
89-
print '连接已断开'
93+
logger.error('连接已断开')
9094
return []
9195
try:
9296
# 参数判断
9397
if table_name is None:
94-
print '查询表名缺少参数'
98+
logger.error('查询表名缺少参数')
9599
return []
96100
cursor = self.conn.cursor()
97101
sql = "select column_name from information_schema.columns where table_name = '%s'" % table_name
98-
print sql
102+
logger.info(sql)
99103
cursor.execute(sql)
100104
result = cursor.fetchall()
101105
row = [item[0] for item in result]
102106
cursor.close()
103107
return row
104108
except Exception, e:
105-
print e
109+
logger.error(e)
106110

107111
def get_row(self, table_name, condition=None):
108112
"""
109113
获取单行数据
110114
:return:
111115
"""
112116
if self.is_conn_open() is False:
113-
print '连接已断开'
117+
logger.error('连接已断开')
114118
return None
115119
try:
116120
# 参数判断
117121
if table_name is None:
118-
print '查询表名缺少参数'
122+
logger.error('查询表名缺少参数')
119123
return None
120124
if condition and not isinstance(condition, list):
121-
print '查询条件参数格式错误'
125+
logger.error('查询条件参数格式错误')
122126
return None
123127
# 组装查询条件
124128
if condition:
@@ -127,14 +131,14 @@ def get_row(self, table_name, condition=None):
127131
else:
128132
sql_condition = ''
129133
sql = 'select * from %s %s limit 1' % (table_name, sql_condition)
130-
print sql
134+
logger.info(sql)
131135
cursor = self.conn.cursor()
132136
cursor.execute(sql)
133137
row = cursor.fetchone()
134138
cursor.close()
135139
return row
136140
except Exception, e:
137-
print e
141+
logger.error(e)
138142

139143
def get_rows(self, table_name, condition=None, limit='limit 10 offset 0'):
140144
"""
@@ -143,15 +147,15 @@ def get_rows(self, table_name, condition=None, limit='limit 10 offset 0'):
143147
con_obj.get_rows('company', ["type='6'"], 'limit 10')
144148
"""
145149
if self.is_conn_open() is False:
146-
print '连接已断开'
150+
logger.error('连接已断开')
147151
return None
148152
try:
149153
# 参数判断
150154
if table_name is None:
151-
print '查询表名缺少参数'
155+
logger.error('查询表名缺少参数')
152156
return None
153157
if condition and not isinstance(condition, list):
154-
print '查询条件参数格式错误'
158+
logger.error('查询条件参数格式错误')
155159
return None
156160
# 组装查询条件
157161
if condition:
@@ -160,30 +164,30 @@ def get_rows(self, table_name, condition=None, limit='limit 10 offset 0'):
160164
else:
161165
sql_condition = ''
162166
sql = 'select * from %s %s %s' % (table_name, sql_condition, limit)
163-
print sql
167+
logger.info(sql)
164168
cursor = self.conn.cursor()
165169
cursor.execute(sql)
166170
rows = cursor.fetchall()
167171
cursor.close()
168172
return rows
169173
except Exception, e:
170-
print e
174+
logger.error(e)
171175

172176
def get_count(self, table_name, condition=None):
173177
"""
174178
获取记录总数
175179
:return:
176180
"""
177181
if self.is_conn_open() is False:
178-
print '连接已断开'
182+
logger.error('连接已断开')
179183
return 0
180184
try:
181185
# 参数判断
182186
if table_name is None:
183-
print '查询表名缺少参数'
187+
logger.error('查询表名缺少参数')
184188
return 0
185189
if condition and not isinstance(condition, list):
186-
print '查询条件参数格式错误'
190+
logger.error('查询条件参数格式错误')
187191
return 0
188192
# 组装查询条件
189193
if condition:
@@ -192,15 +196,15 @@ def get_count(self, table_name, condition=None):
192196
else:
193197
sql_condition = ''
194198
sql = 'select count(*) from %s %s' % (table_name, sql_condition)
195-
print sql
199+
logger.info(sql)
196200
cursor = self.conn.cursor()
197201
cursor.execute(sql)
198202
row = cursor.fetchone()
199203
count = row[0]
200204
cursor.close()
201205
return count
202206
except Exception, e:
203-
print e
207+
logger.error(e)
204208

205209
def output_row(self, table_name, condition=None, style=0):
206210
"""
@@ -210,18 +214,18 @@ def output_row(self, table_name, condition=None, style=0):
210214
"""
211215
# 参数判断
212216
if not table_name:
213-
print '查询数据缺少参数'
217+
logger.error('查询数据缺少参数')
214218
return None
215219
if condition and not isinstance(condition, list):
216-
print '查询条件参数格式错误'
220+
logger.error('查询条件参数格式错误')
217221
return None
218222
columns_name = self.get_columns_name(table_name)
219223
row = self.get_row(table_name, condition)
220224
if not columns_name:
221-
print '表名不存在'
225+
logger.error('表名不存在')
222226
return None
223227
if not row:
224-
print '记录不存在'
228+
logger.error('记录不存在')
225229
return None
226230
if style == 0:
227231
# 获取字段名称最大的长度值作为缩进依据
@@ -244,18 +248,18 @@ def output_rows(self, table_name, condition=None, limit='limit 10 offset 0', sty
244248
"""
245249
# 参数判断
246250
if not table_name:
247-
print '查询数据缺少参数'
251+
logger.error('查询数据缺少参数')
248252
return None
249253
if condition and not isinstance(condition, list):
250-
print '查询条件参数格式错误'
254+
logger.error('查询条件参数格式错误')
251255
return None
252256
columns_name = self.get_columns_name(table_name)
253257
rows = self.get_rows(table_name, condition, limit)
254258
if not columns_name:
255-
print '表名不存在'
259+
logger.error('表名不存在')
256260
return None
257261
if not rows:
258-
print '记录不存在'
262+
logger.error('记录不存在')
259263
return None
260264
if style == 0:
261265
# 获取字段名称最大的长度值作为缩进依据
@@ -281,15 +285,15 @@ def update(self, table_name, update_field, condition=None):
281285
con_obj.update('company', ["title='标题'", "flag='2'"], ["type='6'"])
282286
"""
283287
if self.is_conn_open() is False:
284-
print '连接已断开'
288+
logger.error('连接已断开')
285289
return False
286290
try:
287291
# 参数判断
288292
if not table_name or not update_field:
289-
print '更新数据缺少参数'
293+
logger.error('更新数据缺少参数')
290294
return False
291295
if not isinstance(update_field, list) or (condition and not isinstance(condition, list)):
292-
print '更新数据参数格式错误'
296+
logger.error('更新数据参数格式错误')
293297
return False
294298
# 组装更新字段
295299
if update_field:
@@ -305,28 +309,28 @@ def update(self, table_name, update_field, condition=None):
305309
sql_condition = ''
306310
# 拼接sql语句
307311
sql = 'update %s %s %s' % (table_name, sql_update_field, sql_condition)
308-
print sql
312+
logger.info(sql)
309313
cursor = self.conn.cursor()
310314
cursor.execute(sql)
311315
self.conn.commit()
312-
print '更新行数:%s' % cursor.rowcount
316+
logger.info('更新行数:%s' % cursor.rowcount)
313317
cursor.close()
314318
return True
315319
except Exception, e:
316-
print e
320+
logger.error(e)
317321

318322
def delete(self, table_name, condition=None):
319323
"""
320324
删除数据
321325
con_obj.delete('company', ["type='6'", "flag='2'"])
322326
"""
323327
if self.is_conn_open() is False:
324-
print '连接已断开'
328+
logger.error('连接已断开')
325329
return False
326330
try:
327331
# 参数判断
328332
if condition and not isinstance(condition, list):
329-
print '删除数据参数格式错误'
333+
logger.error('删除数据参数格式错误')
330334
return False
331335
# 组装删除条件
332336
if condition:
@@ -336,70 +340,70 @@ def delete(self, table_name, condition=None):
336340
sql_condition = ''
337341
# 拼接sql语句
338342
sql = 'delete from %s %s' % (table_name, sql_condition)
339-
print sql
343+
logger.info(sql)
340344
cursor = self.conn.cursor()
341345
cursor.execute(sql)
342346
self.conn.commit()
343-
print '删除行数:%s' % cursor.rowcount
347+
logger.info('删除行数:%s' % cursor.rowcount)
344348
cursor.close()
345-
print '删除成功'
349+
logger.info('删除成功')
346350
return True
347351
except Exception, e:
348-
print e
352+
logger.error(e)
349353

350354
def query_by_sql(self, sql=None):
351355
"""
352356
根据sql语句查询
353357
:return:
354358
"""
355359
if self.is_conn_open() is False:
356-
print '连接已断开'
360+
logger.error('连接已断开')
357361
return None
358362
if sql is None:
359-
print 'sql语句不能为空'
363+
logger.error('sql语句不能为空')
360364
return None
361365
# 安全性校验
362366
sql = sql.lower()
363367
if not sql.startswith('select'):
364-
print '未授权的操作'
368+
logger.error('未授权的操作')
365369
return None
366370
try:
367371
cursor = self.conn.cursor()
372+
logger.info(sql)
368373
cursor.execute(sql)
369374
rows = cursor.fetchall()
370375
cursor.close()
371-
# print rows
372376
return rows
373377
except Exception, e:
374-
print e
378+
logger.error(e)
375379

376380
def update_by_sql(self, sql=None):
377381
"""
378382
根据sql语句[增删改]
379383
:return:
380384
"""
381385
if self.is_conn_open() is False:
382-
print '连接已断开'
386+
logger.error('连接已断开')
383387
return False
384388
if sql is None:
385-
print 'sql语句不能为空'
389+
logger.error('sql语句不能为空')
386390
return False
387391
# 安全性校验
388392
sql = sql.lower()
389393
if not (sql.startswith('update') or sql.startswith('insert') or sql.startswith('delete')):
390-
print '未授权的操作'
394+
logger.error('未授权的操作')
391395
return False
392396
try:
393-
print sql
397+
logger.info(sql)
394398
cursor = self.conn.cursor()
395399
cursor.execute(sql)
396400
self.conn.commit()
397-
print '影响行数:%s' % cursor.rowcount
401+
logger.info('影响行数:%s' % cursor.rowcount)
398402
cursor.close()
399-
print '执行成功'
403+
logger.info('执行成功')
400404
return True
401405
except Exception, e:
402-
print e
406+
logger.error(e)
403407

404408

405409
def test_51job():

0 commit comments

Comments
 (0)