public ProcedureCacheEntry GetProcedure(MySqlConnection conn, string spName, string cacheKey) { ProcedureCacheEntry procedureCacheEntry = null; if (cacheKey != null) { int hashCode = cacheKey.GetHashCode(); lock (this.procHash) { this.procHash.TryGetValue(hashCode, out procedureCacheEntry); } } if (procedureCacheEntry == null) { procedureCacheEntry = this.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(procedureCacheEntry); }
internal string GetCacheKey(string spName, ProcedureCacheEntry proc) { string str = string.Empty; StringBuilder stringBuilder = new StringBuilder(spName); stringBuilder.Append("("); string text = ""; if (proc.parameters != null) { foreach (MySqlSchemaRow current in proc.parameters.Rows) { if (current["ORDINAL_POSITION"].Equals(0)) { str = "?="; } else { stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}?", new object[] { text }); text = ","; } } } stringBuilder.Append(")"); return(str + stringBuilder.ToString()); }
internal string GetCacheKey(string spName, ProcedureCacheEntry proc) { string retValue = String.Empty; StringBuilder key = new StringBuilder(spName); key.Append("("); string delimiter = ""; if (proc.parameters != null) { foreach (MySqlSchemaRow row in proc.parameters.Rows) { if (row["ORDINAL_POSITION"].Equals(0)) { retValue = "?="; } else { key.AppendFormat(CultureInfo.InvariantCulture, "{0}?", delimiter); delimiter = ","; } } } key.Append(")"); return(retValue + key.ToString()); }
private static ProcedureCacheEntry GetProcData(MySqlConnection connection, string spName) { string text = string.Empty; string text2 = spName; int num = spName.IndexOf("."); if (num != -1) { text = spName.Substring(0, num); text2 = spName.Substring(num + 1, spName.Length - num - 1); } string[] array = new string[4]; array[1] = ((text.Length > 0) ? text : connection.CurrentDatabase()); array[2] = text2; MySqlSchemaCollection schemaCollection = connection.GetSchemaCollection("procedures", array); if (schemaCollection.Rows.Count > 1) { throw new MySqlException(Resources.ProcAndFuncSameName); } if (schemaCollection.Rows.Count == 0) { throw new MySqlException(string.Format(Resources.InvalidProcName, text2, text)); } ProcedureCacheEntry procedureCacheEntry = new ProcedureCacheEntry(); procedureCacheEntry.procedure = schemaCollection; ISSchemaProvider iSSchemaProvider = new ISSchemaProvider(connection); string[] restrictions = iSSchemaProvider.CleanRestrictions(array); MySqlSchemaCollection procedureParameters = iSSchemaProvider.GetProcedureParameters(restrictions, schemaCollection); procedureCacheEntry.parameters = procedureParameters; return(procedureCacheEntry); }
private ProcedureCacheEntry GetParameters(string procName) { string procCacheKey = GetCacheKey(procName); ProcedureCacheEntry entry = Connection.ProcedureCache.GetProcedure(Connection, procName, procCacheKey); return(entry); }
private ProcedureCacheEntry AddNew(MySqlConnection connection, string spName) { ProcedureCacheEntry procData = GetProcData(connection, spName); if (_maxSize <= 0) { return(procData); } 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); }
internal string GetCacheKey(string spName, ProcedureCacheEntry proc) { string str = string.Empty; StringBuilder stringBuilder = new StringBuilder(spName); stringBuilder.Append("("); string text = ""; if (proc.parameters != null) { using (IEnumerator <MySqlSchemaRow> enumerator = proc.parameters.Rows.GetEnumerator()) { while (enumerator.MoveNext()) { if (enumerator.Current["ORDINAL_POSITION"].Equals(0)) { str = "?="; } else { stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}?", new object[] { text }); text = ","; } } } } stringBuilder.Append(")"); return(str + stringBuilder.ToString()); }
/// <summary> /// Retrieves parameter information from the stored procedure specified /// in the MySqlCommand and populates the Parameters collection of the /// specified MySqlCommand object. /// This method is not currently supported since stored procedures are /// not available in MySql. /// </summary> /// <param name="command">The MySqlCommand referencing the stored /// procedure from which the parameter information is to be derived. /// The derived parameters are added to the Parameters collection of the /// MySqlCommand.</param> /// <exception cref="InvalidOperationException">The command text is not /// a valid stored procedure name.</exception> public static void DeriveParameters(MySqlCommand command) { if (command.CommandType != CommandType.StoredProcedure) { throw new InvalidOperationException(Resources.CanNotDeriveParametersForTextCommands); } // retrieve the proc definition from the cache. string spName = command.CommandText; if (spName.IndexOf(".") == -1) { spName = command.Connection.Database + "." + spName; } try { ProcedureCacheEntry entry = command.Connection.ProcedureCache.GetProcedure(command.Connection, spName, null); command.Parameters.Clear(); foreach (MySqlSchemaRow row in entry.parameters.Rows) { MySqlParameter p = new MySqlParameter(); p.ParameterName = String.Format("@{0}", row["PARAMETER_NAME"]); if (row["ORDINAL_POSITION"].Equals(0) && p.ParameterName == "@") { p.ParameterName = "@RETURN_VALUE"; } p.Direction = GetDirection(row); bool unsigned = StoredProcedure.GetFlags(row["DTD_IDENTIFIER"].ToString()).IndexOf("UNSIGNED") != -1; bool real_as_float = entry.procedure.Rows[0]["SQL_MODE"].ToString().IndexOf("REAL_AS_FLOAT") != -1; p.MySqlDbType = MetaData.NameToType(row["DATA_TYPE"].ToString(), unsigned, real_as_float, command.Connection); if (row["CHARACTER_MAXIMUM_LENGTH"] != null) { p.Size = (int)row["CHARACTER_MAXIMUM_LENGTH"]; } if (row["NUMERIC_PRECISION"] != null) { p.Precision = Convert.ToByte(row["NUMERIC_PRECISION"]); } if (row["NUMERIC_SCALE"] != null) { p.Scale = Convert.ToByte(row["NUMERIC_SCALE"]); } if (p.MySqlDbType == MySqlDbType.Set || p.MySqlDbType == MySqlDbType.Enum) { p.PossibleValues = GetPossibleValues(row); } command.Parameters.Add(p); } } catch (InvalidOperationException ioe) { throw new MySqlException(Resources.UnableToDeriveParameters, ioe); } }
public static void DeriveParameters(MySqlCommand command) { if (command.CommandType != CommandType.StoredProcedure) { throw new InvalidOperationException(Resources.CanNotDeriveParametersForTextCommands); } string text = command.CommandText; if (text.IndexOf(".") == -1) { text = command.Connection.Database + "." + text; } try { ProcedureCacheEntry procedure = command.Connection.ProcedureCache.GetProcedure(command.Connection, text, null); command.Parameters.Clear(); foreach (MySqlSchemaRow current in procedure.parameters.Rows) { MySqlParameter mySqlParameter = new MySqlParameter(); mySqlParameter.ParameterName = string.Format("@{0}", current["PARAMETER_NAME"]); if (current["ORDINAL_POSITION"].Equals(0) && mySqlParameter.ParameterName == "@") { mySqlParameter.ParameterName = "@RETURN_VALUE"; } mySqlParameter.Direction = MySqlCommandBuilder.GetDirection(current); bool unsigned = StoredProcedure.GetFlags(current["DTD_IDENTIFIER"].ToString()).IndexOf("UNSIGNED") != -1; bool realAsFloat = procedure.procedure.Rows[0]["SQL_MODE"].ToString().IndexOf("REAL_AS_FLOAT") != -1; mySqlParameter.MySqlDbType = MetaData.NameToType(current["DATA_TYPE"].ToString(), unsigned, realAsFloat, command.Connection); if (current["CHARACTER_MAXIMUM_LENGTH"] != null) { mySqlParameter.Size = (int)current["CHARACTER_MAXIMUM_LENGTH"]; } if (current["NUMERIC_PRECISION"] != null) { mySqlParameter.Precision = Convert.ToByte(current["NUMERIC_PRECISION"]); } if (current["NUMERIC_SCALE"] != null) { mySqlParameter.Scale = Convert.ToByte(current["NUMERIC_SCALE"]); } if (mySqlParameter.MySqlDbType == MySqlDbType.Set || mySqlParameter.MySqlDbType == MySqlDbType.Enum) { mySqlParameter.PossibleValues = MySqlCommandBuilder.GetPossibleValues(current); } command.Parameters.Add(mySqlParameter); } } catch (InvalidOperationException ex) { throw new MySqlException(Resources.UnableToDeriveParameters, ex); } }
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(1, dotIndex - 1); name = spName.Substring(dotIndex + 3, spName.Length - dotIndex - 4); } 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) { string msg = string.Format(Resources.InvalidProcName, name, schema) + " " + string.Format(Resources.ExecuteProcedureUnauthorized, connection.Settings.UserID, connection.Settings.Server); throw new MySqlException(msg); } 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); }
private MySqlParameterCollection CheckParameters(string spName) { MySqlParameterCollection mySqlParameterCollection = new MySqlParameterCollection(this.command); MySqlParameter returnParameter = this.GetReturnParameter(); ProcedureCacheEntry parameters = this.GetParameters(spName); if (parameters.procedure == null || parameters.procedure.Rows.Count == 0) { throw new InvalidOperationException(string.Format(Resources.RoutineNotFound, spName)); } bool realAsFloat = parameters.procedure.Rows[0]["SQL_MODE"].ToString().IndexOf("REAL_AS_FLOAT") != -1; foreach (MySqlSchemaRow current in parameters.parameters.Rows) { mySqlParameterCollection.Add(this.GetAndFixParameter(spName, current, realAsFloat, returnParameter)); } return(mySqlParameterCollection); }
private MySqlParameterCollection CheckParameters(string spName) { MySqlParameterCollection newParms = new MySqlParameterCollection(command); MySqlParameter returnParameter = GetReturnParameter(); ProcedureCacheEntry entry = GetParameters(spName); if (entry.procedure == null || entry.procedure.Rows.Count == 0) { throw new InvalidOperationException(String.Format(Resources.RoutineNotFound, spName)); } bool realAsFloat = entry.procedure.Rows[0]["SQL_MODE"].ToString().IndexOf("REAL_AS_FLOAT") != -1; foreach (MySqlSchemaRow param in entry.parameters.Rows) { newParms.Add(GetAndFixParameter(spName, param, realAsFloat, returnParameter)); } return(newParms); }
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); } return(proc); }
private ProcedureCacheEntry AddNew(MySqlConnection connection, string spName) { ProcedureCacheEntry procData = ProcedureCache.GetProcData(connection, spName); if (this.maxSize > 0) { string cacheKey = this.GetCacheKey(spName, procData); int hashCode = cacheKey.GetHashCode(); lock (this.procHash) { if (this.procHash.Keys.Count >= this.maxSize) { this.TrimHash(); } if (!this.procHash.ContainsKey(hashCode)) { this.procHash[hashCode] = procData; this.hashQueue.Enqueue(hashCode); } } } return(procData); }
internal string GetCacheKey(string spName, ProcedureCacheEntry proc) { string retValue = String.Empty; StringBuilder key = new StringBuilder(spName); key.Append("("); string delimiter = ""; if (proc.parameters != null) { foreach (MySqlSchemaRow row in proc.parameters.Rows) { if (row["ORDINAL_POSITION"].Equals(0)) retValue = "?="; else { key.AppendFormat(CultureInfo.InvariantCulture, "{0}?", delimiter); delimiter = ","; } } } key.Append(")"); return retValue + key.ToString(); }
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; }