Esempio n. 1
0
        public int WriteInt8(long value)
        {
            if (mRowInfo == null)
            {
                throw new InvalidOperationException("PqsqlCopyFrom.Start must be called before we can write data");
            }

            long begin = LengthCheckReset();

#if CODECONTRACTS
            Contract.Assume(mRowInfo != null);
            Contract.Assume(mPos >= 0 && mPos < mRowInfo.Length);
#endif

            PqsqlColInfo ci = mRowInfo[mPos];
            if (ci == null)
            {
                throw new PqsqlException("PqsqlCopyFrom.Start could not setup column information for column " + mPos);
            }

            PqsqlDbType oid = ci.Oid;
            uint        destination_length;

            // check destination row datatype
            switch (oid)
            {
            case PqsqlDbType.Int8:
                PqsqlBinaryFormat.pqbf_set_int8(mExpBuf, value);
                destination_length = 8;
                break;

            case PqsqlDbType.Int4:
                // dangerous, but let's try it
                PqsqlBinaryFormat.pqbf_set_int4(mExpBuf, (int)value);
                destination_length = 4;
                break;

            case PqsqlDbType.Int2:
                // dangerous, but let's try it
                PqsqlBinaryFormat.pqbf_set_int2(mExpBuf, (short)value);
                destination_length = 2;
                break;

            default:
                throw new PqsqlException("Column " + ci.ColumnName + ": cannot write " + typeof(long) + " to column of type " + oid);
            }

            unsafe
            {
                sbyte *val = PqsqlBinaryFormat.pqbf_get_bufval(mExpBuf) + begin;
                return(PutColumn(val, destination_length));
            }
        }
Esempio n. 2
0
        public int WriteDate(DateTime value)
        {
            if (mRowInfo == null)
            {
                throw new InvalidOperationException($"{nameof(PqsqlCopyFrom)}.{nameof(Start)} must be called before we can write data");
            }

            long begin = LengthCheckReset();

#if CODECONTRACTS
            Contract.Assume(mRowInfo != null);
            Contract.Assume(mPos >= 0 && mPos < mRowInfo.Length);
#endif

            PqsqlColInfo ci = mRowInfo[mPos];
            if (ci == null)
            {
                throw new PqsqlException($"{nameof(PqsqlCopyFrom)}.{nameof(Start)} could not setup column information for column {mPos}");
            }

            PqsqlDbType oid = ci.Oid;

            if (oid != PqsqlDbType.Date)
            {
                throw new PqsqlException($"{nameof(PqsqlCopyFrom)}.{nameof(WriteDate)}: cannot write {PqsqlDbType.Date} into column {mPos} of type {oid}");
            }

            int year;
            int month;
            int day;
            PqsqlBinaryFormat.GetDate(value, out year, out month, out day);

            PqsqlBinaryFormat.pqbf_set_date(mExpBuf, year, month, day);
            unsafe
            {
                sbyte *val = PqsqlBinaryFormat.pqbf_get_bufval(mExpBuf) + begin;
                return(PutColumn(val, 4));
            }
        }