/*-------------------------------------------------------------------------------------------------------------- * Copyright (c) insite-gmbh. All rights reserved. * Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------------------------*/ import { Observable } from 'rxjs/Rx'; import { Injectable } from '@angular/core'; import { Http, Headers, Response } from '@angular/http'; import { InaxConfiguration, ProfileService} from '../../../@inax/common'; import { IResult } from './domain/result'; @Injectable() export class InaxSqlService { constructor(private _http: Http, private _configuration: InaxConfiguration, private _profileService: ProfileService) { } private static isNullOrEmpty(inputString: string): boolean { if (inputString === null || inputString.length === 0) { return true; } else { return false; } } /*********************************************************************** * WEB API * ********************************************************************/ //Builds a command from the given arguments, execute it against the inax connection and returns an list of data. public query(from: string, where: string = null, orderBy: string = null, include: string = null, groupBy: string = null, skip: number = 0, take: number = 2147483647): Observable> { let accessUrl = this._configuration.ServerWithApiUrl + "Sql/Query?from=" + from; if (!InaxSqlService.isNullOrEmpty(where)) accessUrl += "&where=" + where; if (!InaxSqlService.isNullOrEmpty(include)) accessUrl += "&include=" + include; if (!InaxSqlService.isNullOrEmpty(orderBy)) accessUrl += "&orderBy=" + orderBy; if (!InaxSqlService.isNullOrEmpty(groupBy)) accessUrl += "&groupBy=" + groupBy; if (skip > 0) accessUrl += "&skip=" + skip; if (take > 0) accessUrl += "&take=" + take; let headers = this._profileService.addAuthorization(new Headers()); return this._http.get(accessUrl, { headers }).map((res:Response) => this.ExtractData(res.json())); } private ExtractData(data:IResult):Array{ return data.Entries; } //Builds a command from the given arguments, execute it against the inax connection and returns the count. public count(from: string, where: string = null, groupBy: string = null): Observable { let accessUrl = this._configuration.ServerWithApiUrl + "Sql/Count?mappingName=" + from; if (where != null) accessUrl += "&where=" + where; if (groupBy != null) accessUrl += "&groupBy=" + groupBy; let headers = this._profileService.addAuthorization(new Headers()); return this._http.get(accessUrl, { headers }).map(res => res.json()); } //Adds the given entity to the table of the type typeName. With include you can handle an add in more the one tables a call //e.g. if you have a Table who has childs from an other table you declare the other table as include public add(typeName, entity, inlude): Observable { let accessUrl = this._configuration.ServerWithApiUrl + "/Sql/Add?typeName=" + typeName; if (inlude != null) accessUrl += "&include=" + inlude; let headers = this._profileService.addAuthorization(new Headers()); return this._http.post(accessUrl, entity, { headers }).map(res => res.json()); } //Adds the given entities to the table of the type typeName. With include you can handle an add in more the one tables a call //e.g. if you have a Table who has childs from an other table you declare the other table as include public addMany(typeName, entities, inlude): Observable { let accessUrl = this._configuration.ServerWithApiUrl + "/Sql/AddMany?typeName=" + typeName; if (inlude != null) accessUrl += "&inlude=" + inlude; let headers = this._profileService.addAuthorization(new Headers()); return this._http.post(accessUrl, entities, { headers }).map(res => res.json()); } //Delete the given entity from the table public delete(typeName, entity): Observable { let accessUrl = this._configuration.ServerWithApiUrl + "/Sql/Delete?typeName=" + typeName; let headers = this._profileService.addAuthorization(new Headers()); return this._http.post(accessUrl, entity, { headers }).map(res => res.json()); } //Delete the given entities from the table public deleteMany(typeName, entities): Observable { let accessUrl = this._configuration.ServerWithApiUrl + "/Sql/DeleteMany?typeName=" + typeName; let headers = this._profileService.addAuthorization(new Headers()); return this._http.post(accessUrl, entities, { headers }).map(res => res.json()); } //Delete all rows from the given table public deleteAll(typeName): Observable { let accessUrl = this._configuration.ServerWithApiUrl + "/Sql/DeleteAll"; let headers = this._profileService.addAuthorization(new Headers()); return this._http.post(accessUrl, JSON.stringify(typeName), { headers }).map(res => res.json()); } //Execute an sql command public executeSqlCommand(cmd, parameter): Observable { let accessUrl = this._configuration.ServerWithApiUrl + "/Sql/ExecuteSqlCommand?cmd=" + cmd; if (parameter != null) accessUrl += "&queryParams=" + parameter; let headers = this._profileService.addAuthorization(new Headers()); return this._http.post(accessUrl, "", { headers }).map(res => res.json()); } //Executes the CommandText against the Connection and returns an list of data. This methode is used for non inax sql connections. //parameters: @name=value,@name=value public externalQuery(sqlConnectionId, query, parameter, transactionName): Observable { let accessUrl = this._configuration.ServerWithApiUrl + "/Sql/ExternalQuery?sqlConnectionId=" + sqlConnectionId + "&query=" + query; if (parameter != null) accessUrl += "¶merter=" + parameter; if (transactionName != null) accessUrl += "&transactionName=" + transactionName; let headers = this._profileService.addAuthorization(new Headers()); return this._http.get(accessUrl, { headers }).map(res => res.json()); } //Executes a Transact-SQL statement against the connection and returns the number of rows affected. This methode is used for non inax sql connections. //parameters: @name=value,@name=value public externalNonQuery(sqlConnectionId, query, parameter, transactionName): Observable { let accessUrl = this._configuration.ServerWithApiUrl + "/Sql/ExternalNonQuery?sqlConnectionId=" + sqlConnectionId + "&query=" + query; if (parameter != null) accessUrl += "¶merter=" + parameter; if (transactionName != null) accessUrl += "&transactionName=" + transactionName; let headers = this._profileService.addAuthorization(new Headers()); return this._http.put(accessUrl, "", { headers }).map(res => res.json()); } private normalizeUri(uri: string): string { return encodeURIComponent(uri); } }