public MySqlTransactionScope(MySqlConnection con, Transaction trans, MySqlTransaction simpleTransaction) { connection = con; baseTransaction = trans; this.simpleTransaction = simpleTransaction; }
public ProcedureCacheEntry GetProcedure(MySqlConnection conn, string spName, string cacheKey) { ProcedureCacheEntry proc = null; if (cacheKey != null) { int hash = cacheKey.GetHashCode(); lock (procHash) { procHash.TryGetValue(hash, out proc); } } if (proc == null) { proc = AddNew(conn, spName); conn.PerfMonitor.AddHardProcedureQuery(); if (conn.Settings.Logging) MySqlTrace.LogInformation(conn.ServerThread, String.Format(Resources.HardProcQuery, spName)); } else { conn.PerfMonitor.AddSoftProcedureQuery(); if (conn.Settings.Logging) MySqlTrace.LogInformation(conn.ServerThread, String.Format(Resources.SoftProcQuery, spName)); } return proc; }
/// <include file='docs/mysqlcommand.xml' path='docs/ctor4/*'/> public MySqlCommand(string cmdText, MySqlConnection connection, MySqlTransaction transaction) : this(cmdText, connection) { curTransaction = transaction; }
public ExceptionInterceptor(MySqlConnection connection) { this.connection = connection; LoadInterceptors(connection.Settings.ExceptionInterceptors); // we always have the standard interceptor interceptors.Add(new StandardExceptionInterceptor()); }
public MySqlBulkLoader(MySqlConnection connection) { Connection = connection; Local = true; FieldTerminator = defaultFieldTerminator; LineTerminator = defaultLineTerminator; FieldQuotationCharacter = Char.MinValue; ConflictOption = MySqlBulkLoaderConflictOption.None; columns = new List<string>(); expressions = new List<string>(); }
/// <summary> /// Executes a single command against a MySQL database. A new <see cref="MySqlConnection"/> is created /// using the <see cref="MySqlConnection.ConnectionString"/> given. /// </summary> /// <param name="connectionString"><see cref="MySqlConnection.ConnectionString"/> to use</param> /// <param name="commandText">SQL command to be executed</param> /// <param name="parms">Array of <see cref="MySqlParameter"/> objects to use with the command.</param> /// <returns></returns> public static int ExecuteNonQuery(string connectionString, string commandText, params MySqlParameter[] parms) { //create & open a SqlConnection, and dispose of it after we are done. using (MySqlConnection cn = new MySqlConnection(connectionString)) { cn.Open(); //call the overload that takes a connection in place of the connection string return ExecuteNonQuery(cn, commandText, parms); } }
/// <summary> /// Executes a single command against a MySQL database. The <see cref="MySqlConnection"/> is assumed to be /// open when the method is called and remains open after the method completes. /// </summary> /// <param name="connection"><see cref="MySqlConnection"/> object to use</param> /// <param name="commandText">SQL command to be executed</param> /// <param name="commandParameters">Array of <see cref="MySqlParameter"/> objects to use with the command.</param> /// <returns></returns> public static int ExecuteNonQuery(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters) { //create a command and prepare it for execution MySqlCommand cmd = new MySqlCommand(); cmd.Connection = connection; cmd.CommandText = commandText; cmd.CommandType = CommandType.Text; if (commandParameters != null) foreach (MySqlParameter p in commandParameters) cmd.Parameters.Add(p); int result = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return result; }
public SystemPerformanceMonitor(MySqlConnection connection) : base(connection) { string categoryName = Resources.PerfMonCategoryName; if (connection.Settings.UsePerformanceMonitor && procedureHardQueries == null) { try { procedureHardQueries = new PerformanceCounter(categoryName, "HardProcedureQueries", false); procedureSoftQueries = new PerformanceCounter(categoryName, "SoftProcedureQueries", false); } catch (Exception ex) { MySqlTrace.LogError(connection.ServerThread, ex.Message); } } }
internal MySqlTransaction(MySqlConnection c, IsolationLevel il) { conn = c; level = il; open = true; }
public Task ClearPoolAsync(MySqlConnection connection, CancellationToken cancellationToken) { var result = new TaskCompletionSource<bool>(); if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested) { try { ClearPool(connection); result.SetResult(true); } catch (Exception ex) { result.SetException(ex); } } else { result.SetCanceled(); } return result.Task; }
/// <summary> /// Async version of ClearPool /// </summary> /// <param name="connection">The connection associated with the pool to be cleared.</param> /// <returns></returns> public Task ClearPoolAsync(MySqlConnection connection) { return ClearPoolAsync(connection, CancellationToken.None); }
/// <include file='docs/MySqlConnection.xml' path='docs/ClearPool/*'/> public static void ClearPool(MySqlConnection connection) { MySqlPoolManager.ClearPool(connection.Settings); }
private int GetTimeZoneOffset(MySqlConnection con) { MySqlCommand cmd = new MySqlCommand("select timediff( curtime(), utc_time() )", con); string s = cmd.ExecuteScalar() as string; if (s == null) s = "0:00"; return int.Parse(s.Substring(0, s.IndexOf(':'))); }
public CommandTimer(MySqlConnection connection, int timeout) { this.connection = connection; if (connection != null) { timeoutSet = connection.SetCommandTimeout(timeout); } }
private static ProcedureCacheEntry GetProcData(MySqlConnection connection, string spName) { string schema = String.Empty; string name = spName; int dotIndex = spName.IndexOf("."); if (dotIndex != -1) { schema = spName.Substring(0, dotIndex); name = spName.Substring(dotIndex + 1, spName.Length - dotIndex - 1); } string[] restrictions = new string[4]; restrictions[1] = schema.Length > 0 ? schema : connection.CurrentDatabase(); restrictions[2] = name; MySqlSchemaCollection proc = connection.GetSchemaCollection("procedures", restrictions); if (proc.Rows.Count > 1) throw new MySqlException(Resources.ProcAndFuncSameName); if (proc.Rows.Count == 0) throw new MySqlException(String.Format(Resources.InvalidProcName, name, schema)); ProcedureCacheEntry entry = new ProcedureCacheEntry(); entry.procedure = proc; // we don't use GetSchema here because that would cause another // query of procedures and we don't need that since we already // know the procedure we care about. ISSchemaProvider isp = new ISSchemaProvider(connection); string[] rest = isp.CleanRestrictions(restrictions); MySqlSchemaCollection parameters = isp.GetProcedureParameters(rest, proc); entry.parameters = parameters; return entry; }
public virtual void Configure(MySqlConnection connection) { bool firstConfigure = false; // if we have not already configured our server variables // then do so now if (serverProps == null) { firstConfigure = true; // if we are in a pool and the user has said it's ok to cache the // properties, then grab it from the pool try { if (Pool != null && Settings.CacheServerProperties) { if (Pool.ServerProperties == null) Pool.ServerProperties = LoadServerProperties(connection); serverProps = Pool.ServerProperties; } else serverProps = LoadServerProperties(connection); LoadCharacterSets(connection); } catch (MySqlException ex) { // expired password capability if (ex.Number == 1820) { IsPasswordExpired = true; return; } throw; } } #if AUTHENTICATED string licenseType = serverProps["license"]; if (licenseType == null || licenseType.Length == 0 || licenseType != "commercial") throw new MySqlException( "This client library licensed only for use with commercially-licensed MySQL servers." ); #endif // if the user has indicated that we are not to reset // the connection and this is not our first time through, // then we are done. if (!Settings.ConnectionReset && !firstConfigure) return; string charSet = connectionString.CharacterSet; if (charSet == null || charSet.Length == 0) { if (serverCharSetIndex >= 0 && charSets.ContainsKey(serverCharSetIndex)) charSet = (string)charSets[serverCharSetIndex]; else charSet = serverCharSet; } if (serverProps.ContainsKey("max_allowed_packet")) maxPacketSize = Convert.ToInt64(serverProps["max_allowed_packet"]); // now tell the server which character set we will send queries in and which charset we // want results in MySqlCommand charSetCmd = new MySqlCommand("SET character_set_results=NULL", connection); charSetCmd.InternallyCreated = true; string clientCharSet; serverProps.TryGetValue("character_set_client", out clientCharSet); string connCharSet; serverProps.TryGetValue("character_set_connection", out connCharSet); if ((clientCharSet != null && clientCharSet.ToString() != charSet) || (connCharSet != null && connCharSet.ToString() != charSet)) { MySqlCommand setNamesCmd = new MySqlCommand("SET NAMES " + charSet, connection); setNamesCmd.InternallyCreated = true; setNamesCmd.ExecuteNonQuery(); } charSetCmd.ExecuteNonQuery(); if (charSet != null) Encoding = CharSetMap.GetEncoding(Version, charSet); else Encoding = CharSetMap.GetEncoding(Version, "utf8"); handler.Configure(); }
public SchemaProvider(MySqlConnection connectionToUse) { connection = connectionToUse; }
public virtual void CloseQuery(MySqlConnection connection, int statementId) { if (handler.WarningCount > 0) ReportWarnings(connection); }
public virtual List<MySqlError> ReportWarnings(MySqlConnection connection) { List<MySqlError> warnings = new List<MySqlError>(); MySqlCommand cmd = new MySqlCommand("SHOW WARNINGS", connection); cmd.InternallyCreated = true; using (MySqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { warnings.Add(new MySqlError(reader.GetString(0), reader.GetInt32(1), reader.GetString(2))); } } MySqlInfoMessageEventArgs args = new MySqlInfoMessageEventArgs(); args.errors = warnings.ToArray(); if (connection != null) connection.OnInfoMessage(args); return warnings; }
/// <summary> /// Loads all the current character set names and ids for this server /// into the charSets hashtable /// </summary> private void LoadCharacterSets(MySqlConnection connection) { MySqlCommand cmd = new MySqlCommand("SHOW COLLATION", connection); // now we load all the currently active collations try { using (MySqlDataReader reader = cmd.ExecuteReader()) { charSets = new Dictionary<int, string>(); while (reader.Read()) { charSets[Convert.ToInt32(reader["id"], NumberFormatInfo.InvariantInfo)] = reader.GetString(reader.GetOrdinal("charset")); } } } catch (Exception ex) { MySqlTrace.LogError(ThreadID, ex.Message); throw; } }
public MySqlPromotableTransaction(MySqlConnection connection, Transaction baseTransaction) { this.connection = connection; this.baseTransaction = baseTransaction; }
private ProcedureCacheEntry AddNew(MySqlConnection connection, string spName) { ProcedureCacheEntry procData = GetProcData(connection, spName); if (maxSize > 0) { string cacheKey = GetCacheKey(spName, procData); int hash = cacheKey.GetHashCode(); lock (procHash) { if (procHash.Keys.Count >= maxSize) TrimHash(); if (!procHash.ContainsKey(hash)) { procHash[hash] = procData; hashQueue.Enqueue(hash); } } } return procData; }
/// <include file='docs/MySqlDataAdapter.xml' path='docs/Ctor2/*'/> public MySqlDataAdapter(string selectCommandText, MySqlConnection connection) : this() { SelectCommand = new MySqlCommand(selectCommandText, connection); }
/// <include file='docs/mysqlcommand.xml' path='docs/ctor3/*'/> public MySqlCommand(string cmdText, MySqlConnection connection) : this(cmdText) { Connection = connection; }
/// <summary> /// Creates a new MySqlConnection object with the exact same ConnectionString value /// </summary> /// <returns>A cloned MySqlConnection object</returns> public object Clone() { MySqlConnection clone = new MySqlConnection(); string connectionString = Settings.ConnectionString; if (connectionString != null) clone.ConnectionString = connectionString; return clone; }
/// <summary> /// Loads the properties from the connected server into a hashtable /// </summary> /// <param name="connection"></param> /// <returns></returns> private Dictionary<string, string> LoadServerProperties(MySqlConnection connection) { // load server properties Dictionary<string, string> hash = new Dictionary<string, string>(); MySqlCommand cmd = new MySqlCommand("SHOW VARIABLES", connection); try { using (MySqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { string key = reader.GetString(0); string value = reader.GetString(1); hash[key] = value; } } // Get time zone offset as numerical value timeZoneOffset = GetTimeZoneOffset(connection); return hash; } catch (Exception ex) { MySqlTrace.LogError(ThreadID, ex.Message); throw; } }
public PerformanceMonitor(MySqlConnection connection) { Connection = connection; }
public void CancelQuery(int timeout) { MySqlConnectionStringBuilder cb = new MySqlConnectionStringBuilder( Settings.ConnectionString); cb.Pooling = false; cb.AutoEnlist = false; cb.ConnectionTimeout = (uint)timeout; using (MySqlConnection c = new MySqlConnection(cb.ConnectionString)) { c.isKillQueryConnection = true; c.Open(); string commandText = "KILL QUERY " + ServerThread; MySqlCommand cmd = new MySqlCommand(commandText, c); cmd.CommandTimeout = timeout; cmd.ExecuteNonQuery(); } }
public void Dispose() { if (timeoutSet) { timeoutSet = false; connection.ClearCommandTimeout(); connection = null; } }
public ISSchemaProvider(MySqlConnection connection) : base(connection) { }