Edward Baer
06/15/2021, 4:35 PMjanpio
janpio
Edward Baer
06/15/2021, 8:54 PMEdward Baer
06/15/2021, 8:56 PMgetSequenceBlockPrisma: sql: ${sql});
result = await prisma.$executeRaw(sql);
console.log('getSequenceBlockPrisma: result:', result);
sql = `SELECT @newSeqStart, @newSeqEnd FROM DUAL`;
result = await prisma.$queryRaw(sql);
console.log(getSequenceBlockPrisma: sql: ${sql});
console.log(getSequenceBlockPrisma: result:, result);Edward Baer
06/15/2021, 8:56 PMEdward Baer
06/15/2021, 8:57 PMEdward Baer
06/15/2021, 8:58 PMjanpio
janpio
Edward Baer
06/15/2021, 9:06 PMEdward Baer
06/15/2021, 9:07 PMjanpio
Edward Baer
06/15/2021, 9:33 PMgetSequenceBlockPrisma: callGetSequenceBlock:, callGetSequenceBlock);
console.log(getSequenceBlockPrisma: getStartEnd:, getStartEnd);
} catch (error) {
throw error;
}
return data;
};
I get this:
Error: Error in connector: Server terminated the connection.Edward Baer
06/15/2021, 9:37 PMjanpio
janpio
Edward Baer
06/15/2021, 9:50 PMEdward Baer
06/15/2021, 9:52 PMjanpio
Edward Baer
06/15/2021, 9:56 PMEdward Baer
06/15/2021, 9:58 PMEdward Baer
06/15/2021, 10:04 PMEdward Baer
06/15/2021, 10:05 PMMelvin Gaye
08/10/2021, 9:52 PMEdward Baer
08/11/2021, 12:43 PMconst getSequenceBlock = async (sequenceName, addCount = 1) => {
let data = false;
try {
const [callGetSequenceBlock, getStartEnd] = await prisma.$transaction([
prisma.$executeRaw(`CALL GetSequenceBlock('${sequenceName}', ${addCount}, @start, @end);`),
// prisma.$queryRaw(`SELECT @start, @end, @newSeqStart, @newSeqEnd FROM DUAL;`),
prisma.$queryRaw(`SELECT @newSeqStart, @newSeqEnd FROM DUAL;`),
]);
// console.log(`getSequenceBlockPrisma: callGetSequenceBlock:`, callGetSequenceBlock);
// console.log(`getSequenceBlockPrisma: getStartEnd:`, getStartEnd);
// Successful result comes back as [ { '@newSeqStart': 146, '@newSeqEnd': 151 } ]
if (Array.isArray(getStartEnd)) {
const row = getStartEnd[0];
if (typeof row === 'object') {
data = {
start: row['@newSeqStart'],
end: row['@newSeqEnd'],
};
}
}
} catch (error) {
throw error;
}
return data;
};
And here is the Stored Procedure I am calling:
CREATE DEFINER=`ach`@`%` PROCEDURE `GetSequenceBlock`(
IN sequenceName VARCHAR(100),
IN addCount BIGINT,
OUT start BIGINT,
OUT end BIGINT
)
SQL SECURITY INVOKER
BEGIN
DECLARE newSeqStart BIGINT;
DECLARE newSeqEnd BIGINT;
DECLARE normalSequence BIGINT;
SET @normalSequence = LOWER(sequenceName);
SELECT currentValue INTO @currentValue FROM Sequence WHERE name = @normalSequence;
IF @currentValue IS NOT NULL THEN
UPDATE Sequence
SET temp = @newSeqStart := currentValue, currentValue = @newSeqEnd := currentValue + addCount
WHERE name = @normalSequence;
ELSE
SET @newSeqStart = NULL;
SET @newSeqEnd = NULL;
END IF;
SELECT @newSeqStart INTO start;
SELECT @newSeqEnd INTO end;
SELECT @newSeqStart, @newSeqEnd;
END
It is a "Poor Man's" Sequence, because MySQL doesn't have Sequences.
This structure seems to work correctly, but you might not need the IN/OUT pieces.
I believe the trick is the final SELECT in the Stored Procedure must match what you are looking for in the Result set.
ie: @newSeqStart, @newSeqEnd.
The result set return structure is rather weird, and you need to console.log or dump it out to figure out where your actual data is.
I haven't tried it with multiple row returns, so this may not be your solution.
However, I am pretty sure you just make what you want back as the last SELECT in the Stored Procedure.
If you have multiple SELECTS, they will each come out, so be sure to put them into something, then SELECT them at the end...Melvin Gaye
08/11/2021, 1:09 PMEdward Baer
08/11/2021, 1:11 PMRyan
08/12/2021, 6:24 AMEdward Baer
08/12/2021, 1:15 PMRyan
08/12/2021, 1:19 PMEdward Baer
08/12/2021, 1:23 PMMelvin Gaye
08/14/2021, 1:01 PM