static public DbProviderFactory GetFactory(string providerInvariantName)
        {
            ADP.CheckArgumentLength(providerInvariantName, "providerInvariantName");

            // NOTES: Include the Framework Providers and any other Providers listed in the config file.
            DataTable providerTable = GetProviderTable();

            if (null != providerTable)
            {
                // we don't need to copy the DataTable because its used in a controlled fashion
                // also we don't need to create a blank datatable because we know the information won't exist

#if DEBUG
                DataColumn[] pkey = providerTable.PrimaryKey;
                Debug.Assert(null != providerTable.Columns[InvariantName], "missing primary key column");
                Debug.Assert((null != pkey) && (1 == pkey.Length) && (InvariantName == pkey[0].ColumnName), "bad primary key");
#endif
                DataRow providerRow = providerTable.Rows.Find(providerInvariantName);
                if (null != providerRow)
                {
                    return(DbProviderFactories.GetFactory(providerRow));
                }
            }
            throw ADP.ConfigProviderNotFound();
        }
        private static DbProviderFactory?GetFactory(string providerInvariantName, bool throwOnError)
        {
            if (throwOnError)
            {
                ADP.CheckArgumentLength(providerInvariantName, nameof(providerInvariantName));
            }
            else
            {
                if (string.IsNullOrWhiteSpace(providerInvariantName))
                {
                    return(null);
                }
            }
            bool wasRegistered = _registeredFactories.TryGetValue(providerInvariantName, out ProviderRegistration registration);

            if (!wasRegistered)
            {
                return(throwOnError ? throw ADP.Argument(SR.Format(SR.ADP_DbProviderFactories_InvariantNameNotFound, providerInvariantName)) : (DbProviderFactory?)null);
            }
            DbProviderFactory?toReturn = registration.FactoryInstance;

            if (toReturn == null)
            {
                // Deferred registration, do checks now on the type specified and register instance in storage.
                // Even in the case of throwOnError being false, this will throw when an exception occurs checking the registered type as the user has to be notified the
                // registration is invalid, even though the registration is there.
                toReturn = GetFactoryInstance(GetProviderTypeFromTypeName(registration.FactoryTypeAssemblyQualifiedName));
                RegisterFactory(providerInvariantName, toReturn);
            }
            return(toReturn);
        }
Example #3
0
        public static void RegisterFactory(string providerInvariantName, DbProviderFactory factory)
        {
            ADP.CheckArgumentLength(providerInvariantName, nameof(providerInvariantName));
            ADP.CheckArgumentNull(factory, nameof(factory));

            _registeredFactories[providerInvariantName] = new ProviderRegistration(factory.GetType().AssemblyQualifiedName, factory);
        }
        public static void RegisterFactory(string providerInvariantName, string factoryTypeAssemblyQualifiedName)
        {
            ADP.CheckArgumentLength(providerInvariantName, nameof(providerInvariantName));
            ADP.CheckArgumentLength(factoryTypeAssemblyQualifiedName, nameof(factoryTypeAssemblyQualifiedName));

            // this method performs a deferred registration: the type name specified is checked when the factory is requested for the first time.
            _registeredFactories[providerInvariantName] = new ProviderRegistration(factoryTypeAssemblyQualifiedName, null);
        }
        internal static void AppendKeyValuePairBuilder(StringBuilder builder, string keyName, string keyValue, bool useOdbcRules)
        {
            ADP.CheckArgumentNull(builder, "builder");
            ADP.CheckArgumentLength(keyName, "keyName");

            if ((null == keyName) || !s_connectionStringValidKeyRegex.IsMatch(keyName))
            {
                throw ADP.InvalidKeyname(keyName);
            }
            if ((null != keyValue) && !IsValueValidInternal(keyValue))
            {
                throw ADP.InvalidValue(keyName);
            }

            if ((0 < builder.Length) && (';' != builder[builder.Length - 1]))
            {
                builder.Append(";");
            }

            if (useOdbcRules)
            {
                builder.Append(keyName);
            }
            else
            {
                builder.Append(keyName.Replace("=", "=="));
            }
            builder.Append("=");

            if (null != keyValue)
            { // else <keyword>=;
                if (s_connectionStringQuoteValueRegex.IsMatch(keyValue))
                {
                    // <value> -> <value>
                    builder.Append(keyValue);
                }
                else if ((-1 != keyValue.IndexOf('\"')) && (-1 == keyValue.IndexOf('\'')))
                {
                    // <val"ue> -> <'val"ue'>
                    builder.Append('\'');
                    builder.Append(keyValue);
                    builder.Append('\'');
                }
                else
                {
                    // <val'ue> -> <"val'ue">
                    // <=value> -> <"=value">
                    // <;value> -> <";value">
                    // < value> -> <" value">
                    // <va lue> -> <"va lue">
                    // <va'"lue> -> <"va'""lue">
                    builder.Append('\"');
                    builder.Append(keyValue.Replace("\"", "\"\""));
                    builder.Append('\"');
                }
            }
        }
Example #6
0
 internal static void AppendKeyValuePairBuilder(StringBuilder builder, string keyName, string keyValue, bool useOdbcRules)
 {
     ADP.CheckArgumentNull(builder, "builder");
     ADP.CheckArgumentLength(keyName, "keyName");
     if ((keyName == null) || !ConnectionStringValidKeyRegex.IsMatch(keyName))
     {
         throw ADP.InvalidKeyname(keyName);
     }
     if ((keyValue != null) && !IsValueValidInternal(keyValue))
     {
         throw ADP.InvalidValue(keyName);
     }
     if ((0 < builder.Length) && (';' != builder[builder.Length - 1]))
     {
         builder.Append(";");
     }
     if (useOdbcRules)
     {
         builder.Append(keyName);
     }
     else
     {
         builder.Append(keyName.Replace("=", "=="));
     }
     builder.Append("=");
     if (keyValue != null)
     {
         if (useOdbcRules)
         {
             if (((0 < keyValue.Length) && ((('{' == keyValue[0]) || (0 <= keyValue.IndexOf(';'))) || (string.Compare("Driver", keyName, StringComparison.OrdinalIgnoreCase) == 0))) && !ConnectionStringQuoteOdbcValueRegex.IsMatch(keyValue))
             {
                 builder.Append('{').Append(keyValue.Replace("}", "}}")).Append('}');
             }
             else
             {
                 builder.Append(keyValue);
             }
         }
         else if (ConnectionStringQuoteValueRegex.IsMatch(keyValue))
         {
             builder.Append(keyValue);
         }
         else if ((-1 != keyValue.IndexOf('"')) && (-1 == keyValue.IndexOf('\'')))
         {
             builder.Append('\'');
             builder.Append(keyValue);
             builder.Append('\'');
         }
         else
         {
             builder.Append('"');
             builder.Append(keyValue.Replace("\"", "\"\""));
             builder.Append('"');
         }
     }
 }
        public static DbProviderFactory GetFactory(string providerInvariantName)
        {
            ADP.CheckArgumentLength(providerInvariantName, "providerInvariantName");
            DataTable providerTable = GetProviderTable();

            if (providerTable != null)
            {
                DataRow providerRow = providerTable.Rows.Find(providerInvariantName);
                if (providerRow != null)
                {
                    return(GetFactory(providerRow));
                }
            }
            throw ADP.ConfigProviderNotFound();
        }
        internal static void AppendKeyValuePairBuilder(StringBuilder builder, string keyName, string keyValue, bool useOdbcRules)
        {
            ADP.CheckArgumentNull(builder, nameof(builder));
            ADP.CheckArgumentLength(keyName, nameof(keyName));

            if ((null == keyName) || !s_connectionStringValidKeyRegex.IsMatch(keyName))
            {
                throw ADP.InvalidKeyname(keyName);
            }
            if ((null != keyValue) && !IsValueValidInternal(keyValue))
            {
                throw ADP.InvalidValue(keyName);
            }

            if ((0 < builder.Length) && (';' != builder[builder.Length - 1]))
            {
                builder.Append(';');
            }

            if (useOdbcRules)
            {
                builder.Append(keyName);
            }
            else
            {
                builder.Append(keyName.Replace("=", "=="));
            }
            builder.Append('=');

            if (null != keyValue)
            {
                // else <keyword>=;
                if (useOdbcRules)
                {
                    if ((0 < keyValue.Length) &&
                        // string.Contains(char) is .NetCore2.1+ specific
                        (('{' == keyValue[0]) || (0 <= keyValue.IndexOf(';')) || string.Equals(DbConnectionStringKeywords.Driver, keyName, StringComparison.OrdinalIgnoreCase)) &&
                        !s_connectionStringQuoteOdbcValueRegex.IsMatch(keyValue))
                    {
                        // always quote Driver value (required for ODBC Version 2.65 and earlier)
                        // always quote values that contain a ';'
                        builder.Append('{').Append(keyValue.Replace("}", "}}")).Append('}');
                    }
                    else
                    {
                        builder.Append(keyValue);
                    }
                }
                else if (s_connectionStringQuoteValueRegex.IsMatch(keyValue))
                {
                    // <value> -> <value>
                    builder.Append(keyValue);
                }
                // string.Contains(char) is .NetCore2.1+ specific
                else if ((-1 != keyValue.IndexOf('\"')) && (-1 == keyValue.IndexOf('\'')))
                {
                    // <val"ue> -> <'val"ue'>
                    builder.Append('\'');
                    builder.Append(keyValue);
                    builder.Append('\'');
                }
                else
                {
                    // <val'ue> -> <"val'ue">
                    // <=value> -> <"=value">
                    // <;value> -> <";value">
                    // < value> -> <" value">
                    // <va lue> -> <"va lue">
                    // <va'"lue> -> <"va'""lue">
                    builder.Append('\"');
                    builder.Append(keyValue.Replace("\"", "\"\""));
                    builder.Append('\"');
                }
            }
        }
Example #9
0
        protected virtual int Fill(DataTable[] dataTables, IDataReader dataReader, int startRecord, int maxRecords)
        {
            long logScopeId = DataCommonEventSource.Log.EnterScope("<comm.DataAdapter.Fill|API> {0}, dataTables[], dataReader, startRecord, maxRecords", ObjectID);

            try
            {
                ADP.CheckArgumentLength(dataTables, nameof(dataTables));
                if ((null == dataTables) || (0 == dataTables.Length) || (null == dataTables[0]))
                {
                    throw ADP.FillRequires("dataTable");
                }
                if (null == dataReader)
                {
                    throw ADP.FillRequires(nameof(dataReader));
                }
                if ((1 < dataTables.Length) && ((0 != startRecord) || (0 != maxRecords)))
                {
                    throw ADP.NotSupported(); // FillChildren is not supported with FillPage
                }

                int     result            = 0;
                bool    enforceContraints = false;
                DataSet commonDataSet     = dataTables[0].DataSet;
                try
                {
                    if (null != commonDataSet)
                    {
                        enforceContraints = commonDataSet.EnforceConstraints;
                        commonDataSet.EnforceConstraints = false;
                    }
                    for (int i = 0; i < dataTables.Length; ++i)
                    {
                        Debug.Assert(null != dataTables[i], "null DataTable Fill");

                        if (dataReader.IsClosed)
                        {
#if DEBUG
                            Debug.Assert(!_debugHookNonEmptySelectCommand, "Debug hook asserts data reader should be open");
#endif
                            break;
                        }
                        DataReaderContainer readerHandler = DataReaderContainer.Create(dataReader, ReturnProviderSpecificTypes);
                        AssertReaderHandleFieldCount(readerHandler);
                        if (readerHandler.FieldCount <= 0)
                        {
                            if (i == 0)
                            {
                                bool lastFillNextResult;
                                do
                                {
                                    lastFillNextResult = FillNextResult(readerHandler);
                                }while (lastFillNextResult && readerHandler.FieldCount <= 0);
                                if (!lastFillNextResult)
                                {
                                    break;
                                }
                            }
                            else
                            {
                                continue;
                            }
                        }
                        if ((0 < i) && !FillNextResult(readerHandler))
                        {
                            break;
                        }
                        // user must Close/Dispose of the dataReader
                        // user will have to call NextResult to access remaining results
                        int count = FillFromReader(null, dataTables[i], null, readerHandler, startRecord, maxRecords, null, null);
                        if (0 == i)
                        {
                            result = count;
                        }
                    }
                }
                catch (ConstraintException)
                {
                    enforceContraints = false;
                    throw;
                }
                finally
                {
                    if (enforceContraints)
                    {
                        commonDataSet.EnforceConstraints = true;
                    }
                }
                return(result);
            }
            finally
            {
                DataCommonEventSource.Log.ExitScope(logScopeId);
            }
        }
Example #10
0
        protected virtual int Fill(DataTable[] dataTables, IDataReader dataReader, int startRecord, int maxRecords)
        {
            int    num3;
            IntPtr ptr;

            Bid.ScopeEnter(out ptr, "<comm.DataAdapter.Fill|API> %d#, dataTables[], dataReader, startRecord, maxRecords\n", this.ObjectID);
            try
            {
                ADP.CheckArgumentLength(dataTables, "tables");
                if (((dataTables == null) || (dataTables.Length == 0)) || (dataTables[0] == null))
                {
                    throw ADP.FillRequires("dataTable");
                }
                if (dataReader == null)
                {
                    throw ADP.FillRequires("dataReader");
                }
                if ((1 < dataTables.Length) && ((startRecord != 0) || (maxRecords != 0)))
                {
                    throw ADP.NotSupported();
                }
                int     num2 = 0;
                bool    enforceConstraints = false;
                DataSet dataSet            = dataTables[0].DataSet;
                try
                {
                    if (dataSet != null)
                    {
                        enforceConstraints         = dataSet.EnforceConstraints;
                        dataSet.EnforceConstraints = false;
                    }
                    for (int i = 0; i < dataTables.Length; i++)
                    {
                        if (dataReader.IsClosed)
                        {
                            goto Label_00DE;
                        }
                        DataReaderContainer container = DataReaderContainer.Create(dataReader, this.ReturnProviderSpecificTypes);
                        if (container.FieldCount > 0)
                        {
                            if ((0 < i) && !this.FillNextResult(container))
                            {
                                goto Label_00DE;
                            }
                            int num4 = this.FillFromReader(null, dataTables[i], null, container, startRecord, maxRecords, null, null);
                            if (i == 0)
                            {
                                num2 = num4;
                            }
                        }
                    }
                }
                catch (ConstraintException)
                {
                    enforceConstraints = false;
                    throw;
                }
                finally
                {
                    if (enforceConstraints)
                    {
                        dataSet.EnforceConstraints = true;
                    }
                }
Label_00DE:
                num3 = num2;
            }
            finally
            {
                Bid.ScopeLeave(ref ptr);
            }
            return(num3);
        }