shuwenwei commented on code in PR #162:
URL: https://github.com/apache/iotdb-client-go/pull/162#discussion_r3412148839


##########
database/iotdb_std.go:
##########
@@ -0,0 +1,230 @@
+/*
+ * 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 iotdb_go
+
+import (
+       "context"
+       "database/sql"
+       "database/sql/driver"
+       "errors"
+       "io"
+       "log"
+       "net"
+       "os"
+       "syscall"
+)
+
+var globalConnID int64
+
+func init() {
+       var debugf = func(format string, v ...any) {}
+       sql.Register("iotdb", &stdDriver{debugf: debugf})
+}
+
+// isConnBrokenError returns true if the error class indicates that the
+// db connection is no longer usable and should be marked bad
+func isConnBrokenError(err error) bool {
+       if errors.Is(err, io.EOF) || errors.Is(err, syscall.EPIPE) || 
errors.Is(err, syscall.ECONNRESET) {
+               return true
+       }
+       if _, ok := err.(*net.OpError); ok {
+               return true
+       }
+       return false
+}
+
+type stdDriver struct {
+       opt    *Options
+       conn   stdConnect
+       commit func() error
+       debugf func(format string, v ...any)
+}
+
+var _ driver.Conn = (*stdDriver)(nil)
+var _ driver.ConnBeginTx = (*stdDriver)(nil)
+var _ driver.ExecerContext = (*stdDriver)(nil)
+var _ driver.QueryerContext = (*stdDriver)(nil)
+var _ driver.ConnPrepareContext = (*stdDriver)(nil)
+
+func (std *stdDriver) Open(dsn string) (_ driver.Conn, err error) {
+       var opt Options
+       if err := opt.fromDSN(dsn); err != nil {
+               std.debugf("Open dsn error: %v\n", err)
+               return nil, err
+       }
+       var debugf = func(format string, v ...any) {}
+       if opt.Debug {
+               debugf = log.New(os.Stdout, "[iotdb-std][opener] ", 0).Printf
+       }
+       opt.ClientInfo.Comment = []string{"database/sql"}
+       return (&stdConnOpener{opt: &opt, debugf: 
debugf}).Connect(context.Background())
+}
+
+var _ driver.Driver = (*stdDriver)(nil)
+
+func (std *stdDriver) ResetSession(ctx context.Context) error {
+       if std.conn.isBad() {
+               std.debugf("Resetting session because connection is bad")
+               return driver.ErrBadConn
+       }
+       return nil
+}
+
+var _ driver.SessionResetter = (*stdDriver)(nil)
+
+func (std *stdDriver) Ping(ctx context.Context) error {
+       if std.conn.isBad() {
+               std.debugf("Ping: connection is bad")
+               return driver.ErrBadConn
+       }
+
+       return std.conn.ping(ctx)
+}
+
+var _ driver.Pinger = (*stdDriver)(nil)
+
+// Begin starts and returns a new transaction.
+//
+// Deprecated: Drivers should implement ConnBeginTx instead (or additionally).
+func (std *stdDriver) Begin() (driver.Tx, error) {
+       if std.conn.isBad() {
+               std.debugf("Begin: connection is bad")
+               return nil, driver.ErrBadConn
+       }
+       return std, nil
+}
+
+func (std *stdDriver) BeginTx(ctx context.Context, opts driver.TxOptions) 
(driver.Tx, error) {

Review Comment:
   This transaction implementation is unsafe as-is. `Begin`/`BeginTx` return a 
valid `driver.Tx`, but `Commit` is effectively a no-op and `Rollback` only 
clears local state, so callers using `db.Begin()` will
     believe rollback semantics are supported while all executed statements 
have already taken effect.
   
     If IoTDB does not support transactions through this client path, 
`Begin`/`BeginTx` should return an explicit unsupported error instead of a 
no-op transaction. Otherwise this needs real transaction/session
     semantics before exposing `driver.Tx`.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to