| 1060 | |
| 1061 | /** |
| 1062 | * Start an SQL transaction if and only if none is already entered into |
| 1063 | * Note: This function is useless unless you use MySQL's InnoDB or NDBCluster engines |
| 1064 | * |
| 1065 | * Using this function, alongside rollback() and commit() below, will allows to work around the following MySQL quirk: |
| 1066 | * |
| 1067 | * START TRANSACTION; |
| 1068 | * SELECT * FROM foo; -- 2 rows |
| 1069 | * DELETE FROM foo WHERE bar1; -- delete 1 row |
| 1070 | * DELETE FROM foo WHERE bar2; -- MySQL complains and returns an error |
| 1071 | * COMMIT; |
| 1072 | * SELECT * FROM foo; -- 1 row, instead of the 2 you'd get in PGSQL or TSQL |
| 1073 | * |
| 1074 | * @param string $tx_id a unique transaction id that may wrap several sql statements, e.g. 'delete_post_3' |
| 1075 | * @return true on transaction start |
| 1076 | **/ |
| 1090 | * Signal the end of a transaction that spans multiple database statements. |
| 1091 | * |
| 1092 | * Commits if and only if the specified transaction id is that of the first transaction that was started. |
| 1093 | * |
| 1094 | * @param string $tx_id a unique transaction id that may wrap several sql statements, e.g. 'delete_post_3' |
| 1095 | * @return true on commit |
| 1096 | **/ |
| 1097 | |
| 1098 | function commit($tx_id) { |
| 1099 | if ( isset($this->tx_id) && $tx_id && $this->tx_id == $tx_id ) { |
| 1100 | $this->tx_id = false; |
| 1101 | $this->query("COMMIT;"); |
| 1102 | return true; |
| 1103 | } else { |
| 1104 | // still in a transaction, or invalid tx_id |
| 1105 | return false; |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | /** |
| 1110 | * Rollback the current transaction and die with the error message |
| 1111 | * Note: If you've any use for this, you actually *want* WP to die on a failed transaction, rather than catch the error, because it would only ever get called when a workflow error occurs. |
| 1112 | * |
| 1113 | * @param string $message Error message. |
| 1114 | * @param string $title Error title. |
| 1115 | * @param string|array $args Optional arguements to control behaviour. |
| 1116 | * @return void |
| 1117 | **/ |
| 1118 | |
| 1119 | function rollback($message, $title = '', $args = array()) { |
| 1120 | if ( $this->tx_id ) { |
| 1121 | $this->tx_id = false; |
| 1122 | $wpdb->query("ROLLBACK;"); |
| 1123 | } |
| 1124 | // die no matter what, to highlight the workflow error |
| 1125 | wp_die($message, $title, $args); |
| 1126 | } |
| 1127 | |
| 1128 | /** |