Skip to content

Commit

Permalink
v0.3.0b2
Browse files Browse the repository at this point in the history
- Docs update
- Changed abstractions sructure for psql engine
  • Loading branch information
v1a0 committed Mar 31, 2022
1 parent 93a4ef4 commit 863f7f2
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 64 deletions.
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ So It's currently best suited especially for work with it.

PostgreSQL now is only partially support.
It has the same api interface as SQLite3x so feel free to use documentation
of it for PostgreSQLx.
of it for PostgreSQLx. Need to installed ``

---

Expand Down
2 changes: 1 addition & 1 deletion docs/about-column.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# AbstractColumn
# Column

Sub-class of AbstractTable, itself it's one abstract column inside abstract table.
Primary have the same methods but without table name argument.
Expand Down
42 changes: 42 additions & 0 deletions docs/about-engines.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Engine

Engine is abstract class or module to isolate sqllex code from unnecessary dependencies.
Engine have to be corresponding to AbstractEngine class.
You can find the current abstraction for engine in `sqllex/core/entities/abc/{engine.py | connection.py}`

## PostgreSQLx engine
Current abstraction for PostgreSQLx located in `sqllex/core/entities/postgresqlx/engine.py`

> Recommend to use psycopg2 (from the box)
```shell
pip install psycopg2
```

```python
import psycopg2
import sqllex as sx


db = sx.PostgreSQLx(
engine=psycopg2, # Postgres engine
dbname="test_sqllex", # database name
user="postgres", # username
password="admin", # user's password
host="127.0.0.1", # psql host address
port="5432", # connection port

# Optional parameters
template={
'users': {
'id': [sx.INTEGER, sx.AUTOINCREMENT],
'name': sx.TEXT
}
},

# Create connection to database with database class object initialisation
init_connection=True
)
```

Read more in [./about-postgresqlx.md](about-postgresqlx.md)
22 changes: 14 additions & 8 deletions docs/about-postgresqlx.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# PostgreSQLx

Main class to interact with PostgreSQL databases, belongs to the sqllex-databases family, child of AbstractDatabase.
Postgres database need `engine`, we recommend to use psycopg2, just import this lib and give it to Database class constructor.
You can read more [about engines here](./about-engines.md).

```python
# from sqllex import PostgreSQLx, INTEGER, TEXT, AUTOINCREMENT
from sqllex.classes import PostgreSQLx
from sqllex.constants.sqlite import INTEGER, TEXT, AUTOINCREMENT
import psycopg2
import sqllex as sx


db = PostgreSQLx(
dbname="sqllextests", # database name
db = sx.PostgreSQLx(
engine=psycopg2, # Postgres engine
dbname="test_sqllex", # database name
user="postgres", # username
password="admin", # user's password
host="127.0.0.1", # psql host address
Expand All @@ -18,17 +20,21 @@ db = PostgreSQLx(
# Optional parameters
template={
'users': {
'id': [INTEGER, AUTOINCREMENT],
'name': TEXT
'id': [sx.INTEGER, sx.AUTOINCREMENT],
'name': sx.TEXT
}
},

# Create connection to database with database class object initialisation
init_connection=True
)

```

PostgreSQL now is only partially support.
It has the same api interface as SQLite3x so feel free to use documentation
of it for PostgreSQLx. Just replace `SQLite3x` at `PostgreSQLx`.


## PostgreSQLx Public Methods

- [add_column](database-add_column.md)
Expand Down
2 changes: 1 addition & 1 deletion docs/about-table.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# AbstractTable
# Table

```python
class AbstractTable(ABC):
Expand Down
2 changes: 1 addition & 1 deletion sqllex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# "\033[0m"
# "\n")

__version__ = '0.3.0b1'
__version__ = '0.3.0b2'

__all__ = [
# classes
Expand Down
34 changes: 13 additions & 21 deletions sqllex/core/entities/abc/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@


class AbstractConnection(ABC):
"""
Abstract connection
"""

class AbstractCursor(ABC):
"""
Abstract cursor
"""

@abstractmethod
def execute(self, script, values=None):
Expand All @@ -33,19 +25,19 @@ def executemany(self, script, values=None):
"""
pass

@abstractmethod
def executescript(self, script, values=None):
"""
script:
INSERT (?, ?, ?) INTO 'table_name';
SELECT * FROM 'table_name' WHERE col=?
values:
(
1, 2, 3,
'Column_value'
)
"""
pass
# @abstractmethod
# def executescript(self, script, values=None):
# """
# script:
# INSERT (?, ?, ?) INTO 'table_name';
# SELECT * FROM 'table_name' WHERE col=?
# values:
# (
# 1, 2, 3,
# 'Column_value'
# )
# """
# pass

@abstractmethod
def fetchall(self):
Expand Down
26 changes: 2 additions & 24 deletions sqllex/core/entities/abc/engine.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
from abc import ABC, abstractmethod
from sqllex.core.entities.abc.connection import AbstractConnection


class AbstractEngine(ABC):

class AbstractExtensions(ABC):
@abstractmethod
def new_type(self, *args, **kwargs):
pass

@abstractmethod
def register_type(self, *args, **kwargs):
pass

@property
@abstractmethod
def extensions(self):
pass

@abstractmethod
def connect(
self,
dbname=None,
user=None,
password=None,
host=None,
port=None,
**kwargs
):
def connect(self) -> AbstractConnection:
pass

49 changes: 49 additions & 0 deletions sqllex/core/entities/postgresqlx/engine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from sqllex.core.entities.abc.engine import AbstractEngine
from sqllex.core.entities.abc.engine import AbstractConnection


class PostgreSQLxConnection(AbstractConnection):
class PostgreSQLxCursor:

def execute(self, script, values=None):
pass

def executemany(self, script, values=None):
pass

def fetchall(self):
pass

def cursor(self) -> PostgreSQLxCursor:
pass

def commit(self):
pass

def close(self):
pass


class PostgreSQLxExtensions:
def new_type(self, *args, **kwargs):
pass

def register_type(self, *args, **kwargs):
pass


class PostgreSQLxEngine(AbstractEngine):
@property
def extensions(self) -> PostgreSQLxExtensions:
return PostgreSQLxExtensions()

def connect(
self,
dbname=None,
user=None,
password=None,
host=None,
port=None,
**kwargs
) -> PostgreSQLxConnection:
pass
8 changes: 4 additions & 4 deletions sqllex/core/entities/postgresqlx/postgresqlx.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import sqllex.core.entities.postgresqlx.middleware as middleware
from sqllex.core.tools.docs_helpers import copy_docs
from types import ModuleType
from sqllex.core.entities.abc import AbstractEngine
from sqllex.core.entities.abc import AbstractConnection
from sqllex.core.entities.postgresqlx.empty_engine import PostgreSQLxEngine
from sqllex.core.entities.postgresqlx.empty_engine import PostgreSQLxConnection


class PostgreSQLxTransaction(AbstractTransaction):
Expand Down Expand Up @@ -85,7 +85,7 @@ class PostgreSQLx(ABDatabase):

def __init__(
self,
engine: Union[AbstractEngine, ModuleType],
engine: Union[PostgreSQLxEngine, ModuleType],
dbname: AnyStr = "postgres",
user: AnyStr = "postgres",
password: AnyStr = None,
Expand Down Expand Up @@ -173,7 +173,7 @@ def __bool__(self):

@property
@copy_docs(ABDatabase.connection)
def connection(self) -> Union[AbstractConnection, None]:
def connection(self) -> Union[PostgreSQLxConnection, None]:
return self.__connection

@property
Expand Down
6 changes: 3 additions & 3 deletions sqllex/core/entities/sqlite3x/sqlite3x.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
AbstractTable as ABTable, \
AbstractColumn as ABColumn, \
AbstractTransaction
import sqllex.core.tools.parsers.parsers as parse
import sqlite3
from sqllex.debug import logger
from sqllex.exceptions import TableNotExist
from sqllex.types.types import *
import sqllex.core.entities.sqlite3x.middleware as middleware
import sqlite3
from sqllex.core.tools.docs_helpers import copy_docs
import sqllex.core.tools.parsers.parsers as parse
import sqllex.core.entities.sqlite3x.middleware as middleware
import sqllex.core.entities.sqlite3x.script_gens as script_gen


Expand Down

0 comments on commit 863f7f2

Please sign in to comment.