Permalink
Browse files

GrdiFS ExtractFile and err_handler for BSON

  • Loading branch information...
1 parent 8e70f07 commit 36d5dd4f6b0861a347d8865400a34f7b6cbad6dc @gerald-lindsly committed Dec 6, 2012
Showing with 28 additions and 1 deletion.
  1. +8 −0 MongoBson.pas
  2. +20 −1 Test.dpr
View
8 MongoBson.pas
@@ -331,6 +331,8 @@ TBsonIterator = class(TObject)
implementation
uses SysUtils, Variants;
+ procedure set_bson_err_handler(err_handler : Pointer); cdecl; external 'mongoc.dll';
+
function bson_create() : Pointer; cdecl; external 'mongoc.dll';
procedure bson_init(b : Pointer); cdecl; external 'mongoc.dll';
procedure bson_destroy(b : Pointer); cdecl; external 'mongoc.dll';
@@ -1107,8 +1109,14 @@ implementation
Result := bb.finish();
end;
+ procedure err_handler(msg : PAnsiChar);
+ begin
+ Raise Exception.Create(string(msg));
+ end;
+
initialization
bsonEmpty := BSON([]);
+ set_bson_err_handler(Addr(err_handler));
end.
View
21 Test.dpr
@@ -34,7 +34,6 @@ var
bin : TBsonBinary;
sing : Single;
mongo : TMongo;
- count : Integer;
j : Integer;
cursor : TMongoCursor;
databases : TStringArray;
@@ -59,6 +58,25 @@ begin
Writeln(collections[j]);
end;
+procedure ExtractFile(gfs : TGridFS; gfsname : string; filename : string = '');
+ var chunk : Integer;
+ f : File;
+ b : TBson;
+ bin : TBsonBinary;
+begin
+ if filename = '' then filename := gfsname;
+ gf := gfs.find(gfsname);
+ Assign(f, filename);
+ Rewrite(f, 1);
+ for chunk := 0 to gf.getChunkCount - 1 do
+ begin
+ b := gf.getChunk(chunk);
+ bin := b.find('data').getBinary;
+ BlockWrite(f, bin.data^, bin.len);
+ end;
+ Close(f);
+end;
+
begin
try
@@ -337,6 +355,7 @@ begin
buf[20] := Chr(0);
WriteLn(buf);
+ ExtractFile(gfs, 'MongoDB.pas');
WriteLn('Done');
ReadLn;
end

0 comments on commit 36d5dd4

Please sign in to comment.