<commit_before>﻿using System;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using FluentMigrator.Builders.Execute;
using FluentMigrator.Runner.Generators;
using FluentMigrator.Runner.Generators.Hana;
using FluentMigrator.Runner.Helpers;

namespace FluentMigrator.Runner.Processors.Hana
{
    public class HanaProcessor : GenericProcessorBase
    {
        public override string DatabaseType
        {
            get { return "Hana"; }
        }

        public override bool SupportsTransactions
        {
            get { return true; }
        }

        public HanaProcessor(IDbConnection connection, IMigrationGenerator generator, IAnnouncer announcer, IMigrationProcessorOptions options, IDbFactory factory)
            : base(connection, factory, generator, announcer, options)
        {
        }

        public IQuoter Quoter
        {
            get { return ((HanaGenerator)Generator).Quoter; }
        }

        public override bool SchemaExists(string schemaName)
        {
            return false;
        }

        public override bool TableExists(string schemaName, string tableName)
        {
            if (string.IsNullOrEmpty(tableName))
                throw new ArgumentNullException("tableName");

            return Exists(
                "SELECT 1 FROM TABLES WHERE SCHEMA_NAME = CURRENT_SCHEMA AND upper(TABLE_NAME) = upper('{0}')",
                FormatHelper.FormatSqlEscape(Quoter.UnQuote(tableName).ToUpper()));
        }

        public override bool ColumnExists(string schemaName, string tableName, string columnName)
        {
            if (tableName == null)
                throw new ArgumentNullException("tableName");
            if (columnName == null)
                throw new ArgumentNullException("columnName");

            return Exists("SELECT 1 FROM TABLE_COLUMNS WHERE SCHEMA_NAME = CURRENT_SCHEMA AND upper(TABLE_NAME) = '{0}' AND upper(COLUMN_NAME) = '{1}'",
                FormatHelper.FormatSqlEscape(Quoter.UnQuote(tableName).ToUpper()),
                FormatHelper.FormatSqlEscape(Quoter.UnQuote(columnName).ToUpper()));
        }

        public override bool ConstraintExists(string schemaName, string tableName, string constraintName)
        {
            if (tableName == null)
                throw new ArgumentNullException("tableName");
            if (constraintName == null)
                throw new ArgumentNullException("constraintName");

            if (constraintName.Length == 0)
                return false;

            return Exists("SELECT 1 FROM CONSTRAINTS WHERE SCHEMA_NAME = CURRENT_SCHEMA and upper(CONSTRAINT_NAME) = '{0}'",
                FormatHelper.FormatSqlEscape(Quoter.UnQuote(constraintName).ToUpper()));
        }

        public override bool IndexExists(string schemaName, string tableName, string indexName)
        {
            if (tableName == null)
                throw new ArgumentNullException("tableName");
            if (indexName == null)
                throw new ArgumentNullException("indexName");

            if (indexName.Length == 0)
                return false;

            return Exists("SELECT 1 FROM INDEXES WHERE SCHEMA_NAME = CURRENT_SCHEMA AND upper(INDEX_NAME) = '{0}'",
                    FormatHelper.FormatSqlEscape(Quoter.UnQuote(indexName).ToUpper()));
        }

        public override bool SequenceExists(string schemaName, string sequenceName)
        {
            if (sequenceName == null)
                throw new ArgumentNullException("sequenceName");

            if (string.IsNullOrEmpty(sequenceName))
                return false;

            return Exists("SELECT 1 FROM SEQUENCES WHERE SCHEMA_NAME = CURRENT_SCHEMA and upper(SEQUENCE_NAME) = '{0}'",
                FormatHelper.FormatSqlEscape(Quoter.UnQuote(sequenceName).ToUpper()));
        }

        public override bool DefaultValueExists(string schemaName, string tableName, string columnName, object defaultValue)
        {
            return false;
        }

        public override void Execute(string template, params object[] args)
        {
            Process(string.Format(template, args));
        }

        public override bool Exists(string template, params object[] args)
        {
            if (template == null)
                throw new ArgumentNullException("template");

            EnsureConnectionIsOpen();

            var querySql = String.Format(template, args);

            Announcer.Sql(string.Format("{0};", querySql));

            using (var command = Factory.CreateCommand(String.Format(template, args), Connection))
            using (var reader = command.ExecuteReader())
            {
                return reader.Read();
            }
        }

        public override DataSet ReadTableData(string schemaName, string tableName)
        {
            if (tableName == null)
                throw new ArgumentNullException("tableName");

            if (String.IsNullOrEmpty(schemaName))
                return Read("SELECT * FROM {0}", Quoter.QuoteTableName(tableName));

            return Read("SELECT * FROM {0}.{1}", Quoter.QuoteSchemaName(schemaName), Quoter.QuoteTableName(tableName));
        }

        public override DataSet Read(string template, params object[] args)
        {
            if (template == null)
                throw new ArgumentNullException("template");

            EnsureConnectionIsOpen();

            var result = new DataSet();
            using (var command = Factory.CreateCommand(String.Format(template, args), Connection))
            {
                var adapter = Factory.CreateDataAdapter(command);
                adapter.Fill(result);
                return result;
            }
        }

        public override void Process(PerformDBOperationExpression expression)
        {
            Announcer.Say("Performing DB Operation");

            if (Options.PreviewOnly)
                return;

            EnsureConnectionIsOpen();

            if (expression.Operation != null)
                expression.Operation(Connection, null);
        }

        protected override void Process(string sql)
        {
            Announcer.Sql(sql);

            if (Options.PreviewOnly || string.IsNullOrEmpty(sql))
                return;

            EnsureConnectionIsOpen();

            var batches = Regex.Split(sql, @"^\s*;\s*$", RegexOptions.Multiline)
                .Where(x => !string.IsNullOrEmpty(x))
                .Select(x => x.Trim().Replace(";", ""));

            foreach (var batch in batches)
            {
                using (var command = Factory.CreateCommand(batch, Connection))
                    command.ExecuteNonQuery();
            }
        }
    }
}
<commit_msg>Fix error when execute script's.<commit_after>﻿using System;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using FluentMigrator.Builders.Execute;
using FluentMigrator.Runner.Generators;
using FluentMigrator.Runner.Generators.Hana;
using FluentMigrator.Runner.Helpers;

namespace FluentMigrator.Runner.Processors.Hana
{
    public class HanaProcessor : GenericProcessorBase
    {
        public override string DatabaseType
        {
            get { return "Hana"; }
        }

        public override bool SupportsTransactions
        {
            get { return true; }
        }

        public HanaProcessor(IDbConnection connection, IMigrationGenerator generator, IAnnouncer announcer, IMigrationProcessorOptions options, IDbFactory factory)
            : base(connection, factory, generator, announcer, options)
        {
        }

        public IQuoter Quoter
        {
            get { return ((HanaGenerator)Generator).Quoter; }
        }

        public override bool SchemaExists(string schemaName)
        {
            return false;
        }

        public override bool TableExists(string schemaName, string tableName)
        {
            if (string.IsNullOrEmpty(tableName))
                throw new ArgumentNullException("tableName");

            return Exists(
                "SELECT 1 FROM TABLES WHERE SCHEMA_NAME = CURRENT_SCHEMA AND upper(TABLE_NAME) = upper('{0}')",
                FormatHelper.FormatSqlEscape(Quoter.UnQuote(tableName).ToUpper()));
        }

        public override bool ColumnExists(string schemaName, string tableName, string columnName)
        {
            if (tableName == null)
                throw new ArgumentNullException("tableName");
            if (columnName == null)
                throw new ArgumentNullException("columnName");

            return Exists("SELECT 1 FROM TABLE_COLUMNS WHERE SCHEMA_NAME = CURRENT_SCHEMA AND upper(TABLE_NAME) = '{0}' AND upper(COLUMN_NAME) = '{1}'",
                FormatHelper.FormatSqlEscape(Quoter.UnQuote(tableName).ToUpper()),
                FormatHelper.FormatSqlEscape(Quoter.UnQuote(columnName).ToUpper()));
        }

        public override bool ConstraintExists(string schemaName, string tableName, string constraintName)
        {
            if (tableName == null)
                throw new ArgumentNullException("tableName");
            if (constraintName == null)
                throw new ArgumentNullException("constraintName");

            if (constraintName.Length == 0)
                return false;

            return Exists("SELECT 1 FROM CONSTRAINTS WHERE SCHEMA_NAME = CURRENT_SCHEMA and upper(CONSTRAINT_NAME) = '{0}'",
                FormatHelper.FormatSqlEscape(Quoter.UnQuote(constraintName).ToUpper()));
        }

        public override bool IndexExists(string schemaName, string tableName, string indexName)
        {
            if (tableName == null)
                throw new ArgumentNullException("tableName");
            if (indexName == null)
                throw new ArgumentNullException("indexName");

            if (indexName.Length == 0)
                return false;

            return Exists("SELECT 1 FROM INDEXES WHERE SCHEMA_NAME = CURRENT_SCHEMA AND upper(INDEX_NAME) = '{0}'",
                    FormatHelper.FormatSqlEscape(Quoter.UnQuote(indexName).ToUpper()));
        }

        public override bool SequenceExists(string schemaName, string sequenceName)
        {
            if (sequenceName == null)
                throw new ArgumentNullException("sequenceName");

            if (string.IsNullOrEmpty(sequenceName))
                return false;

            return Exists("SELECT 1 FROM SEQUENCES WHERE SCHEMA_NAME = CURRENT_SCHEMA and upper(SEQUENCE_NAME) = '{0}'",
                FormatHelper.FormatSqlEscape(Quoter.UnQuote(sequenceName).ToUpper()));
        }

        public override bool DefaultValueExists(string schemaName, string tableName, string columnName, object defaultValue)
        {
            return false;
        }

        public override void Execute(string template, params object[] args)
        {
            Process(string.Format(template, args));
        }

        public override bool Exists(string template, params object[] args)
        {
            if (template == null)
                throw new ArgumentNullException("template");

            EnsureConnectionIsOpen();

            var querySql = String.Format(template, args);

            Announcer.Sql(string.Format("{0};", querySql));

            using (var command = Factory.CreateCommand(String.Format(template, args), Connection))
            using (var reader = command.ExecuteReader())
            {
                return reader.Read();
            }
        }

        public override DataSet ReadTableData(string schemaName, string tableName)
        {
            if (tableName == null)
                throw new ArgumentNullException("tableName");

            if (String.IsNullOrEmpty(schemaName))
                return Read("SELECT * FROM {0}", Quoter.QuoteTableName(tableName));

            return Read("SELECT * FROM {0}.{1}", Quoter.QuoteSchemaName(schemaName), Quoter.QuoteTableName(tableName));
        }

        public override DataSet Read(string template, params object[] args)
        {
            if (template == null)
                throw new ArgumentNullException("template");

            EnsureConnectionIsOpen();

            var result = new DataSet();
            using (var command = Factory.CreateCommand(String.Format(template, args), Connection))
            {
                var adapter = Factory.CreateDataAdapter(command);
                adapter.Fill(result);
                return result;
            }
        }

        public override void Process(PerformDBOperationExpression expression)
        {
            Announcer.Say("Performing DB Operation");

            if (Options.PreviewOnly)
                return;

            EnsureConnectionIsOpen();

            if (expression.Operation != null)
                expression.Operation(Connection, null);
        }

        protected override void Process(string sql)
        {
            Announcer.Sql(sql);

            if (Options.PreviewOnly || string.IsNullOrEmpty(sql))
                return;

            EnsureConnectionIsOpen();

            var batches = Regex.Split(sql, @"^\s*;\s*$", RegexOptions.Multiline)
                .Where(x => !string.IsNullOrEmpty(x))
                .Select(c => c.Trim());

            foreach (var batch in batches)
            {
                var batchCommand = batch.EndsWith(";") 
                    ? batch.Remove(batch.Length - 1) 
                    : batch;

                using (var command = Factory.CreateCommand(batchCommand, Connection))
                    command.ExecuteNonQuery();
            }
        }

    }
}

{'fraction_non_alphanumeric': 0.09074863950580968, 'fraction_numerical': 0.002573907927636417, 'mean_word_length': 2.0850725952813067, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3747724', 'n_tokens_mistral': 3461, 'n_tokens_neox': 3276, 'n_words': 796}