Skip to content

Commit 0568fa3

Browse files
committed
feat: add execute_update example for go
1 parent dfccd64 commit 0568fa3

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

go/client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ func (client *Client) Execute(sql string) ([]arrow.Record, error) {
102102
return client.doGet(flightInfo.GetEndpoint()[0].GetTicket())
103103
}
104104

105+
// Executes the sql on Datalayers and returns the affected rows.
106+
// The supported sqls are Insert and Delete. Note, the development for supporting Delete is in progress.
107+
func (client *Client) ExecuteUpdate(sql string) (int64, error) {
108+
affectedRows, err := client.inner.ExecuteUpdate(client.ctx, sql)
109+
if err != nil {
110+
return 0, fmt.Errorf("failed to execute a sql: %v", err)
111+
}
112+
return affectedRows, nil
113+
}
114+
105115
// Creates a prepared statement.
106116
func (client *Client) Prepare(sql string) (*flightsql.PreparedStatement, error) {
107117
return client.inner.Prepare(client.ctx, sql)
@@ -119,6 +129,11 @@ func (client *Client) ExecutePrepared(preparedStmt *flightsql.PreparedStatement,
119129
return client.doGet(flightInfo.GetEndpoint()[0].GetTicket())
120130
}
121131

132+
// Closes the prepared statement.
133+
func (client *Client) ClosePrepared(preparedStmt *flightsql.PreparedStatement) error {
134+
return preparedStmt.Close(client.ctx)
135+
}
136+
122137
// Calls the `DoGet` method of the FlightSQL client.
123138
func (client *Client) doGet(ticket *flight.Ticket) ([]arrow.Record, error) {
124139
reader, err := client.inner.DoGet(client.ctx, ticket)

go/main.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,40 @@ func main() {
147147
// 2024-09-01 10:05:00 +0800 CST 2 15.30 1
148148
// 2024-09-02 10:05:00 +0800 CST 2 15.30 1
149149
PrintRecords(result)
150+
151+
// Closes the prepared statement to notify releasing resources on server side.
152+
if err = client.ClosePrepared(preparedStmt); err != nil {
153+
fmt.Println("Failed to close a prepared statement: ", err)
154+
return
155+
}
156+
157+
// There provides a dedicated interface `execute_update` for executing DMLs, including Insert, Delete.
158+
// This interface directly returns the affected rows which might be convenient for some use cases.
159+
//
160+
// Note, Datalayers does not support Update and the development for Delete is in progress.
161+
sql = `
162+
INSERT INTO go.demo (ts, sid, value, flag) VALUES
163+
('2024-09-03T10:00:00+08:00', 1, 4.5, 0),
164+
('2024-09-03T10:05:00+08:00', 2, 11.6, 1);`
165+
affectedRows, err := client.ExecuteUpdate(sql)
166+
if err != nil {
167+
fmt.Println("Failed to insert data: ", err)
168+
return
169+
}
170+
// The output should be:
171+
// Affected rows: 2
172+
fmt.Println("Affected rows: ", affectedRows)
173+
174+
// Checks that the data are inserted successfully.
175+
sql = "SELECT * FROM go.demo where ts >= '2024-09-03T10:00:00+08:00'"
176+
result, err = client.Execute(sql)
177+
if err != nil {
178+
fmt.Println("Failed to scan data: ", err)
179+
return
180+
}
181+
// The result should be:
182+
// ts sid value flag
183+
// 2024-09-03 10:00:00 +0800 CST 1 4.50 0
184+
// 2024-09-03 10:05:00 +0800 CST 2 11.60 1
185+
PrintRecords(result)
150186
}

0 commit comments

Comments
 (0)