Esempio n. 1
0
        /// <summary>
        /// Gets the definition.
        /// </summary>
        /// <returns></returns>
        public String getDefinition()
        {
            String query = String.Format("SELECT * FROM [{0}]", m_Parent.Name);

            SqlConnection myConnection = m_Parent.Parent.Connection.connect();

            if (myConnection == null)
            {
                return(String.Empty);
            }

            Table text_table = ConnectionAdapter.executeSelect(myConnection, query);

            myConnection.Close();

            if (text_table.Rows.Count > 0)
            {
                String ret = String.Empty;

                foreach (Row row in text_table.Rows)
                {
                    foreach (Value val in row.Values)
                    {
                        ret += val.Data;
                    }
                }

                return(ret);
            }

            return(String.Empty);
        }
        /// <summary>
        /// Gets the definition.
        /// </summary>
        /// <returns></returns>
        public String getDefinition()
        {
            String query = String.Format("exec sp_helptext @objname = '{0}'", Name);

            SqlConnection myConnection = m_Parent.Connection.connect();

            if (myConnection == null)
            {
                return(String.Empty);
            }

            Table text_table = ConnectionAdapter.executeSelect(myConnection, query);

            myConnection.Close();

            if (text_table.Rows.Count > 0)
            {
                String ret = String.Empty;

                foreach (Row row in text_table.Rows)
                {
                    ret += row.Values[0].Data;
                }

                return(ret);
            }

            return(String.Empty);
        }
Esempio n. 3
0
        private void Button_Exec_Click(object sender, EventArgs e)
        {
            SqlConnection conn = m_Connection.connect();

            if (conn != null)
            {
                try
                {
                    String query = String.Empty;

                    if (Query.SelectionLength == 0)
                    {
                        query = Query.Text;
                    }
                    else
                    {
                        query = Query.SelectedText;
                    }

                    Table tbl = ConnectionAdapter.executeSelect(conn, query);

                    if (tbl != null)
                    {
                        DataGrid.Rows.Clear();
                        DataGrid.ColumnHeadersHeight = 40;
                        DataGrid.ColumnCount         = tbl.FieldCont.Fields.Count;
                        DataGrid.RowCount            = tbl.Rows.Count + 1;
                        // DataGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader;

                        for (int i = 0; i < tbl.FieldCont.Fields.Count; i++)
                        {
                            DataGrid.Columns[i].HeaderCell.Value = tbl.FieldCont.Fields[i].Name;
                        }

                        if (tbl.Rows.Count > 0)
                        {
                            for (int i = 0; i < tbl.Rows.Count; i++)
                            {
                                for (int j = 0; j < tbl.Rows[i].Values.Count; j++)
                                {
                                    DataGrid.Rows[i].Cells[j].Value = tbl.Rows[i].Values[j].Data;
                                }
                            }
                        }
                        else
                        {
                            DataGrid.Rows[0].Cells[0].Value = "No result";
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            else
            {
            }
        }
Esempio n. 4
0
        private void GetDatabases()
        {
            if (m_Connection == null)
            {
                return;
            }

            SqlConnection connection = m_Connection.connectWithoutDataBaseName();

            if (connection != null)
            {
                try
                {
                    Table tbl = ConnectionAdapter.executeSelect(connection, "SELECT NAME FROM sys.databases");

                    if (tbl != null)
                    {
                        DatabaseList.Items.Clear();

                        foreach (Row row in tbl.Rows)
                        {
                            DatabaseList.Items.Add(row.Values[0].Data);
                        }

                        if (DatabaseList.Items.Contains(m_Connection.Name))
                        {
                            DatabaseList.SelectedItem = m_Connection.Name;
                        }
                        else
                        {
                            if (DatabaseList.SelectedItem != null)
                            {
                                m_Connection.Name = DatabaseList.SelectedItem.ToString();
                            }
                            else if (DatabaseList.Items.Count > 0)
                            {
                                DatabaseList.SelectedItem = DatabaseList.Items[0];
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    connection.Close();
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Gets the view data.
        /// </summary>
        /// <param name="Target">The target.</param>
        /// <param name="config">The config.</param>
        public void GetViewData(View Target, Configuration config)
        {
            SqlConnection myConnection = Connection.connect();

            if (myConnection == null)
            {
                return;
            }

            Target.FieldCont = new ViewFieldContainer(Target);

            Table TableList = ConnectionAdapter.executeSelect
                              (
                myConnection,
                String.Format
                (
                    "SELECT name, object_id, parent_object_id FROM sys.objects WHERE type_desc='VIEW' and name='{0}'",
                    Target.Name
                )
                              );

            String table_name       = TableList.Rows[0].Values[0].Data;
            String object_id        = TableList.Rows[0].Values[1].Data;
            String parent_object_id = TableList.Rows[0].Values[2].Data;

            String query = String.Format
                           (
                "SELECT sys.columns.name, sys.types.name, sys.columns.max_length, sys.columns.is_nullable, sys.columns.is_identity " +
                "FROM sys.columns " +
                // "INNER JOIN sys.types ON sys.columns.system_type_id=sys.types.system_type_id " +
                "INNER JOIN sys.types " +
                "ON sys.columns.system_type_id=sys.types.system_type_id " +
                "AND sys.columns.user_type_id=sys.types.user_type_id " +
                "WHERE sys.columns.object_id='{0}'",
                object_id
                           );

            Table ColumnList = ConnectionAdapter.executeSelect(myConnection, query);

            foreach (Row col in ColumnList.Rows)
            {
                String type     = col.Values[1].Data;
                String num      = col.Values[2].Data;
                String nullable = col.Values[3].Data;
                String identity = col.Values[4].Data;

                if
                (
                    IsTextType(type) == true &&
                    num != String.Empty &&
                    num != "NULL"
                )
                {
                    type += "(" + num + ")";
                }

                ViewField f = new ViewField(col.Values[0].Data, type, nullable.ToLower() == "true" ? true : false, identity.ToLower() == "true" ? true : false);
                f.Parent = Target;

                Target.FieldCont.Fields.Add(f);
            }

            myConnection.Close();
        }
Esempio n. 6
0
        /// <summary>
        /// Gets the table data.
        /// </summary>
        /// <param name="Target">The target.</param>
        public void GetTableData(Table Target, Configuration config)
        {
            SqlConnection myConnection = Connection.connect();

            if (myConnection == null)
            {
                return;
            }

            Target.FieldCont      = new TableFieldContainer(Target);
            Target.DataCont       = new DataContainer(Target);
            Target.ConstraintCont = new ConstraintContainer(Target);

            Table TableList = ConnectionAdapter.executeSelect
                              (
                myConnection,
                String.Format
                (
                    "SELECT name, object_id, parent_object_id FROM sys.objects WHERE type_desc='USER_TABLE' and name='{0}'",
                    Target.Name
                )
                              );

            String table_name       = TableList.Rows[0].Values[0].Data;
            String object_id        = TableList.Rows[0].Values[1].Data;
            String parent_object_id = TableList.Rows[0].Values[2].Data;

            String query = String.Format
                           (
                "SELECT sys.columns.name, sys.types.name, sys.columns.max_length, sys.columns.is_nullable, sys.columns.is_identity " +
                "FROM sys.columns " +
                "INNER JOIN sys.types " +
                "ON sys.columns.system_type_id=sys.types.system_type_id " +
                "AND sys.columns.user_type_id=sys.types.user_type_id " +
                "WHERE sys.columns.object_id='{0}'",
                object_id
                           );

            Table ColumnList = ConnectionAdapter.executeSelect(myConnection, query);

            foreach (Row col in ColumnList.Rows)
            {
                String type     = col.Values[1].Data;
                String num      = col.Values[2].Data;
                String nullable = col.Values[3].Data;
                String identity = col.Values[4].Data;

                if (IsTextType(type) == true && num != String.Empty && num != "NULL")
                {
                    type += "(" + num + ")";
                }

                TableField f = new TableField(col.Values[0].Data, type, nullable.ToLower() == "true" ? true : false, identity.ToLower() == "true" ? true : false);
                f.Parent = Target;

                Target.FieldCont.Fields.Add(f);
            }

            //----------------------------------------------------------------
            // Handle constraints

            query = String.Format("SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME='{0}'", table_name);

            Table ConstraintList = ConnectionAdapter.executeSelect(myConnection, query);

            foreach (Row current_row in ConstraintList.Rows)
            {
                Constraint NewConstraint = new Constraint();

                NewConstraint.Name   = current_row.Values[0].Data;
                NewConstraint.Type   = Constraint.SQLConstraintTypeToConstraintType(current_row.Values[1].Data);
                NewConstraint.Parent = Target;

                if (NewConstraint.Type == Constraint.ConstraintType.Check)
                {
                    query = String.Format("SELECT CHECK_CLAUSE FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS WHERE CONSTRAINT_NAME='{0}'", NewConstraint.Name);

                    Table ConstraintText = ConnectionAdapter.executeSelect(myConnection, query);

                    if (ConstraintText.Rows.Count > 0)
                    {
                        NewConstraint.Text = ConstraintText.Rows[0].Values[0].Data;
                    }
                }
                else if (NewConstraint.Type == Constraint.ConstraintType.ForeignKey)
                {
                    query = String.Format
                            (
                        "SELECT " +
                        "CONSTRAINT_NAME = REF_CONST.CONSTRAINT_NAME, " +
                        "TABLE_NAME = FK.TABLE_NAME, " +
                        "COLUMN_NAME = FK_COLS.COLUMN_NAME, " +
                        "REFERENCED_TABLE_NAME = PK.TABLE_NAME, " +
                        "REFERENCED_COLUMN_NAME = PK_COLS.COLUMN_NAME " +
                        "FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS REF_CONST " +
                        "INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK " +
                        "ON REF_CONST.CONSTRAINT_CATALOG = FK.CONSTRAINT_CATALOG " +
                        "AND REF_CONST.CONSTRAINT_SCHEMA = FK.CONSTRAINT_SCHEMA " +
                        "AND REF_CONST.CONSTRAINT_NAME = FK.CONSTRAINT_NAME " +
                        "AND FK.CONSTRAINT_TYPE = 'FOREIGN KEY' " +
                        "INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON REF_CONST.UNIQUE_CONSTRAINT_CATALOG = PK.CONSTRAINT_CATALOG " +
                        "AND REF_CONST.UNIQUE_CONSTRAINT_SCHEMA = PK.CONSTRAINT_SCHEMA " +
                        "AND REF_CONST.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME " +
                        "AND PK.CONSTRAINT_TYPE = 'PRIMARY KEY' " +
                        "INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE FK_COLS ON REF_CONST.CONSTRAINT_NAME = FK_COLS.CONSTRAINT_NAME " +
                        "INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE PK_COLS ON PK.CONSTRAINT_NAME = PK_COLS.CONSTRAINT_NAME " +
                        "WHERE REF_CONST.CONSTRAINT_NAME = '{0}'",
                        NewConstraint.Name
                            );

                    Table ConstraintText = ConnectionAdapter.executeSelect(myConnection, query);

                    if (ConstraintText != null && ConstraintText.Rows.Count > 0)
                    {
                        NewConstraint.ReferencedTable = ConstraintText.Rows[0].Values[3].Data;
                        NewConstraint.ReferencedFieldNames.Add(ConstraintText.Rows[0].Values[4].Data);
                    }
                }

                query = String.Format
                        (
                    "SELECT COLUMN_NAME " +
                    "FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE " +
                    "WHERE TABLE_NAME='{0}' AND CONSTRAINT_NAME='{1}'",
                    table_name,
                    NewConstraint.Name
                        );

                Table ConstraintRows = ConnectionAdapter.executeSelect(myConnection, query);

                foreach (Row rw in ConstraintRows.Rows)
                {
                    NewConstraint.FieldNames.Add(rw.Values[0].Data);
                }

                Target.ConstraintCont.Constraints.Add(NewConstraint);
            }

            // Handle data

            if (config.AnalyzeData)
            {
                Target.DataCont = new DataContainer(Target);
            }

            myConnection.Close();
        }
Esempio n. 7
0
        /// <summary>
        /// Fetches the data.
        /// </summary>
        /// <param name="Analyzer">The analyzer.</param>
        /// <param name="config">The config.</param>
        public bool fetchData(BaseAnalyzer Analyzer, Configuration config)
        {
            SqlConnection myConnection = Connection.connect();

            if (myConnection == null)
            {
                return(false);
            }

            //----------------------------------------------------------------
            // Handle tables

            if (config.AnalyzeTables)
            {
                int NumTables = 0;

                Table TableCountTable = ConnectionAdapter.executeSelect(myConnection, "SELECT COUNT(name) FROM sys.objects WHERE type_desc='USER_TABLE'");

                if (TableCountTable.Rows.Count > 0)
                {
                    Int32.TryParse(TableCountTable.Rows[0].Values[0].Data, out NumTables);
                }

                Analyzer.Progress_Max     = NumTables;
                Analyzer.Progress_Current = 0;

                Table TableList = ConnectionAdapter.executeSelect(myConnection, "SELECT name FROM sys.objects WHERE type_desc='USER_TABLE'");

                foreach (Row table_new_row in TableList.Rows)
                {
                    Analyzer.Progress_Current++;

                    Table NewTable = new Table(this);
                    NewTable.Name = table_new_row.Values[0].Data;

                    Analyzer.Status = m_Connection.Name + " : " + NewTable.Name + "...";

                    GetTableData(NewTable, config);

                    TableCont.Tables.Add(NewTable);
                }
            }

            //----------------------------------------------------------------
            // Handle views

            if (config.AnalyzeViews)
            {
                int NumViews = 0;

                Table ViewCountTable = ConnectionAdapter.executeSelect(myConnection, "SELECT COUNT(name) FROM sys.objects WHERE type_desc='VIEW'");

                if (ViewCountTable.Rows.Count > 0)
                {
                    Int32.TryParse(ViewCountTable.Rows[0].Values[0].Data, out NumViews);
                }

                Analyzer.Progress_Max     = NumViews;
                Analyzer.Progress_Current = 0;

                Table TableList = ConnectionAdapter.executeSelect(myConnection, "SELECT name FROM sys.objects WHERE type_desc='VIEW'");

                foreach (Row table_new_row in TableList.Rows)
                {
                    Analyzer.Progress_Current++;

                    View NewView = new View(this);
                    NewView.Name = table_new_row.Values[0].Data;

                    Analyzer.Status = m_Connection.Name + " : " + NewView.Name + "...";

                    GetViewData(NewView, config);

                    ViewCont.Views.Add(NewView);
                }
            }

            //----------------------------------------------------------------
            // Handle procedures

            if (config.AnalyzeProcedures)
            {
                Analyzer.Status = "Analyzing procedures...";

                String query = "SELECT name FROM sys.objects WHERE type_desc='SQL_STORED_PROCEDURE'";

                Table proc_list = ConnectionAdapter.executeSelect(myConnection, query);

                foreach (Row proc_new_row in proc_list.Rows)
                {
                    Procedure proc = new Procedure();

                    proc.Name   = proc_new_row.Values[0].Data;
                    proc.Parent = this;

                    ProcedureCont.Procedures.Add(proc);
                }
            }

            //----------------------------------------------------------------
            // Handle functions

            if (config.AnalyzeFunctions)
            {
                Analyzer.Status = "Analyzing functions...";

                String query = "SELECT name FROM sys.objects WHERE type_desc='SQL_SCALAR_FUNCTION' OR type_desc='SQL_TABLE_VALUED_FUNCTION'";

                Table func_list = ConnectionAdapter.executeSelect(myConnection, query);

                foreach (Row func_new_row in func_list.Rows)
                {
                    Function func = new Function();

                    func.Name   = func_new_row.Values[0].Data;
                    func.Parent = this;

                    FunctionCont.Functions.Add(func);
                }
            }

            //----------------------------------------------------------------

            Analyzer.Status           = "Analyze done...";
            Analyzer.Progress_Current = 0;

            myConnection.Close();

            return(true);
        }