Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
olmobrutall committed Jan 24, 2020
2 parents 6779439 + 31e7728 commit 9cd36ef
Show file tree
Hide file tree
Showing 16 changed files with 170 additions and 76 deletions.
2 changes: 1 addition & 1 deletion Signum.Engine/Engine/SchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class SchemaGenerator
Schema s = Schema.Current;
List<ITable> tables = s.GetDatabaseTables().Where(t => !s.IsExternalDatabase(t.Name.Schema.Database)).ToList();

SqlPreCommand? createTables = tables.Select(SqlBuilder.CreateTableSql).Combine(Spacing.Double)?.PlainSqlCommand();
SqlPreCommand? createTables = tables.Select(t => SqlBuilder.CreateTableSql(t)).Combine(Spacing.Double)?.PlainSqlCommand();

SqlPreCommand? foreignKeys = tables.Select(SqlBuilder.AlterTableForeignKeys).Combine(Spacing.Double)?.PlainSqlCommand();

Expand Down
6 changes: 3 additions & 3 deletions Signum.Engine/Engine/SchemaSynchronizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public static class SchemaSynchronizer
removeOld: (tn, dif) => SqlBuilder.DropTable(dif),
mergeBoth: (tn, tab, dif) =>
{
var rename = !object.Equals(dif.Name, tab.Name) ? SqlBuilder.RenameOrMove(dif, tab) : null;
var rename = !object.Equals(dif.Name, tab.Name) ? SqlBuilder.RenameOrMove(dif, tab, tab.Name) : null;

bool disableEnableSystemVersioning = false;

Expand Down Expand Up @@ -253,7 +253,7 @@ public static class SchemaSynchronizer

difCol.Name == tabCol.Name ? null : SqlBuilder.RenameColumn(tab.Name, difCol.Name, tabCol.Name),

difCol.ColumnEquals(tabCol, ignorePrimaryKey: true, ignoreIdentity: false, ignoreGenerateAlways: false) ?
difCol.ColumnEquals(tabCol, ignorePrimaryKey: true, ignoreIdentity: false, ignoreGenerateAlways: true) ?
null :
SqlPreCommand.Combine(Spacing.Simple,
tabCol.PrimaryKey && !difCol.PrimaryKey && dif.PrimaryKeyName != null ? SqlBuilder.DropPrimaryKeyConstraint(tab.Name) : null,
Expand Down Expand Up @@ -342,7 +342,7 @@ public static class SchemaSynchronizer
SqlPreCommand? historyTables = Synchronizer.SynchronizeScript(Spacing.Double, modelTablesHistory, databaseTablesHistory,
createNew: null,
removeOld: (tn, dif) => SqlBuilder.DropTable(dif.Name),
mergeBoth: (tn, tab, dif) => !object.Equals(dif.Name, tab.SystemVersioned!.TableName) ? SqlBuilder.RenameOrChangeSchema(dif.Name, tab.SystemVersioned!.TableName) : null);
mergeBoth: (tn, tab, dif) => !object.Equals(dif.Name, tab.SystemVersioned!.TableName) ? SqlBuilder.RenameOrMove(dif, tab, tab.SystemVersioned!.TableName) : null);

SqlPreCommand? syncEnums = SynchronizeEnumsScript(replacements);

Expand Down
43 changes: 23 additions & 20 deletions Signum.Engine/Engine/SqlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,23 @@ public static class SqlBuilder
};

#region Create Tables
public static SqlPreCommandSimple CreateTableSql(ITable t)
public static SqlPreCommandSimple CreateTableSql(ITable t, ObjectName? tableName = null, bool avoidSystemVersioning = false)
{
var primaryKeyConstraint = t.PrimaryKey == null ? null : "CONSTRAINT {0} PRIMARY KEY CLUSTERED ({1} ASC)".FormatWith(PrimaryClusteredIndex.GetPrimaryKeyName(t.Name), t.PrimaryKey.Name.SqlEscape());
var primaryKeyConstraint = t.PrimaryKey == null || t.SystemVersioned != null && tableName != null && t.SystemVersioned.TableName.Equals(tableName) ?
null : "CONSTRAINT {0} PRIMARY KEY CLUSTERED ({1} ASC)".FormatWith(PrimaryClusteredIndex.GetPrimaryKeyName(t.Name), t.PrimaryKey.Name.SqlEscape());

var systemPeriod = t.SystemVersioned == null ? null : Period(t.SystemVersioned);
var systemPeriod = t.SystemVersioned == null || avoidSystemVersioning ? null : Period(t.SystemVersioned);

var columns = t.Columns.Values.Select(c => SqlBuilder.CreateColumn(c, GetDefaultConstaint(t, c), isChange: false))
var columns = t.Columns.Values.Select(c => SqlBuilder.ColumnLine(c, GetDefaultConstaint(t, c), isChange: false, forHistoryTable: avoidSystemVersioning))
.And(primaryKeyConstraint)
.And(systemPeriod)
.NotNull()
.ToString(",\r\n");

var systemVersioning = t.SystemVersioned == null ? null :
$"\r\nWITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = {t.SystemVersioned.TableName}))";
var systemVersioning = t.SystemVersioned == null || avoidSystemVersioning ? null :
$"\r\nWITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = {t.SystemVersioned.TableName.OnDatabase(null)}))";

return new SqlPreCommandSimple($"CREATE TABLE {t.Name}(\r\n{columns}\r\n)" + systemVersioning);
return new SqlPreCommandSimple($"CREATE TABLE {tableName ?? t.Name}(\r\n{columns}\r\n)" + systemVersioning);
}

public static SqlPreCommand DropTable(DiffTable diffTable)
Expand All @@ -53,7 +54,6 @@ public static SqlPreCommand DropTable(DiffTable diffTable)
return SqlPreCommandConcat.Combine(Spacing.Simple,
AlterTableDisableSystemVersioning(diffTable.Name),
DropTable(diffTable.Name)
//DropTable(diffTable.TemporalTableName)
)!;
}

Expand Down Expand Up @@ -95,7 +95,7 @@ public static SqlPreCommand AlterTableDropPeriod(ITable table)

public static SqlPreCommand AlterTableEnableSystemVersioning(ITable table)
{
return new SqlPreCommandSimple($"ALTER TABLE {table.Name} SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = {table.SystemVersioned!.TableName}))");
return new SqlPreCommandSimple($"ALTER TABLE {table.Name} SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = {table.SystemVersioned!.TableName.OnDatabase(null)}))");
}

public static SqlPreCommandSimple AlterTableDisableSystemVersioning(ObjectName tableName)
Expand All @@ -110,7 +110,7 @@ public static SqlPreCommand AlterTableDropColumn(ITable table, string columnName

public static SqlPreCommand AlterTableAddColumn(ITable table, IColumn column, SqlBuilder.DefaultConstraint? tempDefault = null)
{
return new SqlPreCommandSimple("ALTER TABLE {0} ADD {1}".FormatWith(table.Name, CreateColumn(column, tempDefault ?? GetDefaultConstaint(table, column), isChange: false)));
return new SqlPreCommandSimple("ALTER TABLE {0} ADD {1}".FormatWith(table.Name, ColumnLine(column, tempDefault ?? GetDefaultConstaint(table, column), isChange: false)));
}

public static SqlPreCommand AlterTableAddOldColumn(ITable table, DiffColumn column)
Expand Down Expand Up @@ -167,7 +167,7 @@ public static bool IsDate(SqlDbType sqlDbType)

public static SqlPreCommand AlterTableAlterColumn(ITable table, IColumn column, string? defaultConstraintName = null, ObjectName? forceTableName = null)
{
var alterColumn = new SqlPreCommandSimple("ALTER TABLE {0} ALTER COLUMN {1}".FormatWith(forceTableName ?? table.Name, CreateColumn(column, null, isChange: true)));
var alterColumn = new SqlPreCommandSimple("ALTER TABLE {0} ALTER COLUMN {1}".FormatWith(forceTableName ?? table.Name, ColumnLine(column, null, isChange: true)));

if (column.Default == null)
return alterColumn;
Expand Down Expand Up @@ -224,11 +224,11 @@ public static string CreateOldColumn(DiffColumn c)
);
}

public static string CreateColumn(IColumn c, DefaultConstraint? constraint, bool isChange)
public static string ColumnLine(IColumn c, DefaultConstraint? constraint, bool isChange, bool forHistoryTable = false)
{
string fullType = GetColumnType(c);

var generatedAlways = c is SystemVersionedInfo.Column svc ?
var generatedAlways = c is SystemVersionedInfo.Column svc && !forHistoryTable ?
$"GENERATED ALWAYS AS ROW {(svc.SystemVersionColumnType == SystemVersionedInfo.ColumnType.Start ? "START" : "END")} HIDDEN" :
null;

Expand All @@ -237,7 +237,7 @@ public static string CreateColumn(IColumn c, DefaultConstraint? constraint, bool
return $" ".Combine(
c.Name.SqlEscape(),
fullType,
c.Identity && !isChange ? "IDENTITY " : null,
c.Identity && !isChange && !forHistoryTable ? "IDENTITY " : null,
generatedAlways,
c.Collation != null ? ("COLLATE " + c.Collation) : null,
c.Nullable.ToBool() ? "NULL" : "NOT NULL",
Expand Down Expand Up @@ -505,18 +505,18 @@ public static SqlPreCommand RenameOrChangeSchema(ObjectName oldTableName, Object
oldNewSchema.Equals(newTableName) ? null : RenameTable(oldNewSchema, newTableName.Name))!;
}

public static SqlPreCommand RenameOrMove(DiffTable oldTable, ITable newTable)
public static SqlPreCommand RenameOrMove(DiffTable oldTable, ITable newTable, ObjectName newTableName)
{
if (object.Equals(oldTable.Name.Schema.Database, newTable.Name.Schema.Database))
return RenameOrChangeSchema(oldTable.Name, newTable.Name);
if (object.Equals(oldTable.Name.Schema.Database, newTableName.Schema.Database))
return RenameOrChangeSchema(oldTable.Name, newTableName);

return SqlPreCommand.Combine(Spacing.Simple,
CreateTableSql(newTable),
MoveRows(oldTable.Name, newTable.Name, newTable.Columns.Keys),
CreateTableSql(newTable, newTableName, avoidSystemVersioning: true),
MoveRows(oldTable.Name, newTableName, newTable.Columns.Keys, avoidIdentityInsert: newTable.SystemVersioned != null && newTable.SystemVersioned.Equals(newTable)),
DropTable(oldTable))!;
}

public static SqlPreCommand MoveRows(ObjectName oldTable, ObjectName newTable, IEnumerable<string> columnNames)
public static SqlPreCommand MoveRows(ObjectName oldTable, ObjectName newTable, IEnumerable<string> columnNames, bool avoidIdentityInsert = false)
{
SqlPreCommandSimple command = new SqlPreCommandSimple(
@"INSERT INTO {0} ({2})
Expand All @@ -527,6 +527,9 @@ public static SqlPreCommand MoveRows(ObjectName oldTable, ObjectName newTable, I
columnNames.ToString(a => a.SqlEscape(), ", "),
columnNames.ToString(a => "[table]." + a.SqlEscape(), ", ")));

if (avoidIdentityInsert)
return command;

return SqlPreCommand.Combine(Spacing.Simple,
new SqlPreCommandSimple("SET IDENTITY_INSERT {0} ON".FormatWith(newTable)) { GoBefore = true },
command,
Expand Down
1 change: 1 addition & 0 deletions Signum.Engine/Linq/DbExpressions.Sql.cs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ internal enum SqlFunction
CONVERT,
ISNULL,
STUFF,
COLLATE,
}

internal enum SqlEnums
Expand Down
15 changes: 15 additions & 0 deletions Signum.Engine/Linq/ExpressionVisitor/DbExpressionNominator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,8 @@ protected override Expression VisitMember(MemberExpression m)
return TryLike(m.GetArgument("str"), m.GetArgument("pattern"));
case "StringExtensions.Etc":
return TryEtc(m.GetArgument("str"), m.GetArgument("max"), m.TryGetArgument("etcString"));
case "LinqHints.Collate":
return TryCollate(m.GetArgument("str"), m.GetArgument("collation"));

case "DateTime.Add":
case "DateTime.Subtract":
Expand Down Expand Up @@ -1457,6 +1459,19 @@ protected override Expression VisitMember(MemberExpression m)
Expression.Call(miEtc3, newStr, max, etcString);
}

private Expression? TryCollate(Expression str, Expression collation)
{
var newStr = Visit(str);
if (!Has(newStr))
return null;

var colStr = collation is ConstantExpression col ? (string)col.Value : null;
if (colStr == null)
return null;

return Add(new SqlFunctionExpression(typeof(string), newStr, SqlFunction.COLLATE.ToString(), new[] { newStr, new SqlConstantExpression(colStr) }));
}

static readonly MethodInfo miEtc2 = ReflectionTools.GetMethodInfo(() => "".Etc(2));
static readonly MethodInfo miEtc3 = ReflectionTools.GetMethodInfo(() => "".Etc(2, "..."));

Expand Down
33 changes: 21 additions & 12 deletions Signum.Engine/Linq/ExpressionVisitor/QueryFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -535,22 +535,31 @@ protected internal override Expression VisitAggregate(AggregateExpression aggreg

protected internal override Expression VisitSqlFunction(SqlFunctionExpression sqlFunction)
{
if (sqlFunction.Object != null)
if (sqlFunction.SqlFunction == SqlFunction.COLLATE.ToString())
{
Visit(sqlFunction.Object);
sb.Append(".");
this.Visit(sqlFunction.Arguments[0]);
sb.Append(" COLLATE ");
if (sqlFunction.Arguments[1] is SqlConstantExpression ce)
sb.Append((string)ce.Value!);
}
sb.Append(sqlFunction.SqlFunction);
sb.Append("(");
for (int i = 0, n = sqlFunction.Arguments.Count; i < n; i++)
else
{
Expression exp = sqlFunction.Arguments[i];
if (i > 0)
sb.Append(", ");
this.Visit(exp);
if (sqlFunction.Object != null)
{
Visit(sqlFunction.Object);
sb.Append(".");
}
sb.Append(sqlFunction.SqlFunction);
sb.Append("(");
for (int i = 0, n = sqlFunction.Arguments.Count; i < n; i++)
{
Expression exp = sqlFunction.Arguments[i];
if (i > 0)
sb.Append(", ");
this.Visit(exp);
}
sb.Append(")");
}
sb.Append(")");

return sqlFunction;
}

Expand Down
8 changes: 4 additions & 4 deletions Signum.Engine/Operations/GraphState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Signum.Engine.Operations
{
public interface IGraphHasFromStatesOperation
public interface IGraphHasStatesOperation
{
bool HasFromStates { get; }
}
Expand All @@ -27,7 +27,7 @@ public interface IGraphToStateOperation : IGraphOperation
List<S> ToStates { get; }
}

public interface IGraphFromStatesOperation : IGraphOperation, IGraphHasFromStatesOperation
public interface IGraphFromStatesOperation : IGraphOperation, IGraphHasStatesOperation
{
List<S> FromStates { get; }
}
Expand Down Expand Up @@ -186,7 +186,7 @@ public class Execute : Graph<T>.Execute, IGraphToStateOperation, IGraphFromState
IEnumerable<Enum>? IOperation.UntypedFromStates { get { return FromStates.Cast<Enum>(); } }
Type? IOperation.StateType { get { return typeof(S); } }

bool IGraphHasFromStatesOperation.HasFromStates
bool IGraphHasStatesOperation.HasFromStates
{
get { return !FromStates.IsNullOrEmpty(); }
}
Expand Down Expand Up @@ -236,7 +236,7 @@ public class Delete : Graph<T>.Delete, IGraphOperation, IGraphFromStatesOperatio
IEnumerable<Enum>? IOperation.UntypedFromStates { get { return FromStates.Cast<Enum>(); } }
Type? IOperation.StateType { get { return typeof(S); } }

bool IGraphHasFromStatesOperation.HasFromStates
bool IGraphHasStatesOperation.HasFromStates
{
get { return !FromStates.IsNullOrEmpty(); }
}
Expand Down
2 changes: 1 addition & 1 deletion Signum.Engine/Operations/OperationLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ private static OperationInfo ToOperationInfo(IOperation oper)
CanBeModified = (oper as IEntityOperation)?.CanBeModified,
Returns = oper.Returns,
ReturnType = oper.ReturnType,
HasStates = (oper as IGraphHasFromStatesOperation)?.HasFromStates,
HasStates = (oper as IGraphHasStatesOperation)?.HasFromStates,
HasCanExecute = (oper as IEntityOperation)?.HasCanExecute,
CanBeNew = (oper as IEntityOperation)?.CanBeNew,
BaseType = (oper as IEntityOperation)?.BaseType ?? (oper as IConstructorFromManyOperation)?.BaseType
Expand Down
11 changes: 4 additions & 7 deletions Signum.Entities/Basics/Operation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,12 @@ public interface IEntityOperationSymbolContainer<in T> : IEntityOperationSymbolC
Type BaseType { get; }
}

public interface IConstructFromManySymbolContainer<in T> : IOperationSymbolContainer
where T : class, IEntity
{
Type BaseType { get; }
}


public static class ConstructSymbol<T>
where T : class, IEntity
{


public interface Simple : IOperationSymbolContainer
{
}
Expand All @@ -186,9 +182,10 @@ public interface From<in F> : IEntityOperationSymbolContainer<F>
{
}

public interface FromMany<in F> : IConstructFromManySymbolContainer<F>
public interface FromMany<in F> : IOperationSymbolContainer
where F : class, IEntity
{
Type BaseType { get; }
}
}

Expand Down
4 changes: 2 additions & 2 deletions Signum.React/Scripts/Frames/FrameModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ interface PackAndComponent {
lastEntity: string;
refreshCount: number;
getComponent: (ctx: TypeContext<ModifiableEntity>) => React.ReactElement<any>;
}
}

export const FrameModal = React.forwardRef(function FrameModal(p: FrameModalProps, ref: React.Ref<IHandleKeyboard>) {

Expand Down Expand Up @@ -188,7 +188,7 @@ export const FrameModal = React.forwardRef(function FrameModal(p: FrameModalProp
function renderBody(pc: PackAndComponent) {

const frame: EntityFrame = {
frameComponent: { forceUpdate },
frameComponent: { forceUpdate, createNew: p.createNew },
entityComponent: entityComponent.current,
onReload: (pack, reloadComponent, callback) => {
const newPack = pack || packComponent!.pack;
Expand Down
9 changes: 8 additions & 1 deletion Signum.React/Scripts/Lines/ValueLine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,13 @@ export const ValueLine = React.memo(React.forwardRef(function ValueLine(props: V
return null;

return ValueLineRenderers.renderers[c.props.valueLineType!](c);
}), (prev, next) => LineBaseController.propEquals(prev, next));
}), (prev, next) => {
if (
next.extraButtons || prev.extraButtons)
return false;

return LineBaseController.propEquals(prev, next);
});

export namespace ValueLineRenderers {
export const renderers: {
Expand All @@ -176,6 +182,7 @@ export function isNumber(e: React.KeyboardEvent<any>) {
const c = e.keyCode;
return ((c >= 48 && c <= 57) /*0-9*/ ||
(c >= 96 && c <= 105) /*NumPad 0-9*/ ||
(c == KeyCodes.enter) ||
(c == KeyCodes.backspace) ||
(c == KeyCodes.tab) ||
(c == KeyCodes.clear) ||
Expand Down
3 changes: 1 addition & 2 deletions Signum.React/Scripts/SearchControl/SearchControlLoaded.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,9 @@ export default class SearchControlLoaded extends React.Component<SearchControlLo

canFilter() {
const p = this.props;
return p.showHeader && (p.showFilterButton || p.showFilters)
return p.showHeader == true && (p.showFilterButton || p.showFilters);
}


getQueryRequest(): QueryRequest {
const fo = this.props.findOptions;
const qs = this.props.querySettings;
Expand Down
Loading

0 comments on commit 9cd36ef

Please sign in to comment.