sqlthon 0.2.0__tar.gz → 1.0.0__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.4
2
2
  Name: sqlthon
3
- Version: 0.2.0
3
+ Version: 1.0.0
4
4
  Summary: A simple package for SQLite3 operations
5
5
  Author-email: SAUMS <supportsqlthon@gmail.com>
6
6
  License: MIT
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "sqlthon"
7
- version = '0.2.0'
7
+ version = '1.0.0'
8
8
  description = "A simple package for SQLite3 operations"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -15,7 +15,7 @@ from .core import Connect, Field
15
15
 
16
16
  __all__ = ['Field', 'Connect']
17
17
 
18
- __version__ = '0.2.0'
18
+ __version__ = '1.0.0'
19
19
  __author__ = "SAUMS"
20
20
  __email__ = "supportsqlthon@gmail.com"
21
21
  __description__ = "A Simple Package For SQLite3 Operations."
@@ -1,116 +1,4 @@
1
1
  from .errors import (SqlthonError, YouHackerError, TableError, TableNotFoundError, TableAlreadyExistsError, ColumnError, ColumnNotFoundError, RecordError, UniqueConstraintError, NotNullConstraintError, DataTypeError, ForeignKeyError, CheckConstraintError, PrimaryKeyError, QueryError, SyntaxError_, NoSuchFunctionError, WrongNumberOfArgumentsError, DatabaseError, DatabaseLockedError, DatabaseReadOnlyError, DatabaseCorruptError, DatabaseFullError, DatabaseBusyError, IndexError_, IndexNotFoundError, IndexAlreadyExistsError, ViewError, ViewNotFoundError, ViewAlreadyExistsError, TransactionError, TransactionAlreadyActiveError, TransactionNotActiveError, BackupError, BackupPathError, BackupPermissionError, CsvError, CsvFileNotFoundError, CsvFormatError, PandasError, PandasNotInstalledError, ValidationError, InvalidColumnCountError, InvalidValueError)
2
- class Field:
3
- __slots__ = ("name",)
4
- def __init__(self, name):
5
- _analysis_code(name)
6
- if "." in name:
7
- name = name.replace(".", "\".\"")
8
- self.name = name
9
-
10
- def __gt__(self, value): return _Expression(self, ">", value)
11
- def __lt__(self, value): return _Expression(self, "<", value)
12
- def __ge__(self, value): return _Expression(self, ">=", value)
13
- def __le__(self, value): return _Expression(self, "<=", value)
14
- def __eq__(self, value): return _Expression(self, "=", value)
15
- def __ne__(self, value): return _Expression(self, "!=", value)
16
- def __invert__(self): return _Expression(self, "NOT", None)
17
- def not_like(self, value): return _Expression(self, "NOT LIKE", value)
18
- def like(self, value):
19
- return _Expression(self, "LIKE", value)
20
- def startswith(self, value):
21
- return _Expression(self, "LIKE", f"{value}%")
22
- def endswith(self, value):
23
- return _Expression(self, "LIKE", f"%{value}")
24
- def contains(self, value):
25
- return _Expression(self, "LIKE", f"%{value}%")
26
- def in_(self, value):
27
- return _Expression(self, "IN", value)
28
- def not_in(self, value):
29
- return _Expression(self, "NOT IN", value)
30
- def between(self, value):
31
- return _Expression(self, "BETWEEN", value)
32
- def is_null(self):
33
- return _Expression(self, "IS NULL", None)
34
- def is_not_null(self):
35
- return _Expression(self, "IS NOT NULL", None)
36
-
37
-
38
- class _Expression:
39
- __slots__ = ("left", "op", "right")
40
- def __init__(self, left, op, right):
41
- self.left = left
42
- self.op = op
43
- self.right = right
44
-
45
- def __and__(self, other):
46
- return _Expression(self, "AND", other)
47
- def __or__(self, other):
48
- return _Expression(self, "OR", other)
49
-
50
-
51
- class _Aggregate:
52
- __slots__ = ("func", "column")
53
- def __init__(self, func, column="*"):
54
- if not column == "*":
55
- _analysis_code(column)
56
- self.func = func
57
- self.column = column
58
-
59
- def __gt__(self, value): return _Expression(self, ">", value)
60
- def __lt__(self, value): return _Expression(self, "<", value)
61
- def __ge__(self, value): return _Expression(self, ">=", value)
62
- def __le__(self, value): return _Expression(self, "<=", value)
63
- def __eq__(self, value): return _Expression(self, "=", value)
64
- def __ne__(self, value): return _Expression(self, "!=", value)
65
-
66
- def __str__(self):
67
- return f"{self.func}({self.column})"
68
-
69
-
70
- def _compile(expr):
71
- if isinstance(expr.left, _Expression) or isinstance(expr.right, _Expression):
72
- left_sql, left_params = _compile(expr.left)
73
- right_sql, right_params = _compile(expr.right)
74
- sql = f"({left_sql}) {expr.op} ({right_sql})"
75
- params = left_params + right_params
76
- return sql, params
77
- elif expr.op in ("IN", "NOT IN"):
78
- placeholders = ", ".join(["?"] * len(expr.right))
79
- sql = f'"{expr.left.name}" {expr.op} ({placeholders})'
80
- params = tuple(expr.right)
81
- return sql, params
82
- elif expr.op == "BETWEEN":
83
- sql = f'"{expr.left.name}" BETWEEN ? AND ?'
84
- params = tuple(expr.right)
85
- return sql, params
86
- elif expr.op in ("IS NULL", "IS NOT NULL"):
87
- sql = f'"{expr.left.name}" {expr.op}'
88
- return sql, ()
89
- elif isinstance(expr.left, _Aggregate):
90
- sql = f"{expr.left} {expr.op} ?"
91
- params = (expr.right,)
92
- return sql, params
93
- else:
94
- sql = f'"{expr.left.name}" {expr.op} ?'
95
- params = (expr.right,)
96
- return sql, params
97
-
98
-
99
- def _analysis_code(query):
100
- from re import match
101
- if isinstance(query, (list, tuple)):
102
- for i in query:
103
- _analysis_code(i)
104
- return
105
- if not match("^[A-Za-z_][A-Za-z0-9_.(), ]*$", query) or any(word in query.lower() for word in ("sqlite_", "union", "select", "blob", "pragma")):
106
- for i in {"NOT", "NULL", "PRIMARY", "KEY", "UNIQUE", "DEFAULT",
107
- "CHECK", "AUTOINCREMENT", "REFERENCES", "COLLATE",
108
- "TRUE", "FALSE", "CURRENT_TIMESTAMP", "ON", "DELETE",
109
- "UPDATE", "CASCADE", "RESTRICT", "SET", "NO", "ACTION",
110
- "PRECISION", "DOUBLE", "UNSIGNED", "BIG", "INT",
111
- "NATIVE", "CHARACTER", "VARYING",}:
112
- if not i in query:
113
- raise YouHackerError(f"you hacker for {query!r}.") from None
114
2
 
115
3
 
116
4
  class Connect:
@@ -126,6 +14,8 @@ class Connect:
126
14
  # ____________| TABLE |____________
127
15
  def add_table(self, table_name: str, *columns: tuple, if_not_exists=False) -> None:
128
16
  _analysis_code(table_name)
17
+ if not columns:
18
+ raise TypeError("at least one column must be specified.") from None
129
19
  query = "CREATE TABLE "
130
20
  if if_not_exists:
131
21
  query += "IF NOT EXISTS "
@@ -449,6 +339,140 @@ class Connect:
449
339
  return False
450
340
 
451
341
 
342
+ class Field:
343
+ __slots__ = ("name",)
344
+ def __init__(self, name):
345
+ _analysis_code(name)
346
+ if "." in name:
347
+ name = name.replace(".", "\".\"")
348
+ self.name = name
349
+
350
+ def __gt__(self, value): return _Expression(self, ">", value)
351
+ def __lt__(self, value): return _Expression(self, "<", value)
352
+ def __ge__(self, value): return _Expression(self, ">=", value)
353
+ def __le__(self, value): return _Expression(self, "<=", value)
354
+ def __eq__(self, value): return _Expression(self, "=", value)
355
+ def __ne__(self, value): return _Expression(self, "!=", value)
356
+ def __invert__(self): return _Expression(self, "NOT", None)
357
+ def not_like(self, value): return _Expression(self, "NOT LIKE", value)
358
+ def like(self, value):
359
+ return _Expression(self, "LIKE", value)
360
+ def startswith(self, value):
361
+ return _Expression(self, "LIKE", f"{value}%")
362
+ def endswith(self, value):
363
+ return _Expression(self, "LIKE", f"%{value}")
364
+ def contains(self, value):
365
+ return _Expression(self, "LIKE", f"%{value}%")
366
+ def in_(self, value):
367
+ return _Expression(self, "IN", value)
368
+ def not_in(self, value):
369
+ return _Expression(self, "NOT IN", value)
370
+ def between(self, value):
371
+ return _Expression(self, "BETWEEN", value)
372
+ def is_null(self):
373
+ return _Expression(self, "IS NULL", None)
374
+ def is_not_null(self):
375
+ return _Expression(self, "IS NOT NULL", None)
376
+
377
+
378
+ class _Expression:
379
+ __slots__ = ("left", "op", "right")
380
+ def __init__(self, left, op, right):
381
+ self.left = left
382
+ self.op = op
383
+ self.right = right
384
+
385
+ def __and__(self, other):
386
+ return _Expression(self, "AND", other)
387
+ def __or__(self, other):
388
+ return _Expression(self, "OR", other)
389
+ def __invert__(self):
390
+ return _Expression(self, "NOT", None)
391
+
392
+
393
+ class _Aggregate:
394
+ __slots__ = ("func", "column")
395
+ def __init__(self, func, column="*"):
396
+ if not column == "*":
397
+ _analysis_code(column)
398
+ if func not in ("MAX", "MIN", "SUM", "AVG", "COUNT"):
399
+ raise YouHackerError(f"you hacker for {func!r}.")
400
+ self.func = func
401
+ self.column = column
402
+
403
+ def __gt__(self, value): return _Expression(self, ">", value)
404
+ def __lt__(self, value): return _Expression(self, "<", value)
405
+ def __ge__(self, value): return _Expression(self, ">=", value)
406
+ def __le__(self, value): return _Expression(self, "<=", value)
407
+ def __eq__(self, value): return _Expression(self, "=", value)
408
+ def __ne__(self, value): return _Expression(self, "!=", value)
409
+
410
+ def __str__(self):
411
+ return f"{self.func}({self.column})"
412
+
413
+
414
+ def _compile(expr):
415
+ if isinstance(expr.left, _Expression) or isinstance(expr.right, _Expression):
416
+ left_sql, left_params = _compile(expr.left)
417
+ right_sql, right_params = _compile(expr.right)
418
+ sql = f"({left_sql}) {expr.op} ({right_sql})"
419
+ params = left_params + right_params
420
+ return sql, params
421
+ elif expr.op in ("IN", "NOT IN"):
422
+ placeholders = ", ".join(["?"] * len(expr.right))
423
+ sql = f'"{expr.left.name}" {expr.op} ({placeholders})'
424
+ params = tuple(expr.right)
425
+ return sql, params
426
+ elif expr.op == "BETWEEN":
427
+ sql = f'"{expr.left.name}" BETWEEN ? AND ?'
428
+ params = tuple(expr.right)
429
+ return sql, params
430
+ elif expr.op == "NOT":
431
+ if isinstance(expr.left, Field):
432
+ return f'NOT ("{expr.left.name}")', ()
433
+ sql, params = _compile(expr.left)
434
+ return f"NOT ({sql})", params
435
+ elif expr.op in ("IS NULL", "IS NOT NULL"):
436
+ sql = f'"{expr.left.name}" {expr.op}'
437
+ return sql, ()
438
+ elif isinstance(expr.left, _Aggregate):
439
+ sql = f"{expr.left} {expr.op} ?"
440
+ params = (expr.right,)
441
+ return sql, params
442
+ else:
443
+ sql = f'"{expr.left.name}" {expr.op} ?'
444
+ params = (expr.right,)
445
+ return sql, params
446
+
447
+
448
+ def _analysis_code(query):
449
+ from re import match
450
+ if isinstance(query, (list, tuple)):
451
+ for i in query:
452
+ _analysis_code(i)
453
+ return
454
+ if query.startswith("CHECK "):
455
+ if match(r"^CHECK \([A-Za-z_][A-Za-z0-9_]*\s*(?:>=|<=|!=|=|>|<)\s*(?:-?\d+(?:\.\d+)?|'(?:[^']|'')*')\)$", query):
456
+ return
457
+ raise YouHackerError(f"you hacker for {query!r}.") from None
458
+ if match(r"^DEFAULT '[^']*'$", query) or query == "COUNT(*)":
459
+ return
460
+ elif not match(r"^[A-Za-z_][A-Za-z0-9_.(), ]*$", query) or any(
461
+ word in query.lower()
462
+ for word in ("sqlite_", "union", "select", "blob", "pragma")
463
+ ):
464
+ for i in {
465
+ "NOT", "NULL", "PRIMARY", "KEY", "UNIQUE", "DEFAULT",
466
+ "CHECK", "AUTOINCREMENT", "REFERENCES", "COLLATE",
467
+ "TRUE", "FALSE", "CURRENT_TIMESTAMP", "ON", "DELETE",
468
+ "UPDATE", "CASCADE", "RESTRICT", "SET", "NO", "ACTION",
469
+ "PRECISION", "DOUBLE", "UNSIGNED", "BIG", "INT",
470
+ "NATIVE", "CHARACTER", "VARYING",
471
+ }:
472
+ if not i in query:
473
+ raise YouHackerError(f"you hacker for {query!r}.") from None
474
+
475
+
452
476
  # ____________| ERROR |_________
453
477
  def _handler_error(e: Exception):
454
478
  msg = str(e).lower()
@@ -456,9 +480,6 @@ def _handler_error(e: Exception):
456
480
  if "no such table" in msg:
457
481
  raise TableNotFoundError(f"no table with name {msg.split()[-1]!r}.") from None
458
482
 
459
- elif "already exists" in msg:
460
- raise TableAlreadyExistsError(f"table {msg.split()[1]!r} alredy existed.") from None
461
-
462
483
  elif "no such column" in msg:
463
484
  raise ColumnNotFoundError(f"no column with name {msg.split()[-1]!r}.") from None
464
485
 
@@ -522,5 +543,8 @@ def _handler_error(e: Exception):
522
543
  elif "no transaction is active" in msg:
523
544
  raise TransactionNotActiveError(msg) from None
524
545
 
546
+ elif "already exists" in msg:
547
+ raise TableAlreadyExistsError(f"table {msg.split()[1]!r} alredy existed.") from None
548
+
525
549
  else:
526
550
  raise SqlthonError(msg) from None
@@ -0,0 +1,44 @@
1
+ class SqlthonError(Exception):__module__ = ""
2
+ class YouHackerError(SqlthonError, PermissionError):__module__ = ""
3
+ class TableError(SqlthonError):__module__ = ""
4
+ class TableNotFoundError(TableError):__module__ = ""
5
+ class TableAlreadyExistsError(TableError):__module__ = ""
6
+ class ColumnError(SqlthonError):__module__ = ""
7
+ class ColumnNotFoundError(ColumnError):__module__ = ""
8
+ class RecordError(SqlthonError):__module__ = ""
9
+ class UniqueConstraintError(RecordError):__module__ = ""
10
+ class NotNullConstraintError(RecordError):__module__ = ""
11
+ class DataTypeError(RecordError):__module__ = ""
12
+ class ForeignKeyError(RecordError):__module__ = ""
13
+ class CheckConstraintError(RecordError):__module__ = ""
14
+ class PrimaryKeyError(RecordError):__module__ = ""
15
+ class QueryError(SqlthonError):__module__ = ""
16
+ class SyntaxError_(QueryError):__module__ = ""
17
+ class NoSuchFunctionError(QueryError):__module__ = ""
18
+ class WrongNumberOfArgumentsError(QueryError):__module__ = ""
19
+ class DatabaseError(SqlthonError):__module__ = ""
20
+ class DatabaseLockedError(DatabaseError):__module__ = ""
21
+ class DatabaseReadOnlyError(DatabaseError):__module__ = ""
22
+ class DatabaseCorruptError(DatabaseError):__module__ = ""
23
+ class DatabaseFullError(DatabaseError):__module__ = ""
24
+ class DatabaseBusyError(DatabaseError):__module__ = ""
25
+ class IndexError_(SqlthonError):__module__ = ""
26
+ class IndexNotFoundError(IndexError_):__module__ = ""
27
+ class IndexAlreadyExistsError(IndexError_):__module__ = ""
28
+ class ViewError(SqlthonError):__module__ = ""
29
+ class ViewNotFoundError(ViewError):__module__ = ""
30
+ class ViewAlreadyExistsError(ViewError):__module__ = ""
31
+ class TransactionError(SqlthonError):__module__ = ""
32
+ class TransactionAlreadyActiveError(TransactionError):__module__ = ""
33
+ class TransactionNotActiveError(TransactionError):__module__ = ""
34
+ class BackupError(SqlthonError):__module__ = ""
35
+ class BackupPathError(BackupError):__module__ = ""
36
+ class BackupPermissionError(BackupError):__module__ = ""
37
+ class CsvError(SqlthonError):__module__ = ""
38
+ class CsvFileNotFoundError(CsvError):__module__ = ""
39
+ class CsvFormatError(CsvError):__module__ = ""
40
+ class PandasError(SqlthonError):__module__ = ""
41
+ class PandasNotInstalledError(PandasError):__module__ = ""
42
+ class ValidationError(SqlthonError):__module__ = ""
43
+ class InvalidColumnCountError(ValidationError):__module__ = ""
44
+ class InvalidValueError(ValidationError):__module__ = ""
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sqlthon
3
- Version: 0.2.0
3
+ Version: 1.0.0
4
4
  Summary: A simple package for SQLite3 operations
5
5
  Author-email: SAUMS <supportsqlthon@gmail.com>
6
6
  License: MIT
@@ -1,44 +0,0 @@
1
- class SqlthonError(Exception):__module__ = None
2
- class YouHackerError(SqlthonError, PermissionError):__module__ = None
3
- class TableError(SqlthonError):__module__ = None
4
- class TableNotFoundError(TableError):__module__ = None
5
- class TableAlreadyExistsError(TableError):__module__ = None
6
- class ColumnError(SqlthonError):__module__ = None
7
- class ColumnNotFoundError(ColumnError):__module__ = None
8
- class RecordError(SqlthonError):__module__ = None
9
- class UniqueConstraintError(RecordError):__module__ = None
10
- class NotNullConstraintError(RecordError):__module__ = None
11
- class DataTypeError(RecordError):__module__ = None
12
- class ForeignKeyError(RecordError):__module__ = None
13
- class CheckConstraintError(RecordError):__module__ = None
14
- class PrimaryKeyError(RecordError):__module__ = None
15
- class QueryError(SqlthonError):__module__ = None
16
- class SyntaxError_(QueryError):__module__ = None
17
- class NoSuchFunctionError(QueryError):__module__ = None
18
- class WrongNumberOfArgumentsError(QueryError):__module__ = None
19
- class DatabaseError(SqlthonError):__module__ = None
20
- class DatabaseLockedError(DatabaseError):__module__ = None
21
- class DatabaseReadOnlyError(DatabaseError):__module__ = None
22
- class DatabaseCorruptError(DatabaseError):__module__ = None
23
- class DatabaseFullError(DatabaseError):__module__ = None
24
- class DatabaseBusyError(DatabaseError):__module__ = None
25
- class IndexError_(SqlthonError):__module__ = None
26
- class IndexNotFoundError(IndexError_):__module__ = None
27
- class IndexAlreadyExistsError(IndexError_):__module__ = None
28
- class ViewError(SqlthonError):__module__ = None
29
- class ViewNotFoundError(ViewError):__module__ = None
30
- class ViewAlreadyExistsError(ViewError):__module__ = None
31
- class TransactionError(SqlthonError):__module__ = None
32
- class TransactionAlreadyActiveError(TransactionError):__module__ = None
33
- class TransactionNotActiveError(TransactionError):__module__ = None
34
- class BackupError(SqlthonError):__module__ = None
35
- class BackupPathError(BackupError):__module__ = None
36
- class BackupPermissionError(BackupError):__module__ = None
37
- class CsvError(SqlthonError):__module__ = None
38
- class CsvFileNotFoundError(CsvError):__module__ = None
39
- class CsvFormatError(CsvError):__module__ = None
40
- class PandasError(SqlthonError):__module__ = None
41
- class PandasNotInstalledError(PandasError):__module__ = None
42
- class ValidationError(SqlthonError):__module__ = None
43
- class InvalidColumnCountError(ValidationError):__module__ = None
44
- class InvalidValueError(ValidationError):__module__ = None
File without changes
File without changes
File without changes