Cannot create stored procedure with sequelize - javascript

I am running the following SQL query:
Executing (default): DROP PROCEDURE IF EXISTS x;
DELIMITER $$
CREATE PROCEDURE x()
BEGIN
# do something
END $$
DELIMITER; $$
Like so:
sequelize.query(query, {
type: sequelize.QueryTypes.SELECT
});
But I get this error (even though it works just fine, if I use any other SQL client):
SequelizeDatabaseError:
ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near 'DELIMITER $$
CREATE PROCEDURE x()
BEGIN
I already added multipleStatements = true to my connection config, but it does not do anything.
Update
I ended up using RAW instead, like so:
sequelize.query(query, {
type: sequelize.QueryTypes.RAW
});

DELIMITER is not part of MySQL server, it's part of the MySQL command line client. This feature is not available in the driver.
Source: https://github.com/mysqljs/mysql/issues/280#issuecomment-8081626

Your error is actually in the last line. Take out the $$ after DELIMITER;
The statement: DELIMITER; resets the delimiter to ; the $$ after that confuses the syntax analyzer.

Related

How to insert JSON string to mysql with node.js script?

I have a string like this:
{
WABrowserId: '"hTCl7asDPe/EIymrQC57Hg=="',
WASecretBundle: '{"key":"o65mhfj8XZpuCIIgamAdsaibobs0HKiiBCwTAn36qms=","encKey":"nw5vM2sa05Eh94boiPO5r2qi5fS7CZmJwNNWgIsEj08=","macKey":"o65mhfj8XZpuCIIgamAd3Eibobs0HKiiBCwTAn36qms="}',
WAToken1: '"ArRasrW9r63ByrbKDAauchBnzEUYOO9q0HTWJYfG0RM="',
WAToken2: '"1#GGZtYQss1DkFVbXvuH28Dmm6YdI6wkHvqN1lSbAVAj+S4N5g3qQwuEAdQBsEp/j1cPVRu4bMexECrQ=="'
}
I got an error message like this:
UnhandledPromiseRejectionWarning: Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WABrowserId = '"hTCl7AYDPe/EIymrQC57Hg=="', WASecretBundle = '{"key":"o' at line 1
How can I insert this string into a MySQL database with a Node.js script?
You can't insert normal JS object, you need to parse into JSON using JSON.stringify(obj) function then insert into the database.
you might also need to remove the single quotes and use double quotes.

Execute multiple mongo scripts from the commandline

For executing a single query script (query.js) from the command line the following code was enough.
mongo db-name < query.js
I would like to execute (or match) multiple query files, such as query1.js, query2.js and so on. I tried the following code with no success.
mongo db-name < query*
Please help me out here.
mongo will not allow you to do it you will get an amibgous redirect error. Do this
cat query* | mongo --nodb
the | takes the output of cat query* passes it as input to mongo which in turn executes any thing it gets. cat does not execute the queries it only outputs the content of the files and passes it to mongo.

How to escape $ in SSDT Project

I am working on some legacy code and trying to get everything under control.
I have some stored procedure that contains javascript. Something like
SET #result = #result + '<script>
$(document).ready(function () {'
I have created a SSDT project to keep track of all my database objects. However if I try and publish it SQLCMD will throw a fit as it is getting confused between javascript and its [$(DatabaseName)] syntax.
Is it possible to escape my $ so SQLCMD will correctly parse my SQL code?

Perl's JSON decode_json with JSON.stringify string

I have a front-end and back-end script that are working with one another. The front-end script of type JS does JSON.stringify on an array of strings and calls the backend script for validation on this string.
The backend script of type Perl grabs this string and does a decode_json from the Perl module JSON. At this point the Perl script is croaking.
I think the problem has to do with what's returned by JSON.stringify.
JS:
var jl = ["Flash", "WonderWoman", "BatMan", "SuperMan", "GreenLantern", "MartianManHunter", "HawkWoman"];
var $jl_string = JSON.stringify(jl);
// assume there is code here that makes an AJAX call which calls the backend script
Returns this:
"[\"Flash\", \"WonderWoman\", \"BatMan\", \"SuperMan\", \"GreenLantern\", \"MartianManHunter\", \"HawkWoman\"]"
Perl function that validates the above stringified string:
sub are_jl_members {
my ($jl_string) = #_;
print "<p>$jl_string</p>";
# Failing here. Why?
my $jl = decode_json $jl_string;
print Dumper([$jl]);
}
Thanks for the help.
The problem is the extra "\" in your $jl_string. Your JSON.stringify call is correct satanically.
How are you printing $jl_string here?
Returns this:
"[\"Flash\", \"WonderWoman\", \"BatMan\", \"SuperMan\", \"GreenLantern\", \"MartianManHunter\", \"HawkWoman\"]"

command line argument to a javascript file that works on mongodb

I have a javascript file ( .js ) that works on MongoDB. I run the .js file as
mongo localhost:27017/dbname myjsfile.js .
How can I send command line arguments while running this JavaScript file ? I want to send database name and collection name as command line argument.
Well, you are already setting the database in use as you connect via:
mongo localhost:27017/dbname
So it is now on database "dbname". That is carried through to the db variable. Which is just a placeholder for the "current" database object.
That means that anything in your "script":
var results = db.collection.find().toArray();
For example is using the database you selected and the collection you named.
Need more? This is valid to:
db["mycollection"].find();
It's just JavaScript to the shell.
if you want a collection to be set as a variable then do something like this:
mongo localhost/mydb --eval "var users = db.users" myfile.js
Or otherwise just do that in you JavaScript file. You can test that by:
mongo localhost/mydb --eval "var users = db.users" --shell
And in the shell you now have a variable users that is "aliased" to the users collection.
cli argument no. But you can read a json file and parse it in your script.
// config.json - {"dbname":"dbname","collection":"mycollection"}
var args = JSON.parse(cat("config.json"));

Categories