SchemaValidator.cs :  » Persistence-Frameworks » NHibernate » NHibernate » Tool » hbm2ddl » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Persistence Frameworks » NHibernate 
NHibernate » NHibernate » Tool » hbm2ddl » SchemaValidator.cs
using System;
using System.Collections.Generic;
using System.Data.Common;
using log4net;
using NHibernate.Cfg;
using NHibernate.Util;

namespace NHibernate.Tool.hbm2ddl{
  public class SchemaValidator
  {
    private static readonly ILog log = LogManager.GetLogger(typeof (SchemaValidator));
    private readonly Configuration configuration;
    private readonly IConnectionHelper connectionHelper;
    private readonly Dialect.Dialect dialect;

    public SchemaValidator(Configuration cfg) : this(cfg, cfg.Properties) {}

    public SchemaValidator(Configuration cfg, IDictionary<string, string> connectionProperties)
    {
      configuration = cfg;
      dialect = Dialect.Dialect.GetDialect(connectionProperties);
      IDictionary<string, string> props = new Dictionary<string, string>(dialect.DefaultProperties);
      foreach (var prop in connectionProperties)
      {
        props[prop.Key] = prop.Value;
      }
      connectionHelper = new ManagedProviderConnectionHelper(props);
    }

    public SchemaValidator(Configuration cfg, Settings settings)
    {
      configuration = cfg;
      dialect = settings.Dialect;
      connectionHelper = new SuppliedConnectionProviderConnectionHelper(settings.ConnectionProvider);
    }

    public static void Main(string[] args)
    {
      try
      {
        var cfg = new Configuration();

        //string propFile = null;

        for (int i = 0; i < args.Length; i++)
        {
          if (args[i].StartsWith("--"))
          {
            //if (args[i].StartsWith("--properties="))
            //{
            //  propFile = args[i].Substring(13);
            //}
            //else 
            if (args[i].StartsWith("--config="))
            {
              cfg.Configure(args[i].Substring(9));
            }
            else if (args[i].StartsWith("--naming="))
            {
              cfg.SetNamingStrategy(
                (INamingStrategy)
                Cfg.Environment.BytecodeProvider.ObjectsFactory.CreateInstance(ReflectHelper.ClassForName(args[i].Substring(9))));
            }
          }
          else
          {
            cfg.AddFile(args[i]);
          }
        }
        /* NH: No props file for .NET
        if ( propFile != null ) {
          Properties props = new Properties();
          props.putAll( cfg.getProperties() );
          props.load( new FileInputStream( propFile ) );
          cfg.setProperties( props );
        }
        */
        new SchemaValidator(cfg).Validate();
      }
      catch (Exception e)
      {
        log.Error("Error running schema update", e);
        Console.WriteLine(e);
      }
    }

    /**
   * Perform the validations.
   */

    public void Validate()
    {
      log.Info("Running schema validator");
      try
      {
        DatabaseMetadata meta;
        try
        {
          log.Info("fetching database metadata");
          connectionHelper.Prepare();
          DbConnection connection = connectionHelper.Connection;
          meta = new DatabaseMetadata(connection, dialect, false);
        }
        catch (Exception sqle)
        {
          log.Error("could not get database metadata", sqle);
          throw;
        }
        configuration.ValidateSchema(dialect, meta);
      }
      catch (Exception e)
      {
        log.Error("could not complete schema validation", e);
        throw;
      }
      finally
      {
        try
        {
          connectionHelper.Release();
        }
        catch (Exception e)
        {
          log.Error("Error closing connection", e);
        }
      }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.