DBInterface.cpp 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092
  1. /*************************************************************
  2. /* Copyright (C), 2008-2010, StoneU Tech. Co., Ltd.
  3. /* 文件名: DBInterface.h
  4. /* 作者: Jesse
  5. /* 创建日期: 2010-08-02
  6. /* 版本号: V1.0
  7. /* 描述: 实现多种数据库的支持
  8. /* 其它:
  9. /* 主要类模块: CDBInterface
  10. /* 历史修改记录:
  11. 作者 时间 版本 描述
  12. Jesse 10/08/02 1.0 创建这个模块
  13. ***************************************************************/
  14. #include "stdafx.h"
  15. #include "DBInterface.h"
  16. namespace DBInterface
  17. {
  18. CDBInterface* CDBInterface::m_pDBInterface = NULL;
  19. // 静态成员函数,提供全局访问的接口
  20. CDBInterface* CDBInterface::GetInstancePtr()
  21. {
  22. if( NULL == m_pDBInterface )
  23. {
  24. m_pDBInterface = new CDBInterface();
  25. }
  26. return m_pDBInterface;
  27. }
  28. // 释放资源
  29. void CDBInterface::Release()
  30. {
  31. if( m_pDBInterface )
  32. delete m_pDBInterface;
  33. m_pDBInterface = NULL;
  34. }
  35. // 设置数据库类型
  36. void CDBInterface::SetDBType(char *pDBType)
  37. {
  38. strcpy(m_chDBType, pDBType);
  39. }
  40. //插入设备资料
  41. INT CDBInterface::InsertDevInfo(int nID, CHAR *pUid, int nPort, int nAddr, CHAR *pDevName, int nDevTypeID,
  42. int nRate, int nDataBit, int nStopBit, int nParityBit, int nRsMode,
  43. CHAR *pIPAddr, int nIPPort, int nCommMode, int nDevFlag, int nSpecialDev,
  44. int nTimeout, int nIsuse, CHAR *pProtocalName, CHAR *pIniName,
  45. CHAR *pReserved1, CHAR *pReserved2, CHAR *pReserved3, CHAR *pReserved4, CHAR *pReserved5,
  46. CHAR *pReserved6, CHAR *pReserved7, CHAR *pReserved8, CHAR *pReserved9, CHAR *pReserved10,
  47. int nReserved1, int nReserved2, int nReserved3, int nReserved4, int nReserved5,
  48. int nReserved6, int nReserved7, int nReserved8, int nReserved9, int nReserved10,
  49. int bReserved1, int bReserved2, int bReserved3, int bReserved4, int bReserved5,
  50. int bReserved6, int bReserved7, int bReserved8, int bReserved9, int bReserved10)
  51. {
  52. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  53. sprintf(strSQLText, "insert into t_dev_property(id, uid, port, devideaddr, devicename, " \
  54. "devicetype, baudrate, databit, stopbit, paritybit, ip, ipport, " \
  55. "commmode, deviceflag, specialdevice, timeout, isuse, protocoldllname, ininame, " \
  56. "strreserved1, strreserved2, strreserved3, strreserved4, strreserved5, " \
  57. "strreserved6, strreserved7, strreserved8, strreserved9, strreserved10, " \
  58. "intreserved1, intreserved2, intreserved3, intreserved4, intreserved5, " \
  59. "intreserved6, intreserved7, intreserved8, intreserved9, intreserved10, " \
  60. "boolreserved1, boolreserved2, boolreserved3, boolreserved4, boolreserved5, " \
  61. "boolreserved6, boolreserved7, boolreserved8, boolreserved9, boolreserved10 " \
  62. ") values (" \
  63. "%d, \'%s\', %d, %d, \'%s\', " \
  64. "%d, %d, %d, %d, %d, \'%s\', %d, " \
  65. "%d, %d, \'%d\', %d, \'%d\', \'%s\', \'%s\', " \
  66. "\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', " \
  67. "\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', " \
  68. "%d, %d, %d, %d, %d, " \
  69. "%d, %d, %d, %d, %d, " \
  70. "\'%d\', \'%d\', \'%d\', \'%d\', \'%d\', " \
  71. "\'%d\', \'%d\', \'%d\', \'%d\', \'%d\' " \
  72. ")",
  73. nID, pUid, nPort, nAddr, pDevName, nDevTypeID, nRate, nDataBit, nStopBit,
  74. nParityBit, pIPAddr, nIPPort, nCommMode, nDevFlag, nSpecialDev,
  75. nTimeout, nIsuse, pProtocalName, pIniName,
  76. pReserved1, pReserved2, pReserved3, pReserved4, pReserved5,
  77. pReserved6, pReserved7, pReserved8, pReserved9, pReserved10,
  78. nReserved1, nReserved2, nReserved3, nReserved4, nReserved5,
  79. nReserved6, nReserved7, nReserved8, nReserved9, nReserved10,
  80. bReserved1, bReserved2, bReserved3, bReserved4, bReserved5,
  81. bReserved6, bReserved7, bReserved8, bReserved9, bReserved10
  82. );
  83. return CDBConnection::GetInstancePtr()->Execute(strSQLText);
  84. }
  85. //修改设备资料,标准的SQL,多种数据库都一样
  86. INT CDBInterface::EditDevInfo(CHAR *pOldUid, CHAR *pNewUid, int nPort, int nAddr, CHAR *pDevName, int nDevTypeID,
  87. int nRate, int nDataBit, int nStopBit, int nParityBit, int nRsMode,
  88. CHAR *pIPAddr, int nIPPort, int nCommMode, int nDevFlag, int nSpecialDev,
  89. int nTimeout, int nIsuse, CHAR *pProtocalName, CHAR *pIniName,
  90. CHAR *pReserved1, CHAR *pReserved2, CHAR *pReserved3, CHAR *pReserved4, CHAR *pReserved5,
  91. CHAR *pReserved6, CHAR *pReserved7, CHAR *pReserved8, CHAR *pReserved9, CHAR *pReserved10,
  92. int nReserved1, int nReserved2, int nReserved3, int nReserved4, int nReserved5,
  93. int nReserved6, int nReserved7, int nReserved8, int nReserved9, int nReserved10,
  94. int bReserved1, int bReserved2, int bReserved3, int bReserved4, int bReserved5,
  95. int bReserved6, int bReserved7, int bReserved8, int bReserved9, int bReserved10)
  96. {
  97. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  98. sprintf(strSQLText, "update t_dev_property set uid = \'%s\', port = %d, devideaddr = %d, devicename = \'%s\', " \
  99. "devicetype = %d, baudrate = %d, databit = %d, stopbit = %d, paritybit = %d, ip = \'%s\', ipport = %d, " \
  100. "commmode = %d, deviceflag = %d, specialdevice = \'%d\', timeout = %d, isuse = \'%d\', protocoldllname = \'%s\', " \
  101. "ininame = \'%s\', " \
  102. "strreserved1 = \'%s\', strreserved2 = \'%s\', strreserved3 = \'%s\', strreserved4 = \'%s\', strreserved5 = \'%s\', " \
  103. "strreserved6 = \'%s\', strreserved7 = \'%s\', strreserved8 = \'%s\', strreserved9 = \'%s\', strreserved10 = \'%s\', " \
  104. "intreserved1 = %d, intreserved2 = %d, intreserved3 = %d, intreserved4 = %d, intreserved5 = %d, " \
  105. "intreserved6 = %d, intreserved7 = %d, intreserved8 = %d, intreserved9 = %d, intreserved10 = %d, " \
  106. "boolreserved1 = \'%d\', boolreserved2 = \'%d\', boolreserved3 = \'%d\', boolreserved4 = \'%d\', boolreserved5 = \'%d\', " \
  107. "boolreserved6 = \'%d\', boolreserved7 = \'%d\', boolreserved8 = \'%d\', boolreserved9 = \'%d\', boolreserved10 = \'%d\' " \
  108. "where uid = \'%s\'",
  109. pNewUid, nPort, nAddr, pDevName, nDevTypeID, nRate, nDataBit, nStopBit,
  110. nParityBit, pIPAddr, nIPPort, nCommMode, nDevFlag, nSpecialDev,
  111. nTimeout, nIsuse, pProtocalName, pIniName,
  112. pReserved1, pReserved2, pReserved3, pReserved4, pReserved5,
  113. pReserved6, pReserved7, pReserved8, pReserved9, pReserved10,
  114. nReserved1, nReserved2, nReserved3, nReserved4, nReserved5,
  115. nReserved6, nReserved7, nReserved8, nReserved9, nReserved10,
  116. bReserved1, bReserved2, bReserved3, bReserved4, bReserved5,
  117. bReserved6, bReserved7, bReserved8, bReserved9, bReserved10,
  118. pOldUid);
  119. return CDBConnection::GetInstancePtr()->Execute(strSQLText);
  120. }
  121. //删除设备资料
  122. INT CDBInterface::DeleteDevInfo(CHAR *pUid)
  123. {
  124. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  125. sprintf(strSQLText, "delete from t_dev_property where uid = \'%s\'", pUid);
  126. return CDBConnection::GetInstancePtr()->Execute(strSQLText);
  127. }
  128. //获取设备类型ID
  129. INT CDBInterface::GetDevTypeID( CHAR *pDevTypeName, CHAR *pDevTypeID )
  130. {
  131. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  132. list<string> list1;
  133. list<string>::iterator it;
  134. if (!_stricmp(m_chDBType, "SQL SERVER"))
  135. {
  136. sprintf(strSQLText, "select top 1 id from t_dev_type where equiptype = \'%s\'", pDevTypeName);
  137. }
  138. else if (!_stricmp(m_chDBType, "ACCESS97"))
  139. {
  140. sprintf(strSQLText, "select top 1 id from t_dev_type where equiptype = \'%s\'", pDevTypeName);
  141. }
  142. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  143. {
  144. sprintf(strSQLText, "select top 1 id from t_dev_type where equiptype = \'%s\'", pDevTypeName);
  145. }
  146. else if(!_stricmp(m_chDBType, "PGSQL"))
  147. {
  148. sprintf(strSQLText, "select id from t_dev_type where equiptype = \'%s\' limit 1", pDevTypeName);
  149. }
  150. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  151. if( 0 == list1.size() )
  152. {
  153. return 0;
  154. }
  155. if( nRet != -1 )
  156. {
  157. strcpy(pDevTypeID, list1.begin()->c_str());
  158. }
  159. for( it = list1.begin(); it != list1.end(); )
  160. {
  161. list1.erase(it++);
  162. }
  163. return nRet;
  164. }
  165. //获取指定厂家的设备型号ID
  166. INT CDBInterface::GetDevTypeID( int nDriverID, int nFactoryID, CHAR *pDevTypeName, CHAR *pDevTypeID )
  167. {
  168. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  169. list<string> list1;
  170. list<string>::iterator it;
  171. if (!_stricmp(m_chDBType, "SQL SERVER"))
  172. {
  173. sprintf(strSQLText, "select top 1 id from t_dev_type where driveid = %d and factoryid = %d and equiptype = \'%s\'",
  174. nDriverID, nFactoryID, pDevTypeName);
  175. }
  176. else if (!_stricmp(m_chDBType, "ACCESS97"))
  177. {
  178. sprintf(strSQLText, "select top 1 id from t_dev_type where driveid = %d and factoryid = %d and equiptype = \'%s\'",
  179. nDriverID, nFactoryID, pDevTypeName);
  180. }
  181. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  182. {
  183. sprintf(strSQLText, "select top 1 id from t_dev_type where driveid = %d and factoryid = %d and equiptype = \'%s\'",
  184. nDriverID, nFactoryID, pDevTypeName);
  185. }
  186. else if(!_stricmp(m_chDBType, "PGSQL"))
  187. {
  188. sprintf(strSQLText, "select id from t_dev_type where driveid = %d and factoryid = %d and equiptype = \'%s\' limit 1",
  189. nDriverID, nFactoryID, pDevTypeName);
  190. }
  191. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  192. if( 0 == list1.size() )
  193. {
  194. return 0;
  195. }
  196. if( nRet != -1 )
  197. {
  198. strcpy(pDevTypeID, list1.begin()->c_str());
  199. }
  200. for( it = list1.begin(); it != list1.end(); )
  201. {
  202. list1.erase(it++);
  203. }
  204. return nRet;
  205. }
  206. //获取设备名称
  207. INT CDBInterface::GetDevName( CHAR *pDevUID, CHAR *pDevName )
  208. {
  209. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  210. list<string> list1;
  211. list<string>::iterator it;
  212. if (!_stricmp(m_chDBType, "SQL SERVER"))
  213. {
  214. sprintf(strSQLText, "select top 1 devicename from t_dev_property where uid = \'%s\'", pDevUID);
  215. }
  216. else if (!_stricmp(m_chDBType, "ACCESS97"))
  217. {
  218. sprintf(strSQLText, "select top 1 devicename from t_dev_property where uid = \'%s\'", pDevUID);
  219. }
  220. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  221. {
  222. sprintf(strSQLText, "select top 1 devicename from t_dev_property where uid = \'%s\'", pDevUID);
  223. }
  224. else if(!_stricmp(m_chDBType, "PGSQL"))
  225. {
  226. sprintf(strSQLText, "select devicename from t_dev_property where uid = \'%s\' LIMIT 1", pDevUID);
  227. }
  228. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  229. if( 0 == list1.size() )
  230. {
  231. return 0;
  232. }
  233. if( nRet != -1 )
  234. {
  235. strcpy(pDevName, list1.begin()->c_str());
  236. }
  237. for( it = list1.begin(); it != list1.end(); )
  238. {
  239. list1.erase(it++);
  240. }
  241. return nRet;
  242. }
  243. //获取设备UID
  244. INT CDBInterface::GetDevUID( CHAR *pDevName, CHAR *pDevUID )
  245. {
  246. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  247. list<string> list1;
  248. list<string>::iterator it;
  249. if (!_stricmp(m_chDBType, "SQL SERVER"))
  250. {
  251. sprintf(strSQLText, "select top 1 uid from t_dev_property where devicename = \'%s\'", pDevName);
  252. }
  253. else if (!_stricmp(m_chDBType, "ACCESS97"))
  254. {
  255. sprintf(strSQLText, "select top 1 uid from t_dev_property where devicename = \'%s\'", pDevName);
  256. }
  257. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  258. {
  259. sprintf(strSQLText, "select top 1 uid from t_dev_property where devicename = \'%s\'", pDevName);
  260. }
  261. else if(!_stricmp(m_chDBType, "PGSQL"))
  262. {
  263. sprintf(strSQLText, "select uid from t_dev_property where devicename = \'%s\' LIMIT 1", pDevName);
  264. }
  265. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  266. if( 0 == list1.size() )
  267. {
  268. return 0;
  269. }
  270. if( nRet != -1 )
  271. {
  272. strcpy(pDevUID, list1.begin()->c_str());
  273. }
  274. for( it = list1.begin(); it != list1.end(); )
  275. {
  276. list1.erase(it++);
  277. }
  278. return nRet;
  279. }
  280. //获取表最大的ID, 通用函数
  281. INT CDBInterface::GetMaxID( CHAR *pTableName, CHAR *pFieldName, CHAR *pMaxID )
  282. {
  283. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  284. list<string> list1;
  285. list<string>::iterator it;
  286. sprintf(strSQLText, "select max(%s) as maxid from %s", pFieldName, pTableName);
  287. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  288. if( 0 == list1.size() )
  289. {
  290. return 0;
  291. }
  292. if( nRet != -1 )
  293. {
  294. strcpy(pMaxID, list1.begin()->c_str());
  295. }
  296. for( it = list1.begin(); it != list1.end(); )
  297. {
  298. list1.erase(it++);
  299. }
  300. return nRet;
  301. }
  302. //获取驱动ID
  303. INT CDBInterface::GetDevDriveID( CHAR *pDevDriverName, CHAR *pDevDriverID )
  304. {
  305. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  306. list<string> list1;
  307. list<string>::iterator it;
  308. if (!_stricmp(m_chDBType, "SQL SERVER"))
  309. {
  310. sprintf(strSQLText, "select top 1 id from t_dev_drive where drivename = \'%s\'", pDevDriverName);
  311. }
  312. else if (!_stricmp(m_chDBType, "ACCESS97"))
  313. {
  314. sprintf(strSQLText, "select top 1 id from t_dev_drive where drivename = \'%s\'", pDevDriverName);
  315. }
  316. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  317. {
  318. sprintf(strSQLText, "select top 1 id from t_dev_drive where drivename = \'%s\'", pDevDriverName);
  319. }
  320. else if(!_stricmp(m_chDBType, "PGSQL"))
  321. {
  322. sprintf(strSQLText, "select id from t_dev_drive where drivename = \'%s\' limit 1", pDevDriverName);
  323. }
  324. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  325. if( 0 == list1.size() )
  326. {
  327. return 0;
  328. }
  329. if( nRet != -1 )
  330. {
  331. strcpy(pDevDriverID, list1.begin()->c_str());
  332. }
  333. for( it = list1.begin(); it != list1.end(); )
  334. {
  335. list1.erase(it++);
  336. }
  337. return nRet;
  338. }
  339. //获取指定驱动的厂家ID
  340. INT CDBInterface::GetDevFactoryID( int nDriverID, CHAR *pDevFactoryName, CHAR *pDevFactoryID )
  341. {
  342. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  343. list<string> list1;
  344. list<string>::iterator it;
  345. if (!_stricmp(m_chDBType, "SQL SERVER"))
  346. {
  347. sprintf(strSQLText, "select top 1 factoryid from t_dev_factory where deviceid = %d and factoryname = \'%s\'",
  348. nDriverID, pDevFactoryName);
  349. }
  350. else if (!_stricmp(m_chDBType, "ACCESS97"))
  351. {
  352. sprintf(strSQLText, "select top 1 factoryid from t_dev_factory where deviceid = %d and factoryname = \'%s\'",
  353. nDriverID, pDevFactoryName);
  354. }
  355. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  356. {
  357. sprintf(strSQLText, "select top 1 factoryid from t_dev_factory where deviceid = %d and factoryname = \'%s\'",
  358. nDriverID, pDevFactoryName);
  359. }
  360. else if(!_stricmp(m_chDBType, "PGSQL"))
  361. {
  362. sprintf(strSQLText, "select factoryid from t_dev_factory where deviceid = %d and factoryname = \'%s\' limit 1",
  363. nDriverID, pDevFactoryName);
  364. }
  365. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  366. if( 0 == list1.size() )
  367. {
  368. return 0;
  369. }
  370. if( nRet != -1 )
  371. {
  372. strcpy(pDevFactoryID, list1.begin()->c_str());
  373. }
  374. for( it = list1.begin(); it != list1.end(); )
  375. {
  376. list1.erase(it++);
  377. }
  378. return nRet;
  379. }
  380. //根据UID查找同一类型设备的个数
  381. INT CDBInterface::FindDevInfoByUID( CHAR *pUID, CHAR *pDevNum )
  382. {
  383. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  384. list<string> list1;
  385. list<string>::iterator it;
  386. #if 1
  387. if (!_stricmp(m_chDBType, "SQL SERVER"))
  388. {
  389. sprintf(strSQLText, "select uid from t_dev_property where uid like \'%s%s%s\' order by id desc", "%", pUID, "%");
  390. }
  391. else if (!_stricmp(m_chDBType, "ACCESS97"))
  392. {
  393. sprintf(strSQLText, "select uid from t_dev_property where uid like \'%s%s%s\' order by id desc", "%", pUID, "%");
  394. }
  395. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  396. {
  397. sprintf(strSQLText, "select uid from t_dev_property where uid like \'%s%s%s\' order by id desc", "%", pUID, "%");
  398. }
  399. else if(!_stricmp(m_chDBType, "PGSQL"))
  400. {
  401. sprintf(strSQLText, "select uid from t_dev_property where uid like \'%s%s%s\' order by id desc", "%", pUID, "%");
  402. }
  403. #else
  404. if (!_stricmp(m_chDBType, "SQL SERVER"))
  405. {
  406. sprintf(strSQLText, "select count(id) as Num from t_dev_property where uid like \'%s%s%s\'", "%", pUID, "%");
  407. }
  408. else if (!_stricmp(m_chDBType, "ACCESS97"))
  409. {
  410. sprintf(strSQLText, "select count(id) as Num from t_dev_property where uid like \'%s%s%s\'", "%", pUID, "%");
  411. }
  412. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  413. {
  414. sprintf(strSQLText, "select count(id) as Num from t_dev_property where uid like \'%s%s%s\'", "%", pUID, "%");
  415. }
  416. else if(!_stricmp(m_chDBType, "PGSQL"))
  417. {
  418. sprintf(strSQLText, "select count(id) as Num from t_dev_property where uid like \'%s%s%s\'", "%", pUID, "%");
  419. }
  420. #endif
  421. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  422. if( 0 == list1.size() )
  423. {
  424. return 0;
  425. }
  426. if( nRet != -1 )
  427. {
  428. CString sTemp = list1.begin()->c_str();
  429. CString s;
  430. for( int i=sTemp.GetLength()-1;i>0;i-- )
  431. {
  432. if( sTemp[i]=='.' )
  433. {
  434. s = sTemp.Mid( i+1,sTemp.GetLength()-i );
  435. break;
  436. }
  437. }
  438. strcpy(pDevNum, s);
  439. }
  440. for( it = list1.begin(); it != list1.end(); )
  441. {
  442. list1.erase(it++);
  443. }
  444. return nRet;
  445. }
  446. //得到时间计划
  447. INT CDBInterface::GetDatePlan( int nPlanType, CHAR *pPlanID, unsigned char nBufDate[7][24] )
  448. {
  449. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  450. list<string> list1, list2, list3, list4;
  451. list<string>::iterator it_list1, it_list2, it_list3, it_list4;
  452. sprintf(strSQLText, "select starttime, endtime, startweekofday, endweekofday " \
  453. "from t_plan_info where planid=\'%s\' and plantype = %d",
  454. pPlanID, nPlanType);
  455. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList4(strSQLText, list1, list2, list3, list4);
  456. if( list1.size() == 0 || list2.size() == 0 || list3.size() == 0 || list4.size() == 0 )
  457. {
  458. return 0;
  459. }
  460. if( nRet != -1 )
  461. {
  462. for( it_list1 = list1.begin(), it_list2 = list2.begin(), it_list3 = list3.begin(), it_list4 = list4.begin();
  463. it_list1 != list1.end() && it_list2 != list2.end() && it_list3 != list3.end() && it_list4 != list4.end();
  464. it_list1++, it_list2++, it_list3++, it_list4++)
  465. {
  466. for( int i = atoi((*it_list3).c_str()); i < atoi((*it_list4).c_str()) + 1; i++ )
  467. {
  468. for( int j = atoi((*it_list1).c_str()); j < atoi((*it_list2).c_str()) + 1; j++ )
  469. {
  470. nBufDate[i][j] = 15;
  471. }
  472. }
  473. }
  474. }
  475. for( it_list1 = list1.begin(), it_list2 = list2.begin(), it_list3 = list3.begin(), it_list4 = list4.begin();
  476. it_list1 != list1.end(), it_list2 != list2.end(), it_list3 != list3.end(), it_list4 != list4.end(); )
  477. {
  478. list1.erase(it_list1++);
  479. list2.erase(it_list2++);
  480. list3.erase(it_list3++);
  481. list4.erase(it_list4++);
  482. }
  483. return nRet;
  484. }
  485. //得到时间计划
  486. INT CDBInterface::GetDatePlan( int nPlanType, CHAR *pUid, int nVarID, unsigned char nBufDate[7][24] )
  487. {
  488. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  489. list<string> list1, list2, list3, list4;
  490. list<string>::iterator it_list1, it_list2, it_list3, it_list4;
  491. sprintf(strSQLText, "select starttime, endtime, startweekofday, endweekofday " \
  492. "from t_plan_info where uid=\'%s\' and varid = %d and plantype = %d",
  493. pUid, nVarID, nPlanType);
  494. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList4(strSQLText, list1, list2, list3, list4);
  495. if( list1.size() == 0 || list2.size() == 0 || list3.size() == 0 || list4.size() == 0 )
  496. {
  497. return 0;
  498. }
  499. if( nRet != -1 )
  500. {
  501. for( it_list1 = list1.begin(), it_list2 = list2.begin(), it_list3 = list3.begin(), it_list4 = list4.begin();
  502. it_list1 != list1.end() && it_list2 != list2.end() && it_list3 != list3.end() && it_list4 != list4.end();
  503. it_list1++, it_list2++, it_list3++, it_list4++ )
  504. {
  505. for( int i = atoi((*it_list3).c_str()); i < atoi((*it_list4).c_str()) + 1; i++ )
  506. {
  507. for( int j = atoi((*it_list1).c_str()); j < atoi((*it_list2).c_str()) + 1; j++ )
  508. {
  509. nBufDate[i][j] = 15;
  510. }
  511. }
  512. }
  513. }
  514. for( it_list1 = list1.begin(), it_list2 = list2.begin(), it_list3 = list3.begin(), it_list4 = list4.begin();
  515. it_list1 != list1.end(), it_list2 != list2.end(), it_list3 != list3.end(), it_list4 != list4.end(); )
  516. {
  517. list1.erase(it_list1++);
  518. list2.erase(it_list2++);
  519. list3.erase(it_list3++);
  520. list4.erase(it_list4++);
  521. }
  522. return nRet;
  523. }
  524. //删除原有的时间计划
  525. INT CDBInterface::DeleteDatePlan( int nPlanType, CHAR *pPlanID )
  526. {
  527. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  528. sprintf(strSQLText, "delete from t_plan_info where planid = \'%s\' and plantype=%d",
  529. pPlanID, nPlanType);
  530. return CDBConnection::GetInstancePtr()->Execute(strSQLText);
  531. }
  532. //删除原有的时间计划
  533. INT CDBInterface::DeleteDatePlan( int nPlanType, CString strUid, int nVarID )
  534. {
  535. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  536. sprintf(strSQLText, "delete from t_plan_info where uid = \'%s\' and varid = %d and plantype=%d",
  537. strUid, nVarID, nPlanType);
  538. return CDBConnection::GetInstancePtr()->Execute(strSQLText);
  539. }
  540. //插入时间计划
  541. INT CDBInterface::InsertDatePlan( int nPlanType, CHAR *pPlanID, int nStartHours, int nEndHours, int nStartWeek, int nEndWeek )
  542. {
  543. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  544. sprintf(strSQLText, "insert into t_plan_info(plantype, planid, starttime, endtime, " \
  545. "startweekofday, endweekofday) values (%d, \'%s\', %d, %d, %d, %d) ",
  546. nPlanType, pPlanID, nStartHours, nEndHours, nStartWeek, nEndWeek);
  547. return CDBConnection::GetInstancePtr()->Execute(strSQLText);
  548. }
  549. //插入时间计划
  550. INT CDBInterface::InsertDatePlan( int nPlanType, CString strUid, int nVarID, int nStartHours, int nEndHours, int nStartWeek, int nEndWeek )
  551. {
  552. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  553. sprintf(strSQLText, "insert into t_plan_info(plantype, uid, varid, starttime, endtime, " \
  554. "startweekofday, endweekofday) values (%d, \'%s\', %d, %d, %d, %d, %d) ",
  555. nPlanType, strUid, nVarID, nStartHours, nEndHours, nStartWeek, nEndWeek);
  556. return CDBConnection::GetInstancePtr()->Execute(strSQLText);
  557. }
  558. //获取系统支持所有的驱动类型
  559. INT CDBInterface::GetDevDriveList( list<string>& list1, list<string>& list2 )
  560. {
  561. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  562. sprintf(strSQLText, "select id, drivename from t_dev_drive");
  563. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList2(strSQLText, list1, list2);
  564. return nRet;
  565. }
  566. //获取指定驱动的厂家资料
  567. INT CDBInterface::GetDevFactoryList( int nDriverID, list<string>& list1, list<string>& list2 )
  568. {
  569. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  570. sprintf(strSQLText, "select factoryid, factoryname from t_dev_factory where deviceid = %d", nDriverID);
  571. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList2(strSQLText, list1, list2);
  572. return nRet;
  573. }
  574. //获取指定厂家的设备型号列表
  575. INT CDBInterface::GetDevTypeList( int nDriverID, int nFactoryID, list<string>& list1, list<string>& list2 )
  576. {
  577. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  578. sprintf(strSQLText, "select id, equiptype from t_dev_type where driveid = %d and factoryid = %d",
  579. nDriverID, nFactoryID);
  580. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList2(strSQLText, list1, list2);
  581. return nRet;
  582. }
  583. //获取变量状态列表
  584. INT CDBInterface::GetVarStatusList( CHAR *pVarUID, int nVarID, list<string>& list1, list<string>& list2 )
  585. {
  586. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  587. sprintf(strSQLText, "select statusid, statusdesc from t_var_status where devuid = \'%s\' " \
  588. "and varid = %d ", pVarUID, nVarID);
  589. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList2(strSQLText, list1, list2);
  590. return nRet;
  591. }
  592. //获取变量bit位的ID
  593. INT CDBInterface::GetVarBitID( CHAR *pVarUID, int nStartBit, int nEndBit, CHAR *pVarBitID )
  594. {
  595. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  596. list<string> list1;
  597. list<string>::iterator it;
  598. if (!_stricmp(m_chDBType, "SQL SERVER"))
  599. {
  600. sprintf(strSQLText, "select top 1 id from t_dev_variant_item where uid = \'%s\' " \
  601. "and startbit = %d and endbit = %d", pVarUID, nStartBit, nEndBit);
  602. }
  603. else if (!_stricmp(m_chDBType, "ACCESS97"))
  604. {
  605. sprintf(strSQLText, "select top 1 id from t_dev_variant_item where uid = \'%s\' " \
  606. "and startbit = %d and endbit = %d", pVarUID, nStartBit, nEndBit);
  607. }
  608. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  609. {
  610. sprintf(strSQLText, "select top 1 id from t_dev_variant_item where uid = \'%s\' " \
  611. "and startbit = %d and endbit = %d", pVarUID, nStartBit, nEndBit);
  612. }
  613. else if(!_stricmp(m_chDBType, "PGSQL"))
  614. {
  615. sprintf(strSQLText, "select id from t_dev_variant_item where uid = \'%s\' " \
  616. "and startbit = %d and endbit = %d limit 1", pVarUID, nStartBit, nEndBit);
  617. }
  618. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  619. if( 0 == list1.size() )
  620. {
  621. return 0;
  622. }
  623. if( nRet != -1 )
  624. {
  625. strcpy(pVarBitID, list1.begin()->c_str());
  626. }
  627. for( it = list1.begin(); it != list1.end(); )
  628. {
  629. list1.erase(it++);
  630. }
  631. return nRet;
  632. }
  633. //获取变量类型
  634. INT CDBInterface::GetVarTypeName( int nVarTypeID, CHAR *pVarTypeName )
  635. {
  636. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  637. list<string> list1;
  638. list<string>::iterator it;
  639. if (!_stricmp(m_chDBType, "SQL SERVER"))
  640. {
  641. sprintf(strSQLText, "select top 1 vartype from t_var_type where id = %d", nVarTypeID);
  642. }
  643. else if (!_stricmp(m_chDBType, "ACCESS97"))
  644. {
  645. sprintf(strSQLText, "select top 1 vartype from t_var_type where id = %d", nVarTypeID);
  646. }
  647. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  648. {
  649. sprintf(strSQLText, "select top 1 vartype from t_var_type where id = %d", nVarTypeID);
  650. }
  651. else if(!_stricmp(m_chDBType, "PGSQL"))
  652. {
  653. sprintf(strSQLText, "select vartype from t_var_type where id = %d LIMIT 1", nVarTypeID);
  654. }
  655. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  656. if( 0 == list1.size() )
  657. {
  658. return 0;
  659. }
  660. if( nRet != -1 )
  661. {
  662. strcpy(pVarTypeName, list1.begin()->c_str());
  663. }
  664. for( it = list1.begin(); it != list1.end(); )
  665. {
  666. list1.erase(it++);
  667. }
  668. return nRet;
  669. }
  670. //获取功能码
  671. INT CDBInterface::GetFuncCodeName( int nFuncID, CHAR *pFuncName )
  672. {
  673. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  674. list<string> list1;
  675. list<string>::iterator it;
  676. if (!_stricmp(m_chDBType, "SQL SERVER"))
  677. {
  678. sprintf(strSQLText, "select top 1 funcname from t_dev_func_code where id = %d", nFuncID);
  679. }
  680. else if (!_stricmp(m_chDBType, "ACCESS97"))
  681. {
  682. sprintf(strSQLText, "select top 1 funcname from t_dev_func_code where id = %d", nFuncID);
  683. }
  684. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  685. {
  686. sprintf(strSQLText, "select top 1 funcname from t_dev_func_code where id = %d", nFuncID);
  687. }
  688. else if(!_stricmp(m_chDBType, "PGSQL"))
  689. {
  690. sprintf(strSQLText, "select funcname from t_dev_func_code where id = %d LIMIT 1", nFuncID);
  691. }
  692. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  693. if( 0 == list1.size() )
  694. {
  695. return 0;
  696. }
  697. if( nRet != -1 )
  698. {
  699. strcpy(pFuncName, list1.begin()->c_str());
  700. }
  701. for( it = list1.begin(); it != list1.end(); )
  702. {
  703. list1.erase(it++);
  704. }
  705. return nRet;
  706. }
  707. //获取变量ID
  708. INT CDBInterface::GetVarID( CHAR *pVarName, CHAR *pVarID )
  709. {
  710. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  711. list<string> list1;
  712. list<string>::iterator it;
  713. if (!_stricmp(m_chDBType, "SQL SERVER"))
  714. {
  715. sprintf(strSQLText, "select top 1 id from t_dev_variant where varname = \'%s\'", pVarName);
  716. }
  717. else if (!_stricmp(m_chDBType, "ACCESS97"))
  718. {
  719. sprintf(strSQLText, "select top 1 id from t_dev_variant where varname = \'%s\'", pVarName);
  720. }
  721. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  722. {
  723. sprintf(strSQLText, "select top 1 id from t_dev_variant where varname = \'%s\'", pVarName);
  724. }
  725. else if(!_stricmp(m_chDBType, "PGSQL"))
  726. {
  727. sprintf(strSQLText, "select id from t_dev_variant where varname = \'%s\' limit 1", pVarName);
  728. }
  729. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  730. if( 0 == list1.size() )
  731. {
  732. return 0;
  733. }
  734. if( nRet != -1 )
  735. {
  736. strcpy(pVarID, list1.begin()->c_str());
  737. }
  738. for( it = list1.begin(); it != list1.end(); )
  739. {
  740. list1.erase(it++);
  741. }
  742. return nRet;
  743. }
  744. //获取变量ID
  745. INT CDBInterface::GetVarID( int nStartAddr, int nFuncCode, int nRegNum, CHAR *pVarID )
  746. {
  747. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  748. list<string> list1;
  749. list<string>::iterator it;
  750. if (!_stricmp(m_chDBType, "SQL SERVER"))
  751. {
  752. sprintf(strSQLText, "select top 1 id from t_dev_variant where address = %d and funcid = %d and registernum limit 1",
  753. nStartAddr, nFuncCode, nRegNum);
  754. }
  755. else if (!_stricmp(m_chDBType, "ACCESS97"))
  756. {
  757. sprintf(strSQLText, "select top 1 id from t_dev_variant where address = %d and funcid = %d and registernum limit 1",
  758. nStartAddr, nFuncCode, nRegNum);
  759. }
  760. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  761. {
  762. sprintf(strSQLText, "select top 1 id from t_dev_variant where address = %d and funcid = %d and registernum limit 1",
  763. nStartAddr, nFuncCode, nRegNum);
  764. }
  765. else if(!_stricmp(m_chDBType, "PGSQL"))
  766. {
  767. sprintf(strSQLText, "select id from t_dev_variant where address = %d and funcid = %d and registernum limit 1",
  768. nStartAddr, nFuncCode, nRegNum);
  769. }
  770. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  771. if( 0 == list1.size() )
  772. {
  773. return 0;
  774. }
  775. if( nRet != -1 )
  776. {
  777. strcpy(pVarID, list1.begin()->c_str());
  778. }
  779. for( it = list1.begin(); it != list1.end(); )
  780. {
  781. list1.erase(it++);
  782. }
  783. return nRet;
  784. }
  785. //获取变量类型ID
  786. INT CDBInterface::GetVarTypeID( CHAR *pVarTypeName, CHAR *pVarTypeID )
  787. {
  788. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  789. list<string> list1;
  790. list<string>::iterator it;
  791. if (!_stricmp(m_chDBType, "SQL SERVER"))
  792. {
  793. sprintf(strSQLText, "select top 1 id from t_var_type where vartype = \'%s\'", pVarTypeName);
  794. }
  795. else if (!_stricmp(m_chDBType, "ACCESS97"))
  796. {
  797. sprintf(strSQLText, "select top 1 id from t_var_type where vartype = \'%s\'", pVarTypeName);
  798. }
  799. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  800. {
  801. sprintf(strSQLText, "select top 1 id from t_var_type where vartype = \'%s\'", pVarTypeName);
  802. }
  803. else if(!_stricmp(m_chDBType, "PGSQL"))
  804. {
  805. sprintf(strSQLText, "select id from t_var_type where vartype = \'%s\' LIMIT 1", pVarTypeName);
  806. }
  807. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  808. if( 0 == list1.size() )
  809. {
  810. return 0;
  811. }
  812. if( nRet != -1 )
  813. {
  814. strcpy(pVarTypeID, list1.begin()->c_str());
  815. }
  816. for( it = list1.begin(); it != list1.end(); )
  817. {
  818. list1.erase(it++);
  819. }
  820. return nRet;
  821. }
  822. //获取功能码ID
  823. INT CDBInterface::GetFuncCodeID( CHAR *pFuncName, CHAR *pFuncID )
  824. {
  825. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  826. list<string> list1;
  827. list<string>::iterator it;
  828. if (!_stricmp(m_chDBType, "SQL SERVER"))
  829. {
  830. sprintf(strSQLText, "select top 1 id from t_dev_func_code where funcname = \'%s\'", pFuncName);
  831. }
  832. else if (!_stricmp(m_chDBType, "ACCESS97"))
  833. {
  834. sprintf(strSQLText, "select top 1 id from t_dev_func_code where funcname = \'%s\'", pFuncName);
  835. }
  836. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  837. {
  838. sprintf(strSQLText, "select top 1 id from t_dev_func_code where funcname = \'%s\'", pFuncName);
  839. }
  840. else if(!_stricmp(m_chDBType, "PGSQL"))
  841. {
  842. sprintf(strSQLText, "select id from t_dev_func_code where funcname = \'%s\' LIMIT 1", pFuncName);
  843. }
  844. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  845. if( 0 == list1.size() )
  846. {
  847. return 0;
  848. }
  849. if( nRet != -1 )
  850. {
  851. strcpy(pFuncID, list1.begin()->c_str());
  852. }
  853. for( it = list1.begin(); it != list1.end(); )
  854. {
  855. list1.erase(it++);
  856. }
  857. return nRet;
  858. }
  859. //获取设备名称
  860. INT CDBInterface::GetDevInfo( CHAR *pDevUID, CHAR *pDllName, CHAR *pIniName, CHAR *pDevProtocolType )
  861. {
  862. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  863. list<string> list1, list2, list3;
  864. list<string>::iterator it1, it2, it3;
  865. if (!_stricmp(m_chDBType, "SQL SERVER"))
  866. {
  867. sprintf(strSQLText, "select top 1 protocoldllname, specialdevice, ininame from t_dev_property where uid = \'%s\'", pDevUID);
  868. }
  869. else if (!_stricmp(m_chDBType, "ACCESS97"))
  870. {
  871. sprintf(strSQLText, "select top 1 protocoldllname, specialdevice, ininame from t_dev_property where uid = \'%s\'", pDevUID);
  872. }
  873. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  874. {
  875. sprintf(strSQLText, "select top 1 protocoldllname, specialdevice, ininame from t_dev_property where uid = \'%s\'", pDevUID);
  876. }
  877. else if(!_stricmp(m_chDBType, "PGSQL"))
  878. {
  879. sprintf(strSQLText, "select protocoldllname, specialdevice, ininame from t_dev_property where uid = \'%s\' LIMIT 1", pDevUID);
  880. }
  881. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList3(strSQLText, list1, list2, list3);
  882. if( 0 == list1.size() || 0 == list2.size() || 0 == list3.size() )
  883. {
  884. return 0;
  885. }
  886. if( nRet != -1 )
  887. {
  888. strcpy(pDllName, list1.begin()->c_str());
  889. strcpy(pDevProtocolType, list2.begin()->c_str());
  890. strcpy(pIniName, list3.begin()->c_str());
  891. }
  892. for( it1 = list1.begin(), it2 = list2.begin(), it3 = list3.begin();
  893. it1 != list1.end(), it2 != list2.end(), it3 != list3.end(); )
  894. {
  895. list1.erase(it1++);
  896. list2.erase(it2++);
  897. list3.erase(it3++);
  898. }
  899. return nRet;
  900. }
  901. //修改密码
  902. INT CDBInterface::ModifyPwd(CHAR *pUserID, CHAR *pUserPwd)
  903. {
  904. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  905. sprintf(strSQLText, "UPDATE t_user_info SET pwd=\'%s\' where uid=\'%s\'",
  906. pUserPwd, pUserID);
  907. return CDBConnection::GetInstancePtr()->Execute(strSQLText);
  908. }
  909. //验证用户名、密码是否合法
  910. INT CDBInterface::ValidateUser(CHAR *pUserID, CHAR *pUserPwd)
  911. {
  912. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  913. list<string> list1;
  914. list<string>::iterator it;
  915. if (!_stricmp(m_chDBType, "SQL SERVER"))
  916. {
  917. sprintf(strSQLText, "select top 1 1 from t_user_info where uid = \'%s\' and pwd = \'%s\'", pUserID, pUserPwd);
  918. }
  919. else if (!_stricmp(m_chDBType, "ACCESS97"))
  920. {
  921. sprintf(strSQLText, "select top 1 1 from t_user_info where uid = \'%s\' and pwd = \'%s\'", pUserID, pUserPwd);
  922. }
  923. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  924. {
  925. sprintf(strSQLText, "select top 1 1 from t_user_info where uid = \'%s\' and pwd = \'%s\'", pUserID, pUserPwd);
  926. }
  927. else if(!_stricmp(m_chDBType, "PGSQL"))
  928. {
  929. sprintf(strSQLText, "select id from t_user_info where uid = \'%s\' and pwd = \'%s\' LIMIT 1", pUserID, pUserPwd);
  930. }
  931. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  932. //LOG4C((LOG_NOTICE, "ValidateUser GetFieldValueList1 return %d", nRet));
  933. if( 0 == list1.size() )
  934. {
  935. return 0;
  936. }
  937. for( it = list1.begin(); it != list1.end(); )
  938. {
  939. list1.erase(it++);
  940. }
  941. return nRet;
  942. }
  943. //验证用户是否启用
  944. INT CDBInterface::ValidateUserEnable(CHAR *pUserID, CHAR *pStatus)
  945. {
  946. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  947. list<string> list1;
  948. list<string>::iterator it;
  949. if (!_stricmp(m_chDBType, "SQL SERVER"))
  950. {
  951. sprintf(strSQLText, "select top 1 status from t_user_info where uid = \'%s\'", pUserID);
  952. }
  953. else if (!_stricmp(m_chDBType, "ACCESS97"))
  954. {
  955. sprintf(strSQLText, "select top 1 status from t_user_info where uid = \'%s\'", pUserID);
  956. }
  957. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  958. {
  959. sprintf(strSQLText, "select top 1 status from t_user_info where uid = \'%s\'", pUserID);
  960. }
  961. else if(!_stricmp(m_chDBType, "PGSQL"))
  962. {
  963. sprintf(strSQLText, "select status from t_user_info where uid = \'%s\' limit 1", pUserID);
  964. }
  965. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  966. if( 0 == list1.size() )
  967. {
  968. return 0;
  969. }
  970. if( nRet != -1 )
  971. {
  972. strcpy(pStatus, list1.begin()->c_str());
  973. }
  974. for( it = list1.begin(); it != list1.end(); )
  975. {
  976. list1.erase(it++);
  977. }
  978. return nRet;
  979. }
  980. //得到用户信息ID
  981. INT CDBInterface::GetUserID( CHAR *pID, CHAR *pUserID )
  982. {
  983. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  984. list<string> list1;
  985. list<string>::iterator it;
  986. if (!_stricmp(m_chDBType, "SQL SERVER"))
  987. {
  988. sprintf(strSQLText, "select top 1 id from t_user_info where uid = \'%s\'", pUserID);
  989. }
  990. else if (!_stricmp(m_chDBType, "ACCESS97"))
  991. {
  992. sprintf(strSQLText, "select top 1 id from t_user_info where uid = \'%s\'", pUserID);
  993. }
  994. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  995. {
  996. sprintf(strSQLText, "select top 1 id from t_user_info where uid = \'%s\'", pUserID);
  997. }
  998. else if(!_stricmp(m_chDBType, "PGSQL"))
  999. {
  1000. sprintf(strSQLText, "select id from t_user_info where uid = \'%s\' limit 1", pUserID);
  1001. }
  1002. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  1003. if( 0 == list1.size() )
  1004. {
  1005. return 0;
  1006. }
  1007. if( nRet != -1 )
  1008. {
  1009. strcpy(pUserID, list1.begin()->c_str());
  1010. }
  1011. for( it = list1.begin(); it != list1.end(); )
  1012. {
  1013. list1.erase(it++);
  1014. }
  1015. return nRet;
  1016. }
  1017. // 判断用户是否管理设备权限
  1018. INT CDBInterface::HasDevAlloc( CHAR *pUserID, CHAR *pUid )
  1019. {
  1020. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1021. list<string> list1;
  1022. list<string>::iterator it;
  1023. if (!_stricmp(m_chDBType, "SQL SERVER"))
  1024. {
  1025. sprintf(strSQLText, "select top 1 uid from t_role_equip where userid = \'%s\' " \
  1026. " and uid = \'%s\' and userid <> \'admin\'", pUserID, pUid);
  1027. }
  1028. else if (!_stricmp(m_chDBType, "ACCESS97"))
  1029. {
  1030. sprintf(strSQLText, "select top 1 uid from t_role_equip where userid = \'%s\' " \
  1031. " and uid = \'%s\' and userid <> \'admin\'", pUserID, pUid);
  1032. }
  1033. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  1034. {
  1035. sprintf(strSQLText, "select top 1 uid from t_role_equip where userid = \'%s\' " \
  1036. " and uid = \'%s\' and userid <> \'admin\'", pUserID, pUid);
  1037. }
  1038. else if(!_stricmp(m_chDBType, "PGSQL"))
  1039. {
  1040. sprintf(strSQLText, "select uid from t_role_equip where userid = \'%s\' " \
  1041. " and uid = \'%s\' and userid <> \'admin\' limit 1", pUserID, pUid);
  1042. }
  1043. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  1044. if( 0 == list1.size() )
  1045. {
  1046. return 0;
  1047. }
  1048. for( it = list1.begin(); it != list1.end(); )
  1049. {
  1050. list1.erase(it++);
  1051. }
  1052. return nRet;
  1053. }
  1054. //删除设备权限信息
  1055. INT CDBInterface::DelUserDevInfo(CHAR *pUserID)
  1056. {
  1057. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1058. sprintf(strSQLText, "delete from t_role_equip where userid = \'%s\'", pUserID);
  1059. return CDBConnection::GetInstancePtr()->Execute(strSQLText);
  1060. }
  1061. //添加设备权限信息
  1062. INT CDBInterface::AddUserDevInfo(int nID, CHAR *pUserID, CHAR *pUid)
  1063. {
  1064. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1065. sprintf(strSQLText, "insert into t_role_equip(id, userid, uid) values (%d, " \
  1066. "\'%s\', \'%s\')", nID, pUserID, pUid);
  1067. return CDBConnection::GetInstancePtr()->Execute(strSQLText);
  1068. }
  1069. //获取变量状态表ID
  1070. INT CDBInterface::GetVarStatusID( CHAR *pVarUID, int nVarID, int nStatusID, CHAR *pStatusDesc, CHAR *pID )
  1071. {
  1072. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1073. list<string> list1;
  1074. list<string>::iterator it;
  1075. if (!_stricmp(m_chDBType, "SQL SERVER"))
  1076. {
  1077. sprintf(strSQLText, "select top 1 id from t_var_status where devuid = \'%s\' " \
  1078. "and varid = %d and statusid = %d and statusdesc = \'%s\'",
  1079. pVarUID, nVarID, nStatusID, pStatusDesc);
  1080. }
  1081. else if (!_stricmp(m_chDBType, "ACCESS97"))
  1082. {
  1083. sprintf(strSQLText, "select top 1 id from t_var_status where devuid = \'%s\' " \
  1084. "and varid = %d and statusid = %d and statusdesc = \'%s\'",
  1085. pVarUID, nVarID, nStatusID, pStatusDesc);
  1086. }
  1087. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  1088. {
  1089. sprintf(strSQLText, "select top 1 id from t_var_status where devuid = \'%s\' " \
  1090. "and varid = %d and statusid = %d and statusdesc = \'%s\'",
  1091. pVarUID, nVarID, nStatusID, pStatusDesc);
  1092. }
  1093. else if(!_stricmp(m_chDBType, "PGSQL"))
  1094. {
  1095. sprintf(strSQLText, "select id from t_var_status where devuid = \'%s\' " \
  1096. "and varid = %d and statusid = %d and statusdesc = \'%s\' limit 1",
  1097. pVarUID, nVarID, nStatusID, pStatusDesc);
  1098. }
  1099. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  1100. if( 0 == list1.size() )
  1101. {
  1102. return 0;
  1103. }
  1104. if( nRet != -1 )
  1105. {
  1106. strcpy(pID, list1.begin()->c_str());
  1107. }
  1108. for( it = list1.begin(); it != list1.end(); )
  1109. {
  1110. list1.erase(it++);
  1111. }
  1112. return nRet;
  1113. }
  1114. INT CDBInterface::AddGroupToDll()
  1115. {
  1116. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1117. list<string> list1, list2, list3;
  1118. list<string>::iterator it_list1, it_list2, it_list3;
  1119. int nGroupID = 0;
  1120. CString strName,str,strDesc,strPermitName;
  1121. int nPermitID=0;
  1122. if(!_stricmp(g_strDBType, "PGSQL"))
  1123. {
  1124. sprintf(strSQLText, "select id, groupname, groupdesc from t_user_group");
  1125. }
  1126. else
  1127. {
  1128. sprintf(strSQLText, "select id, groupname, groupdesc from t_user_group");
  1129. }
  1130. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList3(strSQLText, list1, list2, list3);
  1131. if( 0 == list1.size() || 0 == list2.size() || 0 == list3.size() )
  1132. {
  1133. return 0;
  1134. }
  1135. if( nRet != -1 )
  1136. {
  1137. for( it_list1 = list1.begin(), it_list2 = list2.begin(), it_list3 = list3.begin();
  1138. it_list1 != list1.end() && it_list2 != list2.end() && it_list3 != list3.end();
  1139. it_list1++, it_list2++, it_list3++)
  1140. {
  1141. nGroupID = atoi((*it_list1).c_str());
  1142. strName = (*it_list2).c_str();
  1143. strDesc = (*it_list3).c_str();
  1144. PMS_AddGroup(strName, nGroupID);
  1145. int nS=0,nE=0;
  1146. for( int i=0;i<strDesc.GetLength();i++ )
  1147. {
  1148. if( strDesc[i]=='-' )
  1149. {
  1150. nE = i;
  1151. str = strDesc.Mid( nS,nE-nS );
  1152. nS = nE+1;
  1153. nPermitID = atoi(str);
  1154. GetPermitNameByID( strPermitName,nPermitID );
  1155. PMS_AddPermitToGroup( strPermitName,nPermitID,nGroupID );
  1156. }
  1157. }
  1158. str = strDesc.Mid( nS,strDesc.GetLength()-nS );
  1159. nPermitID = atoi(str);
  1160. GetPermitNameByID( strPermitName,nPermitID );
  1161. PMS_AddPermitToGroup( strPermitName,nPermitID,nGroupID );
  1162. }
  1163. }
  1164. for( it_list1 = list1.begin(), it_list2 = list2.begin(), it_list3 = list3.begin();
  1165. it_list1 != list1.end(), it_list2 != list2.end(), it_list3 != list3.end(); )
  1166. {
  1167. list1.erase(it_list1++);
  1168. list2.erase(it_list2++);
  1169. list3.erase(it_list3++);
  1170. }
  1171. return nRet;
  1172. }
  1173. INT CDBInterface::GetAllGroupToTree( CTreeCtrl *pTreeCtrl,HTREEITEM hRoot )
  1174. {
  1175. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1176. list<string> list1;
  1177. list<string>::iterator it_list1;
  1178. CString strName;
  1179. if(!_stricmp(g_strDBType, "PGSQL"))
  1180. {
  1181. sprintf(strSQLText, "select groupname from t_user_group");
  1182. }
  1183. else
  1184. {
  1185. sprintf(strSQLText, "select groupname from t_user_group");
  1186. }
  1187. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  1188. if( 0 == list1.size() )
  1189. {
  1190. return 0;
  1191. }
  1192. if( nRet != -1 )
  1193. {
  1194. for( it_list1 = list1.begin(); it_list1 != list1.end(); it_list1++ )
  1195. {
  1196. strName = (*it_list1).c_str();
  1197. strName = strName.Trim();
  1198. if( strName.Compare("管理员" ) )
  1199. pTreeCtrl->InsertItem(strName,5,5,hRoot);
  1200. }
  1201. }
  1202. for( it_list1 = list1.begin(); it_list1 != list1.end(); )
  1203. {
  1204. list1.erase(it_list1++);
  1205. }
  1206. return nRet;
  1207. }
  1208. INT CDBInterface::AddGroup(int iID, CString sGroupName, CString sGroupDesc)
  1209. {
  1210. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1211. sprintf(strSQLText, "insert into t_user_group(id, groupname,groupdesc) values (" \
  1212. "%d, \'%s\', \'%s\')",
  1213. iID, sGroupName,sGroupDesc);
  1214. return CDBConnection::GetInstancePtr()->Execute(strSQLText);
  1215. }
  1216. INT CDBInterface::ModifyGroup(CString sGroupName, CString sGroupDesc)
  1217. {
  1218. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1219. sprintf(strSQLText, "UPDATE t_user_group SET groupdesc=\'%s\' where groupname=\'%s\' ",
  1220. sGroupDesc, sGroupName);
  1221. return CDBConnection::GetInstancePtr()->Execute(strSQLText);
  1222. }
  1223. INT CDBInterface::DelGroup(int iGroupID)
  1224. {
  1225. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1226. sprintf(strSQLText, "delete from t_user_group where id=%d",iGroupID);
  1227. return CDBConnection::GetInstancePtr()->Execute(strSQLText);
  1228. }
  1229. //添加用户所属组信息
  1230. INT CDBInterface::AddUserToGroup(int iID, CHAR * pUserID, int iGroupID, CHAR *pUserName)
  1231. {
  1232. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1233. sprintf(strSQLText, "insert into t_role_user(id, userid, group_id,roledesc) values (%d, " \
  1234. " \'%s\', %d, \'%s\')", iID, pUserID, iGroupID,pUserName);
  1235. return CDBConnection::GetInstancePtr()->Execute(strSQLText);
  1236. }
  1237. //刪除用戶所屬組
  1238. INT CDBInterface::DeleteUserGroup(CHAR * pUserID, int iGroupID )
  1239. {
  1240. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1241. sprintf(strSQLText, "delete from t_role_user where userid=\'%s\' ", pUserID);
  1242. return CDBConnection::GetInstancePtr()->Execute(strSQLText);
  1243. }
  1244. // 插入日志记录
  1245. INT CDBInterface::InsertLogRecord( int LogType, int ModuleType, CHAR *pHappenTime, CHAR *pContent )
  1246. {
  1247. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1248. int nID = 0;
  1249. CHAR szMaxID[MAX_ID] = {0};
  1250. GetMaxID("t_sys_log", "id", szMaxID);
  1251. nID = atoi( szMaxID )+1;
  1252. CString sTime,sContent;
  1253. SYSTEMTIME systime;
  1254. GetLocalTime( &systime );
  1255. sTime.Format( "%d-%02d-%02d %02d:%02d:%02d",systime.wYear,systime.wMonth,systime.wDay,systime.wHour,systime.wMinute,systime.wSecond );
  1256. sprintf(strSQLText, "insert into t_sys_log(LogType, ModuleType, HappenTime, Contents, id) values (" \
  1257. "%d, %d, \'%s\', \'%s\',%d)",
  1258. LogType, ModuleType, sTime, pContent,nID );
  1259. return CDBConnection::GetInstancePtr()->Execute(strSQLText);
  1260. }
  1261. //得到組的ID通過組名稱
  1262. INT CDBInterface::GetGroupIDByName( CString sGroupName,int &iGroupID )
  1263. {
  1264. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1265. list<string> list1;
  1266. list<string>::iterator it;
  1267. if (!_stricmp(m_chDBType, "SQL SERVER"))
  1268. {
  1269. sprintf(strSQLText, "select top 1 id from t_user_group where groupname = \'%s\'", sGroupName);
  1270. }
  1271. else if (!_stricmp(m_chDBType, "ACCESS97"))
  1272. {
  1273. sprintf(strSQLText, "select top 1 id from t_user_group where groupname = \'%s\'", sGroupName);
  1274. }
  1275. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  1276. {
  1277. sprintf(strSQLText, "select top 1 id from t_user_group where groupname = \'%s\'", sGroupName);
  1278. }
  1279. else if(!_stricmp(m_chDBType, "PGSQL"))
  1280. {
  1281. sprintf(strSQLText, "select id from t_user_group where groupname = \'%s\' limit 1", sGroupName);
  1282. }
  1283. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  1284. if( 0 == list1.size() )
  1285. {
  1286. return 0;
  1287. }
  1288. if( nRet != -1 )
  1289. {
  1290. CString str;
  1291. str = list1.begin()->c_str();
  1292. iGroupID = atoi(str);
  1293. }
  1294. for( it = list1.begin(); it != list1.end(); )
  1295. {
  1296. list1.erase(it++);
  1297. }
  1298. return nRet;
  1299. }
  1300. // 根據權限名得到ID
  1301. INT CDBInterface::GetPermitIDByName( CString sPermitName,int &iPermitID )
  1302. {
  1303. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1304. list<string> list1;
  1305. list<string>::iterator it;
  1306. if (!_stricmp(m_chDBType, "SQL SERVER"))
  1307. {
  1308. sprintf(strSQLText, "select top 1 id from t_user_purview where purview_name = \'%s\'", sPermitName);
  1309. }
  1310. else if (!_stricmp(m_chDBType, "ACCESS97"))
  1311. {
  1312. sprintf(strSQLText, "select top 1 id from t_user_purview where purview_name = \'%s\'", sPermitName);
  1313. }
  1314. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  1315. {
  1316. sprintf(strSQLText, "select top 1 id from t_user_purview where purview_name = \'%s\'", sPermitName);
  1317. }
  1318. else if(!_stricmp(m_chDBType, "PGSQL"))
  1319. {
  1320. sprintf(strSQLText, "select id from t_user_purview where purview_name = \'%s\'", sPermitName);/* limit 1*/
  1321. }
  1322. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  1323. if( 0 == list1.size() )
  1324. {
  1325. return 0;
  1326. }
  1327. if( nRet != -1 )
  1328. {
  1329. CString str;
  1330. str = list1.begin()->c_str();
  1331. iPermitID = atoi(str);
  1332. }
  1333. for( it = list1.begin(); it != list1.end(); )
  1334. {
  1335. list1.erase(it++);
  1336. }
  1337. return nRet;
  1338. }
  1339. // 根據組的ID得到該組的所有權限
  1340. INT CDBInterface::GetGroupPermitByID( CListBox *pListBox,int iGroupID )
  1341. {
  1342. int nCount = pListBox->GetCount();
  1343. for( int i = nCount-1; i >=0; i-- )
  1344. {
  1345. pListBox->DeleteString( i );
  1346. }
  1347. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1348. list<string> list1;
  1349. list<string>::iterator it_list1;
  1350. int nPermitID=0;
  1351. CString strPermitName, strDesc, str;
  1352. if(!_stricmp(g_strDBType, "PGSQL"))
  1353. {
  1354. sprintf(strSQLText, "select groupdesc from t_user_group where id = %d /*limit 1*/", iGroupID);
  1355. }
  1356. else
  1357. {
  1358. sprintf(strSQLText, "select top 1 groupdesc from t_user_group where id = %d", iGroupID);
  1359. }
  1360. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  1361. if( 0 == list1.size() )
  1362. {
  1363. return 0;
  1364. }
  1365. if( nRet != -1 )
  1366. {
  1367. for( it_list1 = list1.begin(); it_list1 != list1.end(); it_list1++ )
  1368. {
  1369. strDesc = (*it_list1).c_str();
  1370. strDesc = strDesc.Trim();
  1371. int nS=0,nE=0;
  1372. for( int i=0;i<strDesc.GetLength();i++ )
  1373. {
  1374. if( strDesc[i]=='-' )
  1375. {
  1376. nE = i;
  1377. str = strDesc.Mid( nS,nE-nS );
  1378. nS = nE+1;
  1379. nPermitID = atoi(str);
  1380. GetPermitNameByID( strPermitName, nPermitID );
  1381. if( strPermitName!="" && strPermitName[0]!='*' )
  1382. {
  1383. pListBox->AddString( strPermitName );
  1384. }
  1385. }
  1386. }
  1387. str = strDesc.Mid( nS,strDesc.GetLength()-nS );
  1388. if( str!="" )
  1389. {
  1390. nPermitID = atoi(str);
  1391. GetPermitNameByID( strPermitName,nPermitID );
  1392. if( strPermitName[0]!='*' )
  1393. {
  1394. pListBox->AddString( strPermitName );
  1395. }
  1396. }
  1397. }
  1398. }
  1399. for( it_list1 = list1.begin(); it_list1 != list1.end(); )
  1400. {
  1401. list1.erase(it_list1++);
  1402. }
  1403. return 0;
  1404. }
  1405. // 如果2中有則1沒
  1406. INT CDBInterface::GetGroupPermitByID( CListBox *pListBox1,CListBox *pListBox2 )
  1407. {
  1408. int nCount2 = pListBox2->GetCount();
  1409. int nCount1 = pListBox1->GetCount();
  1410. for( int i = nCount1-1; i >=0; i-- )
  1411. {
  1412. pListBox1->DeleteString( i );
  1413. }
  1414. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1415. list<string> list1;
  1416. list<string>::iterator it_list1;
  1417. int nPermitID=0;
  1418. CString strName, str;
  1419. if(!_stricmp(g_strDBType, "PGSQL"))
  1420. {
  1421. sprintf(strSQLText, "select purview_name from t_user_purview");
  1422. }
  1423. else
  1424. {
  1425. sprintf(strSQLText, "select purview_name from t_user_purview");
  1426. }
  1427. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  1428. if( 0 == list1.size() )
  1429. {
  1430. return 0;
  1431. }
  1432. if( nRet != -1 )
  1433. {
  1434. for( it_list1 = list1.begin(); it_list1 != list1.end(); it_list1++ )
  1435. {
  1436. strName = (*it_list1).c_str();
  1437. strName = strName.Trim();
  1438. bool bExist = false;
  1439. for( int i=0;i<nCount2;i++ )
  1440. {
  1441. pListBox2->GetText( i, str );
  1442. if( str==strName )
  1443. {
  1444. bExist = true;
  1445. break;
  1446. }
  1447. }
  1448. if( !bExist )
  1449. {
  1450. if( strName[0]!='*' )
  1451. {
  1452. pListBox1->AddString( strName );
  1453. }
  1454. }
  1455. }
  1456. }
  1457. for( it_list1 = list1.begin(); it_list1 != list1.end(); )
  1458. {
  1459. list1.erase(it_list1++);
  1460. }
  1461. return nRet;
  1462. }
  1463. INT CDBInterface::FindGroupByName(CString sGroupName)
  1464. {
  1465. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1466. list<string> list1;
  1467. list<string>::iterator it;
  1468. if (!_stricmp(m_chDBType, "SQL SERVER"))
  1469. {
  1470. sprintf(strSQLText, "select top 1 id from t_user_group where groupname = \'%s\'", sGroupName);
  1471. }
  1472. else if (!_stricmp(m_chDBType, "ACCESS97"))
  1473. {
  1474. sprintf(strSQLText, "select top 1 id from t_user_group where groupname = \'%s\'", sGroupName);
  1475. }
  1476. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  1477. {
  1478. sprintf(strSQLText, "select top 1 id from t_user_group where groupname = \'%s\'", sGroupName);
  1479. }
  1480. else if(!_stricmp(m_chDBType, "PGSQL"))
  1481. {
  1482. sprintf(strSQLText, "select id from t_user_group where groupname = \'%s\' limit 1", sGroupName);
  1483. }
  1484. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  1485. if( 0 == list1.size() )
  1486. {
  1487. return 0;
  1488. }
  1489. for( it = list1.begin(); it != list1.end(); )
  1490. {
  1491. list1.erase(it++);
  1492. }
  1493. return nRet;
  1494. }
  1495. // 根據用戶名得到ID
  1496. INT CDBInterface::GetUserIDByName( CString sUserName,int &iUserID )
  1497. {
  1498. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1499. list<string> list1;
  1500. list<string>::iterator it;
  1501. if (!_stricmp(m_chDBType, "SQL SERVER"))
  1502. {
  1503. sprintf(strSQLText, "select top 1 id from t_user_info where uid = \'%s\'", sUserName);
  1504. }
  1505. else if (!_stricmp(m_chDBType, "ACCESS97"))
  1506. {
  1507. sprintf(strSQLText, "select top 1 id from t_user_info where uid = \'%s\'", sUserName);
  1508. }
  1509. else if (!_stricmp(m_chDBType, "ACCESS2000"))
  1510. {
  1511. sprintf(strSQLText, "select top 1 id from t_user_info where uid = \'%s\'", sUserName);
  1512. }
  1513. else if(!_stricmp(m_chDBType, "PGSQL"))
  1514. {
  1515. sprintf(strSQLText, "select id from t_user_info where uid = \'%s\' limit 1", sUserName);
  1516. }
  1517. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  1518. if( 0 == list1.size() )
  1519. {
  1520. return 0;
  1521. }
  1522. if( nRet != -1 )
  1523. {
  1524. CString str;
  1525. str = list1.begin()->c_str();
  1526. iUserID = atoi(str);
  1527. }
  1528. for( it = list1.begin(); it != list1.end(); )
  1529. {
  1530. list1.erase(it++);
  1531. }
  1532. return nRet;
  1533. }
  1534. INT CDBInterface::AddUserToDll()
  1535. {
  1536. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1537. list<string> list1, list2, list3;
  1538. list<string>::iterator it_list1, it_list2, it_list3;
  1539. int nUserID = 0;
  1540. int nGroupID = 0;
  1541. int nPermitID = 0;
  1542. CString strName,str;
  1543. if(!_stricmp(g_strDBType, "PGSQL"))
  1544. {
  1545. sprintf(strSQLText, "select t_user_info.id,t_user_info.uid,t_role_user.group_id from t_user_info,t_role_user where t_user_info.uid=t_role_user.userid");
  1546. }
  1547. else
  1548. {
  1549. sprintf(strSQLText, "select t_user_info.id,t_user_info.uid,t_role_user.group_id from t_user_info,t_role_user where t_user_info.uid=t_role_user.userid");
  1550. }
  1551. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList3(strSQLText, list1, list2, list3);
  1552. if( 0 == list1.size() || 0 == list2.size() || 0 == list3.size() )
  1553. {
  1554. return 0;
  1555. }
  1556. if( nRet != -1 )
  1557. {
  1558. for( it_list1 = list1.begin(), it_list2 = list2.begin(), it_list3 = list3.begin();
  1559. it_list1 != list1.end() && it_list2 != list2.end() && it_list3 != list3.end();
  1560. it_list1++, it_list2++, it_list3++)
  1561. {
  1562. str = (*it_list1).c_str();
  1563. nUserID = atoi( str.Trim() );
  1564. str = (*it_list2).c_str();
  1565. strName = str.Trim();
  1566. str = (*it_list3).c_str();
  1567. nGroupID = atoi( str.Trim() );
  1568. PMS_AddUserToGroup( strName,nUserID,nGroupID );
  1569. }
  1570. }
  1571. for( it_list1 = list1.begin(), it_list2 = list2.begin(), it_list3 = list3.begin();
  1572. it_list1 != list1.end(), it_list2 != list2.end(), it_list3 != list3.end(); )
  1573. {
  1574. list1.erase(it_list1++);
  1575. list2.erase(it_list2++);
  1576. list3.erase(it_list3++);
  1577. }
  1578. return nRet;
  1579. }
  1580. //得到用戶所屬的組
  1581. INT CDBInterface::GetAllGroupByUserID( CListBox *pListBox,CHAR * pUserID )
  1582. {
  1583. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1584. list<string> list1;
  1585. list<string>::iterator it_list1;
  1586. int nPermitID=0;
  1587. CString strName, str;
  1588. if(!_stricmp(g_strDBType, "PGSQL"))
  1589. {
  1590. sprintf(strSQLText, "select t_user_group.groupname from t_role_user,t_user_group "\
  1591. "where t_user_group.id=t_role_user.group_id and t_role_user.userid=\'%s\'",pUserID );
  1592. }
  1593. else
  1594. {
  1595. sprintf(strSQLText, "select t_user_group.groupname from t_role_user,t_user_group "\
  1596. "where t_user_group.id=t_role_user.group_id and t_role_user.userid=\'%s\'",pUserID );
  1597. }
  1598. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  1599. if( 0 == list1.size() )
  1600. {
  1601. return 0;
  1602. }
  1603. if( nRet != -1 )
  1604. {
  1605. for( it_list1 = list1.begin(); it_list1 != list1.end(); it_list1++ )
  1606. {
  1607. strName = (*it_list1).c_str();
  1608. strName = strName.Trim();
  1609. pListBox->AddString( strName );
  1610. }
  1611. }
  1612. for( it_list1 = list1.begin(); it_list1 != list1.end(); )
  1613. {
  1614. list1.erase(it_list1++);
  1615. }
  1616. return nRet;
  1617. }
  1618. INT CDBInterface::GetAllGroupByUserID( CListBox *pListBox1,CListBox *pListBox2 )
  1619. {
  1620. int nCount2 = pListBox2->GetCount();
  1621. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1622. list<string> list1;
  1623. list<string>::iterator it_list1;
  1624. int nPermitID=0;
  1625. CString strName, str;
  1626. if(!_stricmp(g_strDBType, "PGSQL"))
  1627. {
  1628. sprintf(strSQLText, "select groupname from t_user_group");
  1629. }
  1630. else
  1631. {
  1632. sprintf(strSQLText, "select groupname from t_user_group");
  1633. }
  1634. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  1635. if( 0 == list1.size() )
  1636. {
  1637. return 0;
  1638. }
  1639. if( nRet != -1 )
  1640. {
  1641. for( it_list1 = list1.begin(); it_list1 != list1.end(); it_list1++ )
  1642. {
  1643. strName = (*it_list1).c_str();
  1644. strName = strName.Trim();
  1645. bool bExist = false;
  1646. for( int i=0;i<nCount2;i++ )
  1647. {
  1648. pListBox2->GetText( i, str );
  1649. if( str == strName )
  1650. {
  1651. bExist = true;
  1652. break;
  1653. }
  1654. }
  1655. if( !bExist && strName.Compare("管理员") )
  1656. pListBox1->AddString( strName );
  1657. }
  1658. }
  1659. for( it_list1 = list1.begin(); it_list1 != list1.end(); )
  1660. {
  1661. list1.erase(it_list1++);
  1662. }
  1663. return nRet;
  1664. }
  1665. // 根據權限ID得到名
  1666. INT CDBInterface::GetPermitNameByID( CString &sPermitName,int iPermitID )
  1667. {
  1668. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1669. list<string> list1;
  1670. list<string>::iterator it;
  1671. if (!_stricmp(g_strDBType, "SQL SERVER"))
  1672. {
  1673. sprintf(strSQLText, "select top 1 purview_name from t_user_purview where id = %d", iPermitID);
  1674. }
  1675. else if (!_stricmp(g_strDBType, "ACCESS97"))
  1676. {
  1677. sprintf(strSQLText, "select top 1 purview_name from t_user_purview where id = %d", iPermitID);
  1678. }
  1679. else if (!_stricmp(g_strDBType, "ACCESS2000"))
  1680. {
  1681. sprintf(strSQLText, "select top 1 purview_name from t_user_purview where id = %d", iPermitID);
  1682. }
  1683. else if(!_stricmp(g_strDBType, "PGSQL"))
  1684. {
  1685. sprintf(strSQLText, "select purview_name from t_user_purview where id = %d limit 1", iPermitID);
  1686. }
  1687. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList1(strSQLText, list1);
  1688. if( 0 == list1.size() )
  1689. {
  1690. return 0;
  1691. }
  1692. if( nRet != -1 )
  1693. {
  1694. CString str;
  1695. str = list1.begin()->c_str();
  1696. sPermitName = str.Trim();
  1697. }
  1698. for( it = list1.begin(); it != list1.end(); )
  1699. {
  1700. list1.erase(it++);
  1701. }
  1702. return nRet;
  1703. }
  1704. //根据指定内容删除表内容, 通用函数 //0:不操作 1:字符串 2:整型
  1705. INT CDBInterface::DelTableData( CHAR *pTableName, CHAR *pFieldName1,CHAR *pContent1,int iType1,
  1706. CHAR *pFieldName2,CHAR *pContent2,int iType2,
  1707. CHAR *pFieldName3,CHAR *pContent3,int iType3 )
  1708. {
  1709. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1710. CHAR sTemp[MAX_SQL_LENGTH + 1] = "";
  1711. sprintf(strSQLText, "delete from %s where ", pTableName);
  1712. switch( iType1 )
  1713. {
  1714. case 1://字符串
  1715. sprintf( sTemp, "%s = \'%s\' ", pFieldName1,pContent1 );
  1716. strcat( strSQLText, sTemp );
  1717. break;
  1718. case 2://整型
  1719. sprintf(sTemp, "%s = \'%d\' ", pFieldName1,atoi(pContent1));
  1720. strcat( strSQLText, sTemp );
  1721. break;
  1722. }
  1723. switch( iType2 )
  1724. {
  1725. case 1://字符串
  1726. sprintf( sTemp, "and %s = \'%s\' ", pFieldName2,pContent2 );
  1727. strcat( strSQLText, sTemp );
  1728. break;
  1729. case 2://整型
  1730. sprintf(sTemp, "and %s = \'%d\' ", pFieldName2,atoi(pContent2));
  1731. strcat( strSQLText, sTemp );
  1732. break;
  1733. }
  1734. switch( iType3 )
  1735. {
  1736. case 1://字符串
  1737. sprintf( sTemp, "and %s = \'%s\'", pFieldName3,pContent3 );
  1738. strcat( strSQLText, sTemp );
  1739. break;
  1740. case 2://整型
  1741. sprintf(sTemp, "and %s = \'%d\'", pFieldName3,atoi(pContent3));
  1742. strcat( strSQLText, sTemp );
  1743. break;
  1744. }
  1745. return CDBConnection::GetInstancePtr()->Execute(strSQLText);
  1746. }
  1747. //判断是否有电话,短信,EMAIL功能
  1748. INT CDBInterface::JudgeIsTSE( bool &bTel,bool &bSms,bool &bEmail )
  1749. {
  1750. CHAR strSQLText[MAX_SQL_LENGTH + 1] = "";
  1751. list<string> list1, list2, list3,list4;
  1752. list<string>::iterator it_list1, it_list2, it_list3, it_list4;
  1753. CString sUid,sTel,sMobile,sEmail;
  1754. CString str;
  1755. if(!_stricmp(g_strDBType, "PGSQL"))
  1756. {
  1757. sprintf(strSQLText, "select uid,tel,mobiletel,email from t_user_info");
  1758. }
  1759. else
  1760. {
  1761. sprintf(strSQLText, "select uid,tel,mobiletel,email from t_user_info");
  1762. }
  1763. int nRet = CDBConnection::GetInstancePtr()->GetFieldValueList4(strSQLText, list1, list2, list3,list4);
  1764. if( 0 == list1.size() || 0 == list2.size() || 0 == list3.size() || 0 == list4.size() )
  1765. {
  1766. return 0;
  1767. }
  1768. if( nRet != -1 )
  1769. {
  1770. for( it_list1 = list1.begin(), it_list2 = list2.begin(), it_list3 = list3.begin(), it_list4 = list4.begin();
  1771. it_list1 != list1.end() && it_list2 != list2.end() && it_list3 != list3.end() && it_list4 != list4.end();
  1772. it_list1++, it_list2++, it_list3++, it_list4++)
  1773. {
  1774. str = (*it_list1).c_str();
  1775. sUid = str.Trim();
  1776. str = (*it_list2).c_str();
  1777. sTel = str.Trim();
  1778. str = (*it_list3).c_str();
  1779. sMobile = str.Trim();
  1780. str = (*it_list4).c_str();
  1781. sEmail = str.Trim();
  1782. if( sUid.Compare("admin") )
  1783. {
  1784. if( sTel.Compare("") )
  1785. {
  1786. bTel = true;
  1787. }
  1788. if( sMobile.Compare("") )
  1789. {
  1790. bTel = true;
  1791. bSms = true;
  1792. }
  1793. if( sEmail.Compare("") )
  1794. {
  1795. bEmail = true;
  1796. }
  1797. }
  1798. }
  1799. }
  1800. for( it_list1 = list1.begin(), it_list2 = list2.begin(), it_list3 = list3.begin(), it_list4 = list4.begin();
  1801. it_list1 != list1.end(), it_list2 != list2.end(), it_list3 != list3.end(), it_list4 != list4.end(); )
  1802. {
  1803. list1.erase(it_list1++);
  1804. list2.erase(it_list2++);
  1805. list3.erase(it_list3++);
  1806. list4.erase(it_list4++);
  1807. }
  1808. return nRet;
  1809. }
  1810. };