示例#1
0
        /// <summary>
        /// Returns DataSet based on specified options.
        /// </summary>
        public override DataSet GetDataSet(StiFileDataOptions options)
        {
            var jsonOptions = options as StiJsonOptions;

            if (jsonOptions == null)
            {
                throw new NotSupportedException("Only StiJsonOptions accepted!");
            }

            if (jsonOptions.Content == null || jsonOptions.Content.Length == 0)
            {
                return(null);
            }

            return(StiJsonToDataSetConverter.GetDataSet(jsonOptions.Content));
        }
        public override DataTable GetDataTable(string collectionName, string query, int?index = null, int?count = null)
        {
            try
            {
                var database = GetDatabase();
                //var collection = database.GetCollection<BsonDocument>(collectionName);
                var method = database.GetType().GetMethods().FirstOrDefault(m => m.Name == "GetCollection" &&
                                                                            !m.IsGenericMethodDefinition &&
                                                                            m.GetParameters().Length == 1 &&
                                                                            m.GetParameters()[0].ParameterType ==
                                                                            typeof(string));

                object collection = null;
                if (method != null)//net40
                {
                    collection = method.Invoke(database, new object[] { collectionName });
                }

                /*else//net45
                 * {
                 *  var settingsType = AssemblyHelper.GetType("MongoDB.Driver.MongoCollectionSettings");
                 *  method = database.GetType().GetMethod("GetCollection", new[] { typeof (string), settingsType });
                 *  method = method.MakeGenericMethod(database.GetType());
                 *  collection = method.Invoke(database, new object[] { collectionName, null });
                 * }*/

                //var cursor = string.IsNullOrWhiteSpace(query) ? collection.FindAll() : collection.Find(new QueryDocument(BsonDocument.Parse(query)));
                var         assembly = new StiDataAssemblyHelper("MongoDB.Bson.dll");
                IEnumerable cursor   = null;
                if (string.IsNullOrWhiteSpace(query))
                {
                    method = collection.GetType().GetMethod("FindAll");
                    cursor = method.Invoke(collection, null) as IEnumerable;
                }
                else
                {
                    var bsonDocument = assembly.GetType("MongoDB.Bson.BsonDocument")
                                       .GetMethod("Parse")
                                       .Invoke(null, new object[] { query });
                    var queryDocument = AssemblyHelper.CreateObject("MongoDB.Driver.QueryDocument", bsonDocument);

                    cursor = collection.GetType().GetMethod("Find").Invoke(collection, new[] { queryDocument }) as IEnumerable;
                }

                var list = new List <JToken>();
                var i    = 0;
                foreach (var row in cursor)
                {
                    if (count != null && i >= count)
                    {
                        break;
                    }
                    if (index == null || i >= index)
                    {
                        try
                        {
                            //var jStr = row.ToJson(new JsonWriterSettings { OutputMode = JsonOutputMode.Strict });

                            //var jsonWriterSettings = assembly.CreateObject("MongoDB.Bson.IO.JsonWriterSettings");
                            //jsonWriterSettings.GetType().GetProperty("OutputMode").SetValue(jsonWriterSettings, 0, null);

                            //var method2 = assembly.GetType("MongoDB.Bson.BsonExtensionMethods").GetMethod("ToJson", new[] {typeof(object), typeof(Type), jsonWriterSettings.GetType() });
                            //var jStr = method2.Invoke(row, new[] { jsonWriterSettings }) as string;
                            var jStr = GetAsJson(row);

                            var jToken = JToken.Parse(jStr);
                            list.Add(jToken);
                        }
                        catch
                        {
                        }
                    }

                    i++;
                }

                var dataSet = StiJsonToDataSetConverter.GetDataSet(list, true);
                return(dataSet != null && dataSet.Tables.Count > 0 ? dataSet.Tables[0] : null);
            }
            catch (Exception exception)
            {
                if (exception.InnerException != null)
                {
                    throw exception.InnerException;
                }
                else
                {
                    throw;
                }
            }
        }