mysqljs / mysql

A pure node.js JavaScript Client implementing the MySQL protocol.
MIT License
18.22k stars 2.53k forks source link

Using valid sql "source" command gives ER_PARSE_ERROR #2488

Closed Lukenickerson closed 3 years ago

Lukenickerson commented 3 years ago

Reproduction

Dockerfile:

FROM mariadb:10.5.6
COPY /database/sql/* /sql/
# ^ Contains one sql file "creation.sql"

creation.sql

CREATE DATABASE my_db;

There will eventually be more SQL here to build and populate a test database.

Node js:

import mysql from 'mysql';

const connection = mysql.createConnection({
   // connection info here -- connects to docker container
});

function queryPromise(sql) {
   console.log('SQL> ', sql);
   return new Promise((resolve, reject) => {
      connection.query(sql, (error, results, fields) => {
         if (error) console.error(error);
         console.log('\tSQL result:', results);
         resolve({ error, results });
      });
   });
}

connection.connect();
// Using chained promises to stop queries from running at the same time and the connection getting cut off
queryPromise('SELECT 1 + 1 AS solution') // Works OK
   .then(() => queryPromise('show databases')) // Works OK
   .then(() => queryPromise('source sql/creation.sql')) // Throws an error
   .then(() => connection.end());

Trying to run queryPromise('source sql/creation.sql') via a mysql query it throws an error.

Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'source sql/creation.sql' at line 1
at Query.Sequence._packetToError (C:\code\tox-db-prototype\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)

If I instead go into the container, run mysql, and run source sql/creation.sql from the mysql command line, then it runs fine. If I use queryPromise('CREATE DATABASE my_db') as the query, it also runs okay. The issue seems to be that running "source" as a query with this module is giving an error.

dougwilson commented 3 years ago

Duplicate of #2336