internal T ExecuteFunction <T>(DbTableDataReader reader, DbColumnType valueType, IEnumerable <int> collection) { dynamic oneColumnCollection = collection; //apply quantifiers here if (Query.Select.Function != TokenType.COUNT) { //get real values //not transformed, need to var recordValues = new List <object[]>(); foreach (var offset in collection) { recordValues.Add(reader.ReadRecord(offset)); } //read records return only one column oneColumnCollection = recordValues.Select(cols => cols[0]).Cast <T>(); } MethodInfo method = this.GetType().GetMethod(nameof(ApplyFunction), BindingFlags.Instance | BindingFlags.NonPublic); MethodInfo genMethod = method.MakeGenericMethod(Type.GetType($"System.{valueType}")); var result = genMethod.Invoke(this, new object[] { oneColumnCollection, Query.Select.Function }); return((T)result); }
void StoreKeyIndexedOffsets <T>(DbTableDataReader handler, DbColumn column, io.BinaryWriter writer) where T : IComparable <T> { //write row count var rows = handler.Rows <T>(column).ToList(); writer.Write(rows.Count); //write all offsets foreach (var row in rows) { writer.Write(row); } }
internal DbQueryExpressionExecuter(DbQueryHandler handler, DbQuery.ColumnOperand columnOperand, TokenType oper, DbQuery.ConstantOperand constantOperand) { if (columnOperand == null || !(Operator = oper).IsComparison() || constantOperand == null || (Handler = handler) == null) { throw new ArgumentException($"cannot execute comparison expression"); } //get the table column handler Column = handler.Database.Index(columnOperand.Column.Hash); // . handler.WhereColumns[columnOperand.Column.Hash]; //get the constant value to compare Constant = constantOperand.Evaluate(null); Reader = Handler.TableReaders.FirstOrDefault(r => r.Table == Column.Table); if (Reader == null) { throw new ArgumentException($"could not resolve data table reader for column: {Column}"); } }
/// <summary> /// /// </summary> /// <param name="forced">true to write it anyways</param> void SaveKeyIndexedKeyOffsets(bool forced = false) { foreach (var table in Tables) { var handler = DbTableDataReader.Create(this, table); foreach (var column in table.Columns.Where(col => col.Key)) { var path = $"{BinaryPath}\\{column.Hash}.offset"; if (!io.File.Exists(path) || forced) { using (var writer = new io.BinaryWriter(io.File.Create(path))) { Utils.CallGeneric( this, nameof(StoreKeyIndexedOffsets), column.TypeEnum, new object[] { handler, column, writer }, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance ); } } } } }
/// <summary> /// /// </summary> /// <param name="db"></param> /// <param name="query"></param> public DbQueryHandler(CsvDb db, DbQuery query) { if ((Database = db) == null || (Query = query) == null) { throw new ArgumentException("Cannot create query executer handler"); } //set index transformers to table columns SelectIndexColumns = new SelectColumnHandler(query.Select.Columns); //WhereColumns = new Dictionary<string, DbColumn>(); TableReaders = new List <DbTableDataReader>(); //get table data reader from FROM tables too, in case WHERE has no column foreach (var table in query.From) { if (!TableReaders.Any(t => t.Table.Name == table.Name)) { TableReaders.Add(DbTableDataReader.Create(db, table.Name, this)); } } }