mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-21 02:58:05 +08:00
Update mongodb
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using MongoDB.Bson;
|
using MongoDB.Bson;
|
||||||
using MongoDB.Bson.Serialization;
|
using MongoDB.Bson.Serialization;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Driver.Linq;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data.Common;
|
using System.Data.Common;
|
||||||
@@ -20,8 +21,9 @@ namespace MongoDb.Ado.data
|
|||||||
var aggregateFluent = collection.Aggregate<BsonDocument>(pipeline.Select(stage => new BsonDocument(stage.AsBsonDocument)).ToArray());
|
var aggregateFluent = collection.Aggregate<BsonDocument>(pipeline.Select(stage => new BsonDocument(stage.AsBsonDocument)).ToArray());
|
||||||
|
|
||||||
// 执行聚合查询并返回 DbDataReader
|
// 执行聚合查询并返回 DbDataReader
|
||||||
var cursor = aggregateFluent.ToEnumerable();
|
var cursor = aggregateFluent.ToList();
|
||||||
return new MongoDbBsonDocumentDataReader(cursor);
|
var result= new MongoDbBsonDocumentDataReader(cursor);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using MongoDB.Bson;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Data.Common;
|
using System.Data.Common;
|
||||||
@@ -105,7 +106,12 @@ namespace MongoDb.Ado.data
|
|||||||
DataRow daRow = dt.NewRow();
|
DataRow daRow = dt.NewRow();
|
||||||
for (int i = 0; i < columns.Count; i++)
|
for (int i = 0; i < columns.Count; i++)
|
||||||
{
|
{
|
||||||
daRow[columns[i].ColumnName] = dr.GetValue(i);
|
var value = dr.GetValue(i);
|
||||||
|
if(value is ObjectId)
|
||||||
|
{
|
||||||
|
value = value?.ToString();
|
||||||
|
}
|
||||||
|
daRow[columns[i].ColumnName] = value;
|
||||||
}
|
}
|
||||||
dt.Rows.Add(daRow);
|
dt.Rows.Add(daRow);
|
||||||
}
|
}
|
||||||
|
@@ -6,6 +6,7 @@ using MongoDB.Bson;
|
|||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using MongoDB.Bson.Serialization;
|
||||||
|
|
||||||
namespace MongoDb.Ado.data
|
namespace MongoDb.Ado.data
|
||||||
{
|
{
|
||||||
@@ -13,9 +14,13 @@ namespace MongoDb.Ado.data
|
|||||||
{
|
{
|
||||||
private readonly IEnumerator<BsonDocument> _enumerator;
|
private readonly IEnumerator<BsonDocument> _enumerator;
|
||||||
private BsonDocument _current;
|
private BsonDocument _current;
|
||||||
|
private IEnumerable<BsonDocument> _documents;
|
||||||
|
private BsonDocument _firstObj;
|
||||||
public MongoDbBsonDocumentDataReader(IEnumerable<BsonDocument> documents)
|
public MongoDbBsonDocumentDataReader(IEnumerable<BsonDocument> documents)
|
||||||
{
|
{
|
||||||
_enumerator = documents.GetEnumerator();
|
_enumerator = documents.GetEnumerator();
|
||||||
|
_documents = documents;
|
||||||
|
_firstObj=_documents.FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override bool Read()
|
public override bool Read()
|
||||||
@@ -28,7 +33,7 @@ namespace MongoDb.Ado.data
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override int FieldCount => _current?.Count()??0;
|
public override int FieldCount => _documents?.FirstOrDefault()?.Count()??0;
|
||||||
|
|
||||||
public override int Depth => throw new NotImplementedException();
|
public override int Depth => throw new NotImplementedException();
|
||||||
|
|
||||||
@@ -52,9 +57,15 @@ namespace MongoDb.Ado.data
|
|||||||
public override double GetDouble(int ordinal) => (double)GetValue(ordinal);
|
public override double GetDouble(int ordinal) => (double)GetValue(ordinal);
|
||||||
public override Type GetFieldType(int ordinal)
|
public override Type GetFieldType(int ordinal)
|
||||||
{
|
{
|
||||||
var obj = GetValue(ordinal);
|
var firstObj = _firstObj;
|
||||||
|
if(firstObj==null) return typeof(object);
|
||||||
|
var obj = firstObj.GetValue(ordinal);
|
||||||
|
if (obj is BsonObjectId)
|
||||||
|
{
|
||||||
|
return typeof(string);
|
||||||
|
}
|
||||||
if (obj == null) return typeof(object);
|
if (obj == null) return typeof(object);
|
||||||
return obj.GetType();
|
return BsonTypeMapper.MapToDotNetValue(obj).GetType();
|
||||||
}
|
}
|
||||||
public override float GetFloat(int ordinal) => (float)GetValue(ordinal);
|
public override float GetFloat(int ordinal) => (float)GetValue(ordinal);
|
||||||
public override Guid GetGuid(int ordinal) => (Guid)GetValue(ordinal);
|
public override Guid GetGuid(int ordinal) => (Guid)GetValue(ordinal);
|
||||||
@@ -80,11 +91,12 @@ namespace MongoDb.Ado.data
|
|||||||
|
|
||||||
public override string GetName(int ordinal)
|
public override string GetName(int ordinal)
|
||||||
{
|
{
|
||||||
if (_current == null)
|
var firstObj=_firstObj;
|
||||||
throw new InvalidOperationException("No current document.");
|
if (firstObj == null)
|
||||||
|
return "";
|
||||||
|
|
||||||
// 获取当前文档的字段元素列表(Elements)
|
// 获取当前文档的字段元素列表(Elements)
|
||||||
var elements = _current.Elements.ToList();
|
var elements = firstObj.Elements.ToList();
|
||||||
|
|
||||||
// 确保 ordinal 是有效的索引
|
// 确保 ordinal 是有效的索引
|
||||||
if (ordinal < 0 || ordinal >= elements.Count)
|
if (ordinal < 0 || ordinal >= elements.Count)
|
||||||
@@ -96,8 +108,9 @@ namespace MongoDb.Ado.data
|
|||||||
|
|
||||||
public override int GetOrdinal(string name)
|
public override int GetOrdinal(string name)
|
||||||
{
|
{
|
||||||
|
var firstObj = _firstObj;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
foreach (var elem in _current.Elements)
|
foreach (var elem in firstObj.Elements)
|
||||||
{
|
{
|
||||||
if (elem.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
|
if (elem.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
|
||||||
return i;
|
return i;
|
||||||
|
@@ -141,7 +141,30 @@ namespace SqlSugar.MongoDb
|
|||||||
|
|
||||||
// 结构体/Map类型(MongoDb 1.0+支持)
|
// 结构体/Map类型(MongoDb 1.0+支持)
|
||||||
new KeyValuePair<string, CSharpDataType>("STRUCT", CSharpDataType.@object), // 需动态解析
|
new KeyValuePair<string, CSharpDataType>("STRUCT", CSharpDataType.@object), // 需动态解析
|
||||||
new KeyValuePair<string, CSharpDataType>("MAP", CSharpDataType.@object) // 键值对
|
new KeyValuePair<string, CSharpDataType>("MAP", CSharpDataType.@object), // 键值对
|
||||||
|
|
||||||
|
new KeyValuePair<string, CSharpDataType>("INT16", CSharpDataType.@short), // CLR short
|
||||||
|
new KeyValuePair<string, CSharpDataType>("UINT16", CSharpDataType.@short), // CLR ushort
|
||||||
|
new KeyValuePair<string, CSharpDataType>("INT32", CSharpDataType.@int), // CLR int
|
||||||
|
new KeyValuePair<string, CSharpDataType>("UINT32", CSharpDataType.@int), // CLR uint
|
||||||
|
new KeyValuePair<string, CSharpDataType>("INT64", CSharpDataType.@long), // CLR long
|
||||||
|
new KeyValuePair<string, CSharpDataType>("UINT64", CSharpDataType.@long), // CLR ulong
|
||||||
|
new KeyValuePair<string, CSharpDataType>("BYTE", CSharpDataType.@byte), // CLR byte
|
||||||
|
new KeyValuePair<string, CSharpDataType>("SBYTE", CSharpDataType.@sbyte), // CLR sbyte
|
||||||
|
new KeyValuePair<string, CSharpDataType>("SINGLE", CSharpDataType.@float), // CLR float (Single)
|
||||||
|
new KeyValuePair<string, CSharpDataType>("DECIMAL", CSharpDataType.@decimal), // CLR decimal
|
||||||
|
new KeyValuePair<string, CSharpDataType>("DATETIMEOFFSET", CSharpDataType.@DateTimeOffset), // CLR DateTimeOffset
|
||||||
|
new KeyValuePair<string, CSharpDataType>("DATETIME", CSharpDataType.@DateTime), // CLR DateTime
|
||||||
|
new KeyValuePair<string, CSharpDataType>("BOOLEAN", CSharpDataType.@bool), // CLR bool
|
||||||
|
new KeyValuePair<string, CSharpDataType>("STRING", CSharpDataType.@string), // CLR string
|
||||||
|
new KeyValuePair<string, CSharpDataType>("GUID", CSharpDataType.@Guid), // CLR Guid
|
||||||
|
new KeyValuePair<string, CSharpDataType>("ENUM", CSharpDataType.@enum), // CLR enum
|
||||||
|
new KeyValuePair<string, CSharpDataType>("OBJECT", CSharpDataType.@object), // CLR object
|
||||||
|
new KeyValuePair<string, CSharpDataType>("OTHER", CSharpDataType.@other), // custom/unknown
|
||||||
|
new KeyValuePair<string, CSharpDataType>("BYTE[]", CSharpDataType.@byteArray), // CLR byte[]
|
||||||
|
new KeyValuePair<string, CSharpDataType>("TIME", CSharpDataType.@time), // possibly TimeOnly
|
||||||
|
new KeyValuePair<string, CSharpDataType>("TIMESPAN", CSharpDataType.@TimeSpan), // CLR TimeSpan
|
||||||
|
new KeyValuePair<string, CSharpDataType>("CHAR", CSharpDataType.@char), // CLR char
|
||||||
};
|
};
|
||||||
public override List<string> StringThrow
|
public override List<string> StringThrow
|
||||||
{
|
{
|
||||||
|
@@ -95,7 +95,7 @@ namespace SqlSugar.MongoDb
|
|||||||
|
|
||||||
public override void SetCommandToAdapter(IDataAdapter adapter, DbCommand command)
|
public override void SetCommandToAdapter(IDataAdapter adapter, DbCommand command)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
((MongoDbDataAdapter)adapter).SelectCommand =(MongoDbCommand)command;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override DbCommand GetCommand(string sql, SugarParameter[] pars)
|
public override DbCommand GetCommand(string sql, SugarParameter[] pars)
|
||||||
|
Reference in New Issue
Block a user