using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Text;
namespace TestProject
{
/// <summary>
/// SQLITE 헬퍼
/// </summary>
public static class SQLiteHelper
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region 연결 구하기 - GetConnection(connectionString)
/// <summary>
/// 연결 구하기
/// </summary>
/// <param name="connectionString">연결 문자열</param>
/// <returns>연결</returns>
public static SQLiteConnection GetConnection(string connectionString)
{
SQLiteConnection connection = new SQLiteConnection(connectionString);
return connection;
}
#endregion
#region 명령 구하기 - GetCommand(connection)
/// <summary>
/// 명령 구하기
/// </summary>
/// <param name="connection">연결</param>
/// <returns>명령</returns>
public static SQLiteCommand GetCommand(SQLiteConnection connection)
{
SQLiteCommand command = new SQLiteCommand(connection);
return command;
}
#endregion
#region 데이터 어댑터 구하기 - GetDataAdapter()
/// <summary>
/// 데이터 어댑터 구하기
/// </summary>
/// <returns>데이터 어댑터</returns>
public static SQLiteDataAdapter GetDataAdapter()
{
SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter();
return dataAdapter;
}
#endregion
#region 실행하기 - Execute(connection, commandText, parameterList)
/// <summary>
/// 실행하기
/// </summary>
/// <param name="connection">연결</param>
/// <param name="commandText">명령 텍스트</param>
/// <param name="parameterList">매개 변수 리스트</param>
/// <returns>처리 행 수</returns>
public static int Execute(SQLiteConnection connection, string commandText, List<SQLiteParameter> parameterList = null)
{
using(SQLiteCommand command = GetCommand(connection))
{
command.CommandText = commandText;
if(parameterList != null)
{
foreach(SQLiteParameter parameter in parameterList)
{
command.Parameters.Add(parameter);
}
}
int result = command.ExecuteNonQuery();
return result;
}
}
#endregion
#region 스칼라 실행하기 - ExecuteScalar(connection, commandText, parameterList)
/// <summary>
/// 스칼라 실행하기
/// </summary>
/// <param name="connection">연결</param>
/// <param name="commandText">명령 텍스트</param>
/// <param name="parameterList">매개 변수 리스트</param>
/// <returns>스칼라 값</returns>
public static object ExecuteScalar(SQLiteConnection connection, string commandText, List<SQLiteParameter> parameterList = null)
{
using(SQLiteCommand command = GetCommand(connection))
{
command.CommandText = commandText;
if(parameterList != null)
{
foreach(SQLiteParameter parameter in parameterList)
{
command.Parameters.Add(parameter);
}
}
object result = command.ExecuteScalar();
return result;
}
}
#endregion
#region 데이터 리더 실행하기 - ExecuteReader(connection, commandText, parameterList)
/// <summary>
/// 데이터 리더 실행하기
/// </summary>
/// <param name="connection">연결</param>
/// <param name="commandText">명령 텍스트</param>
/// <param name="parameterList">매개 변수 리스트</param>
/// <returns>데이터 리더</returns>
public static SQLiteDataReader ExecuteDataReader(SQLiteConnection connection, string commandText, List<SQLiteParameter> parameterList = null)
{
using(SQLiteCommand command = GetCommand(connection))
{
command.CommandText = commandText;
if(parameterList != null)
{
foreach(SQLiteParameter parameter in parameterList)
{
command.Parameters.Add(parameter);
}
}
SQLiteDataReader dataReader = command.ExecuteReader();
return dataReader;
}
}
#endregion
#region 데이터 테이블 구하기 - GetDataTable(connection, commandText, parameterList)
/// <summary>
/// 데이터 테이블 구하기
/// </summary>
/// <param name="connection">연결</param>
/// <param name="commandText">명령 텍스트</param>
/// <param name="parameterList">매개 변수 리스트</param>
/// <returns>데이터 테이블</returns>
public static DataTable GetDataTable(SQLiteConnection connection, string commandText, List<SQLiteParameter> parameterList = null)
{
using(SQLiteCommand command = GetCommand(connection))
{
command.CommandText = commandText;
if(parameterList != null)
{
foreach(SQLiteParameter parameter in parameterList)
{
command.Parameters.Add(parameter);
}
}
SQLiteDataAdapter dataAdapter = GetDataAdapter();
dataAdapter.SelectCommand = command;
DataTable dataTable = new DataTable();
dataAdapter.Fill(dataTable);
return dataTable;
}
}
#endregion
#region 문자열 리스트 구하기 - GetStringList(sourceArray)
/// <summary>
/// 문자열 리스트 구하기
/// </summary>
/// <param name="sourceArray">소스 배열</param>
/// <returns>문자열 리스트</returns>
public static List<string> GetStringList(params string[] sourceArray)
{
List<string> list = new List<string>();
foreach(string source in sourceArray)
{
list.Add(source);
}
return list;
}
#endregion
#region 문자열 리스트 구하기 - GetStringList(sourceTable, columnName)
/// <summary>
/// 리스트 구하기
/// </summary>
/// <param name="sourceTable">소스 테이블</param>
/// <param name="columnName">컬럼명</param>
/// <returns>문자열 리스트</returns>
public static List<string> GetStringList(DataTable sourceTable, string columnName)
{
List<string> list = new List<string>();
foreach(DataRow sourceRow in sourceTable.Rows)
{
list.Add(sourceRow[columnName].ToString());
}
return list;
}
#endregion
#region 피벗 합계 SQL 구하기 - GetPivotSummarySQL(tableName, rowColumnNameList, columnColumnName, columnColumnValueList, valueColumnName, orderByColumnNameList)
/// <summary>
/// 피벗 합계 SQL 구하기
/// </summary>
/// <param name="tableName">테이블명</param>
/// <param name="rowColumnNameList">행 컬럼명 리스트</param>
/// <param name="columnColumnName">컬럼 컬럼명</param>
/// <param name="columnColumnValueList">컬럼 컬럼 값 리스트</param>
/// <param name="valueColumnName">값 컬럼명</param>
/// <param name="orderByColumnNameList">정렬 컬럼명 리스트</param>
/// <returns>피벗 SQL</returns>
public static string GetPivotSummarySQL(string tableName, List<string> rowColumnNameList, string columnColumnName, List<string> columnColumnValueList,
string valueColumnName, List<string> orderByColumnNameList)
{
string sql = @"
SELECT
[ROW_COLUMN_NAME_LIST]
[COLUMN_COLUMN_VALUE_LIST]
FROM [TABLE_NAME]
GROUP BY [ROW_COLUMN_NAME_LIST]
ORDER BY [ORDER_BY_COLUMN_NAME_LIST];
";
#region 행 컬럼명 문자열 빌더를 설정한다.
StringBuilder rowColumnNameStringBuilder = new StringBuilder();
foreach(string column in rowColumnNameList)
{
rowColumnNameStringBuilder.AppendFormat("{0},", column);
}
#endregion
#region 컬럼 컬럼 값 문자열 빌더를 설정한다.
StringBuilder columnColumnValueStringBuilder = new StringBuilder();
foreach(string columnColumnValue in columnColumnValueList)
{
columnColumnValueStringBuilder.AppendFormat
(
",SUM(CASE WHEN [{0}] = '{1}' THEN {2} ELSE 0 END) AS [{1}]",
columnColumnName,
columnColumnValue,
valueColumnName
);
columnColumnValueStringBuilder.AppendLine();
}
#endregion
#region 정렬 컬럼명 문자열 빌더를 설정한다.
StringBuilder orderByColumnNameStringBuilder = new StringBuilder();
foreach(string orderByColumnName in orderByColumnNameList)
{
orderByColumnNameStringBuilder.AppendFormat("{0},", orderByColumnName);
}
#endregion
sql = sql.Replace("[ROW_COLUMN_NAME_LIST]" , rowColumnNameStringBuilder.ToString().TrimEnd(',') );
sql = sql.Replace("[COLUMN_COLUMN_VALUE_LIST]" , columnColumnValueStringBuilder.ToString() );
sql = sql.Replace("[TABLE_NAME]" , tableName );
sql = sql.Replace("[ORDER_BY_COLUMN_NAME_LIST]", orderByColumnNameStringBuilder.ToString().TrimEnd(','));
return sql;
}
#endregion
#region 피벗 카운트 SQL 구하기 - GetPivotCountSQL(tableName, rowColumnNameList, columnColumnName, columnColumnValueList, orderByColumnNameList)
/// <summary>
/// 피벗 카운트 SQL 구하기
/// </summary>
/// <param name="tableName">테이블명</param>
/// <param name="rowColumnNameList">행 컬럼명 리스트</param>
/// <param name="columnColumnName">컬럼 컬럼명</param>
/// <param name="columnColumnValueTable">컬럼 컬럼 값 리스트</param>
/// <param name="valueColumnName">값 컬럼명</param>
/// <param name="valueFunctionName">값 함수명</param>
/// <param name="orderByColumnNameList">정렬 컬럼명 리스트</param>
/// <returns>피벗 SQL</returns>
public static string GetPivotCountSQL(string tableName, List<string> rowColumnNameList, string columnColumnName,
List<string> columnColumnValueList, List<string> orderByColumnNameList)
{
string sql = @"
SELECT
[ROW_COLUMN_NAME_LIST]
[COLUMN_COLUMN_VALUE_LIST]
FROM [TABLE_NAME]
GROUP BY [ROW_COLUMN_NAME_LIST]
ORDER BY [ORDER_BY_COLUMN_NAME_LIST];
";
#region 행 컬럼명 문자열 빌더를 설정한다.
StringBuilder rowColumnNameStringBuilder = new StringBuilder();
foreach(string rowColumnName in rowColumnNameList)
{
rowColumnNameStringBuilder.AppendFormat("{0},", rowColumnName);
}
#endregion
#region 컬럼 컬럼 값 문자열 빌더를 설정한다.
StringBuilder columnColumnValueStringBuilder = new StringBuilder();
foreach(string columnColumnValue in columnColumnValueList)
{
columnColumnValueStringBuilder.AppendFormat(",SUM(CASE WHEN [{0}] = '{1}' THEN 1 ELSE 0 END) AS [{1}]", columnColumnName, columnColumnValue);
columnColumnValueStringBuilder.AppendLine();
}
#endregion
#region 정렬 컬럼명 문자열 빌더를 설정한다.
StringBuilder orderByColumnNameStringBuilder = new StringBuilder();
foreach(string orderByColumnName in orderByColumnNameList)
{
orderByColumnNameStringBuilder.AppendFormat("{0},", orderByColumnName);
}
#endregion
sql = sql.Replace("[ROW_COLUMN_NAME_LIST]" , rowColumnNameStringBuilder.ToString().TrimEnd(',') );
sql = sql.Replace("[COLUMN_COLUMN_VALUE_LIST]" , columnColumnValueStringBuilder.ToString() );
sql = sql.Replace("[TABLE_NAME]" , tableName );
sql = sql.Replace("[ORDER_BY_COLUMN_NAME_LIST]", orderByColumnNameStringBuilder.ToString().TrimEnd(','));
return sql;
}
#endregion
#region 피벗 평균 SQL 구하기 - GetPivotAverageSQL(summaryTableName, countTableName, rowColumnNameList, columnColumnValueList, orderByColumnNameList)
/// <summary>
/// 피벗 평균 SQL 구하기
/// </summary>
/// <param name="summaryTableName">합계 테이블명</param>
/// <param name="countTableName">카운트 테이블명</param>
/// <param name="rowColumnNameList">행 컬럼명 리스트</param>
/// <param name="columnColumnValueList">컬럼 컬럼 값 리스트</param>
/// <param name="orderByColumnNameList">정렬 컬럼명 리스트</param>
/// <returns>피벗 평균 SQL</returns>
public static string GetPivotAverageSQL(string summaryTableName, string countTableName, List<string> rowColumnNameList,
List<string> columnColumnValueList, List<string> orderByColumnNameList)
{
string sql = @"
SELECT
[ROW_COLUMN_NAME_LIST]
[COLUMN_COLUMN_VALUE_LIST]
FROM [SUMMARY_TABLE_NAME] A
INNER JOIN [COUNT_TABLE_NAME] B ON [JOIN_CONDITION]
ORDER BY [ORDER_BY_COLUMN_NAME_LIST];
";
#region 행 컬럼명 문자열 빌더를 설정한다.
StringBuilder rowColumnNameStringBuilder = new StringBuilder();
foreach(string rowColumnName in rowColumnNameList)
{
rowColumnNameStringBuilder.AppendFormat("A.{0},", rowColumnName);
}
#endregion
#region 컬럼 컬럼 값 문자열 빌더를 설정한다.
StringBuilder columnColumnValueStringBuilder = new StringBuilder();
foreach(string columnColumnValue in columnColumnValueList)
{
columnColumnValueStringBuilder.AppendFormat(",CASE WHEN B.[{0}] <> 0 THEN A.[{0}] / B.[{0}] ELSE 0 END AS [{0}]", columnColumnValue);
columnColumnValueStringBuilder.AppendLine();
}
#endregion
#region 조인 조건 문자열 빌더를 설정한다.
StringBuilder joinConditionStringBuilder = new StringBuilder();
foreach(string rowColumnName in rowColumnNameList)
{
if(joinConditionStringBuilder.Length > 0)
{
joinConditionStringBuilder.Append(" AND ");
}
joinConditionStringBuilder.AppendFormat("B.{0} = A.{0}", rowColumnName);
}
#endregion
#region 정렬 컬럼명 문자열 빌더를 설정한다.
StringBuilder orderByColumnNameStringBuilder = new StringBuilder();
foreach(string orderByColumnName in orderByColumnNameList)
{
orderByColumnNameStringBuilder.AppendFormat("A.{0},", orderByColumnName);
}
#endregion
sql = sql.Replace("[ROW_COLUMN_NAME_LIST]" , rowColumnNameStringBuilder.ToString().TrimEnd(',') );
sql = sql.Replace("[COLUMN_COLUMN_VALUE_LIST]" , columnColumnValueStringBuilder.ToString() );
sql = sql.Replace("[SUMMARY_TABLE_NAME]" , summaryTableName );
sql = sql.Replace("[COUNT_TABLE_NAME]" , countTableName );
sql = sql.Replace("[JOIN_CONDITION]" , joinConditionStringBuilder.ToString() );
sql = sql.Replace("[ORDER_BY_COLUMN_NAME_LIST]", orderByColumnNameStringBuilder.ToString().TrimEnd(','));
return sql;
}
#endregion
}
}