Пример #1
0
        protected override IDbConnection CreateConnection(DBObject dbObject, out string dsn)
        {
            var dataSourceName = dbObject.IP ?? dbObject.Node;
            var connStrBuilder = new OracleConnectionStringBuilder();

            if (dbObject.ConnectedMode == DBObject.Mode.Direct)
            {
                connStrBuilder.DataSource = string.Format(@"(DESCRIPTION = (CONNECT_DATA = (SERVER=DEDICATED)(SID = {0}))(ADDRESS_LIST = (ADDRESS =  (COMMUNITY = tcp.world)(PROTOCOL = TCP)(Host = {1})(Port = 1521))(ADDRESS = (COMMUNITY = tcp.world)(PROTOCOL = TCP)(Host = {1})(Port = 1526))))", dbObject.Node, dataSourceName);
            }
            else
            {
                connStrBuilder.DataSource = dbObject.Node;
            }
            connStrBuilder.UserID = dbObject.ID;

            if (LazyPassword.GetLazyPasswordEnabled(dbObject))
            {
                dbObject.Password = LazyPassword.GetPassword(new OracleConnection(), dbObject.ID, dbObject.LazyPasswordSecretKey, dbObject.LazyPasswordSecretKeyNode);
            }


            connStrBuilder.Password = dbObject.Password;

            var extra = dbObject.ConnectionString as OracleConnectionString ?? new OracleConnectionString();
            var ps    = typeof(OracleConnectionString).GetProperties().Select(r =>
            {
                var attrs = r.GetCustomAttributes(false).OfType <Attribute>();
                return(new
                {
                    Value = r.GetValue(extra, null),
                    DisplayName = InternalHelper.GetValue <DisplayNameAttribute, string>(attr => attr.DisplayName, attrs),
                    DefaultValue = InternalHelper.GetValue <DefaultValueAttribute, object>(attr => attr.Value, attrs),
                });
            });

            foreach (var p in ps)
            {
                var value = p.Value ?? p.DefaultValue;
                if (value != null)
                {
                    typeof(OracleConnectionStringBuilder).GetProperty(p.DisplayName).SetValue(connStrBuilder, value, null);
                }
            }

            dsn = dbObject.Node;
            return(new OracleConnection(connStrBuilder.ConnectionString));
        }
Пример #2
0
        protected override IDbConnection CreateConnection(DBObject dbObject, out string dsn)
        {
            var connStrBuilder = new OracleConnectionStringBuilder();

            if (dbObject.ConnectedMode == DBObject.Mode.Direct)
            {
                var dataSourceName = dbObject.IP ?? dbObject.Node;
                var port           = dbObject.Port ?? "1521";
                connStrBuilder.DataSource = string.Format(ConnectionStringPattern, dataSourceName, dbObject.Node, port);
            }
            else if (dbObject.ConnectedMode == DBObject.Mode.DSN)
            {
                connStrBuilder.DataSource = dbObject.Node;
            }
            else if (dbObject.ConnectedMode == DBObject.Mode.TNSNAME || LazyPassword.GetLazyPasswordEnabled(dbObject))
            {
                if (!_flgTnsnames)
                {
                    var path = AncestorGlobalOptions.GetString("option.oracle.tnsnames");
                    if (string.IsNullOrEmpty(path))
                    {
                        // try to find tnsnames.ora file for resolve name alias
                        var oracleHome = FindOracleHome();
                        if (oracleHome != null)
                        {
                            path = Path.Combine(oracleHome, "NETWORK", "ADMIN", "TNSNAMES.ora");
                        }
                    }

                    if (File.Exists(path))
                    {
                        _LastTnsLocation = path;
                    }
                    _flgTnsnames = true;
                }

                if (_LastTnsLocation == null)
                {
                    throw new NullReferenceException("no tnsnames.ora found");
                }


                lock (TnsNamesMap)
                {
                    // try resolve tnsnames.ora
                    try
                    {
                        TnsNamesMap.Clear();
                        // parse tnsnames.ora to TnsNamesMap
                        var stack = new Stack <StringBuilder>();
                        var sb    = new StringBuilder();
                        int c;
                        using (var fs = File.OpenRead(_LastTnsLocation))
                            using (var sr = new StreamReader(fs))
                            {
                                while ((c = sr.Read()) != -1)
                                {
                                    switch ((char)c)
                                    {
                                    case '#':
                                        sr.ReadLine();
                                        continue;

                                    case '(':
                                        stack.Push(sb);
                                        sb = new StringBuilder();
                                        break;

                                    case ')':
                                        var t = stack.Pop();
                                        if (stack.Count == 0)
                                        {
                                            var s       = t.ToString();
                                            var splited = s.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                                            if (splited.Length > 0)
                                            {
                                                var name = splited[0].Trim();
                                                var dns  = sb.Insert(0, "(").Append(")").ToString();
                                                dns = Regex.Replace(dns, @"[\r|\n|\s]", string.Empty);
                                                TnsNamesMap.AddOrUpdate(name, dns, (n, v) => dns);
                                            }
                                            sb.Clear();
                                        }
                                        else
                                        {
                                            t.Append(sb.Insert(0, "(").Append(")").ToString());
                                            sb = t;
                                        }
                                        break;

                                    case '\r':
                                    case '\n':
                                        continue;

                                    default:
                                        sb.Append((char)c);
                                        break;
                                    }
                                }
                            }
                    }
                    catch { }
                }
                connStrBuilder.DataSource = FindDataSource(dbObject.Node);
            }

            connStrBuilder.UserID = dbObject.ID;
            if (LazyPassword.GetLazyPasswordEnabled(dbObject))
            {
                dbObject.Password = LazyPassword.GetPassword(new OracleConnection(), dbObject.ID, dbObject.LazyPasswordSecretKey, dbObject.LazyPasswordSecretKeyNode, GetLazyPasswordConnectionString);
            }
            connStrBuilder.Password = dbObject.Password;

            var extra = dbObject.ConnectionString as OracleConnectionString ?? new OracleConnectionString();
            var ps    = typeof(OracleConnectionString).GetProperties().Select(r =>
            {
                var attrs = r.GetCustomAttributes(false).OfType <Attribute>();
                return(new
                {
                    Value = r.GetValue(extra, null),
                    DisplayName = InternalHelper.GetValue <DisplayNameAttribute, string>(attr => attr.DisplayName, attrs),
                    DefaultValue = InternalHelper.GetValue <DefaultValueAttribute, object>(attr => attr.Value, attrs),
                });
            });

            foreach (var p in ps)
            {
                var value = p.Value ?? p.DefaultValue;
                if (value != null)
                {
                    typeof(OracleConnectionStringBuilder).GetProperty(p.DisplayName).SetValue(connStrBuilder, value, null);
                }
            }
            dsn = dbObject.Node;
            return(new OracleConnection(connStrBuilder.ConnectionString));
        }