v1a0 / sqllex

The most pythonic ORM (for SQLite and PostgreSQL). Seriously, try it out!
https://v1a0.github.io/sqllex
GNU General Public License v3.0
92 stars 8 forks source link

FEATURE | Add option to desable connetion with Database object init #56

Closed v1a0 closed 2 years ago

v1a0 commented 2 years ago

Add new parameter into init of Database classes, something like auto_conn or just connect or init_conn, which means "connect immediately" with object initialization. After adding "with-as" statement support it might be reasonable to creating object without creation connection. This parameter have to be True by default.

from sqllex import SQLite3x

# might look like this
db = SQLite3x(path='/path/database.db', init_conn=False)

# or this
db = SQLite3x(path='/path/database.db', auto_conn=False)

# or maybe even
db = SQLite3x(path='/path/database.db', without_conn=True)

In code:

class SQLite3x(ABDatabase):

    def __init__(self,
                 path: PathType = "sql3x.db",
                 init_conn: bool = True,
                 template: DBTemplateType = None):

        ... # another routine

        self.__connection = None  # init connection

        if init_conn:       # <== HERE
            self.connect()  # creating connection with db

        ... # yet another routine

Welcome you to join this discussion! How should we name this parameter?

v1a0 commented 2 years ago

Added since v0.2.0.4

db = SQLite3x(
    path='database.db',
    init_connection=False    #  Disable connection initialization within database class object
)

db.connect(check_same_thread=False)    # example

print(db.tables_names)

db.disconnect()