/// <summary> /// Writes the events to the database using the transaction specified. /// </summary> /// <param name="dbTran">The transaction that the events will be executed under.</param> /// <param name="events">The array of events to insert into the database.</param> /// <remarks> /// <para> /// The transaction argument can be <c>null</c> if the appender has been /// configured not to use transactions. See <see cref="UseTransactions"/> /// property for more information. /// </para> /// </remarks> virtual protected void SendBuffer(IDbTransaction dbTran, LoggingEvent[] events) { // string.IsNotNullOrWhiteSpace() does not exist in ancient .NET frameworks if (CommandText != null && CommandText.Trim() != "") { using (IDbCommand dbCmd = Connection.CreateCommand()) { // Set the command string dbCmd.CommandText = CommandText; // Set the command type dbCmd.CommandType = CommandType; // Send buffer using the prepared command object if (dbTran != null) { dbCmd.Transaction = dbTran; } // prepare the command, which is significantly faster dbCmd.Prepare(); // run for all events foreach (LoggingEvent e in events) { // clear parameters that have been set dbCmd.Parameters.Clear(); // Set the parameter values foreach (AdoNetAppenderParameter param in m_parameters) { param.Prepare(dbCmd); param.FormatValue(dbCmd, e); } // Execute the query dbCmd.ExecuteNonQuery(); } } } else { // create a new command using (IDbCommand dbCmd = Connection.CreateCommand()) { if (dbTran != null) { dbCmd.Transaction = dbTran; } // run for all events foreach (LoggingEvent e in events) { // Get the command text from the Layout string logStatement = GetLogStatement(e); LogLog.Debug(declaringType, "LogStatement [" + logStatement + "]"); dbCmd.CommandText = logStatement; dbCmd.ExecuteNonQuery(); } } } }
public override void Prepare() { if (m_statementPreparer == null) { m_statementPreparer = new SqliteStatementPreparer(DatabaseHandle, CommandText.Trim()); } }
private string[] BuildStoredProcStatement() { #if CODECONTRACTS Contract.Ensures(Contract.Result <string[]>() != null); #endif StringBuilder spq = new StringBuilder(mStoredProcString); spq.Append(CommandText); spq.Append('('); // add parameter index $i for each IN and INOUT parameter int i = 1; bool comma = false; // in and inout parameter are positional in PqsqlParameterBuffer // out, inout, and return values are set using their column names in PqsqlDataReader foreach (PqsqlParameter p in Parameters) { ParameterDirection direction = p.Direction; if (direction == ParameterDirection.Output || direction == ParameterDirection.ReturnValue) { continue; } if (comma) { spq.Append(','); } spq.Append('$'); spq.Append(i++); comma = true; } // we create the query string // select * from Func($1,...) as "Func"; // when CommandText == "Func" // // + if func(.) defines out/inout variables (prorettype is record), // then the result columns will just be named after the output variables. // + otherwise, if func(.) has prorettype != record // (e.g., create function (i int) returns int as 'begin return i; end' ...), then we // get a predictable output column name called "Func" in the result record. The // problem is that whenever we prefix the function name with a schema name or use // mixed-case CommandText strings (e.g., "public"."func" or FuNc), the result column // would just be called "func" without "as \"Func\"". spq.Append(") as \""); spq.Append(CommandText.Trim().Replace("\"", "\"\"")); // escape double quotes spq.Append("\";"); return(new string[] { spq.ToString() }); }
private DbCommand GetDbCommandFromCommandText(DbUtility actualData) { DbCommand cmd = actualData.CreateDbCommand(); if (cmd != null) { cmd.CommandText = CommandText.Trim(); cmd.CommandTimeout = TimeOut; cmd.CommandType = (CommandType)Enum.Parse(typeof(CommandType), CommandType.ToString()); MatchCollection paramsName = Regex.Matches(CommandText, REGEX_GET_PARAMS, RegexOptions.IgnoreCase); MatchCollection localDefParams = Regex.Matches(CommandText, REGEX_GET_LOCAL_PARAMS, RegexOptions.IgnoreCase); var tempParams = new List <string>(); var tempLocalParams = new List <string>(); foreach (Match match in paramsName) { tempParams.Add(match.Value); } foreach (Match match in localDefParams) { tempLocalParams.Add(match.Value); } tempParams.RemoveAll(tempLocalParams.Contains); foreach (string match in tempParams) { if (!cmd.Parameters.Contains(match)) { DbParameter param = cmd.CreateParameter(); param.ParameterName = match; param.Direction = ParameterDirection.Input; param.DbType = DbType.String; cmd.Parameters.Add(param); } } return(cmd); } return(null); }
// requires: this must be a StoreProcedure command // effects: determines the EntityContainer function import referenced by this.CommandText private EdmFunction DetermineFunctionImport() { Debug.Assert(CommandType.StoredProcedure == CommandType); if (string.IsNullOrEmpty(CommandText) || string.IsNullOrEmpty(CommandText.Trim())) { throw new InvalidOperationException(Strings.EntityClient_FunctionImportEmptyCommandText); } // parse the command text string containerName; string functionImportName; string defaultContainerName = null; // no default container in EntityCommand CommandHelper.ParseFunctionImportCommandText(CommandText, defaultContainerName, out containerName, out functionImportName); return(CommandHelper.FindFunctionImport(_connection.GetMetadataWorkspace(), containerName, functionImportName)); }
private DbCommand GetDbCommand(DbUtility actualData) { DbCommand cmd = actualData.CreateDbCommand(); if (cmd != null) { cmd.CommandText = CommandText.Trim(); cmd.CommandTimeout = TimeOut; cmd.CommandType = (CommandType)Enum.Parse(typeof(CommandType), CommandType.ToString()); if (Parameters != null && Parameters.Param != null && Parameters.Param.Length > 0) { foreach (DataOperationsDataCommandParametersParam param in Parameters.Param) { cmd.Parameters.Add(param.GetDbParameter(cmd)); } } return(cmd); } return(null); }
/// <summary> /// Writes the events to the database using the transaction specified. /// </summary> /// <param name="dbTran">The transaction that the events will be executed under.</param> /// <param name="events">The array of events to insert into the database.</param> /// <remarks> /// <para> /// The transaction argument can be <c>null</c> if the appender has been /// configured not to use transactions. See <see cref="UseTransactions"/> /// property for more information. /// </para> /// </remarks> protected virtual void SendBuffer(IDbTransaction dbTran, LoggingEvent[] events) { // string.IsNotNullOrWhiteSpace() does not exist in ancient .NET frameworks if (CommandText != null && CommandText.Trim() != "") { using (IDbCommand dbCmd = Connection.CreateCommand()) { // Set the command string dbCmd.CommandText = CommandText; // Set the command type dbCmd.CommandType = CommandType; // Send buffer using the prepared command object if (dbTran != null) { dbCmd.Transaction = dbTran; } try { // prepare the command, which is significantly faster Prepare(dbCmd); } catch (Exception) { if (dbTran != null) { // rethrow exception in transaction mode, cuz now transaction is in failed state throw; } // ignore prepare exceptions as they can happen without affecting actual logging, eg on npgsql } // run for all events foreach (LoggingEvent e in events) { // No need to clear dbCmd.Parameters, just use existing. // Set the parameter values foreach (AdoNetAppenderParameter param in m_parameters) { param.FormatValue(dbCmd, e); } // Execute the query dbCmd.ExecuteNonQuery(); } } } else { // create a new command using (IDbCommand dbCmd = Connection.CreateCommand()) { if (dbTran != null) { dbCmd.Transaction = dbTran; } // run for all events foreach (LoggingEvent e in events) { // Get the command text from the Layout string logStatement = GetLogStatement(e); LogLog.Debug(declaringType, "LogStatement [" + logStatement + "]"); dbCmd.CommandText = logStatement; dbCmd.ExecuteNonQuery(); } } } }
void CommandReceived(IAsyncResult arg) { _activecommand = false; int CommandSize = 0; try { CommandSize = _clientSocket.EndReceive(arg); } catch { } if (CommandSize == 0) { Disconnect(); //return; } // Wait for the next command to be sent by the client try { _clientSocket.BeginReceive(_portBuffer, 0, _portBuffer.Length, SocketFlags.None, new AsyncCallback(CommandReceived), null); } catch { Disconnect(); return; } _lastcmdtime = DateTime.Now; string CommandText; if (_utf8) { CommandText = Encoding.UTF8.GetString(_portBuffer, 0, CommandSize).TrimStart(' '); } else { CommandText = Encoding.ASCII.GetString(_portBuffer, 0, CommandSize).TrimStart(' '); } string CmdArguments = null, Command = null; int End = 0; if ((End = CommandText.IndexOf(' ')) == -1) { End = (CommandText = CommandText.Trim()).Length; } else { CmdArguments = CommandText.Substring(End).TrimStart(' '); } Command = CommandText.Substring(0, End).ToUpper(); if (CmdArguments != null && CmdArguments.EndsWith("\r\n")) { CmdArguments = CmdArguments.Substring(0, CmdArguments.Length - 2); } bool CommandExecued = false; switch (Command) { case "USER": _activecommand = true; if (CmdArguments != null && CmdArguments.Length > 0) { SendMessage("331 Password required!"); _connuser = CmdArguments; } CommandExecued = true; break; case "PASS": _activecommand = true; if (_connuser == "") { SendMessage("503 Invalid User Name"); return; } if (_server.Users[_connuser] != null) { if (_server.Users[_connuser].SHA1Password == HelperFunctions.SHA1Hash(CmdArguments)) { _isAuthenticated = true; _currentDir = "/"; _currentperms = _server.Users.GetPermissions(_connuser); if (string.IsNullOrEmpty(_server.Users[_connuser].Startupdir) || _server.Users[_connuser].Startupdir == "/") { _localpath = _server.StartupDir; } else { _localpath = _server.Users[_connuser].Startupdir; } _server.Call_Log(new FTPLogEventArgs(FTPLogEventType.UserConnect, _connuser, true, 230, "none")); SendMessage("230 Authentication Successful"); } else { SendMessage("530 Authentication Failed!"); _server.Call_Log(new FTPLogEventArgs(FTPLogEventType.UserConnect, _connuser, false, 530, "none")); } } else { SendMessage("530 Authentication Failed!"); _server.Call_Log(new FTPLogEventArgs(FTPLogEventType.UserConnect, _connuser, false, 530, "none")); } CommandExecued = true; break; } if (!CommandExecued) { if (!_isAuthenticated) { SendMessage("530 Access Denied! Authenticate first"); return; } switch (Command.ToUpper()) { case "CWD": _activecommand = true; CWD(CmdArguments); break; case "CDUP": _activecommand = true; string[] pathParts = _currentDir.Split('/'); if (pathParts.Length > 1) { _currentDir = ""; for (int i = 0; i < (pathParts.Length - 2); i++) { _currentDir += pathParts[i] + "/"; } if (_currentDir.Length == 0) { _currentDir = "/"; } } SendMessage("250 CDUP command successful."); break; case "QUIT": _activecommand = true; SendMessage("221 FTP server signing off"); Disconnect(); break; case "PWD": _activecommand = true; SendMessage("257 \"" + _currentDir + "\""); break; case "PORT": _activecommand = true; PORT(CmdArguments); //done break; case "PASV": _activecommand = true; PASV(CmdArguments); //done break; case "TYPE": _activecommand = true; TYPE(CmdArguments); //done break; case "SYST": _activecommand = true; SendMessage("215 Windows_NT"); break; case "NOOP": _activecommand = true; SendMessage("200 OK"); break; case "RETR": _activecommand = true; RETR(CmdArguments); break; case "STOR": _activecommand = true; STOR(CmdArguments, false); break; case "APPE": _activecommand = true; APPE(CmdArguments); break; case "RNFR": _activecommand = true; RNFR(CmdArguments); break; case "RNTO": _activecommand = true; RNTO(CmdArguments); break; case "DELE": _activecommand = true; DELE(CmdArguments); break; case "RMD": _activecommand = true; RMD(CmdArguments); break; case "MKD": _activecommand = true; MKD(CmdArguments); break; case "LIST": _activecommand = true; LIST(_currentDir); break; case "NLST": _activecommand = true; NLST(CmdArguments); break; /*case "CLNT": * break;*/ case "MDTM": _activecommand = true; MDTM(CmdArguments); break; case "SIZE": _activecommand = true; SIZE(CmdArguments); break; case "OPTS": _activecommand = true; OPTS(CmdArguments); break; case "REIN": _activecommand = true; REIN(CmdArguments); break; case "STOU": _activecommand = true; STOR(CmdArguments, true); break; case "ABOR": case "SHUTDOWN": if (_datasocket != null && _datasocket.Connected) { _datasocket.Close(); } _datasocket = null; GC.Collect(); SendMessage("200 Data transfer aborted"); _server.Call_Log(new FTPLogEventArgs(FTPLogEventType.Abort, _connuser, true, 200)); break; case "FEAT": SendMessage(" SIZE"); SendMessage(" MTDM"); SendMessage("211 Feature list end"); _server.Call_Log(new FTPLogEventArgs(FTPLogEventType.FeatureList, _connuser, true, 211)); break; default: SendMessage("500 Unknown Command."); _server.Call_Log(new FTPLogEventArgs(FTPLogEventType.UnknownCommand, _connuser, true, 500, Command, CmdArguments)); break; // case "STAT": // break; // case "HELP": // break; // case "REST": // break; } } }
private void OnExecuting() { Log.Debug($"Executing SQL: {CommandText.Trim()}"); }