Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data / System / Data / OleDb / OleDbEnumerator.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="OleDbEnumerator.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 //------------------------------------------------------------------------------
8
9 namespace System.Data.OleDb {
10
11     using System;
12     using System.ComponentModel;
13     using System.Data.Common;
14     using System.Globalization;
15     using System.Security;
16     using System.Security.Permissions;
17
18     public sealed class OleDbEnumerator  {
19
20         public OleDbEnumerator() {
21         }
22
23         public DataTable GetElements() {
24             OleDbConnection.ExecutePermission.Demand();
25              
26             DataTable dataTable = new DataTable("MSDAENUM"); // WebData 112482
27             dataTable.Locale = CultureInfo.InvariantCulture;
28             OleDbDataReader dataReader = GetRootEnumerator();
29             OleDbDataAdapter.FillDataTable(dataReader, dataTable);
30             return dataTable;
31         }
32
33         static public OleDbDataReader GetEnumerator(Type type) {
34             OleDbConnection.ExecutePermission.Demand();
35
36             return GetEnumeratorFromType(type);
37         }
38         
39         static internal OleDbDataReader GetEnumeratorFromType(Type type) { // WebData 99005
40             // will demand security appropriately
41             object value = Activator.CreateInstance(type, System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.Instance, null, null, CultureInfo.InvariantCulture, null);
42             return GetEnumeratorReader(value);
43         }
44         
45         static private OleDbDataReader GetEnumeratorReader(object value) {
46             NativeMethods.ISourcesRowset srcrowset = null;
47
48             try {
49                 srcrowset = (NativeMethods.ISourcesRowset) value;
50             }
51             catch(InvalidCastException) {
52                 throw ODB.ISourcesRowsetNotSupported();
53             }
54             if (null == srcrowset) {
55                 throw ODB.ISourcesRowsetNotSupported();
56             }
57             value = null; // still held by ISourcesRowset, reused for IRowset
58
59             int propCount = 0;
60             IntPtr propSets = ADP.PtrZero;
61
62             Bid.Trace("<oledb.ISourcesRowset.GetSourcesRowset|API|OLEDB> IID_IRowset\n");
63             OleDbHResult hr = srcrowset.GetSourcesRowset(ADP.PtrZero, ODB.IID_IRowset, propCount, propSets, out value);
64             Bid.Trace("<oledb.ISourcesRowset.GetSourcesRowset|API|OLEDB|RET> %08X{HRESULT}\n", hr);
65
66             Exception f = OleDbConnection.ProcessResults(hr, null, null);
67             if (null != f) {
68                 throw f;
69             }
70
71             OleDbDataReader dataReader = new OleDbDataReader(null, null, 0, CommandBehavior.Default);
72             dataReader.InitializeIRowset(value, ChapterHandle.DB_NULL_HCHAPTER, ADP.RecordsUnaffected);
73             dataReader.BuildMetaInfo();
74             dataReader.HasRowsRead();
75             return dataReader;
76         }
77         
78         static public OleDbDataReader GetRootEnumerator() {
79             OleDbConnection.ExecutePermission.Demand();
80
81             IntPtr hscp;
82             Bid.ScopeEnter(out hscp, "<oledb.OleDbEnumerator.GetRootEnumerator|API>\n");
83             try {
84                 //readonly Guid CLSID_MSDAENUM = new Guid(0xc8b522d0,0x5cf3,0x11ce,0xad,0xe5,0x00,0xaa,0x00,0x44,0x77,0x3d);
85                 //Type msdaenum = Type.GetTypeFromCLSID(CLSID_MSDAENUM, true);
86                 const string PROGID_MSDAENUM = "MSDAENUM";
87                 Type msdaenum = Type.GetTypeFromProgID(PROGID_MSDAENUM, true);
88                 return GetEnumeratorFromType(msdaenum);
89             }
90             finally {
91                 Bid.ScopeLeave(ref hscp);
92             }
93         }
94     }
95 }
96