Esempio n. 1
0
        public void LoadFile(string fileName, StringNoCaseDictionary <string> parameters, List <BaseCommand> commands)
        {
            if (_Viewer != null)
            {
                _Viewer.Parent = null;
                _Viewer.Dispose();
                _Viewer = null;

                ribbonPageGroupPrint.Visible = false;
                ribbonPageGroupFind.Visible  = false;
                ribbonPageGroupZoom.Visible  = false;
            }

            _Viewer = BaseViewer.CreateViewer(this, fileName, parameters, commands);
            if (_Viewer != null)
            {
                _Viewer.Dock   = DockStyle.Fill;
                _Viewer.Parent = this;
                _Viewer.BringToFront();

                ribbonPageGroupPrint.Visible = _Viewer.SupportPrint;
                ribbonPageGroupFind.Visible  = _Viewer.SupportFind;
                ribbonPageGroupZoom.Visible  = _Viewer.SupportZoom;
            }
        }
Esempio n. 2
0
        private void AddRelation(DataSet dataSet, Relation relation, StringNoCaseDictionary <string> dictTableNames)
        {
            var parentTableName = dictTableNames.ContainsKey(relation.ParentTableName) ? dictTableNames[relation.ParentTableName] : null;
            var childTableName  = dictTableNames.ContainsKey(relation.ChildTableName) ? dictTableNames[relation.ChildTableName] : null;

            if (!string.IsNullOrWhiteSpace(parentTableName) && !string.IsNullOrWhiteSpace(childTableName))
            {
                AddRelation(dataSet, relation.RelationName, parentTableName, childTableName,
                            relation.ParentColumnNames.ToArray(), relation.ChildColumnNames.ToArray());
            }
        }
Esempio n. 3
0
        protected void WriteDataSet()
        {
            if (DataSource == null || DataSource.Tables.Count <= 0)
            {
                return;
            }

            var dataSet = CheckExternalHost().GridDataSet;

            if (dataSet == null)        //Some hosts may have no grids, in this case - do not output anywhere
            {
                return;
            }

            List <BaseCommand> commands = null;

            if (!string.IsNullOrWhiteSpace(Formatting))
            {
                var scanner = new Scanner();
                var parser  = new Parser(scanner);

                var tree = parser.Parse(Formatting);
                if (tree.Errors.Count > 0)
                {
                    var strErrors = new StringBuilder();

                    foreach (var error in tree.Errors)
                    {
                        if (strErrors.Length > 0)
                        {
                            strErrors.AppendLine();
                        }
                        strErrors.Append(error.Message);
                    }

                    throw new Exception(strErrors.ToString());
                }

                try
                {
                    commands = tree.Eval() as List <BaseCommand>;
                }
                catch (Exception)
                {
                    //Do nothing, skip invalid commands
                }

                if (commands != null)
                {
                    int counter = 1;
                    foreach (var formatCondition in commands.OfType <FormatCondition>())
                    {
                        if (string.IsNullOrWhiteSpace(formatCondition.TableName))
                        {
                            continue;
                        }

                        var formatTable = dataSet.Tables[formatCondition.TableName];
                        if (formatTable != null)
                        {
                            formatTable.ExtendedProperties[$"Format_{counter++}"] = formatCondition.AsString();
                        }
                    }

                    counter = 1;
                    foreach (var computedColumn in commands.OfType <ComputedColumn>())
                    {
                        if (string.IsNullOrWhiteSpace(computedColumn.TableName))
                        {
                            continue;
                        }

                        var formatTable = dataSet.Tables[computedColumn.TableName];
                        if (formatTable != null)
                        {
                            formatTable.ExtendedProperties[$"ComputedColumn_{counter++}"] = computedColumn.AsString();
                        }
                    }
                }
            }

            var listTables     = new List <DataTable>();
            var listRelations  = new List <Relation>();
            var dictTableNames = new StringNoCaseDictionary <string>();

            foreach (var relation in DataSource.Relations.OfType <DataRelation>())
            {
                var rel = new Relation()
                {
                    RelationName    = relation.RelationName,
                    ParentTableName = relation.ParentTable?.TableName,
                    ChildTableName  = relation.ChildTable?.TableName,
                };

                rel.ParentColumnNames.AddRange(relation.ParentColumns?.Select(c => c.ColumnName));
                rel.ChildColumnNames.AddRange(relation.ChildColumns?.Select(c => c.ColumnName));

                listRelations.Add(rel);
            }

            foreach (var dataTable in DataSource.Tables.OfType <DataTable>().ToList())
            {
                //Set table name
                var tableName = dataTable.TableName;
                if (string.IsNullOrWhiteSpace(tableName))
                {
                    var tableNames = new List <string>();
                    foreach (DataTable table in dataSet.Tables)
                    {
                        tableNames.Add(table.TableName);
                    }
                    tableName = Utils.AddUniqueString(tableNames, "Table1", StringComparison.CurrentCultureIgnoreCase, false);
                }

                dictTableNames[tableName] = dataTable.TableName;

                DataTable newDataTable;
                if (NoCopyTables)
                {
                    RemoveTableFromDataSet(dataTable);
                    newDataTable = dataTable;
                }
                else
                {
                    newDataTable = dataTable.Copy();
                }
                newDataTable.TableName = tableName;

                listTables.Add(newDataTable);
            }

            ExecuteSynchronized(() => DoWriteDataSet(dataSet, listTables, listRelations, DataSource, commands, dictTableNames));
        }
Esempio n. 4
0
        protected virtual void DoWriteDataSet(DataSet dataSet, List <DataTable> listTables, List <Relation> listRelations,
                                              DataSet dataSource, List <BaseCommand> commands, StringNoCaseDictionary <string> dictTableNames)
        {
            if (ResetDataSet)
            {
                dataSet.Reset();
            }

            foreach (var dataTable in listTables)
            {
                if (Replace)
                {
                    RemoveTableFromDataSet(dataSet.Tables[dataTable.TableName]);
                }

                dataSet.Tables.Add(dataTable);
            }

            if (listRelations != null)
            {
                foreach (var relation in listRelations)
                {
                    AddRelation(dataSet, relation, dictTableNames);
                }
            }

            if (commands != null)
            {
                foreach (var relation in commands.OfType <Relation>())
                {
                    AddRelation(dataSet, relation, dictTableNames);
                }
            }
        }
        public virtual void ListScriptIntellisenseItems(string text, string[] lines, Point caretPosition, ScriptIntellisense intellisense)
        {
            intellisense.UsePeriodInIntellisense = true;

            var              script             = new SqlScript(text);
            string           lastConnectionName = null;
            SqlScriptCommand command            = null;

            for (int i = 0; i < script.Commands.Count; i++)
            {
                if (script.Commands[i].StartLineNumber - 1 > caretPosition.Y)
                {
                    break;
                }

                command            = script.Commands[i];
                lastConnectionName = command.GetConnectionName();
            }

            if (string.IsNullOrWhiteSpace(lastConnectionName) && _SelectedDbConnection == null)
            {
                return;
            }

            var itemCaptions = new Dictionary <(ScriptIntellisenseItem.IntellisenseItemType, string), int>();

            Connection conn = null;

            try
            {
                if (string.IsNullOrWhiteSpace(lastConnectionName))
                {
                    conn = _SelectedDbConnection.Factory.CreateConnection(_SelectedDbConnection.ConnectionString);
                }
                else
                {
                    var connections = DBConnections.LoadConnections();
                    var dbConn      = connections.FindConnection(lastConnectionName);
                    if (dbConn != null)
                    {
                        conn = dbConn.GetConnectionFactory()?.CreateConnection(dbConn.ConnectionString);
                    }
                }

                if (conn == null)
                {
                    return;
                }

                try
                {
                    conn.Open();
                }
                catch (Exception)
                {
                    return;
                }

                var    parts   = new List <string>();
                var    aliases = new StringNoCaseDictionary <TableAlias>();
                string dbName  = conn.DbConnection.Database;

                if (!string.IsNullOrWhiteSpace(command?.Text))
                {
                    var reAlias     = new Regex(@"(?i)\b(?:from|join)\s+(?:(?<Database>(\w+|\[.*?]\]|`.*?`|"".*?"")\.)?(?:(?<Schema>(?:\w+|\[.*?]\]|`.*?`|"".*?""))\.))?(?<Table>(?:\w+|\[.*?\]|`.*?`|"".*?""))[ \t]+(?<Alias>\w+)");
                    var collAliases = reAlias.Matches(command.Text);

                    if ((collAliases?.Count ?? 0) > 0)
                    {
                        foreach (Match matchAlias in collAliases)
                        {
                            if (!matchAlias.Success)
                            {
                                continue;
                            }

                            string alias         = matchAlias.Groups["Alias"].Value;
                            string aliasDatabase = Utils.NullString(Utils.UnquoteString(matchAlias.Groups["Database"].Value));
                            string aliasSchema   = Utils.NullString(Utils.UnquoteString(matchAlias.Groups["Schema"].Value));
                            string aliasTable    = Utils.NullString(Utils.UnquoteString(matchAlias.Groups["Table"].Value));

                            if (string.IsNullOrWhiteSpace(alias) || string.Compare(alias, "where", true) == 0 ||
                                string.Compare(alias, "order", true) == 0 || string.Compare(alias, "join", true) == 0 ||
                                string.Compare(alias, "left", true) == 0 || string.Compare(alias, "right", true) == 0 ||
                                string.Compare(alias, "inner", true) == 0 || string.Compare(alias, "outer", true) == 0 ||
                                string.Compare(alias, "full", true) == 0)
                            {
                                continue;
                            }

                            if (!string.IsNullOrWhiteSpace(aliasTable) && !aliases.ContainsKey(alias))
                            {
                                aliases.Add(alias, new TableAlias()
                                {
                                    Alias = alias, Database = aliasDatabase, Schema = aliasSchema, Table = aliasTable
                                });
                            }
                        }
                    }
                }

                string line = caretPosition.Y < (lines?.Length ?? 0) ? lines[caretPosition.Y] : null;
                if (!string.IsNullOrWhiteSpace(line))
                {
                    if (caretPosition.X < line.Length)
                    {
                        line = line.Substring(0, caretPosition.X);
                    }

                    var reParts    = new Regex(@"((?<Part>(\w+|\[.*?]\]|`.*?`|"".*?""))\.)*([""`[]?(?<LastPart>\w*))$");
                    var matchParts = reParts.Match(line);
                    if (matchParts.Success)
                    {
                        foreach (Capture capture in matchParts.Groups["Part"].Captures)
                        {
                            parts.Add(capture.Value);
                        }
                    }
                }

                if (parts.Count == 0)
                {
                    AddTablesAndProcedures(intellisense, itemCaptions, conn.DbConnection, null, null);
                }
                else if (parts.Count == 1 && !string.IsNullOrWhiteSpace(parts[0]))
                {
                    var part0 = Utils.UnquoteString(parts[0]);
                    if (!string.IsNullOrWhiteSpace(part0))
                    {
                        AddTablesAndProcedures(intellisense, itemCaptions, conn.DbConnection, dbName, part0);
                        AddTablesAndProcedures(intellisense, itemCaptions, conn.DbConnection, part0, null);

                        AddTableColumns(intellisense, itemCaptions, conn.DbConnection, dbName, null, part0);

                        if (aliases.ContainsKey(part0))
                        {
                            var tableAlias = aliases[part0];
                            AddTableColumns(intellisense, itemCaptions, conn.DbConnection, tableAlias.Database ?? dbName, tableAlias.Schema, tableAlias.Table);
                        }
                    }
                }
                else if (parts.Count == 2 && !string.IsNullOrWhiteSpace(parts[0]))
                {
                    AddTablesAndProcedures(intellisense, itemCaptions, conn.DbConnection, Utils.UnquoteString(parts[0]), Utils.UnquoteString(parts[1]));
                }
                else
                {
                    AddTablesAndProcedures(intellisense, itemCaptions, conn.DbConnection, null, null);
                }
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
Esempio n. 6
0
#pragma warning disable IDE0060 // Remove unused parameter
        public static BaseViewer CreateViewer(IWin32Window owner,
                                              string fileName, StringNoCaseDictionary <string> parameters, List <BaseCommand> commands)
#pragma warning restore IDE0060 // Remove unused parameter
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                return(new OtherViewer());
            }

            var        ext    = Path.GetExtension(fileName)?.ToLower();
            BaseViewer result = null;

            switch (ext)
            {
            case ".docx":
            case ".doc":
            case ".rtf":
            case ".htm":
            case ".html":
            case ".mht":
            case ".odt":
            case ".epub":
                result = new BookViewer();
                break;

            case ".png":
            case ".jpg":
            case ".gif":
            case ".tif":
            case ".tiff":
            case ".bmp":
                result = new ImageViewer();
                break;

            case ".csv":
            case ".txt":
                result = new GridViewer();
                break;

            case ".xlsx":
            case ".xls":
                result = new SpreadsheetViewer();
                break;

            case ".ps1":
            case ".psm1":
            case ".psd1":
            case ".ps":
            case ".csx":
            case ".cs":
            case ".fsx":
            case ".fs":
            case ".sql":
            case ".r":
            case ".py":
                result = new SyntaxViewer();
                break;
            }

            if (result == null)
            {
                result = new OtherViewer();
            }

            try
            {
                if (parameters == null)
                {
                    parameters = new StringNoCaseDictionary <string>();
                }
                if (commands == null)
                {
                    commands = new List <BaseCommand>();
                }

                result.LoadFile(fileName, parameters, commands);
            }
            catch (Exception ex)
            {
                result = new OtherViewer($"Cannot load file: {ex.Message}");
            }

            return(result);
        }