예제 #1
0
        private static string[] ParseRestrictions(string restrictions, Hashtable synonyms)
        {
#if DEBUG
            SqlClientEventSource.Log.AdvancedTraceEvent("<comm.DBConnectionString|INFO|ADV> Restrictions='{0}'", restrictions);
#endif
            List <string> restrictionValues = new List <string>();
            StringBuilder buffer            = new StringBuilder(restrictions.Length);

            int nextStartPosition = 0;
            int endPosition       = restrictions.Length;
            while (nextStartPosition < endPosition)
            {
                int startPosition = nextStartPosition;

                string keyname, keyvalue; // since parsing restrictions ignores values, it doesn't matter if we use ODBC rules or OLEDB rules
                nextStartPosition = DbConnectionOptions.GetKeyValuePair(restrictions, startPosition, buffer, false, out keyname, out keyvalue);
                if (!ADP.IsEmpty(keyname))
                {
#if DEBUG
                    SqlClientEventSource.Log.AdvancedTraceEvent("<comm.DBConnectionString|INFO|ADV> KeyName='{0}'", keyname);
#endif
                    string realkeyname = ((null != synonyms) ? (string)synonyms[keyname] : keyname); // MDAC 85144
                    if (ADP.IsEmpty(realkeyname))
                    {
                        throw ADP.KeywordNotSupported(keyname);
                    }
                    restrictionValues.Add(realkeyname);
                }
            }
            return(RemoveDuplicates(restrictionValues.ToArray()));
        }
예제 #2
0
        private static NameValuePair ParseInternal(Dictionary <string, string> parsetable, string connectionString, bool buildChain, Dictionary <string, string> synonyms, bool firstKey)
        {
            Debug.Assert(null != connectionString, "null connectionstring");
            StringBuilder buffer = new StringBuilder();
            NameValuePair localKeychain = null, keychain = null;

#if DEBUG
            try
            {
#endif
            int nextStartPosition = 0;
            int endPosition       = connectionString.Length;
            while (nextStartPosition < endPosition)
            {
                int startPosition = nextStartPosition;

                string keyname, keyvalue;
                nextStartPosition = GetKeyValuePair(connectionString, startPosition, buffer, firstKey, out keyname, out keyvalue);
                if (string.IsNullOrEmpty(keyname))
                {
                    // if (nextStartPosition != endPosition) { throw; }
                    break;
                }
#if DEBUG
                DebugTraceKeyValuePair(keyname, keyvalue, synonyms);
#endif
                Debug.Assert(IsKeyNameValid(keyname), "ParseFailure, invalid keyname");
                Debug.Assert(IsValueValidInternal(keyvalue), "parse failure, invalid keyvalue");

                string realkeyname = (synonyms is not null) ?
                                     (synonyms.TryGetValue(keyname, out string synonym) ? synonym : null) :
                                     keyname;

                if (!IsKeyNameValid(realkeyname))
                {
                    throw ADP.KeywordNotSupported(keyname);
                }
                if (!firstKey || !parsetable.ContainsKey(realkeyname))
                {
                    parsetable[realkeyname] = keyvalue;     // last key-value pair wins (or first)
                }

                if (null != localKeychain)
                {
                    localKeychain = localKeychain.Next = new NameValuePair(realkeyname, keyvalue, nextStartPosition - startPosition);
                }
                else if (buildChain)
                {     // first time only - don't contain modified chain from UDL file
                    keychain = localKeychain = new NameValuePair(realkeyname, keyvalue, nextStartPosition - startPosition);
                }
            }
#if DEBUG
        }

        catch (ArgumentException e)
        {
            ParseComparison(parsetable, connectionString, synonyms, firstKey, e);
            throw;
        }
        ParseComparison(parsetable, connectionString, synonyms, firstKey, null);
#endif
            return(keychain);
        }
예제 #3
0
        private static Dictionary <string, string> SplitConnectionString(string connectionString, Dictionary <string, string> synonyms, bool firstKey)
        {
            var   parsetable = new Dictionary <string, string>();
            Regex parser     = (firstKey ? s_connectionStringRegexOdbc : s_connectionStringRegex);

            const int KeyIndex = 1, ValueIndex = 2;

            Debug.Assert(KeyIndex == parser.GroupNumberFromName("key"), "wrong key index");
            Debug.Assert(ValueIndex == parser.GroupNumberFromName("value"), "wrong value index");

            if (null != connectionString)
            {
                Match match = parser.Match(connectionString);
                if (!match.Success || (match.Length != connectionString.Length))
                {
                    throw ADP.ConnectionStringSyntax(match.Length);
                }
                int indexValue = 0;
                CaptureCollection keyvalues = match.Groups[ValueIndex].Captures;
                foreach (Capture keypair in match.Groups[KeyIndex].Captures)
                {
                    string keyname  = (firstKey ? keypair.Value : keypair.Value.Replace("==", "=")).ToLower(CultureInfo.InvariantCulture);
                    string keyvalue = keyvalues[indexValue++].Value;
                    if (0 < keyvalue.Length)
                    {
                        if (!firstKey)
                        {
                            switch (keyvalue[0])
                            {
                            case '\"':
                                keyvalue = keyvalue.Substring(1, keyvalue.Length - 2).Replace("\"\"", "\"");
                                break;

                            case '\'':
                                keyvalue = keyvalue.Substring(1, keyvalue.Length - 2).Replace("\'\'", "\'");
                                break;

                            default:
                                break;
                            }
                        }
                    }
                    else
                    {
                        keyvalue = null;
                    }
                    DebugTraceKeyValuePair(keyname, keyvalue, synonyms);
                    string synonym;
                    string realkeyname = null != synonyms ?
                                         (synonyms.TryGetValue(keyname, out synonym) ? synonym : null) : keyname;

                    if (!IsKeyNameValid(realkeyname))
                    {
                        throw ADP.KeywordNotSupported(keyname);
                    }
                    if (!firstKey || !parsetable.ContainsKey(realkeyname))
                    {
                        parsetable[realkeyname] = keyvalue; // last key-value pair wins (or first)
                    }
                }
            }
            return(parsetable);
        }