Exemplo n.º 1
0
        void WriteCreateTable(TextWriter writer, TableDataSchema table, string name = null, bool asType = false)
        {
            name = name ?? Escape(table.Name);

            var i = 0;

            if (asType)
            {
                writer.Write("CREATE TYPE {0} AS TABLE (", name);
            }
            else
            {
                writer.Write("CREATE TABLE {0} (", name);
            }

            foreach (var field in table.Fields)
            {
                writer.WriteLine(i++ > 0 ? "," : "");
                writer.Write("    [{0}] {1} {2}", field.Name,
                             GetColumnType(field.ValueType, field.FieldType == FieldType.Key ? 60 : (int?)null),
                             field.FieldType == FieldType.Key || (field.ValueType.IsValueType && Nullable.GetUnderlyingType(field.ValueType) == null) ? "NOT NULL" : "NULL");
            }

            if (table.Keys.Length > 0)
            {
                writer.WriteLine(i++ > 0 ? "," : "");
                //IGNORE_DUP_KEY to ignore hash collisions.
                writer.Write("    PRIMARY KEY CLUSTERED ({0}) WITH (IGNORE_DUP_KEY = ON)", string.Join(",", table.Keys.Select(pos => string.Format("[{0}]", pos.Value.Name))));
            }
            writer.WriteLine();

            writer.WriteLine(")");
            writer.WriteLine();
        }
Exemplo n.º 2
0
            public override ITableDataWriter CreateTableDataWriter(TableDataSchema table)
            {
                var csvTable = _owner.CreateTableData(table, Path.Combine(Directory, table.Name + ".txt"));

                AddTableData(csvTable);
                return(csvTable);
            }
Exemplo n.º 3
0
        public override ITableDataWriter CreateTableDataWriter(TableDataSchema schema)
        {
            var data = new BinaryTableData(schema, Path.Combine(Directory, schema.Name + ".bin"));

            AddTableData(data);
            return(data);
        }
Exemplo n.º 4
0
        public static string GetJoinToFactAncestor(TableDataSchema schema, string ancestorCriteria)
        {
            var sb = new StringBuilder();

            GetJoinToFactAncestor(schema, ancestorCriteria, sb);
            return(sb.ToString());
        }
Exemplo n.º 5
0
        public static void GetJoinToFactAncestor(TableDataSchema schema, string ancestorCriteria, StringBuilder sb)
        {
            var parent = schema.RelatedTables.FirstOrDefault(r => r.RelationType == RelationType.Parent);

            sb.AppendLine();
            if (parent == null)
            {
                sb.Append("WHERE ").Append(ancestorCriteria);
            }
            else
            {
                sb.AppendFormat("INNER JOIN [{0}] ON ", parent.RelatedTable.Name);
                for (var i = 0; i < parent.Fields.Count; i++)
                {
                    if (i > 0)
                    {
                        sb.Append(" AND ");
                    }
                    sb.AppendFormat("[{0}].[{1}] = [{2}].[{3}]",
                                    parent.RelatedTable.Name, parent.RelatedFields[i].Name,
                                    schema.Name, parent.Fields[i].Name);
                }

                GetJoinToFactAncestor(parent.RelatedTable, ancestorCriteria, sb);
            }
        }
 public CsvTableData(TableDataSchema schema, string path, string delimiter = "\t", CultureInfo numberCulture = null)
     : base(schema)
 {
     Path           = path;
     Delimiter      = delimiter;
     NumberCulture  = numberCulture ?? CultureInfo.CurrentCulture;
     DateFormat     = "yyyy-MM-dd";
     DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
 }
Exemplo n.º 7
0
 private WritableTableData CreateTableData(TableDataSchema schema, string path)
 {
     if (BinaryOutput)
     {
         return(new BinaryTableData(schema, path.Replace(".txt", ".bin")));
     }
     return(new CsvTableData(schema, path,
                             Delimiter, _numberCulture));
 }
Exemplo n.º 8
0
        public RowComparer(TableDataSchema schema)
        {
            foreach (var field in GetSortFields(schema))
            {
                _comparers.Add(new ComparerWrapper(field.Value.ValueType.GetComparer(),
                                                   field.Value.SortOrder == SortOrder.Descending)
                               .AsIndexed <IComparer>(field.Index));
            }

            _keys = schema.Fields.FindIndices(schema.IsKey).Select(ix => ix.Index).ToArray();
        }
Exemplo n.º 9
0
        static string GetFieldName(TableDataSchema table, string fieldName)
        {
            var field = table.Fields.FirstOrDefault(f => f.Name == fieldName);

            if (field == null)
            {
                throw new KeyNotFoundException(string.Format("No field with name '{0}' in table '{1}", fieldName, table.Name));
            }

            return(string.IsNullOrEmpty(field.FriendlyName) ? field.Name : field.FriendlyName);
        }
Exemplo n.º 10
0
        public static Tuple <TableDataSchema, PartitionField> GetPartitionField(TableDataSchema schema)
        {
            if (schema.IsCenterTable())
            {
                var field = schema.Fields.OfType <PartitionField>().FirstOrDefault();
                return(field != null ? new Tuple <TableDataSchema, PartitionField>(schema, field) : null);
            }

            var parent = schema.RelatedTables.FirstOrDefault(r => r.RelationType == RelationType.Parent);

            return(parent != null?GetPartitionField(parent.RelatedTable) : null);
        }
Exemplo n.º 11
0
        public virtual Field GetKeyField(TableDataSchema schema)
        {
            var name    = FormatName(schema, null);
            var postfix = 1;

            while (schema.Fields.Any(f => f.Name == name))
            {
                name = FormatName(schema, postfix++);
            }

            return(new Field {
                Name = name, FieldType = FieldType.Key, ValueType = typeof(TKey)
            });
        }
Exemplo n.º 12
0
        public static IEnumerable <Indexed <Field> > GetSortFields(TableDataSchema schema)
        {
            var fields = schema.Fields;

            //Order by fields where SortOrder is specified, then by keys (always order by something)
            foreach (var ix in fields.AsIndexed().OrderBy(ix => ix.Value.SortOrder == SortOrder.Unspecified).ThenBy(ix => ix.Index))
            {
                var field = ix.Value;
                if (field.SortOrder != SortOrder.Unspecified || schema.IsKey(field))
                {
                    yield return(ix);
                }
            }
        }
Exemplo n.º 13
0
        public static string GetUpdateCriteria(TableDataSchema table, Tuple <TableDataSchema, PartitionField> partitionField, bool stale, DateTime?date = null, DateTime?cutoff = null)
        {
            var field   = string.Format("[{0}].[{1}]", partitionField.Item1.Name, partitionField.Item2.Name);
            var refDate = GetPartitionDate(date ?? DateTime.UtcNow.Add(-partitionField.Item2.StaleTime), partitionField.Item2.StaleTime);

            var criteria = new StringBuilder(field);

            criteria.Append(stale ? ">=" : "<");
            criteria.AppendFormat("'{0}'", refDate.ToString("o"));

            if (cutoff.HasValue && !stale)
            {
                criteria.AppendFormat(" AND {0} >= '{1}'", field, cutoff.Value.ToString("o"));
            }

            return(GetJoinToFactAncestor(table, criteria.ToString()));
        }
Exemplo n.º 14
0
 protected virtual string FormatName(TableDataSchema schema, int?postfix)
 {
     return(schema.Name + "Id" + (postfix.HasValue ? "" + postfix : ""));
 }
Exemplo n.º 15
0
 protected TableData(TableDataSchema schema)
 {
     Schema = schema;
 }
Exemplo n.º 16
0
 static string GetFieldFriendlyName(TableDataSchema table, string fieldName)
 {
     return(GetFieldName(table, fieldName));
 }
Exemplo n.º 17
0
 string GetTransientPartitionName(TableDataSchema schema)
 {
     return(schema.Name + "_Transient");
 }
 public MergedTableData(TableDataSchema schema, IEnumerable <TableData> sources = null)
     : base(schema)
 {
     Sources = sources != null?sources.ToList() : new List <TableData>();
 }
Exemplo n.º 19
0
        static string PostFixMeasureName(TableDataSchema table, string name)
        {
            var hasParent = table.RelatedTables.Any(r => r.RelationType == RelationType.Parent);

            return(hasParent ? name + " (" + table.Name + ")" : name);
        }
 public BinaryTableData(TableDataSchema schema, string path)
     : base(schema)
 {
     Path = path;
 }
 protected WritableTableData(TableDataSchema schema) : base(schema)
 {
 }
 /// <summary>
 /// Adds a table with the specified schema to this partition and returns a <see cref="ITableDataWriter"/> for writing data to it
 /// </summary>
 /// <param name="schema">The schema to add a table for</param>
 /// <returns></returns>
 public abstract ITableDataWriter CreateTableDataWriter(TableDataSchema schema);