FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
datajoint-python/datajoint/connection.py at preview_order · mspacek/datajoint-python · GitHub
mspacek
datajoint-python
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
datajoint-python
/
datajoint
/
connection.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
216 lines (183 loc) · 7.8 KB
Breadcrumbs
datajoint-python
/
datajoint
/
connection.py
Copy path
File metadata and controls
216 lines (183 loc) · 7.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""
This module hosts the Connection class that manages the connection to the mysql database,
and the `conn` function that provides access to a persistent connection in datajoint.
"""
import
warnings
from
contextlib
import
contextmanager
import
pymysql
as
client
import
logging
from
getpass
import
getpass
from
.
import
config
from
.
import
DataJointError
from
.
dependencies
import
Dependencies
from
pymysql
import
err
logger
=
logging
.
getLogger
(
__name__
)
def
conn
(
host
=
None
,
user
=
None
,
password
=
None
,
init_fun
=
None
,
reset
=
False
):
"""
Returns a persistent connection object to be shared by multiple modules.
If the connection is not yet established or reset=True, a new connection is set up.
If connection information is not provided, it is taken from config which takes the
information from dj_local_conf.json. If the password is not specified in that file
datajoint prompts for the password.
:param host: hostname
:param user: mysql user
:param password: mysql password
:param init_fun: initialization function
:param reset: whether the connection should be reset or not
"""
if
not
hasattr
(
conn
,
'connection'
)
or
reset
:
host
=
host
if
host
is
not
None
else
config
[
'database.host'
]
user
=
user
if
user
is
not
None
else
config
[
'database.user'
]
password
=
password
if
password
is
not
None
else
config
[
'database.password'
]
if
user
is
None
:
# pragma: no cover
user
=
input
(
"Please enter DataJoint username: "
)
if
password
is
None
:
# pragma: no cover
password
=
getpass
(
prompt
=
"Please enter DataJoint password: "
)
init_fun
=
init_fun
if
init_fun
is
not
None
else
config
[
'connection.init_function'
]
conn
.
connection
=
Connection
(
host
,
user
,
password
,
init_fun
)
return
conn
.
connection
class
Connection
:
"""
A dj.Connection object manages a connection to a database server.
It also catalogues modules, schemas, tables, and their dependencies (foreign keys).
Most of the parameters below should be set in the local configuration file.
:param host: host name
:param user: user name
:param password: password
:param init_fun: connection initialization function (SQL)
"""
def
__init__
(
self
,
host
,
user
,
password
,
init_fun
=
None
):
if
':'
in
host
:
host
,
port
=
host
.
split
(
':'
)
port
=
int
(
port
)
else
:
port
=
config
[
'database.port'
]
self
.
conn_info
=
dict
(
host
=
host
,
port
=
port
,
user
=
user
,
passwd
=
password
)
self
.
init_fun
=
init_fun
print
(
"Connecting {user}@{host}:{port}"
.
format
(
**
self
.
conn_info
))
self
.
_conn
=
None
self
.
connect
()
if
self
.
is_connected
:
logger
.
info
(
"Connected {user}@{host}:{port}"
.
format
(
**
self
.
conn_info
))
self
.
connection_id
=
self
.
query
(
'SELECT connection_id()'
).
fetchone
()[
0
]
else
:
raise
DataJointError
(
'Connection failed.'
)
self
.
_conn
.
autocommit
(
True
)
self
.
_in_transaction
=
False
self
.
schemas
=
dict
()
self
.
dependencies
=
Dependencies
(
self
)
def
__eq__
(
self
,
other
):
return
self
.
conn_info
==
other
.
conn_info
def
__repr__
(
self
):
connected
=
"connected"
if
self
.
is_connected
else
"disconnected"
return
"DataJoint connection ({connected}) {user}@{host}:{port}"
.
format
(
connected
=
connected
,
**
self
.
conn_info
)
def
connect
(
self
):
"""
Connects to the database server.
"""
self
.
_conn
=
client
.
connect
(
init_command
=
self
.
init_fun
,
sql_mode
=
"NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,"
"STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
,
**
self
.
conn_info
)
def
register
(
self
,
schema
):
self
.
schemas
[
schema
.
database
]
=
schema
@
property
def
is_connected
(
self
):
"""
Returns true if the object is connected to the database server.
"""
return
self
.
_conn
.
ping
()
def
query
(
self
,
query
,
args
=
(),
as_dict
=
False
,
suppress_warnings
=
True
):
"""
Execute the specified query and return the tuple generator (cursor).
:param query: mysql query
:param args: additional arguments for the client.cursor
:param as_dict: If as_dict is set to True, the returned cursor objects returns
query results as dictionary.
:param suppress_warning: If True, suppress all warnings arising from underlying query library
"""
cursor
=
client
.
cursors
.
DictCursor
if
as_dict
else
client
.
cursors
.
Cursor
cur
=
self
.
_conn
.
cursor
(
cursor
=
cursor
)
try
:
# Log the query
logger
.
debug
(
"Executing SQL:"
+
query
[
0
:
300
])
with
warnings
.
catch_warnings
():
if
suppress_warnings
:
# suppress all warnings arising from underlying SQL library
warnings
.
simplefilter
(
"ignore"
)
cur
.
execute
(
query
,
args
)
except
err
.
OperationalError
as
e
:
if
'MySQL server has gone away'
in
str
(
e
)
and
config
[
'database.reconnect'
]:
warnings
.
warn
(
'''Mysql server has gone away.
Reconnected to the server. Data from transactions might be lost and referential constraints may
be violated. You can switch off this behavior by setting the 'database.reconnect' to False.
'''
)
self
.
connect
()
logger
.
debug
(
"Re-executing SQL: "
+
query
[
0
:
300
])
cur
.
execute
(
query
,
args
)
else
:
raise
except
err
.
ProgrammingError
as
e
:
print
(
'Error in query:'
)
print
(
query
)
raise
return
cur
def
get_user
(
self
):
"""
:return: the user name and host name provided by the client to the server.
"""
return
self
.
query
(
'SELECT user()'
).
fetchone
()[
0
]
# ---------- transaction processing
@
property
def
in_transaction
(
self
):
"""
:return: True if there is an open transaction.
"""
self
.
_in_transaction
=
self
.
_in_transaction
and
self
.
is_connected
return
self
.
_in_transaction
def
start_transaction
(
self
):
"""
Starts a transaction error.
:raise DataJointError: if there is an ongoing transaction.
"""
if
self
.
in_transaction
:
raise
DataJointError
(
"Nested connections are not supported."
)
self
.
query
(
'START TRANSACTION WITH CONSISTENT SNAPSHOT'
)
self
.
_in_transaction
=
True
logger
.
info
(
"Transaction started"
)
def
cancel_transaction
(
self
):
"""
Cancels the current transaction and rolls back all changes made during the transaction.
"""
self
.
query
(
'ROLLBACK'
)
self
.
_in_transaction
=
False
logger
.
info
(
"Transaction cancelled. Rolling back ..."
)
def
commit_transaction
(
self
):
"""
Commit all changes made during the transaction and close it.
"""
self
.
query
(
'COMMIT'
)
self
.
_in_transaction
=
False
logger
.
info
(
"Transaction committed and closed."
)
# -------- context manager for transactions
@
property
@
contextmanager
def
transaction
(
self
):
"""
Context manager for transactions. Opens an transaction and closes it after the with statement.
If an error is caught during the transaction, the commits are automatically rolled back.
All errors are raised again.
Example:
>>> import datajoint as dj
>>> with dj.conn().transaction as conn:
>>> # transaction is open here
"""
try
:
self
.
start_transaction
()
yield
self
except
:
self
.
cancel_transaction
()
raise
else
:
self
.
commit_transaction
()
Back
|
FazBrowse Home
|
New Git URL