181 lines
7.9 KiB
C#
181 lines
7.9 KiB
C#
using NLog;
|
|
using Npgsql;
|
|
using Server.System;
|
|
using System.Text;
|
|
|
|
namespace Server.Git
|
|
{
|
|
public class ExcelSQL
|
|
{
|
|
|
|
private static readonly NLog.ILogger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
List<Sheet> sheets;
|
|
|
|
public ExcelSQL(List<Sheet> sheets)
|
|
{
|
|
this.sheets = sheets;
|
|
}
|
|
|
|
public void DataUpdate()
|
|
{
|
|
using (NpgsqlConnection connection = new NpgsqlConnection(Statics.EXCEL_SQL_URL))
|
|
{
|
|
try
|
|
{
|
|
// 데이터베이스 연결 열기
|
|
connection.Open();
|
|
|
|
// 쿼리 작성 및 실행
|
|
//모든 쿼리 삭제
|
|
StringBuilder query;
|
|
string header1 = "CREATE TABLE excel.";
|
|
string header2 = "INSERT INTO excel.";
|
|
StringBuilder newTableQuery;
|
|
StringBuilder tableDatas = new StringBuilder();
|
|
#region 모든 테이블 삭제
|
|
ExecuteNonQuery(connection, "DROP SCHEMA excel CASCADE;CREATE SCHEMA excel AUTHORIZATION manager;");
|
|
#endregion
|
|
#region 테이블 세팅
|
|
query = new StringBuilder();
|
|
for (int n = 0; n < sheets.Count; n++)
|
|
{
|
|
//더 윗단에서 지워버려 에러가 발생되는중.
|
|
//시스템 수정할것
|
|
Console.WriteLine(sheets[n].name);
|
|
//초기화
|
|
newTableQuery = new StringBuilder();
|
|
tableDatas.Append("(");
|
|
|
|
#region 신규 테이블 생성
|
|
newTableQuery.Append(header1);
|
|
newTableQuery.Append(sheets[n].name);
|
|
newTableQuery.Append("(");
|
|
for (int m = 0; m < sheets[n].variable.Count; m++)
|
|
{
|
|
if (sheets[n].dataEnum[m] == "client")
|
|
continue;
|
|
|
|
if (sheets[n].type[m] == "long" && sheets[n].variable[m] == "index")
|
|
{
|
|
newTableQuery.Append("index SERIAL PRIMARY KEY");
|
|
tableDatas.Append("index");
|
|
continue;
|
|
}
|
|
if (m != 0)
|
|
{
|
|
newTableQuery.Append(",");
|
|
tableDatas.Append(", ");
|
|
}
|
|
switch (sheets[n].type[m])
|
|
{
|
|
case "bool":
|
|
newTableQuery.Append($"{sheets[n].variable[m]} BOOL ");
|
|
break;
|
|
case "int":
|
|
newTableQuery.Append($"{sheets[n].variable[m]} INT ");
|
|
break;
|
|
case "long":
|
|
newTableQuery.Append($"{sheets[n].variable[m]} LONG ");
|
|
break;
|
|
case "float":
|
|
newTableQuery.Append($"{sheets[n].variable[m]} FLOAT4 ");
|
|
break;
|
|
case "string":
|
|
case "json":
|
|
newTableQuery.Append($"{sheets[n].variable[m]} VARCHAR(255) ");
|
|
break;
|
|
case "enum":
|
|
newTableQuery.Append($"{sheets[n].variable[m]} INT2 ") ;
|
|
break;
|
|
case "text":
|
|
newTableQuery.Append($"{sheets[n].variable[m]} TEXT ");
|
|
break;
|
|
case "time":
|
|
newTableQuery.Append($"{sheets[n].variable[m]} timestamp ");
|
|
break;
|
|
default:
|
|
logger.Error($"unknown data {sheets[n].type[m]}");
|
|
break;
|
|
}
|
|
tableDatas.Append(sheets[n].variable[m]);
|
|
}
|
|
newTableQuery.Append(");\n");
|
|
tableDatas.Append(") VALUES ");
|
|
bool isStart = true;
|
|
query.Append(newTableQuery.ToString());
|
|
|
|
#endregion
|
|
#region 신규 테이블 데이터 올리기
|
|
|
|
query.Append(header2);
|
|
query.Append(sheets[n].name);
|
|
query.Append(tableDatas.ToString());
|
|
foreach(KeyValuePair<long, Dictionary<string, object>> pair in sheets[n].dicViewer)
|
|
{
|
|
if (isStart)
|
|
{
|
|
isStart = !isStart;
|
|
}
|
|
else
|
|
{
|
|
query.Append(", ");
|
|
}
|
|
query.Append("(");
|
|
for (int m = 0; m < sheets[n].variable.Count; m++)
|
|
{
|
|
if (sheets[n].dataEnum[m] == "client")
|
|
continue;
|
|
if (m != 0)
|
|
{
|
|
query.Append(", ");
|
|
}
|
|
switch (sheets[n].type[m])
|
|
{
|
|
case "bool":
|
|
case "enum":
|
|
case "int":
|
|
case "float":
|
|
case "long":
|
|
Console.WriteLine($"{pair.Value[sheets[n].variable[m]]}");
|
|
query.Append($"{pair.Value[sheets[n].variable[m]]}");
|
|
break;
|
|
case "string":
|
|
case "json":
|
|
case "text":
|
|
query.Append($"'{pair.Value[sheets[n].variable[m]]}'");
|
|
break;
|
|
case "time":
|
|
query.Append($"'{((DateTime)pair.Value[sheets[n].variable[m]]).ToString("yyyy-MM-dd HH:mm:ss")}'");
|
|
break;
|
|
default:
|
|
logger.Error($"unknown data value {sheets[n].type[m]}");
|
|
break;
|
|
}
|
|
}
|
|
query.Append(")");
|
|
}
|
|
query.Append(";");
|
|
#endregion
|
|
}
|
|
ExecuteNonQuery(connection, query.ToString());
|
|
|
|
#endregion
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.Error($"Error: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
private void ExecuteNonQuery(NpgsqlConnection connection, string query)
|
|
{
|
|
using (NpgsqlCommand command = new NpgsqlCommand(query, connection))
|
|
{
|
|
logger.Info(query);
|
|
command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
}
|