Ejemplo n.º 1
0
        /**
         * Given a SqlCommand and a way to execute that command,
         * we finally run the query. Used internally by GetReader, GetScarlar
         * and Execute methods.
         *
         * ```cs
         * 	var cmd = this.BuildCmd("SELECT * FROM Foo");
         * 	this.RunQuery
         * 	(
         * 		cmd,
         * 		query => query.ExecuteReader / ExecuteScalar / ExecuteNonQuery
         * 		affected =>
         * 		{
         * 			// given the result from the above execute method
         * 			// you then need to return the number of affected rows.
         * 		},
         * 		dispose: true // do you want the connection disposed afterwards
         * 	);
         * ```
         */
        protected object RunQuery(SqlCommand cmd, Func<SqlCommand, object> query, Func<object, int> affected, bool dispose = true)
        {
            object results;

            try
            {
                results = query.Invoke(cmd);

                if (this.Db.LogWriter != null)
                {
                    this.Db.LogWriter.WriteLine
                    (
                        cmd.ToTraceString
                        (
                            affected.Invoke(results)
                        )
                    );
                }
            }
            catch
            {
                if (this.Db.LogWriter != null)
                {
                    this.Db.LogWriter.WriteLine("-- !!!NEXT QUERY ERRORED!!!");
                    this.Db.LogWriter.WriteLine(cmd.ToTraceString());
                }

                throw;
            }
            finally
            {
                if (dispose) cmd.Connection.Dispose();
            }

            return results;
        }
Ejemplo n.º 2
0
        public void ToTraceStringWithParamsTest()
        {
            var cmd = new SqlCommand("SELECT * FROM Foo WHERE Id = @p0");
            cmd.Parameters.AddWithValue("@p0", 1);

            var expected = new StringBuilder();
            expected.AppendLine("================================================================================");
            expected.AppendLine("SELECT * FROM Foo WHERE Id = @p0");
            expected.AppendLine("-- @p0: Input Int (Size = 0) [1]");
            expected.AppendLine("-- [0] records affected.");
            expected.AppendLine();

            Assert.Equal
            (
                expected.ToString(),
                cmd.ToTraceString()
            );
        }
Ejemplo n.º 3
0
        public void ToTraceStringTest()
        {
            var cmd = new SqlCommand("SELECT * FROM Foo");

            var expected = new StringBuilder();
            expected.AppendLine("================================================================================");
            expected.AppendLine("SELECT * FROM Foo");
            expected.AppendLine("-- [0] records affected.");
            expected.AppendLine();

            Assert.Equal
            (
                expected.ToString(),
                cmd.ToTraceString()
            );
        }
Ejemplo n.º 4
0
        public object ExecuteScalar(SqlCommand command)
        {
            //overime stav klienta
            this.InternalCheckClientState();

            //osetrenie vstupneho argumentu
            if (command == null)
                throw new ArgumentNullException("command");
            if (String.IsNullOrEmpty(command.CommandText))
                throw new ArgumentException("Argument 'command' is not valid.");

            try
            {
                //zalogujeme
                this.InternalTrace(TraceTypes.Verbose, "SQL Command: '{0}'", command.ToTraceString());

                //pridame aktivne spojenie do priakzu
                command.Connection = this.m_connection;
                if (this.m_transaction != null)
                {
                    command.Transaction = this.m_transaction;
                }

                //vykoname pozadovany prikaz
                return command.ExecuteScalar();
            }
            catch (SqlException ex)
            {
                //zalogujeme
                this.InternalTrace(TraceTypes.Error, "Chyba pri vykonavani SQL prikazu. {0}", ex);

                //preposleme vynimku vyssie
                throw;
            }
            catch (Exception ex)
            {
                //zalogujeme
                this.InternalTrace(TraceTypes.Error, "Chyba pri vykonavani SQL prikazu. {0}", ex);

                //preposleme vynimku vyssie
                throw;
            }
        }