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
3510class 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