123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 |
- <#@ template language="c#" HostSpecific="True" #>
- <#@ output extension= ".cs" #>
- <#
- TableHost host = (TableHost)(Host);
- host.Fieldlist.Sort(CodeCommon.CompareByintOrder);
- #>
- // 为每个字段生成注释
- GO
- SET ANSI_PADDING OFF
- <# foreach (ColumnInfo c in host.Fieldlist) { #>
- GO
- EXEC sys.sp_addextendedproperty
- @name=N'MS_Description',
- @value=N'<#= c.ColumnName.ToString().ToLower() #>' ,
- @level0type=N'SCHEMA',
- @level0name=N'dbo',
- @level1type=N'TABLE',
- @level1name=N'<#= host.GetModelClass(host.TableName) #>',
- @level2type=N'COLUMN',
- @level2name=N'<#= c.ColumnName.ToString().ToLower() #>'
- <# } #>
- ///////////////////////////////////////////////////////////////////////////
- // 生成查询语句
- #define SELECT_<#= host.GetModelClass(host.TableName) #> _T("SELECT <# foreach (ColumnInfo c in host.Fieldlist) { #>
- <# if( c == host.Fieldlist[host.Fieldlist.Count-1] ) {#><#= c.ColumnName.ToString().ToLower() #><# } else { #>
- <#= c.ColumnName.ToString().ToLower() #>,<# } #><# } #> FROM <#= host.GetModelClass(host.TableName) #>")
- ///////////////////////////////////////////////////////////////////////////
- // 生成更新语句
- #define UPDATE_<#= host.GetModelClass(host.TableName) #> _T("UPDATE [<#= host.GetModelClass(host.TableName) #>] SET \
- <# foreach (ColumnInfo c in host.Fieldlist) { #>
- <# if( c == host.Fieldlist[host.Fieldlist.Count-1] ) {#>
- <#= c.ColumnName.ToString().ToLower() #>\
- <# } else { #>
- <#= c.ColumnName.ToString().ToLower() #>,\
- <# } #>
- <# } #>
- )VALUES(\
- <# foreach (ColumnInfo c in host.Fieldlist) { #>
- <# if( c == host.Fieldlist[host.Fieldlist.Count-1] ) {#>
- <# if (
- CodeCommon.DbTypeToCS(c.TypeName)=="int"||
- CodeCommon.DbTypeToCS(c.TypeName)=="long"||
- CodeCommon.DbTypeToCS(c.TypeName)=="float"||
- CodeCommon.DbTypeToCS(c.TypeName)=="decimal"||
- CodeCommon.DbTypeToCS(c.TypeName)=="bool")
- {#>
- %d<# } #>
- <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime")
- {#>
- '%s'
- <# } #>
- <# } else { #>
- <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="int"||
- CodeCommon.DbTypeToCS(c.TypeName)=="long"||
- CodeCommon.DbTypeToCS(c.TypeName)=="float"||
- CodeCommon.DbTypeToCS(c.TypeName)=="decimal"||
- CodeCommon.DbTypeToCS(c.TypeName)=="bool")
- {#>
- %d,\
- <# } #>
- <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime")
- {#>
- '%s',\
- <# } #>
- <# } #>
- <# } #>)")
- ///////////////////////////////////////////////////////////////////////////
- // 生成表对应的Insert宏;
- #define INSERT_<#= host.GetModelClass(host.TableName) #> _T("INSERT INTO [<#= host.GetModelClass(host.TableName) #>](\
- <# foreach (ColumnInfo c in host.Fieldlist) { #>
- <# if( c == host.Fieldlist[host.Fieldlist.Count-1] ) {#>
- <#= c.ColumnName.ToString().ToLower() #>\
- <# } else { #>
- <#= c.ColumnName.ToString().ToLower() #>,\
- <# } #>
- <# } #>
- )VALUES(\
- <# foreach (ColumnInfo c in host.Fieldlist) { #>
- <# if( c == host.Fieldlist[host.Fieldlist.Count-1] ) {#>
- <# if (
- CodeCommon.DbTypeToCS(c.TypeName)=="int"||
- CodeCommon.DbTypeToCS(c.TypeName)=="long"||
- CodeCommon.DbTypeToCS(c.TypeName)=="float"||
- CodeCommon.DbTypeToCS(c.TypeName)=="decimal"||
- CodeCommon.DbTypeToCS(c.TypeName)=="bool")
- {#>
- %d<# } #>
- <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime")
- {#>
- '%s'
- <# } #>
- <# } else { #>
- <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="int"||
- CodeCommon.DbTypeToCS(c.TypeName)=="long"||
- CodeCommon.DbTypeToCS(c.TypeName)=="float"||
- CodeCommon.DbTypeToCS(c.TypeName)=="decimal"||
- CodeCommon.DbTypeToCS(c.TypeName)=="bool")
- {#>
- %d,\
- <# } #>
- <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime")
- {#>
- '%s',\
- <# } #>
- <# } #>
- <# } #>)")
- ///////////////////////////////////////////////////////////////////////////
- // 生成表对应的结构体;
- typedef struct __Tbl_<#= host.GetModelClass(host.TableName) #>__
- {
- <# foreach (ColumnInfo c in host.Fieldlist) { #>
- <#= CodeCommon.DbTypeToCS(c.TypeName) #> _<#= c.ColumnName.ToString().ToLower() #>;
- <# } #>
- }Tbl<#= host.GetModelClass(host.TableName) #>,*pTbl<#= host.GetModelClass(host.TableName) #>;
- // 代码自动生成模板
- // 生成AdoImpl类的GetTblRecordSet模板函数;
- ///////////////////////////////////////////////////////////////////////////
- /************************************************************************/
- /*
- 函数:GetTbl_<#= host.GetModelClass(host.TableName) #>RecordSet
- 描述:获取表[<#= host.GetModelClass(host.TableName) #>]的记录集;
- 参数:
- IN: pTableName 表名;
- IN: pFilters 查找条件;
- IN: pSql SQL语句;
- IN: nFieldCount 查找的字段数量;
- OUT: vtRSet 返回的记录集对应的结构体数组;
- 返回:成功返回记录集大小, 失败返回 -1;
- */
- /************************************************************************/
- 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)
- {
- TCHAR szSQL[1024] = _T("");
- if(pFilters == NULL || _tcscmp(pFilters,_T("")) == 0)
- _stprintf_s(szSQL,_T("select count(*) as cot from %s"),pTableName);
- else
- _stprintf_s(szSQL, _T("select count(*) as cot from %s where %s"), pTableName, pFilters);
- int nRecordCount = 0;
- _variant_t vtFieldValue;
- bool bResult = false;
- EnterCriticalSection( &m_csAdo );
- bResult = GetADODateBaseStatus();
- if( false == bResult )
- {
- LeaveCriticalSection( &m_csAdo );
- return -1;
- }
- CADORecordset* pRSet = new CADORecordset(m_pADODatabase);
- LeaveCriticalSection( &m_csAdo );
- if( NULL == pRSet )
- {
- return -1;
- }
- bResult = OpenADORecordSet(pRSet, szSQL);
- if( false == bResult )
- {
- DeleteADORecordSet(pRSet);
- return -1;
- }
- if( !pRSet->IsFieldNull(_T("cot")) )
- {
- pRSet->GetFieldValue(_T("cot"), nRecordCount);
- }
- else
- {
- nRecordCount = -1;
- }
- DeleteADORecordSet(pRSet);
- pRSet = new CADORecordset(m_pADODatabase);
- LeaveCriticalSection( &m_csAdo );
- if( NULL == pRSet )
- {
- return -1;
- }
- bResult = OpenADORecordSet(pRSet, pSql);
- if( false == bResult )
- {
- DeleteADORecordSet(pRSet);
- return -1;
- }
- nFieldCount = pRSet->GetFieldCount();
- for ( int i = 0; i < nRecordCount; i++)
- {
- Tbl_<#= host.GetModelClass(host.TableName) #> tagVal;
- <# foreach (ColumnInfo c in host.Fieldlist){ #>
-
- if( !pRSet->IsFieldNull(_T("<#= c.ColumnName.ToString() #>")) )
- {
- pRSet->GetFieldValue(_T("<#= c.ColumnName.ToString() #>"), vtFieldValue);
- <# if(CodeCommon.DbTypeToCS(c.TypeName)=="int"||
- CodeCommon.DbTypeToCS(c.TypeName)=="long"||
- CodeCommon.DbTypeToCS(c.TypeName)=="float"||
- CodeCommon.DbTypeToCS(c.TypeName)=="decimal"||
- CodeCommon.DbTypeToCS(c.TypeName)=="bool") {#>
- tagVal._<#= c.ColumnName.ToString().ToLower() #> = vtFieldValue;
- <# } #>
- <# if(CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#>
- tagVal._<#= c.ColumnName.ToString().ToLower() #> = TString((TCHAR*)(_bstr_t)vtFieldValue);
- <# } #>
- }
- else
- {
- <# if(CodeCommon.DbTypeToCS(c.TypeName)=="int"||
- CodeCommon.DbTypeToCS(c.TypeName)=="long"||
- CodeCommon.DbTypeToCS(c.TypeName)=="float"||
- CodeCommon.DbTypeToCS(c.TypeName)=="bool"||
- CodeCommon.DbTypeToCS(c.TypeName)=="decimal") {#>
- tagVal._<#= c.ColumnName.ToString().ToLower() #> = 0;
- <# } #>
- <# if(CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#>
- tagVal._<#= c.ColumnName.ToString().ToLower() #> = _T("");
- <# } #>
- }
- <# } #>
-
- vtRSet.push_back(tagVal);
- pRSet->MoveNext();
- }
-
- DeleteADORecordSet(pRSet);
- return nRecordCount;
- }
- // 代码自动生成模板
- // 生成AdoImpl类的GetTblRecordSet模板函数;
- ///////////////////////////////////////////////////////////////////////////
- /************************************************************************/
- /*
- 函数:GetTbl_<#= host.GetModelClass(host.TableName) #>RecordSet
- 描述:获取表[<#= host.GetModelClass(host.TableName) #>]的记录集;
- 参数:
- IN: pTableName 表名;
- IN: pFilters 查找条件;
- IN: pSql SQL语句;
- IN: nFieldCount 查找的字段数量;
- OUT: vtRSet 返回的记录集对应的结构体数组;
- 返回:成功返回记录集大小, 失败返回 -1;
- */
- /************************************************************************/
- 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)
- {
- TCHAR szSQL[1024] = _T("");
- if(pFilters == NULL || _tcscmp(pFilters,_T("")) == 0)
- _stprintf_s(szSQL,_T("select count(*) as cot from %s"),pTableName);
- else
- _stprintf_s(szSQL, _T("select count(*) as cot from %s where %s"), pTableName, pFilters);
- int nRecordCount = 0;
- _variant_t vtFieldValue;
- bool bResult = false;
- EnterCriticalSection( &m_csAdo );
- bResult = GetADODateBaseStatus();
- if( false == bResult )
- {
- LeaveCriticalSection( &m_csAdo );
- return -1;
- }
- CADORecordset* pRSet = new CADORecordset(m_pADODatabase);
- LeaveCriticalSection( &m_csAdo );
- if( NULL == pRSet )
- {
- return -1;
- }
- bResult = OpenADORecordSet(pRSet, szSQL);
- if( false == bResult )
- {
- DeleteADORecordSet(pRSet);
- return -1;
- }
- if( !pRSet->IsFieldNull(_T("cot")) )
- {
- pRSet->GetFieldValue(_T("cot"), nRecordCount);
- }
- else
- {
- nRecordCount = -1;
- }
- DeleteADORecordSet(pRSet);
- pRSet = new CADORecordset(m_pADODatabase);
- LeaveCriticalSection( &m_csAdo );
- if( NULL == pRSet )
- {
- return -1;
- }
- bResult = OpenADORecordSet(pRSet, pSql);
- if( false == bResult )
- {
- DeleteADORecordSet(pRSet);
- return -1;
- }
- TCHAR buffer[65];
- DWORD dwOldSize = AryRSet.GetSize();
- AryRSet.SetSize(dwOldSize + nRecordCount,1);
- INT nIndex = dwOldSize == 0 ? 0 : dwOldSize - 1;
- nFieldCount = pRSet->GetFieldCount();
- for ( int i = nIndex; i < nRecordCount; i++)
- {
- AryRSet.ElementAt(i).RemoveAll();
- <# foreach (ColumnInfo c in host.Fieldlist){ #>
-
- if( !pRSet->IsFieldNull(_T("<#= c.ColumnName.ToString() #>")) )
- {
- pRSet->GetFieldValue(_T("<#= c.ColumnName.ToString() #>"), vtFieldValue);
- <# if(CodeCommon.DbTypeToCS(c.TypeName)=="int"||
- CodeCommon.DbTypeToCS(c.TypeName)=="long"||
- CodeCommon.DbTypeToCS(c.TypeName)=="float"||
- CodeCommon.DbTypeToCS(c.TypeName)=="decimal"||
- CodeCommon.DbTypeToCS(c.TypeName)=="bool") {#>
- _itoa( vtFieldValue, buffer, 10 );
- AryRSet.ElementAt(i).Add(buffer);
- <# } #>
- <# if(CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#>
- AryRSet.ElementAt(i).Add((TCHAR*)(_bstr_t)vtFieldValue);
- <# } #>
- }
- else
- {
- <# if(CodeCommon.DbTypeToCS(c.TypeName)=="int"||
- CodeCommon.DbTypeToCS(c.TypeName)=="long"||
- CodeCommon.DbTypeToCS(c.TypeName)=="float"||
- CodeCommon.DbTypeToCS(c.TypeName)=="bool"||
- CodeCommon.DbTypeToCS(c.TypeName)=="decimal") {#>
- AryRSet.ElementAt(i).Add(_T(""));
- <# } #>
- <# if(CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#>
- AryRSet.ElementAt(i).Add(_T(""));
- <# } #>
- }
- <# } #>
-
- pRSet->MoveNext();
- }
-
- DeleteADORecordSet(pRSet);
- return nRecordCount;
- }
- ///////////////////////////////////////////////////////////////////////////
- // AdomImpl::GetTbl_<#= host.GetModelClass(host.TableName) #>RecordSet()的调用实例;
- //
- void Call_GetTbl_<#= host.GetModelClass(host.TableName) #>RecordSet_Example()
- {
- // 获取配置文件信息;
- if ( -1 == GetIniInfo() )
- {
- // 打开配置文件失败;
- return;
- }
- STAdoDatabaseInfo tagAdoDatabaseInfo;
- _stprintf_s(tagAdoDatabaseInfo.szDataSource,_T("%s"),g_szDBSource);
- _stprintf_s(tagAdoDatabaseInfo.szDatabaseTCPPort,_T("%d"),g_dwDBServerPort);
- _stprintf_s(tagAdoDatabaseInfo.szDatabaseAccount,_T("%s"),g_szDBAccount);
- _stprintf_s(tagAdoDatabaseInfo.szDatabasePassword,_T("%s"),g_szDBPassWord);
- _stprintf_s(tagAdoDatabaseInfo.szDatabaseName,_T("%s"),g_szDBName);
-
- // 调用AdoImpl实例;
- AdoImpl tagAdoImpl;
- tagAdoImpl.SetConnectString(ADO_SQLSERVER, tagAdoDatabaseInfo);
- if ( tagAdoImpl.opendatabase() )
- {
- // 打开数据库失败;
- return;
- }
-
- // 返回结果;
- INT nRetVal = 0;
- // 字段数量;
- INT nFieldCount = 0;
- // 返回的记录集结构体数组;
- vector<Tbl_<#= host.GetModelClass(host.TableName) #>> vtRSet;
-
- TCHAR szSQL[MAX_PATH] = {0};
- TCHAR szFilters[MAX_PATH] = {0};
-
- nRetVal = tagAdoImpl.GetTbl_<#= host.GetModelClass(host.TableName) #>RecordSet(
- _T("<#= host.GetModelClass(host.TableName) #>"),
- szFilters,
- szSQL,
- nFieldCount,
- vtRSet);
-
- if ( nRetVal == -1 )
- {
- // 获取表数据集失败;
- return;
- }
-
- // 成功获取表数据集;
- // do something;
-
- // 关闭数据库连接;
- tagAdoImpl.closedatabase();
- }
- ///////////////////////////////////////////////////////////////////////////
- // 对表[<#= host.GetModelClass(host.TableName) #>]使用宏 INSERT_<#= host.GetModelClass(host.TableName) #> 插入数据;
- // 将vtValues的数据写入数据库表;
- void Call_INSERT_<#= host.GetModelClass(host.TableName) #>_MacroExample(IN vector<Tbl_<#= host.GetModelClass(host.TableName) #>> &vtValues)
- {
- // 获取配置文件信息;
- if ( -1 == GetIniInfo() )
- {
- // 打开配置文件失败;
- return;
- }
- STAdoDatabaseInfo tagAdoDatabaseInfo;
- _stprintf_s(tagAdoDatabaseInfo.szDataSource,_T("%s"),g_szDBSource);
- _stprintf_s(tagAdoDatabaseInfo.szDatabaseTCPPort,_T("%d"),g_dwDBServerPort);
- _stprintf_s(tagAdoDatabaseInfo.szDatabaseAccount,_T("%s"),g_szDBAccount);
- _stprintf_s(tagAdoDatabaseInfo.szDatabasePassword,_T("%s"),g_szDBPassWord);
- _stprintf_s(tagAdoDatabaseInfo.szDatabaseName,_T("%s"),g_szDBName);
-
- // 调用AdoImpl实例;
- AdoImpl tagAdoImpl;
- tagAdoImpl.SetConnectString(ADO_SQLSERVER, tagAdoDatabaseInfo);
- if ( tagAdoImpl.opendatabase() )
- {
- // 打开数据库失败;
- return;
- }
-
- CString strInsert = _T("");
- for ( vector<Tbl_<#= host.GetModelClass(host.TableName) #>>::iterator it = vtValues.begain(); it != vtValues.end(); it++ )
- {
- strInsert.Format(INSERT_<#= host.GetModelClass(host.TableName) #>, <# foreach ( ColumnInfo c in host.Fieldlist) { #>
- <# if ( c == host.Fieldlist[host.Fieldlist.Count - 1] ) { #>
- <# if (
- CodeCommon.DbTypeToCS(c.TypeName)=="int"||
- CodeCommon.DbTypeToCS(c.TypeName)=="long"||
- CodeCommon.DbTypeToCS(c.TypeName)=="float"||
- CodeCommon.DbTypeToCS(c.TypeName)=="decimal"||
- CodeCommon.DbTypeToCS(c.TypeName)=="bool") { #>
- it->_<#= c.ColumnName.ToString().ToLower() #> <# } #>
- <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#>
- it->_<#= c.ColumnName.ToString().ToLower() #>.c_str() <# } #>
- <# } else { #><# if (
- CodeCommon.DbTypeToCS(c.TypeName)=="int"||
- CodeCommon.DbTypeToCS(c.TypeName)=="long"||
- CodeCommon.DbTypeToCS(c.TypeName)=="float"||
- CodeCommon.DbTypeToCS(c.TypeName)=="decimal"||
- CodeCommon.DbTypeToCS(c.TypeName)=="bool") { #>
- it->_<#= c.ColumnName.ToString().ToLower() #>,<# } #>
- <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#>
- it->_<#= c.ColumnName.ToString().ToLower() #>.c_str(),<# } #><# } #><# } #>);
-
- if ( !tagAdoImpl.Execute(strInsert) )
- {
- // 执行SQL语句失败;
- continue;
- }
- }
-
- // 关闭数据库;
- tagAdoImpl.closedatabase();
- }
|