Skip to content

Commit

Permalink
updated and cleaned code & readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackstareye committed Jun 27, 2021
1 parent dc350d5 commit 5372099
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 22 deletions.
72 changes: 67 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
<li>
<a href="#getting-started">Getting Started</a>
</li>
<li><a href="#usage">Usage</a></li>
<li><a href="#backstory">Backstory</a></li>
<li><a href="#methods">Methods</a></li>
<li><a href="#roadmap">Roadmap</a></li>
<li><a href="#contributing">Contributing</a></li>
<li><a href="#license">License</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#acknowledgements">Acknowledgements</a></li>
<li><a href="#support">support</a></li>
</ol>
</details>

Expand All @@ -43,7 +44,7 @@ This PHP class will give you the ability to use
* vanilla sql
* prepared sql

queries in a transaction based way. So you are able to manual rollback or commit your queries.
queries in a transaction based way **in wordpress**. So you are able to manual rollback or commit your queries.

I also implemented a rollback if there were any errors in the execution of the query.
In this case, a rollback will be executed and an error state will be set.
Expand Down Expand Up @@ -91,12 +92,73 @@ Yeah my usecase is an not so common thing todo in wordpress, but I needed it an

### Methods

#### constructor

If you have a logging handler (like Monolog) you can initialize it here and uncomment the logging lines in the code.

#### resetError

resets the error state.

#### setError

Sets the Manager in error state. Now it is not possible to perform any transaction based queries.

#### error

Increases the error count and (if set) writes an info into the logging file.

#### beginTransaction

This starts the transaction mode. Now every query is not directly commited.

It uses the standard mysql **START TRANSACTION** command.

#### rollback

**Undo's** the queries that were run before between using begin transaction and rollback.

#### commit

Makes your executed queries persistent.

#### performTransaction_use_prepare

Runs an [prepared](https://developer.wordpress.org/reference/classes/wpdb/prepare//) SQL query. If it fails it rollbacks. And enables the error state.

#### performTransaction_use_query

Executes the query in transaction mode.
If it fails it rollbacks. And enables the error state.

#### performTransaction_insert

Executes the [wp->insert](https://developer.wordpress.org/reference/classes/wpdb/insert/) in transaction mode.
If it fails it rollbacks. And enables the error state.

#### performTransaction_update

Executes the [wp->update](https://developer.wordpress.org/reference/classes/wpdb/update/) in transaction mode.
If it fails it rollbacks. And enables the error state.

#### performTransaction_delete

Executes the [wp->delete](https://developer.wordpress.org/reference/classes/wpdb/delete/) in transaction mode.
If it fails it rollbacks. And enables the error state.

#### testTransactionModule

This tests if the transaction mode is possible with your underlying db.
You have to use xDebug and your database client so you can monitor if everything runs well.

Only after a commit, the data should be visible.
Neither after a rollback nor before commiting.

## Roadmap

* make the readme prettier
* develop a wordpress plugin with filter-hooks for transaction handling.

*
See the [open issues](https://github.com/Blackstareye/wp_transactionmanager/issues) for a list of proposed features (and known issues).

<!-- CONTRIBUTING -->
Expand Down Expand Up @@ -124,7 +186,7 @@ Blackeye - [@BlackeyeM](https://twitter.com/BlackeyeM) - private_blackeye+transa
Project Link: [https://github.com/Blackstareye/wp_transactionmanager](https://github.com/Blackstareye/wp_transactionmanager)

<!-- ACKNOWLEDGEMENTS -->
## Support / Donation
## Support

If you like what I am doing, you can support me with a little Tip / Donation on those pages:

Expand Down
40 changes: 23 additions & 17 deletions TransactionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,32 @@ function __construct()
// if you want to use a logger, just instantiate it here and uncommend the loggerlines
//$this->logger = your_logger_handler;
}
/**
* resets the error
*/
public function resetError()
{
$this->error_state = false;
}
/**
* sets the modul into error state
*/
public function setError()
{
$this->error_state = true;
}
/**
* perform tasks that are need to be done if there was an error
*/
private function error()
{

// $this->logger->warning("Warning ErrorState not solved. Please refer to the log. Transaction aborted.");
$this->error_count++;
}
/**
* start a transaction
*/
public function beginTransaction()
{
if ($this->error_state) {
Expand All @@ -46,7 +58,6 @@ public function beginTransaction()

global $wpdb;
try {
//code...
$wpdb->query('START TRANSACTION');
} catch (\Throwable $e) {
// $this->logger->warning("Database Transaction could not be established!", ["error" => $e]);
Expand All @@ -55,6 +66,11 @@ public function beginTransaction()
throw $e;
}
}

/**
* @param $second_order don't use that on your own. It's for errors in a rollback
* undo's queries
*/
public function rollback($second_order = false)
{
if ($second_order) {
Expand All @@ -63,7 +79,6 @@ public function rollback($second_order = false)
}
global $wpdb;
try {
//code...
$wpdb->query("ROLLBACK");
// $this->logger->info("Rollback performed.");
} catch (\Throwable $e) {
Expand All @@ -73,6 +88,9 @@ public function rollback($second_order = false)
throw $e;
}
}
/**
* commits changes
*/
public function commit()
{
global $wpdb;
Expand All @@ -89,11 +107,8 @@ public function commit()
/**
* data needs to be sanitized if use prepare is not used. Cannot use LIKE in prepare mode
*
* @param $type
* @param $query_string
* @param array $data
* @param boolean $use_prepare
* @return void
* @param $query_string needs %s or other placeholders in the query
* @param array $data size(amount of placeholders of the query)
*/
public function performTransaction_use_prepare($query_string, array $data)
{
Expand All @@ -114,12 +129,8 @@ public function performTransaction_use_prepare($query_string, array $data)
}
/**
* data needs to be sanitized
*
* @param $type
* executes the query in transaction mode
* @param $query_string
* @param array $data
* @param boolean $use_prepare
* @return void
*/
public function performTransaction_use_query($query_string)
{
Expand All @@ -143,7 +154,6 @@ public function performTransaction_use_query($query_string)
* @param [type] $tablename
* @param array $data
* @param array $formats like %s for the values that will be inserted
* @return void
*/
public function performTransaction_insert($tablename, array $data, array $formats = null)
{
Expand Down Expand Up @@ -175,7 +185,6 @@ public function performTransaction_insert($tablename, array $data, array $forma
* @param array $where
* @param array $formats like %s for the values that will be updated
* @param array $where_formats like %s for the values that will be updated
* @return void
*/
public function performTransaction_update($tablename, array $data, array $where, array $formats = null, array $where_formats = null)
{
Expand Down Expand Up @@ -211,10 +220,8 @@ public function performTransaction_update($tablename, array $data, array $where
*
* performs a transaction based delete.
* @param [type] $tablename
* @param array $data
* @param array $where
* @param array $where_formats like %s for the values that will be deleted
* @return void
*/
public function performTransaction_delete($tablename, array $where, array $where_formats = null)
{
Expand Down Expand Up @@ -243,7 +250,6 @@ public function performTransaction_delete($tablename, array $where, array $wher
* this tests if transactions are possible
* this needs to be done with a xDebug. It would be possible to do that with sql queries but this could be an IMPROVEMENT
*
* @return void
*/
public function testTransactionModule($test_table_name) {
$this->beginTransaction();
Expand Down

0 comments on commit 5372099

Please sign in to comment.