Пример #1
0
        public override DataTable Project(ExpressionEval[] exprs)
        {
            var heading  = DataHeading.Create(exprs);
            var sql      = _gen.SelectAs(GetFrom(), exprs);
            var newtable = DataTableSql.CreateFromSql(heading, sql);

            Logger.WriteLine(4, "[Pro '{0}']", newtable);
            return(newtable);
        }
Пример #2
0
        public override DataTable Rename(ExpressionEval[] exprs)
        {
            // note: heading order must match exprs (cf Local)
            var heading  = DataHeading.Create(exprs);
            var sql      = _gen.SelectAs(GetFrom(), exprs);
            var newtable = DataTableSql.CreateFromSql(heading, sql);

            Logger.WriteLine(4, "[Ren '{0}']", newtable);
            return(newtable);
        }
Пример #3
0
 // Create a codeless expression block for renames and projects
 // Create a lookup so later code can easily track total set of inputs
 public static ExpressionBlock Create(string name, string oldname, DataType rtntype)
 {
     return(new ExpressionBlock {
         Kind = (name == oldname) ? ExpressionKinds.Project : ExpressionKinds.Rename,
         Name = name,
         ReturnType = rtntype,
         OldName = oldname,
         InternalLookup = DataHeading.Create(new DataColumn[] { DataColumn.Create(oldname, rtntype) }),
     });
 }
Пример #4
0
        public static AddinInfo[] GetAddinInfo()
        {
            var addins = new List <AddinInfo>();

            addins.Add(AddinInfo.Create("read", 1, DataTypes.Text, "Read"));
            addins.Add(AddinInfo.Create("write", 1, DataTypes.Void, "Write"));
            addins.Add(AddinInfo.Create("pause", 1, DataTypes.Void, "Pause"));
            addins.Add(AddinInfo.Create("fail", 2, DataTypes.Void, "Fail"));
            addins.Add(AddinInfo.Create("assert", 2, DataTypes.Void, "Assert"));

            addins.Add(AddinInfo.Create("type", 1, DataTypes.Text, "Type"));

            addins.Add(AddinInfo.Create("binary", 1, DataTypes.Binary, "Binary"));
            addins.Add(AddinInfo.Create("bool", 1, DataTypes.Bool, "Bool"));
            addins.Add(AddinInfo.Create("number", 1, DataTypes.Number, "Number,NumberT"));
            addins.Add(AddinInfo.Create("time", 1, DataTypes.Time, "Time,TimeD")); //FIX:
            addins.Add(AddinInfo.Create("text", 1, DataTypes.Text, "Text"));

            addins.Add(AddinInfo.Create("format", 1, DataTypes.Text, "Format"));
            addins.Add(AddinInfo.Create("pp", 1, DataTypes.Text, "PrettyPrint"));
            addins.Add(AddinInfo.Create("length", 1, DataTypes.Number, "Length"));
            addins.Add(AddinInfo.Create("fill", 2, DataTypes.Text, "Fill"));
            addins.Add(AddinInfo.Create("trim", 1, DataTypes.Text, "Trim"));
            addins.Add(AddinInfo.Create("left", 2, DataTypes.Text, "Left"));
            addins.Add(AddinInfo.Create("right", 2, DataTypes.Text, "Right"));
            addins.Add(AddinInfo.Create("before", 2, DataTypes.Text, "Before"));
            addins.Add(AddinInfo.Create("after", 2, DataTypes.Text, "After"));
            addins.Add(AddinInfo.Create("toupper", 1, DataTypes.Text, "ToUpper"));
            addins.Add(AddinInfo.Create("tolower", 1, DataTypes.Text, "ToLower"));

            addins.Add(AddinInfo.Create("bget", 1, DataTypes.Number, "BinaryGet"));
            addins.Add(AddinInfo.Create("bset", 1, DataTypes.Binary, "BinarySet"));
            addins.Add(AddinInfo.Create("blength", 1, DataTypes.Binary, "BinaryLength"));

            addins.Add(AddinInfo.Create("now", 0, DataTypes.Time, "Now"));

            addins.Add(AddinInfo.Create("date", 1, Builtin.DateValue.StaticDatatype, "FromTime"));
            addins.Add(AddinInfo.Create("dateymd", 3, Builtin.DateValue.StaticDatatype, "FromYmd"));
            addins.Add(AddinInfo.Create("year", 1, DataTypes.Number, "Year"));
            addins.Add(AddinInfo.Create("month", 1, DataTypes.Number, "Month"));
            addins.Add(AddinInfo.Create("day", 1, DataTypes.Number, "Day"));
            addins.Add(AddinInfo.Create("dow", 1, DataTypes.Number, "DayOfWeek"));
            addins.Add(AddinInfo.Create("daysdiff", 2, DataTypes.Number, "DaysDifference"));
            //addins.Add(AddinInfo.Create("time", 1, DataTypes.Time, "TimeD"));

            addins.Add(AddinInfo.Create("count", 1, DataTypes.Number, "Count"));
            addins.Add(AddinInfo.Create("degree", 1, DataTypes.Number, "Degree"));
            addins.Add(AddinInfo.Create("schema", 1, DataTypeRelation.Get(DataHeading.Create("Name:text", "Type:text")), "Schema"));
            addins.Add(AddinInfo.Create("seq", 1, DataTypeRelation.Get(DataHeading.Create("N:number")), "Sequence"));
            addins.Add(AddinInfo.Create("andl_variable", 0, DataTypeRelation.Get(Catalog.CatalogTableHeading(Catalog.CatalogTables.Variable)), "Variables"));
            addins.Add(AddinInfo.Create("andl_operator", 0, DataTypeRelation.Get(Catalog.CatalogTableHeading(Catalog.CatalogTables.Operator)), "Operators"));
            addins.Add(AddinInfo.Create("andl_member", 0, DataTypeRelation.Get(Catalog.CatalogTableHeading(Catalog.CatalogTables.Member)), "Members"));

            return(addins.ToArray());
        }
Пример #5
0
        // Create and return new type
        // Note: heading must have IsTuple=false to preserve order
        public static DataTypeCode Get(DataType type, DataColumn[] columns)
        {
            var dt = new DataTypeCode {
                Returns   = type,
                Arguments = (columns == null) ? DataHeading.Empty : DataHeading.Create(columns, false),
                Flags     = TypeFlags.Variable,
            };

            //dt.NativeType = TypeMaker.CreateType(dt); // TODO:
            return(dt);
        }
Пример #6
0
        // relation representing heading
        public RelationValue Schema(RelationValue relarg)
        {
            var heading = DataHeading.Create("Name:text", "Type:text");
            var table   = DataTableLocal.Create(heading);

            foreach (var col in relarg.Value.Heading.Columns)
            {
                table.AddRow(DataRow.Create(heading, col.Name, col.DataType.Name));
            }
            return(RelationValue.Create(table));
        }
Пример #7
0
        DataHeading Peek()
        {
            var schema = GetSchema();
            var scols  = schema.Columns;
            var cols   = Enumerable.Range(0, scols.Count)
                         .Select(x => DataColumn.Create(scols[x].ColumnName,
                                                        _type2datatype.ContainsKey(scols[x].DataType) ? _type2datatype[scols[x].DataType] : DataTypes.Text))
                         .ToArray();

            return(DataHeading.Create(cols));
        }
Пример #8
0
        // Transform tuple
        public TupleValue TransTuple(TupleValue tuparg, params CodeValue[] exprargs)
        {
            Logger.WriteLine(3, "TransTuple {0} {1}", tuparg, exprargs.Select(e => e.AsEval.Kind.ToString()).ToArray());
            var tup     = tuparg.Value;
            var exprs   = exprargs.Select(e => (e as CodeValue).AsEval).ToArray();
            var heading = DataHeading.Create(exprs);
            var tupnew  = tup.Transform(heading, exprs);

            Logger.WriteLine(3, "[TrT {0}]", tupnew);
            return(TupleValue.Create(tupnew));
        }
Пример #9
0
        // Transform plus aggregation
        public RelationValue TransAgg(RelationValue relarg, params CodeValue[] exprargs)
        {
            Logger.WriteLine(3, "TransAgg {0} {1}", relarg, exprargs.Select(e => e.AsEval.Kind.ToString()).ToArray());
            var rel     = relarg.Value;
            var exprs   = exprargs.Select(e => e.AsEval).ToArray();
            var heading = DataHeading.Create(exprs);
            var relnew  = rel.TransformAggregate(heading, exprs);

            Logger.WriteLine(3, "[TrA {0}]", relnew);
            return(RelationValue.Create(relnew));
        }
Пример #10
0
        public string CreateTable(string tablename, DataTable other)
        {
            var heading = (other.Heading.Degree == 0) ? DataHeading.Create("_dummy") : other.Heading;
            var dict    = new Dictionary <string, SubstituteDelegate> {
                { "table", (x) => tablename },
                { "coldefs", (x) => ColDefs(heading.Columns) },
                { "colnames", (x) => NameList(heading) },
            };

            return(_templater.Process("Create", dict));
        }
Пример #11
0
        public string GroupBy(DataColumn[] cols)
        {
            if (cols.Length == 0)
            {
                return("");
            }
            var dict = new Dictionary <string, SubstituteDelegate> {
                { "grpcols", (x) => NameList(DataHeading.Create(cols)) },
            };

            return(_templater.Process("GroupBy", dict));
        }
Пример #12
0
        // Transform plus window functions (which depend on ordering)
        public RelationValue TransWin(RelationValue relarg, params CodeValue[] exprargs)
        {
            Logger.WriteLine(3, "TransOrd {0} {1}", relarg, exprargs.Select(e => e.AsEval.Kind.ToString()).ToArray());
            var rel       = relarg.Value;
            var exprs     = exprargs.Select(e => (e as CodeValue).AsEval).ToArray();
            var tranexprs = exprs.Where(e => !e.IsOrder).ToArray();
            var orderexps = exprs.Where(e => e.IsOrder).ToArray();
            var heading   = DataHeading.Create(tranexprs);
            var relnew    = rel.TransformWindowed(heading, tranexprs, orderexps);

            Logger.WriteLine(3, "[TrO {0}]", relnew);
            return(RelationValue.Create(relnew));
        }
Пример #13
0
        // Transform does Rename and/or Project and/or Extend combo
        public RelationValue Transform(RelationValue relarg, params CodeValue[] exprargs)
        {
            Logger.WriteLine(3, "Transform {0} {1}", relarg, exprargs.Select(e => e.AsEval.Kind.ToString()).ToArray());
            var rel   = relarg.Value;
            var exprs = exprargs.Select(e => (e as CodeValue).AsEval).ToArray();

            Logger.Assert(!exprs.Any(e => e.HasFold), "transform folded");
            var heading = DataHeading.Create(exprs);
            var relnew  = rel.Transform(heading, exprs);

            Logger.WriteLine(3, "[Tr {0}]", relnew);
            return(RelationValue.Create(relnew));
        }
Пример #14
0
        // Project onto named columns
        public override DataTable Project(ExpressionEval[] exprs)
        {
            var newheading = DataHeading.Create(exprs);
            var newtable   = DataTableLocal.Create(newheading);
            var newexprs   = newtable.Heading.Reorder(exprs);

            foreach (var row in GetRows()) //TODO:Enumerable
            {
                newtable.AddRow(row.Transform(newheading, newexprs));
            }
            Logger.WriteLine(4, "[Project={0}]", newtable);
            return(newtable);
        }
Пример #15
0
        // sequence of integers
        public RelationValue Sequence(NumberValue countarg)
        {
            var heading = DataHeading.Create("N:number");
            var table   = DataTableLocal.Create(heading);
            var n       = Decimal.Zero;
            var count   = (int)countarg.Value;

            for (var i = 0; i < count; ++i)
            {
                table.AddRow(DataRow.Create(heading, new TypedValue[] { NumberValue.Create(n) }));
                n += 1;
            }
            return(RelationValue.Create(table));
        }
Пример #16
0
        // read a heading
        public DataHeading ReadHeading()
        {
            var istuple = _reader.ReadBoolean();
            var degree  = _reader.ReadInt32();

            Logger.Assert(degree >= 0 && degree < Persist.MaxDegree, degree);
            var cols = new List <DataColumn>();

            while (degree-- > 0)
            {
                cols.Add(ReadColumn());
            }
            return(DataHeading.Create(cols.ToArray(), istuple));
        }
Пример #17
0
        // Create and add, return new type (must not exist)
        // Note: heading must have IsTuple=false to preserve order
        public static DataTypeUser Get(string name, DataColumn[] columns)
        {
            var old = Find(name);

            if (old != null && columns.SequenceEqual(old.Heading.Columns))
            {
                return(old);
            }
            Logger.Assert(!_usertypes.ContainsKey(name), name);
            var flags = columns.Any(c => c.DataType.IsOrdered) ? TypeFlags.Ordered : TypeFlags.None;
            var dt    = DataTypeUser.Create(name, DataHeading.Create(columns, false), // preserve column order
                                            flags | TypeFlags.Variable | TypeFlags.Generated | TypeFlags.HasName | TypeFlags.HasHeading);

            _usertypes[name] = dt;
            return(dt);
        }
Пример #18
0
        static IEnumerable <DataRow> GetRows(object instance, DataColumn[] columns)
        {
            var type    = instance.GetType();
            var countpi = type.GetProperty("Count");
            var itempi  = type.GetProperty("Item");
            var count   = (int)countpi.GetValue(instance, null);

            var rows    = new List <DataRow>();
            var heading = DataHeading.Create(columns, false); // preserve column order

            for (var x = 0; x < count; ++x)
            {
                var row    = itempi.GetValue(instance, new object[] { x });
                var values = GetValues(row, columns);
                rows.Add(DataRow.Create(heading, values));
            }
            return(rows);
        }
Пример #19
0
        // Generic input
        public override DataHeading Peek(string table)
        {
            Logger.WriteLine(2, "Sql Peek '{0}'", table);
            if (table == "*")
            {
                return(Peek());
            }
            var reader = Open(table);
            var s      = Enumerable.Range(0, reader.FieldCount)
                         .Select(n => reader.GetName(n) + ":" + reader.GetDataTypeName(n)).ToArray();

            Logger.WriteLine(3, "Table {0} fields {1}", table, String.Join(",", s));
            var cols = Enumerable.Range(0, reader.FieldCount)
                       .Where(x => _convdict.ContainsKey(reader.GetDataTypeName(x)))
                       .Select(x => DataColumn.Create(reader.GetName(x), GetType(_convdict[reader.GetDataTypeName(x)])))
                       .ToArray();

            Close();
            return(DataHeading.Create(cols, false)); // preserve order
        }
Пример #20
0
        public override DataHeading Peek(string file)
        {
            var path = GetPath(file);

            if (!File.Exists(path))
            {
                return(null);
            }
            using (var rdr = new TextFieldParser(path)
            {
                TextFieldType = FieldType.Delimited,
                Delimiters = new string[] { "," },
            }) {
                var row = rdr.ReadFields();
                if (_hasid)
                {
                    row = (new string[] { "Id:number" })
                          .Concat(row).ToArray();
                }
                return(DataHeading.Create(row));
            }
        }
Пример #21
0
        ///-------------------------------------------------------------------
        ///
        /// Catalog info
        ///

        // Construct and return a heading for a database table
        // return null if not found or error
        // TODO:
        public DataHeading GetTableHeading(string table)
        {
            Tuple <string, SqlCommonType>[] columns;

            // optional sql
            var sql    = SqlGen.GetColumns(table);
            var optsql = sql.Length > 0;

            Logger.WriteLine(3, "GetTableHeading {0}", optsql ? sql : table);
            if (optsql)
            {
                Logger.WriteLine(2, ">>>(th){0}", sql);
            }
            Logger.Assert(_database != null, "gth");
            var ok = _database.GetTableColumns(optsql ? sql : table, out columns);

            if (!ok || columns.Length == 0)
            {
                return(null);
            }
            var cols = columns.Select(c => DataColumn.Create(c.Item1, FromSqlCommon[c.Item2]));

            return(DataHeading.Create(cols)); // ignore column order
        }
Пример #22
0
 static DataHeading()
 {
     Empty = DataHeading.Create(new DataColumn[0], false); // avoids assert later
 }
Пример #23
0
 public override DataHeading Peek(string file)
 {
     return(DataHeading.Create("line"));
 }