Browse Source

Added MongoCollection.ValidateName. Implemented AddOption, Hint, Max, MaxScan, Min, Snapshot, and Sort in MongoCursor. Added support for wrapped queries to MongoCursor. Added MongoDatabase.ValidateName.

pull/1/head
rstam 15 years ago
parent
commit
2d01cd47ff
  1. 2
      MongoDBClient/Internal/MongoQueryMessage.cs
  2. 82
      MongoDBClient/MongoCollection.cs
  3. 68
      MongoDBClient/MongoCursor.cs
  4. 2
      MongoDBClient/MongoDBClient.csproj
  5. 20
      MongoDBClient/MongoDatabase.cs
  6. 2
      MongoDBClient/QueryFlags.cs
  7. 2
      MongoDBClientTest/Program.cs

2
MongoDBClient/Internal/MongoQueryMessage.cs

@ -35,6 +35,7 @@ namespace MongoDB.MongoDBClient.Internal {
#region constructors
internal MongoQueryMessage(
MongoCollection collection,
QueryFlags flags,
int skip,
int batchSize,
BsonDocument query,
@ -42,6 +43,7 @@ namespace MongoDB.MongoDBClient.Internal {
) :
base(RequestOpCode.Query) {
this.collection = collection;
this.flags = flags;
this.skip = skip;
this.batchSize = batchSize;
this.query = query;

82
MongoDBClient/MongoCollection.cs

@ -35,6 +35,7 @@ namespace MongoDB.MongoDBClient {
MongoDatabase database,
string name
) {
ValidateName(name);
this.database = database;
this.name = name;
this.safeMode = database.SafeMode;
@ -388,6 +389,22 @@ namespace MongoDB.MongoDBClient {
throw new NotImplementedException();
}
#endregion
#region private methods
private void ValidateName(
string name
) {
if (name == null) {
throw new ArgumentNullException("name");
}
if (
name == "" ||
name.Contains('\0')
) {
throw new MongoException("Invalid collection name");
}
}
#endregion
}
public class MongoCollection<T> : MongoCollection where T : new() {
@ -407,59 +424,64 @@ namespace MongoDB.MongoDBClient {
return Find<T>(query);
}
public MongoCursor<T> FindAll() {
return FindAll<T>();
public MongoCursor<T> Find(
BsonDocument query,
BsonDocument fields
) {
return Find<T>(query, fields);
}
public T FindOne() {
return FindOne<T>();
public MongoCursor<T> Find(
string where
) {
return Find<T>(where);
}
public T FindOne(
BsonDocument query
public MongoCursor<T> Find(
string where,
BsonDocument fields
) {
return FindOne<T>(query);
return Find<T>(where, fields);
}
public MongoWriteResult Insert(
IEnumerable<T> documents
) {
return Insert<T>(documents);
public MongoCursor<T> FindAll() {
return FindAll<T>();
}
public MongoWriteResult Insert(
params T[] documents
public MongoCursor<T> FindAll(
BsonDocument fields
) {
return Insert<T>(documents);
return FindAll<T>(fields);
}
public MongoWriteResult Save(
T document
public T FindOne() {
return FindOne<T>();
}
public T FindOne(
BsonDocument query
) {
return Save<T>(document);
return FindOne<T>(query);
}
public MongoWriteResult Update(
public T FindOne(
BsonDocument query,
T update
BsonDocument fields
) {
return Update<T>(query, update, false, false);
return FindOne<T>(query, fields);
}
public MongoWriteResult Update(
BsonDocument query,
T update,
bool upsert,
bool multi
public T FindOne(
string where
) {
return Update<T>(query, update, upsert, multi);
return FindOne<T>(where);
}
public MongoWriteResult UpdateMulti(
BsonDocument query,
T update
public T FindOne(
string where,
BsonDocument fields
) {
return Update<T>(query, update, false, true);
return FindOne<T>(where, fields);
}
#endregion
}

68
MongoDBClient/MongoCursor.cs

@ -28,14 +28,12 @@ namespace MongoDB.MongoDBClient {
private bool disposed = false;
private MongoCollection collection;
private BsonDocument query;
private BsonDocument wrappedQuery = new BsonDocument();
private BsonDocument fields;
private BsonDocument orderBy;
private bool snapshot;
private QueryFlags flags;
private int skip;
private int limit; // number of documents to return (enforced by cursor)
private int batchSize; // number of documents to return in each reply
private bool explain;
private bool frozen; // TODO: freeze cursor once execution begins
#endregion
@ -70,6 +68,15 @@ namespace MongoDB.MongoDBClient {
#endregion
#region public methods
public MongoCursor<T> AddOption(
string name,
object value
) {
if (disposed) { throw new ObjectDisposedException("MongoCursor"); }
wrappedQuery[name] = value;
return this;
}
public MongoCursor<T> Batch(
int batchSize
) {
@ -102,7 +109,7 @@ namespace MongoDB.MongoDBClient {
// TODO: verbose argument?
public BsonDocument Explain() {
explain = true;
wrappedQuery["$explain"] = true;
throw new NotImplementedException();
}
@ -160,7 +167,8 @@ namespace MongoDB.MongoDBClient {
public MongoCursor<T> Hint(
BsonDocument hint
) {
throw new NotImplementedException();
wrappedQuery["$hint"] = hint;
return this;
}
public MongoCursor<T> Limit(
@ -171,6 +179,27 @@ namespace MongoDB.MongoDBClient {
return this;
}
public MongoCursor<T> Max(
BsonDocument max
) {
wrappedQuery["$max"] = max;
return this;
}
public MongoCursor<T> MaxScan(
int maxScan
) {
wrappedQuery["$maxscan"] = maxScan;
return this;
}
public MongoCursor<T> Min(
BsonDocument min
) {
wrappedQuery["$min"] = min;
return this;
}
public int Size() {
if (disposed) { throw new ObjectDisposedException("MongoCursor"); }
var command = new BsonDocument {
@ -197,7 +226,7 @@ namespace MongoDB.MongoDBClient {
public MongoCursor<T> Snapshot() {
if (disposed) { throw new ObjectDisposedException("MongoCursor"); }
this.snapshot = true;
wrappedQuery["$snapshot"] = true;
return this;
}
@ -205,7 +234,7 @@ namespace MongoDB.MongoDBClient {
BsonDocument orderBy
) {
if (disposed) { throw new ObjectDisposedException("MongoCursor"); }
this.orderBy = orderBy;
wrappedQuery["$orderby"] = orderBy;
return this;
}
@ -221,10 +250,10 @@ namespace MongoDB.MongoDBClient {
int direction
) {
if (disposed) { throw new ObjectDisposedException("MongoCursor"); }
orderBy = new BsonDocument {
var orderBy = new BsonDocument {
{ key, direction }
};
return this;
return Sort(orderBy);
}
#endregion
@ -243,7 +272,7 @@ namespace MongoDB.MongoDBClient {
numberToReturn = limit;
}
var message = new MongoQueryMessage(collection, skip, numberToReturn, query, fields);
var message = new MongoQueryMessage(collection, flags, skip, numberToReturn, WrappedQuery(), fields);
connection.SendMessage(message);
var reply = connection.ReceiveMessage<T>();
if ((reply.ResponseFlags & ResponseFlags.QueryFailure) != 0) {
@ -268,6 +297,25 @@ namespace MongoDB.MongoDBClient {
}
return reply;
}
private BsonDocument WrappedQuery() {
if (wrappedQuery.Elements.Count() == 0) {
return query;
}
if (query == null) { query = new BsonDocument(); }
if (query.ContainsElement("$query")) {
// it's already wrapped, just copy over the options
// note that options in wrappedQuery overwrite existing options in query
foreach (var element in wrappedQuery) {
query[element.Name] = element.Value;
}
return query;
} else {
wrappedQuery["$query"] = query;
return wrappedQuery;
}
}
#endregion
}
}

2
MongoDBClient/MongoDBClient.csproj

@ -56,7 +56,7 @@
<Compile Include="Internal\MongoMessage.cs" />
<Compile Include="Internal\MongoQueryMessage.cs" />
<Compile Include="Internal\MongoReplyMessage.cs" />
<Compile Include="Internal\QueryFlags.cs" />
<Compile Include="QueryFlags.cs" />
<Compile Include="Internal\RequestOpCode.cs" />
<Compile Include="Internal\ResponseFlags.cs" />
<Compile Include="MongoCollection.cs" />

20
MongoDBClient/MongoDatabase.cs

@ -36,6 +36,7 @@ namespace MongoDB.MongoDBClient {
MongoServer server,
string name
) {
ValidateName(name);
this.server = server;
this.name = name;
this.safeMode = server.SafeMode;
@ -46,6 +47,7 @@ namespace MongoDB.MongoDBClient {
string name,
MongoCredentials credentials
) {
ValidateName(name);
this.server = server;
this.name = name;
this.credentials = credentials;
@ -276,5 +278,23 @@ namespace MongoDB.MongoDBClient {
return name;
}
#endregion
#region private methods
private void ValidateName(
string name
) {
if (name == null) {
throw new NotImplementedException();
}
if (
name == "" ||
name.IndexOfAny(new char[] { '\0', ' ', '.', '$', '/', '\\' }) != -1 ||
name != name.ToLower() ||
Encoding.UTF8.GetBytes(name).Length > 64
) {
throw new MongoException("Invalid database name");
}
}
#endregion
}
}

2
MongoDBClient/Internal/QueryFlags.cs → MongoDBClient/QueryFlags.cs

@ -18,7 +18,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MongoDB.MongoDBClient.Internal {
namespace MongoDB.MongoDBClient {
[Flags]
public enum QueryFlags {
TailableCursor = 1,

2
MongoDBClientTest/Program.cs

@ -46,7 +46,7 @@ namespace MongoDB.MongoDBClientTest {
string connectionString = "mongodb://localhost/test";
var database = MongoDatabase.FromConnectionString(connectionString);
var collection = database.GetCollection<BsonDocument>("library");
foreach (var document in collection.FindAll()) {
foreach (var document in collection.FindAll().Snapshot()) {
Console.WriteLine(document.ToString());
}
}

Loading…
Cancel
Save