Merge pull request #6878 from timur-sh/6812

[#6812] Added GRANT statement and its implementation.
This commit is contained in:
Lukas Eder 2017-11-30 15:16:24 +01:00 committed by GitHub
commit c5e25c1912
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 900 additions and 0 deletions

View File

@ -51,6 +51,13 @@ public enum Clause {
USER,
ROLE,
PRIVILEGE,
GRANT,
GRANT_PRIVILEGE,
REVOKE,
REVOKE_PRIVILEGE,
// -------------------------------------------------------------------------
// Clauses used in a any type of statement to model constraint references

View File

@ -10840,4 +10840,32 @@ public interface DSLContext extends Scope , AutoCloseable {
*/
@Support
<R extends TableRecord<R>, T> int executeDelete(R record, Condition condition) throws DataAccessException;
// -------------------------------------------------------------------------
// XXX Access control
// -------------------------------------------------------------------------
/**
* Grant privilege on a table to user or role.
*/
@Support
GrantStepOn grant(Privilege privilege);
/**
* Grant privileges on a table to user or role.
*/
@Support
GrantStepOn grant(Collection<? extends Privilege> privileges);
/**
* Revoke a privilege on table from user or role.
*/
@Support
RevokeStepOn revoke(Privilege privilege);
/**
* Revoke privileges on table from user or role.
*/
@Support
RevokeStepOn revoke(Collection<? extends Privilege> privileges);
}

View File

@ -0,0 +1,49 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq;
import java.util.Collection;
/**
* The preparation a privilege.
*
* @author Timur Shaidullin
*/
public interface GrantFirstStep extends GrantStepOn {
GrantStepOn grant(Privilege privilege);
GrantStepOn grant(Collection<? extends Privilege> privileges);
}

View File

@ -0,0 +1,47 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq;
/**
* The preparation a target of privilege.
*
* @author Timur Shaidullin
*/
public interface GrantStepOn extends GrantStepTo {
GrantStepTo on(Table<?> table);
GrantStepTo on(String table);
}

View File

@ -0,0 +1,47 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq;
/**
* The preparation either a user or a role.
*
* @author Timur Shaidullin
*/
public interface GrantStepTo extends Query {
Query to(User user);
Query to(Role role);
}

View File

@ -0,0 +1,41 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq;
/**
* @author Timur Shaidullin
*/
public interface Privilege extends QueryPart {
}

View File

@ -0,0 +1,49 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq;
import java.util.Collection;
/**
* The preparation a privilege.
*
* @author Timur Shaidullin
*/
public interface RevokeFirstStep extends RevokeStepOn {
RevokeStepOn revoke(Privilege privilege);
RevokeStepOn revoke(Collection<? extends Privilege> privileges);
}

View File

@ -0,0 +1,47 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq;
/**
* The preparation either a user or a role.
*
* @author Timur Shaidullin
*/
public interface RevokeStepFrom extends Query {
Query from(User user);
Query from(Role role);
}

View File

@ -0,0 +1,47 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq;
/**
* The preparation a target of privilege.
*
* @author Timur Shaidullin
*/
public interface RevokeStepOn extends RevokeStepFrom {
RevokeStepFrom on(Table<?> table);
RevokeStepFrom on(String table);
}

View File

@ -161,6 +161,7 @@ import org.jooq.DropViewFinalStep;
import org.jooq.False;
import org.jooq.Field;
import org.jooq.FieldOrRow;
import org.jooq.GrantStepOn;
import org.jooq.GroupConcatOrderByStep;
import org.jooq.GroupField;
import org.jooq.Index;
@ -223,6 +224,7 @@ import org.jooq.OrderedAggregateFunction;
import org.jooq.OrderedAggregateFunctionOfDeferredType;
import org.jooq.Param;
import org.jooq.PlainSQL;
import org.jooq.Privilege;
import org.jooq.QuantifiedSelect;
import org.jooq.Queries;
import org.jooq.Query;
@ -254,6 +256,7 @@ import org.jooq.RecordHandler;
import org.jooq.RecordType;
import org.jooq.Result;
import org.jooq.ResultQuery;
import org.jooq.RevokeStepOn;
import org.jooq.Role;
import org.jooq.Row1;
import org.jooq.Row10;
@ -7861,6 +7864,118 @@ public class DSL {
// XXX Access control
// -------------------------------------------------------------------------
/**
* Grant a privilege on table to user or role.
*
* <p>
* Example: <code><pre>
* import static org.jooq.impl.DSL.*;
*
* grant(privilege)
* .on(table)
* .to(user)
*
* grant(privilege)
* .on(table)
* .to(role)
* </pre></code>
*
*
* @see #grant(Collection)
*/
@Support
public static GrantStepOn grant(String privilege) {
return using(new DefaultConfiguration()).grant(privilege(privilege));
}
/**
* Grant a privilege on table to user or role.
*
* <p>
* Example: <code><pre>
* import static org.jooq.impl.DSL.*;
*
* grant(privileges)
* .on(table)
* .to(user)
*
* grant(privileges)
* .on(table)
* .to(role)
* </pre></code>
* <p>
*
* @see #grant(String)
*/
@Support
public static GrantStepOn grant(Collection<? extends Privilege> privileges) {
return using(new DefaultConfiguration()).grant(privileges);
}
/**
* Revoke a privilege on table from user or role.
*
* <p>
* Example: <code><pre>
* import static org.jooq.impl.DSL.*;
*
* revoke(privilege)
* .on(table)
* .from(user)
*
* revoke(privilege)
* .on(table)
* .from(role)
* </pre></code>
* <p>
*
* @see #revoke(Collection)
*/
@Support
public static RevokeStepOn revoke(String privilege) {
return using(new DefaultConfiguration()).revoke(privilege(privilege));
}
/**
* Revoke a privilege on table from user or role.
*
* <p>
* Example: <code><pre>
* import static org.jooq.impl.DSL.*;
*
* revoke(privileges)
* .on(table)
* .from(user)
*
* revoke(privileges)
* .on(table)
* .from(role)
* </pre></code>
* <p>
*
* @see #revoke(String)
*/
@Support
public static RevokeStepOn revoke(Collection<? extends Privilege> privileges) {
return using(new DefaultConfiguration()).revoke(privileges);
}
/**
* Create a new privilege reference.
*
* @see #privilege(Keyword)
*/
public static Privilege privilege(String privilege) {
return privilege(keyword(privilege));
}
/**
* Create a new privilege reference.
*/
public static Privilege privilege(Keyword privilege) {
return new PrivilegeImpl(privilege);
}
/**
* Create a new user reference.
*

View File

@ -124,6 +124,7 @@ import org.jooq.ExecuteContext;
import org.jooq.ExecuteListener;
import org.jooq.Explain;
import org.jooq.Field;
import org.jooq.GrantStepOn;
import org.jooq.Index;
import org.jooq.InsertQuery;
import org.jooq.InsertSetStep;
@ -178,6 +179,7 @@ import org.jooq.MergeUsingStep;
import org.jooq.Meta;
import org.jooq.Name;
import org.jooq.Param;
import org.jooq.Privilege;
import org.jooq.Parser;
import org.jooq.Queries;
import org.jooq.Query;
@ -209,6 +211,7 @@ import org.jooq.RenderContext;
import org.jooq.Result;
import org.jooq.ResultQuery;
import org.jooq.Results;
import org.jooq.RevokeStepOn;
import org.jooq.SQL;
import org.jooq.SQLDialect;
import org.jooq.Schema;
@ -4198,4 +4201,28 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
public String toString() {
return configuration().toString();
}
// -------------------------------------------------------------------------
// XXX Access control
// -------------------------------------------------------------------------
@Override
public GrantStepOn grant(Privilege privilege) {
return new GrantImpl(configuration()).grant(privilege);
}
@Override
public GrantStepOn grant(Collection<? extends Privilege> privileges) {
return new GrantImpl(configuration()).grant(privileges);
}
@Override
public RevokeStepOn revoke(Privilege privilege) {
return new RevokeImpl(configuration()).revoke(privilege);
}
@Override
public RevokeStepOn revoke(Collection<? extends Privilege> privileges) {
return new RevokeImpl(configuration()).revoke(privileges);
}
}

View File

@ -0,0 +1,160 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import org.jooq.Clause;
import org.jooq.Configuration;
import org.jooq.Context;
import org.jooq.GrantFirstStep;
import org.jooq.GrantStepOn;
import org.jooq.GrantStepTo;
import org.jooq.Privilege;
import org.jooq.Query;
import org.jooq.Role;
import org.jooq.Table;
import org.jooq.User;
import java.util.Collection;
import java.util.Collections;
import static org.jooq.Clause.GRANT;
import static org.jooq.Clause.GRANT_PRIVILEGE;
import static org.jooq.impl.Keywords.K_GRANT;
import static org.jooq.impl.Keywords.K_ON;
import static org.jooq.impl.Keywords.K_TO;
/**
* Grant privilege or privileges on a table to user or role.
* @author Timur Shaidullin
*/
final class GrantImpl extends AbstractQuery implements
GrantFirstStep,
GrantStepOn,
GrantStepTo,
Query {
/**
* Generated UID
*/
private static final long serialVersionUID = -6509384254822040545L;
private Clause[] CLAUSE = { GRANT };
private Collection<? extends Privilege> privileges;
private Role role;
private Table<?> table;
private User user;
GrantImpl(Configuration configuration) {
super(configuration);
}
// ------------------------------------------------------------------------
// XXX: QueryPart API
// ------------------------------------------------------------------------
@Override
public void accept(Context<?> ctx) {
ctx.start(GRANT_PRIVILEGE)
.visit(K_GRANT).sql(' ');
Privilege[] arrayOfPrivileges = privileges.toArray(Tools.EMPTY_PRIVILEGE);
for (int i = 0; i < arrayOfPrivileges.length; i++) {
ctx.visit(arrayOfPrivileges[i]);
if (i != (arrayOfPrivileges.length - 1)) {
ctx.sql(',');
}
ctx.sql(' ');
}
ctx.visit(K_ON).sql(' ')
.visit(table).sql(' ')
.visit(K_TO).sql(' ');
if (user != null) {
ctx.visit(user);
} else if (role != null) {
ctx.visit(role);
}
ctx.end(GRANT_PRIVILEGE).sql(';');
}
@Override
public Clause[] clauses(Context<?> ctx) {
return CLAUSE;
}
// ------------------------------------------------------------------------
// XXX: GrantImpl API
// ------------------------------------------------------------------------
@Override
public GrantStepOn grant(Privilege privilege) {
this.privileges = Collections.singletonList(privilege);
return this;
}
@Override
public GrantStepOn grant(Collection<? extends Privilege> privileges) {
this.privileges = privileges;
return this;
}
@Override
public GrantStepTo on(Table<?> table) {
this.table = table;
return this;
}
@Override
public GrantStepTo on(String table) {
this.table = DSL.table(table);
return this;
}
@Override
public Query to(User user) {
this.user = user;
return this;
}
@Override
public Query to(Role role) {
this.role = role;
return this;
}
}

View File

@ -136,6 +136,7 @@ final class Keywords {
static final Keyword K_FROM = keyword("from");
static final Keyword K_GENERATED_BY_DEFAULT_AS_IDENTITY = keyword("generated by default as identity");
static final Keyword K_GLOBAL_TEMPORARY = keyword("global temporary");
static final Keyword K_GRANT = keyword("grant");
static final Keyword K_GROUP_BY = keyword("group by");
static final Keyword K_HAVING = keyword("having");
static final Keyword K_HOUR_TO_SECOND = keyword("hour to second");
@ -220,6 +221,7 @@ final class Keywords {
static final Keyword K_RESTART_WITH = keyword("restart with");
static final Keyword K_RESTRICT = keyword("restrict");
static final Keyword K_RETURNING = keyword("returning");
static final Keyword K_REVOKE = keyword("revoke");
static final Keyword K_ROW = keyword("row");
static final Keyword K_ROWCOUNT = keyword("rowcount");
static final Keyword K_ROWS = keyword("rows");

View File

@ -0,0 +1,73 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import org.jooq.Clause;
import org.jooq.Context;
import org.jooq.Keyword;
import org.jooq.Privilege;
import static org.jooq.Clause.PRIVILEGE;
/**
* @author Timur Shaidullin
*/
final class PrivilegeImpl extends AbstractQueryPart implements Privilege {
/**
* Generated UID
*/
private static final long serialVersionUID = -3106268610481536038L;
private static final Clause[] CLAUSES = { PRIVILEGE };
private final Keyword privilege;
PrivilegeImpl(Keyword privilege) {
this.privilege = privilege;
}
// ------------------------------------------------------------------------
// XXX: QueryPart API
// ------------------------------------------------------------------------
@Override
public void accept(Context<?> ctx) {
ctx.visit(privilege);
}
@Override
public Clause[] clauses(Context<?> ctx) {
return CLAUSES;
}
}

View File

@ -0,0 +1,159 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import org.jooq.Clause;
import org.jooq.Configuration;
import org.jooq.Context;
import org.jooq.Privilege;
import org.jooq.Query;
import org.jooq.RevokeFirstStep;
import org.jooq.RevokeStepOn;
import org.jooq.RevokeStepFrom;
import org.jooq.Role;
import org.jooq.Table;
import org.jooq.User;
import java.util.Collection;
import java.util.Collections;
import static org.jooq.Clause.REVOKE;
import static org.jooq.Clause.REVOKE_PRIVILEGE;
import static org.jooq.impl.Keywords.*;
/**
* Revoke privilege or privileges on a table from user or role.
*
* @author Timur Shaidullin
*/
final class RevokeImpl extends AbstractQuery implements
RevokeFirstStep,
RevokeStepOn,
RevokeStepFrom,
Query {
/**
* Generated UID
*/
private static final long serialVersionUID = -5777612075774539326L;
private Clause[] CLAUSE = { REVOKE };
private Collection<? extends Privilege> privileges;
private Role role;
private Table<?> table;
private User user;
RevokeImpl(Configuration configuration) {
super(configuration);
}
// ------------------------------------------------------------------------
// XXX: QueryPart API
// ------------------------------------------------------------------------
@Override
public void accept(Context<?> ctx) {
ctx.start(REVOKE_PRIVILEGE)
.visit(K_REVOKE).sql(' ');
Privilege[] arrayOfPrivileges = privileges.toArray(Tools.EMPTY_PRIVILEGE);
for (int i = 0; i < arrayOfPrivileges.length; i++) {
ctx.visit(arrayOfPrivileges[i]);
if (i != arrayOfPrivileges.length - 1) {
ctx.sql(',');
}
ctx.sql(' ');
}
ctx.visit(K_ON).sql(' ')
.visit(table).sql(' ')
.visit(K_FROM).sql(' ');
if (user != null) {
ctx.visit(user);
} else if (role != null) {
ctx.visit(role);
}
ctx.end(REVOKE_PRIVILEGE).sql(';');
}
@Override
public Clause[] clauses(Context<?> ctx) {
return CLAUSE;
}
// ------------------------------------------------------------------------
// XXX: RevokeImpl API
// ------------------------------------------------------------------------
@Override
public RevokeStepOn revoke(Privilege privilege) {
this.privileges = Collections.singletonList(privilege);
return this;
}
@Override
public RevokeStepOn revoke(Collection<? extends Privilege> privileges) {
this.privileges = privileges;
return this;
}
@Override
public RevokeStepFrom on(Table<?> table) {
this.table = table;
return this;
}
@Override
public RevokeStepFrom on(String table) {
this.table = DSL.table(table);
return this;
}
@Override
public Query from(User user) {
this.user = user;
return this;
}
@Override
public Query from(Role role) {
this.role = role;
return this;
}
}

View File

@ -193,6 +193,7 @@ import org.jooq.Field;
import org.jooq.Name;
import org.jooq.OrderField;
import org.jooq.Param;
import org.jooq.Privilege;
import org.jooq.Query;
import org.jooq.QueryPart;
import org.jooq.Record;
@ -256,6 +257,7 @@ final class Tools {
static final int[] EMPTY_INT = {};
static final Name[] EMPTY_NAME = {};
static final Param<?>[] EMPTY_PARAM = {};
static final Privilege[] EMPTY_PRIVILEGE = {};
static final Query[] EMPTY_QUERY = {};
static final QueryPart[] EMPTY_QUERYPART = {};
static final Record[] EMPTY_RECORD = {};