DBTable.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include "stdafx.h"
  2. #include "DBTable.h"
  3. namespace OTSSQLITE
  4. {
  5. // Initializes a new instance of the <see cref="TableInfoBase"/> class.
  6. CDBTable::CDBTable()
  7. {
  8. }
  9. CDBTable::~CDBTable(void)
  10. {
  11. }
  12. void CDBTable::AddColumn(ColumnDefine col)
  13. {
  14. m_listcolumnDefines.push_back(col);
  15. }
  16. // Gets the name of the column.
  17. CString CDBTable::GetColumnName(const int a_nColId)
  18. {
  19. if (a_nColId < 0 || a_nColId >(int)m_listcolumnDefines.size())
  20. {
  21. return _T("Invalid");
  22. }
  23. return m_listcolumnDefines[a_nColId].first;
  24. }
  25. // Gets the full name of the column.
  26. CString CDBTable::GetColumnFullName(const int a_nColId)
  27. {
  28. CString sName;
  29. sName.Format(_T("%s.%s"), (LPCTSTR)GetTableName(), (LPCTSTR)GetColumnName(a_nColId));
  30. return sName;
  31. }
  32. // Get column names
  33. CString CDBTable::GetColumnNames(const BOOL a_bWithPrimary/* = TRUE*/)
  34. {
  35. CString sRet;
  36. for (int i = 0; i < GetColumnCount(); ++i)
  37. {
  38. if (!a_bWithPrimary)
  39. {
  40. if (m_listcolumnDefines[i].second.IsPrimaryKey())
  41. {
  42. continue;
  43. }
  44. }
  45. CString sName = GetColumnName(i);
  46. if (sRet.IsEmpty())
  47. {
  48. sRet = sName;
  49. }
  50. else
  51. {
  52. sRet += (_T(", ") + sName);
  53. }
  54. }
  55. return sRet;
  56. }
  57. // Get column full names
  58. CString CDBTable::GetColumnFullNames(const BOOL a_bWithPrimary/* = TRUE*/)
  59. {
  60. CString sRet;
  61. for (int i = 0; i < GetColumnCount(); ++i)
  62. {
  63. if (!a_bWithPrimary)
  64. {
  65. if (m_listcolumnDefines[i].second.IsPrimaryKey())
  66. {
  67. continue;
  68. }
  69. }
  70. CString sName = GetColumnFullName(i);
  71. if (sRet.IsEmpty())
  72. {
  73. sRet = sName;
  74. }
  75. else
  76. {
  77. sRet += (_T(", ") + sName);
  78. }
  79. }
  80. return sRet;
  81. }
  82. // Gets the type of the column.
  83. ColumnType CDBTable::GetColumnType(const int a_nColId)
  84. {
  85. if (a_nColId < 0 || a_nColId >(int)m_listcolumnDefines.size())
  86. {
  87. return ColumnType::ID::NONE;
  88. }
  89. return m_listcolumnDefines[a_nColId].second;
  90. }
  91. // Get create table command string
  92. CString CDBTable::GetCreateTableCommandString()
  93. {
  94. CString sColDefs;
  95. CString sPrimaryKeys;
  96. for (auto columnDef : m_listcolumnDefines)
  97. {
  98. ColumnType columnType = columnDef.second;
  99. CString sItem = columnDef.first + _T(" ") + ColumnType::GetName(columnType.GetTypeId());
  100. if (columnType.IsPrimaryKey())
  101. {
  102. if (sPrimaryKeys.IsEmpty())
  103. {
  104. sPrimaryKeys = columnDef.first;
  105. }
  106. else
  107. {
  108. sPrimaryKeys += _T(", ") + columnDef.first;
  109. }
  110. }
  111. if (columnType.IsIsNotNull())
  112. {
  113. sItem += _T(" ") + ColumnType::NotNullString();
  114. }
  115. if (columnType.IsIsUnique())
  116. {
  117. sItem += _T(" ") + ColumnType::UniqueString();
  118. }
  119. if (sColDefs.IsEmpty())
  120. {
  121. sColDefs = sItem;
  122. }
  123. else
  124. {
  125. sColDefs += _T(", ") + sItem;
  126. }
  127. }
  128. CString sSQLCommand;
  129. if (sPrimaryKeys.IsEmpty())
  130. {
  131. sSQLCommand.Format(_T("CREATE TABLE \'%s\'(%s)"), (LPCTSTR)GetTableName(), (LPCTSTR)sColDefs);
  132. }
  133. else
  134. {
  135. sSQLCommand.Format(_T("CREATE TABLE \'%s\'(%s, %s(%s))"),
  136. (LPCTSTR)GetTableName(),
  137. (LPCTSTR)sColDefs,
  138. (LPCTSTR)ColumnType::PrimaryKeyString(),
  139. (LPCTSTR)sPrimaryKeys
  140. );
  141. }
  142. return sSQLCommand;
  143. }
  144. // Get delete table command string
  145. CString CDBTable::GetDeleteTableCommandString()
  146. {
  147. CString sCommand;
  148. sCommand.Format(_T("DROP TABLE \'%s\'"), (LPCTSTR)GetTableName());
  149. return sCommand;
  150. }
  151. // Get remove all rows command string
  152. CString CDBTable::GetRemoveAllRowsCommandString()
  153. {
  154. CString sCommand;
  155. sCommand.Format(_T("DELETE FROM \'%s\'"), (LPCTSTR)GetTableName());
  156. return sCommand;
  157. }
  158. // Get insert command format string
  159. CString CDBTable::GetInsertCommandFormatString(const BOOL a_bWithPrimary /*= FALSE*/)
  160. {
  161. if (m_listcolumnDefines.size() == 0)
  162. {
  163. LogErrorTrace(__FILE__,__LINE__,_T("There are no column defines for table %s"), (LPCTSTR)GetTableName());
  164. return _T("");
  165. }
  166. std::vector<int> colIndexes;
  167. for (int i = 0; i < (int)GetColumnCount(); ++i)
  168. {
  169. if (!a_bWithPrimary)
  170. {
  171. if (m_listcolumnDefines[i].second.IsPrimaryKey())
  172. {
  173. continue;
  174. }
  175. }
  176. colIndexes.push_back(i);
  177. }
  178. return GetInsertCommandFormatString(colIndexes);
  179. }
  180. // Get insert command format string
  181. CString CDBTable::GetInsertCommandFormatString(std::vector<int>& a_colIndexes)
  182. {
  183. CString sColDefs;
  184. CString sTypeDefs;
  185. for (auto nColIndex : a_colIndexes)
  186. {
  187. if (nColIndex < 0 || nColIndex >= (int)m_listcolumnDefines.size())
  188. {
  189. LogErrorTrace(__FILE__,__LINE__,_T("Invalid column index(%d) which total columns is %d."), nColIndex, (int)m_listcolumnDefines.size());
  190. return _T("");
  191. }
  192. if (sColDefs.IsEmpty())
  193. {
  194. sColDefs = m_listcolumnDefines[nColIndex].first;
  195. sTypeDefs = ColumnType::GetFormat(m_listcolumnDefines[nColIndex].second);
  196. }
  197. else
  198. {
  199. sColDefs += _T(", ") + m_listcolumnDefines[nColIndex].first;
  200. sTypeDefs += _T(", ") + ColumnType::GetFormat(m_listcolumnDefines[nColIndex].second);
  201. }
  202. }
  203. if (sColDefs.IsEmpty() || sTypeDefs.IsEmpty())
  204. {
  205. LogErrorTrace(__FILE__,__LINE__,_T("Invalid column result(%s)(%s) of table %s"), (LPCTSTR)sColDefs, (LPCTSTR)sTypeDefs, (LPCTSTR)GetTableName());
  206. return _T("");
  207. }
  208. CString sCommand;
  209. sCommand.Format(_T("INSERT INTO \'%s\'(%s) VALUES (%s)"), (LPCTSTR)GetTableName(), (LPCTSTR)sColDefs, (LPCTSTR)sTypeDefs);
  210. return sCommand;
  211. }
  212. // Get update command format string
  213. CString CDBTable::GetUpdateCommandFormatString(std::vector<int>& a_updateColIndexes, const int a_nConditionColIndex)
  214. {
  215. CString sUpdateDefs;
  216. for (auto nColIndex : a_updateColIndexes)
  217. {
  218. if (nColIndex < 0 || nColIndex >= (int)m_listcolumnDefines.size())
  219. {
  220. LogErrorTrace(__FILE__,__LINE__,_T("Invalid column index(%d) which total columns is %d."), nColIndex, m_listcolumnDefines.size());
  221. return _T("");
  222. }
  223. if (sUpdateDefs.IsEmpty())
  224. {
  225. sUpdateDefs = m_listcolumnDefines[nColIndex].first;
  226. sUpdateDefs += _T(" = ") + ColumnType::GetFormat(m_listcolumnDefines[nColIndex].second);
  227. }
  228. else
  229. {
  230. sUpdateDefs += _T(", ") + m_listcolumnDefines[nColIndex].first;
  231. sUpdateDefs += _T(" = ") + ColumnType::GetFormat(m_listcolumnDefines[nColIndex].second);
  232. }
  233. }
  234. if (sUpdateDefs.IsEmpty())
  235. {
  236. LogErrorTrace(__FILE__,__LINE__,_T("Invalid column update(%s) of table %s"), (LPCTSTR)sUpdateDefs, (LPCTSTR)GetTableName());
  237. return _T("");
  238. }
  239. CString sConditionDef;
  240. if (a_nConditionColIndex >= 0)
  241. {
  242. sConditionDef.Format(_T("WHERE %s = %s"), m_listcolumnDefines[a_nConditionColIndex].first, ColumnType::GetFormat(m_listcolumnDefines[a_nConditionColIndex].second));
  243. }
  244. CString sCommand;
  245. sCommand.Format(_T("UPDATE \'%s\' SET %s %s"), (LPCTSTR)GetTableName(), (LPCTSTR)sUpdateDefs, (LPCTSTR)sConditionDef);
  246. return sCommand;
  247. }
  248. CDBTableBasePtr CreateNewSQLiteTable()
  249. {
  250. return CDBTableBasePtr(new CDBTable());
  251. }
  252. }