Skip to content

Commit

Permalink
feat(grammar): Support TRUNCATE TABLE statement
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebz committed Aug 20, 2024
1 parent 211d7e6 commit 547cf56
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,6 @@
"examples/truncate_cluster.sql" : [
1
],
"examples/truncate_table.sql" : [
1
],
"examples/unified.sql" : [
1
],
Expand All @@ -461,9 +458,6 @@
"hw-examples/alter_outline.sql" : [
1
],
"hw-examples/truncate_table.sql" : [
1
],
"long-running/order_by07.sql" : [
2
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2936,12 +2936,6 @@
"sqlrf/TRUNCATE-CLUSTER-0.sql" : [
2
],
"sqlrf/TRUNCATE-TABLE-0.sql" : [
2
],
"sqlrf/TRUNCATE-TABLE-1.sql" : [
2
],
"sqlrf/Type-Constructor-Expressions-1.sql" : [
2
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"examples/mybooks_setup.sql" : [
55,
61,
67
],
"examples/test_te_employee.pkb" : [
191,
237,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
{
"examples/mybooks_setup.sql" : [
28,
29,
30,
31,
31,
31,
32,
32,
32,
33,
38,
45,
52,
52,
52,
58,
58,
58,
64,
70
],
"examples/te_employee.pkb" : [
936
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{
"examples/mybooks_setup.sql" : [
19,
20,
21,
22,
23,
54
],
"source/ut_receq.pkb" : [
245,
255
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
"examples/filepath1.pkg" : [
104
],
"examples/mybooks_setup.sql" : [
17
],
"examples/str2list.pkg" : [
62
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"examples/mybooks_setup.sql" : [
48
],
"examples/te_employee.pkb" : [
863,
874,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
199,
238
],
"examples/mybooks_setup.sql" : [
41,
72
],
"examples/test_te_employee.pkb" : [
179
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ enum class DdlGrammar : GrammarRuleKey {
SUBPARTITION_BY_HASH,
SUBPARTITION_TEMPLATE,
CREATE_DIRECTORY,
DROP_DIRECTORY;
DROP_DIRECTORY,
TRUNCATE_TABLE;

companion object {
fun buildOn(b: PlSqlGrammarBuilder) {
Expand Down Expand Up @@ -653,6 +654,27 @@ enum class DdlGrammar : GrammarRuleKey {
b.rule(DROP_DIRECTORY).define(
DROP, DIRECTORY, b.optional(IF, EXISTS), IDENTIFIER_NAME, b.optional(SEMICOLON))

b.rule(TRUNCATE_TABLE).define(
TRUNCATE, TABLE, UNIT_NAME,
b.optional(
b.firstOf(
PRESERVE, PURGE
),
MATERIALIZED, VIEW, LOG
),
b.optional(
b.firstOf(
b.sequence(
DROP, b.optional(ALL)
),
REUSE
),
STORAGE
),
b.optional(CASCADE),
b.optional(SEMICOLON)
)

b.rule(DDL_COMMAND).define(b.firstOf(
DDL_COMMENT,
CREATE_TABLE,
Expand All @@ -665,7 +687,8 @@ enum class DdlGrammar : GrammarRuleKey {
CREATE_SEQUENCE,
CREATE_DIRECTORY,
DROP_DIRECTORY,
DROP_COMMAND))
DROP_COMMAND,
TRUNCATE_TABLE))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ enum class PlSqlKeyword(override val value: String, val isReserved: Boolean = fa
PRESERVE("preserve"),
PRETTY("pretty"),
PRIMARY("primary"),
PURGE("purge"),
QUOTES("quotes"),
RAISE("raise"),
RANGE_KEYWORD("range"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Z PL/SQL Analyzer
* Copyright (C) 2015-2024 Felipe Zorzo
* mailto:felipe AT felipezorzo DOT com DOT br
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.plugins.plsqlopen.api.ddl

import com.felipebz.flr.tests.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.sonar.plugins.plsqlopen.api.DdlGrammar
import org.sonar.plugins.plsqlopen.api.RuleTest

class TruncateTableTest : RuleTest() {

@BeforeEach
fun init() {
setRootRule(DdlGrammar.TRUNCATE_TABLE)
}

@Test
fun matchesSimpleTruncate() {
assertThat(p).matches("truncate table foo;")
}

@Test
fun matchesTruncatePreserveMaterializedViewLog() {
assertThat(p).matches("truncate table foo preserve materialized view log;")
}

@Test
fun matchesTruncatePurgeMaterializedViewLog() {
assertThat(p).matches("truncate table foo purge materialized view log;")
}

@Test
fun matchesTruncateDropStorage() {
assertThat(p).matches("truncate table foo drop storage;")
assertThat(p).matches("truncate table foo drop all storage;")
}

@Test
fun matchesTruncateReuseStorage() {
assertThat(p).matches("truncate table foo reuse storage;")
}

@Test
fun matchesTruncateCascade() {
assertThat(p).matches("truncate table foo cascade;")
}

@Test
fun matchesLongTruncate() {
assertThat(p).matches("truncate table sch.foo purge materialized view log drop all storage cascade;")
}

}

0 comments on commit 547cf56

Please sign in to comment.