CSQLiteTable.cs 9.1 KB

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