Created
January 20, 2021 12:51
-
-
Save alperdincer/c13278509aab4a731dcd58bdb64f5e15 to your computer and use it in GitHub Desktop.
Oracle delay problems
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function addAnnouncement (req, res, next) { | |
try { | |
const body = req.body; | |
const contents = { | |
title: body.title, | |
a_content: body.a_content, | |
start_date: body.start_date + " 00:01", | |
end_date: body.end_date + " 23:59", | |
userId : utils.decryptText(body.userId) | |
}; | |
const announcementResult = await announcementDBAPI.addAnnouncement(contents); | |
if (announcementResult.rowsAffected == 1) { | |
res.status(200).send({ success: true }); | |
} | |
else { | |
res.status(200).send({ success: false }); | |
} | |
} | |
catch (err) { | |
console.log(err); | |
console.log("Error-0057"); | |
res.status(200).send({ success : false }); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function addAnnouncement (context) { | |
const binds = { | |
title: context.title, | |
a_content: context.a_content, | |
start_date: context.start_date, | |
end_date: context.end_date, | |
userId: parseInt(context.userId) | |
}; | |
let insertSql = 'INSERT INTO announcements (title, a_content, start_date, end_date, created_by, created_at) VALUES '; | |
insertSql += '(:title, :a_content, TO_TIMESTAMP(:start_date, \'DD/MM/YYYY HH24:MI\'), TO_TIMESTAMP(:end_date, \'DD/MM/YYYY HH24:MI\'), :userId, SYSDATE)'; | |
const result = await database.simpleExecute(insertSql, binds); | |
return result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
` | |
CREATE TABLE announcements ( | |
id NUMBER GENERATED ALWAYS AS IDENTITY(START with 1 INCREMENT by 1) PRIMARY KEY, | |
title VARCHAR2(100), | |
a_content CLOB, | |
start_date TIMESTAMP, | |
end_date TIMESTAMP, | |
a_type NUMBER DEFAULT 0, | |
created_by NUMBER, | |
created_at TIMESTAMP, | |
deleted_by NUMBER | |
) | |
` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function getAnnouncements (context) { | |
let binds = { | |
}; | |
var selectSql = 'SELECT fl.id AS "id", fl.title AS "title", fl.a_content AS "a_content", ' + | |
' (SELECT fullname FROM users WHERE id = fl.created_by) AS "created_by_ref", fl.created_by AS "created_by", ' + | |
' TO_CHAR(fl.start_date, \'DD/MM/YYYY\') AS "start_date", ' + | |
' TO_CHAR(fl.end_date, \'DD/MM/YYYY\') AS "end_date", ' + | |
' TO_CHAR(fl.created_at, \'DD-MM-YYYY HH24:MI\') AS "created_at", ' + | |
' TO_CHAR(fl.updated_at, \'DD-MM-YYYY HH24:MI\') AS "updated_at", ' + | |
' CASE WHEN SYSDATE >= start_date AND SYSDATE <= end_date THEN 1 ' + | |
' WHEN start_date > SYSDATE AND end_date > SYSDATE THEN 2 ' + | |
' ELSE 0 ' + | |
' END AS "a_status", ' + | |
' CASE WHEN SYSDATE >= start_date AND SYSDATE <= end_date THEN \'Current\' ' + | |
' WHEN start_date > SYSDATE AND end_date > SYSDATE THEN \'Future\' ' + | |
' ELSE \'Past\' ' + | |
' END AS "a_status_ref", ' + | |
' (SELECT fullname FROM users WHERE id = fl.updated_by) AS "updated_by_ref", fl.updated_by AS "updated_by" ' + | |
' FROM announcements fl WHERE fl.deleted_by = 0 '; | |
selectSql += ' ORDER BY fl.id'; | |
const result = await database.simpleExecute(selectSql, binds); | |
return result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
await oracledb.createPool({ | |
user: user, | |
password: pass, | |
connectString: serverConfig.db.connectionString, | |
poolIncrement : 0, | |
poolMax : 4, | |
poolMin : 4, | |
poolPingInterval : 1 | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function simpleExecute(statement, binds = [], opts = {}) { | |
return new Promise(async (resolve, reject) => { | |
let conn; | |
opts.outFormat = oracledb.OBJECT; | |
opts.autoCommit = true; | |
try { | |
conn = await oracledb.getConnection(); | |
const result = await conn.execute(statement, binds, opts); | |
resolve(result); | |
} catch (err) { | |
console.log("---- DB ERROR -----"); | |
console.log(err); | |
console.log("---- DB ERROR -----"); | |
reject(err); | |
} finally { | |
if (conn) { | |
try { | |
await conn.close(); | |
} catch (err) { | |
console.log(err); | |
} | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment