Skip to content

Commit

Permalink
mssql: rename structs to prevent stutter. Use alias for compatibility…
Browse files Browse the repository at this point in the history
… for now.
  • Loading branch information
kardianos committed Mar 15, 2018
1 parent 4fcf9a9 commit 6a30f4e
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 103 deletions.
34 changes: 17 additions & 17 deletions bulkcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ import (
"time"
)

type MssqlBulk struct {
cn *MssqlConn
type Bulk struct {
cn *Conn
metadata []columnStruct
bulkColumns []columnStruct
columnsName []string
tablename string
numRows int

headerSent bool
Options MssqlBulkOptions
Options BulkOptions
Debug bool
}
type MssqlBulkOptions struct {
type BulkOptions struct {
CheckConstraints bool
FireTriggers bool
KeepNulls bool
Expand All @@ -36,13 +36,13 @@ type MssqlBulkOptions struct {

type DataValue interface{}

func (cn *MssqlConn) CreateBulk(table string, columns []string) (_ *MssqlBulk) {
b := MssqlBulk{cn: cn, tablename: table, headerSent: false, columnsName: columns}
func (cn *Conn) CreateBulk(table string, columns []string) (_ *Bulk) {
b := Bulk{cn: cn, tablename: table, headerSent: false, columnsName: columns}
b.Debug = false
return &b
}

func (b *MssqlBulk) sendBulkCommand() (err error) {
func (b *Bulk) sendBulkCommand() (err error) {
//get table columns info
err = b.getMetadata()
if err != nil {
Expand Down Expand Up @@ -139,7 +139,7 @@ func (b *MssqlBulk) sendBulkCommand() (err error) {

// AddRow immediately writes the row to the destination table.
// The arguments are the row values in the order they were specified.
func (b *MssqlBulk) AddRow(row []interface{}) (err error) {
func (b *Bulk) AddRow(row []interface{}) (err error) {
if !b.headerSent {
err = b.sendBulkCommand()
if err != nil {
Expand All @@ -166,7 +166,7 @@ func (b *MssqlBulk) AddRow(row []interface{}) (err error) {
return
}

func (b *MssqlBulk) makeRowData(row []interface{}) ([]byte, error) {
func (b *Bulk) makeRowData(row []interface{}) ([]byte, error) {
buf := new(bytes.Buffer)
buf.WriteByte(byte(tokenRow))

Expand Down Expand Up @@ -196,7 +196,7 @@ func (b *MssqlBulk) makeRowData(row []interface{}) ([]byte, error) {
return buf.Bytes(), nil
}

func (b *MssqlBulk) Done() (rowcount int64, err error) {
func (b *Bulk) Done() (rowcount int64, err error) {
if b.headerSent == false {
//no rows had been sent
return 0, nil
Expand Down Expand Up @@ -235,7 +235,7 @@ func (b *MssqlBulk) Done() (rowcount int64, err error) {
return rowCount, nil
}

func (b *MssqlBulk) createColMetadata() []byte {
func (b *Bulk) createColMetadata() []byte {
buf := new(bytes.Buffer)
buf.WriteByte(byte(tokenColMetadata)) // token
binary.Write(buf, binary.LittleEndian, uint16(len(b.bulkColumns))) // column count
Expand Down Expand Up @@ -267,7 +267,7 @@ func (b *MssqlBulk) createColMetadata() []byte {
return buf.Bytes()
}

func (b *MssqlBulk) getMetadata() (err error) {
func (b *Bulk) getMetadata() (err error) {
stmt, err := b.cn.Prepare("SET FMTONLY ON")
if err != nil {
return
Expand All @@ -283,7 +283,7 @@ func (b *MssqlBulk) getMetadata() (err error) {
if err != nil {
return
}
stmt2 := stmt.(*MssqlStmt)
stmt2 := stmt.(*Stmt)
cols, err := stmt2.QueryMeta()
if err != nil {
return fmt.Errorf("get columns info failed: %v", err.Error())
Expand All @@ -301,8 +301,8 @@ func (b *MssqlBulk) getMetadata() (err error) {
return nil
}

// QueryMeta is almost the same as MssqlStmt.Query, but returns all the columns info.
func (s *MssqlStmt) QueryMeta() (cols []columnStruct, err error) {
// QueryMeta is almost the same as mssql.Stmt.Query, but returns all the columns info.
func (s *Stmt) QueryMeta() (cols []columnStruct, err error) {
if err = s.sendQuery(nil); err != nil {
return
}
Expand All @@ -324,7 +324,7 @@ loop:
return cols, nil
}

func (b *MssqlBulk) makeParam(val DataValue, col columnStruct) (res Param, err error) {
func (b *Bulk) makeParam(val DataValue, col columnStruct) (res Param, err error) {
res.ti.Size = col.ti.Size
res.ti.TypeId = col.ti.TypeId

Expand Down Expand Up @@ -609,7 +609,7 @@ func (b *MssqlBulk) makeParam(val DataValue, col columnStruct) (res Param, err e

}

func (b *MssqlBulk) dlogf(format string, v ...interface{}) {
func (b *Bulk) dlogf(format string, v ...interface{}) {
if b.Debug {
b.cn.sess.log.Printf(format, v...)
}
Expand Down
12 changes: 6 additions & 6 deletions bulkcopy_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ import (
)

type copyin struct {
cn *MssqlConn
bulkcopy *MssqlBulk
cn *Conn
bulkcopy *Bulk
closed bool
}

type serializableBulkConfig struct {
TableName string
ColumnsName []string
Options MssqlBulkOptions
Options BulkOptions
}

func (d *MssqlDriver) OpenConnection(dsn string) (*MssqlConn, error) {
func (d *Driver) OpenConnection(dsn string) (*Conn, error) {
return d.open(context.Background(), dsn)
}

func (c *MssqlConn) prepareCopyIn(query string) (_ driver.Stmt, err error) {
func (c *Conn) prepareCopyIn(query string) (_ driver.Stmt, err error) {
config_json := query[11:]

bulkconfig := serializableBulkConfig{}
Expand All @@ -43,7 +43,7 @@ func (c *MssqlConn) prepareCopyIn(query string) (_ driver.Stmt, err error) {
return ci, nil
}

func CopyIn(table string, options MssqlBulkOptions, columns ...string) string {
func CopyIn(table string, options BulkOptions, columns ...string) string {
bulkconfig := &serializableBulkConfig{TableName: table, Options: options, ColumnsName: columns}

config_json, err := json.Marshal(bulkconfig)
Expand Down
2 changes: 1 addition & 1 deletion bulkcopy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestBulkcopy(t *testing.T) {

log.Println("Preparing copyin statement")

stmt, err := conn.Prepare(CopyIn(tableName, MssqlBulkOptions{}, columns...))
stmt, err := conn.Prepare(CopyIn(tableName, BulkOptions{}, columns...))

for i := 0; i < 10; i++ {
log.Printf("Executing copy in statement %d time with %d values", i+1, len(values))
Expand Down
2 changes: 1 addition & 1 deletion examples/bulk/bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func main() {
log.Fatal(err)
}

stmt, err := txn.Prepare(mssql.CopyIn("test_table", mssql.MssqlBulkOptions{}, "test_varchar", "test_nvarchar", "test_float", "test_bigint"))
stmt, err := txn.Prepare(mssql.CopyIn("test_table", mssql.BulkOptions{}, "test_varchar", "test_nvarchar", "test_float", "test_bigint"))
if err != nil {
log.Fatal(err.Error())
}
Expand Down
Loading

0 comments on commit 6a30f4e

Please sign in to comment.