diff --git a/app.js b/app.js index e9e99fa..5bf2252 100644 --- a/app.js +++ b/app.js @@ -88,11 +88,50 @@ global.syzoj = { }, async connectDatabase() { let Sequelize = require('sequelize'); + let Op = Sequelize.Op; + let operatorsAliases = { + $eq: Op.eq, + $ne: Op.ne, + $gte: Op.gte, + $gt: Op.gt, + $lte: Op.lte, + $lt: Op.lt, + $not: Op.not, + $in: Op.in, + $notIn: Op.notIn, + $is: Op.is, + $like: Op.like, + $notLike: Op.notLike, + $iLike: Op.iLike, + $notILike: Op.notILike, + $regexp: Op.regexp, + $notRegexp: Op.notRegexp, + $iRegexp: Op.iRegexp, + $notIRegexp: Op.notIRegexp, + $between: Op.between, + $notBetween: Op.notBetween, + $overlap: Op.overlap, + $contains: Op.contains, + $contained: Op.contained, + $adjacent: Op.adjacent, + $strictLeft: Op.strictLeft, + $strictRight: Op.strictRight, + $noExtendRight: Op.noExtendRight, + $noExtendLeft: Op.noExtendLeft, + $and: Op.and, + $or: Op.or, + $any: Op.any, + $all: Op.all, + $values: Op.values, + $col: Op.col + }; + this.db = new Sequelize(this.config.db.database, this.config.db.username, this.config.db.password, { host: this.config.db.host, dialect: this.config.db.dialect, storage: this.config.db.storage ? this.utils.resolvePath(this.config.db.storage) : null, - logging: syzoj.production ? false : syzoj.log + logging: syzoj.production ? false : syzoj.log, + operatorsAliases: operatorsAliases }); global.Promise = Sequelize.Promise; this.db.countQuery = async (sql, options) => (await this.db.query(`SELECT COUNT(*) FROM (${sql}) AS \`__tmp_table\``, options))[0][0]['COUNT(*)']; diff --git a/models/common.js b/models/common.js index 43e78a1..19207f1 100644 --- a/models/common.js +++ b/models/common.js @@ -19,6 +19,8 @@ 'use strict'; +let Sequelize = require('sequelize'); + class Model { constructor(record) { this.record = record; @@ -29,9 +31,9 @@ class Model { let model = this.getModel(); let obj = JSON.parse(JSON.stringify(this.record.get({ plain: true }))); for (let key in obj) { - if (model.tableAttributes[key].json) { + if (model.tableAttributes[key].type instanceof Sequelize.JSON) { try { - this[key] = eval(`(${obj[key]})`); + this[key] = JSON.parse(obj[key]); } catch (e) { this[key] = {}; } @@ -43,8 +45,7 @@ class Model { let model = this.getModel(); let obj = JSON.parse(JSON.stringify(this.record.get({ plain: true }))); for (let key in obj) { - if (model.tableAttributes[key].json) obj[key] = JSON.stringify(this[key]); - else obj[key] = this[key]; + obj[key] = this[key]; } return obj; } @@ -76,7 +77,7 @@ class Model { } static async fromID(id) { - return this.fromRecord(this.model.findById(id)) + return this.fromRecord(this.model.findByPk(id)); } static async findOne(options) { diff --git a/models/contest_player.js b/models/contest_player.js index b559dec..ae8e707 100644 --- a/models/contest_player.js +++ b/models/contest_player.js @@ -31,7 +31,7 @@ let model = db.define('contest_player', { user_id: { type: Sequelize.INTEGER }, score: { type: Sequelize.INTEGER }, - score_details: { type: Sequelize.TEXT, json: true }, + score_details: { type: Sequelize.JSON }, time_spent: { type: Sequelize.INTEGER } }, { timestamps: false, @@ -53,7 +53,7 @@ class ContestPlayer extends Model { contest_id: 0, user_id: 0, score: 0, - score_details: '{}', + score_details: {}, time_spent: 0 }, val))); } diff --git a/models/contest_ranklist.js b/models/contest_ranklist.js index 09970dd..9a3316e 100644 --- a/models/contest_ranklist.js +++ b/models/contest_ranklist.js @@ -28,8 +28,8 @@ let ContestPlayer = syzoj.model('contest_player'); let model = db.define('contest_ranklist', { id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true }, - ranking_params: { type: Sequelize.TEXT, json: true }, - ranklist: { type: Sequelize.TEXT, json: true } + ranking_params: { type: Sequelize.JSON }, + ranklist: { type: Sequelize.JSON } }, { timestamps: false, tableName: 'contest_ranklist' @@ -39,8 +39,8 @@ let Model = require('./common'); class ContestRanklist extends Model { static async create(val) { return ContestRanklist.fromRecord(ContestRanklist.model.build(Object.assign({ - ranking_params: '{}', - ranklist: '{}' + ranking_params: {}, + ranklist: {} }, val))); } diff --git a/models/custom_test.js b/models/custom_test.js index 5b1b5bf..6b7d638 100644 --- a/models/custom_test.js +++ b/models/custom_test.js @@ -38,7 +38,7 @@ let model = db.define('custom_test', { pending: { type: Sequelize.BOOLEAN }, memory: { type: Sequelize.INTEGER }, - result: { type: Sequelize.TEXT('medium'), json: true }, + result: { type: Sequelize.JSON }, user_id: { type: Sequelize.INTEGER }, @@ -76,6 +76,7 @@ class CustomTest extends Model { time: 0, memory: 0, + result: {}, status: 'Waiting', }, val))); } diff --git a/models/judge_state.js b/models/judge_state.js index 5fb5edc..63a7e61 100644 --- a/models/judge_state.js +++ b/models/judge_state.js @@ -45,9 +45,9 @@ let model = db.define('judge_state', { max_memory: { type: Sequelize.INTEGER }, // For NOI contest - compilation: { type: Sequelize.TEXT('medium'), json: true }, + compilation: { type: Sequelize.JSON }, - result: { type: Sequelize.TEXT('medium'), json: true }, + result: { type: Sequelize.JSON }, user_id: { type: Sequelize.INTEGER }, @@ -107,7 +107,8 @@ class JudgeState extends Model { total_time: null, max_memory: null, status: 'Unknown', - result: null, + compilation: {}, + result: {}, task_id: randomstring.generate(10), is_public: false }, val))); diff --git a/package.json b/package.json index e255ac2..bc9614f 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "moment": "^2.15.0", "msgpack-lite": "^0.1.26", "multer": "^1.2.0", - "mysql": "^2.11.1", + "mysql2": "^1.6.2", "node-7z": "^0.4.0", "nodemailer": "^4.1.0", "pygmentize-bundled-cached": "^1.1.0", @@ -54,7 +54,7 @@ "request": "^2.74.0", "request-promise": "^4.1.1", "sendmail": "^1.1.1", - "sequelize": "^3.24.3", + "sequelize": "^4.41.0", "session-file-store": "^1.0.0", "socket.io": "^2.0.3", "sqlite3": "^3.1.4",