Пример #1
0
 private void MakeTextPage(ScriptRunner runner, int tabCounter)
 {
     string tabLetter = TabLetter(tabCounter);
     TabPage tp = new TabPage(tabLetter);
     SqlCommon.SqlForm.tabResults.Controls.Add(tp);
     tp.ImageIndex = runner.AnyError ? SqlGuiForm.IconError : SqlGuiForm.IconScript;
     tp.Name = "tp" + tabLetter;
     tp.TabIndex = tabCounter;
     TextBox tb = new TextBox
     {
         Dock = DockStyle.Fill,
         BackColor = Color.White,
         Location = new Point(0, 0),
         Multiline = true,
         ReadOnly = true,
         Text = runner.Details,
         Visible = true
     };
     SqlCommon.SqlForm.textBoxSql.DeselectAll();
     tp.Controls.Add(tb);
     tp.ToolTipText = runner.Summary;
     tp.Visible = true;
 }
Пример #2
0
 // ReSharper disable once UnusedMethodReturnValue.Local
 private bool ExecuteStatements()
 {
     bool anyGridMade = false;
     bool allGridColumnsShapedUp = true;
     using (new WaitingCursor(SqlCommon.SqlForm))
     {
         IEnumerable<string> statements = SqlParser.Split(SqlCommon.Statements.GetSqlStatementForExecution());
         try
         {
             SqlCommon.SqlForm.tabResults.Visible = false;
             SqlCommon.SqlForm.tabResults.TabPages.Clear();
             ScriptRunner runner = new ScriptRunner();
             int tabCounter = 0;
             ConnectionManager.CurrentConnection.Open();
             foreach (string statement in statements)
             {
                 if ((tabCounter >= 26) || ((tabCounter >= 25) && runner.AnythingAdded))
                 {
                     break;
                 }
                 Dictionary<string, ColumnInfo> columnInfo = GetColumnInfo(runner, statement);
                 if (null != columnInfo)
                 {
                     IDbCommand cmd = ConnectionManager.CurrentConnection.CreateCommand();
                     cmd.CommandText = statement;
                     try
                     {
                         using (
                             IDataReader reader =
                                 cmd.ExecuteReader(CommandBehavior.Default | CommandBehavior.SequentialAccess |
                                                   CommandBehavior.SingleResult))
                         {
                             int rowsAffected = reader.RecordsAffected;
                             if (rowsAffected < 0)
                             {
                                 anyGridMade = true;
                                 DataGridView dgv = MakeGridPage(statement, tabCounter, reader);
                                 if (!ShapeUpColumns(dgv, columnInfo))
                                 {
                                     allGridColumnsShapedUp = false;
                                 }
                                 ++tabCounter;
                             }
                             else
                             {
                                 runner.AddResult(statement, rowsAffected);
                             }
                             reader.Close();
                         }
                     }
                     catch (Exception ex)
                     {
                         runner.AddError(statement, ex);
                     }
                 }
             }
             if (runner.AnythingAdded)
             {
                 MakeTextPage(runner, tabCounter);
             }
         }
         finally
         {
             SqlCommon.SqlForm.tabResults.Visible = (SqlCommon.SqlForm.tabResults.TabCount > 0);
             ConnectionManager.CurrentConnection.Close();
         }
     }
     return !anyGridMade || allGridColumnsShapedUp;
 }
Пример #3
0
        private Dictionary<string, ColumnInfo> GetColumnInfo(ScriptRunner runner, string statement)
        {
            Dictionary<string, ColumnInfo> result = new Dictionary<string, ColumnInfo>();
            try
            {
                IDbCommand cmd = ConnectionManager.CurrentConnection.CreateCommand();
                cmd.CommandText = statement;
                using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly))
                {
                    DataTable schema = reader.GetSchemaTable();
                    if (null != schema)
                    {
                        foreach (DataRow row in schema.Rows)
                        {
                            if (!row.IsNull("ColumnName"))
                            {
                                string columnName = row["ColumnName"].ToString();
                                int columnOrdinal = row.IsNull("ColumnOrdinal") ? -1 : (int)row["ColumnOrdinal"];
                                int columnSize = row.IsNull("ColumnSize") ? 0 : (int)row["ColumnSize"];
                                short numericPrecision = row.IsNull("NumericPrecision") ? (short)0 : (short)row["NumericPrecision"];
                                short numericScale = row.IsNull("NumericScale") ? (short)0 : (short)row["NumericScale"];
                                string dataType = row["DataType"].ToString();
                                bool allowDbNull = (bool)row["AllowDBNull"];

                                result.Add(columnName, new ColumnInfo
                                {
                                    DotNetType = dataType.Substring(dataType.LastIndexOf('.') + 1),
                                    Name = columnName,
                                    Nullable = allowDbNull,
                                    Ordinal = columnOrdinal,
                                    Precision = numericPrecision,
                                    Scale = numericScale,
                                    Size = columnSize
                                });
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                runner.AddError(statement, ex);
            }
            return result;
            // Oracle:
            // SELECT DISTINCT DATA_TYPE, DATA_LENGTH, DATA_PRECISION, DATA_SCALE
            // FROM USER_TAB_COLS
            // WHERE TABLE_NAME IN (SELECT DISTINCT TABLE_NAME FROM USER_TABLES WHERE TABLE_NAME LIKE 'A%')
            // ORDER BY DATA_TYPE, DATA_LENGTH, DATA_PRECISION, DATA_SCALE;
            // Finnes i en typisk Agresso-base:
            // Type     Length	Precision	Scale
            // BLOB     4000    -           -
            // CLOB     4000    -           -
            // DATE     7       -           -
            // NUMBER	22      1           0
            // NUMBER	22      3           0
            // NUMBER	22      5           0
            // NUMBER	22      9           0
            // NUMBER	22      15          0
            // NUMBER	22      20          0
            // NUMBER	22      28          3
            // NUMBER	22      30          3
            // NUMBER	22      30          8
            // NUMBER	22      -           0
            // RAW      16		-           -
            // VARCHAR2 x       -           -

            // SQL Server:
            // SELECT DISTINCT DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE
            // FROM INFORMATION_SCHEMA.COLUMNS
            // WHERE TABLE_NAME IN
            // (SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'A%' AND TABLE_TYPE = 'BASE TABLE')
            // Finnes i en typisk Agresso-base:
            // DATA_TYPE        CHARACTER_MAXIMUM_LENGTH    NUMERIC_PRECISION   NUMERIC_SCALE
            // bigint           NULL                        19                  0
            // bit              NULL                        NULL                NULL
            // datetime         NULL                        NULL                NULL
            // decimal          NULL                        28                  3
            // decimal          NULL                        28                  8
            // float            NULL                        53                  NULL
            // int              NULL                        10                  0
            // smallint         NULL                        5                   0
            // tinyint          NULL                        3                   0
            // uniqueidentifier NULL                        NULL                NULL
            // varbinary        -1                          NULL                NULL
            // varchar          x                           NULL                NULL
        }