Skip to content

Commit

Permalink
правил джойны по области данных. Обновляем ВерсиюДанных на каждый inM…
Browse files Browse the repository at this point in the history
…emoryDataContext.Save
  • Loading branch information
Vasiliy Chernoivan committed May 24, 2018
1 parent ff246d8 commit 4ac502a
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 55 deletions.
12 changes: 12 additions & 0 deletions Simple1C/Impl/InMemoryDataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ private InMemoryEntity Save(Abstract1CEntity entity, bool isTableSection)
{
if (entity == null)
return null;

var changed = entity.Controller.Changed;
if (changed != null)
{
Expand Down Expand Up @@ -97,6 +98,11 @@ private InMemoryEntity Save(Abstract1CEntity entity, bool isTableSection)
inMemoryEntity = inmemoryEntityRevision.inMemoryEntity;
if (changed != null)
{
var configurationName = ConfigurationName.Get(entity.GetType());
if (configurationName.Scope == ConfigurationScope.Документы)
{
AssignNewGuid(entity, changed, "ВерсияДанных");
}
inMemoryEntity.revision = new InMemoryEntityRevision(inMemoryEntity, inmemoryEntityRevision, changed);
Collection(entity.GetType()).revision++;
}
Expand All @@ -113,9 +119,15 @@ private InMemoryEntity Save(Abstract1CEntity entity, bool isTableSection)
{
var configurationName = ConfigurationName.Get(entity.GetType());
if (configurationName.Scope == ConfigurationScope.Справочники)
{
AssignNewGuid(entity, changed, "Код");
AssignNewGuid(entity, changed, "ВерсияДанных");
}
else if (configurationName.Scope == ConfigurationScope.Документы)
{
AssignNewGuid(entity, changed, "Номер");
AssignNewGuid(entity, changed, "ВерсияДанных");
}
if (entity.Controller.IsNew && configurationName.HasReference)
{
var idProperty = entity.GetType().GetProperty(EntityHelpers.idPropertyName);
Expand Down
4 changes: 2 additions & 2 deletions Simple1C/Impl/Sql/Translation/QueryToSqlTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ public override SelectClause VisitSelect(SelectClause clause)
if (tableClause == null)
return base.VisitSelect(clause);
var tableMapping = mappingSource.ResolveTableByDbNameOrNull(tableClause.Name);
var property = tableMapping.GetByPropertyName("ОбластьДанныхОсновныеДанные");
if (property == null)
PropertyMapping property;
if (!tableMapping.TryGetProperty("ОбластьДанныхОсновныеДанные",out property))
return base.VisitSelect(clause);
var areaExpression = new InExpression
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,12 @@ public override JoinClause VisitJoin(JoinClause clause)

public override ISqlElement VisitTableDeclaration(TableDeclarationClause clause)
{
contexts.Peek().MainTable = clause;
var context = contexts.Peek();
if (context.MainTable == null)
context.MainTable = clause;
return base.VisitTableDeclaration(clause);
}

public override SubqueryTable VisitSubqueryTable(SubqueryTable subqueryTable)
{
contexts.Peek().MainTable = null;
return base.VisitSubqueryTable(subqueryTable);
}

public override SqlQuery VisitSqlQuery(SqlQuery sqlQuery)
{
contexts.Push(new Context());
Expand Down
121 changes: 75 additions & 46 deletions Simple1C/Impl/Sql/Translation/Visitors/QueryFunctionRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,68 +157,97 @@ public override ISqlElement VisitQueryFunction(QueryFunctionExpression expressio
var tableDeclarationClause = columnReferenceExpression.Table as TableDeclarationClause;
if (tableDeclarationClause == null)
{
const string message = "[{0}] function not supported for subquery column reference, [{1}.{2}]. Table is of type [{3}]";
throw new InvalidOperationException(string.Format(message, expression.KnownFunction,
columnReferenceExpression.Table.Alias, columnReferenceExpression.Name, columnReferenceExpression.Table.GetType().Name));
}
var subqueryDeclaration = columnReferenceExpression.Table as SubqueryTable;
if (subqueryDeclaration == null)
{
const string message =
"[{0}] function not supported for a {1} " +
"column reference, [{2}.{3}]. Table is of type [{4}]";
throw new InvalidOperationException(string.Format(
message, columnReferenceExpression.Table.GetType().Name, expression.KnownFunction,
columnReferenceExpression.Table.Alias, columnReferenceExpression.Name,
columnReferenceExpression.Table.GetType().Name));
}

var resolvedTableMapping = mappingSource.ResolveTableByDbNameOrNull(tableDeclarationClause.Name);
if (resolvedTableMapping == null)
{
const string message = "can't find table [{0}] in column reference [{0}.{1}] for function [{2}]";
throw new InvalidOperationException(string.Format(message,
columnReferenceExpression.Table.Alias, columnReferenceExpression.Name,
expression.KnownFunction));
}
var columnAlias = columnReferenceExpression.Name + "_Type";

foreach (var mapping in resolvedTableMapping.Properties)
foreach (var union in subqueryDeclaration.Query.Query.Unions)
{
AddTypeColumn(union, columnReferenceExpression, columnAlias);
}
return new ColumnReferenceExpression()
{
Name = columnAlias,
Table = columnReferenceExpression.Table
};
}
else
{
if (mapping.SingleLayout != null)
var resolvedTableMapping = mappingSource.ResolveTableByDbNameOrNull(tableDeclarationClause.Name);
if (resolvedTableMapping == null)
{
const string message =
"can't find table [{0}] in column reference [{0}.{1}] for function [{2}]";
throw new InvalidOperationException(string.Format(message,
columnReferenceExpression.Table.Alias, columnReferenceExpression.Name,
expression.KnownFunction));
}

foreach (var mapping in resolvedTableMapping.Properties)
{
if (mapping.SingleLayout.DbColumnName == columnReferenceExpression.Name)
if (mapping.SingleLayout != null)
{
if (string.IsNullOrEmpty(mapping.SingleLayout.NestedTableName))
if (mapping.SingleLayout.DbColumnName == columnReferenceExpression.Name)
{
const string msgFormat =
"[{0}] function not supported for non-reference columns, [{1}.{2}]";
throw new InvalidOperationException(string.Format(msgFormat, expression.KnownFunction,
columnReferenceExpression.Table.Alias, columnReferenceExpression.Name));
}
if (string.IsNullOrEmpty(mapping.SingleLayout.NestedTableName))
{
const string msgFormat =
"[{0}] function not supported for non-reference columns, [{1}.{2}]";
throw new InvalidOperationException(string.Format(msgFormat,
expression.KnownFunction,
columnReferenceExpression.Table.Alias, columnReferenceExpression.Name));
}

var byDbName = mappingSource.ResolveTableOrNull(mapping.SingleLayout.NestedTableName);
if (byDbName == null || !byDbName.Index.HasValue)
{
const string message = "can't find mapping for [{0}] following column reference " +
"[{1}.{2}] for function [{3}]";
throw new InvalidOperationException(string.Format(message,
mapping.SingleLayout.NestedTableName, columnReferenceExpression.Table.Alias,
columnReferenceExpression.Name, expression.KnownFunction));
}
var byDbName = mappingSource.ResolveTableOrNull(mapping.SingleLayout.NestedTableName);
if (byDbName == null || !byDbName.Index.HasValue)
{
const string message = "can't find mapping for [{0}] following column reference " +
"[{1}.{2}] for function [{3}]";
throw new InvalidOperationException(string.Format(message,
mapping.SingleLayout.NestedTableName, columnReferenceExpression.Table.Alias,
columnReferenceExpression.Name, expression.KnownFunction));
}

return new LiteralExpression()
{
SqlType = SqlType.ByteArray,
Value = byDbName.Index.Value
};
return new LiteralExpression()
{
SqlType = SqlType.ByteArray,
Value = byDbName.Index.Value
};
}
}
else if (mapping.UnionLayout != null)
if (mapping.UnionLayout.ReferenceColumnName == columnReferenceExpression.Name)
return new ColumnReferenceExpression()
{
Name = mapping.UnionLayout.TableIndexColumnName,
Table = columnReferenceExpression.Table
};
}
else if (mapping.UnionLayout != null)
if (mapping.UnionLayout.ReferenceColumnName == columnReferenceExpression.Name)
return new ColumnReferenceExpression()
{
Name = mapping.UnionLayout.TableIndexColumnName,
Table = columnReferenceExpression.Table
};
}

const string msg = "could not find columns [{1}.{2}] for function [{0}]";
throw new InvalidOperationException(string.Format(msg, expression.KnownFunction,
columnReferenceExpression.Table.Alias, columnReferenceExpression.Name));
const string msg = "could not find columns [{1}.{2}] for function [{0}]";
throw new InvalidOperationException(string.Format(msg, expression.KnownFunction,
columnReferenceExpression.Table.Alias, columnReferenceExpression.Name));
}
}

return expression;
}

private void AddTypeColumn(UnionClause union, ColumnReferenceExpression columnReferenceExpression, string columnAlias)
{
throw new NotImplementedException();
}

private static void ExpectArgumentCount(QueryFunctionExpression expression,
int expectedCount, int? expectedCount2 = null)
{
Expand Down
26 changes: 26 additions & 0 deletions Tests/Sql/BasicTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,32 @@ when sum > 300 then 2
CheckTranslate(mappings, sourceSql, expected);
}

[Test]
public void AreaOnSeveralLeftJoin()
{
const string sourceSql =
@"select contractors2.НаименованиеПолное as ContractorFullname
FROM справочник.Контрагенты as contractors1
LEFT JOIN справочник.Контрагенты as contractors ON contractors1.Ссылка = contractors.Ссылка
LEFT JOIN справочник.Контрагенты as contractors2 ON contractors1.Ссылка = contractors2.Ссылка";

const string mappings = @"Справочник.Контрагенты t1 Main
Ссылка Single id
наименованиеполное Single f1
ОбластьДанныхОсновныеДанные Single area
";

const string expectedResult =
@"select
contractors2.f1 as ContractorFullname
from t1 as contractors1
left join t1 as contractors on contractors1.area = contractors.area and contractors1.id = contractors.id
left join t1 as contractors2 on contractors1.area = contractors2.area and contractors1.id = contractors2.id
";

CheckTranslate(mappings, sourceSql, expectedResult);
}

[Test]
public void CanUseRussianSyntax()
{
Expand Down

0 comments on commit 4ac502a

Please sign in to comment.