public void PrepareInsertVersionCommand(IDbCommand command, DatabaseVersion version, 
            string failedUpgradeFromVersion, string failedUpgradeToVersion, string failedUpgradeError)
        {
            command.CommandText = "INSERT INTO \"Version\" (\"Product\", \"Version\", " +
                "\"FailedUpgradeFromVersion\", \"FailedUpgradeToVersion\", \"FailedUpgradeError\") " +
                "VALUES (:product, :version, :failedupgradefromversion, " +
                ":failedupgradetoversion, :failedupgradeerror)";

            IDbDataParameter productParameter = command.CreateParameter();
            IDbDataParameter versionParameter = command.CreateParameter();
            IDbDataParameter failedUpgradeFromVersionParameter = command.CreateParameter();
            IDbDataParameter failedUpgradeToVersionParameter = command.CreateParameter();
            IDbDataParameter failedUpgradeErrorParameter = command.CreateParameter();

            command.Parameters.Add(productParameter);
            command.Parameters.Add(versionParameter);
            command.Parameters.Add(failedUpgradeFromVersionParameter);
            command.Parameters.Add(failedUpgradeToVersionParameter);
            command.Parameters.Add(failedUpgradeErrorParameter);

            productParameter.ParameterName = "product";
            productParameter.DbType = DbType.String;
            productParameter.Size = 250;

            versionParameter.ParameterName = "version";
            versionParameter.DbType = DbType.String;
            versionParameter.Size = 250;

            failedUpgradeFromVersionParameter.ParameterName = "failedupgradefromversion";
            failedUpgradeFromVersionParameter.DbType = DbType.String;
            failedUpgradeFromVersionParameter.Size = 50;

            failedUpgradeToVersionParameter.ParameterName = "failedupgradetoversion";
            failedUpgradeToVersionParameter.DbType = DbType.String;
            failedUpgradeToVersionParameter.Size = 50;

            failedUpgradeErrorParameter.ParameterName = "failedupgradeerror";
            failedUpgradeErrorParameter.DbType = DbType.String;
            failedUpgradeErrorParameter.Size = 4000;

            productParameter.Value = version.Product;
            versionParameter.Value = version.Version;

            if (failedUpgradeFromVersion != null)
            {
                failedUpgradeFromVersionParameter.Value = failedUpgradeFromVersion;
                failedUpgradeToVersionParameter.Value = failedUpgradeToVersion;
                failedUpgradeErrorParameter.Value = failedUpgradeError;
            }
            else
            {
                failedUpgradeFromVersionParameter.Value = DBNull.Value;
                failedUpgradeToVersionParameter.Value = DBNull.Value;
                failedUpgradeErrorParameter.Value = DBNull.Value;
            }
        }
        public void PrepareInsertVersionCommand(IDbCommand command, DatabaseVersion version, 
            string failedUpgradeFromVersion, string failedUpgradeToVersion, string failedUpgradeError)
        {
            command.CommandText = "INSERT INTO Version (Product, Version, " +
                "FailedUpgradeFromVersion, FailedUpgradeToVersion, FailedUpgradeError) " +
                "VALUES (@Product, @Version, @FailedUpgradeFromVersion, " +
                "@FailedUpgradeToVersion, @FailedUpgradeError)";

            IDbDataParameter productParameter = command.CreateParameter();
            IDbDataParameter versionParameter = command.CreateParameter();
            IDbDataParameter failedUpgradeFromVersionParameter = command.CreateParameter();
            IDbDataParameter failedUpgradeToVersionParameter = command.CreateParameter();
            IDbDataParameter failedUpgradeErrorParameter = command.CreateParameter();

            command.Parameters.Add(productParameter);
            command.Parameters.Add(versionParameter);
            command.Parameters.Add(failedUpgradeFromVersionParameter);
            command.Parameters.Add(failedUpgradeToVersionParameter);
            command.Parameters.Add(failedUpgradeErrorParameter);

            productParameter.ParameterName = "@Product";
            productParameter.DbType = DbType.String;
            productParameter.Size = 250;

            productParameter.ParameterName = "@Version";
            versionParameter.DbType = DbType.String;
            versionParameter.Size = 250;

            failedUpgradeFromVersionParameter.ParameterName = "@FailedUpgradeFromVersion";
            failedUpgradeFromVersionParameter.DbType = DbType.String;
            failedUpgradeFromVersionParameter.Size = 50;

            failedUpgradeToVersionParameter.ParameterName = "@FailedUpgradeToVersion";
            failedUpgradeToVersionParameter.DbType = DbType.String;
            failedUpgradeToVersionParameter.Size = 50;

            failedUpgradeErrorParameter.ParameterName = "@FailedUpgradeError";
            failedUpgradeErrorParameter.DbType = DbType.String;
            failedUpgradeErrorParameter.Size = 4000;

            productParameter.Value = version.Product;
            versionParameter.Value = version.Version;

            if (failedUpgradeFromVersion != null)
            {
                failedUpgradeFromVersionParameter.Value = failedUpgradeFromVersion;
                failedUpgradeToVersionParameter.Value = failedUpgradeToVersion;
                failedUpgradeErrorParameter.Value = failedUpgradeError;
            }
            else
            {
                failedUpgradeFromVersionParameter.Value = DBNull.Value;
                failedUpgradeToVersionParameter.Value = DBNull.Value;
                failedUpgradeErrorParameter.Value = DBNull.Value;
            }
        }
예제 #3
0
 protected void UpdateVersionTable(DatabaseVersion version)
 {
     UpdateVersionTable(version, null, null, null);
 }
예제 #4
0
        protected void UpdateVersionTable(DatabaseVersion version, 
            string failedUpgradeFromVersion, string failedUpgradeToVersion,
            string failedUpgradeError)
        {
            using (IDbTransaction transaction = _connection.BeginTransaction(IsolationLevel.RepeatableRead))
            {
                using (IDbCommand command = _connection.CreateCommand())
                {
                    command.Transaction = transaction;
                    _implementation.PrepareDeleteVersionCommand(command);
                    command.ExecuteNonQuery();
                }

                using (IDbCommand command = _connection.CreateCommand())
                {
                    command.Transaction = transaction;

                    _implementation.PrepareInsertVersionCommand(command, version,
                        failedUpgradeFromVersion, failedUpgradeToVersion, failedUpgradeError);

                    command.ExecuteNonQuery();
                }

                transaction.Commit();
            }
        }
예제 #5
0
        public DatabaseVersion GetVersion()
        {
            if (!VersionTableExists())
            {
                throw new DatabaseVersionTableMissingException("The database does not " +
                    "contain a database version table; it is probably a new database.");
            }

            try
            {
                using (IDbCommand command = _connection.CreateCommand())
                {
                    _implementation.PrepareFetchVersionRowsCommand(command);

                    using (IDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult))
                    {
                        // Check for a missing row or invalid fields:
                        if (!reader.Read())
                        {
                            throw new DatabaseVersionTableInvalidException(
                                "The database version table does not contain any valid " +
                                "version data.");
                        }

                        if (reader.FieldCount < 5)
                        {
                            throw new DatabaseVersionTableInvalidException(
                              "The database version table has too few fields to be " +
                              "valid. At least 5 fields are expected.");
                        }

                        if (reader.GetName(0) != "Product" ||
                            reader.GetName(1) != "Version" ||
                            reader.GetName(2) != "FailedUpgradeFromVersion" ||
                            reader.GetName(3) != "FailedUpgradeToVersion" ||
                            reader.GetName(4) != "FailedUpgradeError")
                        {
                            throw new DatabaseVersionTableInvalidException(
                              "One of the database version table fields is not the " +
                              "correct field name.");
                        }

                        // Check for an upgrade error:
                        if (!reader.IsDBNull(2))
                        {
                            if (!reader.IsDBNull(3) && !reader.IsDBNull(4))
                            {
                                throw new DatabaseUpgradeFailedException(
                                    reader.GetString(2), reader.GetString(3),
                                    reader.GetString(4));
                            }
                            else
                            {
                                throw new DatabaseVersionTableInvalidException(
                                    "One of the database version upgrade failure warning " +
                                    "fields is not set, when one or more of the others is.");
                            }
                        }

                        // Get the version:
                        DatabaseVersion version = new DatabaseVersion(reader.GetString(0),
                            reader.GetString(1));

                        // Check for multiple rows:
                        if (reader.Read())
                        {
                            throw new DatabaseVersionTableInvalidException(
                                "The database version table includes more than one version row, " +
                                "and is therefore invalid.");
                        }

                        if (version.Product != _product)
                        {
                            throw new DatabaseVersionTableInvalidException(
                                "The database version is not for this product.");
                        }

                        return version;
                    }
                }
            }
            catch (DbException ex)
            {
                throw new DatabaseVersionTableInvalidException("The database version " +
                    "table could not be read due to an unexpected error; check the " +
                    "full error description for details on the specific error.", ex);
            }
        }