|
| 1 | +package chdb |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "database/sql/driver" |
| 7 | + "fmt" |
| 8 | + "reflect" |
| 9 | + "bytes" |
| 10 | + wrapper "github.com/chdb-io/chdb-go/chdb" |
| 11 | + "github.com/chdb-io/chdb-go/chdbstable" |
| 12 | + "github.com/apache/arrow/go/v14/arrow/ipc" |
| 13 | +) |
| 14 | + |
| 15 | +func init() { |
| 16 | + sql.Register("chdb", Driver{}) |
| 17 | +} |
| 18 | + |
| 19 | +type connector struct { |
| 20 | +} |
| 21 | + |
| 22 | +// Connect returns a connection to a database. |
| 23 | +func (c *connector) Connect(ctx context.Context) (driver.Conn, error) { |
| 24 | + return &conn{}, nil |
| 25 | +} |
| 26 | + |
| 27 | +// Driver returns the underying Driver of the connector, |
| 28 | +// compatibility with the Driver method on sql.DB |
| 29 | +func (c *connector) Driver() driver.Driver { return Driver{} } |
| 30 | + |
| 31 | +type Driver struct{} |
| 32 | + |
| 33 | +// Open returns a new connection to the database. |
| 34 | +func (d Driver) Open(name string) (driver.Conn, error) { |
| 35 | + return &conn{}, nil |
| 36 | +} |
| 37 | + |
| 38 | +// OpenConnector expects the same format as driver.Open |
| 39 | +func (d Driver) OpenConnector(dataSourceName string) (driver.Connector, error) { |
| 40 | + return &connector{}, nil |
| 41 | +} |
| 42 | + |
| 43 | +type conn struct { |
| 44 | +} |
| 45 | + |
| 46 | +func (c *conn) Close() error { |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +func (c *conn) Query(query string, values []driver.Value) (driver.Rows, error) { |
| 51 | + namedValues := make([]driver.NamedValue, len(values)) |
| 52 | + for i, value := range values { |
| 53 | + namedValues[i] = driver.NamedValue{ |
| 54 | + // nb: Name field is optional |
| 55 | + Ordinal: i, |
| 56 | + Value: value, |
| 57 | + } |
| 58 | + } |
| 59 | + return c.QueryContext(context.Background(), query, namedValues) |
| 60 | +} |
| 61 | + |
| 62 | +func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) { |
| 63 | + result := wrapper.Query(query, "Arrow") |
| 64 | + reader := bytes.NewReader(result.Buf()) |
| 65 | + // rr, err := ipc.NewReader(reader, ipc.WithAllocator(memory.DefaultAllocator)) |
| 66 | + rr, err := ipc.NewFileReader(reader) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + return &rows{localResult: result, reader: rr}, nil |
| 71 | +} |
| 72 | + |
| 73 | +func (c *conn) Begin() (driver.Tx, error) { |
| 74 | + return nil, fmt.Errorf("does not support Transcation") |
| 75 | +} |
| 76 | + |
| 77 | +func (c *conn) Prepare(query string) (driver.Stmt, error) { |
| 78 | + return c.PrepareContext(context.Background(), query) |
| 79 | +} |
| 80 | + |
| 81 | +func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) { |
| 82 | + return nil, fmt.Errorf("does not support prepare statement") |
| 83 | +} |
| 84 | + |
| 85 | +// todo: func(c *conn) Prepare(query string) |
| 86 | +// todo: func(c *conn) PrepareContext(ctx context.Context, query string) |
| 87 | +// todo: prepared statment |
| 88 | + |
| 89 | +type rows struct { |
| 90 | + localResult *chdbstable.LocalResult |
| 91 | + // reader *ipc.Reader |
| 92 | + reader *ipc.FileReader |
| 93 | +} |
| 94 | + |
| 95 | +func (r *rows) Columns() ([]string) { |
| 96 | + fmt.Println(r.reader.Schema().Metadata().Keys()) |
| 97 | + return r.reader.Schema().Metadata().Keys() |
| 98 | +} |
| 99 | + |
| 100 | +func (r *rows) Close() error { |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +func (r *rows) Next(dest []driver.Value) error { |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +func (r *rows) ColumnTypeDatabaseTypeName(index int) string { |
| 109 | + return "" |
| 110 | +} |
| 111 | + |
| 112 | +func (r *rows) ColumnTypeNullable(index int) (nullable, ok bool) { |
| 113 | + return |
| 114 | +} |
| 115 | + |
| 116 | +func (r *rows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) { |
| 117 | + return |
| 118 | +} |
| 119 | + |
| 120 | +func (r *rows) ColumnTypeScanType(index int) reflect.Type { |
| 121 | + return reflect.TypeOf(nil) |
| 122 | +} |
0 commit comments