public void GetVendorName_ReturnsUnknown_IfCommandIsOfUnknownType()
        {
            var command = new UnknownDbCommand();

            var datastoreName = SqlWrapperHelper.GetVendorName(command);

            Assert.AreEqual(DatastoreVendor.Other, datastoreName);
        }
        public void GetVendorName_ReturnsSqlServer_IfTypeNameIsNotProvidedAndCommandIsSqlCommand()
        {
            var command = new SqlCommand();

            var datastoreName = SqlWrapperHelper.GetVendorName(command);

            Assert.AreEqual(DatastoreVendor.MSSQL, datastoreName);
        }
        public void GetVendorName_ReturnsOracle_IfTypeNameIsNotProvidedAndCommandIsOracleCommand()
        {
#pragma warning disable 618    // Ignore deprecated warnings
            var command = new OracleCommand();
#pragma warning restore 618

            var datastoreName = SqlWrapperHelper.GetVendorName(command);

            Assert.AreEqual(DatastoreVendor.Oracle, datastoreName);
        }
        public DatastoreVendor GetVendorName_ReturnsCorrectHost_IfOleDbConnectionProviderContainsKnownHost(
            string provider)
        {
            var command = new OleDbCommand
            {
                Connection = new OleDbConnection("Provider=" + provider)
            };

            return(SqlWrapperHelper.GetVendorName(command));
        }
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            //This only happens if we are in an async context.  Regardless if we are adding the after delegate as a contination.
            if (instrumentedMethodCall.IsAsync)
            {
                transaction.AttachToAsync();
            }

            var sqlCommand = (IDbCommand)instrumentedMethodCall.MethodCall.InvocationTarget;

            if (sqlCommand == null)
            {
                return(Delegates.NoOp);
            }

            var sql    = sqlCommand.CommandText ?? string.Empty;
            var vendor = SqlWrapperHelper.GetVendorName(sqlCommand);

            object GetConnectionInfo() => ConnectionInfoParser.FromConnectionString(vendor, sqlCommand.Connection.ConnectionString);

            var connectionInfo = (ConnectionInfo)transaction.GetOrSetValueFromCache(sqlCommand.Connection.ConnectionString, GetConnectionInfo);

            var parsedStatement = transaction.GetParsedDatabaseStatement(vendor, sqlCommand.CommandType, sql);

            var queryParameters = SqlWrapperHelper.GetQueryParameters(sqlCommand, agent);

            var segment = transaction.StartDatastoreSegment(instrumentedMethodCall.MethodCall, parsedStatement, connectionInfo, sql, queryParameters, isLeaf: true);

            switch (vendor)
            {
            case DatastoreVendor.MSSQL:
                agent.EnableExplainPlans(segment, () => SqlServerExplainPlanActions.AllocateResources(sqlCommand), SqlServerExplainPlanActions.GenerateExplainPlan, null);
                break;

            case DatastoreVendor.MySQL:
                if (parsedStatement != null)
                {
                    agent.EnableExplainPlans(segment, () => MySqlExplainPlanActions.AllocateResources(sqlCommand), MySqlExplainPlanActions.GenerateExplainPlan, () => MySqlExplainPlanActions.ShouldGenerateExplainPlan(sql, parsedStatement));
                }
                break;
            }

            return(ExecuteAsAsync
                ? Delegates.GetAsyncDelegateFor <Task>(agent, segment)
                : Delegates.GetDelegateFor(segment));
        }
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            {
                var oleDbCommand = (OleDbCommand)instrumentedMethodCall.MethodCall.InvocationTarget;
                if (oleDbCommand == null)
                {
                    return(Delegates.NoOp);
                }

                var sql    = oleDbCommand.CommandText ?? string.Empty;
                var vendor = SqlWrapperHelper.GetVendorName(oleDbCommand);

                var parsedStatement = transaction.GetParsedDatabaseStatement(vendor, oleDbCommand.CommandType, sql);

                var queryParameters = SqlWrapperHelper.GetQueryParameters(oleDbCommand, agent);

                var segment = transaction.StartDatastoreSegment(instrumentedMethodCall.MethodCall, parsedStatement, null, sql, queryParameters);

                return(Delegates.GetDelegateFor(segment));
            }
        }
 GetVendorName_ReturnsCorrectHost(string typeName)
 {
     return(SqlWrapperHelper.GetVendorName(typeName));
 }