public void JetCommandCannotBeLoggedAfterDispose()
        {
            var command = new JetCommand();

            command.CommandText = "foo";
            command.Parameters.Add(command.CreateParameter());
            command.Dispose();

            Assert.Equal(command.CommandText, string.Empty);
            Assert.Equal(command.Parameters.Count, 0);
        }
Пример #2
0
        private static DbDataReader GetDbDataReaderFromSimpleStatement(JetCommand command)
        {
            // Command text format is
            // SELECT * FROM `INFORMATION_SCHEMA.<what>` [WHERE <condition>] [ORDER BY <order>]
            //    <what> :=
            //          tables
            //          columns
            //          indexes
            //          index_columns
            //          relations
            //          relation_columns
            //          check_constraints

            var jetConnection        = (JetConnection)command.Connection;
            var innerCommand         = command.InnerCommand;
            var commandText          = innerCommand.CommandText;
            var innerConnection      = command.InnerCommand.Connection;
            var innerConnectionState = innerConnection.State;

            var match = _regExParseInformationSchemaCommand.Match(commandText);

            if (!match.Success)
            {
                return(null);
            }

            var dbObject     = match.Groups["object"].Value;
            var conditions   = match.Groups["conditions"].Value;
            var orderColumns = match.Groups["orderColumns"].Value;

            Func <JetConnection, DataTable> schemaMethod = dbObject.ToLower() switch
            {
                "tables" => GetTables,
                "columns" => GetColumns,
                "indexes" => GetIndexes,
                "index_columns" => GetIndexColumns,
                "relations" => GetRelations,
                "relation_columns" => GetRelationColumns,
                "check_constraints" => GetCheckConstraints,
                _ => null
            };

            if (schemaMethod == null)
            {
                return(null);
            }

            if (innerConnectionState != ConnectionState.Open)
            {
                innerConnection.Open();
            }

            DataTable dataTable;

            try
            {
                dataTable = schemaMethod(jetConnection);
            }
            finally
            {
                if (innerConnectionState != ConnectionState.Open)
                {
                    innerConnection.Close();
                }
            }

            if (string.IsNullOrWhiteSpace(conditions) &&
                string.IsNullOrWhiteSpace(orderColumns))
            {
                return(dataTable.CreateDataReader());
            }

            var selectedRows = dataTable.Select(
                string.IsNullOrWhiteSpace(conditions)
                    ? null
                    : conditions,
                string.IsNullOrWhiteSpace(orderColumns)
                    ? null
                    : orderColumns);

            var selectedDataTable = dataTable.Clone();

            foreach (var row in selectedRows)
            {
                selectedDataTable.ImportRow(row);
            }

            return(selectedDataTable.CreateDataReader());
        }
Пример #3
0
 public static bool TryGetDataReaderFromInformationSchemaCommand(JetCommand command, out DbDataReader dataReader)
 => (dataReader = GetDbDataReaderFromSimpleStatement(command)) != null;
        public static bool ProcessDatabaseOperation(JetCommand command)
        {
            var commandText = command.CommandText;

            var match = _regExIsCreateOrDropDatabaseCommand.Match(commandText);

            if (!match.Success)
            {
                return(false);
            }

            //
            // CREATE DATABASE:
            //

            match = _regExParseCreateDatabaseCommand.Match(commandText);
            if (match.Success)
            {
                var fileName         = UnescapeSingleQuotes(match.Groups["filename"].Value);
                var databasePassword = UnescapeSingleQuotes(match.Groups["password"].Value);

                if (string.IsNullOrWhiteSpace(fileName))
                {
                    throw new InvalidOperationException("CREATE DATABASE statement is missing database file name.");
                }

                JetConnection.CreateDatabase(fileName, databasePassword: databasePassword);
                return(true);
            }

            match = _regExParseObsoleteCreateDatabaseCommand.Match(commandText);
            if (match.Success)
            {
                var fileName = match.Groups["filename"].Value;

                if (string.IsNullOrWhiteSpace(fileName))
                {
                    throw new InvalidOperationException("CREATE DATABASE statement is missing database file name or connection string.");
                }

                JetConnection.CreateDatabase(fileName);
                return(true);
            }

            match = _regExParseObsoleteCreateDatabaseCommandFromConnection.Match(commandText);
            if (match.Success)
            {
                var connectionString = ExtractFileNameFromConnectionString(match.Groups["connectionString"].Value);

                if (string.IsNullOrWhiteSpace(connectionString))
                {
                    throw new InvalidOperationException("CREATE DATABASE statement is missing database file name or connection string.");
                }

                JetConnection.CreateDatabase(connectionString);
                return(true);
            }

            //
            // DROP DATABASE:
            //

            match = _regExParseDropDatabaseCommand.Match(commandText);
            if (match.Success)
            {
                var fileName = ExpandFileName(UnescapeSingleQuotes(match.Groups["filename"].Value));

                if (string.IsNullOrWhiteSpace(fileName))
                {
                    throw new InvalidOperationException("DROP DATABASE statement is missing database file name or connection string.");
                }

                JetConnection.DropDatabase(fileName);
                return(true);
            }

            match = _regExParseObsoleteDropDatabaseCommand.Match(commandText);
            if (match.Success)
            {
                var fileName = match.Groups["filename"].Value;

                if (string.IsNullOrWhiteSpace(fileName))
                {
                    throw new InvalidOperationException("DROP DATABASE statement is missing database file name or connection string.");
                }

                JetConnection.DropDatabase(fileName);
                return(true);
            }

            match = _regExParseObsoleteDropDatabaseCommandFromConnection.Match(commandText);
            if (match.Success)
            {
                var connectionString = match.Groups["connectionString"].Value;

                if (string.IsNullOrWhiteSpace(connectionString))
                {
                    throw new InvalidOperationException("DROP DATABASE statement is missing database file name or connection string.");
                }

                JetConnection.DropDatabase(connectionString);
                return(true);
            }

            throw new Exception($"\"{commandText}\" is not a valid database command.");
        }
        public static bool ProcessDatabaseOperation(JetCommand command)
        {
            var commandText = command.CommandText;

            var match = _regExIsCreateOrDropDatabaseCommand.Match(commandText);

            if (!match.Success)
            {
                return(false);
            }

            match = _regExParseCreateDatabaseCommand.Match(commandText);
            if (match.Success)
            {
                var fileName = ExpandFileName(
                    UnescapeSingleQuotes(
                        match.Groups["filename"]
                        .Value));

                if (string.IsNullOrWhiteSpace(fileName))
                {
                    throw new InvalidOperationException("CREATE DATABASE statement is missing the database file name.");
                }

                var databaseCreator = new DaoDatabaseCreator();
                databaseCreator.CreateDatabase(fileName);
                return(true);
            }

            match = _regExParseObsoleteCreateDatabaseCommandFromConnection.Match(commandText);
            if (match.Success)
            {
                var databaseCreator = new DaoDatabaseCreator();
                databaseCreator.CreateDatabase(ExtractFileNameFromConnectionString(match.Groups["connectionString"].Value));

                return(true);
            }

            match = _regExParseObsoleteCreateDatabaseCommand.Match(commandText);
            if (match.Success)
            {
                var fileName = match.Groups["filename"]
                               .Value;
                if (string.IsNullOrWhiteSpace(fileName))
                {
                    throw new Exception("Missing file name");
                }

                var databaseCreator = new DaoDatabaseCreator();
                databaseCreator.CreateDatabase(fileName);
                return(true);
            }

            match = _regExParseObsoleteDropDatabaseCommandFromConnection.Match(commandText);
            if (match.Success)
            {
                var connectionString = match.Groups["connectionString"]
                                       .Value;
                var fileName = ExtractFileNameFromConnectionString(connectionString);

                if (string.IsNullOrWhiteSpace(fileName))
                {
                    throw new Exception("Missing file name");
                }

                DeleteFile(fileName);
                return(true);
            }

            match = _regExParseDropDatabaseCommand.Match(commandText);
            if (match.Success)
            {
                var fileName = ExpandFileName(
                    UnescapeSingleQuotes(
                        match.Groups["filename"]
                        .Value));

                if (string.IsNullOrWhiteSpace(fileName))
                {
                    throw new InvalidOperationException("DROP DATABASE statement is missing the database file name.");
                }

                DeleteFile(fileName);
                return(true);
            }

            match = _regExParseObsoleteDropDatabaseCommand.Match(commandText);
            if (match.Success)
            {
                var fileName = match.Groups["filename"]
                               .Value;

                if (string.IsNullOrWhiteSpace(fileName))
                {
                    throw new Exception("Missing file name");
                }

                DeleteFile(fileName);
                return(true);
            }

            throw new Exception(commandText + " is not a valid database command");
        }