Beispiel #1
0
        public DatabaseDictionary GetDictionary(string projectName, string applicationName)
        {
            DatabaseDictionary databaseDictionary = new DatabaseDictionary();

            try
            {
                InitializeScope(projectName, applicationName);

                string keyFile = string.Format("{0}{1}.{2}.key",
                                               _settings["AppDataPath"], _settings["ProjectName"], _settings["ApplicationName"]);

                if (File.Exists(_settings["DBDictionaryPath"]))
                {
                    databaseDictionary = NHibernateUtility.LoadDatabaseDictionary(
                        _settings["DBDictionaryPath"], keyFile);
                }
                else
                {
                    databaseDictionary = new DatabaseDictionary();
                    NHibernateUtility.SaveDatabaseDictionary(databaseDictionary,
                                                             _settings["DBDictionaryPath"], keyFile);
                }
            }
            catch (Exception ex)
            {
                _logger.Error("Error in GetDbDictionary: " + ex);
                return(null);
            }

            return(databaseDictionary);
        }
Beispiel #2
0
        public Response Generate(string projectName, string applicationName)
        {
            Response response = new Response();
            Status   status   = new Status();

            response.StatusList.Add(status);

            try
            {
                status.Identifier = String.Format("{0}.{1}", projectName, applicationName);
                InitializeScope(projectName, applicationName);

                string keyFile = string.Format("{0}{1}.{2}.key",
                                               _settings["AppDataPath"], _settings["ProjectName"], _settings["ApplicationName"]);

                DatabaseDictionary dbDictionary = NHibernateUtility.LoadDatabaseDictionary(_settings["DBDictionaryPath"], keyFile);

                if (String.IsNullOrEmpty(projectName) || String.IsNullOrEmpty(applicationName))
                {
                    status.Messages.Add("Error project name and application name can not be null");
                }
                else if (ValidateDatabaseDictionary(dbDictionary))
                {
                    EntityGenerator generator = _kernel.Get <EntityGenerator>();

                    string compilerVersion = "v4.0";
                    if (!String.IsNullOrEmpty(_settings["CompilerVersion"]))
                    {
                        compilerVersion = _settings["CompilerVersion"];
                    }

                    response.Append(generator.Generate(compilerVersion, dbDictionary, projectName, applicationName));
                    status.Messages.Add("Database dictionary of [" + projectName + "." + applicationName + "] updated successfully.");
                }
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("Error in UpdateDatabaseDictionary: {0}", ex));

                status.Level = StatusLevel.Error;
                status.Messages.Add(string.Format("Error updating database dictionary: {0}", ex));
            }

            return(response);
        }
Beispiel #3
0
        public DataObject GetSchemaObjectSchema(string projectName, string applicationName, string schemaObjectName)
        {
            List <string>      tableNames   = new List <string>();
            DatabaseDictionary dbDictionary = new DatabaseDictionary();
            string             connStrProp  = "connection.connection_string";

            DataObject dataObject = new DataObject
            {
                tableName         = schemaObjectName,
                dataProperties    = new List <DataProperty>(),
                keyProperties     = new List <KeyProperty>(),
                dataRelationships = new List <DataRelationship>(),
                objectNamespace   = String.Format("org.iringtools.adapter.datalayer.proj_{0}.{1}", projectName, applicationName),
                objectName        = Utility.ToSafeName(schemaObjectName)
            };

            try
            {
                InitializeScope(projectName, applicationName);

                string keyFile = string.Format("{0}{1}.{2}.key",
                                               _settings["AppDataPath"], _settings["ProjectName"], _settings["ApplicationName"]);

                if (File.Exists(_settings["DBDictionaryPath"]))
                {
                    dbDictionary = NHibernateUtility.LoadDatabaseDictionary(_settings["DBDictionaryPath"], keyFile);
                }

                string connString    = dbDictionary.ConnectionString;
                string dbProvider    = dbDictionary.Provider.ToString().ToUpper();
                string schemaName    = dbDictionary.SchemaName;
                string parsedConnStr = ParseConnectionString(connString, dbProvider);

                Dictionary <string, string> properties = new Dictionary <string, string>();

                dbDictionary.ConnectionString = parsedConnStr;
                dbDictionary.dataObjects      = new System.Collections.Generic.List <DataObject>();

                properties.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
                properties.Add("proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");
                properties.Add(connStrProp, parsedConnStr);
                properties.Add("connection.driver_class", GetConnectionDriver(dbProvider));
                properties.Add("dialect", GetDatabaseDialect(dbProvider));

                NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
                config.AddProperties(properties);
                ISessionFactory sessionFactory = null;

                try
                {
                    sessionFactory = config.BuildSessionFactory();
                }
                catch (Exception e)
                {
                    if (dbProvider.ToLower().Contains("mssql"))
                    {
                        config.Properties[connStrProp] = getProcessedConnectionString(parsedConnStr);
                        sessionFactory = config.BuildSessionFactory();
                    }
                    else
                    {
                        throw e;
                    }
                }

                ISession         session      = sessionFactory.OpenSession();
                ISQLQuery        query        = session.CreateSQLQuery(GetTableMetaQuery(dbProvider, schemaName, schemaObjectName));
                IList <object[]> metadataList = query.List <object[]>();
                session.Close();

                foreach (object[] metadata in metadataList)
                {
                    bool   isIdentity = Convert.ToBoolean(metadata[3]);
                    string constraint = Convert.ToString(metadata[5]);
                    if (String.IsNullOrEmpty(constraint)) // process columns
                    {
                        DataProperty column = NewColumnInformation(metadata);
                        dataObject.dataProperties.Add(column);
                    }
                    else
                    {
                        KeyType keyType = KeyType.assigned;

                        if (isIdentity)
                        {
                            keyType = KeyType.identity;
                        }
                        else if (constraint.ToUpper() == "FOREIGN KEY" || constraint.ToUpper() == "R")
                        {
                            keyType = KeyType.foreign;
                        }
                        DataProperty key = new DataProperty();
                        key         = NewColumnInformation(metadata);
                        key.keyType = keyType;
                        dataObject.addKeyProperty(key);
                    }
                }
                return(dataObject);
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("Error in GetSchemaObjectSchema: {0}", ex));
                return(dataObject);
            }
        }
Beispiel #4
0
        public DataObjects GetSchemaObjects(string projectName, string applicationName)
        {
            DataObjects        tableNames     = new DataObjects();
            DatabaseDictionary dbDictionary   = new DatabaseDictionary();
            ISessionFactory    sessionFactory = null;

            try
            {
                _logger.Debug(String.Format("In GetSchemaObjects({0}, {1})", projectName, applicationName));

                InitializeScope(projectName, applicationName);

                string keyFile = string.Format("{0}{1}.{2}.key",
                                               _settings["AppDataPath"], _settings["ProjectName"], _settings["ApplicationName"]);

                if (File.Exists(_settings["DBDictionaryPath"]))
                {
                    dbDictionary = NHibernateUtility.LoadDatabaseDictionary(_settings["DBDictionaryPath"], keyFile);
                }
                else
                {
                    return(tableNames);
                }

                string connString    = dbDictionary.ConnectionString;
                string dbProvider    = dbDictionary.Provider.ToString().ToUpper();
                string schemaName    = dbDictionary.SchemaName;
                string parsedConnStr = ParseConnectionString(connString, dbProvider);
                string connStrProp   = "connection.connection_string";

                _logger.DebugFormat("ConnectString: {0} \r\n Provider: {1} \r\n SchemaName: {2} \r\n Parsed: {3}",
                                    connString,
                                    dbProvider,
                                    schemaName,
                                    parsedConnStr);

                Dictionary <string, string> properties = new Dictionary <string, string>();

                dbDictionary.ConnectionString = parsedConnStr;
                dbDictionary.dataObjects      = new System.Collections.Generic.List <DataObject>();

                properties.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
                properties.Add("proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");
                properties.Add(connStrProp, parsedConnStr);

                string connDriver = GetConnectionDriver(dbProvider);

                _logger.DebugFormat("connection.driver_class: {0}", connDriver);

                properties.Add("connection.driver_class", connDriver);

                string databaseDialect = GetConnectionDriver(dbProvider);

                _logger.DebugFormat("dialect: {0}", databaseDialect);

                properties.Add("dialect", databaseDialect);

                NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();

                _logger.Debug("Adding Properties to Config");

                config.AddProperties(properties);

                _logger.Debug("Building Session Factory");

                try
                {
                    sessionFactory = config.BuildSessionFactory();
                }
                catch (Exception e)
                {
                    if (dbProvider.ToLower().Contains("mssql"))
                    {
                        config.Properties[connStrProp] = getProcessedConnectionString(parsedConnStr);
                        sessionFactory = config.BuildSessionFactory();
                    }
                    else
                    {
                        throw e;
                    }
                }

                _logger.Debug("About to Open Session");

                ISession session = sessionFactory.OpenSession();

                _logger.Debug("Session Open");

                string sql = GetDatabaseMetaquery(dbProvider, parsedConnStr.Split(';')[1].Split('=')[1], schemaName);

                _logger.DebugFormat("SQL: {0}",
                                    sql);

                ISQLQuery query = session.CreateSQLQuery(sql);

                DataObjects metadataList = new DataObjects();
                foreach (string tableName in query.List <string>())
                {
                    metadataList.Add(tableName);
                }
                session.Close();

                tableNames = metadataList;
                return(tableNames);
            }
            catch (Exception ex)
            {
                _logger.ErrorFormat("Error while Getting Schema Objects from database. {0}",
                                    ex.ToString());

                return(tableNames);
            }
        }