Example #1
0
 public static IEnumerable <IBedRecord> EnumRows(this IBedReader reader)
 {
     while (reader.Read())
     {
         yield return(reader);
     }
 }
Example #2
0
        public override void WriteContent(string filename, IDatabaseSource db, Dictionary <string, string> vars, Dictionary <string, object> extnames)
        {
            var fw = DataStore as ITabularDataOuputStream;

            if (fw == null)
            {
                throw new InternalError(String.Format("DAE-00060 Cannot write data to {0}", DataStore));
            }

            using (StreamWriter sw = new StreamWriter(filename))
            {
                using (DbCommand cmd = db.Connection.SystemConnection.CreateCommand())
                {
                    cmd.CommandText = Sql.ReplaceAll(vars);
                    using (IBedReader reader = db.GetAnyDDA().AdaptReader(cmd.ExecuteReader()))
                    {
                        ITableStructure table = reader.Structure;
                        //Path.GetFileNameWithoutExtension(filename));
                        object manager = null;
                        fw.WriteStart(sw, table, ref manager);
                        int index = 0;
                        while (reader.Read())
                        {
                            fw.WriteRecord(sw, table, reader, index, manager);
                            index++;
                        }
                        fw.WriteEnd(sw, table, manager);
                    }
                }
            }
        }
Example #3
0
 public InMemoryTable(ITableStructure table, IBedReader reader)
 {
     Initialize();
     m_structure = new TableStructure(table);
     while (reader.Read())
     {
         m_rows.Add(new ArrayDataRecord(reader));
     }
 }
Example #4
0
        internal override void Run(TemplateEnviroment env)
        {
            LanguageQueryFunc func      = (LanguageQueryFunc)m_engine.DefaultModule.Globals["query"];
            IDatabaseSource   dbcontext = (IDatabaseSource)m_engine.DefaultModule.Globals["dbcontext"];
            string            sql       = MakeSqlSubs(m_select, m_engine);
            IBedReader        reader    = dbcontext.GetAnyDDA().AdaptReader(func(sql));

            reader.RunForEachRecordAndDispose(dbcontext.Dialect.DialectCaps.MARS, (rec, index) => ProcessRecord(rec, env, index));
        }
Example #5
0
        private List <string> DoCreateUpdateQuery(string askquery)
        {
            // buffer for update command
            var dh  = CreateHolderDumper();
            var dmp = dh.Dumper;

            using (var cmd = m_syn.m_source.Connection.SystemConnection.CreateCommand())
            {
                cmd.CommandText = askquery;

                using (IBedReader reader = m_syn.m_source.GetAnyDDA().AdaptReader(cmd.ExecuteReader()))
                {
                    while (reader.Read())
                    {
                        IBedRecord rec = reader;
                        foreach (var recada in m_outputAdapters)
                        {
                            rec = recada.AdaptRecord(rec, m_syn.Progress);
                        }

                        dmp.Put("update %f set ", m_item.Target.Table);
                        for (int i = 0; i < m_dstInfo.DataCols.Length; i++)
                        {
                            rec.ReadValue(i + m_dstInfo.KeyCols.Length);
                            if (i > 0)
                            {
                                dmp.Put(", ");
                            }
                            dmp.Put("%i=%v", m_dstInfo.DataCols[i], rec);
                        }
                        dmp.Put(" where ");
                        for (int i = 0; i < m_dstInfo.KeyCols.Length; i++)
                        {
                            if (i > 0)
                            {
                                dmp.Put(" and ");
                            }
                            rec.ReadValue(i);
                            if (rec.GetFieldType() == TypeStorage.Null)
                            {
                                dmp.Put("%i ^is ^null", m_dstInfo.KeyCols[i]);
                            }
                            else
                            {
                                dmp.Put("%i=%v", m_dstInfo.KeyCols[i], rec);
                            }
                        }
                        dmp.EndCommand();
                    }
                }
            }
            return(dh.Scripts);
        }
Example #6
0
        public static BedTable ToBinaryTable(this IBedReader reader, int?maximumRecords)
        {
            //ITableStructure ts = reader.GetTableStructure();
            BedTable dt         = new BedTable(reader.Structure);
            int      allow_recs = maximumRecords != null ? maximumRecords.Value : -1;

            while (reader.Read() && (maximumRecords == null || allow_recs > 0))
            {
                dt.AddRow(reader);
                allow_recs--;
            }
            return(dt);
        }
Example #7
0
        private void FillGridTable(IEnumerable <string[]> keys, GridTable grid, IDatabaseSource conn, IDataSynAdapter sada, SynSourceInfo info)
        {
            string query = CreateQuery(keys, conn, sada, info);

            using (var cmd = conn.Connection.SystemConnection.CreateCommand())
            {
                cmd.CommandText = query;

                using (IBedReader reader = conn.GetAnyDDA().AdaptReader(cmd.ExecuteReader()))
                {
                    grid.Fill(reader);
                }
            }
        }
Example #8
0
        private void DoLoadFootprint(IDatabaseSource conn, SynSourceInfo info, string cacheFile)
        {
            // HACK: external sorting must be implemented
            using (var fw = new FileStream(cacheFile, FileMode.Create))
            {
                var fts = new List <SynFootprint>();
                using (var bw = new BinaryWriter(fw))
                {
                    var sada = conn.Dialect.CreateDataSynAdapter();
                    if (sada == null)
                    {
                        throw new ExpectedError("DAE-00365 " + Texts.Get("s_dialect_doesnt_support_sync$dialect", "dialect", conn.Dialect.DisplayName));
                    }
                    var qbuf  = new StringWriter();
                    var dmp   = conn.Dialect.CreateDumper(qbuf);
                    var qtype = m_options.Update == true ? SynQueryType.SelectKeyMd5 : SynQueryType.SelectKeyNull;
                    info.Query.GenerateSql(dmp, sada, info, qtype, d2 =>
                    {
                        if (!info.SqlCondition.IsEmpty())
                        {
                            d2.Put(" ^where ");
                            d2.WriteRaw(info.SqlCondition);
                        }
                    });
                    using (var cmd = conn.Connection.SystemConnection.CreateCommand())
                    {
                        cmd.CommandText = qbuf.ToString();

                        using (IBedReader reader = conn.GetAnyDDA().AdaptReader(cmd.ExecuteReader()))
                        {
                            while (reader.Read())
                            {
                                var ft = SynFootprint.FromReader(reader, info.KeyCols.Length, m_holder, m_conv, sada);
                                fts.Add(ft);
                            }
                        }
                    }

                    fts.Sort();
                    foreach (var ft in fts)
                    {
                        bw.Write((byte)0);
                        ft.SaveToStream(bw);
                    }
                    bw.Write((byte)1);
                }
            }
        }
Example #9
0
 internal void DoRun(IDatabaseSource db)
 {
     if (!Sql.IsEmpty())
     {
         // run SQL and export for each file
         using (DbCommand cmd = db.Connection.SystemConnection.CreateCommand())
         {
             cmd.CommandText = Sql;
             IBedReader reader = db.GetAnyDDA().AdaptReader(cmd.ExecuteReader());
             reader.RunForEachRecordAndDispose(db.Connection.Dialect.DialectCaps.MARS, (rec, index) => ProcessRecord(db, rec));
         }
     }
     else
     {
         WriteContent(db, FileNameTemplate, null, null);
     }
 }
Example #10
0
        private List <string> DoCreateInsertQuery(string askquery)
        {
            var  dh         = CreateHolderDumper();
            var  dmp        = dh.Dumper;
            var  autoinccol = m_dstInfo.Model.FindAutoIncrementColumn();
            bool idins      = autoinccol != null &&
                              (m_dstInfo.KeyCols.Contains(autoinccol.ColumnName) || m_dstInfo.DataCols.Contains(autoinccol.ColumnName));

            if (idins)
            {
                dmp.AllowIdentityInsert(m_item.Target.Table, true);
            }

            using (var cmd = m_syn.m_source.Connection.SystemConnection.CreateCommand())
            {
                cmd.CommandText = askquery;

                using (IBedReader reader = m_syn.m_source.GetAnyDDA().AdaptReader(cmd.ExecuteReader()))
                {
                    while (reader.Read())
                    {
                        IBedRecord rec = reader;
                        foreach (var recada in m_outputAdapters)
                        {
                            rec = recada.AdaptRecord(rec, m_syn.Progress);
                        }
                        dmp.Put("insert into %f (%,i %s %,i) values (%,v)",
                                m_item.Target.Table,
                                m_dstInfo.KeyCols,
                                m_dstInfo.DataCols.Length > 0 ? "," : "",
                                m_dstInfo.DataCols,
                                rec
                                );
                        dmp.EndCommand();
                    }
                }
            }

            if (idins)
            {
                dmp.AllowIdentityInsert(m_item.Target.Table, false);
            }
            return(dh.Scripts);
        }
Example #11
0
        public void LoadTableData(string queryInstance, Action <IBedRecord> forEachRow)
        {
            string sql = queryInstance;

            using (DbCommand cmd = m_conn.SystemConnection.CreateCommand())
            {
                try
                {
                    lock (m_currentCommandLock) m_currentCommand = cmd;
                    cmd.CommandText = sql;
                    CommandBehavior behaviour = CommandBehavior.Default;
                    if (!HasMetaData)
                    {
                        behaviour |= CommandBehavior.KeyInfo;
                    }
                    using (IBedReader reader = m_dda.AdaptReader(cmd.ExecuteReader(behaviour)))
                    {
                        if (!HasMetaData)
                        {
                            m_structure = reader.Structure;
                        }
                        if (m_structure == null)
                        {
                            return;
                        }

                        while (reader.Read())
                        {
                            forEachRow(reader);
                        }
                    }
                }
                catch (Exception err)
                {
                    err.Data["sql"] = sql;
                    m_conn.FillInfo(err.Data);
                    throw;
                }
                finally
                {
                    lock (m_currentCommandLock) m_currentCommand = null;
                }
            }
        }
Example #12
0
 public static void RunForEachRecordAndDispose(this IBedReader reader, bool allowDirectCall, Action <IBedRecord, int> func)
 {
     if (allowDirectCall)
     {
         try
         {
             int index = 0;
             while (reader.Read())
             {
                 func(reader, index);
                 index++;
             }
         }
         finally
         {
             reader.Dispose();
         }
     }
     else
     {
         List <ArrayDataRecord> cache = new List <ArrayDataRecord>();
         try
         {
             while (reader.Read())
             {
                 cache.Add(new ArrayDataRecord(reader));
             }
         }
         finally
         {
             reader.Dispose();
         }
         int index = 0;
         foreach (var rec in cache)
         {
             func(rec, index);
             index++;
         }
     }
 }
Example #13
0
 void DoRead(IDataQueue queue)
 {
     try
     {
         using (DbCommand cmd = m_conn.DbFactory.CreateCommand())
         {
             cmd.CommandText = m_query;
             cmd.Connection  = m_conn.SystemConnection;
             using (IBedReader reader = m_conn.GetAnyDDA().AdaptReader(cmd.ExecuteReader()))
             {
                 while (reader.Read())
                 {
                     queue.PutRecord(reader);
                 }
             }
         }
     }
     finally
     {
         queue.PutEof();
     }
 }
Example #14
0
        public BedTable LoadTableData(int?start, int?count, string queryInstance)
        {
            int skipcount = 0;

            if (start != null)
            {
                skipcount = start.Value;
            }

            string sql = queryInstance;

            if (m_conn.Dialect.DialectCaps.RangeSelect && (skipcount > 0 || count != null))
            {
                sql       = m_conn.Dialect.GetRangeSelect(queryInstance, skipcount, count.Value);
                skipcount = 0;
            }
            else if (m_conn.Dialect.DialectCaps.LimitSelect && count != null)
            {
                sql = m_conn.Dialect.GetLimitSelect(queryInstance, skipcount + count.Value);
            }

            //WantMetadata();

            using (DbCommand cmd = m_conn.SystemConnection.CreateCommand())
            {
                try
                {
                    lock (m_currentCommandLock) m_currentCommand = cmd;
                    cmd.CommandText = sql;
                    CommandBehavior behaviour = CommandBehavior.Default;
                    if (!HasMetaData)
                    {
                        behaviour |= CommandBehavior.KeyInfo;
                    }
                    using (IBedReader reader = m_dda.AdaptReader(cmd.ExecuteReader(behaviour)))
                    {
                        if (!HasMetaData)
                        {
                            m_structure = reader.Structure;
                        }
                        if (m_structure == null)
                        {
                            return(null);
                        }
                        BedTable res = new BedTable(m_structure);
                        while (skipcount > 0)
                        {
                            if (!reader.Read())
                            {
                                return(res);
                            }
                            skipcount--;
                        }

                        while (count == null || res.Rows.Count < count.Value)
                        {
                            if (!reader.Read())
                            {
                                return(res);
                            }
                            res.AddRow(reader);
                        }

                        return(res);
                    }
                }
                catch (Exception err)
                {
                    err.Data["sql"] = sql;
                    m_conn.FillInfo(err.Data);
                    throw;
                }
                finally
                {
                    lock (m_currentCommandLock) m_currentCommand = null;
                }
            }
        }
Example #15
0
        void DoRead(IDataQueue queue)
        {
            try
            {
                m_conn.SystemConnection.SafeChangeDatabase(m_dbname);
                using (DbCommand cmd = m_conn.DbFactory.CreateCommand())
                {
                    var    fname     = new NameWithSchema(m_schema, m_tblname);
                    string qfullname = m_conn.Dialect.QuoteFullName(fname);
                    cmd.CommandText = "SELECT * FROM " + qfullname;
                    cmd.Connection  = m_conn.SystemConnection;
                    try
                    {
                        using (IBedReader reader = m_conn.GetAnyDDA().AdaptReader(cmd.ExecuteReader()))
                        {
                            while (reader.Read())
                            {
                                queue.PutRecord(reader);
                            }
                        }
                    }
                    catch (Exception err)
                    {
                        if (!m_conn.Dialect.DialectCaps.RangeSelect)
                        {
                            throw;
                        }
                        if (err is ThreadAbortException)
                        {
                            throw;
                        }

                        // try to load in more packets
                        int tblsize = Int32.Parse(m_conn.SystemConnection.ExecuteScalar("SELECT COUNT(*) FROM " + qfullname).ToString());
                        int ofs     = 0;
                        int maxsize = 200;
                        while (ofs < tblsize)
                        {
                            if (ProgressInfo != null)
                            {
                                ProgressInfo.SetCurWork(String.Format("{0}: {1}/{2}", fname, ofs, tblsize));
                            }
                            int pacsize = tblsize - ofs;
                            if (pacsize > maxsize)
                            {
                                pacsize = maxsize;
                            }

                            using (DbCommand cmd2 = m_conn.DbFactory.CreateCommand())
                            {
                                cmd.CommandText = m_conn.Dialect.GetRangeSelect("SELECT * FROM " + qfullname, ofs, pacsize);
                                cmd.Connection  = m_conn.SystemConnection;
                                using (IBedReader reader = m_conn.GetAnyDDA().AdaptReader(cmd.ExecuteReader()))
                                {
                                    while (reader.Read())
                                    {
                                        queue.PutRecord(reader);
                                    }
                                }
                            }

                            ofs += pacsize;
                        }
                    }
                }
            }
            finally
            {
                queue.PutEof();
                if (ProgressInfo != null)
                {
                    ProgressInfo.SetCurWork("");
                }
            }
        }
Example #16
0
 public static BedTable ToBinaryTable(this IBedReader reader)
 {
     return(ToBinaryTable(reader, null));
 }
Example #17
0
 public void Fill(IBedReader reader)
 {
     Fill(reader.EnumRows());
 }