postgreslite 0.0.1__tar.gz → 0.0.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: postgreslite
3
- Version: 0.0.1
3
+ Version: 0.0.3
4
4
  Summary: Python SQLite combined with syntax compared to asyncpg progject.
5
5
  Author: AlexFlipnote
6
6
  License-File: LICENSE
@@ -2,3 +2,6 @@
2
2
  Python SQLite combined with syntax compared to asyncpg progject
3
3
 
4
4
  This project is stillw in the early WIP ages, don't expect too much
5
+
6
+ # Install
7
+ pip install postgreslite
@@ -0,0 +1,4 @@
1
+ from .handler import *
2
+ from .sqlite import *
3
+
4
+ __version__ = "0.0.3"
@@ -0,0 +1,72 @@
1
+ import sqlite3
2
+
3
+ from .sqlite import PoolConnection
4
+ from datetime import datetime, date
5
+
6
+ __all__ = (
7
+ "PostgresLite",
8
+ )
9
+
10
+
11
+ def dict_factory(cursor: sqlite3.Cursor, row: list) -> dict:
12
+ d = {}
13
+ for index, col in enumerate(cursor.description):
14
+ d[col[0]] = row[index]
15
+ return d
16
+
17
+
18
+ class PostgresLite:
19
+ def __init__(self, filename: str = "storage.db"):
20
+ self._prepare_settings()
21
+
22
+ if filename != ":memory:":
23
+ if not filename.endswith(".db"):
24
+ raise ValueError("Database filename must end with '.db'")
25
+
26
+ self._filename = filename
27
+
28
+ def connect(self) -> PoolConnection:
29
+ """ Makes a connection to the database and returns the pool (sync) """
30
+ conn = sqlite3.connect(
31
+ self._filename,
32
+ isolation_level=None,
33
+ detect_types=sqlite3.PARSE_DECLTYPES
34
+ )
35
+
36
+ conn.row_factory = dict_factory
37
+ db = conn.cursor()
38
+ return PoolConnection(db)
39
+
40
+ async def connect_async(self) -> sqlite3.Connection:
41
+ """ Coming soon:tm: """
42
+ pass
43
+
44
+ def _prepare_settings(self) -> None:
45
+ """ Prepare SQLite settings for better experience """
46
+
47
+ def adapt_date_iso(val) -> str:
48
+ """Adapt datetime.date to ISO 8601 date."""
49
+ return val.isoformat()
50
+
51
+ def adapt_datetime_iso(val) -> str:
52
+ """Adapt datetime.datetime to timezone-naive ISO 8601 date."""
53
+ return val.isoformat()
54
+
55
+ sqlite3.register_adapter(date, adapt_date_iso)
56
+ sqlite3.register_adapter(datetime, adapt_datetime_iso)
57
+
58
+ def convert_date(val) -> date:
59
+ """Convert ISO 8601 date to datetime.date object."""
60
+ return date.fromisoformat(val.decode())
61
+
62
+ def convert_datetime(val) -> datetime:
63
+ """Convert ISO 8601 datetime to datetime.datetime object."""
64
+ return datetime.fromisoformat(val.decode())
65
+
66
+ def convert_timestamp(val) -> datetime:
67
+ """Convert Unix epoch timestamp to datetime.datetime object."""
68
+ return datetime.fromtimestamp(int(val))
69
+
70
+ sqlite3.register_converter("date", convert_date)
71
+ sqlite3.register_converter("datetime", convert_datetime)
72
+ sqlite3.register_converter("timestamp", convert_timestamp)
@@ -1,11 +1,9 @@
1
1
  import sqlite3
2
2
 
3
-
4
- def dict_factory(cursor, row) -> dict:
5
- d = {}
6
- for index, col in enumerate(cursor.description):
7
- d[col[0]] = row[index]
8
- return d
3
+ __all__ = (
4
+ "PoolConnection",
5
+ "SQLStatements",
6
+ )
9
7
 
10
8
 
11
9
  class SQLStatements:
@@ -14,7 +12,7 @@ class SQLStatements:
14
12
 
15
13
  @property
16
14
  def prepared(self) -> tuple:
17
- """ Prepare statements for SQLite with *args provided from earlier"""
15
+ """ Prepare statements for SQLite with *args provided from earlier """
18
16
  arg_len = len(self.arguments)
19
17
 
20
18
  if arg_len <= 0:
@@ -25,32 +23,21 @@ class SQLStatements:
25
23
  return tuple(self.arguments)
26
24
 
27
25
 
28
- class PostgresLite:
29
- def __init__(self, filename: str = "storage.db"):
30
- if filename != ":memory:":
31
- if not filename.endswith(".db"):
32
- raise ValueError("Database filename must end with '.db'")
33
-
34
- self.conn = sqlite3.connect(
35
- filename,
36
- isolation_level=None,
37
- detect_types=sqlite3.PARSE_DECLTYPES
38
- )
39
-
40
- self.conn.row_factory = dict_factory
41
- self.db = self.conn.cursor()
26
+ class PoolConnection:
27
+ def __init__(self, pool: sqlite3.Connection):
28
+ self._pool = pool
42
29
 
43
30
  def _init_executor(self, query: str, arguments: list) -> sqlite3.Cursor:
44
31
  """ Initialize SQL executor with args for 'Prepared Statements' """
45
32
  prep = SQLStatements(arguments)
46
- data = self.db.execute(query, prep.prepared)
33
+ data = self._pool.execute(query, prep.prepared)
47
34
  return data
48
35
 
49
36
  def execute(self, query: str, *args) -> str:
50
37
  """ Execute SQL command with args for 'Prepared Statements' """
51
38
  data = self._init_executor(query, [g for g in args])
52
39
 
53
- status_word = query.split(' ')[0].upper()
40
+ status_word = query.split(" ")[0].strip().upper()
54
41
  status_code = data.rowcount if data.rowcount > 0 else 0
55
42
  if status_word == "SELECT":
56
43
  status_code = len(data.fetchall())
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: postgreslite
3
- Version: 0.0.1
3
+ Version: 0.0.3
4
4
  Summary: Python SQLite combined with syntax compared to asyncpg progject.
5
5
  Author: AlexFlipnote
6
6
  License-File: LICENSE
@@ -3,6 +3,7 @@ README.md
3
3
  setup.py
4
4
  postgreslite/__init__.py
5
5
  postgreslite/handler.py
6
+ postgreslite/sqlite.py
6
7
  postgreslite.egg-info/PKG-INFO
7
8
  postgreslite.egg-info/SOURCES.txt
8
9
  postgreslite.egg-info/dependency_links.txt
@@ -1,3 +0,0 @@
1
- from .handler import PostgresLite
2
-
3
- __version__ = "0.0.1"
File without changes
File without changes
File without changes