Esempio n. 1
0
        /// <summary>
        /// This is the actual query wrapper where you read from the database more than a singular value
        /// </summary>
        /// <param name="query">Query string</param>
        /// <param name="parameters">Parameters in dictionary form</param>
        /// <returns>Result of the Query, List of rows containing dictionarys representing each row</returns>
        public Task <MySQLResult> QueryResult(string query, IDictionary <string, dynamic> parameters = null) => Task.Factory.StartNew(() =>
        {
            MySQLResult result = new MySQLResult();

            System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
            long connectionTime = 0, queryTime = 0, readTime = 0;

            using (Connection db = new Connection(settings.ConnectionString))
            {
                timer.Start();
                db.connection.Open();
                connectionTime = timer.ElapsedMilliseconds;

                using (MySqlCommand cmd = db.connection.CreateCommand())
                {
                    cmd.CommandText = query;
                    cmd.AddParameters(parameters);

                    try
                    {
                        timer.Restart();
                        using (MySqlDataReader reader = cmd.ExecuteReader())
                        {
                            queryTime = timer.ElapsedMilliseconds;
                            timer.Restart();
                            while (reader.Read())
                            {
                                result.Add(Enumerable.Range(0, reader.FieldCount).ToDictionary(reader.GetName,
                                                                                               i => (reader.IsDBNull(i)) ? null : reader.GetValue(i)));
                            }

                            if (settings.Debug)
                            {
                                query = cmd.Stringify();
                            }
                        }
                        readTime = timer.ElapsedMilliseconds;
                    }
                    catch (MySqlException mysqlEx)
                    {
                        PrintErrorInformation(mysqlEx);
                    }
                    // I don't think I want to catch the other exceptions. Just throw for now.
                }
            }

            timer.Stop();
            PrintDebugInformation(connectionTime, queryTime, readTime, query);

            return(result);
        }, CancellationToken.None, TaskCreationOptions.None, queryScheduler);
Esempio n. 2
0
        public Task <MySQLResult> QueryResult(string query, IDictionary <string, dynamic> parameters = null) => Task.Factory.StartNew(() => {
            MySQLResult result = new MySQLResult();

            System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
            long connectionTime = 0, queryTime = 0, readTime = 0;

            using (Connection db = new Connection(connectionString))
            {
                timer.Start();
                db.connection.Open();
                connectionTime = timer.ElapsedMilliseconds;

                using (MySqlCommand cmd = db.connection.CreateCommand())
                {
                    cmd.CommandText = query;
                    cmd.AddParameters(parameters);

                    timer.Restart();
                    using (MySqlDataReader reader = cmd.ExecuteReader())
                    {
                        queryTime = timer.ElapsedMilliseconds;
                        timer.Restart();
                        while (reader.Read())
                        {
                            result.Add(Enumerable.Range(0, reader.FieldCount).ToDictionary(reader.GetName, reader.GetValue));
                        }
                    }
                    readTime = timer.ElapsedMilliseconds;
                }
            }

            timer.Stop();
            PrintDebugInformation(connectionTime, queryTime, readTime, query);

            return(result);
        }, CancellationToken.None, TaskCreationOptions.None, queryScheduler);