fixed multiple bugs

This commit is contained in:
Umed Khudoiberdiev 2016-05-09 18:03:04 +05:00
parent eff6a2d3ef
commit 78deb851eb
4 changed files with 36 additions and 11 deletions

View File

@ -1,7 +1,7 @@
{
"name": "typeorm",
"private": true,
"version": "0.0.2-alpha.12",
"version": "0.0.2-alpha.13",
"description": "Data-mapper ORM for Typescript",
"license": "Apache-2.0",
"readmeFilename": "README.md",

View File

@ -47,7 +47,8 @@ createConnection(options).then(connection => {
author.posts = Promise.resolve([secondPost]);
return authorRepository.persist(author);
}).then(author => {
})
.then(author => {
console.log("Author with a new post has been saved. Lets try to update post in the author");
return author.posts.then(posts => {

View File

@ -28,7 +28,7 @@ createConnection(options).then(connection => {
details.metadata = "post,details,one-to-one";
let post = new Post();
post.text = "Hello how are you?";
post.text = "hello how are you?";
post.title = "hello";
post.details = details;
@ -36,7 +36,19 @@ createConnection(options).then(connection => {
postRepository
.persist(post)
.then(post => console.log("Post has been saved"))
.then(post => {
console.log("Post has been saved. Lets try to find this post using query builder: ");
return postRepository
.createQueryBuilder("post")
.where("post.id=:keyword")
.orWhere("post.title=:keyword")
.orWhere("post.details=:keyword")
.setParameter("keyword", "hello")
.getResults();
})
.then(post => {
console.log("Loaded post: ", post);
})
.catch(error => console.log("Cannot save. Error: ", error));
}, error => console.log("Cannot connect: ", error));

View File

@ -148,13 +148,13 @@ export class QueryBuilder<Entity> {
innerJoinAndSelect(entity: Function, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: { [key: string]: any }): this;
innerJoinAndSelect(entityOrProperty: Function|string, alias: string, conditionType: "ON"|"WITH" = "ON", condition: string = "", parameters?: { [key: string]: any }): this {
this.addSelect(alias);
return this.join("INNER", entityOrProperty, alias, conditionType, condition);
return this.join("INNER", entityOrProperty, alias, conditionType, condition, parameters);
}
innerJoin(property: string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: { [key: string]: any }): this;
innerJoin(entity: Function, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: { [key: string]: any }): this;
innerJoin(entityOrProperty: Function|string, alias: string, conditionType: "ON"|"WITH" = "ON", condition: string = "", parameters?: { [key: string]: any }): this {
return this.join("INNER", entityOrProperty, alias, conditionType, condition);
return this.join("INNER", entityOrProperty, alias, conditionType, condition, parameters);
}
leftJoinAndSelect(property: string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: { [key: string]: any }): this;
@ -362,7 +362,12 @@ export class QueryBuilder<Entity> {
.getSql();
return this.driver
.query<any[]>(countQuery)
.then(results => parseInt(results[0]["cnt"]));
.then(results => {
if (!results || !results[0] || !results[0]["cnt"])
return 0;
return parseInt(results[0]["cnt"]);
});
}
getResultsAndCount(): Promise<[Entity[], number]> {
@ -588,9 +593,16 @@ export class QueryBuilder<Entity> {
const joinTable = relation.isOwning ? relation.joinTable : relation.inverseRelation.joinTable; // not sure if this is correct
const joinTableColumn = joinTable.referencedColumn.name; // not sure if this is correct
const inverseJoinColumnName = joinTable.inverseReferencedColumn.name; // not sure if this is correct
const condition1 = junctionAlias + "." + junctionMetadata.columns[0].name + "=" + parentAlias + "." + joinTableColumn; // todo: use column names from junction table somehow
const condition2 = joinAlias + "." + inverseJoinColumnName + "=" + junctionAlias + "." + junctionMetadata.columns[1].name;
let condition1 = "", condition2 = "";
if (relation.isOwning) {
condition1 = junctionAlias + "." + junctionMetadata.columns[0].name + "=" + parentAlias + "." + joinTableColumn;
condition2 = joinAlias + "." + inverseJoinColumnName + "=" + junctionAlias + "." + junctionMetadata.columns[1].name;
} else {
condition1 = junctionAlias + "." + junctionMetadata.columns[1].name + "=" + parentAlias + "." + joinTableColumn;
condition2 = joinAlias + "." + inverseJoinColumnName + "=" + junctionAlias + "." + junctionMetadata.columns[0].name;
}
return " " + joinType + " JOIN " + junctionTable + " " + junctionAlias + " " + join.conditionType + " " + condition1 +
" " + joinType + " JOIN " + joinTableName + " " + joinAlias + " " + join.conditionType + " " + condition2 + appendedCondition;
@ -647,7 +659,7 @@ export class QueryBuilder<Entity> {
protected replaceParameters(sql: string) {
Object.keys(this.parameters).forEach(key => {
const value = this.parameters[key] !== null && this.parameters[key] !== undefined ? this.driver.escape(this.parameters[key]) : "NULL";
sql = sql.replace(":" + key, value); // todo: make replace only in value statements, otherwise problems
sql = sql.replace(new RegExp(":" + key, "g"), value); // todo: make replace only in value statements, otherwise problems
});
return sql;
}