public void Build(ISqExtensionBuilder builder) { var tableExpr = builder.Arguments[0].EvaluateExpression(); var tableType = ((MethodInfo)builder.Member).GetGenericArguments()[0]; var helperType = typeof(TableHelper <>).MakeGenericType(tableType); var tableHelper = (TableHelper)Activator.CreateInstance(helperType, tableExpr) !; var qualified = builder.Arguments.Length <= 1 ? TableQualification.Full : builder.GetValue <TableQualification>(1); var isExpression = builder.Member.Name == "TableExpr"; if (isExpression) { if (qualified == TableQualification.None) { builder.ResultExpression = new SqlExpression(typeof(string), tableHelper.TableName, Precedence.Primary); } else { var table = new SqlTable(tableType) { PhysicalName = tableHelper.TableName, Server = (qualified & TableQualification.ServerName) != 0 ? tableHelper.ServerName : null, Database = (qualified & TableQualification.DatabaseName) != 0 ? tableHelper.DatabaseName : null, Schema = (qualified & TableQualification.SchemaName) != 0 ? tableHelper.SchemaName : null, TableOptions = (qualified & TableQualification.TableOptions) != 0 ? tableHelper.TableOptions : TableOptions.NotSet, }; builder.ResultExpression = table; } } else { var name = tableHelper.TableName; if (qualified != TableQualification.None) { var sb = new StringBuilder(); builder.DataContext.CreateSqlProvider().ConvertTableName(sb, (qualified & TableQualification.ServerName) != 0 ? tableHelper.ServerName : null, (qualified & TableQualification.DatabaseName) != 0 ? tableHelper.DatabaseName : null, (qualified & TableQualification.SchemaName) != 0 ? tableHelper.SchemaName : null, name, (qualified & TableQualification.TableOptions) != 0 ? tableHelper.TableOptions : TableOptions.NotSet); name = sb.ToString(); } builder.ResultExpression = new SqlValue(name); } }
public void Build(ISqExtensionBuilder builder) { var tableExpr = builder.GetExpression(0); SqlTable?sqlTable = null; if (tableExpr is SqlTable t) { sqlTable = t; } else if (tableExpr is SqlField field) { sqlTable = field.Table as SqlTable; } else if (tableExpr is SqlColumn column) { sqlTable = QueryHelper.GetUnderlyingField(column)?.Table as SqlTable; } //TODO: review, maybe we need here TableSource if (sqlTable == null) { throw new LinqToDBException("Can not find Table associated with expression"); } var qualified = builder.Arguments.Length <= 1 ? TableQualification.Full : builder.GetValue <TableQualification>(1); var isExpression = builder.Member.Name == "TableExpr"; var name = sqlTable.PhysicalName !; if (qualified != TableQualification.None) { var sb = new StringBuilder(); builder.DataContext.CreateSqlProvider().ConvertTableName(sb, (qualified & TableQualification.ServerName) != 0 ? sqlTable.Server : null, (qualified & TableQualification.DatabaseName) != 0 ? sqlTable.Database : null, (qualified & TableQualification.SchemaName) != 0 ? sqlTable.Schema : null, sqlTable.PhysicalName !); name = sb.ToString(); } builder.ResultExpression = isExpression ? new SqlExpression(name, Precedence.Primary) : (ISqlExpression) new SqlValue(name); }