const Provider = require('./Provider');

class SQLiteProvider extends Provider {
    constructor(db, tableName, dataColumn) {
        super();

        this.db = db;
        this.tableName = tableName;
        this.dataColumn = dataColumn;
    }

    init() {
        return this.db.all('SELECT * FROM $table', { $table: this.tableName }).then(rows => {
            for (const row of rows) {
                this.items.set(row.id, this.dataColumn ? JSON.parse(row[this.dataColumn]) : row);
            }
        });
    }

    get(id, key, defaultValue) {
        if (this.items.has(id)) {
            const value = this.items.get(id)[key];
            return value == null ? defaultValue : value;
        }

        return defaultValue;
    }

    set(id, key, value) {
        const data = this.items.get(id) || {};
        data[key] = value;
        this.items.set(id, data);

        if (this.dataColumn) {
            return this.db.run('REPLACE INTO $table (id, $key) VALUES ($id, $value)', {
                $table: this.tableName,
                $key: this.dataColumn,
                $id: id,
                $value: JSON.stringify(data)
            });
        }

        return this.db.run('REPLACE INTO $table (id, $key) VALUES ($id, $value)', {
            $table: this.tableName,
            $key: key,
            $id: id,
            $value: value
        });
    }

    delete(id, key) {
        const data = this.items.get(id) || {};
        delete data[key];

        if (this.dataColumn) {
            return this.db.run('REPLACE INTO $table (id, $key) VALUES ($id, $value)', {
                $table: this.tableName,
                $key: this.dataColumn,
                $id: id,
                $value: JSON.stringify(data)
            });
        }

        return this.db.run('REPLACE INTO $table (id, $key) VALUES ($id, $value)', {
            $table: this.tableName,
            $key: key,
            $id: id,
            $value: null
        });
    }

    clear(id) {
        this.items.delete(id);
        return this.db.run('DELETE FROM $table WHERE id = $id', {
            $table: this.tableName,
            $id: id
        });
    }
}

module.exports = SQLiteProvider;

@@ -280,17 +280,17 @@
 .db.all(
-'
+%60
 SELECT *
@@ -300,26 +300,9 @@
 OM $
-table', %7B $table: 
+%7B
 this
@@ -311,18 +311,18 @@
 ableName
- 
 %7D
+%60
 ).then(r
@@ -910,33 +910,33 @@
 urn this.db.run(
-'
+%60
 REPLACE INTO $ta
@@ -929,39 +929,64 @@
 PLACE INTO $
-table (id, $key
+%7Bthis.tableName%7D (id, $%7Bthis.dataColumn%7D
 ) VALUES ($i
@@ -999,92 +999,13 @@
 lue)
-'
+%60
 , %7B%0A
-                $table: this.tableName,%0A                $key: this.dataColumn,%0A
     
@@ -1116,33 +1116,33 @@
 urn this.db.run(
-'
+%60
 REPLACE INTO $ta
@@ -1135,39 +1135,52 @@
 PLACE INTO $
-table
+%7Bthis.tableName%7D
  (id, $
+%7B
 key
+%7D
 ) VALUES ($i
@@ -1193,72 +1193,13 @@
 lue)
-'
+%60
 , %7B%0A
-            $table: this.tableName,%0A            $key: key,%0A
     
@@ -1410,33 +1410,33 @@
 urn this.db.run(
-'
+%60
 REPLACE INTO $ta
@@ -1429,39 +1429,64 @@
 PLACE INTO $
-table (id, $key
+%7Bthis.tableName%7D (id, $%7Bthis.dataColumn%7D
 ) VALUES ($i
@@ -1499,92 +1499,13 @@
 lue)
-'
+%60
 , %7B%0A
-                $table: this.tableName,%0A                $key: this.dataColumn,%0A
     
@@ -1624,17 +1624,17 @@
 .db.run(
-'
+%60
 REPLACE 
@@ -1643,23 +1643,36 @@
 TO $
-table
+%7Bthis.tableName%7D
  (id, $
+%7B
 key
+%7D
 ) VA
@@ -1693,72 +1693,13 @@
 lue)
-'
+%60
 , %7B%0A
-            $table: this.tableName,%0A            $key: key,%0A
     
@@ -1833,17 +1833,17 @@
 .db.run(
-'
+%60
 DELETE F
@@ -1847,21 +1847,32 @@
 E FROM $
-table
+%7Bthis.tableName%7D
  WHERE i
@@ -1882,76 +1882,20 @@
  $id
-', %7B%0A            $table: this.tableName,%0A            $id: id%0A       
+%60, %7B $id: id
  %7D);

Remove incorrect placeholders