Skip to content

Commit

Permalink
add new table user_privileges
Browse files Browse the repository at this point in the history
  • Loading branch information
yangliang9004 committed May 8, 2017
1 parent aafd5e9 commit 1b142cb
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pkg/sql/information_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var informationSchema = virtualSchema{
informationSchemaTablePrivileges,
informationSchemaTablesTable,
informationSchemaViewsTable,
informationSchemaUserPrivileges,
},
}

Expand Down Expand Up @@ -416,6 +417,46 @@ CREATE TABLE information_schema.table_constraints (
},
}

var informationSchemaUserPrivileges = virtualSchemaTable{
schema: `
CREATE TABLE information_schema.user_privileges (
GRANTEE STRING NOT NULL DEFAULT '',
TABLE_CATALOG STRING NOT NULL DEFAULT '',
PRIVILEGE_TYPE STRING NOT NULL DEFAULT '',
IS_GRANTABLE BOOL NOT NULL DEFAULT FALSE
);`,
populate: func(ctx context.Context, p *planner, addRow func(...parser.Datum) error) error {
type Drow struct {
grantee string
privilege string
}
m := make(map[Drow]struct{})
descs, err := getAllDescriptors(ctx, p.txn)
if err != nil {
return nil
}
for _, desc := range descs {
for _, u := range desc.GetPrivileges().Show() {
for _, p := range u.Privileges {
r := Drow{u.User, p}
m[r] = struct{}{}
}
}
}
for u := range m {
if err := addRow(
parser.NewDString(u.grantee), // grantee
defString, // table_catalog
parser.NewDString(u.privilege), // privilege_type
parser.DNull, // is_grantable
); err != nil {
return err
}
}
return nil
},
}

var informationSchemaTablePrivileges = virtualSchemaTable{
schema: `
CREATE TABLE information_schema.table_privileges (
Expand Down
61 changes: 61 additions & 0 deletions pkg/sql/testdata/logic_test/information_schema
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ statistics
table_constraints
table_privileges
tables
user_privileges
views

query TT colnames
Expand Down Expand Up @@ -224,6 +225,7 @@ statistics
table_constraints
table_privileges
tables
user_privileges
views
abc
xyz
Expand Down Expand Up @@ -269,6 +271,7 @@ zones
xyz
views
users
user_privileges
ui
tables
tables
Expand Down Expand Up @@ -326,6 +329,7 @@ def information_schema statistics SYSTEM VIEW 1
def information_schema table_constraints SYSTEM VIEW 1
def information_schema table_privileges SYSTEM VIEW 1
def information_schema tables SYSTEM VIEW 1
def information_schema user_privileges SYSTEM VIEW 1
def information_schema views SYSTEM VIEW 1
def other_db abc VIEW 1
def other_db xyz BASE TABLE 2
Expand Down Expand Up @@ -388,6 +392,7 @@ def information_schema statistics SYSTEM VIEW 1
def information_schema table_constraints SYSTEM VIEW 1
def information_schema table_privileges SYSTEM VIEW 1
def information_schema tables SYSTEM VIEW 1
def information_schema user_privileges SYSTEM VIEW 1
def information_schema views SYSTEM VIEW 1
def pg_catalog pg_am SYSTEM VIEW 1
def pg_catalog pg_attrdef SYSTEM VIEW 1
Expand Down Expand Up @@ -436,6 +441,7 @@ def information_schema statistics SYSTEM VIEW 1
def information_schema table_constraints SYSTEM VIEW 1
def information_schema table_privileges SYSTEM VIEW 1
def information_schema tables SYSTEM VIEW 1
def information_schema user_privileges SYSTEM VIEW 1
def information_schema views SYSTEM VIEW 1
def other_db xyz BASE TABLE 6
def pg_catalog pg_am SYSTEM VIEW 1
Expand Down Expand Up @@ -869,3 +875,58 @@ NULL NULL NULL NULL NU

statement ok
DROP DATABASE other_db


#Verify information_schema.user_privileges

query TTTT colnames
SELECT * FROM information_schema.user_privileges ORDER BY privilege_type
----
grantee table_catalog privilege_type is_grantable
root def ALL NULL
root def DELETE NULL
root def GRANT NULL
root def INSERT NULL
root def SELECT NULL
root def UPDATE NULL


statement ok
CREATE DATABASE sbtest

statement ok
CREATE TABLE sbtest.sbtest (a INT)

statement ok
CREATE USER tester1 WITH PASSWORD '123456'

statement ok
CREATE USER tester2 WITH PASSWORD '123456'

statement ok
GRANT INSERT, DELETE ON TABLE sbtest.sbtest TO tester1

statement ok
GRANT SELECT, UPDATE ON DATABASE sbtest TO tester2

query TTTT colnames
SELECT * FROM information_schema.user_privileges ORDER BY grantee,privilege_type
----
grantee table_catalog privilege_type is_grantable
root def ALL NULL
root def DELETE NULL
root def GRANT NULL
root def INSERT NULL
root def SELECT NULL
root def UPDATE NULL
tester1 def DELETE NULL
tester1 def INSERT NULL
tester2 def SELECT NULL
tester2 def UPDATE NULL



statement ok
DROP DATABASE sbtest


0 comments on commit 1b142cb

Please sign in to comment.