Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: SOF3/Capital
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: SOF3/Capital
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: archive
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 1 commit
  • 7 files changed
  • 1 contributor

Commits on Jan 8, 2022

  1. Archive (incomplete)

    SOF3 committed Jan 8, 2022

    Verified

    This commit was signed with the committer’s verified signature.
    SOF3 Jonathan Chan Kwan Yin
    Copy the full SHA
    48ba7ed View commit details
Showing with 151 additions and 10 deletions.
  1. +4 −0 README.md
  2. +62 −2 resources/mysql/init.sql
  3. +1 −1 resources/sqlite/account.sql
  4. +17 −0 resources/sqlite/archive.sql
  5. +3 −3 resources/sqlite/init.sql
  6. +18 −0 src/SOFe/Capital/Database/Config.php
  7. +46 −4 src/SOFe/Capital/Database/RawQueries.php
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Capital

[![CI](https://github.com/SOF3/Capital/actions/workflows/ci.yml/badge.svg)](https://github.com/SOF3/Capital/actions/workflows/ci.yml)


A simple but very extensible economy plugin for PocketMine-MP.

## How is Capital different from other economy plugins?
64 changes: 62 additions & 2 deletions resources/mysql/init.sql
Original file line number Diff line number Diff line change
@@ -21,6 +21,15 @@ CREATE TABLE IF NOT EXISTS acc_label (
FOREIGN KEY (id) REFERENCES acc(id) ON DELETE CASCADE
);
-- #&
CREATE TABLE IF NOT EXISTS acc_archive (
archive_batch BIGINT NOT NULL,
id CHAR(36) NOT NULL,
value BIGINT NOT NULL,
touch TIMESTAMP NOT NULL,
labels TEXT NOT NULL,
PRIMARY KEY (archive_batch, id)
);
-- #&
CREATE TABLE IF NOT EXISTS tran (
id CHAR(36) PRIMARY KEY,
src CHAR(36) NULL,
@@ -42,6 +51,17 @@ CREATE TABLE IF NOT EXISTS tran_label (
KEY (name, value),
FOREIGN KEY (id) REFERENCES tran(id) ON DELETE CASCADE
);
-- #&
CREATE TABLE IF NOT EXISTS tran_archive (
archive_batch BIGINT NOT NULL,
id CHAR(36) NOT NULL,
src CHAR(36) NULL,
dest CHAR(36) NULL,
value BIGINT NOT NULL,
touch TIMESTAMP NOT NULL,
labels TEXT NOT NULL,
PRIMARY KEY (archive_batch, id)
);
-- # }
-- # { procedures
-- # { tran_create
@@ -70,8 +90,8 @@ CREATE PROCEDURE tran_create (
ELSE
SET param_status = 0;

UPDATE acc SET value = var_src_value WHERE id = param_src;
UPDATE acc SET value = var_dest_value WHERE id = param_dest;
UPDATE acc SET value = var_src_value, touch = CURRENT_TIMESTAMP WHERE id = param_src;
UPDATE acc SET value = var_dest_value, touch = CURRENT_TIMESTAMP WHERE id = param_dest;

INSERT INTO tran (id, src, dest, value)
VALUES (param_id, param_src, param_dest, param_delta);
@@ -115,6 +135,46 @@ CREATE PROCEDURE tran_create_2 (
END IF;
END
-- # }
-- # { archive_acc
CREATE PROCEDURE acc_archive (
IN param_expiry INT,
IN param_batch_id BIGINT,
OUT param_num_rows BIGINT
) BEGIN
START TRANSACTION;

INSERT INTO acc_archive (archive_batch, id, value, touch, labels)
SELECT param_batch_id, id, acc.value, touch,
GROUP_CONCAT(acc_label.name, '=', acc_label.value SEPARATOR '\0')
FROM acc LEFT JOIN acc_label USING (id)
WHERE touch < DATE_SUB(NOW(), INTERVAL param_expiry SECOND);

DELETE FROM acc_label WHERE id IN (SELECT id FROM acc_archive WHERE archive_batch = param_batch_id);
DELETE FROM acc WHERE id IN (SELECT id FROM acc_archive WHERE archive_batch = param_batch_id);

COMMIT;
END
-- # }
-- # { archive_tran
CREATE PROCEDURE tran_archive (
IN param_expiry INT,
IN param_batch_id BIGINT,
OUT param_num_rows BIGINT
) BEGIN
START TRANSACTION;

INSERT INTO tran_archive (archive_batch, id, value, touch, labels)
SELECT param_batch_id, id, tran.value, touch,
GROUP_CONCAT(tran_label.name, '=', tran_label.value SEPARATOR '\0')
FROM tran LEFT JOIN tran_label USING (id)
WHERE touch < DATE_SUB(NOW(), INTERVAL param_expiry SECOND);

DELETE FROM tran_label WHERE id IN (SELECT id FROM tran_archive WHERE archive_batch = param_batch_id);
DELETE FROM tran WHERE id IN (SELECT id FROM tran_archive WHERE archive_batch = param_batch_id);

COMMIT;
END
-- # }
-- # }
-- # }
-- # }
2 changes: 1 addition & 1 deletion resources/sqlite/account.sql
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ INSERT INTO acc (id, value) VALUES (:id, :value);
-- # { delta
-- # :id string
-- # :delta int
UPDATE acc SET value = value + :delta WHERE id = :id;
UPDATE acc SET value = value + :delta, touch = CURRENT_TIMESTAMP WHERE id = :id;
-- # }
-- # }
-- # { fetch
17 changes: 17 additions & 0 deletions resources/sqlite/archive.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- #!sqlite
-- #{ capital
-- # { archive
-- # { account
-- # { delete
-- # :expiry int
DELETE FROM acc WHERE JULIANDAY(CURRENT_TIMESTAMP) - JULIANDAY(touch) > :expiry / 86400.0;
-- # }
-- # }
-- # { transaction
-- # { delete
-- # :expiry int
DELETE FROM tran WHERE JULIANDAY(CURRENT_TIMESTAMP) - JULIANDAY(touch) > :expiry / 86400.0;
-- # }
-- # }
-- # }
-- #}
6 changes: 3 additions & 3 deletions resources/sqlite/init.sql
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ CREATE TABLE IF NOT EXISTS acc_label (
PRIMARY KEY (id, name),
FOREIGN KEY (id) REFERENCES acc(id) ON DELETE CASCADE
);
-- # &
-- #&
CREATE INDEX IF NOT EXISTS acc_label_kv ON acc_label(name, value);
-- #&
CREATE TABLE IF NOT EXISTS tran (
@@ -31,7 +31,7 @@ CREATE TABLE IF NOT EXISTS tran (
FOREIGN KEY (src) REFERENCES acc(id) ON DELETE SET NULL,
FOREIGN KEY (dest) REFERENCES acc(id) ON DELETE SET NULL
);
-- # &
-- #&
CREATE INDEX IF NOT EXISTS tran_created ON tran(created);
-- #&
CREATE TABLE IF NOT EXISTS tran_label (
@@ -42,7 +42,7 @@ CREATE TABLE IF NOT EXISTS tran_label (
PRIMARY KEY (id, name),
FOREIGN KEY (id) REFERENCES tran(id) ON DELETE CASCADE
);
-- # &
-- #&
CREATE INDEX IF NOT EXISTS tran_label_kv ON tran_label(name, value);
-- # }
-- # }
18 changes: 18 additions & 0 deletions src/SOFe/Capital/Database/Config.php
Original file line number Diff line number Diff line change
@@ -15,10 +15,22 @@ final class Config {
/**
* @param array<string, mixed> $libasynql libasynql config.
* @param bool $logQueries Whether to log queries to the console.
* @param int $accountArchiveTime The time in seconds after which an account is moved to the archive table.
* @param int $accountArchiveInterval The interval in ticks between account archiving cycles.
* @param bool $deleteAccountInsteadOfArchive Permanently delete accounts instead of archiving them. Does not affect existing archive.
* @param int $transactionArchiveTime The time in seconds after which an transaction is moved to the archive table.
* @param int $transactionArchiveInterval The interval in ticks between transaction archiving cycles.
* @param bool $deleteTransactionInsteadOfArchive Permanently delete transactions instead of archiving them. Does not affect existing archive.
*/
public function __construct(
public array $libasynql,
public bool $logQueries,
public int $accountArchiveTime,
public int $accountArchiveInterval,
public bool $deleteAccountInsteadOfArchive,
public int $transactionArchiveTime,
public int $transactionArchiveInterval,
public bool $deleteTransactionInsteadOfArchive,
) {}

public static function default(TypeMap $typeMap) : self {
@@ -29,6 +41,12 @@ public static function default(TypeMap $typeMap) : self {
return new self(
libasynql: $config,
logQueries: true,
accountArchiveTime: 86400 * 30, // 30 days
accountArchiveInterval: 20 * 86400, // every day
deleteAccountInsteadOfArchive: false,
transactionArchiveTime: 86400 * 7, // 7 days
transactionArchiveInterval: 20 * 86400, // every day
deleteTransactionInsteadOfArchive: false,
);
}
}
50 changes: 46 additions & 4 deletions src/SOFe/Capital/Database/RawQueries.php
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

/*
* Auto-generated by libasynql-fx
* Created from init.sql, transaction.sql, account.sql, init.sql, transaction.sql, account.sql
* Created from init.sql, transaction.sql, account.sql, init.sql, archive.sql, transaction.sql, account.sql
*/

declare(strict_types=1);
@@ -146,7 +146,49 @@ public function accountSqliteUnsafeDelta(string $id, int $delta, ) : Generator {

/**
* <h4>Declared in:</h4>
* - resources/mysql/init.sql:80
* - resources/sqlite/archive.sql:8
* @param int $expiry
* @return Generator<mixed, 'all'|'once'|'race'|'reject'|'resolve'|array{'resolve'}|Generator<mixed, mixed, mixed, mixed>|null, mixed, int>
*/
public function archiveAccountDelete(int $expiry, ) : Generator {
$this->conn->executeChange("capital.archive.account.delete", ["expiry" => $expiry, ], yield Await::RESOLVE, yield Await::REJECT);
return yield Await::ONCE;
}

/**
* <h4>Declared in:</h4>
* - resources/sqlite/archive.sql:14
* @param int $expiry
* @return Generator<mixed, 'all'|'once'|'race'|'reject'|'resolve'|array{'resolve'}|Generator<mixed, mixed, mixed, mixed>|null, mixed, int>
*/
public function archiveTransactionDelete(int $expiry, ) : Generator {
$this->conn->executeChange("capital.archive.transaction.delete", ["expiry" => $expiry, ], yield Await::RESOLVE, yield Await::REJECT);
return yield Await::ONCE;
}

/**
* <h4>Declared in:</h4>
* - resources/mysql/init.sql:157
* @return Generator<mixed, 'all'|'once'|'race'|'reject'|'resolve'|array{'resolve'}|Generator<mixed, mixed, mixed, mixed>|null, mixed, int>
*/
public function initMysqlProceduresArchiveAcc() : Generator {
$this->conn->executeChange("capital.init.mysql.procedures.archive_acc", [], yield Await::RESOLVE, yield Await::REJECT);
return yield Await::ONCE;
}

/**
* <h4>Declared in:</h4>
* - resources/mysql/init.sql:177
* @return Generator<mixed, 'all'|'once'|'race'|'reject'|'resolve'|array{'resolve'}|Generator<mixed, mixed, mixed, mixed>|null, mixed, int>
*/
public function initMysqlProceduresArchiveTran() : Generator {
$this->conn->executeChange("capital.init.mysql.procedures.archive_tran", [], yield Await::RESOLVE, yield Await::REJECT);
return yield Await::ONCE;
}

/**
* <h4>Declared in:</h4>
* - resources/mysql/init.sql:100
* @return Generator<mixed, 'all'|'once'|'race'|'reject'|'resolve'|array{'resolve'}|Generator<mixed, mixed, mixed, mixed>|null, mixed, int>
*/
public function initMysqlProceduresTranCreate() : Generator {
@@ -156,7 +198,7 @@ public function initMysqlProceduresTranCreate() : Generator {

/**
* <h4>Declared in:</h4>
* - resources/mysql/init.sql:117
* - resources/mysql/init.sql:137
* @return Generator<mixed, 'all'|'once'|'race'|'reject'|'resolve'|array{'resolve'}|Generator<mixed, mixed, mixed, mixed>|null, mixed, int>
*/
public function initMysqlProceduresTranCreate2() : Generator {
@@ -166,7 +208,7 @@ public function initMysqlProceduresTranCreate2() : Generator {

/**
* <h4>Declared in:</h4>
* - resources/mysql/init.sql:45
* - resources/mysql/init.sql:65
* @return Generator<mixed, 'all'|'once'|'race'|'reject'|'resolve'|array{'resolve'}|Generator<mixed, mixed, mixed, mixed>|null, mixed, int>
*/
public function initMysqlTables() : Generator {