Skip to content

Commit dfccd64

Browse files
committed
feat: add execute_update example for python
1 parent c1addd5 commit dfccd64

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

python/client.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ def execute(self, sql: str) -> pandas.DataFrame:
102102
df = reader.read_pandas()
103103
return df
104104

105+
def execute_update(self, sql: str) -> int:
106+
"""
107+
Executes the sql on Datalayers and returns the affected rows.
108+
This method is meant to be used for executing DMLs, including Insert and Delete.
109+
Note, Datalayers does not support Update and the development of Delete is in progress.
110+
"""
111+
112+
return self.inner.execute_update(sql, None)
113+
105114
def prepare(self, sql: str) -> PreparedStatement:
106115
"""
107116
Creates a prepared statement.
@@ -122,6 +131,15 @@ def execute_prepared(
122131
df = reader.read_pandas()
123132
return df
124133

134+
def close_prepared(self, prepared_stmt: PreparedStatement):
135+
"""
136+
Closes the prepared statement.
137+
Note, generally you should not call this method explicitly.
138+
Use with clause to manage the life cycle of a prepared statement instead.
139+
"""
140+
141+
prepared_stmt.close()
142+
125143
def close(self):
126144
"""
127145
Closes the inner Arrow FlightSQL client.

python/main.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,32 @@ def main():
105105
# 1 2024-09-02 10:05:00+08:00 2 15.3 1
106106
print(result)
107107

108+
# There provides a dedicated interface `execute_update` for executing DMLs, including Insert, Delete.
109+
# This interface directly returns the affected rows which might be convenient for some use cases.
110+
#
111+
# Note, Datalayers does not support Update and the development for Delete is in progress.
112+
sql = """
113+
INSERT INTO python.demo (ts, sid, value, flag) VALUES
114+
('2024-09-03T10:00:00+08:00', 1, 4.5, 0),
115+
('2024-09-03T10:05:00+08:00', 2, 11.6, 1);
116+
"""
117+
#! It's expected that the affected rows is 2.
118+
#! However, the flightsql-dbapi library does not implement the `execute_update` correctly
119+
#! and the returned affected rows is always 0.
120+
affected_rows = client.execute_update(sql)
121+
# The output should be:
122+
# Affected rows: 2
123+
# print("Affected rows: {}".format(affected_rows))
124+
125+
# Checks that the data are inserted successfully.
126+
sql = "SELECT * FROM python.demo where ts >= '2024-09-03T10:00:00+08:00'"
127+
result = client.execute(sql)
128+
# The result should be:
129+
# ts sid value flag
130+
# 0 2024-09-03 10:00:00+08:00 1 4.5 0
131+
# 1 2024-09-03 10:05:00+08:00 2 11.6 1
132+
print(result)
133+
108134

109135
if __name__ == "__main__":
110136
main()

0 commit comments

Comments
 (0)