File: XsqldaMarshaler.cs

package info (click to toggle)
mono 1.2.2.1-1etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 142,720 kB
  • ctags: 256,408
  • sloc: cs: 1,495,736; ansic: 249,442; sh: 18,327; xml: 12,463; makefile: 5,046; perl: 1,248; asm: 635; yacc: 285; sql: 7
file content (433 lines) | stat: -rw-r--r-- 11,323 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
 *	Firebird ADO.NET Data provider for .NET and Mono 
 * 
 *	   The contents of this file are subject to the Initial 
 *	   Developer's Public License Version 1.0 (the "License"); 
 *	   you may not use this file except in compliance with the 
 *	   License. You may obtain a copy of the License at 
 *	   http://www.firebirdsql.org/index.php?op=doc&id=idpl
 *
 *	   Software distributed under the License is distributed on 
 *	   an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either 
 *	   express or implied. See the License for the specific 
 *	   language governing rights and limitations under the License.
 * 
 *	Copyright (c) 2002, 2005 Carlos Guzman Alvarez
 *	All Rights Reserved.
 */

using System;
using System.Runtime.InteropServices;
using System.Text;

using FirebirdSql.Data.Common;

namespace FirebirdSql.Data.Embedded
{
	internal sealed	class XsqldaMarshaler
	{
		#region Static Fields

		private	static XsqldaMarshaler instance;

		#endregion

		#region Constructors

		private	XsqldaMarshaler()
		{
		}

		#endregion

		#region Methods

		public static XsqldaMarshaler GetInstance()
		{
			if (XsqldaMarshaler.instance ==	null)
			{
				XsqldaMarshaler.instance = new XsqldaMarshaler();
			}

			return XsqldaMarshaler.instance;
		}

		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);
		}

		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;
		}

		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;
		}

		#endregion

		#region Private	methods

		private	IntPtr GetIntPtr(IntPtr	ptr, int offset)
		{
			return (IntPtr)(ptr.ToInt32() +	offset);
		}

		private	int	ComputeLength(int n)
		{
			return (Marshal.SizeOf(typeof(XSQLDA)) + n * Marshal.SizeOf(typeof(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");
			}
		}

		private	byte[] GetBytes(DbField	field)
		{
			if (field.DbValue.IsDBNull())
			{
				int	length = field.Length;
				
				if (field.SqlType == IscCodes.SQL_VARYING)
				{
					// Add two bytes more for store	value length
					length += 2;
				}

				return new byte[length];
			}

			switch (field.DbDataType)
			{
				case DbDataType.Char:
				{
					string svalue = field.DbValue.GetString();

					if ((field.Length %	field.Charset.BytesPerCharacter) == 0 &&
						svalue.Length >	field.CharCount)
					{	 
						throw new IscException(335544321);	 
					}

					byte[] buffer = new	byte[field.Length];
					for	(int i = 0;	i <	buffer.Length; i++)
					{
						buffer[i] = 32;
					}

					byte[] bytes = field.Charset.GetBytes(svalue);

					Buffer.BlockCopy(bytes, 0, buffer, 0, bytes.Length);

					return buffer;
				}
				
				case DbDataType.VarChar:
				{
					string svalue = field.Value.ToString();

					if ((field.Length %	field.Charset.BytesPerCharacter) == 0 &&
						svalue.Length >	field.CharCount)
					{	 
						throw new IscException(335544321);	 
					}

					byte[] sbuffer = field.Charset.GetBytes(svalue);

					byte[] buffer = new	byte[field.Length +	2];

					// Copy	length
					Buffer.BlockCopy(
						BitConverter.GetBytes((short)sbuffer.Length), 
					 0, buffer, 0, 2);
					
					// Copy	string value
					Buffer.BlockCopy(sbuffer, 0, buffer, 2,	sbuffer.Length);

					return buffer;
				}

				case DbDataType.Numeric:
				case DbDataType.Decimal:
					return this.GetNumericBytes(field);

				case DbDataType.SmallInt:
					return BitConverter.GetBytes(field.DbValue.GetInt16());

				case DbDataType.Integer:
					return BitConverter.GetBytes(field.DbValue.GetInt32());

				case DbDataType.Array:
				case DbDataType.Binary:
				case DbDataType.Text:
				case DbDataType.BigInt:
					return BitConverter.GetBytes(field.DbValue.GetInt64());

				case DbDataType.Float:
					return BitConverter.GetBytes(field.DbValue.GetFloat());
									
				case DbDataType.Double:
					return BitConverter.GetBytes(field.DbValue.GetDouble());

				case DbDataType.Date:
					return BitConverter.GetBytes(
						TypeEncoder.EncodeDate(field.DbValue.GetDateTime()));
				
				case DbDataType.Time:
					return BitConverter.GetBytes(
						TypeEncoder.EncodeTime(field.DbValue.GetDateTime()));
				
				case DbDataType.TimeStamp:
					byte[] date = BitConverter.GetBytes(
						TypeEncoder.EncodeDate(field.DbValue.GetDateTime()));
					
					byte[] time = BitConverter.GetBytes(
						TypeEncoder.EncodeTime(field.DbValue.GetDateTime()));
					
					byte[] result = new	byte[8];

					Buffer.BlockCopy(date, 0, result, 0, date.Length);
					Buffer.BlockCopy(time, 0, result, 4, time.Length);

					return result;

				case DbDataType.Guid:
					return field.DbValue.GetGuid().ToByteArray();

				default:
					throw new NotSupportedException("Unknown data type");
			}
		}

		private	byte[] GetNumericBytes(DbField field)
		{
			decimal	value = field.DbValue.GetDecimal();
			object	numeric = TypeEncoder.EncodeDecimal(value, field.NumericScale, field.DataType);

			switch (field.SqlType)
			{
				case IscCodes.SQL_SHORT:
					return BitConverter.GetBytes((short)numeric);

				case IscCodes.SQL_LONG:
					return BitConverter.GetBytes((int)numeric);

				case IscCodes.SQL_INT64:
				case IscCodes.SQL_QUAD:
					return BitConverter.GetBytes((long)numeric);

				case IscCodes.SQL_DOUBLE:
					return BitConverter.GetBytes(field.DbValue.GetDouble());

				default:
					return null;
			}
		}

		private	byte[] GetStringBuffer(Charset charset,	string value)
		{
			byte[] buffer = new	byte[32];
			
			charset.GetBytes(value, 0, value.Length, buffer, 0);

			return buffer;
		}

		private	string GetString(Charset charset, byte[] buffer)
		{
			string value = charset.GetString(buffer);

			return value.Replace('\0', ' ').Trim();
		}

		#endregion
	}
}