Ejemplo n.º 1
0
 void SqlMapper.IDynamicParameters.AddParameters(IDbCommand command, SqlMapper.Identity identity)
 {
     // we just rely on Dapper.DynamicParameters which implements IDynamicParameters like a charm
     ((SqlMapper.IDynamicParameters)DapperParameters).AddParameters(command, identity);
 }
Ejemplo n.º 2
0
        private static IEnumerable QueryInternal(Type type, Mapping.Table table, System.Data.IDbConnection connection, String sql, Object param, IDbTransaction transaction, CommandType?commandType, Int32?commandTimeout)
        {
            var command  = new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.Buffered);
            var identity = new SqlMapper.Identity(sql, commandType, connection, type, param == null ? null : param.GetType(), null);
            var info     = SqlMapper.GetCacheInfo(identity, param, command.AddToCache);

            IDbCommand  cmd    = null;
            IDataReader reader = null;

            Boolean wasClosed = connection.State == ConnectionState.Closed;

            try
            {
                cmd = command.SetupCommand(connection, info.ParamReader);

                if (wasClosed)
                {
                    connection.Open();
                }
                reader    = cmd.ExecuteReader(wasClosed ? CommandBehavior.CloseConnection | CommandBehavior.SequentialAccess : CommandBehavior.SequentialAccess);
                wasClosed = false; // *if* the connection was closed and we got this far, then we now have a reader
                // with the CloseConnection flag, so the reader will deal with the connection; we
                // still need something in the "finally" to ensure that broken SQL still results
                // in the connection closing itself
                var tuple = info.Deserializer;
                int hash  = SqlMapper.GetColumnHash(reader);
                if (tuple.Func == null || tuple.Hash != hash)
                {
                    if (reader.FieldCount == 0) //https://code.google.com/p/dapper-dot-net/issues/detail?id=57
                    {
                        yield break;
                    }
                    tuple = info.Deserializer = new SqlMapper.DeserializerState(hash, GetDeserializer(type, reader, 0, -1, false, table));
                    if (command.AddToCache)
                    {
                        SqlMapper.SetQueryCache(identity, info);
                    }
                }

                var func = tuple.Func;
                while (reader.Read())
                {
                    yield return(func(reader));
                }
                while (reader.NextResult())
                {
                }
                // happy path; close the reader cleanly - no
                // need for "Cancel" etc
                reader.Dispose();
                reader = null;

                command.OnCompleted();
            }
            finally
            {
                if (reader != null)
                {
                    if (!reader.IsClosed)
                    {
                        try { cmd.Cancel(); }
                        catch { /* don't spoil the existing exception */ }
                    }
                    reader.Dispose();
                }
                if (wasClosed)
                {
                    connection.Close();
                }
                if (cmd != null)
                {
                    cmd.Dispose();
                }
            }
        }
        public void AddParameters(IDbCommand command, SqlMapper.Identity identity)
        {
            var sqlCommand = (SqlCommand)command;

            sqlCommand.CommandType = CommandType.StoredProcedure;
            var items = new List <SqlDataRecord>();

            foreach (var param in _parameters)
            {
                var rec = new SqlDataRecord(
                    _programmeTypeMetaData,
                    _standardCodeMetaData,
                    _frameworkCodeMetaData,
                    _pathwayCodeMetaData,
                    _ukprnMetaData,
                    _learnRefNumberMetaData,
                    _transferSenderAccountIdMetaData,
                    _employerAccountIdMetaData,
                    _paymentStatusMetaData,
                    _negotiatedPriceMetaData,
                    _startDateMetaData,
                    _endDateMetaData,
                    _effectiveFromMetaData,
                    _effectiveToMetaData,
                    _ulnMetaData);

                if (param.ProgrammeType.HasValue)
                {
                    rec.SetInt32(0, param.ProgrammeType.Value);
                }
                if (param.StandardCode.HasValue)
                {
                    rec.SetInt64(1, param.StandardCode.Value);
                }
                if (param.FrameworkCode.HasValue)
                {
                    rec.SetInt32(2, param.FrameworkCode.Value);
                }
                if (param.PathwayCode.HasValue)
                {
                    rec.SetInt32(3, param.PathwayCode.Value);
                }
                rec.SetInt64(4, param.Ukprn);
                rec.SetString(5, param.LearnerReferenceNumber);
                if (param.TransferSenderAccountId.HasValue)
                {
                    rec.SetInt64(6, param.TransferSenderAccountId.Value);
                }
                rec.SetInt64(7, param.EmployerAccountId);
                rec.SetInt32(8, param.PaymentStatus);
                rec.SetDecimal(9, param.NegotiatedPrice);
                rec.SetDateTime(10, param.StartDate);
                rec.SetDateTime(11, param.EndDate);
                rec.SetDateTime(12, param.EffectiveFrom);
                if (param.EffectiveTo != null)
                {
                    rec.SetDateTime(13, param.EffectiveTo.Value);
                }
                rec.SetInt64(14, param.Uln);

                items.Add(rec);
            }

            var p = sqlCommand.Parameters.Add("@commitments", SqlDbType.Structured);

            p.Direction = ParameterDirection.Input;
            p.TypeName  = "[dbo].[CommitmentType]";
            p.Value     = items;
        }
Ejemplo n.º 4
0
 void SqlMapper.IDynamicParameters.AddParameters(IDbCommand command, SqlMapper.Identity identity)
 {
     AddParameters(command, identity);
 }
 public void AddParameters(IDbCommand command, SqlMapper.Identity identity)
 {
     throw new System.NotImplementedException();
 }