Forum > Databases

dOPF - Object persistence framework / ORM

(1/4) > >>

Renat.Su:
I just want to inform about the dOPF library (author Silvio Clécio  https://github.com/silvioprog ). A very convenient, intuitive and lightweight ORM. Previously, it was in the BrookFreePascal plugins folder, although it was a self-contained library and has no dependency on Brook. Now it's got a separate location here https://github.com/pascal-libs/dopf


P.S.
When I need to get a fast and convenient database operations (especially not in GUI applications) this library is my most frequent choice.

Zoran:
Is there any help or tutorial for it?

Renat.Su:

--- Quote from: Zoran on November 17, 2024, 10:16:15 pm ---Is there any help or tutorial for it?

--- End quote ---
The best tutorial is demo folder. There are some main ways to use the lib.

It has different generic classes for working with databases. The most basic way to CRUD operations with maximum approximation is to work with DB engines through objects of your classes. Published properties are fields. Standard for ORM encapsulation of work with SQL engines (MySQL, SQLite, Firebird etc - connecting via ***conn.pas from native sqldb packages and driver property). And many other features.

Renat.Su:
For example, https://github.com/pascal-libs/dopf/tree/main/demos/opf/entity
Work with storage SQLite3 via entity class:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program demo1; {$mode objfpc}{$H+} uses  dOpf, dSQLdbBroker, dbutils, person, sysutils; type  Topf = specialize TdGSQLdbEntityOpf<TPerson>; var  i: TPerson;  pers: Topf.TEntities;  opf: Topf;begin  opf := Topf.Create(dbutils.con, 'person');  pers := Topf.TEntities.Create;  try    WriteLn('Empty table');    opf.Empty;    opf.Apply;    WriteLn('Done.');     WriteLn('Add Silvio Clécio');    opf.Entity.Name := 'Silvio Clécio';    opf.Add;    WriteLn('Done.');     WriteLn('Add Anonymous');    opf.Entity.Id := 1000;    opf.Entity.Name := 'Anonymous';    opf.Add(False);    WriteLn('Done.');     WriteLn('Add Waldir');    opf.Entity.Id := 1001;    opf.Entity.Name := 'Waldir';    opf.Add(False);    WriteLn('Done.');     WriteLn('Add João Morais');    opf.Entity.Name := 'João Morais';    opf.Add;    WriteLn('Done.');     WriteLn('Add Sven Barth');    opf.Entity.Name := 'Sven Barth';    opf.Add;    WriteLn('Done.');     WriteLn('Modify name of Waldir to Waldir Paim');    opf.Entity.Id := 1001;    opf.Entity.Name := 'Waldir Paim';    opf.Modify;    WriteLn('Done.');     WriteLn('Remove Anonymous');    opf.Entity.Id := 1000;    opf.Remove;    WriteLn('Done.');     WriteLn('Get Waldir Paim');    opf.Entity.Id := 1001;    opf.Get;    WriteLn(opf.Entity.Id, ', ', opf.Entity.Name);    WriteLn('Done.');     WriteLn('Find Silvio Clécio by name');    opf.Entity.Name := 'Silvio Clécio';    opf.Find('name = :name');    WriteLn(opf.Entity.Id, ', ', opf.Entity.Name);    WriteLn('Done.');     WriteLn('Search for names containing "a"');    opf.Entity.Name := '%a%';    opf.Find(pers, 'name like (:name)');    for i in pers do      WriteLn(i.Id, ', ', i.Name);    pers.Clear;    WriteLn('Done.');     WriteLn('List all');    opf.List(pers);    for i in pers do      WriteLn(i.Id, ', ', i.Name);    pers.Clear;    WriteLn('Done.');     WriteLn('Search for names containing "a" (order by id DESC)');    opf.Entity.Name := '%a%';    opf.Search(pers, nil,      'select * from person where name like (:name) order by id desc');    for i in pers do      WriteLn(i.Id, ', ', i.Name);    pers.Clear;    WriteLn('Done.');     opf.Apply;  finally    pers.Free;    opf.Free;  end;   ReadLn;end.Connection to DB in dbutils.pas

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit dbutils; {$mode objfpc}{$H+} interface uses  dSQLdbBroker, sqlite3conn, SysUtils; function con: TdSQLdbConnector; implementation var  _con: TdSQLdbConnector = nil; function con: TdSQLdbConnector;begin  if not Assigned(_con) then  begin    _con := TdSQLdbConnector.Create(nil);    _con.Logger.Active := True;    _con.Logger.FileName := 'OUTPUT.LOG';    _con.Driver := 'sqlite3';    _con.Database := '../../data.sqlite3';  end;  Result := _con;end; finalization  FreeAndNil(_con); end. 

Zoran:
Thanks. Apparently, it's taken from the original author to a new maintainer. Can we expect it to be actively maintained?

Navigation

[0] Message Index

[#] Next page

Go to full version