Exemple #1
0
 //
 // Summary:
 //     Removes all System.Data.Common.DbParameter values from the System.Data.Common.DbParameterCollection.
 public void Clear()
 {
     if (mPqPB != IntPtr.Zero)
     {
         PqsqlBinaryFormat.pqpb_reset(mPqPB);
     }
 }
Exemple #2
0
 private void Init()
 {
     mPqPB = PqsqlBinaryFormat.pqpb_create();             // create pqparam_buffer
     if (mPqPB == null)
     {
         throw new PqsqlException("Cannot create buffer for parameter collection");
     }
 }
Exemple #3
0
        // sets val as string with Oid oid (PqsqlDbType.BPChar, PqsqlDbType.Text, PqsqlDbType.Varchar, PqsqlDbType.Name, PqsqlDbType.Char)
        // into pqparam_buffer pb
        internal static unsafe void SetText(IntPtr pb, object val, PqsqlDbType oid)
        {
#if !WIN32
            PqsqlUTF8Statement.AddText(pb, (string)val, (uint)oid);
#else
            fixed(char *t = (string)val)
            {
                PqsqlBinaryFormat.pqbf_add_unicode_text(pb, t, (uint)oid);
            }
#endif
        }
Exemple #4
0
        public int WriteBool(bool value)
        {
            long begin = LengthCheckReset();

            PqsqlBinaryFormat.pqbf_set_bool(mExpBuf, value ? 1 : 0);
            unsafe
            {
                sbyte *val = PqsqlBinaryFormat.pqbf_get_bufval(mExpBuf) + begin;
                return(PutColumn(val, 1));
            }
        }
Exemple #5
0
        // sets val as DateTime with Oid oid (PqsqlDbType.Timestamp, PqsqlDbType.TimestampTZ) into pqparam_buffer pb
        internal static void SetTimestamp(IntPtr pb, object val, PqsqlDbType oid)
        {
            DateTime dt = (DateTime)val;

            long sec;
            int  usec;

            PqsqlBinaryFormat.GetTimestamp(dt, out sec, out usec);

            PqsqlBinaryFormat.pqbf_add_timestamp(pb, sec, usec, (uint)oid);
        }
Exemple #6
0
        // sets val as TimeSpan into pqparam_buffer pb
        internal static void SetInterval(IntPtr pb, object val, PqsqlDbType oid)
        {
            TimeSpan ts = (TimeSpan)val;

            long offset;
            int  day;
            int  month;

            PqsqlBinaryFormat.GetInterval(ts, out offset, out day, out month);

            PqsqlBinaryFormat.pqbf_add_interval(pb, offset, day, month);
        }
Exemple #7
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));
            }
        }
Exemple #8
0
        // sets val as DateTime with Oid oid (PqsqlDbType.Time, PqsqlDbType.TimeTZ) into pqparam_buffer pb
        internal static void SetDate(IntPtr pb, object val, PqsqlDbType oid)
        {
            DateTime dt = (DateTime)val;

            int year;
            int month;
            int day;

            PqsqlBinaryFormat.GetDate(dt, out year, out month, out day);

            PqsqlBinaryFormat.pqbf_add_date(pb, year, month, day);
        }
Exemple #9
0
        // adds o as DateTime array element into PQExpBuffer a
        internal static void SetTimestampArray(IntPtr a, object o)
        {
            DateTime dt = (DateTime)o;

            long sec;
            int  usec;

            PqsqlBinaryFormat.GetTimestamp(dt, out sec, out usec);

            PqsqlBinaryFormat.pqbf_set_array_itemlength(a, 8);
            PqsqlBinaryFormat.pqbf_set_timestamp(a, sec, usec);
        }
Exemple #10
0
        // sets val as TimeSpan with Oid oid PqsqlDbType.Time into pqparam_buffer pb
        internal static void SetTime(IntPtr pb, object val, PqsqlDbType oid)
        {
            TimeSpan ts = (TimeSpan)val;

            int hour;
            int min;
            int sec;
            int fsec;

            PqsqlBinaryFormat.GetTime(ts, out hour, out min, out sec, out fsec);

            PqsqlBinaryFormat.pqbf_add_time(pb, hour, min, sec, fsec);
        }
Exemple #11
0
        public int WriteFloat8(double value)
        {
            long begin = LengthCheckReset();

            // TODO try to infer destination datatype
            PqsqlBinaryFormat.pqbf_set_float8(mExpBuf, value);

            unsafe
            {
                sbyte *val = PqsqlBinaryFormat.pqbf_get_bufval(mExpBuf) + begin;
                return(PutColumn(val, 8));
            }
        }
Exemple #12
0
        public override void Start()
        {
            base.Start();

            if (mColBuf != IntPtr.Zero)
            {
                PqsqlBinaryFormat.pqcb_free(mColBuf);
            }

            IntPtr conn = mConn.PGConnection;

            mColBuf = PqsqlBinaryFormat.pqcb_create(conn, mColumns);
        }
Exemple #13
0
        // adds o as DateTime array element into PQExpBuffer a
        internal static void SetDateArray(IntPtr a, object o)
        {
            DateTime dt = (DateTime)o;

            int year;
            int month;
            int day;

            PqsqlBinaryFormat.GetDate(dt, out year, out month, out day);

            PqsqlBinaryFormat.pqbf_set_array_itemlength(a, 4);
            PqsqlBinaryFormat.pqbf_set_date(a, year, month, day);
        }
        internal static unsafe void SetText(IntPtr p, string text)
        {
            var t = StringToCoTaskMemUTF8(text);

            try
            {
                PqsqlBinaryFormat.pqbf_set_text(p, (sbyte *)t.ToPointer());
            }
            finally
            {
                ZeroFreeCoTaskMemUTF8(t);
            }
        }
        internal static unsafe void AddText(IntPtr pb, string text, uint oid)
        {
            var t = StringToCoTaskMemUTF8(text);

            try
            {
                PqsqlBinaryFormat.pqbf_add_text(pb, (sbyte *)t.ToPointer(), oid);
            }
            finally
            {
                ZeroFreeCoTaskMemUTF8(t);
            }
        }
Exemple #16
0
        // adds o as TimeSpan array element into PQExpBuffer a
        internal static void SetIntervalArray(IntPtr a, object o)
        {
            TimeSpan ts = (TimeSpan)o;

            long offset;
            int  day;
            int  month;

            PqsqlBinaryFormat.GetInterval(ts, out offset, out day, out month);

            PqsqlBinaryFormat.pqbf_set_array_itemlength(a, 16);
            PqsqlBinaryFormat.pqbf_set_interval(a, offset, day, month);
        }
Exemple #17
0
        // adds o as TimeSpan array element into PQExpBuffer a
        internal static void SetTimeArray(IntPtr a, object o)
        {
            TimeSpan ts = (TimeSpan)o;

            int hour;
            int min;
            int sec;
            int fsec;

            PqsqlBinaryFormat.GetTime(ts, out hour, out min, out sec, out fsec);

            PqsqlBinaryFormat.pqbf_set_array_itemlength(a, 8);
            PqsqlBinaryFormat.pqbf_set_time(a, hour, min, sec, fsec);
        }
Exemple #18
0
        // returns current position of mExpBuf
        private long LengthCheckReset()
        {
            long len = PqsqlBinaryFormat.pqbf_get_buflen(mExpBuf);

            if (len > 4096)
            {
                // if we exceed 4k write-boundary, we reset the buffer and
                // start to write from the beginning again
                PqsqlWrapper.resetPQExpBuffer(mExpBuf);
                len = 0;
            }

            return(len);
        }
Exemple #19
0
        // adds o as double array element to PQExpBuffer a
        internal static void SetNumericArray(IntPtr a, object o)
        {
            double d = Convert.ToDouble(o, CultureInfo.InvariantCulture);

            long len0 = PqsqlBinaryFormat.pqbf_get_buflen(a);             // get start position

            PqsqlBinaryFormat.pqbf_set_array_itemlength(a, -2);           // first set an invalid item length
            PqsqlBinaryFormat.pqbf_set_numeric(a, d);                     // encode numeric value (variable length)

            int len = (int)(PqsqlBinaryFormat.pqbf_get_buflen(a) - len0); // get new buffer length

            // update array item length == len - 4 bytes
            PqsqlBinaryFormat.pqbf_update_array_itemlength(a, -len, len - 4);
        }
Exemple #20
0
        public override void Close()
        {
            if (mColBuf != IntPtr.Zero)
            {
                PqsqlBinaryFormat.pqcb_free(mColBuf);
                mColBuf = IntPtr.Zero;
            }

            if (mExpBuf != IntPtr.Zero)
            {
                PqsqlWrapper.destroyPQExpBuffer(mExpBuf);
                mExpBuf = IntPtr.Zero;
            }
        }
Exemple #21
0
        public int WriteTimestamp(DateTime value)
        {
            long begin = LengthCheckReset();

            long sec;
            int  usec;

            PqsqlBinaryFormat.GetTimestamp(value, out sec, out usec);

            PqsqlBinaryFormat.pqbf_set_timestamp(mExpBuf, sec, usec);
            unsafe
            {
                sbyte *val = PqsqlBinaryFormat.pqbf_get_bufval(mExpBuf) + begin;
                return(PutColumn(val, 8));
            }
        }
        // converts null-terminated byte* to string
        internal static unsafe string CreateStringFromUTF8(IntPtr p)
        {
            if (p == IntPtr.Zero)
            {
                return(null);
            }

#if !WIN32
            return(PtrToStringUTF8(p));
#else
            int    dummy = 0;
            IntPtr utp   = PqsqlBinaryFormat.pqbf_get_unicode_text(p, &dummy);

            return(Marshal.PtrToStringUni(utp));
#endif
        }
Exemple #23
0
        public int WriteNumeric(decimal value)
        {
            long begin = LengthCheckReset();

            // TODO try to infer destination datatype
            PqsqlBinaryFormat.pqbf_set_numeric(mExpBuf, decimal.ToDouble(value));
            long end = PqsqlBinaryFormat.pqbf_get_buflen(mExpBuf);

            long len = end - begin;

            unsafe
            {
                sbyte *val = PqsqlBinaryFormat.pqbf_get_bufval(mExpBuf) + begin;
                return(PutColumn(val, (uint)len));
            }
        }
Exemple #24
0
        public int WriteInterval(TimeSpan value)
        {
            long begin = LengthCheckReset();

            long offset;
            int  day;
            int  month;

            PqsqlBinaryFormat.GetInterval(value, out offset, out day, out month);

            PqsqlBinaryFormat.pqbf_set_interval(mExpBuf, offset, day, month);
            unsafe
            {
                sbyte *val = PqsqlBinaryFormat.pqbf_get_bufval(mExpBuf) + begin;
                return(PutColumn(val, 16));
            }
        }
Exemple #25
0
        public int WriteTime(TimeSpan value)
        {
            long begin = LengthCheckReset();

            int hour;
            int min;
            int sec;
            int fsec;

            PqsqlBinaryFormat.GetTime(value, out hour, out min, out sec, out fsec);

            PqsqlBinaryFormat.pqbf_set_time(mExpBuf, hour, min, sec, fsec);
            unsafe
            {
                sbyte *val = PqsqlBinaryFormat.pqbf_get_bufval(mExpBuf) + begin;
                return(PutColumn(val, 8));
            }
        }
Exemple #26
0
        private void Dispose(bool disposing)
        {
            if (mDisposed)
            {
                return;
            }

            if (disposing)
            {
                // always dispose parameter buffer
            }

            if (mPqPB != IntPtr.Zero)
            {
                PqsqlBinaryFormat.pqpb_free(mPqPB);
                mPqPB = IntPtr.Zero;
            }
            mDisposed = true;
        }
Exemple #27
0
        public int GetQueryParams(out IntPtr ptyps, out IntPtr pvals, out IntPtr plens, out IntPtr pfrms)
        {
            int num_param = 0;

            ptyps = IntPtr.Zero;             // oid*
            pvals = IntPtr.Zero;             // char**
            plens = IntPtr.Zero;             // int*
            pfrms = IntPtr.Zero;             // int*

            if (mPqPB != IntPtr.Zero)
            {
                num_param = PqsqlBinaryFormat.pqpb_get_num(mPqPB);
                ptyps     = PqsqlBinaryFormat.pqpb_get_types(mPqPB);
                pvals     = PqsqlBinaryFormat.pqpb_get_vals(mPqPB);
                plens     = PqsqlBinaryFormat.pqpb_get_lens(mPqPB);
                pfrms     = PqsqlBinaryFormat.pqpb_get_frms(mPqPB);
            }

            return(num_param);
        }
Exemple #28
0
        public int WriteText(string value)
        {
            long begin = LengthCheckReset();

            unsafe
            {
#if !WIN32
                PqsqlUTF8Statement.SetText(mExpBuf, value);
#else
                fixed(char *sp = value)
                {
                    PqsqlBinaryFormat.pqbf_set_unicode_text(mExpBuf, sp);
                }
#endif
                long end = PqsqlBinaryFormat.pqbf_get_buflen(mExpBuf);
                long len = end - begin;

                sbyte *val = PqsqlBinaryFormat.pqbf_get_bufval(mExpBuf) + begin;
                return(PutColumn(val, (uint)len));
            }
        }
Exemple #29
0
        // NULL value: value = null, type_length > 0
        // otherwise, value points to beginning of binary encoding with type_length bytes
        private unsafe int PutColumn(sbyte *value, uint type_length)
        {
#if CODECONTRACTS
            Contract.Ensures(mPos < mColumns);
#endif

            int ret = PqsqlBinaryFormat.pqcb_put_col(mColBuf, value, type_length);

            if (ret < 1)
            {
                string err = mColBuf == IntPtr.Zero ? string.Empty : Error();
                throw new PqsqlException(err);
            }

            // rewind mPos in case we hit column boundary
            if (++mPos >= mColumns)
            {
                mPos = 0;
            }

            return((int)type_length);
        }
Exemple #30
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));
            }
        }