Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow char and varchar cases to fall through when creating parameters #7933

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/driver/sqlserver/SqlServerQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2244,15 +2244,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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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');
})));
});