public override IModel Create(string connectionString, [CanBeNull] TableSelectionSet tableSelectionSet)
        {
            var model = base.Create(connectionString, tableSelectionSet);

            model.Scaffolding().UseProviderMethodName = nameof(NpgsqlDbContextOptionsExtensions.UseNpgsql);
            return(model);
        }
 private void CheckSelectionsMatched(TableSelectionSet tableSelectionSet)
 {
     foreach (var tableSelection in tableSelectionSet.Tables.Where(t => !t.IsMatched))
     {
         Logger.MissingTableWarning(tableSelection.Text);
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public override IModel Create(string connectionString, TableSelectionSet tableSelectionSet)
        {
            var model = base.Create(connectionString, tableSelectionSet);

            model.Scaffolding().UseProviderMethodName = nameof(SqlServerDbContextOptionsExtensions.UseSqlServer);
            return(model);
        }
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public virtual DatabaseModel Create(string connectionString, TableSelectionSet tableSelectionSet)
        {
            Check.NotEmpty(connectionString, nameof(connectionString));
            Check.NotNull(tableSelectionSet, nameof(tableSelectionSet));

            ResetState();

            using (_connection = new SqliteConnection(connectionString))
            {
                _connection.Open();
                _tableSelectionSet = tableSelectionSet;

                string databaseName = null;
                try
                {
                    databaseName = Path.GetFileNameWithoutExtension(_connection.DataSource);
                }
                catch (ArgumentException)
                {
                    // graceful fallback
                }

                _databaseModel.DatabaseName = !string.IsNullOrEmpty(databaseName)
                    ? databaseName
                    : _connection.DataSource;

                GetTables();
                GetColumns();
                GetIndexes();
                GetForeignKeys();
                return(_databaseModel);
            }
        }
        public DatabaseModel Create(DbConnection connection, TableSelectionSet tableSelectionSet)
        {
            ResetState();

            _connection = (MySqlConnection)connection;

            var connectionStartedOpen = _connection.State == ConnectionState.Open;

            if (!connectionStartedOpen)
            {
                _connection.Open();
            }

            try
            {
                _tableSelectionSet = tableSelectionSet;

                _databaseModel.DatabaseName      = _connection.Database;
                _databaseModel.DefaultSchemaName = null;

                GetTables();
                GetColumns();
                GetIndexes();
                GetConstraints();
                return(_databaseModel);
            }
            finally
            {
                if (!connectionStartedOpen)
                {
                    _connection.Close();
                }
            }
        }
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public virtual DatabaseModel Create(string connectionString, TableSelectionSet tableSelectionSet)
        {
            Check.NotEmpty(connectionString, nameof(connectionString));
            Check.NotNull(tableSelectionSet, nameof(tableSelectionSet));

            ResetState();

            using (_connection = new SqlConnection(connectionString))
            {
                _connection.Open();
                _tableSelectionSet = tableSelectionSet;

                _databaseModel.DatabaseName = _connection.Database;

                Version.TryParse(_connection.ServerVersion, out _serverVersion);
                if (SupportsSequences)
                {
                    GetSequences();
                }

                GetDefaultSchema();
                GetTypeAliases();
                GetTables();
                GetColumns();
                GetIndexes();
                GetForeignKeys();
                return(_databaseModel);
            }
        }
        public static bool Allows(this TableSelectionSet _tableSelectionSet, [NotNull] string schemaName, [NotNull] string tableName)
        {
            if (_tableSelectionSet == null ||
                (_tableSelectionSet.Schemas.Count == 0 &&
                 _tableSelectionSet.Tables.Count == 0))
            {
                return(true);
            }

            var result = false;

            foreach (var schemaSelection in _tableSelectionSet.Schemas)
            {
                if (EqualsWithQuotes(schemaSelection.Text, schemaName))
                {
                    schemaSelection.IsMatched = true;
                    result = true;
                }
            }

            foreach (var tableSelection in _tableSelectionSet.Tables)
            {
                var components = tableSelection.Text.Split('.');
                if (components.Length == 1
                    ? EqualsWithQuotes(components[0], tableName)
                    : EqualsWithQuotes(components[0], schemaName) && EqualsWithQuotes(components[1], tableName))
                {
                    tableSelection.IsMatched = true;
                    result = true;
                }
            }

            return(result);
        }
 void ResetState()
 {
     _connection        = null;
     _tableSelectionSet = null;
     _databaseModel     = new DatabaseModel();
     _tables            = new Dictionary <string, TableModel>();
     _tableColumns      = new Dictionary <string, ColumnModel>(StringComparer.OrdinalIgnoreCase);
 }
        public override IModel Create(string connectionString, TableSelectionSet tableSelectionSet)
        {
            _typeMapper.ConnectionString = connectionString;
            var model = base.Create(connectionString, tableSelectionSet);

            model.Scaffolding().UseProviderMethodName = nameof(MySqlDbContextOptionsExtensions.UseMySql);
            return(model);
        }
Ejemplo n.º 10
0
 void ResetState()
 {
     Logger.LogDebug("ResetState");
     _connection        = null;
     _tableSelectionSet = null;
     _databaseModel     = new DatabaseModel();
     _tables            = new Dictionary <string, DatabaseTable>();
     _tableColumns      = new Dictionary <string, DatabaseColumn>(StringComparer.OrdinalIgnoreCase);
 }
Ejemplo n.º 11
0
 private void ResetState()
 {
     _connection        = null;
     _serverVersion     = null;
     _tableSelectionSet = null;
     _databaseModel     = new DatabaseModel();
     _tables            = new Dictionary <string, DatabaseTable>();
     _tableColumns      = new Dictionary <string, DatabaseColumn>(StringComparer.OrdinalIgnoreCase);
 }
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public virtual DatabaseModel Create(DbConnection connection, IEnumerable <string> tables, IEnumerable <string> schemas)
        {
            Check.NotNull(connection, nameof(connection));
            Check.NotNull(tables, nameof(tables));

            ResetState();

            _connection = connection as SqlCeConnection;

            var connectionStartedOpen = (_connection != null) && (_connection.State == ConnectionState.Open);

            if (!connectionStartedOpen)
            {
                _connection?.Open();
            }
            try
            {
                _tableSelectionSet = new TableSelectionSet(tables, schemas);

                string databaseName = null;
                try
                {
                    if (_connection != null)
                    {
                        databaseName = Path.GetFileNameWithoutExtension(_connection.DataSource);
                    }
                }
                catch (ArgumentException)
                {
                    // graceful fallback
                }

                if (_connection != null)
                {
                    _databaseModel.DatabaseName = !string.IsNullOrEmpty(databaseName) ? databaseName : _connection.DataSource;
                }

                GetTables();
                GetColumns();
                GetPrimaryKeys();
                GetUniqueConstraints();
                GetIndexes();
                GetForeignKeys();

                CheckSelectionsMatched(_tableSelectionSet);

                return(_databaseModel);
            }
            finally
            {
                if (!connectionStartedOpen)
                {
                    _connection?.Close();
                }
            }
        }
        public DatabaseModel Create(string connectionString, TableSelectionSet tableSelectionSet)
        {
            // Check.NotEmpty(connectionString, nameof(connectionString));
            // Check.NotNull(tableSelectionSet, nameof(tableSelectionSet));

            using (var connection = new MySqlConnection(connectionString))
            {
                return(Create(connection, tableSelectionSet));
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public virtual DatabaseModel Create(string connectionString, TableSelectionSet tableSelectionSet)
        {
            Check.NotEmpty(connectionString, nameof(connectionString));
            Check.NotNull(tableSelectionSet, nameof(tableSelectionSet));

            using (var connection = new SqliteConnection(connectionString))
            {
                return(Create(connection, tableSelectionSet));
            }
        }
Ejemplo n.º 15
0
        public virtual DatabaseModel Create(DbConnection connection, IEnumerable <string> tables, IEnumerable <string> schemas)
        {
            Check.NotNull(connection, nameof(connection));
            Check.NotNull(tables, nameof(tables));
            Check.NotNull(schemas, nameof(schemas));

            ResetState();

            _connection = connection;

            var connectionStartedOpen = _connection.State == ConnectionState.Open;

            if (!connectionStartedOpen)
            {
                _connection.Open();
            }
            try
            {
                _tableSelectionSet = new TableSelectionSet(tables, schemas);

                _databaseModel.DatabaseName = _connection.Database;

                Version.TryParse(_connection.ServerVersion, out _serverVersion);

                if (SupportsSequences)
                {
                    GetSequences();
                }

                GetDefaultSchema();
                GetTables();
                GetColumns();
                GetPrimaryKeys();
                GetUniqueConstraints();
                GetIndexes();
                GetForeignKeys();

                CheckSelectionsMatched(_tableSelectionSet);

                return(_databaseModel);
            }
            finally
            {
                if (!connectionStartedOpen)
                {
                    _connection.Close();
                }
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public virtual DatabaseModel Create(DbConnection connection, TableSelectionSet tableSelectionSet)
        {
            ResetState();

            _connection = connection as SqlCeConnection;

            var connectionStartedOpen = (_connection != null) && (_connection.State == ConnectionState.Open);

            if (!connectionStartedOpen)
            {
                _connection?.Open();
            }
            try
            {
                _tableSelectionSet = tableSelectionSet;

                string databaseName = null;
                try
                {
                    if (_connection != null)
                    {
                        databaseName = Path.GetFileNameWithoutExtension(_connection.DataSource);
                    }
                }
                catch (ArgumentException)
                {
                    // graceful fallback
                }

                if (_connection != null)
                {
                    _databaseModel.DatabaseName = !string.IsNullOrEmpty(databaseName) ? databaseName : _connection.DataSource;
                }

                GetTables();
                GetColumns();
                GetIndexes();
                GetForeignKeys();
                return(_databaseModel);
            }
            finally
            {
                if (!connectionStartedOpen)
                {
                    _connection?.Close();
                }
            }
        }
Ejemplo n.º 17
0
        public override IModel Create(string connectionString, TableSelectionSet tableSelectionSet)
        {
            if ((tableSelectionSet != null) &&
                tableSelectionSet.Schemas.Any())
            {
                //TODO ErikEJ fix logging (see sqlite provider)
                //Logger.LogWarning("You have specified some schema selections. The SQL Server Compact provider does not support these and they will be ignored. Note: it does support table selections.");

                tableSelectionSet.Schemas.ToList().ForEach(s => s.IsMatched = true);
            }

            var model = base.Create(connectionString, tableSelectionSet);

            model.Scaffolding().UseProviderMethodName = nameof(SqlCeDbContextOptionsExtensions.UseSqlCe);
            return(model);
        }
Ejemplo n.º 18
0
        public virtual DatabaseModel Create(DbConnection connection, IEnumerable <string> tables, IEnumerable <string> schemas)
        {
            ResetState();

            _connection = (NpgsqlConnection)connection;

            var connectionStartedOpen = _connection.State == ConnectionState.Open;

            if (!connectionStartedOpen)
            {
                _connection.Open();
            }

            try
            {
                _tableSelectionSet = new TableSelectionSet(tables, schemas);

                _databaseModel.DatabaseName  = _connection.Database;
                _databaseModel.DefaultSchema = "public";

                GetTables();
                GetColumns();
                GetIndexes();
                GetConstraints();
                GetSequences();
                GetExtensions();

                // We may have dropped columns. We load these because constraints take them into
                // account when referencing columns, but must now get rid of them before returning
                // the database model.
                foreach (var table in _databaseModel.Tables)
                {
                    while (table.Columns.Remove(null))
                    {
                    }
                }

                return(_databaseModel);
            }
            finally
            {
                if (!connectionStartedOpen)
                {
                    _connection.Close();
                }
            }
        }
        public override IModel Create(string connectionString, TableSelectionSet tableSelectionSet)
        {
            if (tableSelectionSet != null &&
                tableSelectionSet.Schemas.Any())
            {
                Logger.LogWarning(SqliteDesignStrings.UsingSchemaSelectionsWarning);

                // we've logged a general warning above that sqlite ignores all
                // schema selections so mark all of them as matched so that we don't
                // also log warnings about not matching each individual selection
                tableSelectionSet.Schemas.ToList().ForEach(s => s.IsMatched = true);
            }

            var model = base.Create(connectionString, tableSelectionSet);

            model.Scaffolding().UseProviderMethodName = nameof(SqliteDbContextOptionsBuilderExtensions.UseSqlite);
            return(model);
        }
Ejemplo n.º 20
0
        /// <summary>
        ///     Tests whether the schema/table is allowed by the <see cref="TableSelectionSet" />
        ///     and updates the <see cref="TableSelectionSet" />'s <see cref="TableSelectionSet.Selection" />(s)
        ///     to mark that they have been matched.
        /// </summary>
        /// <param name="tableSelectionSet"> the <see cref="TableSelectionSet" /> to test </param>
        /// <param name="schemaName"> name of the database schema to check </param>
        /// <param name="tableName"> name of the database table to check </param>
        /// <returns> whether or not the schema/table is allowed </returns>
        public static bool Allows(this TableSelectionSet tableSelectionSet, [CanBeNull] string schemaName, [NotNull] string tableName)
        {
            if (tableSelectionSet == null ||
                tableSelectionSet.Schemas.Count == 0 &&
                tableSelectionSet.Tables.Count == 0)
            {
                return(true);
            }

            var result = false;

            //TODO: look into performance for large selection sets and numbers of tables
            if (schemaName != null)
            {
                foreach (var pattern in _schemaPatterns)
                {
                    var patternToMatch           = pattern.Replace("{schema}", schemaName);
                    var matchingSchemaSelections = tableSelectionSet.Schemas.Where(
                        s => s.Text.Equals(patternToMatch, StringComparison.OrdinalIgnoreCase))
                                                   .ToList();
                    if (matchingSchemaSelections.Any())
                    {
                        matchingSchemaSelections.ForEach(selection => selection.IsMatched = true);
                        result = true;
                    }
                }
            }

            foreach (var pattern in _tablePatterns)
            {
                var patternToMatch          = pattern.Replace("{schema}", schemaName).Replace("{table}", tableName);
                var matchingTableSelections = tableSelectionSet.Tables.Where(
                    t => t.Text.Equals(patternToMatch, StringComparison.OrdinalIgnoreCase))
                                              .ToList();
                if (matchingTableSelections.Any())
                {
                    matchingTableSelections.ForEach(selection => selection.IsMatched = true);
                    result = true;
                }
            }

            return(result);
        }
Ejemplo n.º 21
0
        public DatabaseModel Create(DbConnection connection, TableSelectionSet tableSelectionSet)
        {
            ResetState();

            _connection = (FbConnection)connection;

            var connectionStartedOpen = _connection.State == ConnectionState.Open;

            if (!connectionStartedOpen)
            {
                _connection.Open();
            }

            try
            {
                _tableSelectionSet = tableSelectionSet;

                _databaseModel.DatabaseName  = _connection.Database;
                _databaseModel.DefaultSchema = null;

                Logger.LogDebug("GetTables");
                GetTables();
                Logger.LogDebug("GetColumns");
                GetColumns();
                Logger.LogDebug("GetPrimaryKeys");
                GetPrimaryKeys();
                Logger.LogDebug("GetIndexes");
                GetIndexes();
                Logger.LogDebug("GetConstraints");
                GetConstraints();
                Logger.LogDebug("GetConstraints completed");

                return(_databaseModel);
            }
            finally
            {
                if (!connectionStartedOpen)
                {
                    _connection.Close();
                }
            }
        }
Ejemplo n.º 22
0
        public static bool Allows(this TableSelectionSet tableSet, string tableName)
        {
            if ((tableSet == null) ||
                (tableSet.Tables.Count == 0))
            {
                return(true);
            }

            var result = false;
            var matchingTableSelections = tableSet.Tables.Where(
                t => t.Text.Equals(tableName, StringComparison.OrdinalIgnoreCase)).ToList();

            if (matchingTableSelections.Any())
            {
                matchingTableSelections.ToList().ForEach(selection => selection.IsMatched = true);
                result = true;
            }

            return(result);
        }
Ejemplo n.º 23
0
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public virtual DatabaseModel Create(DbConnection connection, TableSelectionSet tableSelectionSet)
        {
            ResetState();

            _connection = connection;

            var connectionStartedOpen = _connection.State == ConnectionState.Open;

            if (!connectionStartedOpen)
            {
                _connection.Open();
            }
            try
            {
                _tableSelectionSet = tableSelectionSet;

                _databaseModel.DatabaseName = _connection.Database;

                Version.TryParse(_connection.ServerVersion, out _serverVersion);
                if (SupportsSequences)
                {
                    GetSequences();
                }

                GetDefaultSchema();
                GetTypeAliases();
                GetTables();
                GetColumns();
                GetIndexes();
                GetForeignKeys();
                return(_databaseModel);
            }
            finally
            {
                if (!connectionStartedOpen)
                {
                    _connection.Close();
                }
            }
        }
        /// <summary>
        ///     Tests whether the table is allowed by the <see cref="TableSelectionSet" /> and
        ///     updates the <see cref="TableSelectionSet" />'s <see cref="TableSelectionSet.Selection" />(s)
        ///     to mark that they have been matched.
        /// </summary>
        /// <param name="tableSet"> the <see cref="TableSelectionSet" /> to test </param>
        /// <param name="tableName"> name of the database table to check </param>
        /// <returns> whether or not the table is allowed </returns>
        public static bool Allows(this TableSelectionSet tableSet, string tableName)
        {
            if (tableSet == null ||
                tableSet.Tables.Count == 0)
            {
                return(true);
            }

            //TODO: look into performance for large selection sets and numbers of tables
            var result = false;
            var matchingTableSelections = tableSet.Tables.Where(
                t => t.Text.Equals(tableName, StringComparison.OrdinalIgnoreCase))
                                          .ToList();

            if (matchingTableSelections.Any())
            {
                matchingTableSelections.ForEach(selection => selection.IsMatched = true);
                result = true;
            }

            return(result);
        }
Ejemplo n.º 25
0
        private IEnumerable <DatabaseTable> GetTables(DbConnection connection, IEnumerable <string> tables)
        {
            var tableSelectionSet = new TableSelectionSet(tables);

            var command = connection.CreateCommand();

            command.CommandText = new StringBuilder()
                                  .AppendLine("SELECT \"name\"")
                                  .AppendLine("FROM \"sqlite_master\"")
                                  .Append("WHERE \"type\" = 'table' AND instr(\"name\", 'sqlite_') <> 1 AND \"name\" <> '")
                                  .Append(HistoryRepository.DefaultTableName)
                                  .AppendLine("';")
                                  .ToString();

            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    var name = reader.GetString(0);
                    if (!tableSelectionSet.Allows(name))
                    {
                        continue;
                    }

                    _logger.TableFound(name);

                    var table = new DatabaseTable {
                        Name = name
                    };

                    foreach (var column in GetColumns(connection, name))
                    {
                        column.Table = table;
                        table.Columns.Add(column);
                    }

                    var primaryKey = GetPrimaryKey(connection, name, table.Columns);
                    if (primaryKey != null)
                    {
                        primaryKey.Table = table;
                        table.PrimaryKey = primaryKey;
                    }

                    foreach (var uniqueConstraints in GetUniqueConstraints(connection, name, table.Columns))
                    {
                        uniqueConstraints.Table = table;
                        table.UniqueConstraints.Add(uniqueConstraints);
                    }

                    foreach (var index in GetIndexes(connection, name, table.Columns))
                    {
                        index.Table = table;
                        table.Indexes.Add(index);
                    }

                    yield return(table);
                }
            }

            foreach (var tableSelection in tableSelectionSet.Tables.Where(t => !t.IsMatched))
            {
                _logger.MissingTableWarning(tableSelection.Text);
            }
        }