#include "stdafx.h"
#include "DBTable.h"
namespace OTSSQLITE
{
// Initializes a new instance of the class.
CDBTable::CDBTable()
{
}
CDBTable::~CDBTable(void)
{
}
void CDBTable::AddColumn(ColumnDefine col)
{
m_listcolumnDefines.push_back(col);
}
// Gets the name of the column.
CString CDBTable::GetColumnName(const int a_nColId)
{
if (a_nColId < 0 || a_nColId >(int)m_listcolumnDefines.size())
{
return _T("Invalid");
}
return m_listcolumnDefines[a_nColId].first;
}
// Gets the full name of the column.
CString CDBTable::GetColumnFullName(const int a_nColId)
{
CString sName;
sName.Format(_T("%s.%s"), (LPCTSTR)GetTableName(), (LPCTSTR)GetColumnName(a_nColId));
return sName;
}
// Get column names
CString CDBTable::GetColumnNames(const BOOL a_bWithPrimary/* = TRUE*/)
{
CString sRet;
for (int i = 0; i < GetColumnCount(); ++i)
{
if (!a_bWithPrimary)
{
if (m_listcolumnDefines[i].second.IsPrimaryKey())
{
continue;
}
}
CString sName = GetColumnName(i);
if (sRet.IsEmpty())
{
sRet = sName;
}
else
{
sRet += (_T(", ") + sName);
}
}
return sRet;
}
// Get column full names
CString CDBTable::GetColumnFullNames(const BOOL a_bWithPrimary/* = TRUE*/)
{
CString sRet;
for (int i = 0; i < GetColumnCount(); ++i)
{
if (!a_bWithPrimary)
{
if (m_listcolumnDefines[i].second.IsPrimaryKey())
{
continue;
}
}
CString sName = GetColumnFullName(i);
if (sRet.IsEmpty())
{
sRet = sName;
}
else
{
sRet += (_T(", ") + sName);
}
}
return sRet;
}
// Gets the type of the column.
ColumnType CDBTable::GetColumnType(const int a_nColId)
{
if (a_nColId < 0 || a_nColId >(int)m_listcolumnDefines.size())
{
return ColumnType::ID::NONE;
}
return m_listcolumnDefines[a_nColId].second;
}
// Get create table command string
CString CDBTable::GetCreateTableCommandString()
{
CString sColDefs;
CString sPrimaryKeys;
for (auto columnDef : m_listcolumnDefines)
{
ColumnType columnType = columnDef.second;
CString sItem = columnDef.first + _T(" ") + ColumnType::GetName(columnType.GetTypeId());
if (columnType.IsPrimaryKey())
{
if (sPrimaryKeys.IsEmpty())
{
sPrimaryKeys = columnDef.first;
}
else
{
sPrimaryKeys += _T(", ") + columnDef.first;
}
}
if (columnType.IsIsNotNull())
{
sItem += _T(" ") + ColumnType::NotNullString();
}
if (columnType.IsIsUnique())
{
sItem += _T(" ") + ColumnType::UniqueString();
}
if (sColDefs.IsEmpty())
{
sColDefs = sItem;
}
else
{
sColDefs += _T(", ") + sItem;
}
}
CString sSQLCommand;
if (sPrimaryKeys.IsEmpty())
{
sSQLCommand.Format(_T("CREATE TABLE \'%s\'(%s)"), (LPCTSTR)GetTableName(), (LPCTSTR)sColDefs);
}
else
{
sSQLCommand.Format(_T("CREATE TABLE \'%s\'(%s, %s(%s))"),
(LPCTSTR)GetTableName(),
(LPCTSTR)sColDefs,
(LPCTSTR)ColumnType::PrimaryKeyString(),
(LPCTSTR)sPrimaryKeys
);
}
return sSQLCommand;
}
// Get delete table command string
CString CDBTable::GetDeleteTableCommandString()
{
CString sCommand;
sCommand.Format(_T("DROP TABLE \'%s\'"), (LPCTSTR)GetTableName());
return sCommand;
}
// Get remove all rows command string
CString CDBTable::GetRemoveAllRowsCommandString()
{
CString sCommand;
sCommand.Format(_T("DELETE FROM \'%s\'"), (LPCTSTR)GetTableName());
return sCommand;
}
// Get insert command format string
CString CDBTable::GetInsertCommandFormatString(const BOOL a_bWithPrimary /*= FALSE*/)
{
if (m_listcolumnDefines.size() == 0)
{
LogErrorTrace(__FILE__,__LINE__,_T("There are no column defines for table %s"), (LPCTSTR)GetTableName());
return _T("");
}
std::vector colIndexes;
for (int i = 0; i < (int)GetColumnCount(); ++i)
{
if (!a_bWithPrimary)
{
if (m_listcolumnDefines[i].second.IsPrimaryKey())
{
continue;
}
}
colIndexes.push_back(i);
}
return GetInsertCommandFormatString(colIndexes);
}
// Get insert command format string
CString CDBTable::GetInsertCommandFormatString(std::vector& a_colIndexes)
{
CString sColDefs;
CString sTypeDefs;
for (auto nColIndex : a_colIndexes)
{
if (nColIndex < 0 || nColIndex >= (int)m_listcolumnDefines.size())
{
LogErrorTrace(__FILE__,__LINE__,_T("Invalid column index(%d) which total columns is %d."), nColIndex, (int)m_listcolumnDefines.size());
return _T("");
}
if (sColDefs.IsEmpty())
{
sColDefs = m_listcolumnDefines[nColIndex].first;
sTypeDefs = ColumnType::GetFormat(m_listcolumnDefines[nColIndex].second);
}
else
{
sColDefs += _T(", ") + m_listcolumnDefines[nColIndex].first;
sTypeDefs += _T(", ") + ColumnType::GetFormat(m_listcolumnDefines[nColIndex].second);
}
}
if (sColDefs.IsEmpty() || sTypeDefs.IsEmpty())
{
LogErrorTrace(__FILE__,__LINE__,_T("Invalid column result(%s)(%s) of table %s"), (LPCTSTR)sColDefs, (LPCTSTR)sTypeDefs, (LPCTSTR)GetTableName());
return _T("");
}
CString sCommand;
sCommand.Format(_T("INSERT INTO \'%s\'(%s) VALUES (%s)"), (LPCTSTR)GetTableName(), (LPCTSTR)sColDefs, (LPCTSTR)sTypeDefs);
return sCommand;
}
// Get update command format string
CString CDBTable::GetUpdateCommandFormatString(std::vector& a_updateColIndexes, const int a_nConditionColIndex)
{
CString sUpdateDefs;
for (auto nColIndex : a_updateColIndexes)
{
if (nColIndex < 0 || nColIndex >= (int)m_listcolumnDefines.size())
{
LogErrorTrace(__FILE__,__LINE__,_T("Invalid column index(%d) which total columns is %d."), nColIndex, m_listcolumnDefines.size());
return _T("");
}
if (sUpdateDefs.IsEmpty())
{
sUpdateDefs = m_listcolumnDefines[nColIndex].first;
sUpdateDefs += _T(" = ") + ColumnType::GetFormat(m_listcolumnDefines[nColIndex].second);
}
else
{
sUpdateDefs += _T(", ") + m_listcolumnDefines[nColIndex].first;
sUpdateDefs += _T(" = ") + ColumnType::GetFormat(m_listcolumnDefines[nColIndex].second);
}
}
if (sUpdateDefs.IsEmpty())
{
LogErrorTrace(__FILE__,__LINE__,_T("Invalid column update(%s) of table %s"), (LPCTSTR)sUpdateDefs, (LPCTSTR)GetTableName());
return _T("");
}
CString sConditionDef;
if (a_nConditionColIndex >= 0)
{
sConditionDef.Format(_T("WHERE %s = %s"), m_listcolumnDefines[a_nConditionColIndex].first, ColumnType::GetFormat(m_listcolumnDefines[a_nConditionColIndex].second));
}
CString sCommand;
sCommand.Format(_T("UPDATE \'%s\' SET %s %s"), (LPCTSTR)GetTableName(), (LPCTSTR)sUpdateDefs, (LPCTSTR)sConditionDef);
return sCommand;
}
CDBTableBasePtr CreateNewSQLiteTable()
{
return CDBTableBasePtr(new CDBTable());
}
}