Skip to content

Commit

Permalink
TMongo connection class finished
Browse files Browse the repository at this point in the history
  • Loading branch information
gerald-lindsly committed Nov 10, 2011
1 parent 46e5054 commit 553428b
Show file tree
Hide file tree
Showing 6 changed files with 911 additions and 29 deletions.
81 changes: 60 additions & 21 deletions MongoBson.pas
Expand Up @@ -84,6 +84,8 @@ TBsonBuffer = class(TObject)
function append(name : PAnsiChar; value : TBsonRegex) : Boolean; overload;
function append(name : PAnsiChar; value : TBsonTimestamp) : Boolean; overload;
function append(name : PAnsiChar; value : TBsonBinary) : Boolean; overload;
function append(name : PAnsiChar; value : TBson) : Boolean; overload;
function append(name : PAnsiChar; value : OleVariant) : Boolean; overload;
function appendNull(name : PAnsiChar) : Boolean;
function appendCode(name : PAnsiChar; value : PAnsiChar) : Boolean;
function appendSymbol(name : PAnsiChar; value : PAnsiChar) : Boolean;
Expand All @@ -98,9 +100,8 @@ TBsonBuffer = class(TObject)
end;

TBson = class(TObject)
private
var handle : Pointer;
public
var handle : Pointer;

function size() : Integer;
function iterator() : TBsonIterator;
function find(name : PAnsiChar) : TBsonIterator;
Expand Down Expand Up @@ -129,7 +130,15 @@ TBsonIterator = class(TObject)
destructor Destroy; override;
end;

function BSON(x : array of Variant) : TBson;
var
bsonEmpty : TBson;

(* The idea for this shorthand way to build a BSON
document from an array of variants came from Stijn Sanders
and his TMongoWire, located here:
https://github.com/stijnsanders/TMongoWire
*)
function BSON(x : array of OleVariant) : TBson;
function ByteToHex(InByte : Byte) : string;

implementation
Expand Down Expand Up @@ -176,6 +185,8 @@ implementation
cdecl; external 'mongoc.dll';
function bson_append_binary(b : Pointer; name : PAnsiChar; kind : Byte; data : Pointer; len : Integer) : Integer;
cdecl; external 'mongoc.dll';
function bson_append_bson(b : Pointer; name : PAnsiChar; value : Pointer) : Integer;
cdecl; external 'mongoc.dll';
function bson_buffer_size(b : Pointer) : Integer; cdecl; external 'mongoc.dll';
function bson_size(b : Pointer) : Integer; cdecl; external 'mongoc.dll';
function bson_iterator_create() : Pointer; external 'mongoc.dll';
Expand Down Expand Up @@ -446,6 +457,26 @@ implementation
Result := (bson_append_binary(handle, name, value.kind, value.data, value.len) = 0);
end;

function TBsonBuffer.append(name : PAnsiChar; value : OleVariant) : Boolean;
var
d : double;
begin
case VarType(value) of
varNull: Result := appendNull(name);
varInteger: Result := append(name, Integer(value));
varSingle, varDouble, varCurrency: begin
d := value;
Result := append(name, d);
end;
varDate: Result := append(name, TDateTime(value));
varInt64: Result := append(name, Int64(value));
varBoolean: Result := append(name, Boolean(value));
varOleStr: Result := append(name, PAnsiChar(AnsiString(value)));
else
raise Exception.Create('TBson.append(variant): type not supported (' + IntToStr(VarType(value)) + ')');
end;
end;

function TBsonBuffer.appendNull(name: PAnsiChar) : Boolean;
begin
if handle = nil then
Expand All @@ -460,6 +491,11 @@ implementation
Result := (bson_append_binary(handle, name, kind, data, length) = 0);
end;

function TBsonBuffer.append(name : PAnsiChar; value : TBson) : Boolean;
begin
Result := (bson_append_bson(handle, name, value.handle) = 0);
end;

function TBsonBuffer.startObject(name: PAnsiChar) : Boolean;
begin
if handle = nil then
Expand Down Expand Up @@ -595,6 +631,9 @@ implementation

procedure TBson.display();
begin
if Self = nil then
WriteLn('nil BSON')
else
_display(iterator, 0);
end;

Expand Down Expand Up @@ -671,49 +710,49 @@ implementation
result := digits[InByte shr 4] + digits[InByte and $0F];
end;

function BSON(x : array of Variant) : TBson;
function BSON(x : array of OleVariant) : TBson;
var
len : Integer;
i : Integer;
bb : TBsonBuffer;
len : Integer;
i : Integer;
bb : TBsonBuffer;
depth : Integer;
key : string;
key : string;
value : string;
name : PAnsiChar;
name : PAnsiChar;
begin
bb := TBsonBuffer.Create();
len := Length(x);
i := 0;
depth := 0;
while i < len do
while i < len do begin
key := VarToStr(x[i]);
if key = '}' then begin
if depth = 0 then
Raise Exception.Create('BSON: unexpected "}"');
Raise Exception.Create('BSON(): unexpected "}"');
bb.finishObject();
dec(depth);
end
else begin
name := PAnsiChar(AnsiString(key));
inc(i);
if i = Len then
raise Exception.Create('BSON: expected value for ' + key);
raise Exception.Create('BSON(): expected value for ' + key);
value := VarToStr(x[i]);
if value = '{' then begin
bb.startObject(name);
inc(depth);
end
else
case VarType(x[i]) of
varNull: bb.appendNull(name);
varInteger: bb.append(name, Integer(x[i]));
end;

bb.append(name, x[i]);
end;
inc(i);



end;
if depth > 0 then
Raise Exception.Create('BSON: open subobject');
Result := bb.finish();
end;

initialization
bsonEmpty := BSON([]);

end.

0 comments on commit 553428b

Please sign in to comment.