Exemple #1
0
        public TOut ExecuteScalar <TOut>(PgSqlCommand sqlCommand)
        {
            Logger.Debug(() =>
            {
                return($"ExecuteScalar<{typeof(TOut).FullName}>::{sqlCommand.CommandAsPlainText()}");
            });
            TOut ret = default(TOut);

            try
            {
                lock (_lock)
                {
                    var command = PrepareCommand(sqlCommand.Command);

                    foreach (var npgsqlParameter in sqlCommand.Parameters)
                    {
                        command.Parameters.Add(npgsqlParameter.ToNpgsqlParameter());
                    }

                    ret = command.ExecuteScalar().CloneTo <TOut>();

                    EndCommand(command);
                }
            }
            catch (Exception e)
            {
                Logger.DebugFormat($"ExecuteScalar<{typeof(TOut).FullName}>::{{command}}", sqlCommand.CommandAsPlainText());
                Logger.DebugFormat($"ExecuteScalar<{typeof(TOut).FullName}>::{{e}}", e);
                throw;
            }
            return(ret);
        }
Exemple #2
0
        public int ExecuteNonQuery(PgSqlCommand sqlCommand)
        {
            Logger.Debug(() =>
            {
                return($"ExecuteNonQuery::{sqlCommand.CommandAsPlainText()}");
            });

            try
            {
                lock (_lock)
                {
                    int ret;
                    var command = PrepareCommand(sqlCommand.Command);

                    foreach (var param in sqlCommand.Parameters)
                    {
                        command.Parameters.Add(param.ToNpgsqlParameter());
                    }
                    ret = command.ExecuteNonQuery();

                    EndCommand(command);
                    return(ret);
                }
            }
            catch (Exception e)
            {
                Logger.ErrorFormat($"ExecuteNonQuery::{{e}}", e);
                throw;
            }
        }
Exemple #3
0
        public IEnumerable <JObject> ExecuteReader(PgSqlCommand sqlCommand)
        {
            Logger.Debug(() =>
            {
                return($"ExecuteReader::{sqlCommand.CommandAsPlainText()}");
            });

            try
            {
                lock (_lock)
                {
                    List <JObject> items = new List <JObject>();

                    var command = PrepareCommand(sqlCommand.Command);


                    foreach (var sqlCommandParameter in sqlCommand.Parameters)
                    {
                        var p = sqlCommandParameter.ToNpgsqlParameter();
                        command.Parameters.Add(p);
                    }

                    using (var reader = command.ExecuteReader())
                    {
                        var columns = reader.GetColumnSchema();
                        while (reader.Read())
                        {
                            var jo = new JObject();
                            for (int i = 0; i < reader.FieldCount; i++)
                            {
                                jo.Add(columns[i].ColumnName, DbExecuterHelper.ConvertFromDB(reader[i], columns[i]));
                            }
                            items.Add(jo);
                        }
                    }
                    EndCommand(command);

                    return(items);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(sqlCommand.CommandAsPlainText());
                Logger.Error(e, "ExecuteReader", sqlCommand.CommandAsPlainText());
                throw;
            }
        }
        public IEnumerable <T> ExecuteCollection <T>(QueryModel queryModel)
        {
            var psqlCommand = PsqlGeneratingQueryModelVisitor.GeneratePsqlQuery(queryModel, _table);

            var pgCommand = new PgSqlCommand();

            pgCommand.Command = psqlCommand.Statement;
            pgCommand.Parameters.AddRange(psqlCommand.Parameters);

            Debug.WriteLine(pgCommand.CommandAsPlainText());

            return(new DbExecuter(_table.GetConnectionString()).ExecuteReader <T>(pgCommand));
        }