Git Product home page Git Product logo

Comments (4)

dhensby avatar dhensby commented on June 1, 2024

How interesting. Can you confirm what you mean by "doesn't work"?

Do you have a minimal set queries / DB structure & data that I can use to replicate the issue?

from node-mssql.

ShahriarKh avatar ShahriarKh commented on June 1, 2024

Doesn't work = returns 0 rows.
My database has ARABIC_CI_AS collation, and although it isn't desired, I can't easily change it.

If you want to test, here's a simple query to create the db:

CREATE DATABASE PersianTest
COLLATE ARABIC_CI_AS
USE PersianTest
CREATE TABLE People (ID INT, Name NVARCHAR(20))

INSERT INTO PersianTest.dbo.People (ID, Name) VALUES (1, 'شادی'), (2, 'شادي'), (3, 'علی'), (4, 'علي')

from node-mssql.

ShahriarKh avatar ShahriarKh commented on June 1, 2024

The same thing happens for procedures:

-- using varchar
CREATE PROCEDURE TestPersian @inputField VARCHAR(255) AS BEGIN
SELECT
  Name
FROM
  People
WHERE
  Name = @inputField;
END
GRANT EXEC ON dbo.TestPersian TO PUBLIC

-- using nvarchar
CREATE PROCEDURE NTestPersian @inputField NVARCHAR(255) AS BEGIN
SELECT
  Name
FROM
  People
WHERE
  Name = @inputField;
END
GRANT EXEC ON dbo.NTestPersian TO PUBLIC

Using sql directly:

/* persian text */
EXEC TestPersian 'علی'; --
EXEC NTestPersian 'علی'; --
EXEC TestPersian N'علی'; --
EXEC NTestPersian N'علی'; --

/* arabic text */
EXEC TestPersian 'علي'; --
EXEC NTestPersian 'علي'; --
EXEC TestPersian N'علي'; --
EXEC NTestPersian N'علي'; --

Using js:

/* arabic text */
data = await pool.request().input('inputField', VarChar, 'علي').execute('TestPersian'); // ✅
data = await pool.request().input('inputField', VarChar, 'علي').execute('NTestPersian'); // ✅
data = await pool.request().input('inputField', NVarChar, 'علي').execute('TestPersian'); // ✅
data = await pool.request().input('inputField', NVarChar, 'علي').execute('NTestPersian'); // ✅

/* persian text */
data = await pool.request().input('inputField', VarChar, 'علی').execute('TestPersian'); // ❌
data = await pool.request().input('inputField', VarChar, 'علی').execute('NTestPersian');  // ❌
data = await pool.request().input('inputField', NVarChar, 'علی').execute('TestPersian'); // ✅
data = await pool.request().input('inputField', NVarChar, 'علی').execute('NTestPersian'); // ❌

from node-mssql.

dhensby avatar dhensby commented on June 1, 2024

OK - this seems to be a problem either with the underlying tedious driver, or SQL as whole. I've just created this test script making use of raw tedious driver and get the same behaviour as I do with node-mssql:

const { connect, Request, TYPES } = require('tedious');

function doConnect () {
  return new Promise((resolve, reject) => {
    const connection = connect({
      server: 'localhost',
      options: {
        encrypt: true,
        database: 'PersianTest',
        trustServerCertificate: true,
        rowCollectionOnRequestCompletion: true
      },
      authentication: {
        type: 'default',
        options: {
          userName: 'sa',
          password: 'yourStrong(!)Password'
        }
      }
    }, (err) => {
      if (err) { reject(err) } else { resolve(connection) }
    })
  })
}

(async () => {
  const names = ['شادی', 'شادي', 'علی', 'علي']
  const res = await Promise.all(names.map((name) => {
    return new Promise(async (resolve, reject) => {
      const conn = await doConnect()
      const request = new Request(`SELECT * FROM [People] WHERE [Name] = @name`, (err, count, rows) => {
        conn.close()
        if (err) {
          reject(err)
        } else {
          resolve(rows.map((cols) => {
            return cols.reduce((acc, col) => ({
              ...acc,
              [col.metadata.colName]: col.value
            }), {})
          }, []))
        }
      })
      request.addParameter('name', TYPES.NVarChar, name)
      conn.execSql(request)
    })
  }))
  console.log(res)
})().then(() => {
  console.log('Done')
}).catch(console.error)

Output:

[
  [],
  [ { ID: 1, Name: 'شادي' }, { ID: 2, Name: 'شادي' } ],
  [],
  [ { ID: 3, Name: 'علي' }, { ID: 4, Name: 'علي' } ]
]

If I don't use a parameter, it works as expected:

const { connect, Request, TYPES } = require('tedious');

function doConnect () {
  return new Promise((resolve, reject) => {
    const connection = connect({
      server: 'localhost',
      options: {
        encrypt: true,
        database: 'PersianTest',
        trustServerCertificate: true,
        rowCollectionOnRequestCompletion: true
      },
      authentication: {
        type: 'default',
        options: {
          userName: 'sa',
          password: 'yourStrong(!)Password'
        }
      }
    }, (err) => {
      if (err) { reject(err) } else { resolve(connection) }
    })
  })
}

(async () => {
  const names = ['شادی', 'شادي', 'علی', 'علي']
  const res = await Promise.all(names.map((name) => {
    return new Promise(async (resolve, reject) => {
      const conn = await doConnect()
      const request = new Request(`SELECT * FROM [People] WHERE [Name] = '${name}'`, (err, count, rows) => {
        conn.close()
        if (err) {
          reject(err)
        } else {
          resolve(rows.map((cols) => {
            return cols.reduce((acc, col) => ({
              ...acc,
              [col.metadata.colName]: col.value
            }), {})
          }, []))
        }
      })
      conn.execSql(request)
    })
  }))
  console.log(res)
})().then(() => {
  console.log('Done')
}).catch(console.error)

Output:

[
  [ { ID: 1, Name: 'شادي' }, { ID: 2, Name: 'شادي' } ],
  [ { ID: 1, Name: 'شادي' }, { ID: 2, Name: 'شادي' } ],
  [ { ID: 3, Name: 'علي' }, { ID: 4, Name: 'علي' } ],
  [ { ID: 3, Name: 'علي' }, { ID: 4, Name: 'علي' } ]
]

Would you mind opening an issue with the tedious driver directly?

from node-mssql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.