-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathSqlHelper.py
More file actions
147 lines (123 loc) · 5.32 KB
/
SqlHelper.py
File metadata and controls
147 lines (123 loc) · 5.32 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
# coding:utf-8
import datetime
from sqlalchemy import Column, Integer, String, DateTime, Numeric, create_engine, VARCHAR
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from config import DB_CONFIG, DEFAULT_SCORE
from db.ISqlHelper import ISqlHelper
'''
sql操作的基类
包括ip,端口,types类型(0高匿名,1透明),protocol(0 http,1 https http),country(国家),area(省市),updatetime(更新时间)
speed(连接速度)
'''
BaseModel = declarative_base()
class Proxy(BaseModel):
__tablename__ = 'proxys'
id = Column(Integer, primary_key=True, autoincrement=True)
ip = Column(VARCHAR(16), nullable=False)
port = Column(Integer, nullable=False)
types = Column(Integer, nullable=False)
protocol = Column(Integer, nullable=False, default=0)
country = Column(VARCHAR(100), nullable=False)
area = Column(VARCHAR(100), nullable=False)
updatetime = Column(DateTime(), default=datetime.datetime.utcnow)
speed = Column(Numeric(5, 2), nullable=False)
score = Column(Integer, nullable=False, default=DEFAULT_SCORE)
class SqlHelper(ISqlHelper):
params = {'ip': Proxy.ip, 'port': Proxy.port, 'types': Proxy.types, 'protocol': Proxy.protocol,
'country': Proxy.country, 'area': Proxy.area, 'score': Proxy.score}
def __init__(self):
if 'sqlite' in DB_CONFIG['DB_CONNECT_STRING']:
connect_args = {'check_same_thread': False}
self.engine = create_engine(DB_CONFIG['DB_CONNECT_STRING'], echo=False, connect_args=connect_args)
else:
self.engine = create_engine(DB_CONFIG['DB_CONNECT_STRING'], echo=False)
DB_Session = sessionmaker(bind=self.engine)
self.session = DB_Session()
def init_db(self):
BaseModel.metadata.create_all(self.engine)
def drop_db(self):
BaseModel.metadata.drop_all(self.engine)
def insert(self, value):
proxy = Proxy(ip=value['ip'], port=value['port'], types=value['types'], protocol=value['protocol'],
country=value['country'],
area=value['area'], speed=value['speed'])
self.session.add(proxy)
self.session.commit()
def delete(self, conditions=None):
if conditions:
conditon_list = []
for key in list(conditions.keys()):
if self.params.get(key, None):
conditon_list.append(self.params.get(key) == conditions.get(key))
conditions = conditon_list
query = self.session.query(Proxy)
for condition in conditions:
query = query.filter(condition)
deleteNum = query.delete()
self.session.commit()
else:
deleteNum = 0
return ('deleteNum', deleteNum)
def update(self, conditions=None, value=None):
'''
conditions的格式是个字典。类似self.params
:param conditions:
:param value:也是个字典:{'ip':192.168.0.1}
:return:
'''
if conditions and value:
conditon_list = []
for key in list(conditions.keys()):
if self.params.get(key, None):
conditon_list.append(self.params.get(key) == conditions.get(key))
conditions = conditon_list
query = self.session.query(Proxy)
for condition in conditions:
query = query.filter(condition)
updatevalue = {}
for key in list(value.keys()):
if self.params.get(key, None):
updatevalue[self.params.get(key, None)] = value.get(key)
updateNum = query.update(updatevalue)
self.session.commit()
else:
updateNum = 0
return {'updateNum': updateNum}
def select(self, count=None, conditions=None):
'''
conditions的格式是个字典。类似self.params
:param count:
:param conditions:
:return:
'''
if conditions:
conditon_list = []
for key in list(conditions.keys()):
if self.params.get(key, None):
conditon_list.append(self.params.get(key) == conditions.get(key))
conditions = conditon_list
else:
conditions = []
query = self.session.query(Proxy.ip, Proxy.port, Proxy.protocol, Proxy.score)
if len(conditions) > 0 and count:
for condition in conditions:
query = query.filter(condition)
return query.order_by(Proxy.score.desc(), Proxy.speed).limit(count).all()
elif count:
return query.order_by(Proxy.score.desc(), Proxy.speed).limit(count).all()
elif len(conditions) > 0:
for condition in conditions:
query = query.filter(condition)
return query.order_by(Proxy.score.desc(), Proxy.speed).all()
else:
return query.order_by(Proxy.score.desc(), Proxy.speed).all()
def close(self):
pass
if __name__ == '__main__':
sqlhelper = SqlHelper()
sqlhelper.init_db()
proxy = {'ip': '192.168.1.1', 'port': 80, 'type': 0, 'protocol': 0, 'country': '中国', 'area': '广州', 'speed': 11.123, 'types': ''}
sqlhelper.insert(proxy)
sqlhelper.update({'ip': '192.168.1.1', 'port': 80}, {'score': 10})
print(sqlhelper.select(1))