Skip to content

Commit 31342d4

Browse files
committed
Connect, read and write to socket
Update dev dependency to support >= python3.6 Setup IDE
1 parent 72fc921 commit 31342d4

16 files changed

Lines changed: 594 additions & 1028 deletions

.devcontainer/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM mcr.microsoft.com/devcontainers/python:3.6-bullseye
2+
3+
ADD https://dl.yarnpkg.com/debian/pubkey.gpg /etc/apt/trusted.gpg.d/yarn.asc
4+
5+
RUN chmod +r /etc/apt/trusted.gpg.d/*.asc && \
6+
echo "deb http://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list

.devcontainer/devcontainer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
2+
// README at: https://github.com/devcontainers/templates/tree/main/src/python
3+
{
4+
"name": "Python 3",
5+
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
6+
"build": {
7+
"dockerfile": "Dockerfile"
8+
},
9+
"features": {
10+
"ghcr.io/warrenbuckley/codespace-features/sqlite:1": {}
11+
},
12+
13+
// Features to add to the dev container. More info: https://containers.dev/features.
14+
// "features": {},
15+
16+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
17+
// "forwardPorts": [],
18+
19+
// Use 'postCreateCommand' to run commands after the container is created.
20+
// "postCreateCommand": "pip3 install --user -r requirements.txt",
21+
22+
// Configure tool-specific properties.
23+
"customizations": {
24+
"vscode": {
25+
"extensions": [
26+
"littlefoxteam.vscode-python-test-adapter",
27+
"jkillian.custom-local-formatters"
28+
]
29+
}
30+
}
31+
32+
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
33+
// "remoteUser": "root"
34+
}

.env.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SQLITE_CONNECTION_STRING=sqlitecloud://myhost.sqlite.cloud
2+
SQLITE_USER=admin
3+
SQLITE_PASSWORD=
4+
SQLITE_API_KEY=
5+
SQLITE_HOST=myhost.sqlite.cloud
6+
SQLITE_DB=chinook.sqlite
7+
SQLITE_PORT=8860

.github/dependabot.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# To get started with Dependabot version updates, you'll need to specify which
2+
# package ecosystems to update and where the package manifests are located.
3+
# Please see the documentation for more information:
4+
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
5+
# https://containers.dev/guide/dependabot
6+
7+
version: 2
8+
updates:
9+
- package-ecosystem: "devcontainers"
10+
directory: "/"
11+
schedule:
12+
interval: weekly

requirements-dev.txt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
pylint==2.15.6
2-
pytest==7.1.2
3-
mypy==1.6.1
4-
mypy-extensions==1.0.0
5-
typing-extensions==4.8.0
1+
pylint==2.13.9
2+
mypy==0.971
3+
typing-extensions==4.1.1
64
bump2version==1.0.1
7-
pytest-mock==3.10.0
8-
black==23.7.0
9-
python-dotenv==1.0.0
5+
pytest==7.0.1
6+
pytest-mock==3.6.1
7+
black==22.8.0
8+
python-dotenv==0.20.0

src/sqlitecloud/client.py

Lines changed: 54 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,52 @@
11
""" Module to interact with remote SqliteCloud database
22
33
"""
4-
import ctypes
5-
from dataclasses import dataclass
6-
from typing import Any, List, Optional, Tuple
7-
8-
from sqlitecloud.driver import (
9-
SQCloudConnect,
10-
SQCloudErrorMsg,
11-
SQCloudIsError,
12-
SQCloudExec,
13-
SQCloudExecArray,
14-
SQCloudConnectWithString,
15-
SQCloudDisconnect,
16-
SQCloudPubSubCB,
17-
SQCloudResultDump,
18-
SQCloudResultIsError,
19-
SqlParameter,
20-
)
21-
from sqlitecloud.pubsub import SQCloudPubSubCallback, subscribe_pub_sub
22-
from sqlitecloud.resultset import SqliteCloudResultSet
23-
from sqlitecloud.wrapper_types import SQCloudConfig, SQCloudResult
24-
25-
26-
@dataclass
27-
class SqliteCloudAccount:
28-
username: str
29-
password: str
30-
hostname: str
31-
dbname: str
32-
port: int
4+
from typing import Any, List, Optional
5+
6+
from sqlitecloud.driver import Driver
7+
from sqlitecloud.types import SQCloudConfig, SQCloudConnect, SqliteCloudAccount
338

349

3510
class SqliteCloudClient:
3611
"""
3712
Client to connect to SqliteCloud
3813
"""
3914

40-
_config: Optional[SQCloudConfig] = None
41-
connection_str: Optional[str] = None
42-
hostname: str
43-
dbname: str
44-
port: int
45-
_pub_sub_cbs: List[Tuple[str, SQCloudPubSubCB]] = []
46-
4715
def __init__(
4816
self,
4917
cloud_account: Optional[SqliteCloudAccount] = None,
5018
connection_str: Optional[str] = None,
51-
pub_subs: SQCloudPubSubCallback = [],
19+
# pub_subs: SQCloudPubSubCallback = [],
5220
) -> None:
5321
"""Initializes a new instance of the class.
5422
5523
Args:
5624
connection_str (str): The connection string for the database.
57-
uuid (UUID, optional): The UUID for the instance. Defaults to a new UUID generated using uuid4().
5825
5926
Raises:
6027
ValueError: If the connection string is invalid.
6128
6229
"""
63-
for pb in pub_subs:
64-
self._pub_sub_cbs.append(("channel1", SQCloudPubSubCB(pb)))
30+
self.driver = Driver()
31+
32+
self.hostname: str = ''
33+
self.port: int = 8860
34+
35+
self.config = SQCloudConfig()
36+
37+
# for pb in pub_subs:
38+
# self._pub_sub_cbs.append(("channel1", SQCloudPubSubCB(pb)))
6539
if connection_str:
66-
self.connection_str = connection_str
67-
elif cloud_account:
40+
# TODO: parse connection string to create the config
6841
self.config = SQCloudConfig()
69-
self.config.username = self._encode_str_to_c(cloud_account.username)
70-
self.config.password = self._encode_str_to_c(cloud_account.password)
42+
elif cloud_account:
43+
self.config.account = cloud_account
7144
self.hostname = cloud_account.hostname
72-
self.dbname = cloud_account.dbname
7345
self.port = cloud_account.port
7446

7547
else:
7648
raise Exception("Missing connection parameters")
7749

78-
def _encode_str_to_c(self, text):
79-
return ctypes.c_char_p(text.encode("utf-8"))
80-
8150
def open_connection(self) -> SQCloudConnect:
8251
"""Opens a connection to the SQCloud server.
8352
@@ -87,29 +56,14 @@ def open_connection(self) -> SQCloudConnect:
8756
Raises:
8857
Exception: If an error occurs while opening the connection.
8958
"""
59+
connection = self.driver.connect(self.hostname, self.port, self.config)
9060

91-
# Set other config properties...
92-
connection = None
93-
if self.connection_str:
94-
connection = SQCloudConnectWithString(self.connection_str, None)
95-
else:
96-
connection = SQCloudConnect(
97-
self._encode_str_to_c(self.hostname), self.port, self.config
98-
)
99-
self._check_connection(connection)
100-
SQCloudExec(connection, self._encode_str_to_c(f"USE DATABASE {self.dbname};"))
101-
self._check_connection(connection)
102-
for cb in self._pub_sub_cbs:
103-
subscribe_pub_sub(connection, cb[0], cb[1])
61+
# SQCloudExec(connection, f"USE DATABASE {self.dbname};")
10462

105-
return connection
63+
# for cb in self._pub_sub_cbs:
64+
# subscribe_pub_sub(connection, cb[0], cb[1])
10665

107-
def _check_connection(self, connection) -> None:
108-
is_error = SQCloudIsError(connection)
109-
if is_error:
110-
error_message = SQCloudErrorMsg(connection)
111-
print("An error occurred.", error_message.decode("utf-8"))
112-
raise Exception(error_message)
66+
return connection
11367

11468
def disconnect(self, conn: SQCloudConnect) -> None:
11569
"""Closes the connection to the database.
@@ -119,11 +73,11 @@ def disconnect(self, conn: SQCloudConnect) -> None:
11973
Returns:
12074
None: This method does not return any value.
12175
"""
122-
SQCloudDisconnect(conn)
76+
self.driver.disconnect(conn)
12377

124-
def exec_query(
125-
self, query: str, conn: SQCloudConnect = None
126-
) -> SqliteCloudResultSet:
78+
# def exec_query(
79+
# self, query: str, conn: SQCloudConnect = None
80+
# ) -> SqliteCloudResultSet:
12781
"""Executes a SQL query on the SQLite database.
12882
12983
Args:
@@ -132,26 +86,31 @@ def exec_query(
13286
Returns:
13387
SqliteCloudResultSet: The result set of the executed query.
13488
"""
135-
print(query)
136-
# pylint: disable=unused-variable
137-
local_conn, close_at_end = (
138-
(conn, False) if conn else (self.open_connection(), True)
139-
)
140-
result: SQCloudResult = SQCloudExec(local_conn, self._encode_str_to_c(query))
141-
self._check_connection(local_conn)
142-
return SqliteCloudResultSet(result)
143-
144-
def exec_statement(
145-
self, query: str, values: List[Any], conn: SQCloudConnect = None
146-
) -> SqliteCloudResultSet:
147-
local_conn = conn if conn else self.open_connection()
148-
result: SQCloudResult = SQCloudExecArray(
149-
local_conn,
150-
self._encode_str_to_c(query),
151-
[SqlParameter(self._encode_str_to_c(str(v)), v) for v in values],
152-
)
153-
if SQCloudResultIsError(result):
154-
raise Exception(
155-
"Query error: " + str(SQCloudResultDump(local_conn, result))
156-
)
157-
return SqliteCloudResultSet(result)
89+
# print(query)
90+
# # pylint: disable=unused-variable
91+
# local_conn, close_at_end = (
92+
# (conn, False) if conn else (self.open_connection(), True)
93+
# )
94+
# result: SQCloudResult = SQCloudExec(local_conn, self._encode_str_to_c(query))
95+
# self._check_connection(local_conn)
96+
# return SqliteCloudResultSet(result)
97+
# pass
98+
99+
# def exec_statement(
100+
# self, query: str, values: List[Any], conn: SQCloudConnect = None
101+
# ) -> SqliteCloudResultSet:
102+
# local_conn = conn if conn else self.open_connection()
103+
# result: SQCloudResult = SQCloudExecArray(
104+
# local_conn,
105+
# self._encode_str_to_c(query),
106+
# [SqlParameter(self._encode_str_to_c(str(v)), v) for v in values],
107+
# )
108+
# if SQCloudResultIsError(result):
109+
# raise Exception(
110+
# "Query error: " + str(SQCloudResultDump(local_conn, result))
111+
# )
112+
# return SqliteCloudResultSet(result)
113+
# pass
114+
115+
def sendblob(self):
116+
pass

0 commit comments

Comments
 (0)