devdb-sql 1.0.9__tar.gz → 1.0.10__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.
- {devdb_sql-1.0.9 → devdb_sql-1.0.10}/PKG-INFO +1 -1
- devdb_sql-1.0.10/devdb_sql/db.py +61 -0
- {devdb_sql-1.0.9 → devdb_sql-1.0.10}/devdb_sql.egg-info/PKG-INFO +1 -1
- {devdb_sql-1.0.9 → devdb_sql-1.0.10}/pyproject.toml +1 -1
- devdb_sql-1.0.9/devdb_sql/db.py +0 -104
- {devdb_sql-1.0.9 → devdb_sql-1.0.10}/LICENSE +0 -0
- {devdb_sql-1.0.9 → devdb_sql-1.0.10}/README.md +0 -0
- {devdb_sql-1.0.9 → devdb_sql-1.0.10}/devdb_sql/__init__.py +0 -0
- {devdb_sql-1.0.9 → devdb_sql-1.0.10}/devdb_sql/schema.py +0 -0
- {devdb_sql-1.0.9 → devdb_sql-1.0.10}/devdb_sql/table.py +0 -0
- {devdb_sql-1.0.9 → devdb_sql-1.0.10}/devdb_sql.egg-info/SOURCES.txt +0 -0
- {devdb_sql-1.0.9 → devdb_sql-1.0.10}/devdb_sql.egg-info/dependency_links.txt +0 -0
- {devdb_sql-1.0.9 → devdb_sql-1.0.10}/devdb_sql.egg-info/top_level.txt +0 -0
- {devdb_sql-1.0.9 → devdb_sql-1.0.10}/setup.cfg +0 -0
- {devdb_sql-1.0.9 → devdb_sql-1.0.10}/test/test.py +0 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import sqlite3
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
from .schema import compile_schema
|
|
5
|
+
from .table import Table
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class DB:
|
|
9
|
+
def __init__(self, path):
|
|
10
|
+
if not path:
|
|
11
|
+
raise ValueError("Database path is required")
|
|
12
|
+
|
|
13
|
+
self.path = path
|
|
14
|
+
|
|
15
|
+
parent = Path(path).parent
|
|
16
|
+
if str(parent) not in ("", "."):
|
|
17
|
+
parent.mkdir(parents=True, exist_ok=True)
|
|
18
|
+
|
|
19
|
+
self.sqlite = sqlite3.connect(path)
|
|
20
|
+
self.sqlite.row_factory = sqlite3.Row
|
|
21
|
+
self._tables = {}
|
|
22
|
+
|
|
23
|
+
def create(self, name, schema):
|
|
24
|
+
if not name:
|
|
25
|
+
raise ValueError("Table name is required")
|
|
26
|
+
|
|
27
|
+
if not isinstance(schema, dict):
|
|
28
|
+
raise ValueError(f'Schema is required for table "{name}"')
|
|
29
|
+
|
|
30
|
+
if name in self._tables:
|
|
31
|
+
raise ValueError(f'Table "{name}" is already registered')
|
|
32
|
+
|
|
33
|
+
sql = compile_schema(name, schema)
|
|
34
|
+
self.sqlite.executescript(sql)
|
|
35
|
+
self.sqlite.commit()
|
|
36
|
+
|
|
37
|
+
table = Table(self, name, schema)
|
|
38
|
+
self._tables[name] = table
|
|
39
|
+
return table
|
|
40
|
+
|
|
41
|
+
def drop(self, name):
|
|
42
|
+
self.sqlite.execute(f"DROP TABLE IF EXISTS {name}")
|
|
43
|
+
self.sqlite.commit()
|
|
44
|
+
self._tables.pop(name, None)
|
|
45
|
+
return True
|
|
46
|
+
|
|
47
|
+
def tables(self):
|
|
48
|
+
rows = self.sqlite.execute("""
|
|
49
|
+
SELECT name
|
|
50
|
+
FROM sqlite_master
|
|
51
|
+
WHERE type = 'table'
|
|
52
|
+
AND name NOT LIKE 'sqlite_%'
|
|
53
|
+
ORDER BY name
|
|
54
|
+
""").fetchall()
|
|
55
|
+
return [row["name"] for row in rows]
|
|
56
|
+
|
|
57
|
+
def show_tables(self):
|
|
58
|
+
return self.tables()
|
|
59
|
+
|
|
60
|
+
def close(self):
|
|
61
|
+
self.sqlite.close()
|
devdb_sql-1.0.9/devdb_sql/db.py
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import sqlite3
|
|
2
|
-
import threading
|
|
3
|
-
from pathlib import Path
|
|
4
|
-
|
|
5
|
-
from .schema import compile_schema
|
|
6
|
-
from .table import Table
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class DB:
|
|
10
|
-
def __init__(self, path):
|
|
11
|
-
if not path:
|
|
12
|
-
raise ValueError("Database path is required")
|
|
13
|
-
|
|
14
|
-
self.path = path
|
|
15
|
-
|
|
16
|
-
parent = Path(path).parent
|
|
17
|
-
|
|
18
|
-
if str(parent) not in ("", "."):
|
|
19
|
-
parent.mkdir(
|
|
20
|
-
parents=True,
|
|
21
|
-
exist_ok=True,
|
|
22
|
-
)
|
|
23
|
-
|
|
24
|
-
# SQLite connection can now be accessed
|
|
25
|
-
# from different threads.
|
|
26
|
-
self.sqlite = sqlite3.connect(
|
|
27
|
-
path,
|
|
28
|
-
check_same_thread=False,
|
|
29
|
-
timeout=30,
|
|
30
|
-
)
|
|
31
|
-
|
|
32
|
-
self.sqlite.row_factory = sqlite3.Row
|
|
33
|
-
|
|
34
|
-
# RLock is important because methods like insert()
|
|
35
|
-
# acquire the lock and then call find_by_id().
|
|
36
|
-
self.lock = threading.RLock()
|
|
37
|
-
|
|
38
|
-
self._tables = {}
|
|
39
|
-
|
|
40
|
-
def create(self, name, schema):
|
|
41
|
-
if not name:
|
|
42
|
-
raise ValueError("Table name is required")
|
|
43
|
-
|
|
44
|
-
if not isinstance(schema, dict):
|
|
45
|
-
raise ValueError(
|
|
46
|
-
f'Schema is required for table "{name}"'
|
|
47
|
-
)
|
|
48
|
-
|
|
49
|
-
with self.lock:
|
|
50
|
-
if name in self._tables:
|
|
51
|
-
raise ValueError(
|
|
52
|
-
f'Table "{name}" is already registered'
|
|
53
|
-
)
|
|
54
|
-
|
|
55
|
-
sql = compile_schema(name, schema)
|
|
56
|
-
|
|
57
|
-
self.sqlite.executescript(sql)
|
|
58
|
-
self.sqlite.commit()
|
|
59
|
-
|
|
60
|
-
table = Table(
|
|
61
|
-
self,
|
|
62
|
-
name,
|
|
63
|
-
schema,
|
|
64
|
-
)
|
|
65
|
-
|
|
66
|
-
self._tables[name] = table
|
|
67
|
-
|
|
68
|
-
return table
|
|
69
|
-
|
|
70
|
-
def drop(self, name):
|
|
71
|
-
with self.lock:
|
|
72
|
-
self.sqlite.execute(
|
|
73
|
-
f"DROP TABLE IF EXISTS {name}"
|
|
74
|
-
)
|
|
75
|
-
|
|
76
|
-
self.sqlite.commit()
|
|
77
|
-
|
|
78
|
-
self._tables.pop(name, None)
|
|
79
|
-
|
|
80
|
-
return True
|
|
81
|
-
|
|
82
|
-
def tables(self):
|
|
83
|
-
with self.lock:
|
|
84
|
-
rows = self.sqlite.execute(
|
|
85
|
-
"""
|
|
86
|
-
SELECT name
|
|
87
|
-
FROM sqlite_master
|
|
88
|
-
WHERE type = 'table'
|
|
89
|
-
AND name NOT LIKE 'sqlite_%'
|
|
90
|
-
ORDER BY name
|
|
91
|
-
"""
|
|
92
|
-
).fetchall()
|
|
93
|
-
|
|
94
|
-
return [
|
|
95
|
-
row["name"]
|
|
96
|
-
for row in rows
|
|
97
|
-
]
|
|
98
|
-
|
|
99
|
-
def show_tables(self):
|
|
100
|
-
return self.tables()
|
|
101
|
-
|
|
102
|
-
def close(self):
|
|
103
|
-
with self.lock:
|
|
104
|
-
self.sqlite.close()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|