123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- #include "stdafx.h"
- #include "DBTable.h"
- namespace OTSSQLITE
- {
- // Initializes a new instance of the <see cref="TableInfoBase"/> 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<int> 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<int>& 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<int>& 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());
- }
- }
|