<commit_before>﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Signum.Utilities;
using Signum.Engine.Maps;
using System.Linq;
using System.IO;
using System.Data.SqlClient;
using Signum.Utilities.ExpressionTrees;
using System.Text.RegularExpressions;
using Signum.Engine.DynamicQuery;
using System.Data.Common;
using System.Linq.Expressions;
using Signum.Entities;
using Signum.Utilities.Reflection;
using System.Reflection;
using Microsoft.SqlServer.Server;

namespace Signum.Engine
{

    public abstract class Connector
    {
        static readonly Variable<Connector> currentConnection = Statics.ThreadVariable<Connector>("connection");

        public static IDisposable Override(Connector connection)
        {
            Connector oldConnection = currentConnection.Value;

            currentConnection.Value = connection;

            return new Disposable(() => currentConnection.Value = oldConnection);
        }

        public static Connector Current
        {
            get { return currentConnection.Value ?? Default; }
        }

        static Connector @default;
        public static Connector Default
        {
            get { return @default; }
            set { @default = value; }
        }

        static readonly Variable<int?> scopeTimeout = Statics.ThreadVariable<int?>("scopeTimeout");
        public static int? ScopeTimeout { get { return scopeTimeout.Value; } }
        public static IDisposable CommandTimeoutScope(int? timeoutMilliseconds)
        {
            var old = scopeTimeout.Value;
            scopeTimeout.Value = timeoutMilliseconds;
            return new Disposable(() => scopeTimeout.Value = old);
        }

        public Connector(Schema schema, DynamicQueryManager dqm)
        {
            this.Schema = schema;
            this.DynamicQueryManager = dqm;
            this.IsolationLevel = IsolationLevel.Unspecified;
        }

        public Schema Schema { get; private set; }
        public DynamicQueryManager DynamicQueryManager { get; private set; }

        static readonly Variable<TextWriter> logger = Statics.ThreadVariable<TextWriter>("connectionlogger");
        public static TextWriter CurrentLogger
        {
            get { return logger.Value; }
            set { logger.Value = value; }
        }

        protected static void Log(SqlPreCommandSimple pcs)
        {
            var log = logger.Value;
            if (log != null)
            {
                log.WriteLine(pcs.Sql);
                if (pcs.Parameters != null)
                    log.WriteLine(pcs.Parameters<commit_msg>fix timeoutMilliseconds (is actually timeoutSeconds)<commit_after>﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Signum.Utilities;
using Signum.Engine.Maps;
using System.Linq;
using System.IO;
using System.Data.SqlClient;
using Signum.Utilities.ExpressionTrees;
using System.Text.RegularExpressions;
using Signum.Engine.DynamicQuery;
using System.Data.Common;
using System.Linq.Expressions;
using Signum.Entities;
using Signum.Utilities.Reflection;
using System.Reflection;
using Microsoft.SqlServer.Server;

namespace Signum.Engine
{

    public abstract class Connector
    {
        static readonly Variable<Connector> currentConnection = Statics.ThreadVariable<Connector>("connection");

        public static IDisposable Override(Connector connection)
        {
            Connector oldConnection = currentConnection.Value;

            currentConnection.Value = connection;

            return new Disposable(() => currentConnection.Value = oldConnection);
        }

        public static Connector Current
        {
            get { return currentConnection.Value ?? Default; }
        }

        static Connector @default;
        public static Connector Default
        {
            get { return @default; }
            set { @default = value; }
        }

        static readonly Variable<int?> scopeTimeout = Statics.ThreadVariable<int?>("scopeTimeout");
        public static int? ScopeTimeout { get { return scopeTimeout.Value; } }
        public static IDisposable CommandTimeoutScope(int? timeoutSeconds)
        {
            var old = scopeTimeout.Value;
            scopeTimeout.Value = timeoutSeconds;
            return new Disposable(() => scopeTimeout.Value = old);
        }

        public Connector(Schema schema, DynamicQueryManager dqm)
        {
            this.Schema = schema;
            this.DynamicQueryManager = dqm;
            this.IsolationLevel = IsolationLevel.Unspecified;
        }

        public Schema Schema { get; private set; }
        public DynamicQueryManager DynamicQueryManager { get; private set; }

        static readonly Variable<TextWriter> logger = Statics.ThreadVariable<TextWriter>("connectionlogger");
        public static TextWriter CurrentLogger
        {
            get { return logger.Value; }
            set { logger.Value = value; }
        }

        protected static void Log(SqlPreCommandSimple pcs)
        {
            var log = logger.Value;
            if (log != null)
            {
                log.WriteLine(pcs.Sql);
                if (pcs.Parameters != null)
                    log.WriteLine(pcs.Parameters
{'fraction_non_alphanumeric': 0.08097319901159475, 'fraction_numerical': 0.0, 'mean_word_length': 2.4504918032786884, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '914216', 'n_tokens_mistral': 1310, 'n_tokens_neox': 1280, 'n_words': 331}