Skip to content

Commit

Permalink
Allow run command in database
Browse files Browse the repository at this point in the history
  • Loading branch information
snakeice committed Apr 25, 2019
1 parent 8d4e6d2 commit e63d911
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 22 deletions.
33 changes: 18 additions & 15 deletions src/ugar.db.Mongo.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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<TUgarBsonDocument>;
function ToArray: TArray<TUgarBsonDocument>;
end;

IUgarClient = interface
['{66FF5346-48F6-44E1-A46F-D8B958F06EA0}']
function ListDatabaseNames: TArray<String>;
Expand All @@ -27,6 +33,8 @@ interface
function ListCollections: TArray<TUgarBsonDocument>;

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;
Expand All @@ -36,46 +44,41 @@ interface
property Name: String read _GetName;
end;

IUgarCursor = interface
['{18813F27-1B41-453C-86FE-E98AFEB3D905}']
function GetEnumerator: TEnumerator<TUgarBsonDocument>;
function ToArray: TArray<TUgarBsonDocument>;
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;

function InsertMany(const ADocuments: TArray<TUgarBsonDocument>; const AOrdered: Boolean = True): Integer; overload;
function InsertMany(const ADocuments: TArray<TJsonObject>; const AOrdered: Boolean = True): Integer; overload;
function InsertMany(const ADocuments: TArray<string>; const AOrdered: Boolean = True): Integer; overload;

function InsertMany(const ADocuments: TEnumerable<TUgarBsonDocument>; const AOrdered: Boolean = True): Integer; overload;
function InsertMany(const ADocuments: TEnumerable<TUgarBsonDocument>; const AOrdered: Boolean = True)
: Integer; overload;
function InsertMany(const ADocuments: TEnumerable<TJsonObject>; const AOrdered: Boolean = True): Integer; overload;
function InsertMany(const ADocuments: TEnumerable<string>; const AOrdered: Boolean = True): Integer; overload;

function DeleteOne(const AFilter: TUgarFilter): Boolean;

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;

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;
Expand All @@ -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;
Expand Down
45 changes: 38 additions & 7 deletions src/ugar.db.mongo.internals.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -218,6 +221,21 @@ function TUgarDatabase.ListCollections: TArray<TUgarBsonDocument>;
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;
Expand Down Expand Up @@ -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<TUgarBsonDocument>;
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;
Expand Down Expand Up @@ -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<TJsonObject>; const AOrdered: Boolean): Integer;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e63d911

Please sign in to comment.