Skip to content
Browse files

Bson array functions added

  • Loading branch information...
1 parent 09bc522 commit 42fb41624544e1d4ca35f38766da34062676b7e6 @gerald-lindsly committed Jan 3, 2012
Showing with 200 additions and 0 deletions.
  1. +184 −0 MongoBson.pas
  2. +16 −0 Test.dpr
View
184 MongoBson.pas
@@ -6,6 +6,10 @@
interface
type TBson = class;
+ TIntegerArray = array of Integer;
+ TDoubleArray = array of Double;
+ TBooleanArray = array of Boolean;
+ TStringArray = array of string;
{ A value of TBsonType indicates the type of the data associated
with a field within a BSON document. }
@@ -155,6 +159,14 @@ TBsonBuffer = class(TObject)
{ Generic version of append. Calls one of the other append functions
if the type contained in the variant is supported. }
function append(name : PAnsiChar; value : OleVariant) : Boolean; overload;
+ { Append an array of Integers }
+ function appendArray(name : PAnsiChar; value : array of Integer) : Boolean; overload;
+ { Append an array of Doubles }
+ function appendArray(name : PAnsiChar; value : array of Double) : Boolean; overload;
+ { Append an array of Booleans }
+ function appendArray(name : PAnsiChar; value : array of Boolean) : Boolean; overload;
+ { Append an array of strings }
+ function appendArray(name : PAnsiChar; value : array of string) : Boolean; overload;
{ Append a NULL field to the buffer }
function appendNull(name : PAnsiChar) : Boolean;
{ Append an UNDEFINED field to the buffer }
@@ -251,6 +263,18 @@ TBsonIterator = class(TObject)
{ Get a TBsonBinary object for the BINDATA field pointed to by this
iterator. }
function getBinary() : TBsonBinary;
+ { Get an array of Integers. This iterator must point to ARRAY field
+ which has each component type as Integer }
+ function getIntegerArray() : TIntegerArray;
+ { Get an array of Doubles. This iterator must point to ARRAY field
+ which has each component type as Double }
+ function getDoubleArray() : TDoubleArray;
+ { Get an array of strings. This iterator must point to ARRAY field
+ which has each component type as string }
+ function getStringArray() : TStringArray;
+ { Get an array of Booleans. This iterator must point to ARRAY field
+ which has each component type as Boolean }
+ function getBooleanArray() : TBooleanArray;
{ Internal usage only. Create an uninitialized TBsonIterator }
constructor Create(); overload;
{ Create a TBsonIterator that points to the first field of the given
@@ -484,6 +508,98 @@ implementation
Result := i;
end;
+ function TBsonIterator.getIntegerArray() : TIntegerArray;
+ var
+ i : TBsonIterator;
+ j, count : Integer;
+ begin
+ if kind() <> bsonArray then
+ raise Exception.Create('Iterator does not point to an array');
+ i := subiterator();
+ count := 0;
+ while i.next() do begin
+ if i.kind() <> bsonINT then
+ raise Exception.Create('Array component is not an Integer');
+ inc(count);
+ end;
+ i := subiterator;
+ j := 0;
+ SetLength(Result, count);
+ while i.next() do begin
+ Result[j] := i.value();
+ inc(j);
+ end;
+ end;
+
+ function TBsonIterator.getDoubleArray() : TDoubleArray;
+ var
+ i : TBsonIterator;
+ j, count : Integer;
+ begin
+ if kind() <> bsonArray then
+ raise Exception.Create('Iterator does not point to an array');
+ i := subiterator();
+ count := 0;
+ while i.next() do begin
+ if i.kind() <> bsonDOUBLE then
+ raise Exception.Create('Array component is not a Double');
+ inc(count);
+ end;
+ i := subiterator;
+ j := 0;
+ SetLength(Result, count);
+ while i.next() do begin
+ Result[j] := i.value();
+ inc(j);
+ end;
+ end;
+
+ function TBsonIterator.getStringArray() : TStringArray;
+ var
+ i : TBsonIterator;
+ j, count : Integer;
+ begin
+ if kind() <> bsonArray then
+ raise Exception.Create('Iterator does not point to an array');
+ i := subiterator();
+ count := 0;
+ while i.next() do begin
+ if i.kind() <> bsonSTRING then
+ raise Exception.Create('Array component is not a string');
+ inc(count);
+ end;
+ i := subiterator;
+ j := 0;
+ SetLength(Result, count);
+ while i.next() do begin
+ Result[j] := i.value();
+ inc(j);
+ end;
+ end;
+
+ function TBsonIterator.getBooleanArray() : TBooleanArray;
+ var
+ i : TBsonIterator;
+ j, count : Integer;
+ begin
+ if kind() <> bsonArray then
+ raise Exception.Create('Iterator does not point to an array');
+ i := subiterator();
+ count := 0;
+ while i.next() do begin
+ if i.kind() <> bsonBOOL then
+ raise Exception.Create('Array component is not a Boolean');
+ inc(count);
+ end;
+ i := subiterator;
+ j := 0;
+ SetLength(Result, count);
+ while i.next() do begin
+ Result[j] := i.value();
+ inc(j);
+ end;
+ end;
+
function TBson.value(name: PAnsiChar) : Variant;
var
i : TBsonIterator;
@@ -651,6 +767,74 @@ implementation
Result := (bson_append_bson(handle, name, value.handle) = 0);
end;
+ function TBsonBuffer.appendArray(name : PAnsiChar; value : array of Integer) : Boolean;
+ var
+ success : Boolean;
+ i, len : Integer;
+ begin
+ success := (bson_append_start_array(handle, name) = 0);
+ len := Length(value);
+ i := 0;
+ while success and (i < len) do begin
+ success := (bson_append_int(handle, PAnsiChar(AnsiString(IntToStr(i))), value[i]) = 0);
+ inc(i);
+ end;
+ if success then
+ success := (bson_append_finish_object(handle) = 0);
+ Result := success;
+ end;
+
+ function TBsonBuffer.appendArray(name : PAnsiChar; value : array of Double) : Boolean;
+ var
+ success : Boolean;
+ i, len : Integer;
+ begin
+ success := (bson_append_start_array(handle, name) = 0);
+ len := Length(value);
+ i := 0;
+ while success and (i < len) do begin
+ success := (bson_append_double(handle, PAnsiChar(AnsiString(IntToStr(i))), value[i]) = 0);
+ inc(i);
+ end;
+ if success then
+ success := (bson_append_finish_object(handle) = 0);
+ Result := success;
+ end;
+
+ function TBsonBuffer.appendArray(name : PAnsiChar; value : array of Boolean) : Boolean;
+ var
+ success : Boolean;
+ i, len : Integer;
+ begin
+ success := (bson_append_start_array(handle, name) = 0);
+ len := Length(value);
+ i := 0;
+ while success and (i < len) do begin
+ success := (bson_append_bool(handle, PAnsiChar(AnsiString(IntToStr(i))), value[i]) = 0);
+ inc(i);
+ end;
+ if success then
+ success := (bson_append_finish_object(handle) = 0);
+ Result := success;
+ end;
+
+ function TBsonBuffer.appendArray(name : PAnsiChar; value : array of string) : Boolean;
+ var
+ success : Boolean;
+ i, len : Integer;
+ begin
+ success := (bson_append_start_array(handle, name) = 0);
+ len := Length(value);
+ i := 0;
+ while success and (i < len) do begin
+ success := (bson_append_string(handle, PAnsiChar(AnsiString(IntToStr(i))), PAnsiChar(AnsiString(value[i]))) = 0);
+ inc(i);
+ end;
+ if success then
+ success := (bson_append_finish_object(handle) = 0);
+ Result := success;
+ end;
+
function TBsonBuffer.startObject(name: PAnsiChar) : Boolean;
begin
if handle = nil then
View
16 Test.dpr
@@ -27,6 +27,8 @@ var
gfw : TGridfileWriter;
gf : TGridfile;
buf : array[0..100] of AnsiChar;
+ ia1 : array[0..4] of Integer;
+ ia2 : TIntegerArray;
const
db = 'test';
@@ -44,6 +46,20 @@ end;
begin
try
+
+ bb := TBsonBuffer.Create();
+ ia1[0] := 5;
+ ia1[1] := 7;
+ ia1[2] := 9;
+ ia1[3] := 11;
+ ia1[4] := 13;
+ bb.appendArray('ia', ia1);
+ b := bb.finish;
+ i := b.iterator;
+ ia2 := i.getIntegerArray();
+ for j := 0 to Length(ia2) - 1 do
+ WriteLn(ia2[j]);
+
bb := TBsonBuffer.Create();
bb.append('test', 'testing');
cws := TBsonCodeWScope.Create('Code for scope', bb.finish());

0 comments on commit 42fb416

Please sign in to comment.
Something went wrong with that request. Please try again.