diff --git a/src/ugar.db.Mongo.pas b/src/ugar.db.Mongo.pas index e6650dc..3121e1c 100644 --- a/src/ugar.db.Mongo.pas +++ b/src/ugar.db.Mongo.pas @@ -3,14 +3,20 @@ interface uses - Grijjy.Bson, System.Generics.Collections, System.JSON, System.SysUtils, ugar.db.mongo.Enum, - ugar.db.mongo.Query; + Grijjy.Bson, System.Generics.Collections, System.JSON, System.SysUtils, ugar.db.Mongo.Enum, + ugar.db.Mongo.Query; type IUgarDatabase = interface; IUgarCollection = interface; + IUgarCursor = interface + ['{18813F27-1B41-453C-86FE-E98AFEB3D905}'] + function GetEnumerator: TEnumerator; + function ToArray: TArray; + end; + IUgarClient = interface ['{66FF5346-48F6-44E1-A46F-D8B958F06EA0}'] function ListDatabaseNames: TArray; @@ -27,6 +33,8 @@ interface function ListCollections: TArray; procedure DropCollection(const AName: String); + function RunCommand(const ACommand: string): IUgarCursor; overload; + function RunCommand(const ACommand: TUgarBsonDocument): IUgarCursor; overload; procedure DropDatabase(); function GetCollection(const AName: String): IUgarCollection; @@ -36,21 +44,16 @@ interface property Name: String read _GetName; end; - IUgarCursor = interface - ['{18813F27-1B41-453C-86FE-E98AFEB3D905}'] - function GetEnumerator: TEnumerator; - function ToArray: TArray; - end; - IUgarCollection = interface ['{9822579B-1682-4FAC-81CF-A4B239777812}'] function _GetDatabase: IUgarDatabase; function _GetName: String; function InsertOne(const ADocument: TUgarBsonDocument): Boolean; overload; - function InsertOne(const ADocument: TJsonObject): Boolean; overload; + function InsertOne(const ADocument: TJsonObject): TJsonObject; overload; function InsertOne(const ADocument: string): Boolean; overload; - function InsertMany(const ADocuments: array of TUgarBsonDocument; const AOrdered: Boolean = True): Integer; overload; + function InsertMany(const ADocuments: array of TUgarBsonDocument; const AOrdered: Boolean = True): Integer; + overload; function InsertMany(const ADocuments: array of TJsonObject; const AOrdered: Boolean = True): Integer; overload; function InsertMany(const ADocuments: array of string; const AOrdered: Boolean = True): Integer; overload; @@ -58,7 +61,8 @@ interface function InsertMany(const ADocuments: TArray; const AOrdered: Boolean = True): Integer; overload; function InsertMany(const ADocuments: TArray; const AOrdered: Boolean = True): Integer; overload; - function InsertMany(const ADocuments: TEnumerable; const AOrdered: Boolean = True): Integer; overload; + function InsertMany(const ADocuments: TEnumerable; const AOrdered: Boolean = True) + : Integer; overload; function InsertMany(const ADocuments: TEnumerable; const AOrdered: Boolean = True): Integer; overload; function InsertMany(const ADocuments: TEnumerable; const AOrdered: Boolean = True): Integer; overload; @@ -66,8 +70,7 @@ interface function DeleteMany(const AFilter: TUgarFilter; const AOrdered: Boolean = True): Integer; - function UpdateOne(const AFilter: TUgarFilter; const AUpdate: TUgarUpdate; - const AUpsert: Boolean = False): Boolean; + function UpdateOne(const AFilter: TUgarFilter; const AUpdate: TUgarUpdate; const AUpsert: Boolean = False): Boolean; function UpdateMany(const AFilter: TUgarFilter; const AUpdate: TUgarUpdate; const AUpsert: Boolean = False; const AOrdered: Boolean = True): Integer; @@ -75,7 +78,7 @@ interface function Find(const AFilter: TUgarFilter; const AProjection: TUgarProjection): IUgarCursor; overload; function Find(const AFilter: TUgarFilter): IUgarCursor; overload; function Find(const AProjection: TUgarProjection): IUgarCursor; overload; - function Find: IUgarCursor; overload; + function Find: TJSONArray; overload; function Find(const AFilter: TUgarFilter; const ASort: TUgarSort): IUgarCursor; overload; function Find(const AFilter: TUgarFilter; const AProjection: TUgarProjection; const ASort: TUgarSort) : IUgarCursor; overload; @@ -91,7 +94,7 @@ interface property Name: String read _GetName; end; - TUgarDatabaseFunction = reference to function(AName: string): IUgarCollection; + TUgarDatabaseFunction = reference to function(AName: string = '_'): IUgarCollection; IUgarConnection = Interface function GetDatabase(ADatabaseName: string): TUgarDatabaseFunction; diff --git a/src/ugar.db.mongo.internals.pas b/src/ugar.db.mongo.internals.pas index 081af03..d977308 100644 --- a/src/ugar.db.mongo.internals.pas +++ b/src/ugar.db.mongo.internals.pas @@ -29,6 +29,9 @@ TUgarDatabase = class(TInterfacedObject, IUgarDatabase) procedure DropCollection(const AName: String); procedure DropDatabase; function GetCollection(const AName: String): IUgarCollection; + + function RunCommand(const ACommand: string): IUgarCursor; overload; + function RunCommand(const ACommand: TUgarBsonDocument): IUgarCursor; overload; protected property Protocol: TUgarMongoProtocol read FProtocol; property Name: String read FName; @@ -94,7 +97,7 @@ TUgarCollection = class(TInterfacedObject, IUgarCollection) function _GetName: String; function InsertOne(const ADocument: TUgarBsonDocument): Boolean; overload; - function InsertOne(const ADocument: TJsonObject): Boolean; overload; + function InsertOne(const ADocument: TJsonObject): TJSONObject; overload; function InsertOne(const ADocument: string): Boolean; overload; function InsertMany(const ADocuments: array of TUgarBsonDocument; const AOrdered: Boolean = True): Integer; overload; @@ -120,7 +123,7 @@ TUgarCollection = class(TInterfacedObject, IUgarCollection) function Find(const AFilter: TUgarFilter; const AProjection: TUgarProjection): IUgarCursor; overload; function Find(const AFilter: TUgarFilter): IUgarCursor; overload; function Find(const AProjection: TUgarProjection): IUgarCursor; overload; - function Find: IUgarCursor; overload; + function Find: TJSONArray; overload; function Find(const AFilter: TUgarFilter; const ASort: TUgarSort): IUgarCursor; overload; function Find(const AFilter: TUgarFilter; const AProjection: TUgarProjection; const ASort: TUgarSort) : IUgarCursor; overload; @@ -218,6 +221,21 @@ function TUgarDatabase.ListCollections: TArray; Result[LIndex] := LDocs[LIndex].AsBsonDocument; end; +function TUgarDatabase.RunCommand(const ACommand: TUgarBsonDocument): IUgarCursor; +var + Writer: IUgarBsonWriter; + Reply: IUgarMongoReply; +begin + Reply := FProtocol.OpQuery(FFullCommandCollectionName, [], 0, -1, ACommand.ToBson, nil); + HandleCommandReply(Reply); + Result := TUgarCursor.Create(FProtocol, FName, Reply.Documents, Reply.CursorId); +end; + +function TUgarDatabase.RunCommand(const ACommand: string): IUgarCursor; +begin + Result := RunCommand(TgoBsonDocument.Parse(ACommand)); +end; + function TUgarDatabase._GetClient: IUgarClient; begin Result := FClient; @@ -354,9 +372,19 @@ function TUgarCollection.Find(const AFilter, AProjection: TBytes): IUgarCursor; Result := TUgarCursor.Create(FProtocol, FFullName, Reply.Documents, Reply.CursorId); end; -function TUgarCollection.Find: IUgarCursor; +function TUgarCollection.Find: TJSONArray; +var + LBSON: TEnumerator; begin - Result := Find(nil, nil); + Result := TJSONArray.Create; + + LBSON := Find(nil, nil).GetEnumerator; + + while LBSON.MoveNext do + begin + Result.AddElement(TJSONObject.ParseJSONValue(LBSON.Current.ToJson)); + end; + end; function TUgarCollection.FindOne(const AFilter, AProjection: TBytes): TUgarBsonDocument; @@ -466,9 +494,10 @@ function TUgarCollection.InsertOne(const ADocument: TUgarBsonDocument): Boolean; Result := (HandleCommandReply(Reply) = 1); end; -function TUgarCollection.InsertOne(const ADocument: TJsonObject): Boolean; +function TUgarCollection.InsertOne(const ADocument: TJsonObject): TJSONObject; begin - Result := InsertOne(TUgarBsonDocument.Parse(ADocument.ToJSON)); + InsertOne(TUgarBsonDocument.Parse(ADocument.ToJSON)); + Result := ADocument; end; function TUgarCollection.InsertMany(const ADocuments: TArray; const AOrdered: Boolean): Integer; @@ -656,8 +685,10 @@ function TUgarCursor.TEnumerator.DoMoveNext: Boolean; if Result then Inc(FIndex) else if (FCursorId <> 0) then + begin GetMore; - Result := (FPage <> nil); + Result := (FPage <> nil); + end; end; procedure TUgarCursor.TEnumerator.GetMore;