Example #1
0
 public void Fill(ScopedTable table)
 {
     foreach (DataRow row in table.Rows)
     {
         DateTime date = row.GetDate(table.Schema);
         if (myCache.ContainsKey(date))
         {
             throw new InvalidOperationException("date is not uniq for stock and origin");
         }
         myCache[date] = row.Field <TId>(table.Schema.IdColumn);
     }
 }
Example #2
0
        /// <summary>
        /// Imports the given table into TOM.
        /// <remarks>
        /// The values of the table belong to the given origin and the given currency.
        /// All rows are owned by the given stock. The table name identifies the datum
        /// (and so the target table).
        /// </remarks>
        /// </summary>
        public static void Import(this MauiX.IImport self, StockHandle stock, DataTable table, DatumOrigin origin, Maui.Entities.Currency currency)
        {
            // XXX: how to handle origin and currency if we allow "merged results"?
            //  then we actually already need to enrich the result table from the policy
            //  with origin and currency ...

            var tomScripting = Engine.ServiceProvider.TomScripting();

            var datum = table.TableName;

            using (TransactionScope trans = new TransactionScope())
            {
                // we have a datum so we need the table now
                var mgr = tomScripting.GetManager(datum);
                if (mgr == null)
                {
                    throw new Exception("No table found for datum: " + datum);
                }

                // get scoped data for owner id
                long        ownerId = stock.GetId(mgr.Schema.OwnerIdColumn);
                ScopedTable output  = mgr.Query(ownerId);

                var resultColumns = table.Columns.ToSet();
                var dateCol       = resultColumns.FirstOrDefault(c => c.IsDateColumn());

                // setting date and values depend on the format
                Action <DataRow, DataRow> SetValues = null;
                // default date setter: assume there is no date
                Action <DataRow, DataRow> SetDate = (dest, src) => { };
                if (dateCol != null)
                {
                    SetDate = (dest, src) => dest.SetDate(mgr.Schema, src.GetDate(dateCol.ColumnName));
                }

                var datumCols = output.Schema.DatumColumns
                                .Where(col => resultColumns.Any(c => c.ColumnName.Equals(col.ColumnName, StringComparison.OrdinalIgnoreCase)));
                SetValues = (dest, src) =>
                {
                    foreach (var col in datumCols)
                    {
                        dest[col.ColumnName] = src[col.ColumnName];
                    }
                };

                // ok - now import the data

                DateIdCache <long> cache = new DateIdCache <long>();
                if (output.Schema.DateColumn != null)
                {
                    cache.Fill(output);
                }

                foreach (DataRow row in table.Rows)
                {
                    var newRow = output.NewRow();

                    // set owner id
                    newRow[output.Schema.OwnerIdColumn] = ownerId;

                    // a "datum" always need a date
                    SetDate(newRow, row);

                    // set values
                    SetValues(newRow, row);

                    // set currency (the value is of no use when there is no currency)
                    // -> currency might implicitly available through TradedStock->StockExchange
                    if (output.Schema.CurrencyColumn != null)
                    {
                        newRow[output.Schema.CurrencyColumn] = currency.Id;
                    }

                    // set origin (optional)
                    if (output.Schema.OriginColumn != null)
                    {
                        newRow[output.Schema.OriginColumn] = origin.Id;
                    }

                    if (output.Schema.DateColumn != null && cache.Contains(newRow.GetDate(output.Schema)))
                    {
                        long id = cache[newRow.GetDate(output.Schema)];
                        output.Update(id, newRow);
                    }
                    else
                    {
                        output.Add(newRow);
                    }
                }

                trans.Complete();
            }
        }
Example #3
0
        protected void AddRow( ScopedTable table, long id, DateTime date, int value )
        {
            DataRow row = table.NewRow();
            row[ table.Schema.OwnerIdColumn ] = id;
            row.SetDate( table.Schema, date );
            row[ "Value" ] = value;

            table.Add( row );
        }
Example #4
0
        protected void AddRow( ScopedTable table, long id, string value )
        {
            DataRow row = table.NewRow();
            row[ table.Schema.OwnerIdColumn ] = id;
            row[ "Value" ] = value;

            table.Add( row );
        }
Example #5
0
        public void NotNullIsNull()
        {
            var col = new DataColumn( "stock_id", typeof( long ) );
            col.AllowDBNull = false;

            TableSchema schema = new TableSchema( "test1",
                col,
                new DataColumn( "value", typeof( double ) ) );

            DataTable t = schema.NewTempTable();

            ScopedTable table = new ScopedTable( schema, t, null );

            DataRow row = table.NewRow();
            row[ "value" ] = 23.0d;

            table.Add( row );
        }
Example #6
0
 private void AddStockPrice( ScopedTable table, string date, double value )
 {
     DataRow row = table.NewRow();
     row[ "traded_stock_id" ] = CurrentTradedStockId;
     row.SetDate( table.Schema, DateTime.Parse( date ) );
     row[ "close" ] = value;
     table.Add( row );
 }
Example #7
0
        private void AddRow( ScopedTable table, long ownerId, string date, long origin, double value )
        {
            DataRow row = table.NewRow();
            row[ table.Schema.OwnerIdColumn ] = ownerId;
            row.SetDate( table.Schema, DateTime.Parse( date ) );
            row[ table.Schema.OriginColumn ] = origin;
            row[ "value" ] = value;

            table.AddOrUpdate( row );
        }
Example #8
0
        public void SetTimestamp()
        {
            TableSchema schema = new TableSchema( "test1",
                new DataColumn( "stock_id", typeof( long ) ),
                new DataColumn( "timestamp", typeof( string ) ),
                new DataColumn( "value", typeof( double ) ) );

            DataTable t = schema.NewTempTable();

            ScopedTable table = new ScopedTable( schema, t, null );

            DataRow row = table.NewRow();
            row[ "stock_id" ] = 1;
            row[ "value" ] = 23.0d;

            table.Add( row );

            Assert.IsTrue( DateTime.Now.AlmostEquals( row.GetDate( "timestamp" ), 1 ) );
        }
Example #9
0
        public void OriginRankingWithMerge()
        {
            TableSchema schema = new TableSchema( "OriginTest",
                new DataColumn( "stock_id", typeof( long ) ),
                new DataColumn( "date", typeof( string ) ),
                new DataColumn( "datum_origin_id", typeof( long ) ),
                new DataColumn( "value", typeof( double ) ) );

            DataTable t = schema.NewTempTable();

            var originClause = new OriginClause( true, 2, 1 );

            ScopedTable table = new ScopedTable( schema, t, originClause );

            AddRow( table, 1, "2002-01-02 00:00", 1, 2.1d );
            AddRow( table, 1, "2002-01-02 00:00", 2, 2.2d );
            AddRow( table, 1, "2002-01-02 00:00", 3, 2.3d );
            AddRow( table, 1, "2003-01-02 00:00", 1, 3.1d );
            AddRow( table, 1, "2003-01-02 00:00", 3, 3.3d );
            AddRow( table, 1, "2004-01-02 00:00", 3, 4.3d );

            var rows = table.Rows.ToList();
            Assert.AreEqual( 3, rows.Count );
            Assert.AreEqual( 2.2d, (double)rows[ 0 ][ "value" ], 0.000001d );
            Assert.AreEqual( 3.1d, (double)rows[ 1 ][ "value" ], 0.000001d );
            Assert.AreEqual( 4.3d, (double)rows[ 2 ][ "value" ], 0.000001d );
        }
Example #10
0
        public void AddOrUpdateWithDate()
        {
            TableSchema schema = new TableSchema( "test1",
                new DataColumn( "stock_id", typeof( long ) ),
                new DataColumn( "date", typeof( string ) ),
                new DataColumn( "value", typeof( double ) ) );

            DataTable t = schema.NewTempTable();

            ScopedTable table = new ScopedTable( schema, t, null );

            // initial row
            {
                DataRow row = table.NewRow();
                row[ "stock_id" ] = 1;
                row.SetDate( schema, DateTime.Parse( "2002-01-01 00:00" ) );
                row[ "value" ] = 23.0d;

                table.AddOrUpdate( row );
            }

            // same id, same date => update
            {
                DataRow row = table.NewRow();
                row[ "stock_id" ] = 1;
                row.SetDate( schema, DateTime.Parse( "2002-01-01 00:00" ) );
                row[ "value" ] = 42.0d;

                table.AddOrUpdate( row );
            }

            // same id, new date => add
            {
                DataRow row = table.NewRow();
                row[ "stock_id" ] = 1;
                row.SetDate( schema, DateTime.Parse( "2002-01-02 00:00" ) );
                row[ "value" ] = 25.0d;

                table.AddOrUpdate( row );
            }

            // new stock_id, same date => add
            {
                DataRow row = table.NewRow();
                row[ "stock_id" ] = 2;
                row.SetDate( schema, DateTime.Parse( "2002-01-02 00:00" ) );
                row[ "value" ] = 37.0d;

                table.AddOrUpdate( row );
            }

            var rows = table.Rows.ToList();
            Assert.AreEqual( 3, rows.Count );

            Assert.AreEqual( 1, rows[ 0 ][ schema.OwnerIdColumn ] );
            Assert.AreEqual( 42.0d, rows[ 0 ][ "value" ] );

            Assert.AreEqual( 1, rows[ 1 ][ schema.OwnerIdColumn ] );
            Assert.AreEqual( 25.0d, rows[ 1 ][ "value" ] );

            Assert.AreEqual( 2, rows[ 2 ][ schema.OwnerIdColumn ] );
            Assert.AreEqual( 37.0d, rows[ 2 ][ "value" ] );
        }
Example #11
0
        public void NotNullOk()
        {
            var col = new DataColumn( "stock_id", typeof( long ) );
            col.AllowDBNull = false;

            TableSchema schema = new TableSchema( "test1",
                col,
                new DataColumn( "value", typeof( double ) ) );

            DataTable t = schema.NewTempTable();

            ScopedTable table = new ScopedTable( schema, t, null );

            DataRow row = table.NewRow();
            row[ "stock_id" ] = 1;
            row[ "value" ] = 23.0d;

            table.Add( row );

            var rows = table.Rows.ToList();
            Assert.AreEqual( 1, rows.Count );
            Assert.AreEqual( 1, row[ "stock_id" ] );
            Assert.AreEqual( 23.0d, row[ "value" ] );
        }