Skip to content

Commit

Permalink
Add some trace functions
Browse files Browse the repository at this point in the history
  • Loading branch information
simulot committed Nov 21, 2020
1 parent d765b92 commit ec6bb5b
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 14 deletions.
1 change: 1 addition & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ func (stmt *Stmt) read(dataSet *DataSet) error {
if err != nil {
return err
}
stmt.connection.connOption.Tracer.Printf("Summary:\n%#v", stmt.connection.session.Summary)
//fmt.Println(stmt.connection.session.Summary)
//fmt.Println(stmt.connection.session.Summary)

Expand Down
30 changes: 28 additions & 2 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"database/sql/driver"
"errors"
"fmt"
"github.com/sijms/go-ora/converters"
"github.com/sijms/go-ora/network"
"os"
"os/user"
"strconv"
"strings"

"github.com/sijms/go-ora/converters"
"github.com/sijms/go-ora/network"
"github.com/sijms/go-ora/trace"
)

type ConnectionState int
Expand Down Expand Up @@ -64,6 +66,7 @@ type Connection struct {
strConv *converters.StringConverter
NLSData NLSData
}

type oracleDriver struct {
}

Expand All @@ -76,6 +79,7 @@ func (drv *oracleDriver) Open(name string) (driver.Conn, error) {
if err != nil {
return nil, err
}

return conn, conn.Open()
}

Expand Down Expand Up @@ -155,10 +159,12 @@ func (conn *Connection) GetNLS() (*NLSData, error) {
}

func (conn *Connection) Prepare(query string) (driver.Stmt, error) {
conn.connOption.Tracer.Print("Prepare\n", query)
return NewStmt(query, conn), nil
}

func (conn *Connection) Ping(ctx context.Context) error {
conn.connOption.Tracer.Print("Ping")
conn.session.ResetBuffer()
return (&simpleObject{
session: conn.session,
Expand All @@ -168,6 +174,7 @@ func (conn *Connection) Ping(ctx context.Context) error {
}

func (conn *Connection) Logoff() error {
conn.connOption.Tracer.Print("Logoff")
session := conn.session
session.ResetBuffer()
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,
Expand Down Expand Up @@ -220,6 +227,9 @@ func (conn *Connection) Logoff() error {
}

func (conn *Connection) Open() error {

conn.connOption.Tracer.Print("Open :\n", conn.connOption.ConnectionData())

switch conn.conStr.DBAPrivilege {
case SYSDBA:
conn.LogonMode |= SysDba
Expand Down Expand Up @@ -287,10 +297,12 @@ func (conn *Connection) Open() error {
if err != nil {
return err
}

return nil
}

func (conn *Connection) Begin() (driver.Tx, error) {
conn.connOption.Tracer.Print("Begin transaction")
conn.autoCommit = false
return &Transaction{conn: conn}, nil
}
Expand Down Expand Up @@ -340,6 +352,16 @@ func NewConnection(databaseUrl string) (*Connection, error) {
},
//InAddrAny: false,
}

if len(conStr.Trace) > 0 {
tf, err := os.Create(conStr.Trace)
if err != nil {
return nil, fmt.Errorf("Can't open trace file: %w", err)
}
connOption.Tracer = trace.NewTraceWriter(tf)
} else {
connOption.Tracer = trace.NilTracer()
}
return &Connection{
State: Closed,
conStr: conStr,
Expand All @@ -349,16 +371,20 @@ func NewConnection(databaseUrl string) (*Connection, error) {
}

func (conn *Connection) Close() (err error) {
conn.connOption.Tracer.Print("Close")
//var err error = nil
if conn.session != nil {
//err = conn.Logoff()
conn.session.Disconnect()
conn.session = nil
}
conn.connOption.Tracer.Print("Connection Closed")
conn.connOption.Tracer.Close()
return
}

func (conn *Connection) doAuth() error {
conn.connOption.Tracer.Print("doAuth")
conn.session.ResetBuffer()
conn.session.PutBytes(3, 118, 0, 1)
conn.session.PutUint(len(conn.conStr.UserID), 4, true, true)
Expand Down
5 changes: 4 additions & 1 deletion connection_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type ConnectionString struct {
PoolReglator int
ConnectionPoolTimeout int
PasswordlessConString string
Trace string // Trace file
}

func NewConnectionString() *ConnectionString {
Expand Down Expand Up @@ -149,7 +150,7 @@ func newConnectionStringFromUrl(databaseUrl string) (*ConnectionString, error) {
if len(ret.UserID) == 0 {
return nil, errors.New("empty user name")
}
if len(ret.Password) == 0{
if len(ret.Password) == 0 {
return nil, errors.New("empty password")
}
if len(ret.Host) == 0 {
Expand Down Expand Up @@ -250,6 +251,8 @@ func newConnectionStringFromUrl(databaseUrl string) (*ConnectionString, error) {
// conStr.Password = strings.Trim(val, "\"")
case "PROXY PASSWORD":
ret.ProxyPassword = val[0]
case "TRACE FILE":
ret.Trace = val[0]
}
}
}
Expand Down
15 changes: 8 additions & 7 deletions network/connect_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package network

import (
"strconv"

"github.com/sijms/go-ora/trace"
)

type ClientData struct {
ProgramPath string
ProgramName string
UserName string
HostName string
DriverName string
PID int
UserName string
HostName string
DriverName string
PID int
}
type ConnectionOption struct {
Port int
Expand All @@ -29,10 +31,10 @@ type ConnectionOption struct {
ServiceName string
InstanceName string
DomainName string
DBName string
DBName string
ClientData ClientData
//InAddrAny bool

Tracer trace.Tracer
}

func (op *ConnectionOption) ConnectionData() string {
Expand Down Expand Up @@ -64,4 +66,3 @@ func (op *ConnectionOption) ConnectionData() string {
// SessionDataUnitSize: 0xFFFF,
// }
//}

11 changes: 7 additions & 4 deletions network/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"encoding/binary"
"errors"
"fmt"
"github.com/sijms/go-ora/converters"
"log"
"net"
"strings"

"github.com/sijms/go-ora/converters"
)

type Data interface {
Expand Down Expand Up @@ -49,6 +50,7 @@ func NewSession(connOption ConnectionOption) *Session {

func (session *Session) Connect() error {
session.Disconnect()
session.connOption.Tracer.Print("Connect")
var err error
var host string
if !strings.Contains(session.connOption.Host, ":") {
Expand Down Expand Up @@ -188,7 +190,7 @@ func (session *Session) read(numBytes int) ([]byte, error) {
func (session *Session) writePacket(pck PacketInterface) error {
session.sendPcks = append(session.sendPcks, pck)
tmp := pck.bytes()
//log.Printf("Request: %#v\n\n", tmp)
session.connOption.Tracer.LogPacket("Write packet:", tmp)
_, err := session.conn.Write(tmp)
if err != nil {
return err
Expand Down Expand Up @@ -251,8 +253,9 @@ func (session *Session) readPacket() (PacketInterface, error) {
}
continue
}

return append(head, body...), nil
ret := append(head, body...)
session.connOption.Tracer.LogPacket("Read packet:", ret)
return ret, nil
}

}
Expand Down
60 changes: 60 additions & 0 deletions trace/trace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package trace

import (
"encoding/hex"
"fmt"
"io"
"time"
)

type Tracer interface {
Close() error
Print(vs ...interface{})
Printf(f string, s ...interface{})
LogPacket(s string, p []byte)
}

type traceWriter struct {
w io.WriteCloser
}

func NewTraceWriter(w io.WriteCloser) *traceWriter {
return &traceWriter{w}
}

func (t *traceWriter) Close() (err error) {
if t.w != nil {
err = t.w.Close()
}
return
}
func (t traceWriter) Print(vs ...interface{}) {
if t.w != nil {
t.w.Write([]byte(fmt.Sprintf("%s: ", time.Now().Format("2006-01-02T15:04:05.0000"))))
for _, v := range vs {
t.w.Write([]byte(fmt.Sprintf("%v", v)))
}
t.w.Write([]byte{'\n'})
}
}

func (t traceWriter) Printf(f string, s ...interface{}) {
if t.w != nil {
t.Print(fmt.Sprintf(f, s...))
}
}

func (t traceWriter) LogPacket(s string, p []byte) {
if t.w != nil {
t.Print(s)
t.w.Write([]byte(hex.Dump(p)))
}
}

type nilTracer struct{}

func NilTracer() *nilTracer { return &nilTracer{} }
func (nilTracer) Close() error { return nil }
func (nilTracer) Print(vs ...interface{}) {}
func (nilTracer) Printf(f string, s ...interface{}) {}
func (nilTracer) LogPacket(s string, p []byte) {}

0 comments on commit ec6bb5b

Please sign in to comment.