Exemple #1
0
        //public static void ClearCache(this IPhysicalConnection conn)
        //{
        //    lock (conn.CachedData) conn.CachedData.Clear();
        //}

        public static void SetOnOpenDatabase(this IPhysicalConnection conn, string dbname)
        {
            if (!String.IsNullOrEmpty(dbname))
            {
                conn.AfterOpen += ConnTools.ChangeDatabaseCallback(dbname);
            }
        }
Exemple #2
0
 public void CreateNewObject()
 {
     if (m_conn.DatabaseCaps.ExecuteSql)
     {
         OpenQueryParameters pars    = new OpenQueryParameters();
         IPhysicalConnection newconn = GetConnection().Clone();
         string dbname = TreeNodeExtension.GetDatabaseName(this);
         if (dbname != null)
         {
             newconn.AfterOpen += ConnTools.ChangeDatabaseCallback(dbname);
         }
         pars.GenerateSql = delegate(IPhysicalConnection conn)
         {
             return(m_dbtype.GenerateCreateNew(conn.SystemConnection, TreeNodeExtension.GetAnyObjectPath(this)));
         };
         pars.HideDesign       = true;
         pars.ExecutedCallback = this.CompleteRefresh;
         MainWindow.Instance.OpenContent(new QueryFrame(newconn, pars));
     }
     else
     {
         string sql  = m_dbtype.GenerateCreateNew(m_conn.Connection.SystemConnection, TreeNodeExtension.GetAnyObjectPath(this));
         var    pars = new ObjectEditorPars {
             SavedCallback = this.CompleteRefresh
         };
         var frm = new SpecificObjectFrame(m_conn, m_repr.ObjectType, sql, pars);
         MainWindow.Instance.OpenContent(frm);
     }
 }
        //public static BedTable ToBinaryTable(this IDataReader reader, int? maximumRecords)
        //{
        //    ITableStructure ts = reader.GetTableStructure(null);
        //    BedTable dt = new BedTable(ts);
        //    int allow_recs = maximumRecords != null ? maximumRecords.Value : -1;
        //    while (reader.Read() && (maximumRecords == null || allow_recs > 0))
        //    {
        //        dt.AddRow(reader);
        //        allow_recs--;
        //    }
        //    return dt;
        //}

        //public static BedTable ToBinaryTable(this IDataReader reader)
        //{
        //    return ToBinaryTable(reader, null);
        //}

        public static DataTable ToDataTable(this IDataReader reader, int?maximumRecords)
        {
            ITableStructure ts         = reader.GetTableStructure(null);
            DataTable       dt         = ConnTools.DataTableFromStructure(ts);
            int             allow_recs = maximumRecords != null ? maximumRecords.Value : -1;

            while (reader.Read() && (maximumRecords == null || allow_recs > 0))
            {
                DataRow row = dt.NewRow();
                for (int i = 0; i < ts.Columns.Count; i++)
                {
                    try
                    {
                        row[i] = reader[i];
                    }
                    catch (Exception)
                    {
                        row[i] = DBNull.Value;
                    }
                }
                dt.Rows.Add(row);
                allow_recs--;
            }
            return(dt);
        }
Exemple #4
0
        public static void WriteQueueToXml(IDataQueue queue, Stream fw, string dbName, string tableName)
        {
            DataTable table = ConnTools.DataTableFromStructure(queue.GetRowFormat);

            table.TableName = tableName;

            List <string> fldnames = new List <string>();

            foreach (IColumnStructure col in queue.GetRowFormat.Columns)
            {
                fldnames.Add(col.ColumnName);
            }

            XmlWriter xw = XmlTextWriter.Create(fw, new XmlWriterSettings {
                Encoding = Encoding.UTF8, CheckCharacters = false
            });

            //XmlTextWriter xw = new XmlTextWriter(fw, Encoding.UTF8);
            //xw.Settings = new XmlWriterSettings { CheckCharacters = false };

            xw.WriteStartDocument();
            xw.WriteStartElement(dbName);
            // mono has bug in writing schema
            if (!Core.IsMono)
            {
                table.WriteXmlSchema(xw);
            }
            List <string> ids = new List <string>();

            foreach (IColumnStructure col in queue.GetRowFormat.Columns)
            {
                ids.Add(XmlTool.NormalizeIdentifier(col.ColumnName));
            }
            try
            {
                while (!queue.IsEof)
                {
                    IBedRecord row = queue.GetRecord();
                    xw.WriteStartElement(tableName);
                    for (int i = 0; i < row.FieldCount; i++)
                    {
                        row.ReadValue(i);
                        var type = row.GetFieldType();
                        if (type == TypeStorage.Null)
                        {
                            continue;
                        }
                        xw.WriteElementString(ids[i], XmlTool.ObjectToString(row.GetValue(i)));
                    }
                    xw.WriteEndElement();
                }
            }
            finally
            {
                queue.CloseReading();
            }
            xw.WriteEndElement();
            xw.WriteEndDocument();
            xw.Flush();
        }
Exemple #5
0
        public virtual void RecreateTable(ITableStructure oldTable, ITableStructure newTable)
        {
            if (oldTable.GroupId != newTable.GroupId)
            {
                throw new InternalError("DAE-00040 Recreate is not possible: oldTable.GroupId != newTable.GroupId");
            }
            var    columnMap = GetColumnMap(oldTable, newTable);
            int    id        = System.Threading.Interlocked.Increment(ref m_lastAlterTableId);
            string tmptable  = ConnTools.GenerateTempTableName(id);

            // remove constraints
            //DropConstraints(oldTable.GetReferencedFrom(), DropFlags.None);
            DropConstraints(oldTable.Constraints, DropFlags.None);

            RenameTable(oldTable.FullName, tmptable);

            TableStructure old = new TableStructure(oldTable);

            old.FullName = new NameWithSchema(oldTable.FullName.Schema, tmptable);

            CreateTable(newTable);

            var  idcol    = newTable.FindAutoIncrementColumn();
            bool hasident = idcol != null && columnMap[idcol.ColumnOrder] >= 0;

            if (hasident)
            {
                AllowIdentityInsert(newTable.FullName, true);
            }
            PutCmd("^insert ^into %f (%,i) select %,s ^from %f", newTable.FullName,
                   from c in newTable.Columns
                   where columnMap[c.ColumnOrder] >= 0
                   select c.ColumnName,
                   from dstindex in
                   (
                       from i in PyList.Range(newTable.Columns.Count)
                       where columnMap[i] >= 0
                       select i
                   )
                   let srcindex = columnMap[dstindex]
                                  select
                                      (srcindex < 0
                                      // srcindex < 0 should not occur thanks to filtering
                    ? Format("^null ^as %i", newTable.Columns[dstindex].ColumnName)
                    : Format("^%i ^as %i", old.Columns[srcindex].ColumnName, newTable.Columns[dstindex].ColumnName)),
                   old.FullName);
            if (hasident)
            {
                AllowIdentityInsert(newTable.FullName, false);
            }

            // newTable.Constraints are allready created
            //CreateConstraints(newTable.GetReferencedFrom());

            PutCmd("^drop ^table %i", tmptable);
        }
Exemple #6
0
        public ITabularDataStore GetDataStoreAndClone()
        {
            IPhysicalConnection conn = m_conn.Clone();

            if (!String.IsNullOrEmpty(m_dbname))
            {
                conn.AfterOpen += ConnTools.ChangeDatabaseCallback(m_dbname);
            }
            return(new GenericTabularDataStore(conn, m_dbname, m_schema, m_viewname));
        }
Exemple #7
0
        public ITabularDataStore GetStoreAndClone(TableDataSetProperties props)
        {
            IPhysicalConnection conn = m_conn.Clone();

            if (!String.IsNullOrEmpty(m_dbname))
            {
                conn.AfterOpen += ConnTools.ChangeDatabaseCallback(m_dbname);
            }
            return(new GenericQueryDataStore(conn, m_dbname, GetQuery(props, QueryType.SelectVisible)));
        }
Exemple #8
0
        public override DataTable GetSchemaTable()
        {
            DataTable res = ConnTools.SchemaFromStructure(m_structure);

            for (int i = 0; i < m_structure.Columns.Count; i++)
            {
                DataRow row = res.Rows[i];
                row[SchemaTableColumn.BaseTableName] = m_structure.FullName != null ? m_structure.FullName.Name : null;
                row[SchemaTableColumn.IsKey]         = m_structure.GetKeyWithColumn <IPrimaryKey>(m_structure.Columns[i]) != null;
            }
            return(res);
        }
        public override void CreateNew(AppObject appobj, ConnectionPack connpack)
        {
            var tp = _Type;

            if (tp.ObjectType == "table")
            {
                var pars = new AlterTableEditorPars {
                    SavedCallback = RefreshList
                };
                MainWindow.Instance.OpenContent(new TableEditFrame(appobj.CreateDatabaseConnection(), null, pars));
            }
            else
            {
                var dbconn = appobj.FindDatabaseConnection(connpack);
                var dbtype = dbconn.Dialect.GetSpecificObjectType(tp.ObjectType);
                if (dbconn.DatabaseCaps.ExecuteSql)
                {
                    OpenQueryParameters pars    = new OpenQueryParameters();
                    IPhysicalConnection newconn = dbconn.Connection.Clone();
                    string dbname = appobj.FindDatabaseName();
                    if (dbname != null)
                    {
                        newconn.AfterOpen += ConnTools.ChangeDatabaseCallback(dbname);
                    }
                    pars.GenerateSql = delegate(IPhysicalConnection conn)
                    {
                        return(dbtype.GenerateCreateNew(conn.SystemConnection, appobj.GetObjectPath()));
                    };
                    pars.HideDesign       = true;
                    pars.ExecutedCallback = RefreshList;
                    MainWindow.Instance.OpenContent(new QueryFrame(newconn, pars));
                }
                else
                {
                    string sql  = dbtype.GenerateCreateNew(dbconn.Connection.SystemConnection, appobj.GetObjectPath());
                    var    pars = new ObjectEditorPars {
                        SavedCallback = RefreshList
                    };
                    var frm = new SpecificObjectFrame(dbconn, tp.ObjectType, sql, pars);
                    MainWindow.Instance.OpenContent(frm);
                }
            }
        }
        public void Edit()
        {
            if (!EditEnabled())
            {
                return;
            }
            var dbconn = this.FindDatabaseConnection(ConnPack);

            if (dbconn.DatabaseCaps.ExecuteSql)
            {
                OpenQueryParameters pars    = new OpenQueryParameters();
                IPhysicalConnection newconn = Connection.CreateConnection();
                string dbname = DatabaseName;
                if (dbname != null)
                {
                    newconn.AfterOpen += ConnTools.ChangeDatabaseCallback(dbname);
                }
                pars.GenerateSql = delegate(IPhysicalConnection conn)
                {
                    var props = new SqlFormatProperties {
                        OmitVersionTests = true
                    };
                    var dbs = conn.PhysicalFactory.CreateDatabaseSource(conn, dbname);
                    var so  = new SpecificObjectStructure(dbs.LoadSpecificObjectDetail(DbObjectType, DbObjectName));
                    if (dbconn.Dialect != null)
                    {
                        so.CreateSql = dbconn.Dialect.ReformatSpecificObject(so.ObjectType, so.CreateSql);
                    }
                    string drop   = newconn.Dialect.GenerateScript(dmp => { dmp.DropSpecificObject(so); }, props);
                    string create = newconn.Dialect.GenerateScript(dmp => { dmp.CreateSpecificObject(so); }, props);
                    return(drop + "\n\nGO\n\n" + create);
                };
                MainWindow.Instance.OpenContent(new QueryFrame(newconn, pars));
            }
            else
            {
                var frm = new SpecificObjectFrame(dbconn.CloneSource(), LoadStructure(), new ObjectEditorPars {
                });
                MainWindow.Instance.OpenContent(frm);
            }
        }
Exemple #11
0
        public void EditScript()
        {
            if (QueryFrame.FindOpenedFile(m_file.DataDiskPath))
            {
                return;
            }
            IPhysicalConnection conn = MainWindowExtension.SelectedConnection;

            if (conn != null)
            {
                conn = conn.Clone();
            }
            OpenQueryParameters pars = new OpenQueryParameters();
            string dbname            = MainWindowExtension.SelectedDatabaseName;

            pars.File           = m_file;
            pars.DisableContext = m_conn != null;
            if (dbname != null && conn != null)
            {
                conn.AfterOpen += ConnTools.ChangeDatabaseCallback(dbname);
            }
            MainWindow.Instance.OpenContent(new QueryFrame(conn, pars));
        }
Exemple #12
0
 public DataTable GetSchemaTable()
 {
     return(ConnTools.SchemaFromStructure(m_table.Columns.GetTableStructure("table")));
 }