Skip to content

Commit

Permalink
1- solve the problem when server send 0 precision and 0 scale I need …
Browse files Browse the repository at this point in the history
…to define precision = 38 and scale = 0

2- redefine PutBytes to receive array of bytes instead of byte slice
  • Loading branch information
sijms committed Nov 1, 2020
1 parent a7481f7 commit 42e545b
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 88 deletions.
8 changes: 4 additions & 4 deletions auth_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ func NewAuthObject(username string, password string, tcpNego *TCPNego, session *
func (obj *AuthObject) Write(connOption *network.ConnectionOption, mode LogonMode, session *network.Session) error {
session.ResetBuffer()
keyValSize := 22
session.PutBytes([]byte{3, 0x73, 0})
session.PutBytes(3, 0x73, 0)
if len(connOption.UserID) > 0 {
session.PutInt(1, 1, false, false)
session.PutInt(len(connOption.UserID), 4, true, true)
} else {
session.PutBytes([]byte{0, 0})
session.PutBytes(0, 0)
}

if len(connOption.UserID) > 0 && len(obj.EPassword) > 0 {
Expand All @@ -151,9 +151,9 @@ func (obj *AuthObject) Write(connOption *network.ConnectionOption, mode LogonMod
session.PutUint(int(mode), 4, true, true)
session.PutUint(1, 1, false, false)
session.PutUint(keyValSize, 4, true, true)
session.PutBytes([]byte{1, 1})
session.PutBytes(1, 1)
if len(connOption.UserID) > 0 {
session.PutBytes([]byte(connOption.UserID))
session.PutBytes([]byte(connOption.UserID)...)
}
index := 0
if len(obj.EClientSessKey) > 0 {
Expand Down
61 changes: 36 additions & 25 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,24 @@ func NewStmt(text string, conn *Connection) *Stmt {

func (stmt *Stmt) write(session *network.Session) error {
exeOp := stmt.getExeOption()
session.PutBytes([]byte{3, 0x5E, 0})
session.PutBytes(3, 0x5E, 0)
session.PutUint(exeOp, 4, true, true)
session.PutUint(stmt.cursorID, 2, true, true)
if stmt.cursorID == 0 {
session.PutUint(1, 1, false, false)
session.PutBytes(1)
//session.PutUint(1, 1, false, false)
} else {
session.PutUint(0, 1, false, false)
session.PutBytes(0)
//session.PutUint(0, 1, false, false)
}
session.PutUint(len(stmt.Text), 4, true, true)
session.PutUint(1, 1, false, false)
session.PutBytes(1)
//session.PutUint(1, 1, false, false)
session.PutUint(13, 2, true, true)
session.PutBytes([]byte{0, 0})
session.PutBytes(0, 0)
if exeOp&0x40 == 0 && exeOp&0x20 != 0 && exeOp&0x1 != 0 && stmt.stmtType == SELECT {
session.PutUint(0, 1, false, false)
session.PutBytes(0)
//session.PutUint(0, 1, false, false)
session.PutUint(stmt.noOfRowsToFetch, 4, true, true)
} else {
session.PutUint(0, 4, true, true)
Expand All @@ -135,42 +139,49 @@ func (stmt *Stmt) write(session *network.Session) error {
// longFetchSize == 0 marshal 1 else marshal longFetchSize
session.PutUint(1, 4, true, true)
if len(stmt.Pars) > 0 {
session.PutUint(1, 1, false, false)
session.PutBytes(1)
//session.PutUint(1, 1, false, false)
session.PutUint(len(stmt.Pars), 2, true, true)
} else {
session.PutUint(0, 1, false, false)
session.PutUint(0, 1, false, false)
session.PutBytes(0, 0)
//session.PutUint(0, 1, false, false)
//session.PutUint(0, 1, false, false)
}
session.PutBytes([]byte{0, 0, 0, 0, 0})
session.PutBytes(0, 0, 0, 0, 0)
if stmt.define {
session.PutUint(1, 1, false, false)
session.PutBytes(1)
//session.PutUint(1, 1, false, false)
session.PutUint(stmt.noOfDefCols, 2, true, true)
} else {
session.PutUint(0, 1, false, false)
session.PutUint(0, 1, false, false)
session.PutBytes(0, 0)
//session.PutUint(0, 1, false, false)
//session.PutUint(0, 1, false, false)
}
if session.TTCVersion >= 4 {
session.PutUint(0, 1, false, false) // dbChangeRegisterationId
session.PutUint(0, 1, false, false)
session.PutUint(1, 1, false, false)
session.PutBytes(0, 0, 1)
//session.PutUint(0, 1, false, false) // dbChangeRegisterationId
//session.PutUint(0, 1, false, false)
//session.PutUint(1, 1, false, false)
}
if session.TTCVersion >= 5 {
session.PutUint(0, 1, false, false)
session.PutUint(0, 1, false, false)
session.PutUint(0, 1, false, false)
session.PutUint(0, 1, false, false)
session.PutUint(0, 1, false, false)
session.PutBytes(0, 0, 0, 0, 0)
//session.PutUint(0, 1, false, false)
//session.PutUint(0, 1, false, false)
//session.PutUint(0, 1, false, false)
//session.PutUint(0, 1, false, false)
//session.PutUint(0, 1, false, false)
}

session.PutBytes([]byte(stmt.Text))
session.PutBytes([]byte(stmt.Text)...)
for x := 0; x < len(stmt.al8i4); x++ {
session.PutUint(stmt.al8i4[x], 2, true, true)
}
for _, par := range stmt.Pars {
_ = par.write(session)
}
if len(stmt.Pars) > 0 {
session.PutUint(7, 1, false, false)
session.PutBytes(7)
//session.PutUint(7, 1, false, false)
for _, par := range stmt.Pars {
session.PutClr(par.Value)
}
Expand Down Expand Up @@ -247,7 +258,7 @@ func (stmt *Stmt) getExeOption() int {
}
func (stmt *Stmt) fetch(dataSet *DataSet) error {
stmt.connection.session.ResetBuffer()
stmt.connection.session.PutBytes([]byte{3, 5, 0})
stmt.connection.session.PutBytes(3, 5, 0)
stmt.connection.session.PutInt(stmt.cursorID, 2, true, true)
stmt.connection.session.PutInt(stmt.noOfRowsToFetch, 2, true, true)
err := stmt.connection.session.Write()
Expand Down Expand Up @@ -583,7 +594,7 @@ func (stmt *Stmt) read(dataSet *DataSet) error {
func (stmt *Stmt) Close() error {
session := stmt.connection.session
session.ResetBuffer()
session.PutBytes([]byte{17, 105, 0, 1, 1, 1})
session.PutBytes(17, 105, 0, 1, 1, 1)
session.PutInt(stmt.cursorID, 4, true, true)
return (&simpleObject{
session: session,
Expand Down
12 changes: 5 additions & 7 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,8 @@ func (conn *Connection) Ping(ctx context.Context) error {
func (conn *Connection) Logoff() error {
session := conn.session
session.ResetBuffer()
session.PutBytes([]byte{0x11, 0x87, 0, 0, 0, 0x2, 0x1, 0x11,
0x1, 0, 0, 0, 0x1, 0, 0, 0,
0, 0, 0x1, 0, 0, 0, 0, 0,
3, 9, 0})
session.PutBytes(0x11, 0x87, 0, 0, 0, 0x2, 0x1, 0x11, 0x1, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0,
3, 9, 0)
err := session.Write()
if err != nil {
return err
Expand Down Expand Up @@ -362,12 +360,12 @@ func (conn *Connection) Close() (err error) {

func (conn *Connection) doAuth() error {
conn.session.ResetBuffer()
conn.session.PutBytes([]byte{3, 118, 0, 1})
conn.session.PutBytes(3, 118, 0, 1)
conn.session.PutUint(len(conn.conStr.UserID), 4, true, true)
conn.LogonMode = conn.LogonMode | NoNewPass
conn.session.PutUint(int(conn.LogonMode), 4, true, true)
conn.session.PutBytes([]byte{1, 1, 5, 1, 1})
conn.session.PutBytes([]byte(conn.conStr.UserID))
conn.session.PutBytes(1, 1, 5, 1, 1)
conn.session.PutBytes([]byte(conn.conStr.UserID)...)
conn.session.PutKeyValString("AUTH_TERMINAL", conn.connOption.ClientData.HostName, 0)
conn.session.PutKeyValString("AUTH_PROGRAM_NM", conn.connOption.ClientData.ProgramName, 0)
conn.session.PutKeyValString("AUTH_MACHINE", conn.connOption.ClientData.HostName, 0)
Expand Down
2 changes: 1 addition & 1 deletion data_type_nego.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ func buildTypeNego(nego *TCPNego, session *network.Session) (*DataTypeNego, erro
result.RuntimeTypeAndRep = result.TypeAndRep
}
session.ResetBuffer()
session.PutBytes(result.bytes())
session.PutBytes(result.bytes()...)
err := session.Write()
if err != nil {
return nil, err
Expand Down
36 changes: 18 additions & 18 deletions db_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (
)

type DBVersion struct {
Info string
Text string
Number uint16
MajorVersion int
MinorVersion int
PatchsetVersion int
Info string
Text string
Number uint16
MajorVersion int
MinorVersion int
PatchsetVersion int
isDb10gR20OrHigher bool
isDb11gR10OrHigher bool
}

func GetDBVersion(session *network.Session) (*DBVersion, error){
func GetDBVersion(session *network.Session) (*DBVersion, error) {
session.ResetBuffer()
session.PutBytes([]byte{3, 0x3B, 0})
session.PutBytes(3, 0x3B, 0)
session.PutUint(1, 1, false, false)
session.PutUint(0x100, 2, true, true)
session.PutUint(1, 1, false, false)
Expand All @@ -44,16 +44,16 @@ func GetDBVersion(session *network.Session) (*DBVersion, error){
if err != nil {
return nil, err
}
version := (number >> 24 & 0xFF) * 1000 + (number >> 20 & 0xF) * 100 + (number >> 12 & 0xF) * 10 + (number >> 8 & 0xF)
text := fmt.Sprintf("%d.%d.%d.%d.%d", number >> 24 & 0xFF, number >> 20 & 0xF,
number >> 12 & 0xF, number >> 8 & 0xF, number & 0xFF)
version := (number>>24&0xFF)*1000 + (number>>20&0xF)*100 + (number>>12&0xF)*10 + (number >> 8 & 0xF)
text := fmt.Sprintf("%d.%d.%d.%d.%d", number>>24&0xFF, number>>20&0xF,
number>>12&0xF, number>>8&0xF, number&0xFF)

ret := &DBVersion{
Info: string(info),
Text: text,
Number: uint16(version),
MajorVersion: int(number >> 24 & 0xFF),
MinorVersion: int(number >> 20 & 0xF),
ret := &DBVersion{
Info: string(info),
Text: text,
Number: uint16(version),
MajorVersion: int(number >> 24 & 0xFF),
MinorVersion: int(number >> 20 & 0xF),
PatchsetVersion: int(number >> 8 & 0xF),
}
if ret.MajorVersion > 10 || (ret.MajorVersion == 10 && ret.MinorVersion >= 2) {
Expand All @@ -63,4 +63,4 @@ func GetDBVersion(session *network.Session) (*DBVersion, error){
ret.isDb11gR10OrHigher = true
}
return ret, nil
}
}
11 changes: 8 additions & 3 deletions network/redirect_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ func newRedirectPacketFromData(packetData []byte) *RedirectPacket {
}
data := string(packetData[10 : 10+dataLen])
if pck.packet.flag&0x2 == 0 {
return nil
pck.redirectAddr = data
return &pck
}
length := strings.Index(data, "\x00")
pck.redirectAddr = data[:length]
pck.reconnectData = data[length:]
if length > 0 {
pck.redirectAddr = data[:length]
pck.reconnectData = data[length:]
} else {
pck.redirectAddr = data
}
return &pck
}
8 changes: 6 additions & 2 deletions network/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (session *Session) Connect() error {
return err
}
if connectPacket.packet.length == 58 {
session.PutBytes(connectPacket.buffer)
session.PutBytes(connectPacket.buffer...)
err = session.Write()
if err != nil {
return err
Expand Down Expand Up @@ -349,10 +349,14 @@ func (session *Session) readPacket() (PacketInterface, error) {
}
}

func (session *Session) PutBytes(data []byte) {
func (session *Session) PutBytes(data ...byte) {
session.outBuffer = append(session.outBuffer, data...)
}

//func (session *Session) PutByte(num byte) {
// session.outBuffer = append(session.outBuffer, num)
//}

func (session *Session) PutUint(number interface{}, size uint8, bigEndian bool, compress bool) {
var num uint64
switch number := number.(type) {
Expand Down
55 changes: 34 additions & 21 deletions parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ type ParameterInfo struct {
DataType OracleType
IsXmlType bool
Flag uint8
Precision uint16
Scale uint16
Precision uint8
Scale uint8
MaxLen int
MaxCharLen int
MaxNoOfArrayElements int
Expand Down Expand Up @@ -116,8 +116,9 @@ func (par *ParameterInfo) read(session *network.Session) error {
if err != nil {
return err
}
precision, err := session.GetInt(1, false, false)
var scale int
par.Precision, err = session.GetByte()
//precision, err := session.GetInt(1, false, false)
//var scale int
switch par.DataType {
case NUMBER:
fallthrough
Expand All @@ -136,20 +137,29 @@ func (par *ParameterInfo) read(session *network.Session) error {
case TimeStampLTZ_DTY:
fallthrough
case TimeStampeLTZ:
scale, err = session.GetInt(2, true, true)
if scale, err := session.GetInt(2, true, true); err != nil {
return err
} else {
if scale == -127 {
par.Precision = uint8(math.Ceil(float64(par.Precision) * 0.30103))
par.Scale = 0xFF
} else {
par.Scale = uint8(scale)
}
}
default:
scale, err = session.GetInt(1, false, false)
par.Scale, err = session.GetByte()
//scale, err = session.GetInt(1, false, false)
}
if scale == -127 {
precision = int(math.Ceil(float64(precision) * 0.30103))
scale = 0xFF
//if par.Scale == uint8(-127) {
//
//}
if par.DataType == NUMBER && par.Precision == 0 && (par.Scale == 0 || par.Scale == 0xFF) {
par.Precision = 38
par.Scale = 0
}
if par.DataType == NUMBER && precision == 0 && (scale == 0 || scale == 0xFF) {
precision = 38
scale = 0xFF
}
par.Scale = uint16(scale)
par.Precision = uint16(precision)
//par.Scale = uint16(scale)
//par.Precision = uint16(precision)
par.MaxLen, err = session.GetInt(4, true, true)
if err != nil {
return err
Expand Down Expand Up @@ -233,22 +243,25 @@ func (par *ParameterInfo) read(session *network.Session) error {
return nil
}
func (par *ParameterInfo) write(session *network.Session) error {
session.PutUint(int(par.DataType), 1, false, false)
session.PutUint(par.Flag, 1, false, false)
session.PutUint(par.Precision, 1, false, false)
session.PutUint(par.Scale, 1, false, false)
session.PutBytes(uint8(par.DataType), par.Flag, par.Precision, par.Scale)
//session.PutUint(int(par.DataType), 1, false, false)
//session.PutUint(par.Flag, 1, false, false)
//session.PutUint(par.Precision, 1, false, false)
//session.PutUint(par.Scale, 1, false, false)
session.PutUint(par.MaxLen, 4, true, true)
session.PutInt(par.MaxNoOfArrayElements, 4, true, true)
session.PutInt(par.ContFlag, 4, true, true)
if par.ToID == nil {
session.PutInt(0, 1, false, false)
session.PutBytes(0)
//session.PutInt(0, 1, false, false)
} else {
session.PutInt(len(par.ToID), 4, true, true)
session.PutClr(par.ToID)
}
session.PutUint(par.Version, 2, true, true)
session.PutUint(par.CharsetID, 2, true, true)
session.PutUint(par.CharsetForm, 1, false, false)
session.PutBytes(uint8(par.CharsetForm))
//session.PutUint(par.CharsetForm, 1, false, false)
session.PutUint(par.MaxCharLen, 4, true, true)
return nil
}
Expand Down
Loading

0 comments on commit 42e545b

Please sign in to comment.