Instantiate dataset.Database object once and return it without re-instantiating it.
This commit is contained in:
parent
0fae3d305a
commit
18fb69dff8
@ -7,7 +7,7 @@ __author__ = "Davide Testa"
|
||||
__email__ = "davide@davte.it"
|
||||
__credits__ = "Marco Origlia"
|
||||
__license__ = "GNU General Public License v3.0"
|
||||
__version__ = "1.5.4"
|
||||
__version__ = "1.5.5"
|
||||
__maintainer__ = "Davide Testa"
|
||||
__contact__ = "t.me/davte"
|
||||
|
||||
|
@ -166,8 +166,10 @@ class Bot(telepot.aio.Bot, Gettable):
|
||||
name=db_name,
|
||||
ext='.db' if not db_name.endswith('.db') else ''
|
||||
)
|
||||
self._database = dataset.connect(self.db_url)
|
||||
else:
|
||||
self._db_url = None
|
||||
self._database = None
|
||||
self._unauthorized_message = None
|
||||
self.authorization_function = lambda update, authorization_level: True
|
||||
self.get_chat_id = lambda update: (
|
||||
@ -222,12 +224,20 @@ class Bot(telepot.aio.Bot, Gettable):
|
||||
|
||||
@property
|
||||
def db(self):
|
||||
"""Connect to bot's database.
|
||||
"""Return the dataset.Database instance related to `self`.
|
||||
|
||||
It must be used inside a with statement: `with bot.db as db`
|
||||
To start a transaction with bot's database, use a with statement:
|
||||
```python3
|
||||
with bot.db as db:
|
||||
db['your_table'].insert(
|
||||
dict(
|
||||
name='John',
|
||||
age=45
|
||||
)
|
||||
)
|
||||
```
|
||||
"""
|
||||
if self.db_url:
|
||||
return dataset.connect(self.db_url)
|
||||
return self._database
|
||||
|
||||
@property
|
||||
def default_keyboard(self):
|
||||
|
Loading…
x
Reference in New Issue
Block a user