Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.0.74
current_version = 0.0.75
commit = True
tag = True

Expand Down
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@ You can install SqliteCloud Package using Python Package Index (PYPI):
$ pip install SqliteCloud
```

- Follow the instructions reported here https://github.com/sqlitecloud/sdk/tree/master/C to build the driver.

- Set SQLITECLOUD_DRIVER_PATH environment variable to the path of the driver file build.

## Usage
<hr>

```python
from sqlitecloud.client import SqliteCloudClient, SqliteCloudAccount
from sqlitecloud.client import SqliteCloudClient
from sqlitecloud.types import SqliteCloudAccount
```

### _Init a connection_
Expand All @@ -45,9 +42,8 @@ conn = client.open_connection()
### _Execute a query_
You can bind values to parametric queries: you can pass parameters as positional values in an array
```python
result = client.exec_statement(
"SELECT * FROM table_name WHERE id = ?",
[1],
result = client.exec_query(
"SELECT * FROM table_name WHERE id = 1"
conn=conn
)
```
Expand Down
10 changes: 5 additions & 5 deletions samples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -37,7 +37,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -75,7 +75,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -92,7 +92,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -124,7 +124,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand Down
12 changes: 4 additions & 8 deletions src/README-PYPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@ You can install SqliteCloud Package using Python Package Index (PYPI):
$ pip install SqliteCloud
```

- Follow the instructions reported here https://github.com/sqlitecloud/sdk/tree/master/C to build the driver.

- Set SQLITECLOUD_DRIVER_PATH environment variable to the path of the driver file build.

## Usage
<hr>

```python
from sqlitecloud.client import SqliteCloudClient, SqliteCloudAccount
from sqlitecloud.client import SqliteCloudClient
from sqlitecloud.types import SqliteCloudAccount
```

### _Init a connection_
Expand All @@ -45,9 +42,8 @@ conn = client.open_connection()
### _Execute a query_
You can bind values to parametric queries: you can pass parameters as positional values in an array
```python
result = client.exec_statement(
"SELECT * FROM table_name WHERE id = ?",
[1],
result = client.exec_query(
"SELECT * FROM table_name WHERE id = 1"
conn=conn
)
```
Expand Down
14 changes: 7 additions & 7 deletions src/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@ def read_file(filename):

setup(
name='SqliteCloud',
version='0.0.74',
author='Sam Reghenzi & Matteo Fredi',
version='0.0.75',
author='sqlitecloud.io',
description='A Python package for working with SQLite databases in the cloud.',
long_description=read_file('README-PYPI.md'),
long_description_content_type='text/markdown',
url="https://github.com/sqlitecloud/python",
packages=find_packages(),
install_requires=[
'mypy == 1.6.1',
'mypy-extensions == 1.0.0',
'typing-extensions == 4.8.0',
'black == 23.7.0',
'python-dotenv == 1.0.0',
'lz4 == 3.1.10',
],
classifiers=[
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
Expand Down
2 changes: 1 addition & 1 deletion src/sqlitecloud/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = "0.0.74"
VERSION = "0.1.0"
38 changes: 23 additions & 15 deletions src/sqlitecloud/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ def __init__(
self,
cloud_account: Optional[SqliteCloudAccount] = None,
connection_str: Optional[str] = None,
# pub_subs: SQCloudPubSubCallback = [],
) -> None:
"""Initializes a new instance of the class with connection information.

Args:
connection_str (str): The connection string for the database.
cloud_account (SqliteCloudAccount): The account information for the SQlite Cloud database.
connection_str (str): The connection string for the SQlite Cloud database.
Eg: sqlitecloud://user:pass@host.com:port/dbname?timeout=10&apikey=abcd123

"""
self.driver = Driver()
self._driver = Driver()

self.config = SQCloudConfig()

Expand All @@ -53,18 +54,29 @@ def open_connection(self) -> SQCloudConnect:
Raises:
SQCloudException: If an error occurs while opening the connection.
"""
connection = self.driver.connect(
connection = self._driver.connect(
self.config.account.hostname, self.config.account.port, self.config
)

return connection

def disconnect(self, conn: SQCloudConnect) -> None:
"""Close the connection to the database."""
self.driver.disconnect(conn)
self._driver.disconnect(conn)

def is_connected(self, conn: SQCloudConnect) -> bool:
"""Check if the connection is still open.

Args:
conn (SQCloudConnect): The connection to the database.

Returns:
bool: True if the connection is open, False otherwise.
"""
return self._driver.is_connected(conn)

def exec_query(
self, query: str, conn: SQCloudConnect = None
self, query: str, conn: SQCloudConnect
) -> SqliteCloudResultSet:
"""Executes a SQL query on the SQLite Cloud database.

Expand All @@ -73,15 +85,11 @@ def exec_query(

Returns:
SqliteCloudResultSet: The result set of the executed query.
"""
provided_connection = conn is not None
if not provided_connection:
conn = self.open_connection()

result = self.driver.execute(query, conn)

if not provided_connection:
self.disconnect(conn)
Raises:
SQCloudException: If an error occurs while executing the query.
"""
result = self._driver.execute(query, conn)

return SqliteCloudResultSet(result)

Expand All @@ -92,7 +100,7 @@ def sendblob(self, blob: bytes, conn: SQCloudConnect) -> SqliteCloudResultSet:
blob (bytes): The blob to be sent to the database.
conn (SQCloudConnect): The connection to the database.
"""
return self.driver.sendblob(blob, conn)
return self._driver.send_blob(blob, conn)

def _parse_connection_string(self, connection_string) -> SQCloudConfig:
# URL STRING FORMAT
Expand Down
41 changes: 41 additions & 0 deletions src/sqlitecloud/download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from io import BufferedWriter
import logging

from sqlitecloud.driver import Driver
from sqlitecloud.types import SQCloudConnect


def xCallback(
fd: BufferedWriter, data: bytes, blen: int, ntot: int, nprogress: int
) -> None:
"""
Callback function used for downloading data.
Data is passed to the callback to be written to the file and to
monitor the progress.

Args:
fd (BufferedWriter): The file descriptor to write the downloaded data to.
data (bytes): The data to be written.
blen (int): The length of the data.
ntot (int): The total length of the data being downloaded.
nprogress (int): The number of bytes already downloaded.
"""
fd.write(data)

if blen == 0:
logging.log(logging.DEBUG, "DOWNLOAD COMPLETE")
else:
logging.log(logging.DEBUG, f"{(nprogress + blen) / ntot * 100:.2f}%")


def download_db(connection: SQCloudConnect, dbname: str, filename: str) -> None:
"""
Download a database from the server.

Raises:
SQCloudException: If an error occurs while downloading the database.
"""
driver = Driver()

with open(filename, "wb") as fd:
driver.download_database(connection, dbname, fd, xCallback, False)
Loading