@mysql/xdevapi
Version:
MySQL Connector/Node.js - A Node.js driver for MySQL using the X Protocol and X DevAPI.
218 lines (193 loc) • 7.14 kB
JavaScript
/*
* Copyright (c) 2018, 2023, Oracle and/or its affiliates.
*
* 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.
*
* Without limiting anything contained in the foregoing, this file,
* which is part of MySQL Connector/Node.js, is also subject to the
* Universal FOSS Exception, version 1.0, a copy of which can be found at
* http://oss.oracle.com/licenses/universal-foss-exception.
*
* 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
*/
;
const column = require('./Column');
const baseResult = require('./BaseResult');
/**
* Relational table API for retrieving data.
* @module RowResult
* @mixes module:BaseResult
*/
/**
* @private
* @alias module:RowResult
* @param {number} [index] - The current position of the result set iterator.
* @param {int64.Type} [integerType] - The convertion mode selected by the
* application to handle integer values in result sets for the current session.
* @param {Array<module:Column>} [metadata] - A list containing metadata of
* each column.
* @param {Array<Array<*>>} [results] - The list of rows (each one a list of
* column values in and of itself).
* @param {BigInt} [rowsAffected] - The number of rows affected by the
* statement.
* @param {Array<Warning>} [warnings] - The list of warnings generated by the
* statement.
* @returns {module:RowResult}
*/
function RowResult ({ index = 0, integerType, metadata = [], results = [], rowsAffected = 0n, warnings } = {}) {
return {
...baseResult({ warnings }),
/**
* Consume the current result set from memory (and flush it).
* @function
* @name module:RowResult#fetchAll
* @example
* table.select()
* .execute()
* .then(res => {
* // get the list of documents in the result set
* var rows = res.fetchAll()
* })
*
* session.sql("SELECT 'foo'")
* .execute()
* .then(res => {
* console.log(res.fetchAll()) // [['foo']]
* })
* @returns {Array<Array>} A list of rows.
*/
fetchAll () {
if (!results || !results.length) {
return [];
}
const current = results[index] || [];
if (current.length) {
results[index] = null;
}
// fetchOne() might have been called already.
const lastNullable = current.lastIndexOf(null);
const startIndex = lastNullable > -1 ? lastNullable + 1 : 0;
return current.slice(startIndex, current.length).map(row => row.toArray({ integerType }));
},
/**
* Consume a single result set row from memory (and flush it).
* @function
* @name module:RowResult#fetchOne
* @example
* table.select()
* .execute()
* .then(res => {
* // iterate over the documents in the result set
* while (var row = res.fetchOne()) {
* // do something with the current document
* }
* })
*
* session.sql("SELECT 'foo'")
* .execute()
* .then(res => {
* console.log(res.fetchOne()) // ['foo']
* })
* @returns {Array} A row.
*/
fetchOne () {
if (!results || !results.length) {
return;
}
const current = results[index] || [];
let i = 0;
while (i < current.length) {
if (current[i]) {
// consume the current item in the result set and deallocate the memory
const row = current[i];
current[i] = null;
if (i === current.length - 1) {
// the result set has been entirely consumed and the memory can be deallocated
results[index] = null;
}
return row.toArray({ integerType });
}
++i;
}
},
/**
* Retrieve the list of [columns]{@link module:Column} that are part of the result set.
* @function
* @name module:RowResult#getColumns
* @example
* session.sql("SELECT 'foo' AS name")
* .execute()
* .then(res => {
* var columns = res.getColumns()
* console.log(columns[0].getColumnLabel()) // name
* })
* @returns {Array<module:Column>} A list of [columns]{@link module:Column}.
*/
getColumns () {
const columns = metadata[index] || [];
return columns.map(m => column(m));
},
/**
* Retrieve the entire result set (without flushing).
* @function
* @name module:RowResult#getResults
* @returns {Array<Array<Array>>}
*/
getResults () {
return results;
},
/**
* Move to the next available result set.
* @function
* @name module:RowResult#nextResult
* @example
* // CREATE PROCEDURE proc() BEGIN
* // SELECT 'foo' as name;
* // SELECT 'bar' as name;
* // END
* session.sql('CALL proc()')
* .execute()
* .then(res => {
* // iterate over multiple result sets
* do {
* console.log(res.fetchOne())
* } while (res.nextResult())
* })
* @returns {boolean}
*/
nextResult () {
index += 1;
if (!results || !results.length || !results[index]) {
return false;
}
return true;
},
/**
* Returns the current result set (without flushing) as a JavaScript Arrray.
* @function
* @name module:RowResult#toArray
* @returns {Array}
*/
toArray () {
return results[index].map(row => row.toArray({ integerType }));
}
};
}
module.exports = RowResult;