-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDB_Repository.py
171 lines (118 loc) · 5.04 KB
/
DB_Repository.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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import github
import db_operation
import datetime
NONEINT = -1
class DB_Repository:
"""
This class represents DB_Repository as a database operating class for github.Repository.Repository
"""
repo = None
db = None
table = "Repository"
def __init__(self, repo, db):
self.repo = repo
self.db = db
def open_if_connection_closed(self):
try:
if self.db is None:
return False
if self.db.open == 0:
self.db = db_operation.connect_to_db_simple()
if self.db is None:
return False
return True
except Exception, e:
print e
return False
def save(self):
try:
if self.open_if_connection_closed() == False:
return False
if self.exist():
return True
D = [(a,v) for (a, v) in self.repo.__dict__.iteritems() if \
(a,) not in github.GithubObject.CompletableGithubObject.__dict__.iteritems() \
and (a,) not in github.GithubObject.GithubObject.__dict__.iteritems()]
'''
construct sql statement
'''
sql_header = "insert into %s "%(self.table)
sql_fields = "("
sql_values = "values ("
for (attr, value) in D:
if attr == "_CompletableGithubObject__completed" or attr == "_headers" or \
attr == "_rawData" or attr == "_requester":
continue
if isinstance(value, github.GithubObject._NotSetType):
continue
v = value.value
if isinstance(v, bool):
sql_fields = sql_fields + attr[1:] + ","
if v:
sql_values = sql_values + str(1) + ","
else:
sql_values = sql_values + str(0) + ","
elif isinstance(v, int):
sql_fields = sql_fields + attr[1:] + ","
sql_values = sql_values + str(v) + ","
elif isinstance(v, basestring):
sql_fields = sql_fields + attr[1:] + ","
# escape
v = v.replace("'", "\\'").replace('"', '\\"')
sql_values = sql_values + "'" + v + "',"
elif isinstance(v, datetime.datetime):
sql_fields = sql_fields + attr[1:] + ","
sql_values = sql_values + "'" + str(v) + "',"
elif isinstance(v, github.NamedUser.NamedUser):
sql_fields = sql_fields + attr[1:] + ","
sql_values = sql_values + str(v.id) + ","
elif isinstance(v, github.Organization.Organization):
sql_fields = sql_fields + attr[1:] + ","
sql_values = sql_values + str(v.id) + ","
elif isinstance(v, github.Repository.Repository):
sql_fields = sql_fields + attr[1:] + ","
sql_values = sql_values + str(v.id) + ","
sql = sql_header + sql_fields[:-1] + ") " + sql_values[:-1] + ");"
cursor = self.db.cursor()
cursor.execute(sql)
self.db.commit()
return True
except Exception as e:
print e
return False
def update_one(self, field, value):
try:
if self.open_if_connection_closed() == False:
return False
sql = None
if isinstance(value, basestring):
sql = "update %s set %s='%s' where id=%d;"%(self.table, field, value, self.repo.id)
elif isinstance(value, int):
sql = "update %s set %s=%d where id=%d;"%(self.table, field, value, self.repo.id)
elif isinstance(value, datetime.datetime):
sql = "update %s set %s='%s' where id=%d;"%(self.table, field, value, self.repo.id)
if sql is None:
return False
cursor = self.db.cursor()
cursor.execute(sql)
self.db.commit()
return True
except Exception as e:
print e
return False
def exist(self):
try:
if self.open_if_connection_closed() == False:
return False
cursor = self.db.cursor()
sql = "select count(*) from %s where id=%d;"%(self.table, self.repo.id)
cursor.execute(sql)
result = cursor.fetchall()
if result[0][0] == 0:
return False
return True
except Exception as e:
print e
return False