<commit_before>unit DbfIndexedStorage;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils,
  FileUtil, LazLogger,
  dbf, db;

type
  TRecordCallback = procedure(args: Array of const) of object;

  TDbfIndexedStorage = Class(TComponent)
    private
      _isDebug: Boolean;
      _databasePath: String;
      _solutionsTable: TDbf;
      _projectsTable: TDbf;
      _filesTable: TDbf;
    private
      procedure CreateSolutionsTable;
      procedure CreateProjectsTable;
      procedure CreateFilesTable;
      procedure SetDatabasePath(path: String);
      procedure Log(message: String);
    public
      constructor Create(aOwner: TComponent); override;
      procedure CreateTables;

      function AppendSolution(title: String): Integer;

      function AppendProject(
        title: String;
        path: String;
        solutionId: Integer): Integer;

      function AppendFile(
        title: String;
        path: String;
        solutionId: Integer;
        projectId: Integer): Integer;

      procedure SolutionsDo(cb: TRecordCallback);
      procedure ProjectsDo(cb: TRecordCallback);
      procedure FilesDo(cb: TRecordCallback);

      property IsDebug : Boolean write _isDebug default False;
      property DatabasePath : String write SetDatabasePath;
  end;

implementation

constructor TDbfIndexedStorage.Create(aOwner: TComponent);
begin
  inherited Create(aOwner);
  _solutionsTable := TDbf.Create(self);
  _solutionsTable.TableLevel := 7;
  _solutionsTable.TableName := 'solutions.dbf';
  _projectsTable := TDbf.Create(self);
  _projectsTable.TableLevel := 7;
  _projectsTable.TableName := 'projects.dbf';
  _filesTable := TDbf.Create(self);
  _filesTable.TableLevel := 7;
  _filesTable.TableName := 'files.dbf';
end;

procedure TDbfIndexedStorage.CreateTables;
begin
  Log('create tables');
  ForceDirectories(_databasePath);
  CreateSolutionsTable;
  CreateProjectsTable;
  CreateFilesTable;
  Log('create tables done');
end;

function TDbfIndexedStorage.AppendSolution(title: String): Integer;
begin
  Log('append solution ' + title);
  with _solutionsTable do begin
    Open;
    Append;
    FieldByName('title').AsString := title;
    Post;
    Result := FieldByName('solution_id').AsInteger;
    Log(Format('solution #%d appended', [Result]));
    Close;
  end;
end;

function TDbfIndexedStorage.AppendProject(
  title: String;
  path: String;
  solutionId: Integer): Integer;
begin
  Log('append project ' + title);
  with _projectsTable do begin
    Open;
    Append;
    FieldByName('title').AsString := title;
    FieldByName('path').AsString := path;
    FieldByName('solution_id').AsInteger := solutionId;
    Post;
    Result := FieldByName('project_id').AsInteger;
    Log(Format('project #%d appended', [Result]));
    Close;
  end;
end;

function TDbfIndexedStorage.AppendFile(
  title: String;
  path: String;
  solutionId: Integer;
  projectId: Integer): Integer;
begin
  Log('append file ' + title);
  with _projectsTable do begin
    Open;
    Append;
    FieldByName('title').AsString := title;
    FieldByName('path').AsString := path;
    FieldByName('solution_id').AsInteger := solutionId;
    FieldByName('project_id').AsInteger := projectId;
    Post;
    Result := FieldByName('file_id').AsInteger;
    Log(Format('file #%d appended', [Result]));
    Close;
  end;
end;<commit_msg>Fix append file<commit_after>unit DbfIndexedStorage;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils,
  FileUtil, LazLogger,
  dbf, db;

type
  TRecordCallback = procedure(args: Array of const) of object;

  TDbfIndexedStorage = Class(TComponent)
    private
      _isDebug: Boolean;
      _databasePath: String;
      _solutionsTable: TDbf;
      _projectsTable: TDbf;
      _filesTable: TDbf;
    private
      procedure CreateSolutionsTable;
      procedure CreateProjectsTable;
      procedure CreateFilesTable;
      procedure SetDatabasePath(path: String);
      procedure Log(message: String);
    public
      constructor Create(aOwner: TComponent); override;
      procedure CreateTables;

      function AppendSolution(title: String): Integer;

      function AppendProject(
        title: String;
        path: String;
        solutionId: Integer): Integer;

      function AppendFile(
        title: String;
        path: String;
        solutionId: Integer;
        projectId: Integer): Integer;

      procedure SolutionsDo(cb: TRecordCallback);
      procedure ProjectsDo(cb: TRecordCallback);
      procedure FilesDo(cb: TRecordCallback);

      property IsDebug : Boolean write _isDebug default False;
      property DatabasePath : String write SetDatabasePath;
  end;

implementation

constructor TDbfIndexedStorage.Create(aOwner: TComponent);
begin
  inherited Create(aOwner);
  _solutionsTable := TDbf.Create(self);
  _solutionsTable.TableLevel := 7;
  _solutionsTable.TableName := 'solutions.dbf';
  _projectsTable := TDbf.Create(self);
  _projectsTable.TableLevel := 7;
  _projectsTable.TableName := 'projects.dbf';
  _filesTable := TDbf.Create(self);
  _filesTable.TableLevel := 7;
  _filesTable.TableName := 'files.dbf';
end;

procedure TDbfIndexedStorage.CreateTables;
begin
  Log('create tables');
  ForceDirectories(_databasePath);
  CreateSolutionsTable;
  CreateProjectsTable;
  CreateFilesTable;
  Log('create tables done');
end;

function TDbfIndexedStorage.AppendSolution(title: String): Integer;
begin
  Log('append solution ' + title);
  with _solutionsTable do begin
    Open;
    Append;
    FieldByName('title').AsString := title;
    Post;
    Result := FieldByName('solution_id').AsInteger;
    Log(Format('solution #%d appended', [Result]));
    Close;
  end;
end;

function TDbfIndexedStorage.AppendProject(
  title: String;
  path: String;
  solutionId: Integer): Integer;
begin
  Log('append project ' + title);
  with _projectsTable do begin
    Open;
    Append;
    FieldByName('title').AsString := title;
    FieldByName('path').AsString := path;
    FieldByName('solution_id').AsInteger := solutionId;
    Post;
    Result := FieldByName('project_id').AsInteger;
    Log(Format('project #%d appended', [Result]));
    Close;
  end;
end;

function TDbfIndexedStorage.AppendFile(
  title: String;
  path: String;
  solutionId: Integer;
  projectId: Integer): Integer;
begin
  Log('append file ' + title);
  with _filesTable do begin
    Open;
    Append;
    FieldByName('title').AsString := title;
    FieldByName('path').AsString := path;
    FieldByName('solution_id').AsInteger := solutionId;
    FieldByName('project_id').AsInteger := projectId;
    Post;
    Result := FieldByName('file_id').AsInteger;
    Log(Format('file #%d appended', [Result]));
    Close;
  end;
end;
{'fraction_non_alphanumeric': 0.10822965072702743, 'fraction_numerical': 0.000899415380002998, 'mean_word_length': 3.902277736958119, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3718205', 'n_tokens_mistral': 1991, 'n_tokens_neox': 1973, 'n_words': 477}