Skip to content

Commit

Permalink
release 2.573.24077
Browse files Browse the repository at this point in the history
  • Loading branch information
klahnakoski committed Mar 17, 2024
2 parents ade8986 + 60065ab commit 8e4f638
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 23 deletions.
53 changes: 42 additions & 11 deletions mo_sqlite/transacfion.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@
# Contact: Kyle Lahnakoski (kyle@lahnakoski.com)
#
from dataclasses import is_dataclass, fields
from typing import Optional

from mo_dots import unwraplist, Data, is_missing
from mo_dots import unwraplist, Data, is_missing, dict_to_data
from mo_future import allocate_lock as _allocate_lock
from mo_json import to_jx_type, union_type
from mo_logs import Except, logger
from mo_logs.exceptions import get_stacktrace
from mo_sql import sql_iso
from mo_sql.utils import untype_field
from mo_threads import Lock

from mo_sql.utils import untype_field, sql_type_key_to_json_type
from mo_sqlite.utils import CommandItem, FORMAT_COMMAND, ROLLBACK, COMMIT, quote_column
from mo_threads import Lock


class Transaction(object):
Expand Down Expand Up @@ -80,7 +79,14 @@ def do_all(self):
except Exception as e:
logger.error("problem running commands", current=c, cause=e)

def query(self, query, *, as_dataclass=None):
def query(
self,
query,
*,
format="table", # RETURN TABLE OR LIST
as_dataclass=None, # RETURN TABLE AS LIST OF DATACLASS OBJECTS
raw=False, # raw=False WILL UNTYPE THE DATA
):
if self.db.closed:
logger.error("database is closed")

Expand All @@ -92,18 +98,43 @@ def query(self, query, *, as_dataclass=None):
signal.acquire()
if result.exception:
logger.error("Problem with Sqlite call", cause=result.exception)
if as_dataclass is None:
# RETURN TABLE
if raw:
return result
return table_to_list(result, as_dataclass=as_dataclass)
if as_dataclass is None:
if not result.header:
return result
# REMOVE TYPING
clean_header, jx_type = zip(*(
(name, name+to_jx_type(json_type))
for h in result.header
for name, json_type in [untype_field(h)]
))
jx_type = union_type(*jx_type)
if format == "list":
return dict_to_data({
"meta": {"format": "list"},
"type": jx_type,
"data": [{h: c for h, c in zip(clean_header, row)} for row in result.data]
})
else:
# RETURN TABLE
return dict_to_data({
"meta": result.meta,
"type": jx_type,
"header": clean_header,
"data": result.data
})
else:
result.header = [untype_field(h)[0] for h in result.header]
return table_to_list(result, as_dataclass=as_dataclass)

def about(self, table_name):
"""
:param table_name: TABLE OF INTEREST
:return: SOME INFORMATION ABOUT THE TABLE
(cid, name, dtype, notnull, dfft_value, pk) tuples
"""
details = self.query("PRAGMA table_info" + sql_iso(quote_column(table_name)))
details = self.query("PRAGMA table_info" + sql_iso(quote_column(table_name)), raw=True)
return details.data

def rollback(self):
Expand All @@ -122,7 +153,7 @@ def table_to_list(result, *, as_dataclass):
fields_to_index = [None] * len(field_names)
for fi, f in enumerate(field_names):
for index, h in enumerate(result.header):
if untype_field(h)[0] == f.name:
if h == f.name:
fields_to_index[fi] = index

output = []
Expand Down
4 changes: 2 additions & 2 deletions packaging/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
description='Multithreading for Sqlite, plus expression composition',
extras_require={"tests":["mo-testing>=7.562.24075"]},
include_package_data=True,
install_requires=["jx-python==4.570.24076","mo-dots==9.570.24076","mo-files==6.570.24076","mo-future==7.546.24057","mo-imports==7.546.24057","mo-json==6.570.24076","mo-kwargs==7.570.24076","mo-logs==8.570.24076","mo-math==7.570.24076","mo-sql==4.570.24076","mo-sql==4.570.24076","mo-threads==6.570.24076","mo-times==5.570.24076"],
install_requires=["jx-python==4.572.24077","mo-dots==9.570.24076","mo-files==6.571.24077","mo-future==7.546.24057","mo-imports==7.546.24057","mo-json==6.571.24077","mo-kwargs==7.570.24076","mo-logs==8.570.24076","mo-math==7.570.24076","mo-sql==4.571.24077","mo-sql==4.571.24077","mo-threads==6.570.24076","mo-times==5.570.24076"],
license='MPL 2.0',
long_description='# More SQLite!\n\nMultithreading for Sqlite, plus expression composition\n\n\n[![PyPI Latest Release](https://img.shields.io/pypi/v/mo-sqlite.svg)](https://pypi.org/project/mo-sqlite/)\n[![Build Status](https://github.com/klahnakoski/mo-sqlite/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/klahnakoski/mo-sqlite/actions/workflows/build.yml)\n[![Coverage Status](https://coveralls.io/repos/github/klahnakoski/mo-sqlite/badge.svg?branch=dev)](https://coveralls.io/github/klahnakoski/mo-sqlite?branch=dev)\n[![Downloads](https://pepy.tech/badge/mo-sqlite/month)](https://pepy.tech/project/mo-sqlite)\n\n\n## Multi-threaded Sqlite\n\nThis module wraps the `sqlite3.connection` with thread-safe traffic manager. Here is typical usage: \n\n from mo_sqlite import Sqlite\n db = Sqlite("mydb.sqlite")\n with db.transaction() as t:\n t.command("insert into mytable values (1, 2, 3)")\n\nWhile you may have each thread own a `sqlite3.connection` to the same file, you will still get exceptions when another thread has the file locked.\n\n## Pull JSON out of database\n\nThis module includes a minimum experimental structure that can describe pulling deeply nested JSON documents out of a normalized database. The tactic is to shape a single query who\'s resultset can be easily converted to the desired JSON by Python. Read more on [pulling json from a database](docs/JSON%20in%20Database.md)\n\nThere are multiple normal forms, including domain key normal form, and columnar form; these have a multitude one-to-one relations, all represent the same logical schema, but differ in their access patterns to optimize for particular use cases. This module intends to hide the particular database schema from the caller; exposing just the logical schema. \n\n\n\nThis experiment compliments the [mo-columns](https://github.com/klahnakoski/mo-columns) experiment, which is about pushing JSON into a database. \n ',
long_description_content_type='text/markdown',
name='mo-sqlite',
packages=["mo_sqlite","mo_sqlite.expressions"],
url='https://github.com/klahnakoski/mo-sqlite',
version='2.570.24076'
version='2.573.24077'
)
8 changes: 4 additions & 4 deletions packaging/setuptools.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
"extras_require": {"tests": ["mo-testing>=7.562.24075"]},
"include_package_data": true,
"install_requires": [
"jx-python==4.570.24076", "mo-dots==9.570.24076", "mo-files==6.570.24076",
"mo-future==7.546.24057", "mo-imports==7.546.24057", "mo-json==6.570.24076",
"jx-python==4.572.24077", "mo-dots==9.570.24076", "mo-files==6.571.24077",
"mo-future==7.546.24057", "mo-imports==7.546.24057", "mo-json==6.571.24077",
"mo-kwargs==7.570.24076", "mo-logs==8.570.24076", "mo-math==7.570.24076",
"mo-sql==4.570.24076", "mo-sql==4.570.24076", "mo-threads==6.570.24076",
"mo-sql==4.571.24077", "mo-sql==4.571.24077", "mo-threads==6.570.24076",
"mo-times==5.570.24076"
],
"license": "MPL 2.0",
Expand Down Expand Up @@ -66,5 +66,5 @@
"name": "mo-sqlite",
"packages": ["mo_sqlite", "mo_sqlite.expressions"],
"url": "https://github.com/klahnakoski/mo-sqlite",
"version": "2.570.24076"
"version": "2.573.24077"
}
12 changes: 6 additions & 6 deletions tests/requirements.lock
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# Tests pass with these versions 2024-03-16
# Tests pass with these versions 2024-03-17
# pip install --no-deps -r tests/requirements.lock
certifi==2024.2.2
charset-normalizer==3.3.2
hjson==3.1.0
idna==3.6
jx-python==4.570.24076
jx-python==4.572.24077
mo-collections==5.570.24076
mo-dots==9.570.24076
mo-files==6.570.24076
mo-files==6.571.24077
mo-future==7.546.24057
mo-imports==7.546.24057
mo-json==6.570.24076
mo-json-config==4.570.24076
mo-json==6.571.24077
mo-json-config==4.571.24077
mo-kwargs==7.570.24076
mo-logs==8.570.24076
mo-math==7.570.24076
mo-sql==4.570.24076
mo-sql==4.571.24077
mo-testing==7.562.24075
mo-threads==6.570.24076
mo-times==5.570.24076
Expand Down

0 comments on commit 8e4f638

Please sign in to comment.