unit uDBDataSet;

interface

uses Rubies;

var
  cDBDataSet: Tvalue;

procedure Init_DBDataSet;

implementation

uses DBTables, uDefUtils, Pythia, uDatabase, uRDBExt;

function dl_DBDataSet(This: Tvalue): TDBDataSet;
begin
  result := ap_data_get_struct(This);
end;

function DataSet_get_updates_pending(This: Tvalue): Tvalue; cdecl;
begin
  result := ap_bool(dl_DBDataSet(This).UpdatesPending);
end;

function DataSet_apply_updates(This: Tvalue): Tvalue; cdecl;
begin
  result := This;
  dl_DBDataSet(This).ApplyUpdates;
end;

function DataSet_commit_updates(This: Tvalue): Tvalue; cdecl;
begin
  result := This;
  dl_DBDataSet(This).CommitUpdates;
end;

function DataSet_update_status(This: Tvalue): Tvalue; cdecl;
begin
  result := INT2FIX(ord(dl_DBDataSet(This).UpdateStatus));
end;

function DataSet_get_database(This: Tvalue): Tvalue; cdecl;
begin
  result := ap_iDatabase(dl_DBDataSet(This).Database, This);
end;

function DataSet_get_modified(This: Tvalue): Tvalue; cdecl;
begin
  result := ap_Bool(dl_DBDataSet(This).Modified);
end;

procedure Init_DBDataSet;
begin
  cDBDataSet := DefinePersistentClass(ap_mRDB, TDBDataSet, ap_cDataSet, nil);

  DefineAttrGet(cDBDataSet, 'updates_pending', DataSet_get_updates_pending);
  rb_define_method(cDBDataSet, 'apply_updates', @DataSet_apply_updates, 0);
  rb_define_method(cDBDataSet, 'update_status', @DataSet_update_status, 0);
  rb_define_method(cDBDataSet, 'commit_updates', @DataSet_commit_updates, 0);

  DefineAttrGet(cDBDataSet, 'database', DataSet_get_database);
  DefineAttrGet(cDBDataSet, 'modified', DataSet_get_modified);
  rb_define_alias(cDBDataSet, 'modified?', 'modified');
end;

end.
