using System;
using System.IO;
using System.Text;
using System.Globalization;
using System.Collections.Generic;

using System.Data;
using System.Data.SqlClient;

using CommonHelpers;

namespace SqlToCsvExport {

//**************************************************************************************************
partial class Scripter {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// <exception cref="ArgumentNullException" />
/// <exception cref="OperationFailedException" />
public string LoadNParse(string filePath)
{
   HArgChecking.VerifyNotNull( filePath );
   InsertionDictionary.Clear();
   var sb = new StringBuilder();
   int len = 0;
   try
   {
      StreamReader reader = null;
      Exception error = null;
      try
      {
         reader = new StreamReader( filePath, Encoding.Default, true );
         string str; int lineNo = 0;
         while ( (str = reader.ReadLine()) != null )
         {
            sb.AppendLine( ProcessScriptLine( str, ++lineNo ) );
            if ( !string.IsNullOrWhiteSpace( str ) &&
                 !IsGoSeparatorLine( str ) )
               len = sb.Length;
         }
      }
      catch (Exception e)
      {
         error = e; throw;
      }
      finally
      {
         if ( reader != null )
            try
            {
               reader.Close(); reader.Dispose();
            }
            catch (Exception e)
            {
               if ( error == null )
                  throw OpFailedExceptionCreator.Create( error = e );
            }
      }
   }
   catch (Exception e)
   {
      throw OpFailedExceptionCreator.Create(HLocal.TEXT(
         "Can not load script file: \"{0}\".\n"+
         "{1}",
         "Не удаётся загрузить скрипт: \"{0}\".\n"+
         "{1}" ),
         filePath, e.Message );
   }
   return sb.ToString( 0, len );
}

//==================================================================================================

/// <exception cref="ArgumentNullException" />
static bool IsGoSeparatorLine(string str)
{
   HArgChecking.VerifyNotNull( str );
   str = str.TrimStart();
   if ( !str.StartsWith( "GO", true, CultureInfo.InvariantCulture ) )
      return false;
   string rest = str.Substring( 2 ).TrimStart();
   return rest == "" || rest.StartsWith( "--", StringComparison.Ordinal );
}

/// <exception cref="ArgumentNullException" />
public static bool IsNormalAsciiIdent(string ident)
{
   HArgChecking.VerifyNotNull( ident );
   if ( ident == "" )
      return false;
   for (int i=0; i<ident.Length; i++)
   {
      char ch = ident[ i ];
      bool isDigit = char.IsDigit( ch );
      if ( i==0 && isDigit || !(isDigit || ch=='_' || HChar.IsEnglishLetter( ch )) )
         return false;
   }
   return true;
}

//==================================================================================================

readonly Dictionary<string,string>
   InsertionDictionary = new Dictionary<string,string>( StringComparer.OrdinalIgnoreCase );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // Scripter
//**************************************************************************************************

} // SqlToCsvExport
