GetTblRecordSet.cmt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <#@ template language="c#" HostSpecific="True" #>
  2. <#@ output extension= ".cs" #>
  3. <#
  4. TableHost host = (TableHost)(Host);
  5. host.Fieldlist.Sort(CodeCommon.CompareByintOrder);
  6. #>
  7. // 为每个字段生成注释
  8. GO
  9. SET ANSI_PADDING OFF
  10. <# foreach (ColumnInfo c in host.Fieldlist) { #>
  11. GO
  12. EXEC sys.sp_addextendedproperty
  13. @name=N'MS_Description',
  14. @value=N'<#= c.ColumnName.ToString().ToLower() #>' ,
  15. @level0type=N'SCHEMA',
  16. @level0name=N'dbo',
  17. @level1type=N'TABLE',
  18. @level1name=N'<#= host.GetModelClass(host.TableName) #>',
  19. @level2type=N'COLUMN',
  20. @level2name=N'<#= c.ColumnName.ToString().ToLower() #>'
  21. <# } #>
  22. ///////////////////////////////////////////////////////////////////////////
  23. // 生成查询语句
  24. #define SELECT_<#= host.GetModelClass(host.TableName) #> _T("SELECT <# foreach (ColumnInfo c in host.Fieldlist) { #>
  25. <# if( c == host.Fieldlist[host.Fieldlist.Count-1] ) {#><#= c.ColumnName.ToString().ToLower() #><# } else { #>
  26. <#= c.ColumnName.ToString().ToLower() #>,<# } #><# } #> FROM <#= host.GetModelClass(host.TableName) #>")
  27. ///////////////////////////////////////////////////////////////////////////
  28. // 生成更新语句
  29. #define UPDATE_<#= host.GetModelClass(host.TableName) #> _T("UPDATE [<#= host.GetModelClass(host.TableName) #>] SET \
  30. <# foreach (ColumnInfo c in host.Fieldlist) { #>
  31. <# if( c == host.Fieldlist[host.Fieldlist.Count-1] ) {#>
  32. <#= c.ColumnName.ToString().ToLower() #>\
  33. <# } else { #>
  34. <#= c.ColumnName.ToString().ToLower() #>,\
  35. <# } #>
  36. <# } #>
  37. )VALUES(\
  38. <# foreach (ColumnInfo c in host.Fieldlist) { #>
  39. <# if( c == host.Fieldlist[host.Fieldlist.Count-1] ) {#>
  40. <# if (
  41. CodeCommon.DbTypeToCS(c.TypeName)=="int"||
  42. CodeCommon.DbTypeToCS(c.TypeName)=="long"||
  43. CodeCommon.DbTypeToCS(c.TypeName)=="float"||
  44. CodeCommon.DbTypeToCS(c.TypeName)=="decimal"||
  45. CodeCommon.DbTypeToCS(c.TypeName)=="bool")
  46. {#>
  47. %d<# } #>
  48. <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime")
  49. {#>
  50. '%s'
  51. <# } #>
  52. <# } else { #>
  53. <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="int"||
  54. CodeCommon.DbTypeToCS(c.TypeName)=="long"||
  55. CodeCommon.DbTypeToCS(c.TypeName)=="float"||
  56. CodeCommon.DbTypeToCS(c.TypeName)=="decimal"||
  57. CodeCommon.DbTypeToCS(c.TypeName)=="bool")
  58. {#>
  59. %d,\
  60. <# } #>
  61. <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime")
  62. {#>
  63. '%s',\
  64. <# } #>
  65. <# } #>
  66. <# } #>)")
  67. ///////////////////////////////////////////////////////////////////////////
  68. // 生成表对应的Insert宏;
  69. #define INSERT_<#= host.GetModelClass(host.TableName) #> _T("INSERT INTO [<#= host.GetModelClass(host.TableName) #>](\
  70. <# foreach (ColumnInfo c in host.Fieldlist) { #>
  71. <# if( c == host.Fieldlist[host.Fieldlist.Count-1] ) {#>
  72. <#= c.ColumnName.ToString().ToLower() #>\
  73. <# } else { #>
  74. <#= c.ColumnName.ToString().ToLower() #>,\
  75. <# } #>
  76. <# } #>
  77. )VALUES(\
  78. <# foreach (ColumnInfo c in host.Fieldlist) { #>
  79. <# if( c == host.Fieldlist[host.Fieldlist.Count-1] ) {#>
  80. <# if (
  81. CodeCommon.DbTypeToCS(c.TypeName)=="int"||
  82. CodeCommon.DbTypeToCS(c.TypeName)=="long"||
  83. CodeCommon.DbTypeToCS(c.TypeName)=="float"||
  84. CodeCommon.DbTypeToCS(c.TypeName)=="decimal"||
  85. CodeCommon.DbTypeToCS(c.TypeName)=="bool")
  86. {#>
  87. %d<# } #>
  88. <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime")
  89. {#>
  90. '%s'
  91. <# } #>
  92. <# } else { #>
  93. <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="int"||
  94. CodeCommon.DbTypeToCS(c.TypeName)=="long"||
  95. CodeCommon.DbTypeToCS(c.TypeName)=="float"||
  96. CodeCommon.DbTypeToCS(c.TypeName)=="decimal"||
  97. CodeCommon.DbTypeToCS(c.TypeName)=="bool")
  98. {#>
  99. %d,\
  100. <# } #>
  101. <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime")
  102. {#>
  103. '%s',\
  104. <# } #>
  105. <# } #>
  106. <# } #>)")
  107. ///////////////////////////////////////////////////////////////////////////
  108. // 生成表对应的结构体;
  109. typedef struct __Tbl_<#= host.GetModelClass(host.TableName) #>__
  110. {
  111. <# foreach (ColumnInfo c in host.Fieldlist) { #>
  112. <#= CodeCommon.DbTypeToCS(c.TypeName) #> _<#= c.ColumnName.ToString().ToLower() #>;
  113. <# } #>
  114. }Tbl<#= host.GetModelClass(host.TableName) #>,*pTbl<#= host.GetModelClass(host.TableName) #>;
  115. // 代码自动生成模板
  116. // 生成AdoImpl类的GetTblRecordSet模板函数;
  117. ///////////////////////////////////////////////////////////////////////////
  118. /************************************************************************/
  119. /*
  120. 函数:GetTbl_<#= host.GetModelClass(host.TableName) #>RecordSet
  121. 描述:获取表[<#= host.GetModelClass(host.TableName) #>]的记录集;
  122. 参数:
  123. IN: pTableName 表名;
  124. IN: pFilters 查找条件;
  125. IN: pSql SQL语句;
  126. IN: nFieldCount 查找的字段数量;
  127. OUT: vtRSet 返回的记录集对应的结构体数组;
  128. 返回:成功返回记录集大小, 失败返回 -1;
  129. */
  130. /************************************************************************/
  131. INT AdoImpl::GetTbl_<#= host.GetModelClass(host.TableName) #>RecordSet(IN CONST TCHAR *pTableName, IN CONST TCHAR *pFilters, IN CONST TCHAR *pSql, INT &nFieldCount, OUT vector<Tbl_<#= host.GetModelClass(host.TableName) #>>& vtRSet)
  132. {
  133. TCHAR szSQL[1024] = _T("");
  134. if(pFilters == NULL || _tcscmp(pFilters,_T("")) == 0)
  135. _stprintf_s(szSQL,_T("select count(*) as cot from %s"),pTableName);
  136. else
  137. _stprintf_s(szSQL, _T("select count(*) as cot from %s where %s"), pTableName, pFilters);
  138. int nRecordCount = 0;
  139. _variant_t vtFieldValue;
  140. bool bResult = false;
  141. EnterCriticalSection( &m_csAdo );
  142. bResult = GetADODateBaseStatus();
  143. if( false == bResult )
  144. {
  145. LeaveCriticalSection( &m_csAdo );
  146. return -1;
  147. }
  148. CADORecordset* pRSet = new CADORecordset(m_pADODatabase);
  149. LeaveCriticalSection( &m_csAdo );
  150. if( NULL == pRSet )
  151. {
  152. return -1;
  153. }
  154. bResult = OpenADORecordSet(pRSet, szSQL);
  155. if( false == bResult )
  156. {
  157. DeleteADORecordSet(pRSet);
  158. return -1;
  159. }
  160. if( !pRSet->IsFieldNull(_T("cot")) )
  161. {
  162. pRSet->GetFieldValue(_T("cot"), nRecordCount);
  163. }
  164. else
  165. {
  166. nRecordCount = -1;
  167. }
  168. DeleteADORecordSet(pRSet);
  169. pRSet = new CADORecordset(m_pADODatabase);
  170. LeaveCriticalSection( &m_csAdo );
  171. if( NULL == pRSet )
  172. {
  173. return -1;
  174. }
  175. bResult = OpenADORecordSet(pRSet, pSql);
  176. if( false == bResult )
  177. {
  178. DeleteADORecordSet(pRSet);
  179. return -1;
  180. }
  181. nFieldCount = pRSet->GetFieldCount();
  182. for ( int i = 0; i < nRecordCount; i++)
  183. {
  184. Tbl_<#= host.GetModelClass(host.TableName) #> tagVal;
  185. <# foreach (ColumnInfo c in host.Fieldlist){ #>
  186. if( !pRSet->IsFieldNull(_T("<#= c.ColumnName.ToString() #>")) )
  187. {
  188. pRSet->GetFieldValue(_T("<#= c.ColumnName.ToString() #>"), vtFieldValue);
  189. <# if(CodeCommon.DbTypeToCS(c.TypeName)=="int"||
  190. CodeCommon.DbTypeToCS(c.TypeName)=="long"||
  191. CodeCommon.DbTypeToCS(c.TypeName)=="float"||
  192. CodeCommon.DbTypeToCS(c.TypeName)=="decimal"||
  193. CodeCommon.DbTypeToCS(c.TypeName)=="bool") {#>
  194. tagVal._<#= c.ColumnName.ToString().ToLower() #> = vtFieldValue;
  195. <# } #>
  196. <# if(CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#>
  197. tagVal._<#= c.ColumnName.ToString().ToLower() #> = TString((TCHAR*)(_bstr_t)vtFieldValue);
  198. <# } #>
  199. }
  200. else
  201. {
  202. <# if(CodeCommon.DbTypeToCS(c.TypeName)=="int"||
  203. CodeCommon.DbTypeToCS(c.TypeName)=="long"||
  204. CodeCommon.DbTypeToCS(c.TypeName)=="float"||
  205. CodeCommon.DbTypeToCS(c.TypeName)=="bool"||
  206. CodeCommon.DbTypeToCS(c.TypeName)=="decimal") {#>
  207. tagVal._<#= c.ColumnName.ToString().ToLower() #> = 0;
  208. <# } #>
  209. <# if(CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#>
  210. tagVal._<#= c.ColumnName.ToString().ToLower() #> = _T("");
  211. <# } #>
  212. }
  213. <# } #>
  214. vtRSet.push_back(tagVal);
  215. pRSet->MoveNext();
  216. }
  217. DeleteADORecordSet(pRSet);
  218. return nRecordCount;
  219. }
  220. // 代码自动生成模板
  221. // 生成AdoImpl类的GetTblRecordSet模板函数;
  222. ///////////////////////////////////////////////////////////////////////////
  223. /************************************************************************/
  224. /*
  225. 函数:GetTbl_<#= host.GetModelClass(host.TableName) #>RecordSet
  226. 描述:获取表[<#= host.GetModelClass(host.TableName) #>]的记录集;
  227. 参数:
  228. IN: pTableName 表名;
  229. IN: pFilters 查找条件;
  230. IN: pSql SQL语句;
  231. IN: nFieldCount 查找的字段数量;
  232. OUT: vtRSet 返回的记录集对应的结构体数组;
  233. 返回:成功返回记录集大小, 失败返回 -1;
  234. */
  235. /************************************************************************/
  236. INT AdoImpl::GetTbl_<#= host.GetModelClass(host.TableName) #>RecordSet(IN CONST TCHAR *pTableName, IN CONST TCHAR *pFilters, IN CONST TCHAR *pSql, INT &nFieldCount, OUT CArray<CStringArray,CStringArray>& AryRSet)
  237. {
  238. TCHAR szSQL[1024] = _T("");
  239. if(pFilters == NULL || _tcscmp(pFilters,_T("")) == 0)
  240. _stprintf_s(szSQL,_T("select count(*) as cot from %s"),pTableName);
  241. else
  242. _stprintf_s(szSQL, _T("select count(*) as cot from %s where %s"), pTableName, pFilters);
  243. int nRecordCount = 0;
  244. _variant_t vtFieldValue;
  245. bool bResult = false;
  246. EnterCriticalSection( &m_csAdo );
  247. bResult = GetADODateBaseStatus();
  248. if( false == bResult )
  249. {
  250. LeaveCriticalSection( &m_csAdo );
  251. return -1;
  252. }
  253. CADORecordset* pRSet = new CADORecordset(m_pADODatabase);
  254. LeaveCriticalSection( &m_csAdo );
  255. if( NULL == pRSet )
  256. {
  257. return -1;
  258. }
  259. bResult = OpenADORecordSet(pRSet, szSQL);
  260. if( false == bResult )
  261. {
  262. DeleteADORecordSet(pRSet);
  263. return -1;
  264. }
  265. if( !pRSet->IsFieldNull(_T("cot")) )
  266. {
  267. pRSet->GetFieldValue(_T("cot"), nRecordCount);
  268. }
  269. else
  270. {
  271. nRecordCount = -1;
  272. }
  273. DeleteADORecordSet(pRSet);
  274. pRSet = new CADORecordset(m_pADODatabase);
  275. LeaveCriticalSection( &m_csAdo );
  276. if( NULL == pRSet )
  277. {
  278. return -1;
  279. }
  280. bResult = OpenADORecordSet(pRSet, pSql);
  281. if( false == bResult )
  282. {
  283. DeleteADORecordSet(pRSet);
  284. return -1;
  285. }
  286. TCHAR buffer[65];
  287. DWORD dwOldSize = AryRSet.GetSize();
  288. AryRSet.SetSize(dwOldSize + nRecordCount,1);
  289. INT nIndex = dwOldSize == 0 ? 0 : dwOldSize - 1;
  290. nFieldCount = pRSet->GetFieldCount();
  291. for ( int i = nIndex; i < nRecordCount; i++)
  292. {
  293. AryRSet.ElementAt(i).RemoveAll();
  294. <# foreach (ColumnInfo c in host.Fieldlist){ #>
  295. if( !pRSet->IsFieldNull(_T("<#= c.ColumnName.ToString() #>")) )
  296. {
  297. pRSet->GetFieldValue(_T("<#= c.ColumnName.ToString() #>"), vtFieldValue);
  298. <# if(CodeCommon.DbTypeToCS(c.TypeName)=="int"||
  299. CodeCommon.DbTypeToCS(c.TypeName)=="long"||
  300. CodeCommon.DbTypeToCS(c.TypeName)=="float"||
  301. CodeCommon.DbTypeToCS(c.TypeName)=="decimal"||
  302. CodeCommon.DbTypeToCS(c.TypeName)=="bool") {#>
  303. _itoa( vtFieldValue, buffer, 10 );
  304. AryRSet.ElementAt(i).Add(buffer);
  305. <# } #>
  306. <# if(CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#>
  307. AryRSet.ElementAt(i).Add((TCHAR*)(_bstr_t)vtFieldValue);
  308. <# } #>
  309. }
  310. else
  311. {
  312. <# if(CodeCommon.DbTypeToCS(c.TypeName)=="int"||
  313. CodeCommon.DbTypeToCS(c.TypeName)=="long"||
  314. CodeCommon.DbTypeToCS(c.TypeName)=="float"||
  315. CodeCommon.DbTypeToCS(c.TypeName)=="bool"||
  316. CodeCommon.DbTypeToCS(c.TypeName)=="decimal") {#>
  317. AryRSet.ElementAt(i).Add(_T(""));
  318. <# } #>
  319. <# if(CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#>
  320. AryRSet.ElementAt(i).Add(_T(""));
  321. <# } #>
  322. }
  323. <# } #>
  324. pRSet->MoveNext();
  325. }
  326. DeleteADORecordSet(pRSet);
  327. return nRecordCount;
  328. }
  329. ///////////////////////////////////////////////////////////////////////////
  330. // AdomImpl::GetTbl_<#= host.GetModelClass(host.TableName) #>RecordSet()的调用实例;
  331. //
  332. void Call_GetTbl_<#= host.GetModelClass(host.TableName) #>RecordSet_Example()
  333. {
  334. // 获取配置文件信息;
  335. if ( -1 == GetIniInfo() )
  336. {
  337. // 打开配置文件失败;
  338. return;
  339. }
  340. STAdoDatabaseInfo tagAdoDatabaseInfo;
  341. _stprintf_s(tagAdoDatabaseInfo.szDataSource,_T("%s"),g_szDBSource);
  342. _stprintf_s(tagAdoDatabaseInfo.szDatabaseTCPPort,_T("%d"),g_dwDBServerPort);
  343. _stprintf_s(tagAdoDatabaseInfo.szDatabaseAccount,_T("%s"),g_szDBAccount);
  344. _stprintf_s(tagAdoDatabaseInfo.szDatabasePassword,_T("%s"),g_szDBPassWord);
  345. _stprintf_s(tagAdoDatabaseInfo.szDatabaseName,_T("%s"),g_szDBName);
  346. // 调用AdoImpl实例;
  347. AdoImpl tagAdoImpl;
  348. tagAdoImpl.SetConnectString(ADO_SQLSERVER, tagAdoDatabaseInfo);
  349. if ( tagAdoImpl.opendatabase() )
  350. {
  351. // 打开数据库失败;
  352. return;
  353. }
  354. // 返回结果;
  355. INT nRetVal = 0;
  356. // 字段数量;
  357. INT nFieldCount = 0;
  358. // 返回的记录集结构体数组;
  359. vector<Tbl_<#= host.GetModelClass(host.TableName) #>> vtRSet;
  360. TCHAR szSQL[MAX_PATH] = {0};
  361. TCHAR szFilters[MAX_PATH] = {0};
  362. nRetVal = tagAdoImpl.GetTbl_<#= host.GetModelClass(host.TableName) #>RecordSet(
  363. _T("<#= host.GetModelClass(host.TableName) #>"),
  364. szFilters,
  365. szSQL,
  366. nFieldCount,
  367. vtRSet);
  368. if ( nRetVal == -1 )
  369. {
  370. // 获取表数据集失败;
  371. return;
  372. }
  373. // 成功获取表数据集;
  374. // do something;
  375. // 关闭数据库连接;
  376. tagAdoImpl.closedatabase();
  377. }
  378. ///////////////////////////////////////////////////////////////////////////
  379. // 对表[<#= host.GetModelClass(host.TableName) #>]使用宏 INSERT_<#= host.GetModelClass(host.TableName) #> 插入数据;
  380. // 将vtValues的数据写入数据库表;
  381. void Call_INSERT_<#= host.GetModelClass(host.TableName) #>_MacroExample(IN vector<Tbl_<#= host.GetModelClass(host.TableName) #>> &vtValues)
  382. {
  383. // 获取配置文件信息;
  384. if ( -1 == GetIniInfo() )
  385. {
  386. // 打开配置文件失败;
  387. return;
  388. }
  389. STAdoDatabaseInfo tagAdoDatabaseInfo;
  390. _stprintf_s(tagAdoDatabaseInfo.szDataSource,_T("%s"),g_szDBSource);
  391. _stprintf_s(tagAdoDatabaseInfo.szDatabaseTCPPort,_T("%d"),g_dwDBServerPort);
  392. _stprintf_s(tagAdoDatabaseInfo.szDatabaseAccount,_T("%s"),g_szDBAccount);
  393. _stprintf_s(tagAdoDatabaseInfo.szDatabasePassword,_T("%s"),g_szDBPassWord);
  394. _stprintf_s(tagAdoDatabaseInfo.szDatabaseName,_T("%s"),g_szDBName);
  395. // 调用AdoImpl实例;
  396. AdoImpl tagAdoImpl;
  397. tagAdoImpl.SetConnectString(ADO_SQLSERVER, tagAdoDatabaseInfo);
  398. if ( tagAdoImpl.opendatabase() )
  399. {
  400. // 打开数据库失败;
  401. return;
  402. }
  403. CString strInsert = _T("");
  404. for ( vector<Tbl_<#= host.GetModelClass(host.TableName) #>>::iterator it = vtValues.begain(); it != vtValues.end(); it++ )
  405. {
  406. strInsert.Format(INSERT_<#= host.GetModelClass(host.TableName) #>, <# foreach ( ColumnInfo c in host.Fieldlist) { #>
  407. <# if ( c == host.Fieldlist[host.Fieldlist.Count - 1] ) { #>
  408. <# if (
  409. CodeCommon.DbTypeToCS(c.TypeName)=="int"||
  410. CodeCommon.DbTypeToCS(c.TypeName)=="long"||
  411. CodeCommon.DbTypeToCS(c.TypeName)=="float"||
  412. CodeCommon.DbTypeToCS(c.TypeName)=="decimal"||
  413. CodeCommon.DbTypeToCS(c.TypeName)=="bool") { #>
  414. it->_<#= c.ColumnName.ToString().ToLower() #> <# } #>
  415. <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#>
  416. it->_<#= c.ColumnName.ToString().ToLower() #>.c_str() <# } #>
  417. <# } else { #><# if (
  418. CodeCommon.DbTypeToCS(c.TypeName)=="int"||
  419. CodeCommon.DbTypeToCS(c.TypeName)=="long"||
  420. CodeCommon.DbTypeToCS(c.TypeName)=="float"||
  421. CodeCommon.DbTypeToCS(c.TypeName)=="decimal"||
  422. CodeCommon.DbTypeToCS(c.TypeName)=="bool") { #>
  423. it->_<#= c.ColumnName.ToString().ToLower() #>,<# } #>
  424. <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#>
  425. it->_<#= c.ColumnName.ToString().ToLower() #>.c_str(),<# } #><# } #><# } #>);
  426. if ( !tagAdoImpl.Execute(strInsert) )
  427. {
  428. // 执行SQL语句失败;
  429. continue;
  430. }
  431. }
  432. // 关闭数据库;
  433. tagAdoImpl.closedatabase();
  434. }