public ConnectionTreeModel Load()
        {
            var connector               = DatabaseConnectorFactory.SqlDatabaseConnectorFromSettings();
            var dataProvider            = new SqlDataProvider(connector);
            var metaDataRetriever       = new SqlDatabaseMetaDataRetriever();
            var databaseVersionVerifier = new SqlDatabaseVersionVerifier(connector);
            var cryptoProvider          = new LegacyRijndaelCryptographyProvider();

            var metaData = metaDataRetriever.GetDatabaseMetaData(connector) ??
                           HandleFirstRun(metaDataRetriever, connector);
            var decryptionKey = GetDecryptionKey(metaData);

            if (!decryptionKey.Any())
            {
                throw new Exception("Could not load SQL connections");
            }

            databaseVersionVerifier.VerifyDatabaseVersion(metaData.ConfVersion);
            var dataTable      = dataProvider.Load();
            var deserializer   = new DataTableDeserializer(cryptoProvider, decryptionKey.First());
            var connectionTree = deserializer.Deserialize(dataTable);

            ApplyLocalConnectionProperties(connectionTree.RootNodes.First(i => i is RootNodeInfo));
            return(connectionTree);
        }
Exemplo n.º 2
0
        public void Save(ConnectionTreeModel connectionTreeModel, string propertyNameTrigger = "")
        {
            var rootTreeNode = connectionTreeModel.RootNodes.OfType <RootNodeInfo>().First();

            UpdateLocalConnectionProperties(rootTreeNode);

            if (PropertyIsLocalOnly(propertyNameTrigger))
            {
                Runtime.MessageCollector.AddMessage(MessageClass.DebugMsg,
                                                    $"Property {propertyNameTrigger} is local only. Not saving to database.");
                return;
            }

            if (SqlUserIsReadOnly())
            {
                Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg,
                                                    "Trying to save connection tree but the SQL read only checkbox is checked, aborting!");
                return;
            }

            using (var dbConnector = DatabaseConnectorFactory.DatabaseConnectorFromSettings())
            {
                dbConnector.Connect();
                var databaseVersionVerifier = new SqlDatabaseVersionVerifier(dbConnector);
                var metaDataRetriever       = new SqlDatabaseMetaDataRetriever();
                var metaData = metaDataRetriever.GetDatabaseMetaData(dbConnector);

                if (!databaseVersionVerifier.VerifyDatabaseVersion(metaData.ConfVersion))
                {
                    Runtime.MessageCollector.AddMessage(MessageClass.ErrorMsg,
                                                        Language.strErrorConnectionListSaveFailed);
                    return;
                }

                metaDataRetriever.WriteDatabaseMetaData(rootTreeNode, dbConnector);
                UpdateConnectionsTable(rootTreeNode, dbConnector);
                UpdateUpdatesTable(dbConnector);
            }

            Runtime.MessageCollector.AddMessage(MessageClass.DebugMsg, "Saved connections to database");
        }
 private SqlConnectionListMetaData HandleFirstRun(SqlDatabaseMetaDataRetriever metaDataRetriever, SqlDatabaseConnector connector)
 {
     metaDataRetriever.WriteDatabaseMetaData(new RootNodeInfo(RootNodeType.Connection), connector);
     return(metaDataRetriever.GetDatabaseMetaData(connector));
 }