Пример #1
0
        public IntPtr MarshalManagedToNative(Charset charset, Descriptor descriptor)
        {
            // Set up XSQLDA structure
            XSQLDA xsqlda = new XSQLDA();

            xsqlda.version = descriptor.Version;
            xsqlda.sqln    = descriptor.Count;
            xsqlda.sqld    = descriptor.ActualCount;

            XSQLVAR[] xsqlvar = new XSQLVAR[descriptor.Count];

            for (int i = 0; i < xsqlvar.Length; i++)
            {
                // Create a	new	XSQLVAR	structure and fill it
                xsqlvar[i] = new XSQLVAR();

                xsqlvar[i].sqltype    = descriptor[i].DataType;
                xsqlvar[i].sqlscale   = descriptor[i].NumericScale;
                xsqlvar[i].sqlsubtype = descriptor[i].SubType;
                xsqlvar[i].sqllen     = descriptor[i].Length;

                // Create a	new	pointer	for	the	xsqlvar	data
                byte[] buffer = this.GetBytes(descriptor[i]);
                if (buffer.Length > 0)
                {
                    xsqlvar[i].sqldata = Marshal.AllocHGlobal(buffer.Length);
                    Marshal.Copy(buffer, 0, xsqlvar[i].sqldata, buffer.Length);
                }

                // Create a	new	pointer	for	the	sqlind value
                xsqlvar[i].sqlind = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Int16)));
                Marshal.WriteInt16(xsqlvar[i].sqlind, descriptor[i].NullFlag);

                // Name
                xsqlvar[i].sqlname        = this.GetStringBuffer(charset, descriptor[i].Name);
                xsqlvar[i].sqlname_length = (short)xsqlvar[i].sqlname.Length;

                // Relation	Name
                xsqlvar[i].relname        = this.GetStringBuffer(charset, descriptor[i].Relation);
                xsqlvar[i].relname_length = (short)xsqlvar[i].relname.Length;

                // Owner name
                xsqlvar[i].ownername        = this.GetStringBuffer(charset, descriptor[i].Owner);
                xsqlvar[i].ownername_length = (short)xsqlvar[i].ownername.Length;

                // Alias name
                xsqlvar[i].aliasname        = this.GetStringBuffer(charset, descriptor[i].Alias);
                xsqlvar[i].aliasname_length = (short)xsqlvar[i].aliasname.Length;
            }

            return(this.MarshalManagedToNative(xsqlda, xsqlvar));
        }
Пример #2
0
        public Descriptor MarshalNativeToManaged(Charset charset, IntPtr pNativeData)
        {
            // Obtain XSQLDA information
            XSQLDA xsqlda = new     XSQLDA();

            xsqlda = (XSQLDA)Marshal.PtrToStructure(pNativeData, typeof(XSQLDA));

            // Create a	new	Descriptor
            Descriptor descriptor = new Descriptor(xsqlda.sqln);

            descriptor.ActualCount = xsqlda.sqld;

            // Obtain XSQLVAR members information
            XSQLVAR[] xsqlvar = new XSQLVAR[xsqlda.sqln];

            for (int i = 0; i < xsqlvar.Length; i++)
            {
                xsqlvar[i] = (XSQLVAR)Marshal.PtrToStructure(
                    this.GetIntPtr(pNativeData, this.ComputeLength(i)), typeof(XSQLVAR));

                // Map XSQLVAR information to Descriptor
                descriptor[i].DataType     = xsqlvar[i].sqltype;
                descriptor[i].NumericScale = xsqlvar[i].sqlscale;
                descriptor[i].SubType      = xsqlvar[i].sqlsubtype;
                descriptor[i].Length       = xsqlvar[i].sqllen;

                // Decode sqlind value
                if (xsqlvar[i].sqlind == IntPtr.Zero)
                {
                    descriptor[i].NullFlag = 0;
                }
                else
                {
                    descriptor[i].NullFlag = Marshal.ReadInt16(xsqlvar[i].sqlind);
                }

                // Set value
                if (descriptor[i].NullFlag != -1)
                {
                    descriptor[i].SetValue(this.GetBytes(xsqlvar[i]));
                }

                descriptor[i].Name     = this.GetString(charset, xsqlvar[i].sqlname);
                descriptor[i].Relation = this.GetString(charset, xsqlvar[i].relname);
                descriptor[i].Owner    = this.GetString(charset, xsqlvar[i].ownername);
                descriptor[i].Alias    = this.GetString(charset, xsqlvar[i].aliasname);
            }

            return(descriptor);
        }
Пример #3
0
        private byte[] GetBytes(XSQLVAR xsqlvar)
        {
            if (xsqlvar.sqllen == 0 || xsqlvar.sqldata == IntPtr.Zero)
            {
                return(null);
            }

            byte[] buffer = new     byte[xsqlvar.sqllen];

            switch (xsqlvar.sqltype & ~1)
            {
            case IscCodes.SQL_VARYING:
                short length = Marshal.ReadInt16(xsqlvar.sqldata);

                buffer = new byte[length];

                IntPtr tmp = this.GetIntPtr(xsqlvar.sqldata, 2);

                Marshal.Copy(tmp, buffer, 0, buffer.Length);

                return(buffer);

            case IscCodes.SQL_TEXT:
            case IscCodes.SQL_SHORT:
            case IscCodes.SQL_LONG:
            case IscCodes.SQL_FLOAT:
            case IscCodes.SQL_DOUBLE:
            case IscCodes.SQL_D_FLOAT:
            case IscCodes.SQL_QUAD:
            case IscCodes.SQL_INT64:
            case IscCodes.SQL_BLOB:
            case IscCodes.SQL_ARRAY:
            case IscCodes.SQL_TIMESTAMP:
            case IscCodes.SQL_TYPE_TIME:
            case IscCodes.SQL_TYPE_DATE:
                Marshal.Copy(xsqlvar.sqldata, buffer, 0, buffer.Length);

                return(buffer);

            default:
                throw new NotSupportedException("Unknown data type");
            }
        }
Пример #4
0
        public void     CleanUpNativeData(ref IntPtr pNativeData)
        {
            if (pNativeData != IntPtr.Zero)
            {
                // Obtain XSQLDA information
                XSQLDA xsqlda = new     XSQLDA();

                xsqlda = (XSQLDA)Marshal.PtrToStructure(pNativeData, typeof(XSQLDA));

                // Destroy XSQLDA structure
                Marshal.DestroyStructure(pNativeData, typeof(XSQLDA));

                // Destroy XSQLVAR structures
                for (int i = 0; i < xsqlda.sqln; i++)
                {
                    // Free	sqldata	and	sqlind pointers	if needed
                    XSQLVAR sqlvar = (XSQLVAR)Marshal.PtrToStructure(
                        this.GetIntPtr(pNativeData, this.ComputeLength(i)), typeof(XSQLVAR));

                    if (sqlvar.sqldata != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(sqlvar.sqldata);
                        sqlvar.sqldata = IntPtr.Zero;
                    }
                    if (sqlvar.sqlind != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(sqlvar.sqlind);
                        sqlvar.sqlind = IntPtr.Zero;
                    }

                    Marshal.DestroyStructure(
                        this.GetIntPtr(pNativeData, this.ComputeLength(i)), typeof(XSQLVAR));
                }

                // Free	pointer	memory
                Marshal.FreeHGlobal(pNativeData);

                pNativeData = IntPtr.Zero;
            }
        }
		public IntPtr MarshalManagedToNative(Charset charset, Descriptor descriptor)
		{
			// Set up XSQLDA structure
			XSQLDA xsqlda = new XSQLDA();

			xsqlda.version = descriptor.Version;
			xsqlda.sqln	 = descriptor.Count;
			xsqlda.sqld	 = descriptor.ActualCount;
			
			XSQLVAR[] xsqlvar = new	XSQLVAR[descriptor.Count];

			for	(int i = 0;	i <	xsqlvar.Length;	i++)
			{
				// Create a	new	XSQLVAR	structure and fill it
				xsqlvar[i] = new XSQLVAR();

				xsqlvar[i].sqltype	 = descriptor[i].DataType;
				xsqlvar[i].sqlscale	 = descriptor[i].NumericScale;
				xsqlvar[i].sqlsubtype = descriptor[i].SubType;
				xsqlvar[i].sqllen	 = descriptor[i].Length;

				// Create a	new	pointer	for	the	xsqlvar	data
				byte[] buffer = this.GetBytes(descriptor[i]);
				if (buffer.Length > 0)
				{
					xsqlvar[i].sqldata = Marshal.AllocHGlobal(buffer.Length);
					Marshal.Copy(buffer, 0,	xsqlvar[i].sqldata,	buffer.Length);
				}

				// Create a	new	pointer	for	the	sqlind value
				xsqlvar[i].sqlind = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Int16)));
				Marshal.WriteInt16(xsqlvar[i].sqlind, descriptor[i].NullFlag);				  

				// Name
				xsqlvar[i].sqlname		 = this.GetStringBuffer(charset,	descriptor[i].Name);
				xsqlvar[i].sqlname_length = (short)xsqlvar[i].sqlname.Length;

				// Relation	Name
				xsqlvar[i].relname		 = this.GetStringBuffer(charset,	descriptor[i].Relation);
				xsqlvar[i].relname_length = (short)xsqlvar[i].relname.Length;

				// Owner name
				xsqlvar[i].ownername	 = this.GetStringBuffer(charset,	descriptor[i].Owner);
				xsqlvar[i].ownername_length = (short)xsqlvar[i].ownername.Length;

				// Alias name
				xsqlvar[i].aliasname	 = this.GetStringBuffer(charset,	descriptor[i].Alias);
				xsqlvar[i].aliasname_length = (short)xsqlvar[i].aliasname.Length;
			}

			return this.MarshalManagedToNative(xsqlda, xsqlvar);
		}
		private	byte[] GetBytes(XSQLVAR	xsqlvar)
		{
			if (xsqlvar.sqllen == 0	|| xsqlvar.sqldata == IntPtr.Zero)
			{
				return null;
			}

			byte[] buffer = new	byte[xsqlvar.sqllen];

			switch (xsqlvar.sqltype	& ~1)
			{
				case IscCodes.SQL_VARYING:
					short length = Marshal.ReadInt16(xsqlvar.sqldata);

					buffer = new byte[length];

					IntPtr tmp = this.GetIntPtr(xsqlvar.sqldata, 2);

					Marshal.Copy(tmp, buffer, 0, buffer.Length);

					return buffer;

				case IscCodes.SQL_TEXT:	
				case IscCodes.SQL_SHORT:
				case IscCodes.SQL_LONG:
				case IscCodes.SQL_FLOAT:
				case IscCodes.SQL_DOUBLE:
				case IscCodes.SQL_D_FLOAT:
				case IscCodes.SQL_QUAD:
				case IscCodes.SQL_INT64:
				case IscCodes.SQL_BLOB:
				case IscCodes.SQL_ARRAY:	
				case IscCodes.SQL_TIMESTAMP:
				case IscCodes.SQL_TYPE_TIME:
				case IscCodes.SQL_TYPE_DATE:
					Marshal.Copy(xsqlvar.sqldata, buffer, 0, buffer.Length);

					return buffer;

				default:
					throw new NotSupportedException("Unknown data type");
			}
		}
		public Descriptor MarshalNativeToManaged(Charset charset, IntPtr pNativeData)
		{
			// Obtain XSQLDA information
			XSQLDA xsqlda = new	XSQLDA();
			
			xsqlda = (XSQLDA)Marshal.PtrToStructure(pNativeData, typeof(XSQLDA));

			// Create a	new	Descriptor
			Descriptor descriptor = new Descriptor(xsqlda.sqln);
			descriptor.ActualCount = xsqlda.sqld;
			
			// Obtain XSQLVAR members information
			XSQLVAR[] xsqlvar = new	XSQLVAR[xsqlda.sqln];
			
			for	(int i = 0;	i <	xsqlvar.Length;	i++)
			{
				xsqlvar[i] = (XSQLVAR)Marshal.PtrToStructure(
					this.GetIntPtr(pNativeData,	this.ComputeLength(i)),	typeof(XSQLVAR));

				// Map XSQLVAR information to Descriptor
				descriptor[i].DataType	 = xsqlvar[i].sqltype;
				descriptor[i].NumericScale = xsqlvar[i].sqlscale;
				descriptor[i].SubType	 = xsqlvar[i].sqlsubtype;
				descriptor[i].Length	 = xsqlvar[i].sqllen;

				// Decode sqlind value
				if (xsqlvar[i].sqlind == IntPtr.Zero)
				{
					descriptor[i].NullFlag = 0;
				}
				else
				{
					descriptor[i].NullFlag = Marshal.ReadInt16(xsqlvar[i].sqlind);
				}
				
				// Set value
				if (descriptor[i].NullFlag != -1)
				{
					descriptor[i].SetValue(this.GetBytes(xsqlvar[i]));
				}
				
				descriptor[i].Name	 = this.GetString(charset, xsqlvar[i].sqlname);
				descriptor[i].Relation = this.GetString(charset, xsqlvar[i].relname);
				descriptor[i].Owner	 = this.GetString(charset, xsqlvar[i].ownername);
				descriptor[i].Alias	 = this.GetString(charset, xsqlvar[i].aliasname);
			}

			return descriptor;
		}
		public IntPtr MarshalManagedToNative(XSQLDA	xsqlda,	XSQLVAR[] xsqlvar)
		{
			int		size = this.ComputeLength(xsqlda.sqln);
			IntPtr	ptr	 = Marshal.AllocHGlobal(size);

			Marshal.StructureToPtr(xsqlda, ptr,	true);

			for	(int i = 0;	i <	xsqlvar.Length;	i++)
			{
				int	offset = this.ComputeLength(i);
				Marshal.StructureToPtr(xsqlvar[i], this.GetIntPtr(ptr, offset),	true);
			}

			return ptr;
		}