Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
Add ROLE support.
Browse files Browse the repository at this point in the history
  • Loading branch information
rowland committed Feb 4, 2015
1 parent b0803ab commit 905334b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
8 changes: 4 additions & 4 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (fc *firebirdsqlConn) Query(query string, args []driver.Value) (rows driver
}

func newFirebirdsqlConn(dsn string) (fc *firebirdsqlConn, err error) {
addr, dbName, user, password, err := parseDSN(dsn)
addr, dbName, user, password, role, err := parseDSN(dsn)
wp, err := newWireProtocol(addr)
if err != nil {
return
Expand All @@ -96,7 +96,7 @@ func newFirebirdsqlConn(dsn string) (fc *firebirdsqlConn, err error) {
if err != nil {
return
}
wp.opAttach(dbName, user, password)
wp.opAttach(dbName, user, password, role)
wp.dbHandle, _, _, err = wp.opResponse()
if err != nil {
return
Expand All @@ -118,7 +118,7 @@ func newFirebirdsqlConn(dsn string) (fc *firebirdsqlConn, err error) {

func createFirebirdsqlConn(dsn string) (fc *firebirdsqlConn, err error) {
// Create Database
addr, dbName, user, password, err := parseDSN(dsn)
addr, dbName, user, password, role, err := parseDSN(dsn)
wp, err := newWireProtocol(addr)
if err != nil {
return
Expand All @@ -131,7 +131,7 @@ func createFirebirdsqlConn(dsn string) (fc *firebirdsqlConn, err error) {
if err != nil {
return
}
wp.opCreate(dbName, user, password)
wp.opCreate(dbName, user, password, role)
wp.dbHandle, _, _, err = wp.opResponse()

fc = new(firebirdsqlConn)
Expand Down
39 changes: 39 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,45 @@ func TestError(t *testing.T) {
}
}

func TestRole(t *testing.T) {
conn1, err := sql.Open("firebirdsql_createdb", "sysdba:masterkey@localhost:3050/tmp/go_test_role.fdb")
if err != nil {
t.Fatalf("Error creating: %v", err)
}
conn1.Exec("CREATE TABLE test_role (f1 integer)")
conn1.Exec("INSERT INTO test_role (f1) values (1)")
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
conn1.Exec("CREATE ROLE DRIVERROLE")
if err != nil {
t.Fatalf("Error creating role: %v", err)
}
conn1.Exec("GRANT DRIVERROLE TO DRIVERTEST")
if err != nil {
t.Fatalf("Error creating role: %v", err)
}
conn1.Exec("GRANT SELECT ON test_role TO DRIVERROLE")
if err != nil {
t.Fatalf("Error granting right to role: %v", err)
}
conn1.Close()

conn2, err := sql.Open("firebirdsql", "drivertest:driverpw:driverrole@localhost:3050/tmp/go_test_role.fdb")
if err != nil {
t.Fatalf("Error connecting: %v", err)
}

rows, err := conn2.Query("SELECT f1 FROM test_role")
defer conn2.Close()
if err != nil {
t.Fatalf("Error Query: %v", err)
}

for rows.Next() {
}
}

/*
func TestFB3(t *testing.T) {
conn, err := sql.Open("firebirdsql_createdb", "sysdba:masterkey@localhost:3050/tmp/go_test_fb3.fdb")
Expand Down
3 changes: 2 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,10 @@ func split1(src string, delm string) (string, string) {
return src, ""
}

func parseDSN(dsn string) (addr string, dbName string, user string, passwd string, err error) {
func parseDSN(dsn string) (addr string, dbName string, user string, passwd string, role string, err error) {
s1, s2 := split1(dsn, "@")
user, passwd = split1(s1, ":")
passwd, role = split1(passwd, ":")
addr, dbName = split1(s2, "/")
if !strings.ContainsRune(addr, ':') {
addr += ":3050"
Expand Down
14 changes: 8 additions & 6 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ func TestDSNParse(t *testing.T) {
dbName string
user string
passwd string
role string
}{
{"user:password@localhost:3000/dbname", "localhost:3000", "dbname", "user", "password"},
{"user:password@localhost/dbname", "localhost:3050", "dbname", "user", "password"},
{"user:password@localhost/dir/dbname", "localhost:3050", "/dir/dbname", "user", "password"},
{"user:password@localhost/c:\\fbdata\\database.fdb", "localhost:3050", "c:\\fbdata\\database.fdb", "user", "password"},
{"user:password@localhost:3000/dbname", "localhost:3000", "dbname", "user", "password", ""},
{"user:password@localhost/dbname", "localhost:3050", "dbname", "user", "password", ""},
{"user:password@localhost/dir/dbname", "localhost:3050", "/dir/dbname", "user", "password", ""},
{"user:password@localhost/c:\\fbdata\\database.fdb", "localhost:3050", "c:\\fbdata\\database.fdb", "user", "password", ""},
{"user:password:role@localhost/dbname", "localhost:3050", "dbname", "user", "password", "role"},
}

for _, d := range testDSNs {
addr, dbName, user, passwd, err := parseDSN(d.dsn)
if addr != d.addr || dbName != d.dbName || user != d.user || passwd != d.passwd {
addr, dbName, user, passwd, role, err := parseDSN(d.dsn)
if addr != d.addr || dbName != d.dbName || user != d.user || passwd != d.passwd || role != d.role {
err = errors.New("parse DSN fail")
}
if err != nil {
Expand Down
9 changes: 6 additions & 3 deletions wireprotocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,20 +474,22 @@ func (p *wireProtocol) opConnect(dbName string, user string, password string, cl
p.sendPackets()
}

func (p *wireProtocol) opCreate(dbName string, user string, password string) {
func (p *wireProtocol) opCreate(dbName string, user string, password string, role string) {
debugPrint(p, "opCreate")
var page_size int32
page_size = 4096

encode := bytes.NewBufferString("UTF8").Bytes()
userBytes := bytes.NewBufferString(strings.ToUpper(user)).Bytes()
passwordBytes := bytes.NewBufferString(password).Bytes()
roleBytes := []byte(role)
dpb := bytes.Join([][]byte{
[]byte{1},
[]byte{68, byte(len(encode))}, encode,
[]byte{48, byte(len(encode))}, encode,
[]byte{28, byte(len(userBytes))}, userBytes,
[]byte{29, byte(len(passwordBytes))}, passwordBytes,
[]byte{60, byte(len(roleBytes))}, roleBytes,
[]byte{63, 4}, int32_to_bytes(3),
[]byte{24, 4}, bint32_to_bytes(1),
[]byte{54, 4}, bint32_to_bytes(1),
Expand Down Expand Up @@ -606,17 +608,18 @@ func (p *wireProtocol) opAccept(user string, password string, clientPublic *big.
return
}

func (p *wireProtocol) opAttach(dbName string, user string, password string) {
func (p *wireProtocol) opAttach(dbName string, user string, password string, role string) {
debugPrint(p, "opAttach")
encode := bytes.NewBufferString("UTF8").Bytes()
userBytes := bytes.NewBufferString(strings.ToUpper(user)).Bytes()
passwordBytes := bytes.NewBufferString(password).Bytes()

roleBytes := []byte(role)
dbp := bytes.Join([][]byte{
[]byte{1},
[]byte{48, byte(len(encode))}, encode,
[]byte{28, byte(len(userBytes))}, userBytes,
[]byte{29, byte(len(passwordBytes))}, passwordBytes,
[]byte{60, byte(len(roleBytes))}, roleBytes,
}, nil)
p.packInt(op_attach)
p.packInt(0) // Database Object ID
Expand Down

0 comments on commit 905334b

Please sign in to comment.