Skip to content

Commit

Permalink
fix: use nvarchar/ntext during transit for SQLServer queries (#7933)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ceshion committed Jul 31, 2021
1 parent d365acc commit 62d7976
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
3 changes: 0 additions & 3 deletions src/driver/sqlserver/SqlServerQueryRunner.ts
Expand Up @@ -2272,15 +2272,12 @@ export class SqlServerQueryRunner extends BaseQueryRunner implements QueryRunner
case "tinyint":
return this.driver.mssql.TinyInt;
case "char":
return this.driver.mssql.Char(...parameter.params);
case "nchar":
return this.driver.mssql.NChar(...parameter.params);
case "text":
return this.driver.mssql.Text;
case "ntext":
return this.driver.mssql.Ntext;
case "varchar":
return this.driver.mssql.VarChar(...parameter.params);
case "nvarchar":
return this.driver.mssql.NVarChar(...parameter.params);
case "xml":
Expand Down
22 changes: 22 additions & 0 deletions test/github-issues/7932/entity/Example.ts
@@ -0,0 +1,22 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn
} from '../../../../src';

@Entity()
export class Example {

@PrimaryGeneratedColumn('uuid')
id?: string;

@CreateDateColumn({ type: 'datetime' })
created?: Date;

@Column('varchar', { length: 10 })
content: string = '';

@Column('char', { length: 10 })
fixedLengthContent: string = '';
}
56 changes: 56 additions & 0 deletions test/github-issues/7932/issue-7932.ts
@@ -0,0 +1,56 @@
import "reflect-metadata";
import { expect } from "chai";
import { Connection } from "../../../src";
import { closeTestingConnections, createTestingConnections, reloadTestingDatabases } from "../../utils/test-utils";
import { Example } from "./entity/Example";

describe("github issues > #7932 non-ascii characters assigned to var/char columns in SQL are truncated to one byte", () => {

let connections: Connection[];
before(async () => {
connections = await createTestingConnections({
enabledDrivers: ["mssql"],
entities: [Example],
schemaCreate: false,
dropSchema: true
});
});
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("should store non-ascii characters in var/char without data loss", () => Promise.all(connections.map(async connection => {
const repo = connection.getRepository(Example);

const entity = new Example();
entity.content = '\u2021';
entity.fixedLengthContent = '\u2022';

await repo.save(entity);
const savedEntity =
await repo.findOne({ order: { created: 'DESC' } });

expect(savedEntity?.content).to.be.equal(entity.content);
expect(savedEntity?.fixedLengthContent).to.be.equal('\u2022 ');
})));

it("should throw an error if characters in a string are too long to store", () => Promise.all(connections.map(async connection => {
const repo = connection.getRepository(Example);

const entity = new Example();
entity.content = '💖';
entity.fixedLengthContent = '🏍';

expect(repo.save(entity)).to.eventually.be.rejectedWith(Error);
})));

it("should not change char or varchar column types to nchar or nvarchar", () => Promise.all(connections.map(async connection => {
const repo = connection.getRepository(Example);

const columnMetadata = repo.metadata.ownColumns;
const contentColumnType = columnMetadata.find(m => m.propertyName === 'content')?.type;
const fixedLengthContentColumnType = columnMetadata.find(m => m.propertyName === 'fixedLengthContent')?.type;

expect(contentColumnType).to.be.equal('varchar');
expect(fixedLengthContentColumnType).to.be.equal('char');
})));
});

0 comments on commit 62d7976

Please sign in to comment.