Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
1. SQL Binder: Support Comment statement SQL bind - [#36012](https://github.com/apache/shardingsphere/pull/36012)
1. SQL Binder: Support Prepare statement SQL bind - [#36064](https://github.com/apache/shardingsphere/pull/36064)
1. SQL Binder: Support Revoke statement SQL bind - [#36124](https://github.com/apache/shardingsphere/pull/36124)
1. SQL Binder: Support Grant statement SQL bind - [#36207](https://github.com/apache/shardingsphere/pull/36207)
1. SQL Binder: Add alter table metadata check - [#35877](https://github.com/apache/shardingsphere/pull/35877)
1. SQL Router: Add check for select with union all routing to multi data sources - [#35037](https://github.com/apache/shardingsphere/pull/35037)
1. SQL Router: Improve support for executing tableless SQL with single data source - [#35659](https://github.com/apache/shardingsphere/pull/35659)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.apache.shardingsphere.infra.binder.engine.statement.dcl;

import com.cedarsoftware.util.CaseInsensitiveMap.CaseInsensitiveString;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import org.apache.shardingsphere.infra.binder.engine.segment.dml.from.context.TableSegmentBinderContext;
import org.apache.shardingsphere.infra.binder.engine.segment.dml.from.type.SimpleTableSegmentBinder;
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinder;
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext;
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementCopyUtils;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dcl.GrantStatement;

import java.util.ArrayList;
import java.util.Collection;

/**
* Grant statement binder.
*/
public final class GrantStatementBinder implements SQLStatementBinder<GrantStatement> {

@Override
public GrantStatement bind(final GrantStatement sqlStatement, final SQLStatementBinderContext binderContext) {
Collection<SimpleTableSegment> tables = sqlStatement.getTables();
if (tables.isEmpty()) {
return sqlStatement;
}
Multimap<CaseInsensitiveString, TableSegmentBinderContext> tableBinderContexts = LinkedHashMultimap.create();
Collection<SimpleTableSegment> boundTables = new ArrayList<>();
for (SimpleTableSegment each : tables) {
boundTables.add(SimpleTableSegmentBinder.bind(each, binderContext, tableBinderContexts));
}
return copyGrantStatement(sqlStatement, boundTables);
}

private GrantStatement copyGrantStatement(final GrantStatement sqlStatement, final Collection<SimpleTableSegment> tables) {
if (tables.equals(sqlStatement.getTables())) {
return sqlStatement;
}
GrantStatement result = new GrantStatement(sqlStatement.getDatabaseType());
result.getTables().addAll(tables);
SQLStatementCopyUtils.copyAttributes(sqlStatement, result);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@

import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext;
import org.apache.shardingsphere.infra.binder.engine.statement.dcl.RevokeStatementBinder;
import org.apache.shardingsphere.infra.binder.engine.statement.dcl.GrantStatementBinder;
import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dcl.DCLStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dcl.RevokeStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dcl.GrantStatement;

/**
* DCL statement bind engine.
Expand All @@ -38,6 +40,9 @@ public DCLStatement bind(final DCLStatement statement, final SQLStatementBinderC
if (statement instanceof RevokeStatement) {
return new RevokeStatementBinder().bind((RevokeStatement) statement, binderContext);
}
if (statement instanceof GrantStatement) {
return new GrantStatementBinder().bind((GrantStatement) statement, binderContext);
}
return statement;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.apache.shardingsphere.infra.binder.engine.statement.dcl;

import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dcl.GrantStatement;
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Collection;
import java.util.Collections;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class GrantStatementBinderTest {

private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");

@Mock
private ShardingSphereMetaData metaData;

@Mock
private ShardingSphereDatabase database;

@Mock
private ShardingSphereSchema schema;

@Mock
private ShardingSphereTable table;

@Test
void assertBind() {
when(metaData.containsDatabase("foo_db")).thenReturn(true);
when(metaData.getDatabase("foo_db")).thenReturn(database);
when(database.containsSchema("foo_db")).thenReturn(true);
when(database.getSchema("foo_db")).thenReturn(schema);
when(schema.containsTable("test_table")).thenReturn(true);
when(schema.getTable("test_table")).thenReturn(table);
when(table.getAllColumns()).thenReturn(Collections.emptyList());
HintValueContext hintValueContext = new HintValueContext();
hintValueContext.setSkipMetadataValidate(true);
SimpleTableSegment tableSegment = new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("test_table")));
GrantStatement original = new GrantStatement(databaseType);
original.getTables().add(tableSegment);
SQLStatementBinderContext binderContext = new SQLStatementBinderContext(metaData, "foo_db", hintValueContext, original);
GrantStatement actual = new GrantStatementBinder().bind(original, binderContext);
Collection<SimpleTableSegment> actualTables = actual.getTables();
assertThat(actualTables.size(), is(1));
assertThat(actualTables.iterator().next().getTableName().getIdentifier().getValue(), is("test_table"));
}

@Test
void assertBindWithEmptyTables() {
GrantStatement original = new GrantStatement(databaseType);
HintValueContext hintValueContext = new HintValueContext();
hintValueContext.setSkipMetadataValidate(true);
SQLStatementBinderContext binderContext = new SQLStatementBinderContext(metaData, "foo_db", hintValueContext, original);
GrantStatement actual = new GrantStatementBinder().bind(original, binderContext);
Collection<SimpleTableSegment> actualTables = actual.getTables();
assertThat(actualTables.size(), is(0));
}
}
28 changes: 28 additions & 0 deletions test/it/binder/src/test/resources/cases/dcl/grant.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You 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.
-->

<sql-parser-test-cases>
<grant sql-case-id="grant_select_on_table">
<table name="t_order" start-index="16" stop-index="22">
<table-bound>
<original-database name="foo_db_1" />
<original-schema name="foo_db_1" />
</table-bound>
</table>
</grant>
</sql-parser-test-cases>
21 changes: 21 additions & 0 deletions test/it/binder/src/test/resources/sqls/dcl/grant.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You 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.
-->

<sql-cases>
<sql-case id="grant_select_on_table" value="GRANT SELECT ON t_order TO user1" db-types="MySQL"/>
</sql-cases>