Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Memos for Visual FoxPro #118

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Memo/Creator/AbstractMemoCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function getExtension(): string
public function createFile(): string
{
$pi = pathinfo($this->table->filepath);
$memoFilepath = sprintf('%s/%s.%s', $pi['dirname'], $pi['filename'], self::getExtension());
$memoFilepath = sprintf('%s/%s.%s', $pi['dirname'], $pi['filename'], $this->getExtension());

$stream = Stream::createFromFile($memoFilepath, 'wb+');
$this->writeHeader($stream);
Expand Down
24 changes: 24 additions & 0 deletions src/Memo/Creator/FoxProMemoCreator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

namespace XBase\Memo\Creator;

use XBase\Stream\Stream;

class FoxProMemoCreator extends AbstractMemoCreator
{
public static function getExtension(): string
{
return 'fpt';
}

protected function writeHeader(Stream $stream): void
{
$stream->write(pack('N',8)); //next block

$stream->seek(4);
$stream->write(str_pad('',2,chr(0))); //reserved

$stream->seek(6);
$stream->write(pack('n',64)); //Block size. 8 * 64 == 512, which will start records after the header
}
}
d4mation marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions src/Memo/Creator/MemoCreatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public static function create(Table $table)
case TableType::DBASE_7_MEMO:
return new DBase7MemoCreator($table);
//todo foxpro
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we delete this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The todo? That I'm not sure, actually. I don't know anything about the other FoxPro versions and I've found that documentation is a bit scarce. I'm not sure if my changes here will work properly for the other versions :/

case TableType::VISUAL_FOXPRO:
return new FoxProMemoCreator($table);
default:
throw new \Exception('Memo creator not realized for table version '.$table->getVersion());
}
Expand Down
4 changes: 3 additions & 1 deletion src/Memo/MemoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use XBase\DataConverter\Encoder\EncoderInterface;
use XBase\Enum\TableType;
use XBase\Table\Table;
use XBase\Memo\Creator\MemoCreatorFactory;

class MemoFactory
{
Expand All @@ -25,7 +26,8 @@ public static function create(Table $table, EncoderInterface $encoder): ?MemoInt
}
$memoFilepath = $fileInfo['dirname'].DIRECTORY_SEPARATOR.$fileInfo['filename'].$memoExt;
if (!file_exists($memoFilepath)) {
return null; //todo create file?
$memo_creator = MemoCreatorFactory::create($table);
Copy link
Collaborator

@gam6itko gam6itko Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the DB does not have memo fields, then a memo file will still be created. It is not right. TableWriter should be responsible for creating files. MemoFactory is part of TableReader. If memo file missing (but it should be), we need to pass this info to MemoFactory of use something like fixer-tool

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, you're right. Without that line changed in MemoFactory it is creating the Memo file. I'm not sure why I thought changing that was necessary 🤔

Anyway, currently TableWriter appears to always create an empty Memo file if the TableType supports Memos.

TableEditor has a nice method for grabbing any set Memo Columns, but TableWriter is its own Class that doesn't have access to TableEditor's methods so we cannot currently use that. Would it be appropriate to move that method to a Trait so that both TableEditor and TableWriter can use it?

If TableWriter had that method, we could have it check whether or not the Memo file needs to be created instead of always creating it.

$memo_creator->createFile();
}

return $refClass->newInstance($table, $memoFilepath, $encoder);
Expand Down