Esempio n. 1
0
        /// <summary>
        /// Prints the specified result
        ///
        /// </summary>
        /// <param name="result">Result.</param>
        internal static void Print(ExecutionResults result)
        {
            if (!string.IsNullOrWhiteSpace(result.ErrorMessage))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"{result.ErrorMessage}\n");
                Console.ForegroundColor = ConsoleColor.White;
                return;
            }

            if (!result.HasRows)
            {
                return;
            }

            PrintHeader(result);
            PrintRows(result);
        }
Esempio n. 2
0
        /// <summary>
        /// Executes statement that does not return a set of rows.
        /// </summary>
        /// <returns>An empty <see cref="ExecutionResults"/> object.</returns>
        /// <param name="cmd">The IDbCommand that will execute the statement.</param>
        internal ExecutionResults ExecuteNonQuery(IDbCommand cmd)
        {
            var results = new ExecutionResults()
            {
                CommandText = cmd.CommandText
            };

            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                results.ErrorMessage = ex.Message;
            }

            return(results);
        }
Esempio n. 3
0
        /// <summary>
        /// Reads the raw results out of the data reader.
        /// </summary>
        /// <returns>The raw results.</returns>
        /// <param name="reader">Reader.</param>
        internal ExecutionResults ReadRawResults(SqlDataReader reader)
        {
            var rawData = new ExecutionResults()
            {
                ColumnCount = reader.FieldCount,
                Rows        = new List <IList <string> >()
            };

            try
            {
                if (!reader.HasRows)
                {
                    return(rawData);
                }

                var header = new List <string>();
                for (int i = 0; i < rawData.ColumnCount; i++)
                {
                    header.Add(reader.GetName(i));
                }

                rawData.Rows.Add(header);

                while (reader.Read())
                {
                    var row = new List <string>();

                    for (int i = 0; i < rawData.ColumnCount; i++)
                    {
                        var value = reader[i];
                        row.Add(value != null ? value.ToString() : "<null>");
                    }

                    rawData.Rows.Add(row);
                }
            }
            finally
            {
                reader.Close();
            }

            return(rawData);
        }
Esempio n. 4
0
        private static void PrintHeader(ExecutionResults result)
        {
            for (int h = 0; h < result.ColumnCount; h++)
            {
                if (h == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Magenta;
                    Console.Write($"\n{result.Rows[0][h]}");
                    continue;
                }

                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("|");
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.Write(result.Rows[0][h]);
            }

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("\n" + "-".PadRight(result.Padding.Values.Sum(), '-'));
        }
Esempio n. 5
0
        /// <summary>
        /// Executes a statement that is expected to return a rows.
        /// </summary>
        /// <returns>The results of the query.</returns>
        /// <param name="cmd">The IDbCommand that will execute the query.</param>
        internal ExecutionResults ExecuteReader(IDbCommand cmd)
        {
            var results = new ExecutionResults()
            {
                CommandText = cmd.CommandText
            };

            try
            {
                var reader  = cmd.ExecuteReader() as SqlDataReader;
                var rawData = this.ReadRawResults(reader);
                results = this.FormatQueryResult(rawData);
            }
            catch (Exception ex)
            {
                results.ErrorMessage = ex.Message;
            }

            return(results);
        }
Esempio n. 6
0
        internal ExecutionResults FormatQueryResult(ExecutionResults rawResults)
        {
            calculatePadding();

            var formattedResult = new ExecutionResults()
            {
                ColumnCount = rawResults.ColumnCount,
                Rows        = new List <IList <string> >(),
                Padding     = rawResults.Padding
            };

            for (var r = 0; r < rawResults.Rows.Count; r++)
            {
                var rawRow = rawResults.Rows[r];
                var newRow = new List <string>();

                for (var f = 0; f < rawResults.ColumnCount; f++)
                {
                    newRow.Add(rawRow[f].PadRight(rawResults.Padding[f], ' '));
                }

                formattedResult.Rows.Add(newRow);
            }

            return(formattedResult);

            void calculatePadding()
            {
                rawResults.Padding = new Dictionary <int, int>();

                for (int i = 0; i < rawResults.ColumnCount; i++)
                {
                    var maxFieldSize = rawResults.Rows
                                       .Select(r => r[i] ?? "")
                                       .Max(f => f.Count());

                    rawResults.Padding.Add(i, maxFieldSize + 2);
                }
            }
        }