internal static void RegisterAllConnections()
        {
            ConcurrentDictionary <string, bool> dbproviderRegistereds = new ConcurrentDictionary <string, bool>();
            IEnumerator <KeyValuePair <string, ConnectionSetting> > e = sConnectionSettings.GetEnumerator();

            while (e.MoveNext())
            {
                ConnectionSetting conn    = e.Current.Value;
                string            dialect = conn.Dialect.ToLower();
                if (!dbproviderRegistereds.ContainsKey(dialect))
                {
                    if (sAllowedDatabases.ContainsKey(dialect))
                    {
                        DbUtils.RegisterDbProvider(dialect, sAllowedDatabases[dialect]);
                        dbproviderRegistereds.TryAdd(dialect, true);
                    }
                    else
                    {
                        throw new Exception(string.Concat("不支持的数据库:", conn.Dialect));
                    }
                }

                DbUtils.AddDataSource(conn.DataSource, dialect, conn.ToString());
            }
        }
Exemple #2
0
        private static void ParseConnectionFile(FileInfo connectionFile, string parent)
        {
            string            connectionFilePath = connectionFile.FullName;
            ConnectionSetting conn = ParseConnectionSetting(connectionFilePath);

            conn.DataSource = parent.ToLower();
            ConnectionUtils.AddConnection(parent, conn);
        }
 internal static bool AddConnection(string datasource, ConnectionSetting conn)
 {
     if (sConnectionSettings.TryAdd(datasource.ToLower(), conn))
     {
         sConnectionSettingIndexes.AddOrUpdate(sConnectionSettingIndexes.Count,
                                               datasource.ToLower(), (key, value) =>
         {
             return(datasource);
         });
         return(true);
     }
     return(false);
 }
        internal static ConnectionSetting GetConnectionByModelName(string modelName)
        {
            string fullModelName = ModelUtils.GetFullModelName(modelName).ToLower();

            if (sModelConnections.ContainsKey(fullModelName))
            {
                ConnectionSetting result;
                if (sModelConnections.TryGetValue(fullModelName, out result))
                {
                    return(result);
                }
            }

            string modelPath = fullModelName;
            int    pos       = modelPath.LastIndexOf("/");

            while (pos >= 0)
            {
                modelPath = modelPath.Substring(0, pos);
                if (pos == 0)
                {
                    modelPath = "/";
                }

                if (sConnectionSettings.ContainsKey(modelPath))
                {
                    ConnectionSetting conns = sConnectionSettings[modelPath];
                    sModelConnections.AddOrUpdate(fullModelName, conns, (key, value) =>
                    {
                        return(conns);
                    });
                    return(conns);
                }

                if (pos == 0)
                {
                    break;
                }
            }

            throw new Exception("未定义数据源:" + modelName);
        }
Exemple #5
0
        private static ConnectionSetting ParseConnectionSetting(string connectionFilePath)
        {
            ConnectionSetting result = new ConnectionSetting();

            Xmtool.Xml().Iterate(connectionFilePath, (nodeInfo) => {
                if (!nodeInfo.IsEndNode)
                {
                    if (nodeInfo.Path == "/connection/dialect/@text")
                    {
                        result.Dialect = nodeInfo.Text.Trim().ToLower();
                    }
                    else if (nodeInfo.Path == "/connection/host/@text")
                    {
                        result.Host = nodeInfo.Text.Trim();
                    }
                    else if (nodeInfo.Path == "/connection/port/@text")
                    {
                        int port;
                        if (int.TryParse(nodeInfo.Text, out port))
                        {
                            result.Port = port;
                        }
                        else
                        {
                            throw new Exception("name属性不能为空。 " + connectionFilePath + " - Line " + nodeInfo.Line);
                        }
                    }
                    else if (nodeInfo.Path == "/connection/user/@text")
                    {
                        result.User = nodeInfo.Text.Trim();
                    }
                    else if (nodeInfo.Path == "/connection/password/@text")
                    {
                        result.Password = nodeInfo.Text.Trim();
                    }
                    else if (nodeInfo.Path == "/connection/database/@text")
                    {
                        result.Database = nodeInfo.Text.Trim();
                    }
                    else if (nodeInfo.Path == "/connection/charset/@text")
                    {
                        result.Charset = nodeInfo.Text.Trim();
                    }
                    else if (nodeInfo.Path == "/connection/encrypt/@text")
                    {
                        if (!reBool.IsMatch(nodeInfo.Text.Trim()))
                        {
                            throw new Exception("encrypt节点值必须是布尔型。 " + connectionFilePath + " - Line " + nodeInfo.Line);
                        }
                        result.Encrypt = bool.Parse(nodeInfo.Text.Trim());
                    }
                    else if (nodeInfo.Path == "/connection/pool")
                    {
                        string maxStr = nodeInfo.GetAttribute("max");
                        if (!Xmtool.Regex().IsPositiveInteger(maxStr))
                        {
                            throw new Exception("max属性必须是有效正整数。 " + connectionFilePath + " - Line " + nodeInfo.Line);
                        }
                        result.MaxPoolSize = int.Parse(maxStr);

                        string minStr = nodeInfo.GetAttribute("min");
                        if (!Xmtool.Regex().IsNaturalInteger(minStr))
                        {
                            throw new Exception("min属性必须是有效自然数。 " + connectionFilePath + " - Line " + nodeInfo.Line);
                        }
                        result.MinPoolSize = int.Parse(minStr);
                    }
                    else if (nodeInfo.Path == "/connection/pool/@text")
                    {
                        if (!reBool.IsMatch(nodeInfo.Text.Trim()))
                        {
                            throw new Exception("pool节点值必须是布尔型。 " + connectionFilePath + " - Line " + nodeInfo.Line);
                        }
                        result.Pooling = bool.Parse(nodeInfo.Text.Trim());
                    }
                }
                return(true);
            });
            return(result);
        }