Skip to content

Commit

Permalink
Потребовалось писать запросы к Константам и прокидывать параметры
Browse files Browse the repository at this point in the history
  • Loading branch information
Vasiliy Chernoivan committed Apr 11, 2017
1 parent 30ba60a commit cf064f3
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Simple1C/Impl/Sql/SchemaMapping/Simple1cSchemaCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private TableMapping CreateDefaultTableMapping(ValueTableRow tableRow,
ConfigurationItemDescriptor descriptor;
object comObject;
TableType tableType;
if (purpose == "Основная")
if (purpose == "Основная" || purpose == "Константа")
{
var configurationName = ConfigurationName.ParseOrNull(queryTableName);
if (!configurationName.HasValue)
Expand Down
61 changes: 37 additions & 24 deletions Simple1C/Impl/Sql/SqlAccess/Parsing/QueryGrammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,21 @@ public class QueryGrammar : Grammar
russianAlphabet.ToUpper() +
"_";

private readonly NumberLiteral numberLiteral =
new NumberLiteral("number", NumberOptions.Default,
(context, node) => node.AstNode = new LiteralExpression {Value = node.Token.Value});
private static readonly string[] aggregateFunctions =
{
"Count", "Min", "Max", "Sum", "Avg",
"КОЛИЧЕСТВО", "МИНИМУМ", "МАКСИМУМ", "СУММА", "СРЕДНЕЕ"
};

private readonly BnfExpression not;
private readonly NonTerminal by;
private readonly NonTerminal distinctOpt;

private readonly BnfExpression not;

private readonly NumberLiteral numberLiteral =
new NumberLiteral("number", NumberOptions.Default,
(context, node) => node.AstNode = new LiteralExpression {Value = node.Token.Value});

public QueryGrammar()
: base(false)
{
Expand Down Expand Up @@ -59,7 +66,7 @@ public QueryGrammar()
var groupClauseOpt = GroupBy(expression);
var orderClauseOpt = OrderBy(expression);
var havingKeyword = Transient("havingKeyword", ToTerm("HAVING") | "ИМЕЮЩИЕ");
var havingClauseOpt = NonTerminal("havingClauseOpt", Empty | havingKeyword + expression,
var havingClauseOpt = NonTerminal("havingClauseOpt", Empty | (havingKeyword + expression),
node => node.ChildNodes.Count == 0 ? null : node.ChildNodes[1].AstNode);
var columnItemList = NonTerminal("columnItemList", null);

Expand All @@ -78,7 +85,7 @@ public QueryGrammar()
+ joinItemList + whereClauseOpt
+ groupClauseOpt + havingClauseOpt;
selectList.Rule = columnItemList | "*";
topOpt.Rule = Empty | Transient("top", ToTerm("TOP") | "ПЕРВЫЕ") + numberLiteral;
topOpt.Rule = Empty | (Transient("top", ToTerm("TOP") | "ПЕРВЫЕ") + numberLiteral);
distinctOpt.Rule = Empty | Transient("distinct", ToTerm("DISTINCT") | "РАЗЛИЧНЫЕ");

columnSource.Rule = tableDeclaration | subqueryTable;
Expand Down Expand Up @@ -140,12 +147,6 @@ private NonTerminal Identifier()
return id;
}

private static readonly string[] aggregateFunctions =
{
"Count", "Min", "Max", "Sum", "Avg",
"КОЛИЧЕСТВО", "МИНИМУМ", "МАКСИМУМ", "СУММА", "СРЕДНЕЕ"
};

private NonTerminal Expression(NonTerminal identifier, NonTerminal selectStatement)
{
var datePartLiteral = NonTerminal("datePartLiteral", null,
Expand All @@ -159,6 +160,16 @@ private NonTerminal Expression(NonTerminal identifier, NonTerminal selectStateme
StringOptions.AllowsAllEscapes | StringOptions.AllowsDoubledQuote,
(context, node) => node.AstNode = new LiteralExpression {Value = node.Token.Value});

var paramName = new IdentifierTerminal("paramName")
{
AllFirstChars = validChars,
AllChars = validChars + "1234567890"
};
paramName.SetFlag(TermFlags.NoAstNode);

var paramLiteral = NonTerminal("parameter", "@" + paramName,
node => new ParamLiteralExpression {Value = "@" + node.ChildNodes[1].Token.ValueString});

var valueLiteral = NonTerminal("valueLiteral",
Transient("valueFunction", ToTerm("value") | "ЗНАЧЕНИЕ") + "(" + identifier + ")",
node => new ValueLiteralExpression
Expand All @@ -176,7 +187,7 @@ private NonTerminal Expression(NonTerminal identifier, NonTerminal selectStateme
var text = node.FindTokenAndGetText().ToLower();
return new LiteralExpression
{
Value = text == "true" || text == "истина"
Value = (text == "true") || (text == "истина")
};
});
var columnRef = NonTerminal("columnRef",
Expand Down Expand Up @@ -227,7 +238,7 @@ private NonTerminal Expression(NonTerminal identifier, NonTerminal selectStateme
parExprList.Rule = "(" + exprList + ")";
parExpr.Rule = "(" + expression + ")";
term.Rule = columnRef | stringLiteral | numberLiteral | valueLiteral | boolLiteral | nullLiteral
| aggregate | queryFunctionExpr
| aggregate | queryFunctionExpr | paramLiteral
| parExpr | subquery;
subquery.Rule = "(" + selectStatement + ")";
unOp.Rule = not | "-";
Expand All @@ -248,7 +259,7 @@ private NonTerminal Expression(NonTerminal identifier, NonTerminal selectStateme
for (var i = 1; i < aggregateFunctions.Length; i++)
aggregateFunctionName.Rule |= aggregateFunctions[i];

aggregateArg.Rule = ToTerm("*") | distinctOpt + expression;
aggregateArg.Rule = ToTerm("*") | (distinctOpt + expression);
aggregate.Rule = aggregateFunctionName + "(" + aggregateArg + ")";
inExpr.Rule = columnRef + Transient("in", ToTerm("IN") | "В") + functionArgs;

Expand Down Expand Up @@ -294,7 +305,7 @@ private NonTerminal GroupBy(NonTerminal expression)
});
groupColumnList.Rule = MakePlusRule(groupColumnList, ToTerm(","), expression);
groupClauseOpt.Rule = Empty |
NonTerminal("group", ToTerm("GROUP") | "СГРУППИРОВАТЬ") + by + groupColumnList;
(NonTerminal("group", ToTerm("GROUP") | "СГРУППИРОВАТЬ") + by + groupColumnList);
return groupClauseOpt;
}

Expand Down Expand Up @@ -344,9 +355,9 @@ private static InExpression ToInExpression(ParseTreeNode node)
{
Column = (ColumnReferenceExpression) node.ChildNodes[0].AstNode,
Source = sqlSource ?? new ListExpression
{
Elements = sourceNode.Elements().Cast<ISqlElement>().ToList()
}
{
Elements = sourceNode.Elements().Cast<ISqlElement>().ToList()
}
};
}

Expand Down Expand Up @@ -392,7 +403,7 @@ private NonTerminal OrderBy(BnfTerm expression)
var orderColumnList = NonTerminal("orderColumnList", null);
orderColumnList.Rule = MakePlusRule(orderColumnList, ToTerm(","), orderingExpression);
return NonTerminal("orderClauseOpt",
Empty | Transient("order", ToTerm("ORDER") | "УПОРЯДОЧИТЬ") + by + orderColumnList,
Empty | (Transient("order", ToTerm("ORDER") | "УПОРЯДОЧИТЬ") + by + orderColumnList),
ToOrderByClause);
}

Expand Down Expand Up @@ -442,7 +453,9 @@ private static SelectClause ToSelectClause(ParseTreeNode n)
else
result.Fields.AddRange(selectColumns);
var topNode = n.ChildNodes[1].ChildNodes.ElementAtOrDefault(1);
result.Top = topNode != null && topNode.Token != null ? int.Parse(topNode.Token.ValueString) : (int?) null;
result.Top = (topNode != null) && (topNode.Token != null)
? int.Parse(topNode.Token.ValueString)
: (int?) null;
result.IsDistinct = n.ChildNodes[2].ChildNodes.Any();
result.JoinClauses.AddRange(elements.OfType<JoinClause>());
result.WhereExpression = (ISqlElement) n.ChildNodes[7].AstNode;
Expand Down Expand Up @@ -515,7 +528,7 @@ private static AggregateFunctionExpression ToAggregateFunctionExpression(ParseTr
if (!string.IsNullOrEmpty(distinctText))
isDistinct = true;
}
if (!isSelectAll && argumentNode.AstNode == null)
if (!isSelectAll && (argumentNode.AstNode == null))
throw new InvalidOperationException(string.Format("Invalid aggregation argument {0}", argumentNode));
return new AggregateFunctionExpression
{
Expand Down Expand Up @@ -600,8 +613,8 @@ private static IsNullExpression ToIsNullExpression(ParseTreeNode arg)
return new IsNullExpression
{
Argument = (ISqlElement) arg.ChildNodes[0].AstNode,
IsNotNull = notToken != null && (notToken.ValueString.EqualsIgnoringCase("not")
|| notToken.ValueString.EqualsIgnoringCase("не"))
IsNotNull = (notToken != null) && (notToken.ValueString.EqualsIgnoringCase("not")
|| notToken.ValueString.EqualsIgnoringCase("не"))
};
}

Expand Down
14 changes: 14 additions & 0 deletions Simple1C/Impl/Sql/SqlAccess/Syntax/ParamLiteralExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Simple1C.Impl.Sql.Translation;

namespace Simple1C.Impl.Sql.SqlAccess.Syntax
{
internal class ParamLiteralExpression : ISqlElement
{
public string Value { get; set; }

public ISqlElement Accept(SqlVisitor visitor)
{
return visitor.VisitParamLiteral(this);
}
}
}
6 changes: 6 additions & 0 deletions Simple1C/Impl/Sql/Translation/SqlFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ public override JoinClause VisitJoin(JoinClause clause)
return clause;
}

public override ISqlElement VisitParamLiteral(ParamLiteralExpression expression)
{
builder.Append(expression.Value);
return expression;
}

public override ISqlElement VisitLiteral(LiteralExpression expression)
{
var value = expression.SqlType.HasValue
Expand Down
7 changes: 6 additions & 1 deletion Simple1C/Impl/Sql/Translation/SqlVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ public virtual ISqlElement VisitValueLiteral(ValueLiteralExpression expression)
{
return expression;
}


public virtual ISqlElement VisitParamLiteral(ParamLiteralExpression expression)
{
return expression;
}

public virtual ISqlElement VisitIsReference(IsReferenceExpression expression)
{
expression.Argument = (ColumnReferenceExpression) VisitColumnReference(expression.Argument);
Expand Down
6 changes: 3 additions & 3 deletions Simple1C/Simple1C.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
<Compile Include="Impl\Sql\SqlAccess\Syntax\ListExpression.cs" />
<Compile Include="Impl\Sql\SqlAccess\Syntax\OperatorPrecedenceAttribute.cs" />
<Compile Include="Impl\Sql\SqlAccess\Syntax\OperatorSynonymsAttribute.cs" />
<Compile Include="Impl\Sql\SqlAccess\Syntax\ParamLiteralExpression.cs" />
<Compile Include="Impl\Sql\SqlAccess\Syntax\SubqueryTable.cs" />
<Compile Include="Impl\Sql\SqlAccess\Syntax\UnaryExpression.cs" />
<Compile Include="Impl\Sql\SqlAccess\Syntax\UnaryOperator.cs" />
Expand Down Expand Up @@ -276,16 +277,15 @@
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>-->

<Target Name="AfterBuild">
<CreateItem Include="@(ReferencePath)" Condition="'%(CopyLocal)'=='true'">
<Output TaskParameter="Include" ItemName="IlmergeAssemblies" />
</CreateItem>
<PropertyGroup>
<ILRepackCommand>"..\packages\ILRepack.2.0.11\tools\ILRepack.exe" /internalize /out:$(OutputPath)\$(AssemblyName).dll "@(IntermediateAssembly)" @(IlmergeAssemblies->'"%(FullPath)"', ' ')</ILRepackCommand>
</PropertyGroup>
<Exec Condition = "'$(OS)' = 'Unix'" Command="mono $(ILRepackCommand)"/>
<Exec Condition = "'$(OS)' != 'Unix'" Command="$(ILRepackCommand)"/>
<Exec Condition="'$(OS)' == 'Unix'" Command="mono $(ILRepackCommand)" />
<Exec Condition="'$(OS)' != 'Unix'" Command="$(ILRepackCommand)" />
<Delete Files="@(ReferenceCopyLocalPaths->'$(OutDir)%(DestinationSubDirectory)%(Filename)%(Extension)')" />
</Target>
</Project>
2 changes: 1 addition & 1 deletion nuget/simple1C.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Simple1C</id>
<version>1.0.52</version>
<version>1.0.53</version>
<authors>gusev_p, ivan_816</authors>
<owners>gusev_p, ivan_816</owners>
<licenseUrl>https://github.com/gusev-p/simple-1c/blob/master/LICENSE.txt</licenseUrl>
Expand Down

0 comments on commit cf064f3

Please sign in to comment.