3 var mariaSql = require('mariasql');
4 var JL = require('jsnlog').JL;
5 var logger = JL('mariadb.js');
6 var _ = require('lodash');
16 var mariadb = mariadb || {};
20 'insert into tic_job ',
21 '(job_status, job_deleted, job_hasksfile, job_updater, job_uptime, job_register, job_regtime) ',
22 'values ("READY", false, false, "tic", now(), "tic", now());'
25 'select tic_job.job_id job_id, ',
26 'tic_job.job_status job_status, ',
27 'tic_job.job_image_id job_image_id, ',
28 'tic_image.image_name job_image_name, ',
29 'tic_image.image_size job_image_size, ',
30 'tic_job.job_hasksfile job_hasksfile, ',
31 'tic_job.job_uptime job_uptime ',
33 'left join tic_image on tic_job.job_image_id = tic_image.image_id ',
34 'where tic_job.job_deleted = false ',
35 'and job_id = <%= strJobId %>;'
37 'getJobsTotalCount': [
38 'select count(job_id) as total_count ',
40 'where job_deleted = false;'
43 'select tic_job.job_id job_id, ',
44 'tic_job.job_status job_status, ',
45 'tic_job.job_image_id job_image_id, ',
46 'tic_image.image_name job_image_name, ',
47 'tic_image.image_size job_image_size, ',
48 'tic_job.job_hasksfile job_hasksfile, ',
49 'tic_job.job_uptime job_uptime ',
51 'left join tic_image on tic_job.job_image_id = tic_image.image_id ',
52 'where tic_job.job_deleted = false ',
53 'order by job_id desc ',
54 'limit <%= startNum %> , 10;'
57 'select tic_job.job_image_id image_id, ',
58 'tic_job.job_id image_job_id, ',
59 'tic_image.image_name image_name, ',
60 'tic_image.image_size image_size, ',
61 'tic_job.job_hasksfile image_hasksfile, ',
62 'tic_job.job_status image_status, ',
63 'tic_job.job_uptime image_uptime ',
64 'from tic_job inner join tic_image ',
65 'where tic_job.job_image_id = tic_image.image_id ',
66 'and tic_job.job_deleted = false ',
67 'order by tic_job.job_id desc ',
68 'limit <%= startNum %> , 10;'
70 'getImagesTotalCount': [
71 'select count(image_id) as total_count ',
75 'insert into tic_image ',
81 '"<%= imageType %>", ',
87 mariadb.doQuery = function doQuery (queryString, callback) {
88 return mariaSqlClient.query(queryString, callback);
97 mariadb.editJob = function addJob (req, res) {
98 var queryString, strJobId, reqParam,
99 job_status, job_deleted, job_hasksfile, job_image_id;
101 function onSuccess (err, rows) {
107 logger.info('editJob.success');
111 strJobId = req.params.id;
113 job_status = reqParam.job_status;
114 job_deleted = reqParam.job_deleted;
115 job_hasksfile = reqParam.job_hasksfile;
116 job_image_id = reqParam.job_image_id;
118 queryString = 'update tic_job set';
119 if (job_image_id) queryString += ' job_image_id = "' + job_image_id + '",';
120 if (job_status) queryString += ' job_status = "' + job_status + '",';
121 if (job_deleted) queryString += ' job_deleted = ' + job_deleted + ',';
122 if (job_hasksfile) queryString += ' job_hasksfile = ' + job_hasksfile + ',';
123 queryString += ' job_updater = "tic",';
124 queryString += ' job_uptime = now()';
125 queryString += ' where job_id = ' + strJobId + ';';
127 logger.info('editJob: query = ' + queryString);
129 this.doQuery(queryString, onSuccess);
135 mariadb.addJob = function addJob (req, res) {
138 function onSuccess (err, rows) {
144 logger.info('addJob.success: {job_id: ' + rows.info.insertId + '}');
146 job_id: rows.info.insertId
150 queryString = _.join(this.queries['addJob'], '');
152 logger.info('addJob: query = ' + queryString);
155 this.doQuery(queryString, onSuccess);
161 mariadb.getJobById = function getJobById (req, res) {
162 var queryString, strJobId;
164 function onSuccess (err, rows) {
172 strJobId = req.params.id;
174 queryString = _.template(_.join(this.queries['getJobById'], ''))({
178 logger.info('getJobById: query = ' + queryString);
181 this.doQuery(queryString, onSuccess);
185 * Get Total Count of Job
187 mariadb.getJobsTotalCount = function getJobsTotalCount (req, res) {
190 function onSuccess (err, rows) {
198 queryString = _.join(this.queries['getJobsTotalCount'], '');
200 logger.info('getJobsTotalCount: query = ' + queryString);
203 this.doQuery(queryString, onSuccess);
209 mariadb.getJobsAllList = function getJobsAllList (req, res) {
210 var queryString, reqParam, pageNum, startNum;
212 function onSuccess (err, rows) {
222 pageNum = reqParam.pageNum;
223 startNum = (pageNum - 1) * 10;
225 queryString = _.template(_.join(this.queries['getJobsAllList'], ''))({
229 logger.info('getJobsAllList: query = ' + queryString);
232 this.doQuery(queryString, onSuccess);
243 mariadb.getImagesAllList = function getImagesAllList (req, res) {
244 var queryString, reqParam, pageNum, startNum;
246 function onSuccess (err, rows) {
256 pageNum = reqParam.pageNum;
257 startNum = (pageNum - 1) * 10;
259 queryString = _.template(_.join(this.queries['getImagesAllList'], ''))({
263 logger.info('getImagesAllList: query = ' + queryString);
266 this.doQuery(queryString, onSuccess);
270 * Get Total Count of Job
272 mariadb.getImagesTotalCount = function getImagesTotalCount (req, res) {
275 function onSuccess (err, rows) {
283 queryString = _.join(this.queries['getImagesTotalCount'], '');
285 logger.info('getImagesTotalCount: query = ' + queryString);
288 this.doQuery(queryString, onSuccess);
294 mariadb.addImage = function addImage (req, res) {
295 var queryString, paramObj, imagePid;
297 function onSuccess (err, rows) {
303 logger.info('addJob: result = {image_id: ' + rows.info.insertId + '}');
305 image_id: rows.info.insertId
310 imagePid = paramObj.imagePid;
312 queryString = _.template(_.join(this.queries['addImage'], ''))({
313 imageType: 'external',
317 logger.info('addJob: query = ' + queryString);
320 this.doQuery(queryString, onSuccess);
324 mariadb.editImage = function editImage (req, res) {
325 var queryString, strJobId, reqParam,
326 job_status, job_deleted, job_hasksfile, job_image_id;
328 function onSuccess (err, rows) {
334 logger.info('editJob.success');
338 strJobId = req.params.id;
340 job_status = reqParam.job_status;
341 job_deleted = reqParam.job_deleted;
342 job_hasksfile = reqParam.job_hasksfile;
343 job_image_id = reqParam.job_image_id;
345 queryString = 'update tic_job set';
346 if (job_image_id) queryString += ' job_image_id = "' + job_image_id + '",';
347 if (job_status) queryString += ' job_status = "' + job_status + '",';
348 if (job_deleted) queryString += ' job_deleted = ' + job_deleted + ',';
349 if (job_hasksfile) queryString += ' job_hasksfile = ' + job_hasksfile + ',';
350 queryString += ' job_updater = "tic",';
351 queryString += ' job_uptime = now()';
352 queryString += ' where job_id = ' + strJobId + ';';
354 logger.info('editJob: query = ' + queryString);
356 this.doQuery(queryString, onSuccess);
365 mariadb.connectToClient = function connectToClient (){
366 return new Promise(function (resolve, reject) {
368 mariaSqlClient.connect(function (err) {
370 logger.error('connection error');
374 logger.info('connection success');
382 mariadb.createClient = function createClient () {
383 return new Promise(function (resolve, reject) {
384 logger.info('create client');
399 mariaSqlClient = new mariaSql(mariaSqlConfig);
407 mariadb.init = function init () {
412 .then(self.connectToClient);
417 module.exports = mariadb;