mirror of
https://github.com/GothenburgBitFactory/timewarrior.git
synced 2025-07-07 20:06:39 +02:00
#9 TI-1: Add TransactionFactory
- CmdUndo pops last transaction from array (and applies undo actions - tbd) - TransactionFactory reads contents of undo.data into transaction array - Transaction array is written back to undo.data
This commit is contained in:
parent
d8a4b8ff43
commit
76ee22e7e5
8 changed files with 175 additions and 0 deletions
|
@ -14,6 +14,7 @@ set (timew_SRCS CLI.cpp CLI.h
|
|||
Range.cpp Range.h
|
||||
Rules.cpp Rules.h
|
||||
Transaction.cpp Transaction.h
|
||||
TransactionsFactory.cpp TransactionsFactory.h
|
||||
UndoAction.cpp UndoAction.h
|
||||
data.cpp
|
||||
dom.cpp
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
#include <sstream>
|
||||
#include <iterator>
|
||||
#include <iomanip>
|
||||
#include <TransactionsFactory.h>
|
||||
#include <ctime>
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -236,6 +237,45 @@ void Database::recordIntervalAction (
|
|||
recordUndoAction ("interval", before, after);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
Transaction Database::popLastTransaction ()
|
||||
{
|
||||
File undo (_location + "/undo.data");
|
||||
|
||||
std::vector <std::string> read_lines;
|
||||
undo.read (read_lines);
|
||||
undo.close ();
|
||||
|
||||
TransactionsFactory transactionsFactory;
|
||||
|
||||
for (auto& line: read_lines)
|
||||
{
|
||||
transactionsFactory.parseLine (line);
|
||||
}
|
||||
|
||||
std::vector <Transaction> transactions = transactionsFactory.get ();
|
||||
|
||||
if (transactions.empty ())
|
||||
{
|
||||
return Transaction {};
|
||||
}
|
||||
|
||||
Transaction last = transactions.back ();
|
||||
transactions.pop_back ();
|
||||
|
||||
File::remove (undo._data);
|
||||
undo.open ();
|
||||
|
||||
for (auto& transaction : transactions)
|
||||
{
|
||||
undo.append (transaction.toString ());
|
||||
}
|
||||
|
||||
undo.close ();
|
||||
|
||||
return last;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
std::string Database::dump () const
|
||||
{
|
||||
|
|
|
@ -54,6 +54,8 @@ public:
|
|||
void recordConfigAction(const std::string&, const std::string&);
|
||||
void recordIntervalAction(const std::string&, const std::string&);
|
||||
|
||||
Transaction popLastTransaction ();
|
||||
|
||||
std::string dump () const;
|
||||
|
||||
private:
|
||||
|
|
|
@ -35,6 +35,11 @@ void Transaction::addUndoAction (
|
|||
_actions.emplace_back (type, before, after);
|
||||
}
|
||||
|
||||
std::vector<UndoAction> Transaction::getActions ()
|
||||
{
|
||||
return _actions;
|
||||
}
|
||||
|
||||
std::string Transaction::toString ()
|
||||
{
|
||||
std::string output = "txn:\n";
|
||||
|
|
|
@ -37,6 +37,8 @@ public:
|
|||
|
||||
std::string toString();
|
||||
|
||||
std::vector<UndoAction> getActions ();
|
||||
|
||||
private:
|
||||
std::vector<UndoAction> _actions {};
|
||||
};
|
||||
|
|
59
src/TransactionsFactory.cpp
Normal file
59
src/TransactionsFactory.cpp
Normal file
|
@ -0,0 +1,59 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright 2018, Thomas Lauf, Paul Beckingham, Federico Hernandez.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included
|
||||
// in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// https://www.opensource.org/licenses/mit-license.php
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <vector>
|
||||
#include "TransactionsFactory.h"
|
||||
#include "Transaction.h"
|
||||
|
||||
void TransactionsFactory::parseLine (const std::string& line)
|
||||
{
|
||||
if (!line.compare(0, 4, "txn:"))
|
||||
{
|
||||
_transactions.emplace_back ();
|
||||
}
|
||||
else if (!line.compare(0, 7, " type:"))
|
||||
{
|
||||
_type = line.substr (8, line.size ());
|
||||
}
|
||||
else if (!line.compare(0, 9, " before:"))
|
||||
{
|
||||
_before = line.substr (10, line.size ());
|
||||
}
|
||||
else if (!line.compare(0, 8, " after:"))
|
||||
{
|
||||
_after = line.substr (9, line.size ());
|
||||
_transactions.back ().addUndoAction (_type, _before, _after);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw "Cannot handle line '" + line + "'";
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Transaction> TransactionsFactory::get ()
|
||||
{
|
||||
return _transactions;
|
||||
}
|
49
src/TransactionsFactory.h
Normal file
49
src/TransactionsFactory.h
Normal file
|
@ -0,0 +1,49 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright 2018, Thomas Lauf, Paul Beckingham, Federico Hernandez.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included
|
||||
// in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// https://www.opensource.org/licenses/mit-license.php
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef INCLUDED_TRANSACTIONSFACTORY
|
||||
#define INCLUDED_TRANSACTIONSFACTORY
|
||||
|
||||
#include <string>
|
||||
#include <Transaction.h>
|
||||
|
||||
class TransactionsFactory
|
||||
{
|
||||
public:
|
||||
void parseLine(const std::string& line);
|
||||
|
||||
std::vector< Transaction > get();
|
||||
|
||||
private:
|
||||
std::string _type;
|
||||
std::string _before;
|
||||
std::string _after;
|
||||
|
||||
std::vector< Transaction > _transactions {};
|
||||
};
|
||||
|
||||
|
||||
#endif //INCLUDED_TRANSACTIONSFACTORY
|
|
@ -31,6 +31,23 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
int CmdUndo (Database& database)
|
||||
{
|
||||
Transaction transaction = database.popLastTransaction ();
|
||||
|
||||
std::vector <UndoAction> actions = transaction.getActions ();
|
||||
|
||||
if (actions.empty ())
|
||||
{
|
||||
// No (more) undoing...
|
||||
}
|
||||
else
|
||||
{
|
||||
for (auto& action : actions)
|
||||
{
|
||||
// Select database...
|
||||
// Rollback action...
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue