lidavidm commented on code in PR #3325:
URL: https://github.com/apache/arrow-adbc/pull/3325#discussion_r2335003947


##########
go/adbc/driver/databricks/statement.go:
##########
@@ -0,0 +1,225 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package databricks
+
+import (
+       "context"
+       "database/sql"
+       "database/sql/driver"
+       "fmt"
+
+       "github.com/apache/arrow-adbc/go/adbc"
+       "github.com/apache/arrow-go/v18/arrow"
+       "github.com/apache/arrow-go/v18/arrow/array"
+
+       dbsqlrows "github.com/databricks/databricks-sql-go/rows"
+)
+
+type statementImpl struct {
+       conn       *connectionImpl
+       query      string
+       parameters []interface{}
+       prepared   *sql.Stmt
+}
+
+func (s *statementImpl) Close() error {
+       if s.conn == nil {
+               return adbc.Error{
+                       Msg:  "statement already closed",
+                       Code: adbc.StatusInvalidState,
+               }
+       }
+       if s.prepared != nil {
+               return s.prepared.Close()
+       }
+       return nil
+}
+
+func (s *statementImpl) SetOption(key, val string) error {
+       // No statement-specific options are supported yet
+       return adbc.Error{
+               Code: adbc.StatusNotImplemented,
+               Msg:  fmt.Sprintf("unsupported statement option: %s", key),
+       }
+}
+
+func (s *statementImpl) SetSqlQuery(query string) error {
+       s.query = query
+       // Reset prepared statement if query changes
+       if s.prepared != nil {
+               if err := s.prepared.Close(); err != nil {
+                       return adbc.Error{
+                               Code: adbc.StatusInvalidState,
+                               Msg:  fmt.Sprintf("failed to close previous 
prepared statement: %w", err),
+                       }
+               }
+               s.prepared = nil
+       }
+       return nil
+}
+
+func (s *statementImpl) Prepare(ctx context.Context) error {
+       if s.query == "" {
+               return adbc.Error{
+                       Code: adbc.StatusInvalidState,
+                       Msg:  "no query set",
+               }
+       }
+
+       stmt, err := s.conn.conn.PrepareContext(ctx, s.query)
+       if err != nil {
+               return adbc.Error{
+                       Code: adbc.StatusInvalidState,
+                       Msg:  fmt.Sprintf("failed to prepare statement: %v", 
err),
+               }
+       }
+
+       s.prepared = stmt
+       return nil
+}
+
+func (s *statementImpl) ExecuteQuery(ctx context.Context) (array.RecordReader, 
int64, error) {
+       if s.query == "" {
+               return nil, -1, adbc.Error{
+                       Code: adbc.StatusInvalidState,
+                       Msg:  "no query set",
+               }
+       }
+
+       // Execute query using raw driver interface to get Arrow batches
+       var driverRows driver.Rows
+       var err error
+       err = s.conn.conn.Raw(func(driverConn interface{}) error {
+               // Use raw driver interface for direct Arrow access
+               if queryerCtx, ok := driverConn.(driver.QueryerContext); ok {
+                       // Convert parameters to driver.NamedValue slice
+                       var driverArgs []driver.NamedValue
+                       for i, param := range s.parameters {
+                               driverArgs = append(driverArgs, 
driver.NamedValue{
+                                       Ordinal: i + 1,
+                                       Value:   param,
+                               })
+                       }
+                       driverRows, err = queryerCtx.QueryContext(ctx, s.query, 
driverArgs)
+                       return err
+               }
+               return fmt.Errorf("driver does not support QueryerContext")

Review Comment:
   I think it's cleaner to just assume the interface is available; if it isn't, 
the driver is broken _anyways_.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to