Skip to content

Commit c8e4fca

Browse files
Merge pull request #5 from sqlitecloud/#3-last-features
#3 - upload/download, pubsub
2 parents 25ae065 + d98732a commit c8e4fca

22 files changed

+1115
-277
lines changed

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.0.74
2+
current_version = 0.0.75
33
commit = True
44
tag = True
55

README.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@ You can install SqliteCloud Package using Python Package Index (PYPI):
1313
$ pip install SqliteCloud
1414
```
1515

16-
- Follow the instructions reported here https://github.com/sqlitecloud/sdk/tree/master/C to build the driver.
17-
18-
- Set SQLITECLOUD_DRIVER_PATH environment variable to the path of the driver file build.
19-
2016
## Usage
2117
<hr>
2218

2319
```python
24-
from sqlitecloud.client import SqliteCloudClient, SqliteCloudAccount
20+
from sqlitecloud.client import SqliteCloudClient
21+
from sqlitecloud.types import SqliteCloudAccount
2522
```
2623

2724
### _Init a connection_
@@ -45,9 +42,8 @@ conn = client.open_connection()
4542
### _Execute a query_
4643
You can bind values to parametric queries: you can pass parameters as positional values in an array
4744
```python
48-
result = client.exec_statement(
49-
"SELECT * FROM table_name WHERE id = ?",
50-
[1],
45+
result = client.exec_query(
46+
"SELECT * FROM table_name WHERE id = 1"
5147
conn=conn
5248
)
5349
```

samples.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
},
1414
{
1515
"cell_type": "code",
16-
"execution_count": 8,
16+
"execution_count": 1,
1717
"metadata": {},
1818
"outputs": [],
1919
"source": [
@@ -37,7 +37,7 @@
3737
},
3838
{
3939
"cell_type": "code",
40-
"execution_count": 9,
40+
"execution_count": 2,
4141
"metadata": {},
4242
"outputs": [],
4343
"source": [
@@ -75,7 +75,7 @@
7575
},
7676
{
7777
"cell_type": "code",
78-
"execution_count": 15,
78+
"execution_count": 6,
7979
"metadata": {},
8080
"outputs": [],
8181
"source": [
@@ -92,7 +92,7 @@
9292
},
9393
{
9494
"cell_type": "code",
95-
"execution_count": 16,
95+
"execution_count": 7,
9696
"metadata": {},
9797
"outputs": [
9898
{
@@ -124,7 +124,7 @@
124124
},
125125
{
126126
"cell_type": "code",
127-
"execution_count": 17,
127+
"execution_count": 8,
128128
"metadata": {},
129129
"outputs": [],
130130
"source": [

src/README-PYPI.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@ You can install SqliteCloud Package using Python Package Index (PYPI):
1313
$ pip install SqliteCloud
1414
```
1515

16-
- Follow the instructions reported here https://github.com/sqlitecloud/sdk/tree/master/C to build the driver.
17-
18-
- Set SQLITECLOUD_DRIVER_PATH environment variable to the path of the driver file build.
19-
2016
## Usage
2117
<hr>
2218

2319
```python
24-
from sqlitecloud.client import SqliteCloudClient, SqliteCloudAccount
20+
from sqlitecloud.client import SqliteCloudClient
21+
from sqlitecloud.types import SqliteCloudAccount
2522
```
2623

2724
### _Init a connection_
@@ -45,9 +42,8 @@ conn = client.open_connection()
4542
### _Execute a query_
4643
You can bind values to parametric queries: you can pass parameters as positional values in an array
4744
```python
48-
result = client.exec_statement(
49-
"SELECT * FROM table_name WHERE id = ?",
50-
[1],
45+
result = client.exec_query(
46+
"SELECT * FROM table_name WHERE id = 1"
5147
conn=conn
5248
)
5349
```

src/setup.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,24 @@ def read_file(filename):
1818

1919
setup(
2020
name='SqliteCloud',
21-
version='0.0.74',
22-
author='Sam Reghenzi & Matteo Fredi',
21+
version='0.0.75',
22+
author='sqlitecloud.io',
2323
description='A Python package for working with SQLite databases in the cloud.',
2424
long_description=read_file('README-PYPI.md'),
2525
long_description_content_type='text/markdown',
2626
url="https://github.com/sqlitecloud/python",
2727
packages=find_packages(),
2828
install_requires=[
29-
'mypy == 1.6.1',
30-
'mypy-extensions == 1.0.0',
31-
'typing-extensions == 4.8.0',
32-
'black == 23.7.0',
33-
'python-dotenv == 1.0.0',
29+
'lz4 == 3.1.10',
3430
],
3531
classifiers=[
3632
'Development Status :: 3 - Alpha',
3733
'Intended Audience :: Developers',
3834
'License :: OSI Approved :: MIT License',
35+
'Programming Language :: Python :: 3.6',
36+
'Programming Language :: Python :: 3.7',
37+
'Programming Language :: Python :: 3.8',
38+
'Programming Language :: Python :: 3.9',
3939
'Programming Language :: Python :: 3.10',
4040
'Programming Language :: Python :: 3.11',
4141
'Programming Language :: Python :: 3.12',

src/sqlitecloud/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = "0.0.74"
1+
VERSION = "0.1.0"

src/sqlitecloud/client.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ def __init__(
2525
self,
2626
cloud_account: Optional[SqliteCloudAccount] = None,
2727
connection_str: Optional[str] = None,
28-
# pub_subs: SQCloudPubSubCallback = [],
2928
) -> None:
3029
"""Initializes a new instance of the class with connection information.
3130
3231
Args:
33-
connection_str (str): The connection string for the database.
32+
cloud_account (SqliteCloudAccount): The account information for the SQlite Cloud database.
33+
connection_str (str): The connection string for the SQlite Cloud database.
34+
Eg: sqlitecloud://user:[email protected]:port/dbname?timeout=10&apikey=abcd123
3435
3536
"""
36-
self.driver = Driver()
37+
self._driver = Driver()
3738

3839
self.config = SQCloudConfig()
3940

@@ -53,18 +54,29 @@ def open_connection(self) -> SQCloudConnect:
5354
Raises:
5455
SQCloudException: If an error occurs while opening the connection.
5556
"""
56-
connection = self.driver.connect(
57+
connection = self._driver.connect(
5758
self.config.account.hostname, self.config.account.port, self.config
5859
)
5960

6061
return connection
6162

6263
def disconnect(self, conn: SQCloudConnect) -> None:
6364
"""Close the connection to the database."""
64-
self.driver.disconnect(conn)
65+
self._driver.disconnect(conn)
66+
67+
def is_connected(self, conn: SQCloudConnect) -> bool:
68+
"""Check if the connection is still open.
69+
70+
Args:
71+
conn (SQCloudConnect): The connection to the database.
72+
73+
Returns:
74+
bool: True if the connection is open, False otherwise.
75+
"""
76+
return self._driver.is_connected(conn)
6577

6678
def exec_query(
67-
self, query: str, conn: SQCloudConnect = None
79+
self, query: str, conn: SQCloudConnect
6880
) -> SqliteCloudResultSet:
6981
"""Executes a SQL query on the SQLite Cloud database.
7082
@@ -73,15 +85,11 @@ def exec_query(
7385
7486
Returns:
7587
SqliteCloudResultSet: The result set of the executed query.
76-
"""
77-
provided_connection = conn is not None
78-
if not provided_connection:
79-
conn = self.open_connection()
80-
81-
result = self.driver.execute(query, conn)
8288
83-
if not provided_connection:
84-
self.disconnect(conn)
89+
Raises:
90+
SQCloudException: If an error occurs while executing the query.
91+
"""
92+
result = self._driver.execute(query, conn)
8593

8694
return SqliteCloudResultSet(result)
8795

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

97105
def _parse_connection_string(self, connection_string) -> SQCloudConfig:
98106
# URL STRING FORMAT

src/sqlitecloud/download.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from io import BufferedWriter
2+
import logging
3+
4+
from sqlitecloud.driver import Driver
5+
from sqlitecloud.types import SQCloudConnect
6+
7+
8+
def xCallback(
9+
fd: BufferedWriter, data: bytes, blen: int, ntot: int, nprogress: int
10+
) -> None:
11+
"""
12+
Callback function used for downloading data.
13+
Data is passed to the callback to be written to the file and to
14+
monitor the progress.
15+
16+
Args:
17+
fd (BufferedWriter): The file descriptor to write the downloaded data to.
18+
data (bytes): The data to be written.
19+
blen (int): The length of the data.
20+
ntot (int): The total length of the data being downloaded.
21+
nprogress (int): The number of bytes already downloaded.
22+
"""
23+
fd.write(data)
24+
25+
if blen == 0:
26+
logging.log(logging.DEBUG, "DOWNLOAD COMPLETE")
27+
else:
28+
logging.log(logging.DEBUG, f"{(nprogress + blen) / ntot * 100:.2f}%")
29+
30+
31+
def download_db(connection: SQCloudConnect, dbname: str, filename: str) -> None:
32+
"""
33+
Download a database from the server.
34+
35+
Raises:
36+
SQCloudException: If an error occurs while downloading the database.
37+
"""
38+
driver = Driver()
39+
40+
with open(filename, "wb") as fd:
41+
driver.download_database(connection, dbname, fd, xCallback, False)

0 commit comments

Comments
 (0)