internal Func <FieldReader, object> GetRowReader() { ParameterExpression reader = Expression.Parameter(typeof(FieldReader)); var tupleConstructor = TupleReflection.TupleChainConstructor( table.Columns.Values.Select((c, i) => FieldReader.GetExpression(reader, i, GetColumnType(c))) ); return(Expression.Lambda <Func <FieldReader, object> >(tupleConstructor, reader).Compile()); }
public Expression GetColumnExpression(Expression row, Alias alias, string name, Type type) { if (alias != Alias) { throw new InvalidOperationException("alias '{0}' not found".FormatWith(alias)); } int position = Positions.GetOrThrow(name, "column name '{0}' not found in alias '" + alias + "'"); return(FieldReader.GetExpression(Expression.Property(row, miReader), position, type)); }
protected override Expression VisitMember(MemberExpression node) { if (node.Expression == param) { var field = table.GetField(node.Member); var column = GetColumn(field); columns.Add(column); return(FieldReader.GetExpression(reader, columns.Count - 1, column.Type)); } return(base.VisitMember(node)); }
protected override Expression VisitMember(MemberExpression node) { if (node.Expression == param) { var field = table.GetField(node.Member); var column = GetColumn(field); columns.Add(column); return(FieldReader.GetExpression(reader, columns.Count - 1, column.Type)); } if (node.Expression is MethodCallExpression me && me.Method.IsInstantiationOf(miMixin)) { var type = me.Method.GetGenericArguments()[0]; var mixin = table.Mixins !.GetOrThrow(type); var field = mixin.GetField(node.Member); var column = GetColumn(field); columns.Add(column); return(FieldReader.GetExpression(reader, columns.Count - 1, column.Type)); } return(base.VisitMember(node)); }
public static Expression GetToString(Table table, ParameterExpression reader, List <IColumn> columns) { LambdaExpression lambda = ExpressionCleaner.GetFieldExpansion(table.Type, CachedTableBase.ToStringMethod) !; if (lambda == null) { columns.Add(table.ToStrColumn !); return(FieldReader.GetExpression(reader, columns.Count - 1, typeof(string))); } ToStringExpressionVisitor toStr = new ToStringExpressionVisitor( lambda.Parameters.SingleEx(), reader, columns, table ); var result = toStr.Visit(lambda.Body); return(result); }
public override void SchemaCompleted() { List <IColumn> columns = new List <IColumn> { table.PrimaryKey }; ParameterExpression reader = Expression.Parameter(typeof(FieldReader)); var expression = ToStringExpressionVisitor.GetToString(table, reader, columns); //Query using (ObjectName.OverrideOptions(new ObjectNameOptions { AvoidDatabaseName = true })) { string select = "SELECT {0}\r\nFROM {1} {2}\r\n".FormatWith( columns.ToString(c => currentAlias + "." + c.Name.SqlEscape(), ", "), table.Name.ToString(), currentAlias.ToString()); select += this.lastPartialJoin + currentAlias + "." + table.PrimaryKey.Name.SqlEscape() + "\r\n" + this.remainingJoins; query = new SqlPreCommandSimple(select); } //Reader { var kvpConstructor = Expression.New(CachedTableConstructor.ciKVPIntString, CachedTableConstructor.NewPrimaryKey(FieldReader.GetExpression(reader, 0, this.table.PrimaryKey.Type)), expression); rowReader = Expression.Lambda <Func <FieldReader, KeyValuePair <PrimaryKey, string> > >(kvpConstructor, reader).Compile(); } toStrings = new ResetLazy <Dictionary <PrimaryKey, string> >(() => { CacheLogic.AssertSqlDependencyStarted(); var connector = (SqlConnector)Connector.Current; var subConnector = connector.ForDatabase(table.Name.Schema?.Database); Dictionary <PrimaryKey, string> result = new Dictionary <PrimaryKey, string>(); using (MeasureLoad()) using (Connector.Override(subConnector)) using (Transaction tr = Transaction.ForceNew(IsolationLevel.ReadCommitted)) { if (CacheLogic.LogWriter != null) { CacheLogic.LogWriter.WriteLine("Load {0}".FormatWith(GetType().TypeName())); } ((SqlConnector)Connector.Current).ExecuteDataReaderOptionalDependency(query, OnChange, fr => { var kvp = rowReader(fr); result[kvp.Key] = kvp.Value; }); tr.Commit(); } return(result); }, mode: LazyThreadSafetyMode.ExecutionAndPublication); if (this.subTables != null) { foreach (var item in this.subTables) { item.SchemaCompleted(); } } }