Skip to content

Commit

Permalink
add IStringConverter for custom encode and decode
Browse files Browse the repository at this point in the history
  • Loading branch information
sijms committed Apr 9, 2021
1 parent 977caa8 commit 8e9f092
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
24 changes: 12 additions & 12 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,11 +639,11 @@ func (stmt *defaultStmt) read(dataSet *DataSet) error {
//fmt.Println("type: ", dataSet.Cols[x].DataType)
switch dataSet.Cols[x].DataType {
case NCHAR, CHAR, LONG:
if stmt.connection.strConv.LangID != dataSet.Cols[x].CharsetID {
tempCharset := stmt.connection.strConv.LangID
stmt.connection.strConv.LangID = dataSet.Cols[x].CharsetID
if stmt.connection.strConv.GetLangID() != dataSet.Cols[x].CharsetID {
tempCharset := stmt.connection.strConv.GetLangID()
stmt.connection.strConv.SetLangID(dataSet.Cols[x].CharsetID)
dataSet.currentRow[x] = stmt.connection.strConv.Decode(temp)
stmt.connection.strConv.LangID = tempCharset
stmt.connection.strConv.SetLangID(tempCharset)
} else {
dataSet.currentRow[x] = stmt.connection.strConv.Decode(temp)
}
Expand Down Expand Up @@ -725,18 +725,18 @@ func (stmt *defaultStmt) read(dataSet *DataSet) error {
}
dataSet.currentRow[x] = lobData
} else {
tempCharset := stmt.connection.strConv.LangID
tempCharset := stmt.connection.strConv.GetLangID()
if lob.variableWidthChar() {
if stmt.connection.dBVersion.Number < 10200 && lob.littleEndianClob() {
stmt.connection.strConv.LangID = 2002
stmt.connection.strConv.SetLangID(2002)
} else {
stmt.connection.strConv.LangID = 2000
stmt.connection.strConv.SetLangID(2000)
}
} else {
stmt.connection.strConv.LangID = dataSet.Cols[x].CharsetID
stmt.connection.strConv.SetLangID(dataSet.Cols[x].CharsetID)
}
resultClobString := stmt.connection.strConv.Decode(lobData)
stmt.connection.strConv.LangID = tempCharset
stmt.connection.strConv.SetLangID(tempCharset)
if dataSize != int64(len([]rune(resultClobString))) {
return errors.New("error reading clob data")
}
Expand Down Expand Up @@ -1048,10 +1048,10 @@ func (stmt *Stmt) NewParam(name string, val driver.Value, size int, direction Pa
param.BValue = nil
param.MaxLen = 1
} else {
tempCharset := stmt.connection.strConv.LangID
stmt.connection.strConv.LangID = param.CharsetID
tempCharset := stmt.connection.strConv.GetLangID()
stmt.connection.strConv.SetLangID(param.CharsetID)
param.BValue = stmt.connection.strConv.Encode(val)
stmt.connection.strConv.LangID = tempCharset
stmt.connection.strConv.SetLangID(tempCharset)
if size > len(val) {
param.MaxCharLen = size
}
Expand Down
2 changes: 1 addition & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type Connection struct {
sessionID int
serialID int
transactionID []byte
strConv *converters.StringConverter
strConv converters.IStringConverter
NLSData NLSData
}

Expand Down
14 changes: 13 additions & 1 deletion converters/string_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import (
"unicode/utf16"
)

type IStringConverter interface {
Encode(string) []byte
Decode([]byte) string
GetLangID() int
SetLangID(langID int)
}

type StringConverter struct {
LangID int
CharWidth int
Expand Down Expand Up @@ -33,7 +40,12 @@ func MaxBytePerChar(charsetID int) int {
return 1
}
}

func (conv *StringConverter) GetLangID() int {
return conv.LangID
}
func (conv *StringConverter) SetLangID(langID int) {
conv.LangID = langID
}
func (conv *StringConverter) Encode(input string) []byte {
if len(input) == 0 {
return nil
Expand Down
2 changes: 1 addition & 1 deletion network/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Session struct {
HasFSAPCapability bool
Summary *SummaryObject
states []sessionState
StrConv *converters.StringConverter
StrConv converters.IStringConverter
UseBigClrChunks bool
ClrChunkSize int
}
Expand Down

0 comments on commit 8e9f092

Please sign in to comment.