forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjscrund_dbspi.js
164 lines (139 loc) · 5.4 KB
/
jscrund_dbspi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights
reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
'use strict';
var spi = require('../Adapter/impl/SPI.js'),
dbt_module = require('../Adapter/impl/common/DBTableHandler.js'),
DBTableHandler = dbt_module.DBTableHandler,
unified_debug = require('../Adapter/api/unified_debug.js'),
udebug = unified_debug.getLogger('jscrund_dbspi.js');
function implementation() {
};
implementation.prototype = {
dbServiceProvider : null,
dbConnPool : null,
dbSession : null,
inBatchMode : false,
operations : null
};
implementation.prototype.getDefaultProperties = function(adapter) {
this.dbServiceProvider = spi.getDBServiceProvider(adapter);
return this.dbServiceProvider.getDefaultConnectionProperties();
};
implementation.prototype.close = function(callback) {
var impl = this;
impl.dbSession.close(function() { impl.dbConnPool.close(callback); });
};
implementation.prototype.initialize = function(options, callback) {
udebug.log("initialize");
var impl = this;
var mappings = options.annotations;
var nmappings = mappings.length;
function getMapping(n) {
function gotMapping(err, tableMetadata) {
udebug.log("gotMapping", n);
nmappings--;
var dbt = new DBTableHandler(tableMetadata, mappings[n].prototype.mynode.mapping,
mappings[n]);
udebug.log("Got DBTableHandler", dbt);
mappings[n].dbt = dbt;
if(nmappings == 0) {
callback(null); /* All done */
}
}
impl.dbConnPool.getTableMetadata(options.properties.database,
mappings[n].prototype.mynode.mapping.table,
impl.dbSession, gotMapping);
}
function onDbSession(err, dbSession) {
var n;
if(err) { callback(err, null); }
else {
impl.dbSession = dbSession;
if(mappings.length) {
for(n = 0 ; n < mappings.length ; n++) { getMapping(n); }
}
else {
callback(null);
}
}
}
function onConnect(err, dbConnectionPool) {
impl.dbConnPool = dbConnectionPool;
if(err) { callback(err, null); }
else {
dbConnectionPool.getDBSession(1, onDbSession);
}
}
impl.dbServiceProvider.connect(options.properties, onConnect);
};
implementation.prototype.execOneOperation = function(op, tx, callback, row) {
if(this.inBatchMode) {
this.operations.push(op);
}
else {
tx.execute([op], function(err) { if(err) console.log("TX EXECUTE ERR:", err, row); });
}
};
implementation.prototype.persist = function(parameters, callback) {
udebug.log_detail('persist object:', parameters.object);
var dbt = parameters.object.constructor.dbt;
var tx = this.dbSession.getTransactionHandler();
var op = this.dbSession.buildInsertOperation(dbt, parameters.object, tx, callback);
this.execOneOperation(op, tx, callback, parameters.object);
};
implementation.prototype.find = function(parameters, callback) {
udebug.log_detail('find key:', parameters.key);
var dbt = parameters.object.constructor.dbt;
var tx = this.dbSession.getTransactionHandler();
var index = dbt.getIndexHandler(parameters.key, true);
var op = this.dbSession.buildReadOperation(index, parameters.key, tx, callback);
this.execOneOperation(op, tx, callback);
};
implementation.prototype.remove = function(parameters, callback) {
udebug.log_detail('remove key:', parameters.key);
var dbt = parameters.object.constructor.dbt;
var tx = this.dbSession.getTransactionHandler();
var index = dbt.getIndexHandler(parameters.key, true);
var op = this.dbSession.buildDeleteOperation(index, parameters.key, tx, callback);
this.execOneOperation(op, tx, callback);
};
implementation.prototype.createBatch = function(callback) {
udebug.log_detail('createBatch');
this.operations = [];
this.inBatchMode = true;
callback(null);
};
implementation.prototype.executeBatch = function(callback) {
udebug.log_detail('executeBatch');
this.inBatchMode = false;
this.dbSession.getTransactionHandler().execute(this.operations, callback);
};
implementation.prototype.begin = function(callback) {
udebug.log_detail('begin');
this.dbSession.begin();
callback(null);
};
implementation.prototype.commit = function(callback) {
udebug.log_detail('commit');
this.dbSession.commit(callback);
};
exports.implementation = implementation;