🔒 Prepare SQL statements

This commit is contained in:
C-3PO 2018-11-09 02:45:36 +01:00
parent 334b52f4c6
commit 48d5e2e3c4
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
4 changed files with 29 additions and 28 deletions

View file

@ -1,15 +1,15 @@
import { getSolidpkg, Product, getManifest } from 'ssn'; import { getManifest, Product } from 'ssn';
import IProductData from "./interfaces/IProductData"; import IProductData from './interfaces/IProductData';
import * as model from './model/model';
import * as database from './model/database';
import writeLog from './logger/writeLog'; import writeLog from './logger/writeLog';
import * as database from './model/database';
import * as model from './model/model';
//create lock file to prevent conflicts? //create lock file to prevent conflicts?
const newPatches: any[] = []; const newPatches: any[] = [];
(async function() { (async function() {
const productData: { [key in Product]: IProductData } = await model.init(); const productData: { [key in Product]?: IProductData } = await model.init();
const productDataEntries = Object.entries(productData) as Array<[Product, IProductData]>; const productDataEntries = Object.entries(productData) as Array<[Product, IProductData]>;
await Promise.all(productDataEntries.map(async ([product, { manifest, http}]) => { await Promise.all(productDataEntries.map(async ([product, { manifest, http}]) => {
@ -29,8 +29,7 @@ const newPatches: any[] = [];
})); }));
database.exit(); database.exit();
}()) }());
/*//for each product, check new versions /*//for each product, check new versions
Object.entries(products).forEach(([product, { manifestVersion, httpVersion }]: [string, IDatabaseRow]) => { Object.entries(products).forEach(([product, { manifestVersion, httpVersion }]: [string, IDatabaseRow]) => {

View file

@ -1,5 +1,5 @@
import { Product } from "ssn"; import { Product } from 'ssn';
import IProductData from "./IProductData"; import IProductData from './IProductData';
export default interface IDatabaseRow extends IProductData { export default interface IDatabaseRow extends IProductData {
/** Name of the product. */ /** Name of the product. */

View file

@ -1,18 +1,15 @@
import * as mysql from 'mysql';
import { Product } from 'ssn'; import { Product } from 'ssn';
import * as database from './database';
import products from './products';
import writeLog from '../logger/writeLog';
import createStatement from './createStatement';
import IDatabaseRow from '../interfaces/IDatabaseRow'; import IDatabaseRow from '../interfaces/IDatabaseRow';
import IProductData from '../interfaces/IProductData'; import IProductData from '../interfaces/IProductData';
import writeLog from '../logger/writeLog';
import createStatement from './createStatement';
import * as database from './database';
import products from './products';
const state: any = {}; export async function init(): Promise<{ [key in Product]?: IProductData }> {
const output: { [key in Product]?: IProductData } = {};
function escapeProduct(product: Product) {
return String(product).replace(/'/g, '\\\'');
}
export async function init(): Promise<{ [key in Product]: IProductData }> {
await database.init(); await database.init();
//Only create table if it does not exist yet //Only create table if it does not exist yet
@ -27,18 +24,22 @@ export async function init(): Promise<{ [key in Product]: IProductData }> {
return; return;
} }
state[row.product] = { http: row.http, manifest: row.manifest }; output[row.product] = { http: row.http, manifest: row.manifest };
}); });
//If row does not exist, INSERT into table //If row does not exist, INSERT into table
await Promise.all(products.filter((product) => state[product] === undefined).map(function(product) { await Promise.all(products.filter((product) => output[product] === undefined).map(function(product) {
state[product] = { http: -1, manifest: -1 }; output[product] = { http: -1, manifest: -1 };
return database.query(`INSERT INTO \`ssn_versions\` (\`product\`, \`manifest\`, \`http\`) VALUES ('${escapeProduct(product)}', '-1', '-1');`) const queryTemplate = `INSERT INTO \`ssn_versions\` (\`product\`, \`manifest\`, \`http\`) VALUES (?, '-1', '-1');`;
const query = mysql.format(queryTemplate, [String(product)]);
return database.query(query);
})); }));
return state; return output;
} }
export function updateManifestVersion(product: Product, newVersion: number) { export function updateManifestVersion(product: Product, newVersion: number) {
return database.query(`UPDATE \`ssn_versions\` SET \`manifest\` = '${Number(newVersion)}' WHERE \`product\` = '${escapeProduct(product)}' COLLATE utf8mb4_bin;`); const queryTemplate = `UPDATE \`ssn_versions\` SET \`manifest\` = ? WHERE \`product\` = ?;`;
const query = mysql.format(queryTemplate, [Number(newVersion), String(product)]);
return database.query(query);
} }

View file

@ -1,7 +1,7 @@
{ {
"defaultSeverity": "error", "defaultSeverity": "error",
"extends": [ "extends": [
"tslint:recommended", "tslint:recommended"
], ],
"jsRules": {}, "jsRules": {},
"rules": { "rules": {
@ -12,7 +12,8 @@
"no-console": false, "no-console": false,
"no-invalid-this": [true, "check-function-in-method"], "no-invalid-this": [true, "check-function-in-method"],
"object-literal-sort-keys": [true, "match-declaration-order"], "object-literal-sort-keys": [true, "match-declaration-order"],
"quotemark": [true, "single"], "only-arrow-functions": false,
"quotemark": [true, "single"]
}, },
"rulesDirectory": [], "rulesDirectory": []
} }