Code (PHP) pasted on 2019-01-13, 13:21 Raw Source
- <?php
- //$securePath = '../00_private';
- $db = new PDO('sqlite:' . $securePath . '/finances.sqlite3');
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- try {
- PRAGMA journal_mode = wal;
- PRAGMA foreign_keys = ON;
- ");
- CREATE TABLE IF NOT EXISTS account (
- id INTEGER PRIMARY KEY,
- name TEXT
- );
- CREATE UNIQUE INDEX idx_account_name ON account(name);
- ");
- CREATE TABLE IF NOT EXISTS payer (
- id INTEGER PRIMARY KEY,
- name TEXT
- );
- CREATE UNIQUE INDEX idx_payer_name ON payer(name);
- ");
- CREATE TABLE IF NOT EXISTS payment (
- id INTEGER PRIMARY KEY,
- account INTEGER,
- payer INTEGER,
- subject TEXT,
- date NUMERIC,
- amount NUMERIC,
- type TEXT,
- FOREIGN KEY(account) REFERENCES account(id) ON UPDATE CASCADE ON DELETE RESTRICT,
- FOREIGN KEY(payer) REFERENCES payer(id) ON UPDATE CASCADE ON DELETE RESTRICT
- );
- CREATE INDEX idx_payment_amount ON payment(date, amount);
- CREATE INDEX idx_payment_subject ON payment(date, subject);
- ");
- CREATE TABLE IF NOT EXISTS category (
- id INTEGER PRIMARY KEY,
- pid INTEGER,
- name TEXT
- );
- CREATE UNIQUE INDEX idx_category_name ON category(name);
- ");
- CREATE TABLE IF NOT EXISTS payment2category (
- id INTEGER PRIMARY KEY,
- paymentID INTEGER,
- categoryID INTEGER,
- FOREIGN KEY(paymentID) REFERENCES payment(id) ON UPDATE CASCADE ON DELETE RESTRICT,
- FOREIGN KEY(categoryID) REFERENCES category(id) ON UPDATE CASCADE ON DELETE RESTRICT
- );
- CREATE INDEX idx_p2c_pc ON payment2category(paymentID, categoryID);
- CREATE INDEX idx_p2c_cp ON payment2category(categoryID, paymentID);
- ");
- } catch (PDOException $e) {
- echo $e->getMessage();
- }