Ejemplo n.º 1
0
        /// <summary>
        /// 将ConnectionName和Table.xml进行映射
        /// </summary>
        /// <param name="connectionName"></param>
        /// <param name="tableXmlPath"></param>
        public static void Assemble(string connectionName, string tableXmlPath)
        {
            if (_datasourceInfo == null)
            {
                _datasourceInfo = new Dictionary <string, Dictionary <string, EntityBase> >();
            }
            Dictionary <string, ConnectionInfo> connDic = ConnectionProviderBase.GetAllConnectionInfos();

            if (connDic.ContainsKey(connectionName))
            {
                Dictionary <string, EntityBase> entityDic = ConvertTableXmlToEntityDic(tableXmlPath);
                if (!_datasourceInfo.ContainsKey(connectionName))
                {
                    _datasourceInfo.Add(connectionName, entityDic);
                }
                else
                {
                    foreach (var key in entityDic.Keys)
                    {
                        if (!_datasourceInfo[connectionName].ContainsKey(key))
                        {
                            _datasourceInfo[connectionName].Add(key, entityDic[key]);
                        }
                    }
                }
            }
        }
        public void CreateDatabase(string connectionName = null)
        {
            string databaseName = ConnectionProviderBase.GetDatabaseName(connectionName);
            string sqlTemplate  = "CREATE DATABASE {0}";
            string sqlText      = string.Format(sqlTemplate, databaseName);
            string connStr      = SqlServerConnectionProvider.Instance.GetConnectionStringWithoutIC(connectionName);

            SqlServerBaseOrder.ExecuteNonQuery(connStr, sqlText);
        }
        public bool DatabaseExist(string connectionName = null)
        {
            string databaseName = ConnectionProviderBase.GetDatabaseName(connectionName);
            string sqlTemplate  = "SELECT * FROM master.dbo.sysdatabases WHERE name='{0}'";
            string sqlText      = string.Format(sqlTemplate, databaseName);
            string connStr      = SqlServerConnectionProvider.Instance.GetConnectionStringWithoutIC(connectionName);
            object result       = SqlServerBaseOrder.ExecuteScalar(connStr, sqlText);

            if (result != null && string.Equals(result as string, databaseName))
            {
                return(true);
            }
            return(false);
        }
Ejemplo n.º 4
0
        private static void Compare(Dictionary <string, Dictionary <string, EntityBase> > dbDic, Dictionary <string, Dictionary <string, EntityBase> > newDic)
        {
            Dictionary <string, ConnectionInfo> connDic = ConnectionProviderBase.GetAllConnectionInfos();

            foreach (var conn in newDic.Keys)
            {
                IDBOperatorBase manager = InjectContainer.Resolve <IDBOperatorBase>(connDic[conn].DBType.ToString());



                foreach (var entityName in newDic[conn].Keys)
                {
                    if (!dbDic[conn].ContainsKey(entityName))
                    {
                        //新增表
                        manager.CreateTable(newDic[conn][entityName], conn);
                        newDic[conn][entityName].Id = Guid.NewGuid().ToString();
                        manager.InsertIntoEntityBase(newDic[conn][entityName], conn);
                        foreach (var attr in newDic[conn][entityName].Attributes.Keys)
                        {
                            newDic[conn][entityName].Attributes[attr].Id       = Guid.NewGuid().ToString();
                            newDic[conn][entityName].Attributes[attr].EntityId = newDic[conn][entityName].Id;
                            manager.InsertIntoEntityAttributeBase(newDic[conn][entityName].Attributes[attr], conn);
                        }
                    }
                    else
                    {
                        newDic[conn][entityName].Id = dbDic[conn][entityName].Id;
                        foreach (var attrName in newDic[conn][entityName].Attributes.Keys)
                        {
                            newDic[conn][entityName].Attributes[attrName].EntityId = newDic[conn][entityName].Id;
                            if (!dbDic[conn][entityName].Attributes.ContainsKey(attrName))
                            {
                                //新增字段
                                manager.CreateField(entityName, newDic[conn][entityName].Attributes[attrName], conn);
                                newDic[conn][entityName].Attributes[attrName].Id = Guid.NewGuid().ToString();
                                manager.InsertIntoEntityAttributeBase(newDic[conn][entityName].Attributes[attrName], conn);
                            }
                            else
                            {
                                newDic[conn][entityName].Attributes[attrName].Id = dbDic[conn][entityName].Attributes[attrName].Id;
                            }
                        }
                    }
                }
            }
            _datasourceInfo = newDic;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 初始化,其中包括同步Table.xml信息到数据库,并且加载新的Entity信息到内存中
        /// </summary>
        public static void Init()
        {
            var dbSource = new Dictionary <string, Dictionary <string, EntityBase> >();

            InjectContainer.LoadConnection();
            Dictionary <string, ConnectionInfo> connDic = ConnectionProviderBase.GetAllConnectionInfos();

            if (connDic.Count > 0)
            {
                List <string> removeConns = new List <string>();
                foreach (var key in connDic.Keys)
                {
                    if (_datasourceInfo.ContainsKey(key))
                    {
                        var             dbType  = connDic[key].DBType;
                        IDBOperatorBase manager = InjectContainer.Resolve <IDBOperatorBase>(dbType.ToString());
                        if (!manager.DatabaseExist(key))
                        {
                            manager.CreateDatabase(key);
                        }
                        if (!manager.TableExist("EntityBase", key))
                        {
                            manager.CreateDefaultTable(key);
                        }
                        var entityDic = manager.QueryEntityDic(key);
                        dbSource.Add(key, entityDic);
                    }
                    else
                    {
                        removeConns.Add(key);
                    }
                }
                ConnectionProviderBase.DeleteConnections(removeConns);
            }
            Compare(dbSource, _datasourceInfo);
        }
        public static T ResolveByName <T>(string connectionName)
        {
            Dictionary <string, ConnectionInfo> connDic = ConnectionProviderBase.GetAllConnectionInfos();

            return(_container.Resolve <T>(connDic[connectionName].DBType.ToString()));
        }