Skip to content
This repository has been archived by the owner on Jan 3, 2022. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/antlr-plsqlparser'
Browse files Browse the repository at this point in the history
  • Loading branch information
tsutomi committed Apr 13, 2016
2 parents fc42e35 + 11e9c85 commit e9463dd
Show file tree
Hide file tree
Showing 18 changed files with 391 additions and 154 deletions.
4 changes: 2 additions & 2 deletions src/deveeldb-nunit/Deveel.Data.Client/DbCommandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public sealed class DbCommandTest : ContextBasedTest {
Assert.IsNotNull(command);
Assert.IsNotNull(command.Connection);

command.CommandText = "a = ?";
command.CommandText = "a := ?";
command.Parameters.Add(22);

Assert.DoesNotThrow(() => command.ExecuteNonQuery());
Expand All @@ -84,7 +84,7 @@ public sealed class DbCommandTest : ContextBasedTest {
Assert.IsNotNull(command);
Assert.IsNotNull(command.Connection);

command.CommandText = "a = ?";
command.CommandText = "a := ?";
command.Parameters.Add(22);

var result = command.ExecuteScalar();
Expand Down
27 changes: 8 additions & 19 deletions src/deveeldb-nunit/Deveel.Data.Sql.Compile/PlSqlCodeBlockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,11 @@
namespace Deveel.Data.Sql.Compile {
[TestFixture]
public sealed class PlSqlCodeBlockTests : SqlCompileTestBase {
[Test]
public void EmptyBlock() {
const string sql = @"BEGIN END";

var result = Compile(sql);

Assert.IsNotNull(result);
Assert.IsFalse(result.HasErrors);

Assert.AreEqual(1, result.Statements.Count);

var obj = result.Statements.ElementAt(0);

Assert.IsNotNull(obj);
Assert.IsInstanceOf<PlSqlBlockStatement>(obj);
}

[Test]
public void SelectInBlock() {
const string sql = @"BEGIN
SELECT * FROM test WHERE a = 90 AND
b > 12.922
b > 12.922;
END";

var result = Compile(sql);
Expand Down Expand Up @@ -59,13 +42,19 @@ public sealed class PlSqlCodeBlockTests : SqlCompileTestBase {
public void DeclarationsBeforeBlock() {
const string sql = @"DECLARE a INT := 23
BEGIN
SELECT * FROM test WHERE a < test.a
SELECT * FROM test WHERE a < test.a;
END";

var result = Compile(sql);

Assert.IsNotNull(result);
Assert.IsFalse(result.HasErrors);

Assert.AreEqual(1, result.Statements.Count);

var statement = result.Statements.ElementAt(0);

Assert.IsInstanceOf<PlSqlBlockStatement>(statement);
}
}
}
3 changes: 1 addition & 2 deletions src/deveeldb-nunit/Deveel.Data.Types/StringTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ public class StringTypeTests {
[Test]
public void SizedVarChar_Parse() {
const string typeString = "VARCHAR(255)";
SqlType sqlType = null;
Assert.DoesNotThrow(() => sqlType = SqlType.Parse(typeString));
var sqlType = SqlType.Parse(typeString);
Assert.IsNotNull(sqlType);
Assert.IsInstanceOf<StringType>(sqlType);
Assert.AreEqual(SqlTypeCode.VarChar, sqlType.TypeCode);
Expand Down
5 changes: 0 additions & 5 deletions src/deveeldb-nunit/deveeldb-nunit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@
<HintPath>..\packages\dmath.1.5.71\lib\net20\Deveel.Math.pdb\Deveel.Math.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Irony, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ca48ace7223ead47, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\irony.net35\Irony.dll</HintPath>
</Reference>
<Reference Include="nunit.framework, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -119,7 +115,6 @@
<Compile Include="Deveel.Data.Sql.Compile\CreateViewTests.cs" />
<Compile Include="Deveel.Data.Sql.Compile\DeclareVariableTests.cs" />
<Compile Include="Deveel.Data.Sql.Compile\DropTriggerTests.cs" />
<Compile Include="Deveel.Data.Sql.Compile\DropTypeTests.cs" />
<Compile Include="Deveel.Data.Sql.Compile\DropUserTests.cs" />
<Compile Include="Deveel.Data.Sql.Compile\SchemaCompileTests.cs" />
<Compile Include="Deveel.Data.Sql.Compile\SqlCompileTestBase.cs" />
Expand Down
26 changes: 18 additions & 8 deletions src/deveeldb/Deveel.Data.Sql.Compile/DataTypeVisitor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

Expand Down Expand Up @@ -81,7 +82,7 @@ class DataTypeVisitor : PlSqlParserBaseVisitor<DataTypeInfo> {
}

public override DataTypeInfo VisitString_type(PlSqlParser.String_typeContext context) {
var size = Number.PositiveInteger(context.numeric()) ?? -1;
var size = Number.PositiveInteger(context.numeric());

SqlTypeCode typeCode;
if (context.CHAR() != null) {
Expand All @@ -98,14 +99,23 @@ class DataTypeVisitor : PlSqlParserBaseVisitor<DataTypeInfo> {
throw new ParseCanceledException("Invalid string type");
}

// TODO: Support COLLATE
var meta = new[] {
new DataTypeMeta("Size", size.ToString()),
new DataTypeMeta("Encoding", "utf-8"),
new DataTypeMeta("Collate", "invariant")
};
string encoding = null;
if (context.ENCODING() != null)
encoding = InputString.AsNotQuoted(context.encoding.Text);

return new DataTypeInfo(typeCode.ToString().ToUpperInvariant(), meta);
string locale = null;
if (context.LOCALE() != null)
locale = InputString.AsNotQuoted(context.locale.Text);

var meta = new List<DataTypeMeta>();
if (size != null)
meta.Add(new DataTypeMeta("MaxSize", size.Value.ToString()));
if (locale != null)
meta.Add(new DataTypeMeta("Locale", locale));
if (encoding != null)
meta.Add(new DataTypeMeta("Encoding", encoding));

return new DataTypeInfo(typeCode.ToString().ToUpperInvariant(), meta.ToArray());
}

public override DataTypeInfo VisitBinary_type(PlSqlParser.Binary_typeContext context) {
Expand Down
15 changes: 15 additions & 0 deletions src/deveeldb/Deveel.Data.Sql.Compile/Name.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Deveel.Data.Sql.Compile {
public static class Name {
[CLSCompliant(false)]
public static ObjectName Object(PlSqlParser.ObjectNameContext context) {
if (context == null)
return null;
Expand All @@ -23,67 +24,78 @@ public static class Name {
return name;
}

[CLSCompliant(false)]
public static string Simple(PlSqlParser.IdContext context) {
return InputString.AsNotQuoted(context.GetText());
}

[CLSCompliant(false)]
public static string Simple(PlSqlParser.Column_aliasContext context) {
return Simple(context.id());
}

[CLSCompliant(false)]
public static string Simple(PlSqlParser.LabelNameContext context) {
return Simple(context.id());
}

[CLSCompliant(false)]
public static string Simple(PlSqlParser.UserNameContext context) {
if (context == null)
return null;

return Simple(context.id());
}

[CLSCompliant(false)]
public static string Simple(PlSqlParser.GranteeNameContext context) {
if (context.userName() != null)
return Simple(context.userName());

return Simple(context.roleName());
}

[CLSCompliant(false)]
public static string Simple(PlSqlParser.RoleNameContext context) {
if (context == null)
return null;

return Simple(context.id());
}

[CLSCompliant(false)]
public static string Simple(PlSqlParser.ColumnNameContext context) {
if (context == null)
return null;

return Simple(context.id());
}

[CLSCompliant(false)]
public static string Simple(PlSqlParser.Cursor_nameContext context) {
if (context == null)
return null;

return Simple(context.id());
}

[CLSCompliant(false)]
public static string Simple(PlSqlParser.Regular_idContext context) {
if (context == null)
return null;

return context.GetText();
}

[CLSCompliant(false)]
public static ObjectName Select(PlSqlParser.ObjectNameContext context, bool glob) {
var name = Object(context);
if (glob)
name = new ObjectName(name, "*");
return name;
}

[CLSCompliant(false)]
public static string Variable(PlSqlParser.Bind_variableContext context) {
var text = context.GetText();
if (String.IsNullOrEmpty(text))
Expand All @@ -95,17 +107,20 @@ public static class Name {
return text;
}

[CLSCompliant(false)]
public static string Variable(PlSqlParser.Variable_nameContext context) {
if (context.bind_variable() != null)
return Variable(context.bind_variable());

return Simple(context.id());
}

[CLSCompliant(false)]
public static string Simple(PlSqlParser.Parameter_nameContext context) {
return Simple(context.id());
}

[CLSCompliant(false)]
public static string Simple(PlSqlParser.Variable_nameContext context) {
return Simple(context.id());
}
Expand Down
17 changes: 17 additions & 0 deletions src/deveeldb/Deveel.Data.Sql.Compile/PlSqlBody.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;

using Deveel.Data.Sql.Statements;

namespace Deveel.Data.Sql.Compile {
class PlSqlBody : SqlStatement {
public PlSqlBody() {
Statements = new List<SqlStatement>();
ExceptionHandlers = new List<ExceptionHandler>();
}

public List<SqlStatement> Statements { get; private set; }

public List<ExceptionHandler> ExceptionHandlers { get; private set; }
}
}
61 changes: 32 additions & 29 deletions src/deveeldb/Deveel.Data.Sql.Compile/PlSqlCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,52 @@

namespace Deveel.Data.Sql.Compile {
public sealed class PlSqlCompiler : ISqlCompiler {
private PlSqlParser plSqlParser;
private List<SqlCompileMessage> messages;
//private PlSqlParser plSqlParser;
//private List<SqlCompileMessage> messages;

public PlSqlCompiler() {
plSqlParser = MakeParser(String.Empty, message => messages.Add(message));
messages = new List<SqlCompileMessage>(12);
//plSqlParser = MakeParser(String.Empty, message => messages.Add(message));
//messages = new List<SqlCompileMessage>(12);
}

~PlSqlCompiler() {
Dispose(false);
}
//~PlSqlCompiler() {
// Dispose(false);
//}

public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
//Dispose(true);
//GC.SuppressFinalize(this);
}

private void Dispose(bool disposing) {
if (disposing) {
if (messages != null)
messages.Clear();
}
//private void Dispose(bool disposing) {
// if (disposing) {
// if (messages != null)
// messages.Clear();
// }

plSqlParser = null;
messages = null;
}
// //plSqlParser = null;
// messages = null;
//}

private void SetInput(string inputString) {
plSqlParser.SetInputStream(new BufferedTokenStream(new PlSqlLexer(new AntlrInputStream(inputString))));
messages.Clear();
}
//private void SetInput(string inputString) {
// plSqlParser.SetInputStream(new BufferedTokenStream(new PlSqlLexer(new AntlrInputStream(inputString))));
// messages.Clear();
//}

public SqlCompileResult Compile(SqlCompileContext context) {
var result = new SqlCompileResult(context);

try {
SetInput(context.SourceText);
//SetInput(context.SourceText);

var plSqlParser = MakeParser(context.SourceText, message => result.Messages.Add(message));
var parseResult = plSqlParser.compilationUnit();

if (messages.Count > 0) {
foreach (var message in messages) {
result.Messages.Add(message);
}
}
//if (messages.Count > 0) {
// foreach (var message in messages) {
// result.Messages.Add(message);
// }
//}

var visitor = new SqlStatementVisitor();
var statement = visitor.Visit(parseResult);
Expand Down Expand Up @@ -105,7 +106,8 @@ private class ErrorHandler : BaseErrorListener {
}

public SqlExpression ParseExpression(string text) {
SetInput(text);
//SetInput(text);
var plSqlParser = MakeParser(text, null);
var parseResult = plSqlParser.expression_unit();

var visitor = new SqlExpressionVisitor();
Expand All @@ -114,7 +116,8 @@ private class ErrorHandler : BaseErrorListener {
}

public DataTypeInfo ParseDataType(string s) {
SetInput(s);
// SetInput(s);
var plSqlParser = MakeParser(s, null);
var parseResult = plSqlParser.datatype();

var visitor = new DataTypeVisitor();
Expand Down

0 comments on commit e9463dd

Please sign in to comment.