From 82c1dfa2392e7524c6cf7f7c57fecbd6f9119159 Mon Sep 17 00:00:00 2001 From: Timur Shaidullin Date: Tue, 28 Nov 2017 18:12:08 +0300 Subject: [PATCH] [#6812] Added GRANT statement and its implementation. [#6812] Revoke statement was added and its implementation. [#6839] Enhancement and refactoring --- jOOQ/src/main/java/org/jooq/Clause.java | 7 + jOOQ/src/main/java/org/jooq/DSLContext.java | 28 +++ .../main/java/org/jooq/GrantFirstStep.java | 49 ++++++ jOOQ/src/main/java/org/jooq/GrantStepOn.java | 47 +++++ jOOQ/src/main/java/org/jooq/GrantStepTo.java | 47 +++++ jOOQ/src/main/java/org/jooq/Privilege.java | 41 +++++ .../main/java/org/jooq/RevokeFirstStep.java | 49 ++++++ .../main/java/org/jooq/RevokeStepFrom.java | 47 +++++ jOOQ/src/main/java/org/jooq/RevokeStepOn.java | 47 +++++ jOOQ/src/main/java/org/jooq/impl/DSL.java | 115 +++++++++++++ .../java/org/jooq/impl/DefaultDSLContext.java | 27 +++ .../main/java/org/jooq/impl/GrantImpl.java | 160 ++++++++++++++++++ .../src/main/java/org/jooq/impl/Keywords.java | 2 + .../java/org/jooq/impl/PrivilegeImpl.java | 73 ++++++++ .../main/java/org/jooq/impl/RevokeImpl.java | 159 +++++++++++++++++ jOOQ/src/main/java/org/jooq/impl/Tools.java | 2 + 16 files changed, 900 insertions(+) create mode 100644 jOOQ/src/main/java/org/jooq/GrantFirstStep.java create mode 100644 jOOQ/src/main/java/org/jooq/GrantStepOn.java create mode 100644 jOOQ/src/main/java/org/jooq/GrantStepTo.java create mode 100644 jOOQ/src/main/java/org/jooq/Privilege.java create mode 100644 jOOQ/src/main/java/org/jooq/RevokeFirstStep.java create mode 100644 jOOQ/src/main/java/org/jooq/RevokeStepFrom.java create mode 100644 jOOQ/src/main/java/org/jooq/RevokeStepOn.java create mode 100644 jOOQ/src/main/java/org/jooq/impl/GrantImpl.java create mode 100644 jOOQ/src/main/java/org/jooq/impl/PrivilegeImpl.java create mode 100644 jOOQ/src/main/java/org/jooq/impl/RevokeImpl.java diff --git a/jOOQ/src/main/java/org/jooq/Clause.java b/jOOQ/src/main/java/org/jooq/Clause.java index 0a8f511ffe..3229b58595 100644 --- a/jOOQ/src/main/java/org/jooq/Clause.java +++ b/jOOQ/src/main/java/org/jooq/Clause.java @@ -48,6 +48,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 diff --git a/jOOQ/src/main/java/org/jooq/DSLContext.java b/jOOQ/src/main/java/org/jooq/DSLContext.java index 8463b1c587..5f484e34b4 100644 --- a/jOOQ/src/main/java/org/jooq/DSLContext.java +++ b/jOOQ/src/main/java/org/jooq/DSLContext.java @@ -10837,4 +10837,32 @@ public interface DSLContext extends Scope , AutoCloseable { */ @Support , 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 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 privileges); } diff --git a/jOOQ/src/main/java/org/jooq/GrantFirstStep.java b/jOOQ/src/main/java/org/jooq/GrantFirstStep.java new file mode 100644 index 0000000000..d703326512 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/GrantFirstStep.java @@ -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 privileges); +} diff --git a/jOOQ/src/main/java/org/jooq/GrantStepOn.java b/jOOQ/src/main/java/org/jooq/GrantStepOn.java new file mode 100644 index 0000000000..52a5413004 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/GrantStepOn.java @@ -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); +} diff --git a/jOOQ/src/main/java/org/jooq/GrantStepTo.java b/jOOQ/src/main/java/org/jooq/GrantStepTo.java new file mode 100644 index 0000000000..9428d4f4e8 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/GrantStepTo.java @@ -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); +} diff --git a/jOOQ/src/main/java/org/jooq/Privilege.java b/jOOQ/src/main/java/org/jooq/Privilege.java new file mode 100644 index 0000000000..636592c76d --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/Privilege.java @@ -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 { +} diff --git a/jOOQ/src/main/java/org/jooq/RevokeFirstStep.java b/jOOQ/src/main/java/org/jooq/RevokeFirstStep.java new file mode 100644 index 0000000000..daf14d1c17 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/RevokeFirstStep.java @@ -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 privileges); +} diff --git a/jOOQ/src/main/java/org/jooq/RevokeStepFrom.java b/jOOQ/src/main/java/org/jooq/RevokeStepFrom.java new file mode 100644 index 0000000000..501e4c249c --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/RevokeStepFrom.java @@ -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); +} diff --git a/jOOQ/src/main/java/org/jooq/RevokeStepOn.java b/jOOQ/src/main/java/org/jooq/RevokeStepOn.java new file mode 100644 index 0000000000..9af12a9743 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/RevokeStepOn.java @@ -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); +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 634a06c428..81c087700f 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -158,6 +158,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; @@ -220,6 +221,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; @@ -251,6 +253,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; @@ -7858,6 +7861,118 @@ public class DSL { // XXX Access control // ------------------------------------------------------------------------- + /** + * Grant a privilege on table to user or role. + * + *

+ * Example:

+     * import static org.jooq.impl.DSL.*;
+     *
+     * grant(privilege)
+     *   .on(table)
+     *   .to(user)
+     *
+     * grant(privilege)
+     *   .on(table)
+     *   .to(role)
+     * 
+ * + * + * @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. + * + *

+ * Example:

+     * import static org.jooq.impl.DSL.*;
+     *
+     * grant(privileges)
+     *   .on(table)
+     *   .to(user)
+     *
+     * grant(privileges)
+     *   .on(table)
+     *   .to(role)
+     * 
+ *

+ * + * @see #grant(String) + */ + @Support + public static GrantStepOn grant(Collection privileges) { + return using(new DefaultConfiguration()).grant(privileges); + } + + /** + * Revoke a privilege on table from user or role. + * + *

+ * Example:

+     * import static org.jooq.impl.DSL.*;
+     *
+     * revoke(privilege)
+     *   .on(table)
+     *   .from(user)
+     *
+     * revoke(privilege)
+     *   .on(table)
+     *   .from(role)
+     * 
+ *

+ * + * @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. + * + *

+ * Example:

+     * import static org.jooq.impl.DSL.*;
+     *
+     * revoke(privileges)
+     *   .on(table)
+     *   .from(user)
+     *
+     * revoke(privileges)
+     *   .on(table)
+     *   .from(role)
+     * 
+ *

+ * + * @see #revoke(String) + */ + @Support + public static RevokeStepOn revoke(Collection 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. * diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java index 3ecf2229bf..2e4b7eddf3 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java @@ -121,6 +121,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; @@ -175,6 +176,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; @@ -206,6 +208,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; @@ -4195,4 +4198,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 privileges) { + return new GrantImpl(configuration()).grant(privileges); + } + + @Override + public RevokeStepOn revoke(Privilege privilege) { + return new RevokeImpl(configuration()).revoke(privilege); + } + + @Override + public RevokeStepOn revoke(Collection privileges) { + return new RevokeImpl(configuration()).revoke(privileges); + } } diff --git a/jOOQ/src/main/java/org/jooq/impl/GrantImpl.java b/jOOQ/src/main/java/org/jooq/impl/GrantImpl.java new file mode 100644 index 0000000000..1ea72d8f60 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/GrantImpl.java @@ -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 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 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; + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/Keywords.java b/jOOQ/src/main/java/org/jooq/impl/Keywords.java index 6abba242a8..bcd11e8cc8 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Keywords.java +++ b/jOOQ/src/main/java/org/jooq/impl/Keywords.java @@ -133,6 +133,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"); @@ -217,6 +218,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"); diff --git a/jOOQ/src/main/java/org/jooq/impl/PrivilegeImpl.java b/jOOQ/src/main/java/org/jooq/impl/PrivilegeImpl.java new file mode 100644 index 0000000000..7d10d7f0f9 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/PrivilegeImpl.java @@ -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; + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/RevokeImpl.java b/jOOQ/src/main/java/org/jooq/impl/RevokeImpl.java new file mode 100644 index 0000000000..966b5be006 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/RevokeImpl.java @@ -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 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 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; + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/Tools.java b/jOOQ/src/main/java/org/jooq/impl/Tools.java index a19495d433..d4c294bcca 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Tools.java +++ b/jOOQ/src/main/java/org/jooq/impl/Tools.java @@ -190,6 +190,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; @@ -253,6 +254,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 = {};