예제 #1
0
        /// <summary>
        /// this will derive the parameters if this is a StoredProcedure type of command
        /// </summary>
        /// <param name="commandText"></param>
        /// <param name="connectionString"></param>
        /// <param name="commandType"></param>
        /// <returns></returns>
        public static SqlCommand GetCommand(this string commandText, string connectionString, int commandTimeout = 30,
                                            CommandType commandType = CommandType.StoredProcedure)
        {
            connectionString = ConnectionMaintenance.ConnectionStringTimeout(connectionString);
            var found = getFromCache(commandText, connectionString);

            if (found == null)
            {
                using (var conn = new SqlConnection(connectionString)) {
                    var sqlCommand = new SqlCommand {
                        CommandText    = commandText,
                        CommandType    = commandType,
                        CommandTimeout = commandTimeout
                    };

                    if (commandType == CommandType.StoredProcedure)
                    {
                        CommandBuilder.DeriveParametersForProcedure(commandText, connectionString, sqlCommand);
                    }
                    else
                    {
                        deriveParametersForInlineCommand(sqlCommand);
                    }
                    if (SqlCommandCacheTimeout != SqlCommandCacheTimeout.IsNeverCached)
                    {
                        CachedCommands.Add(new SqlCommandCacheObject(commandText, connectionString, sqlCommand));
                    }
                    return(sqlCommand);
                }
            }
            else
            {
                return(createCommandFromCachedDefinedCommand(found));
            }
        }
예제 #2
0
        public static void RemoveFromCache(SqlCommand command)
        {
            var found = getFromCache(command.CommandText, command.Connection.ConnectionString);

            if (found != null)
            {
                CachedCommands.Remove(found);
            }
        }
예제 #3
0
        static SqlCommandCacheObject getFromCache(string commandText, string connectionString)
        {
            var key   = commandText + connectionString;
            var found = CachedCommands.FirstOrDefault(o => o.Key == key);

            if (found != null &&
                found.ExpiresAt < DateTime.Now)
            {
                found = null;
            }
            return(found);
        }
예제 #4
0
        protected virtual void InsertPlugin(Type type, StartupConfig startupConfig)
        {
            try
            {
                PluginBase plugin = (PluginBase)Activator.CreateInstance(type);
                string     pluginType, error = "", commands = "";
                switch (plugin.PluginType)
                {
                case PluginType.Command:
                    pluginType = "命令";
                    CommandPlugin cmdPlugin = (CommandPlugin)plugin;
                    if (cmdPlugin.Commands != null && cmdPlugin.Commands.Length > 0)
                    {
                        foreach (var cmd in cmdPlugin.Commands)
                        {
                            TaggedPlugins.Add(new TaggedClass <PluginBase>(cmd, cmdPlugin));
                            CachedCommands.Add(new TaggedClass <Type>(cmd, type));
                        }

                        commands = $"({string.Join(",", cmdPlugin.Commands)}) ";
                    }
                    else
                    {
                        error = "但此命令插件未设置命令。";
                    }

                    break;

                case PluginType.Unknown:
                    throw new NotSupportedException();

                case PluginType.Application:
                case PluginType.Service:
                default:
                    pluginType = plugin.PluginType == PluginType.Application ? "应用" : "服务";
                    TaggedPlugins.Add(new TaggedClass <PluginBase>(null, plugin));
                    break;
                }

                plugin.OnInitialized(startupConfig);
                AllPluginInitialized += plugin.AllPlugins_Initialized;
                Logger.Origin($"{pluginType} \"{plugin.Name}\" {commands}已经加载完毕。{error}");
            }
            catch (Exception ex)
            {
                Logger.Exception(ex.InnerException ?? ex);
                Logger.Error($"加载插件{type.Name}失败。");
            }
        }
예제 #5
0
 public Type GetPluginType(string command)
 {
     return(CachedCommands.FirstOrDefault(k => k.Tag == command).Instance);
 }