CSQLiteTable.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace OTSModelSharp.DTLBase
  7. {
  8. public class CSQLiteTable: IDBTableBase
  9. {
  10. //表结构
  11. private TableModel table;
  12. /// <summary>
  13. /// 初始换
  14. /// </summary>
  15. /// <param name="conStr">数据库地址</param>
  16. /// <param name="_table">表结构</param>
  17. public CSQLiteTable()
  18. {
  19. table = new TableModel();
  20. }
  21. public CSQLiteTable(string tableName)
  22. {
  23. table = new TableModel();
  24. table.TableName = tableName;
  25. }
  26. public CSQLiteTable(TableModel t)
  27. {
  28. table = t;
  29. }
  30. public TableModel GetTableInfo()
  31. {
  32. return table;
  33. }
  34. public void AddColumn(ColumnDefine col)
  35. {
  36. table.columns.Add(col);
  37. }
  38. public int GetColumnCount()
  39. {
  40. return table.columns.Count;
  41. }
  42. public string GetTableName()
  43. {
  44. return table.TableName;
  45. }
  46. public void SetTableName(string a_sTableName)
  47. {
  48. table.TableName = a_sTableName;
  49. }
  50. public string GetColumnName(int a_nColId)
  51. {
  52. return table.columns[a_nColId].ColumName;
  53. }
  54. public string GetColumnFullName(int a_nColId)
  55. {
  56. return table.TableName+"."+ table.columns[a_nColId].ColumName;
  57. }
  58. public string GetColumnNames(bool a_bWithPrimary = true)
  59. {
  60. StringBuilder builder = new StringBuilder();
  61. int ind = 0;
  62. foreach (var item in table.columns)
  63. {
  64. ++ind;
  65. if (!a_bWithPrimary&&item.IsPrimarykey)
  66. {
  67. continue;
  68. }
  69. builder.Append(item.ColumName);
  70. if (ind == table.columns.Count)
  71. {
  72. builder.Append("");
  73. }
  74. else
  75. {
  76. builder.Append(",");
  77. }
  78. }
  79. return builder.ToString();
  80. }
  81. public string GetColumnFullNames(bool a_bWithPrimary = true)
  82. {
  83. StringBuilder builder = new StringBuilder();
  84. int ind = 0;
  85. foreach (var item in table.columns)
  86. {
  87. ++ind;
  88. if (!a_bWithPrimary && item.IsPrimarykey)
  89. {
  90. continue;
  91. }
  92. builder.Append(table.TableName + "." + item.ColumName);
  93. if (ind == table.columns.Count)
  94. {
  95. builder.Append("");
  96. }
  97. else
  98. {
  99. builder.Append(",");
  100. }
  101. }
  102. return builder.ToString();
  103. }
  104. public ColumnType GetColumnType(int a_nColId)
  105. {
  106. return table.columns[a_nColId].ColumType;
  107. }
  108. public string GetCreateTableCommandString()
  109. {
  110. StringBuilder builder = new StringBuilder();
  111. builder.Append("CREATE TABLE IF NOT EXISTS " + table.TableName + "(");
  112. int ind = 0;
  113. foreach (var item in table.columns)
  114. {
  115. builder.Append(item.ColumName + " " + item.ColumType.GetName() + " " + (item.IsPrimarykey ? "PRIMARY KEY" : ""));
  116. if (++ind == table.columns.Count)
  117. {
  118. builder.Append(")");
  119. }
  120. else
  121. {
  122. builder.Append(",");
  123. }
  124. }
  125. return builder.ToString();
  126. }
  127. public string GetDeleteTableCommandString()
  128. {
  129. string queryString = "drop table " + table.TableName;
  130. return queryString;
  131. }
  132. public string GetRemoveAllRowsCommandString()
  133. {
  134. string queryString = "DELETE FROM " + table.TableName;
  135. return queryString;
  136. }
  137. public string GetInsertCommandFormatString(bool a_bWithPrimary = false)
  138. {
  139. StringBuilder builder = new StringBuilder();
  140. builder.Append("INSERT INTO " + table.TableName + "(");
  141. int ind = 0;
  142. foreach (var item in table.columns)
  143. {
  144. ++ind;
  145. if (!a_bWithPrimary && item.IsPrimarykey)
  146. {
  147. continue;
  148. }
  149. builder.Append(item.ColumName);
  150. if (ind == table.columns.Count)
  151. {
  152. builder.Append(")");
  153. }
  154. else
  155. {
  156. builder.Append(",");
  157. }
  158. }
  159. ind = 0;
  160. builder.Append(" VALUES (");
  161. foreach (var item in table.columns)
  162. {
  163. ++ind;
  164. if (!a_bWithPrimary && item.IsPrimarykey)
  165. {
  166. continue;
  167. }
  168. builder.Append(item.ColumType.GetFormat(ind-1));
  169. if (ind == table.columns.Count)
  170. {
  171. builder.Append(")");
  172. }
  173. else
  174. {
  175. builder.Append(",");
  176. }
  177. }
  178. return builder.ToString();
  179. }
  180. public string GetInsertCommandFormatString(List<int> a_colIndexes)
  181. {
  182. StringBuilder builder = new StringBuilder();
  183. builder.Append("INSERT INTO " + table.TableName + "(");
  184. int ind = 0;
  185. foreach (var item in a_colIndexes)
  186. {
  187. builder.Append(table.columns[item].ColumName);
  188. if (++ind == a_colIndexes.Count)
  189. {
  190. builder.Append(")");
  191. }
  192. else
  193. {
  194. builder.Append(",");
  195. }
  196. }
  197. ind = 0;
  198. builder.Append(" VALUES ");
  199. foreach (var item in a_colIndexes)
  200. {
  201. builder.Append(table.columns[item].ColumType.GetFormat(ind));
  202. if (++ind == a_colIndexes.Count)
  203. {
  204. builder.Append(")");
  205. }
  206. else
  207. {
  208. builder.Append(",");
  209. }
  210. }
  211. return builder.ToString();
  212. }
  213. public string GetUpdateCommandFormatString(List<int> a_updateColIndexes, int a_nConditionColIndex)
  214. {
  215. StringBuilder builder = new StringBuilder();
  216. builder.Append("UPDATE " + table.TableName + " SET ");
  217. int ind = 0;
  218. foreach (var item in a_updateColIndexes)
  219. {
  220. builder.Append(table.columns[item].ColumName+"="+ table.columns[(int)item].ColumType.GetFormat(ind));
  221. if (++ind == a_updateColIndexes.Count)
  222. {
  223. builder.Append(" WHERE "+ table.columns[a_nConditionColIndex].ColumName+"="+ table.columns[a_nConditionColIndex].ColumType.GetFormat(ind));
  224. }
  225. else
  226. {
  227. builder.Append(",");
  228. }
  229. }
  230. return builder.ToString();
  231. }
  232. }
  233. }