UNPKG

orange-orm

Version:

Object Relational Mapper

113 lines (104 loc) 2.8 kB
const wrapQuery = require('./wrapQuery'); const wrapCommand = require('./wrapCommand'); const encodeBoolean = require('./encodeBoolean'); const deleteFromSql = require('./deleteFromSql'); const selectForUpdateSql = require('./selectForUpdateSql'); const lastInsertedSql = require('./lastInsertedSql'); const limitAndOffset = require('./limitAndOffset'); const insertSql = require('./insertSql'); const insert = require('./insert'); const formatBigintOut = require('./formatBigintOut'); const formatDateOut = require('./formatDateOut'); const formatDateIn = require('./formatDateIn'); const quote = require('./quote'); function newResolveTransaction(domain, pool, { readonly = false } = {}) { var rdb = {poolFactory: pool}; if (!pool.connect) { pool = pool(); rdb.pool = pool; } rdb.begin = 'SET TRANSACTION ISOLATION LEVEL READ COMMITTED'; rdb.engine = 'oracle'; rdb.encodeBoolean = encodeBoolean; rdb.decodeJSON = decodeJSON; rdb.encodeJSON = JSON.stringify; rdb.formatDateOut = formatDateOut; rdb.formatDateIn = formatDateIn; rdb.formatBigintOut = formatBigintOut; rdb.deleteFromSql = deleteFromSql; rdb.selectForUpdateSql = selectForUpdateSql; rdb.lastInsertedSql = lastInsertedSql; rdb.insertSql = insertSql; rdb.insert = insert; rdb.lastInsertedIsSeparate = true; rdb.multipleStatements = false; rdb.limitAndOffset = limitAndOffset; rdb.accept = function(caller) { caller.visitSqlite(); }; rdb.aggregateCount = 0; rdb.quote = quote; rdb.cache = {}; rdb.changes = []; if (readonly) { rdb.dbClient = { executeQuery: function(query, callback) { pool.connect((err, client, done) => { if (err) { return callback(err); } try { wrapQuery(domain, client)(query, (err, res) => { done(); callback(err, res); }); } catch (e) { done(); callback(e); } }); }, executeCommand: function(query, callback) { pool.connect((err, client, done) => { if (err) { return callback(err); } try { wrapCommand(domain, client)(query, (err, res) => { done(); callback(err, res); }); } catch (e) { done(); callback(e); } }); } }; domain.rdb = rdb; return (onSuccess) => onSuccess(); } return function(onSuccess, onError) { pool.connect(onConnected); function onConnected(err, client, done) { try { if (err) { onError(err); return; } client.executeQuery = wrapQuery(domain, client); client.executeCommand = wrapCommand(domain, client); rdb.dbClient = client; rdb.dbClientDone = done; domain.rdb = rdb; onSuccess(); } catch (e) { onError(e); } } }; } function decodeJSON(value) { return JSON.parse(value); } module.exports = newResolveTransaction;