-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
389 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
name: TRE SQL PG Container | ||
|
||
on: | ||
pull_request: | ||
paths: | ||
- 'src/Tre-SQL-PG/**' | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- 'src/Tre-SQL-PG/**' | ||
|
||
permissions: | ||
contents: write | ||
pull-requests: read | ||
actions: read | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.ref }} | ||
cancel-in-progress: false | ||
|
||
jobs: | ||
tre-hasura-container: | ||
uses: SwanseaUniversityMedical/workflows/.github/workflows/pr-and-release-container.yaml@v1.4.2-containers | ||
with: | ||
job-name: tre-sqlpg-container | ||
registry: ${{ vars.HARBOR_REGISTRY }} | ||
registry-user: ${{ vars.HARBOR_USER }} | ||
registry-repo: ${{ vars.HARBOR_PROJECT }}/${{ vars.HARBOR_REPO }}-tre-sqlpg | ||
release-tag-format: 'DARE-TRE-SQLPG-Container-${version}' | ||
cosign-public-key: ${{ vars.COSIGN_PUBLIC_KEY }} | ||
slack-channel: ${{ vars.SLACK_CHANNEL }} | ||
build-file: src/Tre-SQL-PG/Dockerfile | ||
build-context: src | ||
secrets: | ||
cosign-private-key: ${{ secrets.COSIGN_PRIVATE_KEY }} | ||
cosign-password: ${{ secrets.COSIGN_PASSWORD }} | ||
registry-token: ${{ secrets.HARBOR_TOKEN }} | ||
slack-token: ${{ secrets.SLACK_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
name: TRE SQL Trino Container | ||
|
||
on: | ||
pull_request: | ||
paths: | ||
- 'src/Tre-SQL-Trino/**' | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- 'src/Tre-SQL-Trino/**' | ||
|
||
permissions: | ||
contents: write | ||
pull-requests: read | ||
actions: read | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.ref }} | ||
cancel-in-progress: false | ||
|
||
jobs: | ||
tre-hasura-container: | ||
uses: SwanseaUniversityMedical/workflows/.github/workflows/pr-and-release-container.yaml@v1.4.2-containers | ||
with: | ||
job-name: tre-sqltrino-container | ||
registry: ${{ vars.HARBOR_REGISTRY }} | ||
registry-user: ${{ vars.HARBOR_USER }} | ||
registry-repo: ${{ vars.HARBOR_PROJECT }}/${{ vars.HARBOR_REPO }}-tre-sqltrino | ||
release-tag-format: 'DARE-TRE-SQLTRINO-Container-${version}' | ||
cosign-public-key: ${{ vars.COSIGN_PUBLIC_KEY }} | ||
slack-channel: ${{ vars.SLACK_CHANNEL }} | ||
build-file: src/Tre-SQL-Trino/Dockerfile | ||
build-context: src | ||
secrets: | ||
cosign-private-key: ${{ secrets.COSIGN_PRIVATE_KEY }} | ||
cosign-password: ${{ secrets.COSIGN_PASSWORD }} | ||
registry-token: ${{ secrets.HARBOR_TOKEN }} | ||
slack-token: ${{ secrets.SLACK_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging. | ||
|
||
FROM mcr.microsoft.com/dotnet/runtime:7.0 AS base | ||
WORKDIR /app | ||
|
||
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build | ||
WORKDIR /src | ||
COPY ["src/Tre-SQL-PG/Tre-SQL-PG.csproj", "src/Tre-SQL-PG/"] | ||
RUN dotnet restore "src/Tre-SQL-PG/Tre-SQL-PG.csproj" | ||
COPY . . | ||
WORKDIR "/src/src/Tre-SQL-PG" | ||
RUN dotnet build "Tre-SQL-PG.csproj" -c Release -o /app/build | ||
|
||
FROM build AS publish | ||
RUN dotnet publish "Tre-SQL-PG.csproj" -c Release -o /app/publish /p:UseAppHost=false | ||
|
||
FROM base AS final | ||
WORKDIR /app | ||
COPY --from=publish /app/publish . | ||
ENTRYPOINT ["dotnet", "Tre-SQL-PG.dll"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using System.Data; | ||
using System.IO; | ||
using Npgsql; | ||
|
||
Console.WriteLine("TREFX SQL Runner Module : Postgress"); | ||
|
||
// Tre-SQL-PG.exe "--Query=select * from \"profileForm\"" --Output=data.csv | ||
|
||
var Token = ""; | ||
var OutputFilename = "data.csv"; | ||
var Query = "SELECT * FROM \"profileForm\""; | ||
string connectionString = "Host=localhost:5432;Username=admin;Password=admin;Database=assets3"; | ||
|
||
foreach (var arg in args) | ||
{ | ||
Console.WriteLine("Arg = " + arg); | ||
|
||
if (arg.StartsWith("--Output")) | ||
{ | ||
OutputFilename = arg.Replace("--Output=", ""); | ||
} | ||
|
||
if (arg.StartsWith("--Token_")) | ||
{ | ||
Token = arg.Replace("--Token_", ""); | ||
} | ||
|
||
if (arg.StartsWith("--Connection")) | ||
{ | ||
connectionString = arg.Replace("--Connection=", ""); | ||
} | ||
|
||
if (arg.StartsWith("--Query")) | ||
{ | ||
Query = arg.Replace("--Query=", ""); | ||
} | ||
|
||
} | ||
|
||
Console.WriteLine("Query > " + Query); | ||
Console.WriteLine("Token > " + Token); | ||
Console.WriteLine("Output > " + OutputFilename); | ||
|
||
ExportToCsv(connectionString, Query, OutputFilename); | ||
|
||
Console.WriteLine("Completed"); | ||
|
||
|
||
|
||
static void ExportToCsv(string connectionString, string sqlQuery, string csvFilePath) | ||
{ | ||
using (var connection = new NpgsqlConnection(connectionString)) | ||
{ | ||
connection.Open(); | ||
using (var command = new NpgsqlCommand(sqlQuery, connection)) | ||
{ | ||
using (var reader = command.ExecuteReader()) | ||
{ | ||
if (reader.HasRows) | ||
{ | ||
using (var streamWriter = new StreamWriter(csvFilePath)) | ||
{ | ||
// Write column headers | ||
for (int i = 0; i < reader.FieldCount; i++) | ||
{ | ||
if (i > 0) | ||
streamWriter.Write(","); | ||
streamWriter.Write(reader.GetName(i)); | ||
} | ||
streamWriter.WriteLine(); | ||
|
||
// Write data rows | ||
while (reader.Read()) | ||
{ | ||
for (int i = 0; i < reader.FieldCount; i++) | ||
{ | ||
if (i > 0) | ||
streamWriter.Write(","); | ||
streamWriter.Write(reader[i].ToString()); | ||
} | ||
streamWriter.WriteLine(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"profiles": { | ||
"Tre-SQL-PG": { | ||
"commandName": "Project" | ||
}, | ||
"Docker": { | ||
"commandName": "Docker" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<RootNamespace>Tre_SQL_PG</RootNamespace> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> | ||
<DockerfileContext>..\..</DockerfileContext> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.18.1" /> | ||
<PackageReference Include="Npgsql" Version="8.0.2" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging. | ||
|
||
FROM mcr.microsoft.com/dotnet/runtime:7.0 AS base | ||
WORKDIR /app | ||
|
||
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build | ||
WORKDIR /src | ||
COPY ["src/Tre-SQL-Trino/Tre-SQL-Trino.csproj", "src/Tre-SQL-Trino/"] | ||
RUN dotnet restore "src/Tre-SQL-Trino/Tre-SQL-Trino.csproj" | ||
COPY . . | ||
WORKDIR "/src/src/Tre-SQL-Trino" | ||
RUN dotnet build "Tre-SQL-Trino.csproj" -c Release -o /app/build | ||
|
||
FROM build AS publish | ||
RUN dotnet publish "Tre-SQL-Trino.csproj" -c Release -o /app/publish /p:UseAppHost=false | ||
|
||
FROM base AS final | ||
WORKDIR /app | ||
COPY --from=publish /app/publish . | ||
ENTRYPOINT ["dotnet", "Tre-SQL-Trino.dll"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using System; | ||
using System.Data; | ||
using System.IO; | ||
using System.Net.Http; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using System.Threading.Tasks; | ||
|
||
Console.WriteLine("TREFX SQL Runner Module : Trino"); | ||
|
||
// Tre-SQL-Trino.exe "--Query=select * from \"profileForm\"" --Output=data.csv | ||
|
||
string trinoUrl = "http://trino-server:8080/v1/statement"; | ||
var Token = ""; | ||
var OutputFilename = "data.csv"; | ||
var Query = "SELECT * FROM \"profileForm\""; | ||
|
||
foreach (var arg in args) | ||
{ | ||
Console.WriteLine("Arg = " + arg); | ||
|
||
if (arg.StartsWith("--Output")) | ||
{ | ||
OutputFilename = arg.Replace("--Output=", ""); | ||
} | ||
|
||
if (arg.StartsWith("--Token_")) | ||
{ | ||
Token = arg.Replace("--Token_", ""); | ||
} | ||
|
||
if (arg.StartsWith("--Connection")) | ||
{ | ||
trinoUrl = arg.Replace("--Connection=", ""); | ||
} | ||
|
||
if (arg.StartsWith("--Query")) | ||
{ | ||
Query = arg.Replace("--Query=", ""); | ||
} | ||
|
||
} | ||
|
||
Console.WriteLine("Query > " + Query); | ||
Console.WriteLine("Token > " + Token); | ||
Console.WriteLine("Output > " + OutputFilename); | ||
|
||
await ExportToCsv(trinoUrl, Query, OutputFilename); | ||
|
||
Console.WriteLine("Completed"); | ||
|
||
|
||
static async Task ExportToCsv(string trinoUrl, string sqlQuery, string csvFilePath) | ||
{ | ||
using (var httpClient = new HttpClient()) | ||
{ | ||
var requestBody = new | ||
{ | ||
query = sqlQuery | ||
}; | ||
|
||
var requestContent = new StringContent(JsonConvert.SerializeObject(requestBody)); | ||
requestContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); | ||
|
||
using (var response = await httpClient.PostAsync(trinoUrl, requestContent)) | ||
{ | ||
response.EnsureSuccessStatusCode(); | ||
|
||
using (var responseStream = await response.Content.ReadAsStreamAsync()) | ||
using (var streamReader = new StreamReader(responseStream)) | ||
using (var jsonReader = new JsonTextReader(streamReader)) | ||
{ | ||
var jsonSerializer = new JsonSerializer(); | ||
|
||
using (var streamWriter = new StreamWriter(csvFilePath)) | ||
using (var csvWriter = new CsvHelper.CsvWriter(streamWriter, System.Globalization.CultureInfo.InvariantCulture)) | ||
{ | ||
while (jsonReader.Read()) | ||
{ | ||
if (jsonReader.TokenType == JsonToken.StartObject) | ||
{ | ||
// Deserialize JSON response into a dynamic object | ||
var jsonObject = jsonSerializer.Deserialize<JObject>(jsonReader); | ||
|
||
// Write column headers | ||
var columns = jsonObject["columns"]; | ||
foreach (var column in columns) | ||
{ | ||
csvWriter.WriteField(column["name"].ToString()); | ||
} | ||
csvWriter.NextRecord(); | ||
|
||
// Write data rows | ||
var data = jsonObject["data"]; | ||
foreach (var rowData in data) | ||
{ | ||
foreach (var value in rowData) | ||
{ | ||
csvWriter.WriteField(value.ToString()); | ||
} | ||
csvWriter.NextRecord(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.