PageRenderTime 65ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/source/PostGISGDO/PGSRecordset.cpp

#
C++ | 5121 lines | 3996 code | 433 blank | 692 comment | 806 complexity | c340f46aa961673929493a677456d12a MD5 | raw file
Possible License(s): Apache-2.0
  1. // Copyright 2011 Intergraph Corporation
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "PGSRecordset.hpp"
  15. #include "../Common/LogFiles.hpp"
  16. #include "../Common/PGtoGDOmaps.hpp"
  17. #include "../Common/VarUtils.hpp"
  18. #include "../Common/GDOtoEWKB.hpp"
  19. #include "../Common/pg_const.h"
  20. #include <stdio.h>
  21. #include "PostGISGDO.hpp"
  22. #include "PostGIS.rh"
  23. LPWSTR GetTableName(LPWSTR sSQL)
  24. {
  25. LPWSTR sRes = NULL;
  26. if(!wcschr(sSQL, ' '))
  27. {
  28. sRes = (LPWSTR)malloc((wcslen(sSQL) + 1)*sizeof(wchar_t));
  29. wcscpy(sRes, sSQL);
  30. }
  31. else
  32. {
  33. LPWSTR sFrom = wcsstr(sSQL, L" FROM ");
  34. if(!sFrom) sFrom = wcsstr(sSQL, L" from ");
  35. if(!sFrom) sFrom = wcsstr(sSQL, L" From ");
  36. if(sFrom)
  37. {
  38. sFrom += 6;
  39. while((sFrom[0] == ' ') && (sFrom[0] != 0))
  40. {
  41. sFrom++;
  42. }
  43. int slen = wcslen(sFrom);
  44. LPWSTR sSpace = wcschr(sFrom, ' ');
  45. LPWSTR sFirstDblQ = wcschr(sFrom, '\"');
  46. bool bDoubleQuoted = false;
  47. if(sFirstDblQ)
  48. {
  49. if(sSpace)
  50. {
  51. if(sFirstDblQ < sSpace) bDoubleQuoted = true;
  52. else slen = sSpace - sFrom;
  53. }
  54. else bDoubleQuoted = true;
  55. }
  56. else if(sSpace) slen = sSpace - sFrom;
  57. if(bDoubleQuoted)
  58. {
  59. LPWSTR sNextDblQ = wcschr(sFirstDblQ + 1, '\"');
  60. LPWSTR sDot = wcschr(sFrom, '.');
  61. if(sDot)
  62. {
  63. if(sDot < sFirstDblQ)
  64. {
  65. if(sNextDblQ) slen = sNextDblQ - sFrom + 1;
  66. }
  67. else if(sNextDblQ)
  68. {
  69. if(sDot == sNextDblQ + 1)
  70. {
  71. sDot++;
  72. if(sDot[0] == '\"')
  73. {
  74. sDot++;
  75. sNextDblQ = wcschr(sDot, '\"');
  76. if(sNextDblQ) slen = sNextDblQ - sFrom + 1;
  77. }
  78. else
  79. {
  80. sSpace = wcschr(sDot, ' ');
  81. if(sSpace) slen = sSpace - sFrom;
  82. }
  83. }
  84. else slen = sNextDblQ - sFrom + 1;
  85. }
  86. }
  87. else
  88. {
  89. if(sNextDblQ) slen = sNextDblQ - sFrom + 1;
  90. }
  91. }
  92. sRes = (LPWSTR)malloc((slen + 1)*sizeof(wchar_t));
  93. wcsncpy(sRes, sFrom, slen);
  94. sRes[slen] = 0;
  95. #if DBGLEVEL > 2
  96. WriteMallocReport(1, slen, wcslen(sRes));
  97. #endif
  98. }
  99. else
  100. {
  101. // the table name contains space(s}. If it is correctly enclosed
  102. // double quotes, then it should work
  103. sRes = (LPWSTR)malloc((wcslen(sSQL) + 1)*sizeof(wchar_t));
  104. wcscpy(sRes, sSQL);
  105. }
  106. }
  107. return(sRes);
  108. }
  109. bool IsMetatable(PConnStruct pConnStruct, LPSTR sName)
  110. {
  111. if(stricmp(pConnStruct->sGAlias, sName) == 0) return(true);
  112. bool bTrans = (pConnStruct->iSysTrans | pConnStruct->iGdoTrans);
  113. int iLen = 40 + strlen(pConnStruct->sGAlias) + strlen(sName);
  114. LPSTR sSql = (LPSTR)malloc(iLen*sizeof(char));
  115. sprintf(sSql, "select * from %s where tablename = '%s'",
  116. pConnStruct->sGAlias, sName);
  117. #if DBGLEVEL > 2
  118. WriteMallocReport(2, iLen - 1, strlen(sSql));
  119. #endif
  120. SetSP(pConnStruct->pConn, bTrans);
  121. //LPSTR lsCmd = GetSaveSQL(sSql, bTrans);
  122. PGresult *res = PQexec(pConnStruct->pConn, sSql);
  123. //free(lsCmd);
  124. free(sSql);
  125. if(PQresultStatus(res) != PGRES_TUPLES_OK)
  126. {
  127. PQclear(res);
  128. RollbackSP(pConnStruct->pConn, bTrans);
  129. return(false);
  130. }
  131. long nrows = PQntuples(res);
  132. PQclear(res);
  133. ReleaseSP(pConnStruct->pConn, bTrans);
  134. return(nrows > 0);
  135. /*if(stricmp(pConnStruct->sGFeatures, sName) == 0) return(true);
  136. if(stricmp(pConnStruct->sFieldLookup, sName) == 0) return(true);
  137. if(stricmp(pConnStruct->sAttributeProperties, sName) == 0) return(true);
  138. if(stricmp(pConnStruct->sGeometryProperties, sName) == 0) return(true);
  139. if(stricmp(pConnStruct->sCoordSystems, sName) == 0) return(true);
  140. if(stricmp(pConnStruct->sModTables, sName) == 0) return(true);
  141. if(stricmp(pConnStruct->sModLog, sName) == 0) return(true);
  142. if(stricmp(pConnStruct->sPickLists, sName) == 0) return(true);
  143. return(false);*/
  144. }
  145. // FldExtendedPropertySet
  146. FldExtendedPropertySet::FldExtendedPropertySet(IUnknown *pUnkOuter, ITypeLib *ALib) :
  147. CCOMDispatch(pUnkOuter, ALib, 0)
  148. {
  149. #if DBGLEVEL > 1
  150. WriteLogFile("FldExtendedPropertySet::FldExtendedPropertySet-1\r\n", true);
  151. #endif // DBGLEVEL
  152. m_lSinglePass = 0;
  153. m_lCache = 0;
  154. m_pField = NULL;
  155. }
  156. FldExtendedPropertySet::FldExtendedPropertySet(IUnknown *pUnkOuter, ITypeLib *ALib,
  157. int iIndex) : CCOMDispatch(pUnkOuter, ALib, iIndex)
  158. {
  159. #if DBGLEVEL > 1
  160. WriteLogFile("FldExtendedPropertySet::FldExtendedPropertySet-2\r\n", true);
  161. #endif // DBGLEVEL
  162. m_lSinglePass = 0;
  163. m_lCache = 0;
  164. m_pField = NULL;
  165. }
  166. FldExtendedPropertySet::~FldExtendedPropertySet()
  167. {
  168. #if DBGLEVEL > 1
  169. WriteLogFile("FldExtendedPropertySet::~FldExtendedPropertySet\r\n", true);
  170. #endif // DBGLEVEL
  171. }
  172. HRESULT FldExtendedPropertySet::QueryInterface(REFIID iid, void **ppvObject)
  173. {
  174. #if DBGLEVEL > 2
  175. WriteLogFile("FldExtendedPropertySet::QueryInterface\r\n", true);
  176. #endif // DBGLEVEL
  177. HRESULT hres = CCOMDispatch::QueryInterface(iid, ppvObject);
  178. if(hres != S_OK)
  179. {
  180. if(IsEqualIID(iid, DIID_ExtendedPropertySet))
  181. {
  182. hres = S_OK;
  183. *ppvObject = this;
  184. ((IUnknown*)*ppvObject)->AddRef();
  185. }
  186. else
  187. {
  188. hres = E_NOINTERFACE;
  189. #if DBGLEVEL > 2
  190. char buf[128];
  191. FormatGuid(buf, "Unknown Interface: ", "\r\n", iid);
  192. WriteLogFile(buf, true);
  193. #endif // DBGLEVEL
  194. }
  195. }
  196. return(hres);
  197. }
  198. ULONG FldExtendedPropertySet::Release()
  199. {
  200. ULONG lRest;
  201. IUnknown *pUnkOuter = m_pUnkOuter;
  202. if(pUnkOuter) lRest = pUnkOuter->Release();
  203. else lRest = --m_lRefCount;
  204. #if DBGLEVEL > 2
  205. char buf[64];
  206. if(pUnkOuter) sprintf(buf, "FldExtendedPropertySet::Release (aggregated) - %d\r\n", lRest);
  207. else sprintf(buf, "FldExtendedPropertySet::Release - %d\r\n", lRest);
  208. WriteLogFile(buf, true);
  209. #endif // DBGLEVEL
  210. if(pUnkOuter || (lRest > 0)) return lRest;
  211. delete(this);
  212. return(0);
  213. }
  214. HRESULT FldExtendedPropertySet::GetValue(BSTR PropertyName, VARIANT *Value)
  215. {
  216. #if DBGLEVEL > 0
  217. WriteLogFile("FldExtendedPropertySet::GetValue\r\n ", true);
  218. WriteLogFile("Name: ", false);
  219. WriteLogFile(PropertyName, false);
  220. WriteLogFile("\r\n", false);
  221. if(m_pField)
  222. {
  223. WriteLogFile(" Field Name: ", false);
  224. WriteLogFile(m_pField->GetNamePtr(), false);
  225. WriteLogFile("\r\n", false);
  226. WriteLogFile(" Field Table Name: ", false);
  227. WriteLogFile(m_pField->GetTblNamePtr(), false);
  228. WriteLogFile("\r\n", false);
  229. }
  230. #endif // DBGLEVEL
  231. if(!Value) return(E_POINTER);
  232. if(wcsicmp(PropertyName, L"Single Pass") == 0)
  233. {
  234. VariantClear(Value);
  235. Value->vt = VT_I4;
  236. Value->lVal = m_lSinglePass;
  237. return(S_OK);
  238. }
  239. else if(wcsicmp(PropertyName, L"Cache") == 0)
  240. {
  241. VariantClear(Value);
  242. Value->vt = VT_I4;
  243. Value->lVal = m_lCache;
  244. return(S_OK);
  245. }
  246. else if(wcsicmp(PropertyName, L"Key") == 0)
  247. {
  248. VariantClear(Value);
  249. if(m_pField)
  250. {
  251. Value->vt = VT_BOOL;
  252. if(m_pField->IsKeyField()) Value->boolVal = VARIANT_TRUE;
  253. else Value->boolVal = VARIANT_FALSE;
  254. return(S_OK);
  255. }
  256. }
  257. /*else if(wcsicmp(PropertyName, L"Description") == 0)
  258. {
  259. Value->vt = VT_BSTR;
  260. Value->bstrVal = SysAllocString(L"Desc");
  261. return(S_OK);
  262. }
  263. else if(wcsicmp(PropertyName, L"GeometryType") == 0)
  264. {
  265. if(m_pField)
  266. {
  267. Value->vt = VT_I4;
  268. Value->lVal = m_pField->GetGeomType();
  269. return(S_OK);
  270. }
  271. }
  272. else if(wcsicmp(PropertyName, L"PrimaryGeometryFlag") == 0)
  273. {
  274. if(m_pField)
  275. {
  276. Value->vt = VT_BOOL;
  277. Value->boolVal = VARIANT_FALSE;
  278. return(S_OK);
  279. }
  280. }
  281. else if(wcsicmp(PropertyName, L"GCoordSystemGUID") == 0)
  282. {
  283. if(m_pField)
  284. {
  285. return(m_pField->get_CoordSystemGUID(Value));
  286. }
  287. }
  288. else if(wcsicmp(PropertyName, L"Locatable") == 0)
  289. {
  290. Value->vt = VT_BOOL;
  291. Value->boolVal = VARIANT_TRUE;
  292. return(S_OK);
  293. }
  294. else if(wcsicmp(PropertyName, L"Type") == 0)
  295. {
  296. if(m_pField)
  297. {
  298. Value->vt = VT_I2;
  299. Value->iVal = m_pField->GetType();
  300. return(S_OK);
  301. }
  302. }
  303. else if(wcsicmp(PropertyName, L"Displayable") == 0)
  304. {
  305. Value->vt = VT_BOOL;
  306. Value->boolVal = VARIANT_TRUE;
  307. return(S_OK);
  308. }
  309. else if(wcsicmp(PropertyName, L"Format") == 0)
  310. {
  311. Value->vt = VT_BSTR;
  312. Value->bstrVal = SysAllocString(L"general");
  313. return(S_OK);
  314. }
  315. else if(wcsicmp(PropertyName, L"Precision") == 0)
  316. {
  317. if((m_pField->GetType() == 7) || m_pField->GetType() == 8))
  318. {
  319. Value->vt = VT_I4;
  320. Value->lVal = 8;
  321. return(S_OK);
  322. }
  323. }*/
  324. Value->vt = VT_NULL;
  325. //return(S_OK);
  326. return(E_NOTIMPL);
  327. }
  328. HRESULT FldExtendedPropertySet::SetValue(BSTR PropertyName, VARIANT Value, long *Status)
  329. {
  330. #if DBGLEVEL > 0
  331. WriteLogFile("FldExtendedPropertySet::SetValue\r\n ", true);
  332. WriteLogFile("Name: ", false);
  333. WriteLogFile(PropertyName, false);
  334. WriteLogFile("\r\n", false);
  335. #endif // DBGLEVEL
  336. if(wcsicmp(PropertyName, L"Single Pass") == 0)
  337. {
  338. m_lSinglePass = VarToLong(Value);
  339. *Status = S_OK;
  340. return(S_OK);
  341. }
  342. else if(wcsicmp(PropertyName, L"Cache") == 0)
  343. {
  344. m_lCache = VarToLong(Value);
  345. *Status = S_OK;
  346. return(S_OK);
  347. }
  348. return(E_NOTIMPL);
  349. }
  350. void FldExtendedPropertySet::SetTDField(GTDField *pField)
  351. {
  352. m_pField = pField;
  353. return;
  354. }
  355. // GField
  356. GField::GField(IUnknown *pUnkOuter, ITypeLib *ALib) : CCOMDispatch(pUnkOuter, ALib, 20)
  357. {
  358. #if DBGLEVEL > 1
  359. WriteLogFile("GField::GField-1\r\n", true);
  360. #endif // DBGLEVEL
  361. m_sOrigName = NULL;
  362. m_wsName = NULL;
  363. m_pConnStruct = NULL;
  364. m_pExt = NULL;
  365. m_pLitConv = NULL;
  366. m_sNewIdCmd = NULL;
  367. m_sGenIdCmd = NULL;
  368. m_pvBuffer = NULL;
  369. InitFldAttrs(&m_cAttrs);
  370. VariantInit(&m_vNewVal);
  371. m_vNewVal.vt = VT_EMPTY;
  372. m_iSpecType = 0;
  373. m_iSpecField = 0;
  374. m_pSpecField = NULL;
  375. m_bIsMetatable = false;
  376. }
  377. GField::GField(IUnknown *pUnkOuter, ITypeLib *ALib, int iIndex) :
  378. CCOMDispatch(pUnkOuter, ALib, iIndex)
  379. {
  380. #if DBGLEVEL > 1
  381. WriteLogFile("GField::GField-2\r\n", true);
  382. #endif // DBGLEVEL
  383. m_sOrigName = NULL;
  384. m_wsName = NULL;
  385. m_pConnStruct = NULL;
  386. m_pExt = NULL;
  387. m_pLitConv = NULL;
  388. m_sNewIdCmd = NULL;
  389. m_sGenIdCmd = NULL;
  390. m_pvBuffer = NULL;
  391. InitFldAttrs(&m_cAttrs);
  392. VariantInit(&m_vNewVal);
  393. m_vNewVal.vt = VT_EMPTY;
  394. m_iSpecType = 0;
  395. m_iSpecField = 0;
  396. m_pSpecField = NULL;
  397. m_bIsMetatable = false;
  398. }
  399. GField::~GField()
  400. {
  401. #if DBGLEVEL > 1
  402. WriteLogFile("GField::~GField\r\n", true);
  403. #endif // DBGLEVEL
  404. if(m_pSpecField) m_pSpecField->Release();
  405. VariantClear(&m_vNewVal);
  406. ClearFldAttrs(&m_cAttrs);
  407. if(m_sGenIdCmd) free(m_sGenIdCmd);
  408. if(m_sNewIdCmd) free(m_sNewIdCmd);
  409. if(m_pExt) m_pExt->SetOuter(NULL);
  410. if(m_pLitConv) m_pLitConv->SetOuter(NULL);
  411. if(m_wsName) free(m_wsName);
  412. if(m_sOrigName) free(m_sOrigName);
  413. }
  414. HRESULT GField::QueryInterface(REFIID iid, void **ppvObject)
  415. {
  416. #if DBGLEVEL > 2
  417. WriteLogFile("GField::QueryInterface\r\n", true);
  418. #endif // DBGLEVEL
  419. HRESULT hres = CCOMDispatch::QueryInterface(iid, ppvObject);
  420. if(hres != S_OK)
  421. {
  422. if(IsEqualIID(iid, DIID_GField))
  423. {
  424. hres = S_OK;
  425. *ppvObject = this;
  426. ((IUnknown*)*ppvObject)->AddRef();
  427. }
  428. else
  429. {
  430. hres = E_NOINTERFACE;
  431. #if DBGLEVEL > 2
  432. char buf[128];
  433. FormatGuid(buf, "Unknown Interface: ", "\r\n", iid);
  434. WriteLogFile(buf, true);
  435. #endif // DBGLEVEL
  436. }
  437. }
  438. return(hres);
  439. }
  440. ULONG GField::Release()
  441. {
  442. ULONG lRest;
  443. IUnknown *pUnkOuter = m_pUnkOuter;
  444. if(pUnkOuter) lRest = pUnkOuter->Release();
  445. else lRest = --m_lRefCount;
  446. #if DBGLEVEL > 2
  447. char buf[64];
  448. if(pUnkOuter) sprintf(buf, "GField::Release (aggregated) - %d\r\n", lRest);
  449. else sprintf(buf, "GField::Release - %d\r\n", lRest);
  450. WriteLogFile(buf, true);
  451. #endif // DBGLEVEL
  452. if(pUnkOuter || (lRest > 0)) return lRest;
  453. delete(this);
  454. return(0);
  455. }
  456. HRESULT GField::get_Value(VARIANT *pValue)
  457. {
  458. #if DBGLEVEL > 0
  459. WriteLogFile("GField::get_Value (", true);
  460. WriteLogFile(m_wsName, false);
  461. WriteLogFile(")\r\n", false);
  462. GFields *pFlds = (GFields*)m_pUnkOuter;
  463. GRecordset *pRS = pFlds->GetParentRecordset();
  464. WriteLogFile(" Recordset (", false);
  465. WriteLogFile(pRS->GetNamePtr(), false);
  466. WriteLogFile(")\r\n", false);
  467. #endif // DBGLEVEL
  468. if(m_iSpecField == 1)
  469. {
  470. HRESULT hres = m_pSpecField->get_Value(pValue);
  471. if(pValue->vt > VT_NULL) return(hres);
  472. }
  473. if(!pValue) return(E_POINTER);
  474. //if(!m_pvBuffer) return(E_POINTER);
  475. // It looks like GeoMedia is able to pass an uninitialized VARIANT into
  476. // this method. When the vt is somehow od (e.g. 83 in our case),
  477. // the system does not know how to clear it and refuses to copy into it
  478. // So we try to clear it and if it fails, we simply set the vt to empty
  479. //VariantClear(pValue);
  480. pValue->vt = VT_EMPTY;
  481. // end of hack
  482. if(m_vNewVal.vt > VT_EMPTY) VariantCopy(pValue, &m_vNewVal);
  483. else if(m_pvBuffer) VariantCopy(pValue, m_pvBuffer);
  484. /*WriteVariantToLogFile(L" Value: ", *pValue);
  485. char buf[32];
  486. sprintf(buf, " VType: %d\r\n", pValue->vt);
  487. WriteLogFile(buf, false);*/
  488. return(S_OK);
  489. }
  490. HRESULT GField::set_Value(VARIANT pValue)
  491. {
  492. ValidateVariant(&pValue);
  493. #if DBGLEVEL > 0
  494. WriteLogFile("GField::set_Value\r\n", true);
  495. WriteLogFile(" Field name: ", false);
  496. WriteLogFile(m_wsName, false);
  497. WriteVariantToLogFile(L"\r\n Value: ", pValue);
  498. char buf[32];
  499. sprintf(buf, " VType: %d\r\n", pValue.vt);
  500. WriteLogFile(buf, false);
  501. #endif // DBGLEVEL
  502. if(m_iSpecField > 0) m_pSpecField->set_Value(pValue);
  503. // hack for G/I Toolkit
  504. if((pValue.vt == VT_BSTR) && (m_cAttrs.iType < 8))
  505. {
  506. int iVal;
  507. float fltVal;
  508. switch(m_cAttrs.iType)
  509. {
  510. case 1:
  511. VariantClear(&m_vNewVal);
  512. m_vNewVal.vt = VT_BOOL;
  513. m_vNewVal.boolVal = (wcslen(pValue.bstrVal) > 0) &&
  514. ((pValue.bstrVal[0] == 'T') || (pValue.bstrVal[0] == 't'));
  515. return(S_OK);
  516. case 2:
  517. VariantClear(&m_vNewVal);
  518. if(swscanf(pValue.bstrVal, L"%d", &iVal) == 1)
  519. {
  520. m_vNewVal.vt = VT_UI1;
  521. m_vNewVal.bVal = (BYTE)iVal;
  522. }
  523. return(S_OK);
  524. case 3:
  525. VariantClear(&m_vNewVal);
  526. if(swscanf(pValue.bstrVal, L"%d", &iVal) == 1)
  527. {
  528. m_vNewVal.vt = VT_I2;
  529. m_vNewVal.iVal = iVal;
  530. }
  531. return(S_OK);
  532. case 4:
  533. VariantClear(&m_vNewVal);
  534. if(swscanf(pValue.bstrVal, L"%d", &iVal) == 1)
  535. {
  536. m_vNewVal.vt = VT_I4;
  537. m_vNewVal.lVal = iVal;
  538. }
  539. return(S_OK);
  540. case 5:
  541. VariantClear(&m_vNewVal);
  542. m_vNewVal.vt = VT_CY;
  543. m_vNewVal.cyVal.int64 = CurrencyFromBSTR(pValue.bstrVal, m_pConnStruct->sDecSep);
  544. return(S_OK);
  545. case 6:
  546. VariantClear(&m_vNewVal);
  547. if(swscanf(pValue.bstrVal, L"%f", &fltVal) == 1)
  548. {
  549. m_vNewVal.vt = VT_R4;
  550. m_vNewVal.fltVal = fltVal;
  551. }
  552. return(S_OK);
  553. case 7:
  554. VariantClear(&m_vNewVal);
  555. if(swscanf(pValue.bstrVal, L"%f", &fltVal) == 1)
  556. {
  557. m_vNewVal.vt = VT_R8;
  558. m_vNewVal.dblVal = fltVal;
  559. }
  560. return(S_OK);
  561. }
  562. }
  563. // end of hack
  564. VariantCopy(&m_vNewVal, &pValue);
  565. return(S_OK);
  566. }
  567. HRESULT GField::get_AllowZeroLength(VARIANT_BOOL *pAllow)
  568. {
  569. #if DBGLEVEL > 0
  570. WriteLogFile("GField::get_AllowZeroLength\r\n", true);
  571. #endif // DBGLEVEL
  572. if(!pAllow) return(E_POINTER);
  573. /*if((m_iSpecType == 2) && (wcsicmp(m_wsName, L"csguid") == 0))
  574. {
  575. *pAllow = VARIANT_FALSE;
  576. return(S_OK);
  577. }*/
  578. *pAllow = VARIANT_TRUE;
  579. return(S_OK);
  580. }
  581. HRESULT GField::set_AllowZeroLength(VARIANT_BOOL pAllow)
  582. {
  583. #if DBGLEVEL > 0
  584. WriteLogFile("GField::set_AllowZeroLength\r\n", true);
  585. #endif // DBGLEVEL
  586. return(E_NOTIMPL);
  587. }
  588. HRESULT GField::get_Attributes(long *pAttr)
  589. {
  590. #if DBGLEVEL > 0
  591. WriteLogFile("GField::get_Attributes\r\n", true);
  592. #endif // DBGLEVEL
  593. if(!pAttr) return(E_POINTER);
  594. *pAttr = m_cAttrs.lAttr;
  595. return(S_OK);
  596. }
  597. HRESULT GField::set_Attributes(long pAttr)
  598. {
  599. #if DBGLEVEL > 0
  600. WriteLogFile("GField::set_Attributes\r\n", true);
  601. #endif // DBGLEVEL
  602. return(E_NOTIMPL);
  603. }
  604. HRESULT GField::get_CollatingOrder(long *pOrder)
  605. {
  606. #if DBGLEVEL > 0
  607. WriteLogFile("GField::get_CollatingOrder\r\n", true);
  608. #endif // DBGLEVEL
  609. if(!pOrder) return(E_POINTER);
  610. *pOrder = 0;
  611. return(S_OK);
  612. }
  613. HRESULT GField::get_CoordSystemGUID(VARIANT *CSGuid)
  614. {
  615. #if DBGLEVEL > 0
  616. WriteLogFile("GField::get_CoordSystemGUID\r\n", true);
  617. #endif // DBGLEVEL
  618. if(!CSGuid) return(E_POINTER);
  619. if((m_cAttrs.ulSrid == 0) && (!m_cAttrs.sCSGuid[0])) return(E_FAIL);
  620. VariantClear(CSGuid);
  621. if(m_cAttrs.sCSGuid[0])
  622. {
  623. CSGuid->vt = VT_BSTR;
  624. CSGuid->bstrVal = SysAllocString(m_cAttrs.sCSGuid);
  625. }
  626. else
  627. {
  628. VARIANT *pRow = FindCSTableRow(&m_pConnStruct->cCSTable,
  629. m_cAttrs.ulSrid);
  630. if(pRow) VariantCopy(CSGuid, &pRow[1]);
  631. else CSGuid->vt = VT_NULL;
  632. }
  633. #if DBGLEVEL > 0
  634. WriteVariantToLogFile(L"CS GUID: ", *CSGuid);
  635. #endif // DBGLEVEL
  636. return(S_OK);
  637. }
  638. HRESULT GField::set_CoordSystemGUID(VARIANT CSGuid)
  639. {
  640. #if DBGLEVEL > 0
  641. WriteLogFile("GField::set_CoordSystemGUID\r\n", true);
  642. #endif // DBGLEVEL
  643. return(E_NOTIMPL);
  644. }
  645. HRESULT GField::get_DataUpdatable(VARIANT_BOOL *pUpdatable)
  646. {
  647. #if DBGLEVEL > 0
  648. WriteLogFile("GField::get_DataUpdatable\r\n", true);
  649. #endif // DBGLEVEL
  650. if(!pUpdatable) return(E_POINTER);
  651. if(m_cAttrs.bUpdatable) *pUpdatable = VARIANT_TRUE;
  652. else *pUpdatable = VARIANT_FALSE;
  653. return(S_OK);
  654. }
  655. HRESULT GField::set_DefaultValue(BSTR pValue)
  656. {
  657. #if DBGLEVEL > 0
  658. WriteLogFile("GField::set_DefaultValue\r\n", true);
  659. #endif // DBGLEVEL
  660. return(E_NOTIMPL);
  661. }
  662. HRESULT GField::get_DefaultValue(BSTR *pValue)
  663. {
  664. #if DBGLEVEL > 0
  665. WriteLogFile("GField::get_DefaultValue\r\n", true);
  666. #endif // DBGLEVEL
  667. if(!pValue) return(E_POINTER);
  668. //if(*pValue) SysFreeString(*pValue);
  669. *pValue = NULL;
  670. if(m_cAttrs.wsDefVal) *pValue = SysAllocString(m_cAttrs.wsDefVal);
  671. else *pValue = SysAllocString(L"");
  672. return(S_OK);
  673. }
  674. HRESULT GField::get_Name(BSTR *pName)
  675. {
  676. #if DBGLEVEL > 0
  677. WriteLogFile("GField::get_Name\r\n", true);
  678. #if DBGLEVEL > 2
  679. WriteLogFile(" FieldName: ", false);
  680. WriteLogFile(m_wsName, false);
  681. if(m_cAttrs.wsTblName)
  682. {
  683. WriteLogFile("\r\n TableName: ", false);
  684. WriteLogFile(m_cAttrs.wsTblName, false);
  685. }
  686. WriteLogFile("\r\n", false);
  687. #endif // DBGLEVEL
  688. #endif // DBGLEVEL
  689. if(!pName) return(E_POINTER);
  690. //if(*pName) SysFreeString(*pName);
  691. // Hack for GCoordSystem table
  692. // at some point, GeoMedia requires that the GCoordSystem table fields
  693. // are in uppercase
  694. if(m_iSpecType == 2)
  695. {
  696. if(m_wsName)
  697. {
  698. wchar_t wsbuf[64];
  699. wcscpy(wsbuf, m_wsName);
  700. _wcsupr(wsbuf);
  701. *pName = SysAllocString(wsbuf);
  702. }
  703. else *pName = SysAllocString(L"");
  704. return(S_OK);
  705. }
  706. if(m_wsName) *pName = SysAllocString(m_wsName);
  707. else *pName = SysAllocString(L"");
  708. return(S_OK);
  709. }
  710. HRESULT GField::set_Name(BSTR pName)
  711. {
  712. #if DBGLEVEL > 0
  713. WriteLogFile("GField::set_Name\r\n", true);
  714. #endif // DBGLEVEL
  715. return(E_NOTIMPL);
  716. }
  717. HRESULT GField::get_Required(VARIANT_BOOL *pRequired)
  718. {
  719. #if DBGLEVEL > 0
  720. WriteLogFile("GField::get_Required\r\n", true);
  721. #endif // DBGLEVEL
  722. if(!pRequired) return(E_POINTER);
  723. if(!m_cAttrs.wsTblName)
  724. {
  725. *pRequired = VARIANT_FALSE;
  726. return(S_OK);
  727. }
  728. if((m_iSpecType == 2) && (wcsicmp(m_wsName, L"csguid") == 0))
  729. {
  730. *pRequired = VARIANT_TRUE;
  731. return(S_OK);
  732. }
  733. if(m_cAttrs.bRequired) *pRequired = VARIANT_TRUE;
  734. else *pRequired = VARIANT_FALSE;
  735. return(S_OK);
  736. }
  737. HRESULT GField::set_Required(VARIANT_BOOL pRequired)
  738. {
  739. #if DBGLEVEL > 0
  740. WriteLogFile("GField::set_Required\r\n", true);
  741. #endif // DBGLEVEL
  742. return(E_NOTIMPL);
  743. }
  744. HRESULT GField::get_Size(long *pSize)
  745. {
  746. #if DBGLEVEL > 0
  747. WriteLogFile("GField::get_Size\r\n", true);
  748. #endif // DBGLEVEL
  749. if(!pSize) return(E_POINTER);
  750. *pSize = m_cAttrs.lSize;
  751. return(S_OK);
  752. }
  753. HRESULT GField::set_Size(long pSize)
  754. {
  755. #if DBGLEVEL > 0
  756. WriteLogFile("GField::set_Size\r\n", true);
  757. #endif // DBGLEVEL
  758. return(E_NOTIMPL);
  759. }
  760. HRESULT GField::get_SourceDatabase(BSTR *pDatabase)
  761. {
  762. #if DBGLEVEL > 0
  763. WriteLogFile("GField::get_SourceDatabase\r\n", true);
  764. #endif // DBGLEVEL
  765. if(!pDatabase) return(E_POINTER);
  766. //if(*pDatabase) SysFreeString(*pDatabase);
  767. *pDatabase = NULL;
  768. if(m_pConnStruct->wsServerName)
  769. *pDatabase = SysAllocString(m_pConnStruct->wsServerName);
  770. return(S_OK);
  771. }
  772. HRESULT GField::get_SourceField(BSTR *pField)
  773. {
  774. #if DBGLEVEL > 0
  775. WriteLogFile("GField::get_SourceField\r\n", true);
  776. #endif // DBGLEVEL
  777. if(!pField) return(E_POINTER);
  778. //if(*pField) SysFreeString(*pField);
  779. if(m_wsName) *pField = SysAllocString(m_wsName);
  780. else *pField = SysAllocString(L"");
  781. return(S_OK);
  782. }
  783. HRESULT GField::get_SourceTable(BSTR *pTable)
  784. {
  785. #if DBGLEVEL > 0
  786. WriteLogFile("GField::get_SourceTable\r\n", true);
  787. #endif // DBGLEVEL
  788. if(!pTable) return(E_POINTER);
  789. //if(*pTable) SysFreeString(*pTable);
  790. *pTable = NULL;
  791. if(m_cAttrs.wsTblName) *pTable = SysAllocString(m_cAttrs.wsTblName);
  792. return(S_OK);
  793. }
  794. HRESULT GField::get_SubType(long *pGeometryType)
  795. {
  796. #if DBGLEVEL > 0
  797. WriteLogFile("GField::get_SubType\r\n", true);
  798. #endif // DBGLEVEL
  799. if(!pGeometryType) return(E_POINTER);
  800. *pGeometryType = m_cAttrs.lSubType;
  801. return(S_OK);
  802. }
  803. HRESULT GField::set_SubType(long pGeometryType)
  804. {
  805. #if DBGLEVEL > 0
  806. WriteLogFile("GField::set_SubType\r\n", true);
  807. #endif // DBGLEVEL
  808. return(E_NOTIMPL);
  809. }
  810. HRESULT GField::get_Type(short *pType)
  811. {
  812. #if DBGLEVEL > 0
  813. WriteLogFile("GField::get_Type\r\n", true);
  814. #if DBGLEVEL > 2
  815. WriteLogFile(" FieldName: ", false);
  816. WriteLogFile(m_wsName, false);
  817. if(m_cAttrs.wsTblName)
  818. {
  819. WriteLogFile("\r\n TableName: ", false);
  820. WriteLogFile(m_cAttrs.wsTblName, false);
  821. }
  822. WriteLogFile("\r\n", false);
  823. #endif // DBGLEVEL
  824. char buf[32];
  825. sprintf(buf, " Type: %d\r\n", m_cAttrs.iType);
  826. WriteLogFile(buf, false);
  827. #endif // DBGLEVEL
  828. if(!pType) return(E_POINTER);
  829. *pType = m_cAttrs.iType;
  830. return(S_OK);
  831. }
  832. HRESULT GField::set_Type(short pType)
  833. {
  834. #if DBGLEVEL > 0
  835. WriteLogFile("GField::set_Type\r\n", true);
  836. #endif // DBGLEVEL
  837. return(E_NOTIMPL);
  838. }
  839. HRESULT GField::AppendChunk(VARIANT Val)
  840. {
  841. #if DBGLEVEL > 0
  842. WriteLogFile("GField::AppendChunk\r\n", true);
  843. #endif // DBGLEVEL
  844. return(E_NOTIMPL);
  845. }
  846. HRESULT GField::FieldSize(long *pSize)
  847. {
  848. #if DBGLEVEL > 0
  849. WriteLogFile("GField::FieldSize\r\n", true);
  850. #endif // DBGLEVEL
  851. if(!pSize) return(E_POINTER);
  852. if(!m_pvBuffer) return(S_FALSE);
  853. *pSize = VarGetSize(*m_pvBuffer);
  854. return(S_OK);
  855. }
  856. HRESULT GField::GetChunk(long Offset, long Bytes, VARIANT *pChunk)
  857. {
  858. #if DBGLEVEL > 0
  859. WriteLogFile("GField::GetChunk\r\n", true);
  860. #endif // DBGLEVEL
  861. if(!pChunk) return(E_POINTER);
  862. return(E_NOTIMPL);
  863. }
  864. HRESULT GField::GetExtension(BSTR Name, IDispatch * *ppGExtension)
  865. {
  866. #if DBGLEVEL > 0
  867. WriteLogFile("GField::GetExtension\r\n", true);
  868. WriteLogFile(" FieldName: ", false);
  869. WriteLogFile(m_wsName, false);
  870. if(m_cAttrs.wsTblName)
  871. {
  872. WriteLogFile("\r\n TableName: ", false);
  873. WriteLogFile(m_cAttrs.wsTblName, false);
  874. }
  875. WriteLogFile("\r\n Name: ", false);
  876. WriteLogFile(Name, false);
  877. WriteLogFile("\r\n", false);
  878. #endif // DBGLEVEL
  879. //if(m_bIsMetatable && (m_iSpecType != 2)) return(E_NOTIMPL);
  880. if(m_bIsMetatable) return(E_NOTIMPL);
  881. if(wcsicmp(Name, L"DefaultValueLiteralConversion") == 0)
  882. {
  883. //return(E_NOTIMPL);
  884. if(!m_pLitConv)
  885. {
  886. m_pLitConv = new FldLitConvExt(this,
  887. ((ITypeLib**)m_pConnStruct->ppTypeLibs)[2]);
  888. }
  889. if(!ppGExtension) return(E_POINTER);
  890. if(*ppGExtension) (*ppGExtension)->Release();
  891. m_pLitConv->AddRef();
  892. *ppGExtension = m_pLitConv;
  893. return(S_OK);
  894. }
  895. if(wcsicmp(Name, L"ExtendedPropertySet") == 0)
  896. {
  897. if(!m_pExt) return(E_NOTIMPL);
  898. if(!ppGExtension) return(E_POINTER);
  899. if(*ppGExtension) (*ppGExtension)->Release();
  900. m_pExt->AddRef();
  901. *ppGExtension = m_pExt;
  902. return(S_OK);
  903. }
  904. return(E_NOTIMPL);
  905. }
  906. LPWSTR GField::GetNamePtr()
  907. {
  908. return(m_wsName);
  909. }
  910. LPSTR GField::GetOrigNamePtr()
  911. {
  912. return(m_sOrigName);
  913. }
  914. Oid GField::GetTableOid()
  915. {
  916. return(m_cAttrs.lTblOid);
  917. }
  918. void GField::SetAttrs(PConnStruct pConnStruct, PGresult *pRes, int iPos,
  919. Oid lType, bool bUpdatable, int iSpecType, bool bMetatable)
  920. {
  921. m_pConnStruct = pConnStruct;
  922. m_pErrors = (GErrors*)m_pConnStruct->pErrors;
  923. m_iPos = iPos;
  924. m_iSpecType = iSpecType;
  925. m_bIsMetatable = bMetatable;
  926. if(m_sOrigName) free(m_sOrigName);
  927. m_sOrigName = NULL;
  928. if(m_wsName) free(m_wsName);
  929. m_wsName = NULL;
  930. LPSTR sVal = PQfname(pRes, iPos);
  931. if(sVal)
  932. {
  933. m_sOrigName = (LPSTR)malloc((strlen(sVal) + 1)*sizeof(char));
  934. strcpy(m_sOrigName, sVal);
  935. m_wsName = DBStrToWChar(sVal, m_pConnStruct->lDBEnc);
  936. }
  937. //if(!iSpecType)
  938. {
  939. m_pExt = new FldExtendedPropertySet(this,
  940. ((ITypeLib**)pConnStruct->ppTypeLibs)[1]);
  941. }
  942. m_cAttrs.lTblOid = PQftable(pRes, iPos);
  943. m_cAttrs.iTblPos = PQftablecol(pRes, iPos);
  944. m_cAttrs.bUpdatable = bUpdatable;
  945. int iMod = PQfmod(pRes, iPos);
  946. int iSize = PQfsize(pRes, iPos);
  947. m_cAttrs.lTypeOid = lType;
  948. m_cAttrs.iType = OidToGdoType(lType, iSize, iMod, &m_cAttrs.lSize);
  949. #if DBGLEVEL > 1
  950. WriteLogFile("GField::SetAttrs\r\n", true);
  951. char sbuf[128];
  952. sprintf(sbuf, " Name: %s, PG Type: %d, GDO Type: %d\r\n",
  953. m_sOrigName, lType, m_cAttrs.iType);
  954. WriteLogFile(sbuf, false);
  955. #endif // DBGLEVEL
  956. return;
  957. }
  958. int GField::UpdateField(GTableDefs *pGTbls)
  959. {
  960. GTableDef *ptd = pGTbls->FindByOid(m_cAttrs.lTblOid);
  961. GTDField *pfld = NULL;
  962. if(ptd)
  963. {
  964. ptd->BuildFromConnection();
  965. pfld = ptd->GetFieldPtrByTblPos(m_cAttrs.iTblPos);
  966. }
  967. if(pfld)
  968. {
  969. pfld->CopyAttributes(&m_cAttrs);
  970. // this was formerly intended for tables without key, however
  971. // but the oposite way seems to be usually used in GM
  972. //if(!ptd->GetHasUniqueIndex()) m_pExt->SetTDField(pfld);
  973. if(m_pExt && ptd->GetHasUniqueIndex()) m_pExt->SetTDField(pfld);
  974. }
  975. if(m_cAttrs.sDefValDef)
  976. {
  977. LPSTR sEnd = NULL;
  978. LPSTR sStart = strchr(m_cAttrs.sDefValDef, '\'');
  979. if(sStart)
  980. {
  981. sStart++;
  982. sEnd = strchr(sStart, '\'');
  983. }
  984. if(sEnd)
  985. {
  986. m_cAttrs.bUpdatable = false;
  987. m_cAttrs.lAttr &= ~32;
  988. m_cAttrs.lAttr |= 16;
  989. int ilen = sEnd - sStart;
  990. m_sNewIdCmd = (LPSTR)malloc((ilen + 38)*sizeof(char));
  991. strcpy(m_sNewIdCmd, "select last_value, is_called from ");
  992. int icurlen = strlen(m_sNewIdCmd);
  993. strncat(m_sNewIdCmd, sStart, ilen);
  994. m_sNewIdCmd[icurlen + ilen] = 0;
  995. #if DBGLEVEL > 2
  996. WriteMallocReport(4, ilen + 37, strlen(m_sNewIdCmd));
  997. #endif
  998. m_sGenIdCmd = (LPSTR)malloc((ilen + 22)*sizeof(char));
  999. strcpy(m_sGenIdCmd, "select nextval('");
  1000. icurlen = strlen(m_sGenIdCmd);
  1001. strncat(m_sGenIdCmd, sStart, ilen);
  1002. m_sGenIdCmd[icurlen + ilen] = 0;
  1003. strcat(m_sGenIdCmd, "')");
  1004. #if DBGLEVEL > 2
  1005. WriteMallocReport(5, ilen + 21, strlen(m_sGenIdCmd));
  1006. #endif
  1007. }
  1008. }
  1009. else if(m_cAttrs.wsDefVal)
  1010. {
  1011. if(m_cAttrs.iType == gdbGuid)
  1012. {
  1013. if(wcschr(m_cAttrs.wsDefVal, '(') != NULL)
  1014. {
  1015. m_cAttrs.lAttr &= ~32;
  1016. m_cAttrs.bUpdatable = false;
  1017. }
  1018. }
  1019. }
  1020. #if DBGLEVEL > 1
  1021. WriteLogFile("GField::UpdateField\r\n", true);
  1022. char sbuf[128];
  1023. sprintf(sbuf, " Name: %s, GDO Type: %d, SubType: %ld\r\n",
  1024. m_sOrigName, m_cAttrs.iType, m_cAttrs.lSubType);
  1025. #if DBGLEVEL > 2
  1026. WriteMallocReport(6, 127, strlen(sbuf));
  1027. #endif
  1028. WriteLogFile(sbuf, false);
  1029. #endif // DBGLEVEL
  1030. return(m_cAttrs.iGeomDim);
  1031. }
  1032. void GField::SetNewVal()
  1033. {
  1034. VariantClear(&m_vNewVal);
  1035. m_vNewVal.vt = VT_EMPTY;
  1036. if(m_sNewIdCmd)
  1037. {
  1038. bool bTrans = (m_pConnStruct->iSysTrans | m_pConnStruct->iGdoTrans);
  1039. SetSP(m_pConnStruct->pConn, bTrans);
  1040. //LPSTR lsCmd = GetSaveSQL(m_sNewIdCmd, bTrans);
  1041. PGresult *res = PQexecParams(m_pConnStruct->pConn, m_sNewIdCmd, 0,
  1042. NULL, NULL, NULL, NULL, 1);
  1043. //free(lsCmd);
  1044. if(PQresultStatus(res) != PGRES_TUPLES_OK)
  1045. {
  1046. WritePQErrorToLog("Spot 1: ", m_pConnStruct->pConn);
  1047. PQclear(res);
  1048. RollbackSP(m_pConnStruct->pConn, bTrans);
  1049. }
  1050. else
  1051. {
  1052. long nrows = PQntuples(res);
  1053. if(nrows > 0)
  1054. {
  1055. Oid piTypes[2];
  1056. VARIANT vVals[2];
  1057. VariantInit(&vVals[0]);
  1058. VariantInit(&vVals[1]);
  1059. DescribeFieldTypes(res, 2, piTypes);
  1060. GetRecordBin(res, 0, 2, piTypes, vVals,
  1061. m_pConnStruct->lDBEnc, m_pConnStruct->lGeomOid, NULL, m_pConnStruct->iCurrDigits);
  1062. m_vNewVal.vt = VT_I4;
  1063. if(VarToBool(vVals[1]))
  1064. m_vNewVal.lVal = VarToLong(vVals[0]) + 1;
  1065. else m_vNewVal.lVal = 1;
  1066. }
  1067. PQclear(res);
  1068. ReleaseSP(m_pConnStruct->pConn, bTrans);
  1069. }
  1070. }
  1071. else if(m_cAttrs.wsDefVal)
  1072. {
  1073. int iVal;
  1074. float fVal;
  1075. SYSTEMTIME sTime;
  1076. switch(m_cAttrs.iType)
  1077. {
  1078. case gdbBoolean:
  1079. m_vNewVal.vt = VT_BOOL;
  1080. m_vNewVal.boolVal = FALSE;
  1081. if((m_cAttrs.wsDefVal[0] == 'T') || (m_cAttrs.wsDefVal[0] == 't'))
  1082. m_vNewVal.boolVal = TRUE;
  1083. break;
  1084. case gdbByte:
  1085. if(swscanf(m_cAttrs.wsDefVal, L"%d", &iVal) == 1)
  1086. {
  1087. m_vNewVal.vt = VT_UI1;
  1088. m_vNewVal.bVal = iVal;
  1089. }
  1090. break;
  1091. case gdbInteger:
  1092. if(swscanf(m_cAttrs.wsDefVal, L"%d", &iVal) == 1)
  1093. {
  1094. m_vNewVal.vt = VT_I2;
  1095. m_vNewVal.iVal = iVal;
  1096. }
  1097. break;
  1098. case gdbLong:
  1099. if(swscanf(m_cAttrs.wsDefVal, L"%d", &iVal) == 1)
  1100. {
  1101. m_vNewVal.vt = VT_I4;
  1102. m_vNewVal.lVal = iVal;
  1103. }
  1104. break;
  1105. case gdbSingle:
  1106. if(swscanf(m_cAttrs.wsDefVal, L"%f", &fVal) == 1)
  1107. {
  1108. m_vNewVal.vt = VT_R4;
  1109. m_vNewVal.fltVal = fVal;
  1110. }
  1111. break;
  1112. case gdbDouble:
  1113. if(swscanf(m_cAttrs.wsDefVal, L"%f", &fVal) == 1)
  1114. {
  1115. m_vNewVal.vt = VT_R8;
  1116. m_vNewVal.dblVal = fVal;
  1117. }
  1118. break;
  1119. case gdbDate:
  1120. if(wcsicmp(m_cAttrs.wsDefVal, L"now()") == 0)
  1121. {
  1122. m_vNewVal.vt = VT_DATE;
  1123. GetLocalTime(&sTime);
  1124. SystemTimeToVariantTime(&sTime, &m_vNewVal.date);
  1125. }
  1126. break;
  1127. default:
  1128. if(wcschr(m_cAttrs.wsDefVal, '(') == NULL)
  1129. {
  1130. m_vNewVal.vt = VT_BSTR;
  1131. m_vNewVal.bstrVal = SysAllocString(m_cAttrs.wsDefVal);
  1132. }
  1133. // else the default value is most likely a function, which result we don't know
  1134. }
  1135. }
  1136. return;
  1137. }
  1138. void GField::SetBuffer(VARIANT *pBuffer)
  1139. {
  1140. m_pvBuffer = pBuffer;
  1141. return;
  1142. }
  1143. void GField::CancelNewVal()
  1144. {
  1145. VariantClear(&m_vNewVal);
  1146. m_vNewVal.vt = VT_EMPTY;
  1147. return;
  1148. }
  1149. bool GField::Modified()
  1150. {
  1151. return(!m_sNewIdCmd && (m_vNewVal.vt > VT_EMPTY));
  1152. }
  1153. bool GField::HasSeq()
  1154. {
  1155. return(m_sNewIdCmd);
  1156. }
  1157. VARIANT GField::GetModVal()
  1158. {
  1159. if(m_vNewVal.vt > VT_EMPTY) return(m_vNewVal);
  1160. if(m_pvBuffer) return(*m_pvBuffer);
  1161. return(m_vNewVal);
  1162. }
  1163. void GField::UpdateVal(VARIANT *pvVal)
  1164. {
  1165. VariantClear(&m_vNewVal);
  1166. m_vNewVal.vt = VT_EMPTY;
  1167. m_pvBuffer = pvVal;
  1168. return;
  1169. }
  1170. bool GField::GetRequired()
  1171. {
  1172. return(m_cAttrs.bRequired);
  1173. }
  1174. Oid GField::GetTypeOid()
  1175. {
  1176. return(m_cAttrs.lTypeOid);
  1177. }
  1178. LPSTR GField::GetSchemaNamePtr()
  1179. {
  1180. return(m_cAttrs.sOrigSchema);
  1181. }
  1182. LPSTR GField::GetTableNamePtr()
  1183. {
  1184. return(m_cAttrs.sOrigTable);
  1185. }
  1186. void GField::FillGeomInfo(PGeomInfo pgInfo)
  1187. {
  1188. pgInfo->iGdoType = m_cAttrs.iType;
  1189. pgInfo->lSubType = m_cAttrs.lSubType;
  1190. pgInfo->iGeomDim = m_cAttrs.iGeomDim;
  1191. pgInfo->ulSrid = m_cAttrs.ulSrid;
  1192. pgInfo->iPsgType = m_cAttrs.iPsgType;
  1193. return;
  1194. }
  1195. void GField::ChangeCSGuid(LPWSTR wsOldCSGuid, LPWSTR wsNewCSGuid)
  1196. {
  1197. if(wcsicmp(m_cAttrs.sCSGuid, wsOldCSGuid) == 0)
  1198. wcscpy(m_cAttrs.sCSGuid, wsNewCSGuid);
  1199. return;
  1200. }
  1201. void GField::SetSpecField(GField *pSpecFld, int iSpecFld)
  1202. {
  1203. #if DBGLEVEL > 2
  1204. char buf[64];
  1205. WriteLogFile("GField::SetSpecField\r\n", true);
  1206. WriteLogFile(" Field name: ", false);
  1207. WriteLogFile(m_wsName, false);
  1208. sprintf(buf, "\r\n SpecType: %d\r\n", iSpecFld, false);
  1209. WriteLogFile(buf, false);
  1210. #endif // DBGLEVEL
  1211. m_iSpecField = iSpecFld; // 1 - has INGR field, 2 - has Native field;
  1212. m_pSpecField = pSpecFld;
  1213. m_pSpecField->AddRef();
  1214. return;
  1215. }
  1216. void GField::SetSpecFldAttrs(unsigned long ulSrid, long iGDOGeomType,
  1217. LPSTR sSchema, LPSTR sTable)
  1218. {
  1219. #if DBGLEVEL > 2
  1220. char buf[64];
  1221. WriteLogFile("GField::SetSpecFldAttrs\r\n", true);
  1222. WriteLogFile(" Field name: ", false);
  1223. WriteLogFile(m_wsName, false);
  1224. sprintf(buf, "\r\n GeomType: %d\r\n", iGDOGeomType);
  1225. WriteLogFile(buf, false);
  1226. #endif // DBGLEVEL
  1227. m_cAttrs.sOrigSchema = sSchema;
  1228. m_cAttrs.sOrigTable = sTable;
  1229. m_cAttrs.iGeomDim = 2;
  1230. m_cAttrs.ulSrid = ulSrid;
  1231. if(iGDOGeomType == 4) // raster
  1232. {
  1233. m_cAttrs.iType = 32;
  1234. m_cAttrs.lSubType = 2;
  1235. m_cAttrs.iPsgType = 3;
  1236. }
  1237. else if(iGDOGeomType == 5) // text
  1238. {
  1239. m_cAttrs.iType = 32;
  1240. m_cAttrs.lSubType = 10;
  1241. m_cAttrs.iPsgType = 1;
  1242. }
  1243. else
  1244. {
  1245. m_cAttrs.iType = 11;
  1246. m_cAttrs.lSubType = 0;
  1247. m_cAttrs.iPsgType = 0;
  1248. m_cAttrs.iGeomDim = 0;
  1249. }
  1250. return;
  1251. }
  1252. bool GField::IsThisField(LPSTR sSchema, LPSTR sTable, LPSTR sName)
  1253. {
  1254. bool bRes = (strcmp(sTable, m_cAttrs.sOrigTable) == 0) &&
  1255. (strcmp(sName, m_sOrigName) == 0);
  1256. if(sSchema)
  1257. {
  1258. if(m_cAttrs.sOrigSchema)
  1259. bRes &= (strcmp(sSchema, m_cAttrs.sOrigSchema) == 0);
  1260. else bRes = false;
  1261. }
  1262. else bRes &= (m_cAttrs.sOrigSchema == NULL);
  1263. return(bRes);
  1264. }
  1265. // GFields
  1266. GFields::GFields(IUnknown *pUnkOuter, ITypeLib *ALib) :
  1267. _IGDynaCollection(true, pUnkOuter, ALib, 23)
  1268. {
  1269. #if DBGLEVEL > 1
  1270. WriteLogFile("GFields::GFields-1\r\n", true);
  1271. #endif // DBGLEVEL
  1272. m_pConnStruct = NULL;
  1273. m_pTblDefs = NULL;
  1274. m_iCtidCol = -1;
  1275. m_bIsMetatable = false;
  1276. m_iSpecFields = 0;
  1277. m_piListIndex = NULL;
  1278. }
  1279. GFields::GFields(IUnknown *pUnkOuter, ITypeLib *ALib, int iIndex) :
  1280. _IGDynaCollection(true, pUnkOuter, ALib, iIndex)
  1281. {
  1282. #if DBGLEVEL > 1
  1283. WriteLogFile("GFields::GFields-2\r\n", true);
  1284. #endif // DBGLEVEL
  1285. m_pConnStruct = NULL;
  1286. m_pTblDefs = NULL;
  1287. m_iCtidCol = -1;
  1288. m_bIsMetatable = false;
  1289. m_iSpecFields = 0;
  1290. m_piListIndex = NULL;
  1291. }
  1292. GFields::~GFields()
  1293. {
  1294. #if DBGLEVEL > 1
  1295. WriteLogFile("GFields::~GFields\r\n", true);
  1296. #endif // DBGLEVEL
  1297. if(m_piListIndex) free(m_piListIndex);
  1298. }
  1299. HRESULT GFields::QueryInterface(REFIID iid, void **ppvObject)
  1300. {
  1301. #if DBGLEVEL > 2
  1302. WriteLogFile("GFields::QueryInterface\r\n", true);
  1303. #endif // DBGLEVEL
  1304. HRESULT hres = _IGDynaCollection::QueryInterface(iid, ppvObject);
  1305. if(hres != S_OK)
  1306. {
  1307. if(IsEqualIID(iid, DIID_GFields))
  1308. {
  1309. hres = S_OK;
  1310. *ppvObject = this;
  1311. ((IUnknown*)*ppvObject)->AddRef();
  1312. }
  1313. else
  1314. {
  1315. hres = E_NOINTERFACE;
  1316. #if DBGLEVEL > 2
  1317. char buf[128];
  1318. FormatGuid(buf, "Unknown Interface: ", "\r\n", iid);
  1319. WriteLogFile(buf, true);
  1320. #endif // DBGLEVEL
  1321. }
  1322. }
  1323. return(hres);
  1324. }
  1325. ULONG GFields::Release()
  1326. {
  1327. ULONG lRest;
  1328. IUnknown *pUnkOuter = m_pUnkOuter;
  1329. if(pUnkOuter) lRest = pUnkOuter->Release();
  1330. else lRest = --m_lRefCount;
  1331. #if DBGLEVEL > 2
  1332. char buf[64];
  1333. if(pUnkOuter) sprintf(buf, "GFields::Release (aggregated) - %d\r\n", lRest);
  1334. else sprintf(buf, "GFields::Release - %d\r\n", lRest);
  1335. WriteLogFile(buf, true);
  1336. #endif // DBGLEVEL
  1337. if(pUnkOuter || (lRest > 0)) return lRest;
  1338. delete(this);
  1339. return(0);
  1340. }
  1341. HRESULT GFields::get_Count(short *Count)
  1342. {
  1343. #if DBGLEVEL > 0
  1344. WriteLogFile("_IGCollection::get_Count\r\n", true);
  1345. #endif // DBGLEVEL
  1346. if(!Count) return(E_POINTER);
  1347. *Count = m_iDataLen - m_iSpecFields;
  1348. return(S_OK);
  1349. }
  1350. HRESULT GFields::_NewEnum(IUnknown * *Enum)
  1351. {
  1352. #if DBGLEVEL > 0
  1353. WriteLogFile("GFields::_NewEnum\r\n", true);
  1354. #endif // DBGLEVEL
  1355. if(!Enum) return(E_POINTER);
  1356. if(*Enum) (*Enum)->Release();
  1357. *Enum = new CEnumGDO(0, m_iDataLen - m_iSpecFields, m_pData);
  1358. (*Enum)->AddRef();
  1359. return(S_OK);
  1360. }
  1361. HRESULT GFields::Append(IDispatch * Object)
  1362. {
  1363. #if DBGLEVEL > 0
  1364. WriteLogFile("GFields::Append\r\n", true);
  1365. #endif // DBGLEVEL
  1366. return(E_NOTIMPL);
  1367. }
  1368. HRESULT GFields::Delete(BSTR Name)
  1369. {
  1370. #if DBGLEVEL > 0
  1371. WriteLogFile("GFields::Delete\r\n", true);
  1372. #endif // DBGLEVEL
  1373. return(E_NOTIMPL);
  1374. }
  1375. HRESULT GFields::get_Item(VARIANT index, GField* *ppGField)
  1376. {
  1377. ValidateVariant(&index);
  1378. #if DBGLEVEL > 0
  1379. WriteLogFile("GFields::get_Item\r\n", true);
  1380. WriteVariantToLogFile(L" index: ", index);
  1381. #endif // DBGLEVEL
  1382. if(!ppGField) return(E_POINTER);
  1383. // a "get_CollectionItem" method is probably an exception from the COM
  1384. // standards - it can accept uninitialized return values, similary as
  1385. // QueryInterface method
  1386. //if(*ppGField) (*ppGField)->Release();
  1387. *ppGField = NULL;
  1388. GField *pFld = NULL;
  1389. if(index.vt == VT_BSTR) pFld = FindByWName(index.bstrVal);
  1390. else if(index.vt == (VT_BYREF | VT_BSTR))
  1391. pFld = FindByWName(*index.pbstrVal);
  1392. else
  1393. {
  1394. int i = VarToInt(index);
  1395. if(m_piListIndex) i = m_piListIndex[i];
  1396. pFld = (GField*)_IGCollection::GetItem(i);
  1397. }
  1398. if(!pFld) return(ResultFromScode(S_FALSE));
  1399. pFld->AddRef();
  1400. *ppGField = pFld;
  1401. return(S_OK);
  1402. }
  1403. void GFields::ClearAll()
  1404. {
  1405. _IGCollection::ClearAll();
  1406. return;
  1407. }
  1408. void GFields::BuildFromConn(PConnStruct pConnStruct, PGresult *pRes,
  1409. GTableDefs *pGTbls, int iCtidCol, Oid *plTypes, int iSpecType,
  1410. bool bIsMetatable)
  1411. {
  1412. m_pConnStruct = pConnStruct;
  1413. m_pTblDefs = pGTbls;
  1414. m_iCtidCol = iCtidCol;
  1415. m_bIsMetatable = bIsMetatable;
  1416. GField *pFld;
  1417. int ncols = PQnfields(pRes);
  1418. for(int i = 0; i < ncols; i++)
  1419. {
  1420. if(i != m_iCtidCol)
  1421. {
  1422. pFld = new GField(NULL, m_pTypeLib);
  1423. pFld->SetAttrs(pConnStruct, pRes, i, plTypes[i], m_iCtidCol > -1,
  1424. iSpecType, bIsMetatable);
  1425. _IGCollection::AddItem(pFld);
  1426. }
  1427. }
  1428. return;
  1429. }
  1430. GField* FindBaseField(GField *pSpecFld, int iNameLen, int iSpecPos, int iFields,
  1431. GField **ppFields)
  1432. {
  1433. bool bFound = false;
  1434. int i = 0;
  1435. GField *pFld = NULL;
  1436. LPSTR sName = pSpecFld->GetOrigNamePtr();
  1437. while(!bFound && (i < iFields))
  1438. {
  1439. if(i != iSpecPos)
  1440. {
  1441. pFld = ppFields[i++];
  1442. bFound = (strncmp(sName, pFld->GetOrigNamePtr(), iNameLen) == 0);
  1443. }
  1444. else i++;
  1445. }
  1446. return(bFound ? pFld : NULL);
  1447. }
  1448. void GFields::UpdateFields(int *piGeomDims)
  1449. {
  1450. bool bBuildIndex = false;
  1451. CGeomInfo gInfo;
  1452. GField *pFld;
  1453. for(int i = 0; i < m_iDataLen; i++)
  1454. {
  1455. pFld = (GField*)m_pData[i];
  1456. piGeomDims[i] = pFld->UpdateField(m_pTblDefs);
  1457. pFld->FillGeomInfo(&gInfo);
  1458. bBuildIndex |= (gInfo.iGdoType > 31);
  1459. }
  1460. if(bBuildIndex)
  1461. {
  1462. m_piListIndex = (int*)malloc(m_iDataLen*sizeof(int));
  1463. int i = 0;
  1464. int j = 0;
  1465. int iSpecFld, iLen;
  1466. LPSTR sName;
  1467. GField *pBaseFld;
  1468. while(i < m_iDataLen)
  1469. {
  1470. pBaseFld = NULL;
  1471. iSpecFld = 0;
  1472. m_piListIndex[i] = -1;
  1473. m_piListIndex[j] = i;
  1474. pFld = (GField*)m_pData[i++];
  1475. sName = pFld->GetOrigNamePtr();
  1476. iLen = strlen(sName);
  1477. if(iLen > 4)
  1478. {
  1479. iLen -= 4;
  1480. if(strcmp(&sName[iLen], "_igr") == 0) iSpecFld = 1;
  1481. else if(strcmp(&sName[iLen], "_nat") == 0) iSpecFld = 2;
  1482. }
  1483. if(iSpecFld > 0)
  1484. pBaseFld = FindBaseField(pFld, iLen, i - 1, m_iDataLen,
  1485. (GField**)m_pData);
  1486. if(pBaseFld)
  1487. {
  1488. if(iSpecFld == 1) piGeomDims[i - 1] = 0;
  1489. else piGeomDims[i - 1] = 2;
  1490. pBaseFld->SetSpecField(pFld, iSpecFld);
  1491. pBaseFld->FillGeomInfo(&gInfo);
  1492. pFld->SetSpecFldAttrs(gInfo.ulSrid, gInfo.lSubType,
  1493. pBaseFld->GetSchemaNamePtr(), pBaseFld->GetTableNamePtr());
  1494. m_iSpecFields++;
  1495. }
  1496. else j++;
  1497. }
  1498. }
  1499. return;
  1500. }
  1501. /*GField* GFields::FindKeyField()
  1502. {
  1503. GField *pFld;
  1504. bool bFound = false;
  1505. int i = 0;
  1506. while(!bFound && (i < GetCount()))
  1507. {
  1508. pFld = (GField*)GetItem(i++);
  1509. bFound = pFld->IsKey();
  1510. }
  1511. return(bFound ? pFld : NULL);
  1512. }*/
  1513. /*GField* GFields::FindByName(LPSTR sVal)
  1514. {
  1515. bool bFound = false;
  1516. GField *pFld = NULL;
  1517. int i = 0;
  1518. while(!bFound && (i < _IGCollection::GetCount()))
  1519. {
  1520. pFld = (GField*)m_pData[i++];
  1521. bFound = (stricmp(pFld->GetNamePtr(), sVal) == 0);
  1522. }
  1523. return(bFound ? pFld : NULL);
  1524. }*/
  1525. GField* GFields::FindByWName(BSTR bsVal)
  1526. {
  1527. bool bFound = false;
  1528. GField *pFld = NULL;
  1529. int i = 0;
  1530. if(m_bIsMetatable)
  1531. {
  1532. while(!bFound && (i < m_iDataLen))
  1533. {
  1534. pFld = (GField*)m_pData[i++];
  1535. bFound = (wcsicmp(pFld->GetNamePtr(), bsVal) == 0);
  1536. }
  1537. }
  1538. else
  1539. {
  1540. while(!bFound && (i < m_iDataLen))
  1541. {
  1542. pFld = (GField*)m_pData[i++];
  1543. bFound = (wcscmp(pFld->GetNamePtr(), bsVal) == 0);
  1544. }
  1545. }
  1546. return(bFound ? pFld : NULL);
  1547. }
  1548. void GFields::SetNewRow()
  1549. {
  1550. GField *pFld;
  1551. for(int i = 0; i < m_iDataLen; i++)
  1552. {
  1553. pFld = (GField*)GetItem(i);
  1554. pFld->SetNewVal();
  1555. }
  1556. return;
  1557. }
  1558. void GFields::SetBuffer(VARIANT *pBuffer)
  1559. {
  1560. GField *pFld;
  1561. if(pBuffer)
  1562. {
  1563. for(int i = 0; i < m_iDataLen; i++)
  1564. {
  1565. pFld = (GField*)GetItem(i);
  1566. pFld->SetBuffer(&pBuffer[i]);
  1567. }
  1568. }
  1569. else
  1570. {
  1571. for(int i = 0; i < m_iDataLen; i++)
  1572. {
  1573. pFld = (GField*)GetItem(i);
  1574. pFld->SetBuffer(NULL);
  1575. }
  1576. }
  1577. return;
  1578. }
  1579. void GFields::CancelNewRow()
  1580. {
  1581. GField *pFld;
  1582. for(int i = 0; i < m_iDataLen; i++)
  1583. {
  1584. pFld = (GField*)GetItem(i);
  1585. pFld->CancelNewVal();
  1586. }
  1587. return;
  1588. }
  1589. void GFields::UpdateRow(VARIANT *pBuffer)
  1590. {
  1591. GField *pFld;
  1592. for(int i = 0; i < m_iDataLen; i++)
  1593. {
  1594. pFld = (GField*)GetItem(i);
  1595. pFld->UpdateVal(&pBuffer[i]);
  1596. }
  1597. return;
  1598. }
  1599. GTableDefs* GFields::GetTableDefsPtr()
  1600. {
  1601. return(m_pTblDefs);
  1602. }
  1603. void GFields::BroadcastCSChange(LPWSTR wsOldCSGuid, LPWSTR wsNewCSGuid)
  1604. {
  1605. GField *pFld;
  1606. for(int i = 0; i < m_iDataLen; i++)
  1607. {
  1608. pFld = (GField*)GetItem(i);
  1609. pFld->ChangeCSGuid(wsOldCSGuid, wsNewCSGuid);
  1610. }
  1611. return;
  1612. }
  1613. int GFields::HasField(LPSTR sSchema, LPSTR sTable, LPSTR sName)
  1614. {
  1615. GField *pFld;
  1616. bool bFound = false;
  1617. int i = 0;
  1618. while(!bFound && (i < m_iDataLen))
  1619. {
  1620. pFld = (GField*)GetItem(i++);
  1621. bFound = pFld->IsThisField(sSchema, sTable, sName);
  1622. }
  1623. return(bFound ? i - 1 : -1);
  1624. }
  1625. #if DBGLEVEL > 0
  1626. GRecordset* GFields::GetParentRecordset()
  1627. {
  1628. return (GRecordset*)m_pUnkOuter;
  1629. }
  1630. #endif
  1631. // GFeatureClass
  1632. GFeatureClassExt::GFeatureClassExt(IUnknown *pUnkOuter, ITypeLib *ALib) :
  1633. CCOMDispatch(pUnkOuter, ALib, 4)
  1634. {
  1635. #if DBGLEVEL > 2
  1636. WriteLogFile("GFeatureClassExt::GFeatureClassExt-1\r\n", true);
  1637. #endif // DBGLEVEL
  1638. m_wsTblName = NULL;
  1639. }
  1640. GFeatureClassExt::GFeatureClassExt(IUnknown *pUnkOuter, ITypeLib *ALib,
  1641. int iIndex) : CCOMDispatch(pUnkOuter, ALib, iIndex)
  1642. {
  1643. #if DBGLEVEL > 2
  1644. WriteLogFile("GFeatureClassExt::GFeatureClassExt-2\r\n", true);
  1645. #endif // DBGLEVEL
  1646. m_wsTblName = NULL;
  1647. }
  1648. GFeatureClassExt::~GFeatureClassExt()
  1649. {
  1650. #if DBGLEVEL > 2
  1651. WriteLogFile("GFeatureClassExt::~GFeatureClassExt\r\n", true);
  1652. #endif // DBGLEVEL
  1653. if(m_wsTblName) free(m_wsTblName);
  1654. }
  1655. HRESULT GFeatureClassExt::QueryInterface(REFIID iid, void **ppvObject)
  1656. {
  1657. #if DBGLEVEL > 2
  1658. WriteLogFile("GFeatureClassExt::QueryInterface\r\n", true);
  1659. #endif // DBGLEVEL
  1660. HRESULT hres = CCOMDispatch::QueryInterface(iid, ppvObject);
  1661. if(hres != S_OK)
  1662. {
  1663. if(IsEqualIID(iid, DIID__GFeatureClass))
  1664. {
  1665. hres = S_OK;
  1666. *ppvObject = this;
  1667. ((IUnknown*)*ppvObject)->AddRef();
  1668. }
  1669. else
  1670. {
  1671. hres = E_NOINTERFACE;
  1672. #if DBGLEVEL > 2
  1673. char buf[128];
  1674. FormatGuid(buf, "Unknown Interface: ", "\r\n", iid);
  1675. WriteLogFile(buf, true);
  1676. #endif // DBGLEVEL
  1677. }
  1678. }
  1679. return(hres);
  1680. }
  1681. ULONG GFeatureClassExt::Release()
  1682. {
  1683. ULONG lRest;
  1684. IUnknown *pUnkOuter = m_pUnkOuter;
  1685. if(pUnkOuter) lRest = pUnkOuter->Release();
  1686. else lRest = --m_lRefCount;
  1687. #if DBGLEVEL > 2
  1688. char buf[64];
  1689. if(pUnkOuter) sprintf(buf, "GFeatureClassExt::Release (aggregated) - %d\r\n", lRest);
  1690. else sprintf(buf, "GFeatureClassExt::Release - %d\r\n", lRest);
  1691. WriteLogFile(buf, true);
  1692. #endif // DBGLEVEL
  1693. if(pUnkOuter || (lRest > 0)) return lRest;
  1694. delete(this);
  1695. return(0);
  1696. }
  1697. HRESULT GFeatureClassExt::get_AssociationRoleClasses(
  1698. _GAssociationRoleClasses* *objClasses)
  1699. {
  1700. #if DBGLEVEL > 1
  1701. WriteLogFile("GFeatureClassExt::get_AssociationRoleClasses\r\n", true);
  1702. #endif // DBGLEVEL
  1703. return(E_NOTIMPL);
  1704. }
  1705. HRESULT GFeatureClassExt::get_AttributeClasses(_GAttributeClasses* *objClasses)
  1706. {
  1707. #if DBGLEVEL > 1
  1708. WriteLogFile("GFeatureClassExt::get_AttributeClasses\r\n", true);
  1709. #endif // DBGLEVEL
  1710. return(E_NOTIMPL);
  1711. }
  1712. HRESULT GFeatureClassExt::get_KeyAttributeNames(VARIANT *pvarKeyAttributeNames)
  1713. {
  1714. #if DBGLEVEL > 1
  1715. WriteLogFile("GFeatureClassExt::get_KeyAttributeNames\r\n", true);
  1716. #endif // DBGLEVEL
  1717. return(E_NOTIMPL);
  1718. }
  1719. HRESULT GFeatureClassExt::get_Name(BSTR *strVal)
  1720. {
  1721. #if DBGLEVEL > 1
  1722. WriteLogFile("GFeatureClassExt::get_Name\r\n", true);
  1723. #endif // DBGLEVEL
  1724. if(!strVal) return(E_POINTER);
  1725. if(!m_wsTblName) return(E_NOTIMPL);
  1726. *strVal = SysAllocString(m_wsTblName);
  1727. return(S_OK);
  1728. }
  1729. HRESULT GFeatureClassExt::get_OperationClasses(_GOperationClasses* *objClasses)
  1730. {
  1731. #if DBGLEVEL > 1
  1732. WriteLogFile("GFeatureClassExt::get_OperationClasses\r\n", true);
  1733. #endif // DBGLEVEL
  1734. return(E_NOTIMPL);
  1735. }
  1736. HRESULT GFeatureClassExt::get_Parent(IDispatch * *objParent)
  1737. {
  1738. #if DBGLEVEL > 1
  1739. WriteLogFile("GFeatureClassExt::get_Parent\r\n", true);
  1740. #endif // DBGLEVEL
  1741. return(E_NOTIMPL);
  1742. }
  1743. HRESULT GFeatureClassExt::get_RuleClasses(_GRuleClasses* *objClasses)
  1744. {
  1745. #if DBGLEVEL > 1
  1746. WriteLogFile("GFeatureClassExt::get_RuleClasses\r\n", true);
  1747. #endif // DBGLEVEL
  1748. return(E_NOTIMPL);
  1749. }
  1750. HRESULT GFeatureClassExt::GetFeature(GRecordset* objSelection,
  1751. _GFeature* *objIFeature)
  1752. {
  1753. #if DBGLEVEL > 1
  1754. WriteLogFile("GFeatureClassExt::GetFeature\r\n", true);
  1755. #endif // DBGLEVEL
  1756. return(E_NOTIMPL);
  1757. }
  1758. HRESULT GFeatureClassExt::OpenSelection(BSTR bstrFilter, VARIANT vType,
  1759. VARIANT vOptions, VARIANT vSpatialOperator, VARIANT vSpatialGeometryFilter,
  1760. VARIANT vGeometryFieldName, GRecordset* *ppSelection)
  1761. {
  1762. #if DBGLEVEL > 1
  1763. WriteLogFile("GFeatureClassExt::OpenSelection\r\n", true);
  1764. #endif // DBGLEVEL
  1765. return(E_NOTIMPL);
  1766. }
  1767. void GFeatureClassExt::SetTableName(LPWSTR wsTblName)
  1768. {
  1769. if(m_wsTblName) free(m_wsTblName);
  1770. int ilen = wcslen(wsTblName);
  1771. m_wsTblName = (LPWSTR)malloc((ilen + 1)*sizeof(wchar_t));
  1772. wcscpy(m_wsTblName, wsTblName);
  1773. return;
  1774. }
  1775. // GRecordset
  1776. GRecordset::GRecordset(GRecordsets *pParent, ITypeLib *ALib) : CCOMDispatch(NULL, ALib, 26)
  1777. {
  1778. #if DBGLEVEL > 1
  1779. WriteLogFile("GRecordset::GRecordset-1\r\n", true);
  1780. #endif // DBGLEVEL
  1781. m_lRefCount++;
  1782. m_pParent = pParent;
  1783. m_sName[0] = 0;
  1784. VariantInit(&m_vType);
  1785. VariantInit(&m_voptions);
  1786. VariantInit(&m_vSpatialOp);
  1787. VariantInit(&m_vSpatialGeometryFilter);
  1788. VariantInit(&m_vGeometryFieldName);
  1789. m_pFields = new GFields(this, m_pTypeLib);
  1790. m_pConnStruct = NULL;
  1791. m_iOpenStatus = 0;
  1792. m_iSpecType = 0;
  1793. m_iEditMode = 0;
  1794. m_sSelect[0] = 0;
  1795. m_plTypes = NULL;
  1796. m_vBuffer.iCtidLen = 0;
  1797. m_lBufSize = 0;
  1798. m_lRecordCount = 0;
  1799. m_lCurPos = -1;
  1800. m_lModPos = 1;
  1801. m_iCtidCol = -1;
  1802. m_pPGres = NULL;
  1803. m_bBOF = false;
  1804. m_bEOF = false;
  1805. m_pTblDef = NULL;
  1806. m_lTblOid = 0;
  1807. m_piGeomDims = NULL;
  1808. m_pFCExt = NULL;
  1809. m_lInserted = 0;
  1810. }
  1811. GRecordset::GRecordset(GRecordsets *pParent, ITypeLib *ALib, int iIndex) :
  1812. CCOMDispatch(NULL, ALib, iIndex)
  1813. {
  1814. #if DBGLEVEL > 1
  1815. WriteLogFile("GRecordset::GRecordset-2\r\n", true);
  1816. #endif // DBGLEVEL
  1817. m_lRefCount++;
  1818. m_pParent = pParent;
  1819. m_sName[0] = 0;
  1820. VariantInit(&m_vType);
  1821. VariantInit(&m_voptions);
  1822. VariantInit(&m_vSpatialOp);
  1823. VariantInit(&m_vSpatialGeometryFilter);
  1824. VariantInit(&m_vGeometryFieldName);
  1825. m_pFields = new GFields(this, m_pTypeLib);
  1826. m_iOpenStatus = 0;
  1827. m_pConnStruct = NULL;
  1828. m_iSpecType = 0;
  1829. m_iEditMode = 0;
  1830. m_sSelect[0] = 0;
  1831. m_plTypes = NULL;
  1832. m_vBuffer.iCtidLen = 0;
  1833. m_lBufSize = 0;
  1834. m_lRecordCount = 0;
  1835. m_lCurPos = -1;
  1836. m_lModPos = -1;
  1837. m_iCtidCol = -1;
  1838. m_pPGres = NULL;
  1839. m_bBOF = false;
  1840. m_bEOF = false;
  1841. m_pTblDef = NULL;
  1842. m_lTblOid = 0;
  1843. m_piGeomDims = NULL;
  1844. m_pFCExt = NULL;
  1845. m_lInserted = 0;
  1846. }
  1847. GRecordset::~GRecordset()
  1848. {
  1849. #if DBGLEVEL > 1
  1850. WriteLogFile("GRecordset::~GRecordset\r\n ", true);
  1851. WriteLogFile(m_sName, false);
  1852. WriteLogFile("\r\n", false);
  1853. #endif // DBGLEVEL
  1854. if(m_iOpenStatus > 0)
  1855. {
  1856. bool bTrans = (m_pConnStruct->iSysTrans | m_pConnStruct->iGdoTrans);
  1857. char sCmd[64];
  1858. sprintf(sCmd, "deallocate %s", m_sStmtName);
  1859. SetSP(m_pConnStruct->pConn, bTrans);
  1860. PGresult *res = PQexec(m_pConnStruct->pConn, sCmd);
  1861. if(PQresultStatus(res) != PGRES_COMMAND_OK)
  1862. {
  1863. PQclear(res);
  1864. RollbackSP(m_pConnStruct->pConn, bTrans);
  1865. }
  1866. else
  1867. {
  1868. PQclear(res);
  1869. ReleaseSP(m_pConnStruct->pConn, bTrans);
  1870. }
  1871. for(int i = 0; i < m_iParams; i++)
  1872. {
  1873. if(m_psParams[i]) free(m_psParams[i]);
  1874. }
  1875. free(m_psParams);
  1876. free(m_piParType);
  1877. free(m_piParLenght);
  1878. PQclear(m_pPGres);
  1879. for(int i = 0; i < m_iRowSize; i++)
  1880. {
  1881. VariantClear(&m_vBuffer.pvData[i]);
  1882. }
  1883. free(m_vBuffer.pvData);
  1884. }
  1885. if(m_pFCExt) m_pFCExt->SetOuter(NULL);
  1886. m_pTblDef = NULL;
  1887. if(m_piGeomDims) free(m_piGeomDims);
  1888. if(m_plTypes) free(m_plTypes);
  1889. m_pFields->SetOuter(NULL);
  1890. VariantClear(&m_vType);
  1891. VariantClear(&m_voptions);
  1892. VariantClear(&m_vSpatialOp);
  1893. VariantClear(&m_vSpatialGeometryFilter);
  1894. VariantClear(&m_vGeometryFieldName);
  1895. if(m_pParent) m_pParent->DeletePtr(this);
  1896. }
  1897. HRESULT GRecordset::QueryInterface(REFIID iid, void **ppvObject)
  1898. {
  1899. #if DBGLEVEL > 2
  1900. WriteLogFile("GRecordset::QueryInterface\r\n", true);
  1901. #endif // DBGLEVEL
  1902. HRESULT hres = CCOMDispatch::QueryInterface(iid, ppvObject);
  1903. if(hres != S_OK)
  1904. {
  1905. if(IsEqualIID(iid, DIID_GRecordset))
  1906. {
  1907. hres = S_OK;
  1908. *ppvObject = this;
  1909. ((IUnknown*)*ppvObject)->AddRef();
  1910. }
  1911. else
  1912. {
  1913. hres = E_NOINTERFACE;
  1914. #if DBGLEVEL > 2
  1915. char buf[128];
  1916. FormatGuid(buf, "Unknown Interface: ", "\r\n", iid);
  1917. WriteLogFile(buf, true);
  1918. #endif // DBGLEVEL
  1919. }
  1920. }
  1921. return(hres);
  1922. }
  1923. ULONG GRecordset::Release()
  1924. {
  1925. // GRecordset is never aggregated
  1926. ULONG lRest = --m_lRefCount;
  1927. #if DBGLEVEL > 2
  1928. char buf[64];
  1929. sprintf(buf, "GRecordset::Release - %d\r\n", lRest);
  1930. WriteLogFile(buf, true);
  1931. #endif // DBGLEVEL
  1932. if(lRest > 0) return(lRest);
  1933. delete(this);
  1934. return(0);
  1935. }
  1936. HRESULT GRecordset::get_GFields(GFields* *ppGFields)
  1937. {
  1938. #if DBGLEVEL > 0
  1939. WriteLogFile("GRecordset::get_GFields\r\n", true);
  1940. #endif // DBGLEVEL
  1941. if(!ppGFields) return(E_POINTER);
  1942. if(*ppGFields) (*ppGFields)->Release();
  1943. m_pFields->AddRef();
  1944. *ppGFields = m_pFields;
  1945. return(S_OK);
  1946. }
  1947. HRESULT GRecordset::get_BOF(VARIANT_BOOL *pb)
  1948. {
  1949. #if DBGLEVEL > 0
  1950. WriteLogFile("GRecordset::get_BOF\r\n", true);
  1951. #if DBGLEVEL > 2
  1952. WriteLogFile(" ", false);
  1953. WriteLogFile(m_sName, false);
  1954. WriteLogFile("\r\n", false);
  1955. #endif // DBGLEVEL > 2
  1956. #endif // DBGLEVEL
  1957. if(!pb) return(E_POINTER);
  1958. if(m_bBOF) *pb = VARIANT_TRUE;
  1959. else *pb = VARIANT_FALSE;
  1960. return(S_OK);
  1961. }
  1962. HRESULT GRecordset::get_Bookmark(SAFEARRAY **ppsach)
  1963. {
  1964. #if DBGLEVEL > 0
  1965. WriteLogFile("GRecordset::get_Bookmark\r\n", true);
  1966. #if DBGLEVEL > 2
  1967. WriteLogFile(" ", false);
  1968. WriteLogFile(m_sName, false);
  1969. WriteLogFile("\r\n", false);
  1970. #endif // DBGLEVEL
  1971. #endif // DBGLEVEL
  1972. if(!ppsach) return(E_POINTER);
  1973. if(!m_pPGres) return(S_FALSE);
  1974. // we also know situations when this throws an exception
  1975. //if(*ppsach) SafeArrayDestroy(*ppsach);
  1976. SAFEARRAYBOUND sab = {4, 0};
  1977. *ppsach = SafeArrayCreate(VT_UI1, 1, &sab);
  1978. long *plng = NULL;
  1979. SafeArrayAccessData(*ppsach, (void**)&plng);
  1980. *plng = m_lCurPos + 1;
  1981. #if DBGLEVEL > 2
  1982. char buf[64];
  1983. sprintf(buf, "get_Bookmark - %d\r\n", *plng);
  1984. WriteLogFile(buf, true);
  1985. #endif // DBGLEVEL
  1986. SafeArrayUnaccessData(*ppsach);
  1987. return(S_OK);
  1988. }
  1989. HRESULT GRecordset::set_Bookmark(SAFEARRAY **ppsach)
  1990. {
  1991. #if DBGLEVEL > 0
  1992. WriteLogFile("GRecordset::set_Bookmark\r\n", true);
  1993. #if DBGLEVEL > 2
  1994. WriteLogFile(" ", false);
  1995. WriteLogFile(m_sName, false);
  1996. WriteLogFile("\r\n", false);
  1997. #endif // DBGLEVEL
  1998. #endif // DBGLEVEL
  1999. if(!m_pPGres)
  2000. {
  2001. #if DBGLEVEL > 2
  2002. WriteLogFile("Invalid recordset buffer\r\n", true);
  2003. #endif // DBGLEVEL
  2004. return(S_FALSE);
  2005. }
  2006. long lNewPos;
  2007. long *plng = NULL;
  2008. SafeArrayAccessData(*ppsach, (void**)&plng);
  2009. lNewPos = *plng;
  2010. SafeArrayUnaccessData(*ppsach);
  2011. #if DBGLEVEL > 2
  2012. char buf[64];
  2013. sprintf(buf, "set_Bookmark - %d\r\n", lNewPos);
  2014. WriteLogFile(buf, true);
  2015. #endif // DBGLEVEL
  2016. if(lNewPos > 0)
  2017. {
  2018. m_lCurPos = lNewPos - 1;
  2019. SyncBuf();
  2020. m_pFields->SetBuffer(m_vBuffer.pvData);
  2021. return(S_OK);
  2022. }
  2023. m_lCurPos = -1;
  2024. m_pFields->SetBuffer(NULL);
  2025. return(S_FALSE);
  2026. }
  2027. HRESULT GRecordset::get_Bookmarkable(VARIANT_BOOL *pb)
  2028. {
  2029. #if DBGLEVEL > 0
  2030. WriteLogFile("GRecordset::get_Bookmarkable\r\n", true);
  2031. #endif // DBGLEVEL
  2032. if(!pb) return(E_POINTER);
  2033. *pb = VARIANT_TRUE;
  2034. return(S_OK);
  2035. }
  2036. HRESULT GRecordset::get_EditMode(short *pi)
  2037. {
  2038. #if DBGLEVEL > 0
  2039. WriteLogFile("GRecordset::get_EditMode\r\n", true);
  2040. #endif // DBGLEVEL
  2041. if(!pi) return(E_POINTER);
  2042. *pi = m_iEditMode;
  2043. return(S_OK);
  2044. }
  2045. HRESULT GRecordset::get_EOF(VARIANT_BOOL *pb)
  2046. {
  2047. #if DBGLEVEL > 0
  2048. WriteLogFile("GRecordset::get_EOF\r\n", true);
  2049. #if DBGLEVEL > 2
  2050. WriteLogFile(" ", false);
  2051. WriteLogFile(m_sName, false);
  2052. WriteLogFile("\r\n", false);
  2053. #endif // DBGLEVEL > 2
  2054. #endif // DBGLEVEL
  2055. if(!pb) return(E_POINTER);
  2056. if(m_bEOF) *pb = VARIANT_TRUE;
  2057. else *pb = VARIANT_FALSE;
  2058. return(S_OK);
  2059. }
  2060. HRESULT GRecordset::get_LastModified(SAFEARRAY * *LastModified)
  2061. {
  2062. #if DBGLEVEL > 0
  2063. WriteLogFile("GRecordset::get_LastModified\r\n", true);
  2064. #endif // DBGLEVEL
  2065. if(!LastModified) return(E_POINTER);
  2066. if(!m_pPGres) return(S_FALSE);
  2067. if(m_lModPos < 0) return(S_FALSE);
  2068. if(*LastModified) SafeArrayDestroy(*LastModified);
  2069. SAFEARRAYBOUND sab = {4, 0};
  2070. *LastModified = SafeArrayCreate(VT_UI1, 1, &sab);
  2071. long *plng = NULL;
  2072. SafeArrayAccessData(*LastModified, (void**)&plng);
  2073. *plng = m_lModPos + 1;
  2074. #if DBGLEVEL > 2
  2075. char buf[64];
  2076. sprintf(buf, "get_LastModified - %d\r\n", *plng);
  2077. WriteLogFile(buf, true);
  2078. #endif // DBGLEVEL
  2079. SafeArrayUnaccessData(*LastModified);
  2080. return(S_OK);
  2081. }
  2082. HRESULT GRecordset::get_Name(BSTR *Name)
  2083. {
  2084. #if DBGLEVEL > 0
  2085. WriteLogFile("GRecordset::get_Name\r\n ", true);
  2086. WriteLogFile(m_sName, false);
  2087. WriteLogFile("\r\n", false);
  2088. #endif // DBGLEVEL
  2089. if(!Name) return(E_POINTER);
  2090. if(*Name) SysFreeString(*Name);
  2091. *Name = SysAllocString(m_sName);
  2092. return(S_OK);
  2093. }
  2094. HRESULT GRecordset::get_RecordCount(long *RecordCount)
  2095. {
  2096. #if DBGLEVEL > 0
  2097. WriteLogFile("GRecordset::get_RecordCount\r\n", true);
  2098. #endif // DBGLEVEL
  2099. if(!RecordCount) return(E_POINTER);
  2100. if(!m_pPGres) return(E_NOTIMPL);
  2101. *RecordCount = m_lRecordCount;
  2102. return(S_OK);
  2103. }
  2104. HRESULT GRecordset::get_Type(short *Type)
  2105. {
  2106. #if DBGLEVEL > 0
  2107. WriteLogFile("GRecordset::get_Type\r\n", true);
  2108. #endif // DBGLEVEL
  2109. if(!Type) return(E_POINTER);
  2110. *Type = VarToInt(m_vType);
  2111. return(S_OK);
  2112. }
  2113. HRESULT GRecordset::get_Updatable(VARIANT_BOOL *Updatable)
  2114. {
  2115. #if DBGLEVEL > 0
  2116. WriteLogFile("GRecordset::get_Updatable\r\n", true);
  2117. #endif // DBGLEVEL
  2118. if(!Updatable) return(E_POINTER);
  2119. if(m_bUpdatable) *Updatable = VARIANT_TRUE;
  2120. else *Updatable = VARIANT_FALSE;
  2121. return(S_OK);
  2122. }
  2123. bool GRecordset::IsCurDeleted()
  2124. {
  2125. if(m_iCtidCol < 0) return false;
  2126. if(m_lCurPos < 0) return true;
  2127. if(m_vBuffer.iCtidLen < 1) return true;
  2128. return false;
  2129. }
  2130. HRESULT GRecordset::AddNew()
  2131. {
  2132. #if DBGLEVEL > 0
  2133. WriteLogFile("GRecordset::AddNew\r\n", true);
  2134. if(m_iSpecType == 2) WriteLogFile(" GCoordSystem\r\n", false);
  2135. #if DBGLEVEL > 2
  2136. else
  2137. {
  2138. WriteLogFile(" ", false);
  2139. WriteLogFile(m_sName, false);
  2140. WriteLogFile("\r\n", false);
  2141. }
  2142. #endif // DBGLEVEL > 2
  2143. #endif // DBGLEVEL
  2144. if(!m_pPGres) return(S_FALSE);
  2145. if(!m_bUpdatable) return(S_FALSE);
  2146. m_pFields->SetNewRow();
  2147. m_iEditMode = 2;
  2148. return(S_OK);
  2149. }
  2150. HRESULT GRecordset::CancelUpdate()
  2151. {
  2152. #if DBGLEVEL > 0
  2153. WriteLogFile("GRecordset::CancelUpdate\r\n", true);
  2154. if(m_iSpecType == 2) WriteLogFile(" GCoordSystem\r\n", false);
  2155. #endif // DBGLEVEL
  2156. if(!m_bUpdatable) return(S_FALSE);
  2157. m_pFields->CancelNewRow();
  2158. return(S_OK);
  2159. }
  2160. HRESULT GRecordset::Close()
  2161. {
  2162. #if DBGLEVEL > 0
  2163. WriteLogFile("GRecordset::Close\r\n (", true);
  2164. WriteLogFile(m_sName, false);
  2165. WriteLogFile(")\r\n", false);
  2166. #endif // DBGLEVEL
  2167. if(!m_pPGres) return(E_NOTIMPL);
  2168. if(m_iOpenStatus > 0)
  2169. {
  2170. bool bTrans = (m_pConnStruct->iSysTrans | m_pConnStruct->iGdoTrans);
  2171. char sCmd[64];
  2172. sprintf(sCmd, "deallocate %s", m_sStmtName);
  2173. SetSP(m_pConnStruct->pConn, bTrans);
  2174. PGresult *res = PQexec(m_pConnStruct->pConn, sCmd);
  2175. if(PQresultStatus(res) != PGRES_COMMAND_OK)
  2176. {
  2177. PQclear(res);
  2178. RollbackSP(m_pConnStruct->pConn, bTrans);
  2179. }
  2180. else
  2181. {
  2182. PQclear(res);
  2183. ReleaseSP(m_pConnStruct->pConn, bTrans);
  2184. }
  2185. for(int i = 0; i < m_iParams; i++)
  2186. {
  2187. if(m_psParams[i]) free(m_psParams[i]);
  2188. }
  2189. free(m_psParams);
  2190. free(m_piParType);
  2191. free(m_piParLenght);
  2192. PQclear(m_pPGres);
  2193. for(int i = 0; i < m_iRowSize; i++)
  2194. {
  2195. VariantClear(&m_vBuffer.pvData[i]);
  2196. }
  2197. free(m_vBuffer.pvData);
  2198. }
  2199. m_pPGres = NULL;
  2200. m_iOpenStatus = 0;
  2201. m_lInserted = 0;
  2202. return(S_OK);
  2203. }
  2204. long WriteModLog(PConnStruct pConnStruct, Oid lTblOid, GFields *pFields,
  2205. int iMode)
  2206. {
  2207. long lRes = -1;
  2208. GTableDefs *pTDs = pFields->GetTableDefsPtr();
  2209. GTableDef *pTD = pTDs->FindByOid(lTblOid);
  2210. if(!pTD) return(lRes);
  2211. pTD->BuildFromConnection();
  2212. int nKeys = pTD->GetKeyCount();
  2213. if(nKeys < 1) return(lRes);
  2214. GTDFields *ptdFlds = pTD->GetFieldsPtr();
  2215. int nFlds = ptdFlds->GetCount();
  2216. GTDField *ptdFld;
  2217. GField *pFld;
  2218. LPSTR sTblName = WCharToDBStr(pTD->GetNamePtr(), pConnStruct->lDBEnc);
  2219. int ilen = 52 + strlen(pConnStruct->sModTables) + strlen(sTblName);
  2220. LPSTR sSql = (LPSTR)malloc(ilen*sizeof(char));
  2221. sprintf(sSql, "select modifiedtableid from %s where tablename = '%s'",
  2222. pConnStruct->sModTables, sTblName);
  2223. #if DBGLEVEL > 2
  2224. WriteMallocReport(7, ilen - 1, strlen(sSql));
  2225. #endif
  2226. bool bTrans = (pConnStruct->iSysTrans | pConnStruct->iGdoTrans);
  2227. SetSP(pConnStruct->pConn, bTrans);
  2228. //LPSTR lsCmd = GetSaveSQL(sSql, bTrans);
  2229. PGresult *res = PQexec(pConnStruct->pConn, sSql);
  2230. //free(lsCmd);
  2231. free(sSql);
  2232. if(PQresultStatus(res) != PGRES_TUPLES_OK)
  2233. {
  2234. WritePQErrorToLog("Spot 2: ", pConnStruct->pConn);
  2235. PQclear(res);
  2236. RollbackSP(pConnStruct->pConn, bTrans);
  2237. return(lRes);
  2238. }
  2239. long nrows = PQntuples(res);
  2240. char sBuf[32];
  2241. LPSTR sParams[13];
  2242. int i, j;
  2243. for(i = 0; i < 13; i++) sParams[i] = NULL;
  2244. if(nrows < 1)
  2245. {
  2246. PQclear(res);
  2247. ReleaseSP(pConnStruct->pConn, bTrans);
  2248. ilen = 64 + strlen(pConnStruct->sModTables) + nKeys*18;
  2249. sSql = (LPSTR)malloc(ilen*sizeof(char));
  2250. sprintf(sSql, "insert into %s(tablename", pConnStruct->sModTables);
  2251. for(i = 0; i < nKeys; i++)
  2252. {
  2253. sprintf(sBuf, ", keyvalue%d", i + 1);
  2254. strcat(sSql, sBuf);
  2255. }
  2256. strcat(sSql, ") values ($1");
  2257. for(i = 0; i < nKeys; i++)
  2258. {
  2259. sprintf(sBuf, ", $%d", i + 2);
  2260. strcat(sSql, sBuf);
  2261. }
  2262. strcat(sSql, ") returning modifiedtableid");
  2263. #if DBGLEVEL > 2
  2264. WriteMallocReport(8, ilen - 1, strlen(sSql));
  2265. #endif
  2266. sParams[0] = sTblName;
  2267. i = 0;
  2268. j = 1;
  2269. while((i < nFlds) && (j < nKeys + 1))
  2270. {
  2271. ptdFld = (GTDField*)ptdFlds->GetItem(i++);
  2272. if(ptdFld->IsKeyField())
  2273. {
  2274. sParams[j++] = WCharToDBStr(ptdFld->GetNamePtr(),
  2275. pConnStruct->lDBEnc);
  2276. }
  2277. }
  2278. SetSP(pConnStruct->pConn, bTrans);
  2279. res = PQexecParams(pConnStruct->pConn, sSql, nKeys + 1, NULL, sParams,
  2280. NULL, NULL, 0);
  2281. free(sSql);
  2282. for(i = 1; i < 13; i++)
  2283. {
  2284. if(sParams[i]) free(sParams[i]);
  2285. sParams[i] = NULL;
  2286. }
  2287. if(PQresultStatus(res) != PGRES_TUPLES_OK)
  2288. {
  2289. WritePQErrorToLog("Spot 3: ", pConnStruct->pConn);
  2290. PQclear(res);
  2291. RollbackSP(pConnStruct->pConn, bTrans);
  2292. return(lRes);
  2293. }
  2294. else nrows = PQntuples(res);
  2295. }
  2296. else free(sTblName);
  2297. sParams[0] = NULL;
  2298. if(nrows < 1)
  2299. {
  2300. PQclear(res);
  2301. ReleaseSP(pConnStruct->pConn, bTrans);
  2302. return(lRes);
  2303. }
  2304. Oid lType;
  2305. VARIANT vTblId;
  2306. VariantInit(&vTblId);
  2307. DescribeFieldTypes(res, 1, &lType);
  2308. GetRecord(res, 0, 1, &lType, &vTblId, pConnStruct->sDecSep);
  2309. PQclear(res);
  2310. ReleaseSP(pConnStruct->pConn, bTrans);
  2311. ilen = 100 + strlen(pConnStruct->sModLog) + nKeys*18;
  2312. sSql = (LPSTR)malloc(ilen*sizeof(char));
  2313. sprintf(sSql, "insert into %s(type, modifiedtableid", pConnStruct->sModLog);
  2314. for(int i = 0; i < nKeys; i++)
  2315. {
  2316. sprintf(sBuf, ", keyvalue%d", i + 1);
  2317. strcat(sSql, sBuf);
  2318. }
  2319. strcat(sSql, ", sessionid) values ($1, $2");
  2320. for(int i = 0; i < nKeys; i++)
  2321. {
  2322. sprintf(sBuf, ", $%d", i + 3);
  2323. strcat(sSql, sBuf);
  2324. }
  2325. sprintf(sBuf, ", $%d)", nKeys + 3);
  2326. strcat(sSql, sBuf);
  2327. strcat(sSql, " returning modificationnumber");
  2328. #if DBGLEVEL > 0
  2329. WriteLogFile("Mod log sql: ", true);
  2330. WriteLogFile(sSql, false);
  2331. WriteLogFile("\r\n", false);
  2332. WriteMallocReport(9, ilen - 1, strlen(sSql));
  2333. #endif
  2334. int piLen[13];
  2335. int piTypes[13];
  2336. CGeomInfo cgInfo = {0, 0, 0, 0, 0};
  2337. long lParams[8];
  2338. VARIANT V;
  2339. V.vt = VT_I2;
  2340. V.iVal = iMode;
  2341. piLen[0] = VarToBinaryLen(V, INT2OID, pConnStruct->lDBEnc, &cgInfo, lParams);
  2342. sParams[0] = (LPSTR)malloc((piLen[0] + 1)*sizeof(char));
  2343. VarToBinaryBuf(V, INT2OID, pConnStruct->lDBEnc, sParams[0], piLen[0],
  2344. &cgInfo, lParams, pConnStruct->iCurrDigits);
  2345. piTypes[0] = 1;
  2346. piLen[1] = VarToBinaryLen(vTblId, INT4OID, pConnStruct->lDBEnc, &cgInfo,
  2347. lParams);
  2348. sParams[1] = (LPSTR)malloc((piLen[1] + 1)*sizeof(char));
  2349. VarToBinaryBuf(vTblId, INT4OID, pConnStruct->lDBEnc, sParams[1], piLen[1],
  2350. &cgInfo, lParams, pConnStruct->iCurrDigits);
  2351. piTypes[1] = 1;
  2352. VARIANT vVal;
  2353. VariantInit(&vVal);
  2354. i = 0;
  2355. j = 2;
  2356. while((i < nFlds) && (j < nKeys + 2))
  2357. {
  2358. ptdFld = (GTDField*)ptdFlds->GetItem(i++);
  2359. if(ptdFld->IsKeyField())
  2360. {
  2361. pFld = pFields->FindByWName(ptdFld->GetNamePtr());
  2362. if(pFld)
  2363. {
  2364. pFld->get_Value(&vVal);
  2365. V.vt = VT_BSTR;
  2366. V.bstrVal = VarToBSTR(vVal);
  2367. }
  2368. else V.vt = VT_NULL;
  2369. piLen[j] = VarToBinaryLen(V, VARCHAROID, pConnStruct->lDBEnc,
  2370. &cgInfo, lParams);
  2371. sParams[j] = (LPSTR)malloc((piLen[j] + 1)*sizeof(char));
  2372. VarToBinaryBuf(V, VARCHAROID, pConnStruct->lDBEnc, sParams[j],
  2373. piLen[j], &cgInfo, lParams, pConnStruct->iCurrDigits);
  2374. piTypes[j++] = 1;
  2375. VariantClear(&V);
  2376. }
  2377. }
  2378. VariantClear(&V);
  2379. V.vt = VT_I4;
  2380. V.lVal = pConnStruct->lSessionId;
  2381. piLen[nKeys + 2] = VarToBinaryLen(V, INT4OID, pConnStruct->lDBEnc, &cgInfo,
  2382. lParams);
  2383. sParams[nKeys + 2] = (LPSTR)malloc((piLen[nKeys + 2] + 1)*sizeof(char));
  2384. VarToBinaryBuf(V, INT4OID, pConnStruct->lDBEnc, sParams[nKeys + 2],
  2385. piLen[nKeys + 2], &cgInfo, lParams, pConnStruct->iCurrDigits);
  2386. piTypes[nKeys + 2] = 1;
  2387. SetSP(pConnStruct->pConn, bTrans);
  2388. res = PQexecParams(pConnStruct->pConn, sSql, nKeys + 3, NULL, sParams,
  2389. piLen, piTypes, 0);
  2390. free(sSql);
  2391. for(i = 0; i < 13; i++)
  2392. {
  2393. if(sParams[i]) free(sParams[i]);
  2394. }
  2395. if(PQresultStatus(res) != PGRES_TUPLES_OK)
  2396. {
  2397. WritePQErrorToLog("Spot 4: ", pConnStruct->pConn);
  2398. PQclear(res);
  2399. RollbackSP(pConnStruct->pConn, bTrans);
  2400. return(lRes);
  2401. }
  2402. nrows = PQntuples(res);
  2403. if(nrows < 1)
  2404. {
  2405. PQclear(res);
  2406. ReleaseSP(pConnStruct->pConn, bTrans);
  2407. return(lRes);
  2408. }
  2409. DescribeFieldTypes(res, 1, &lType);
  2410. GetRecord(res, 0, 1, &lType, &V, pConnStruct->sDecSep);
  2411. PQclear(res);
  2412. ReleaseSP(pConnStruct->pConn, bTrans);
  2413. lRes = VarToLong(V);
  2414. return(lRes);
  2415. }
  2416. void DelFromModLog(long lModId, PConnStruct pConnStruct)
  2417. {
  2418. char buf[128];
  2419. sprintf(buf, "delete from %s where modificationnumber = %ld",
  2420. pConnStruct->sModLog, lModId);
  2421. ExecuteCommand(pConnStruct, buf);
  2422. return;
  2423. }
  2424. HRESULT GRecordset::Delete()
  2425. {
  2426. #if DBGLEVEL > 0
  2427. WriteLogFile("GRecordset::Delete\r\n", true);
  2428. if(m_iSpecType == 2) WriteLogFile(" GCoordSystem\r\n", false);
  2429. #endif // DBGLEVEL
  2430. if(!m_bUpdatable) return(S_FALSE);
  2431. if(m_pFields->GetCount() < 1) return(S_FALSE);
  2432. if(IsCurDeleted()) return S_FALSE;
  2433. if(m_lCurPos < 0) return S_FALSE;
  2434. int ilen = 36 + GetTableNameLen(m_pTblDef->GetOrigSchemaPtr(),
  2435. m_pTblDef->GetOrigNamePtr());
  2436. LPSTR sCmd = (LPSTR)malloc(ilen*sizeof(char));
  2437. strcpy(sCmd, "delete from ");
  2438. CatTableName(sCmd, m_pTblDef->GetOrigSchemaPtr(),
  2439. m_pTblDef->GetOrigNamePtr(), !m_bIsMetatable);
  2440. strcat(sCmd, " where ctid = $1");
  2441. int iParType = 1;
  2442. bool bTrans = (m_pConnStruct->iSysTrans | m_pConnStruct->iGdoTrans);
  2443. SetSP(m_pConnStruct->pConn, bTrans);
  2444. LPSTR sParams[1] = {m_vBuffer.sCtid};
  2445. //LPSTR lsCmd = GetSaveSQL(sCmd, bTrans);
  2446. PGresult *res = PQexecParams(m_pConnStruct->pConn, sCmd, 1, NULL,
  2447. sParams, &m_vBuffer.iCtidLen, &iParType, 0);
  2448. //free(lsCmd);
  2449. free(sCmd);
  2450. if(PQresultStatus(res) != PGRES_COMMAND_OK)
  2451. {
  2452. m_pErrors->HandlePQError(m_pConnStruct->pConn, m_pConnStruct->hInstance);
  2453. PQclear(res);
  2454. RollbackSP(m_pConnStruct->pConn, bTrans);
  2455. return(S_FALSE);
  2456. }
  2457. PQclear(res);
  2458. ReleaseSP(m_pConnStruct->pConn, bTrans);
  2459. GDatabase *pDB = (GDatabase*)m_pConnStruct->lSessionId;
  2460. if(pDB->GetModificationLogging())
  2461. WriteModLog(m_pConnStruct, m_lTblOid, m_pFields, 3);
  2462. //pDB->BroadcastDelRecord(m_sTblName, pRow->iCtidLen, pRow->pCtid);
  2463. m_vBuffer.iCtidLen = 0;
  2464. PQsetvalue(m_pPGres, m_lCurPos, m_iCtidCol, NULL, -1);
  2465. m_pFields->SetBuffer(NULL);
  2466. m_lRecordCount--;
  2467. if(m_lRecordCount < 1)
  2468. {
  2469. m_lCurPos = -1;
  2470. m_bBOF = true;
  2471. m_bEOF = true;
  2472. }
  2473. m_lModPos = m_lCurPos;
  2474. return(S_OK);
  2475. }
  2476. HRESULT GRecordset::Edit()
  2477. {
  2478. #if DBGLEVEL > 0
  2479. WriteLogFile("GRecordset::Edit\r\n", true);
  2480. if(m_iSpecType == 2) WriteLogFile(" GCoordSystem\r\n", false);
  2481. #if DBGLEVEL > 2
  2482. else
  2483. {
  2484. WriteLogFile(" ", false);
  2485. WriteLogFile(m_sName, false);
  2486. WriteLogFile("\r\n", false);
  2487. }
  2488. #endif // DBGLEVEL > 2
  2489. #endif // DBGLEVEL
  2490. if(!m_bUpdatable) return(S_FALSE);
  2491. if(IsCurDeleted()) return S_FALSE;
  2492. if(m_lCurPos < 0) return S_FALSE;
  2493. m_iEditMode = 1;
  2494. return(S_OK);
  2495. }
  2496. HRESULT GRecordset::GetExtension(BSTR Name, IDispatch * *ppGExtension)
  2497. {
  2498. #if DBGLEVEL > 0
  2499. WriteLogFile("GRecordset::GetExtension\r\n", true);
  2500. WriteLogFile(" Name: ", false);
  2501. WriteLogFile(Name, false);
  2502. WriteLogFile("\r\n", false);
  2503. #endif // DBGLEVEL
  2504. #if DBGLEVEL > 2
  2505. WriteLogFile(" Recordset: ", false);
  2506. WriteLogFile(m_sName, false);
  2507. WriteLogFile("\r\n", false);
  2508. #endif // DBGLEVEL
  2509. /*if(wcsicmp(Name, L"FeatureClass") == 0)
  2510. {
  2511. if(!m_pFCExt) return(E_NOTIMPL);
  2512. if(!ppGExtension) return(E_POINTER);
  2513. if(*ppGExtension) (*ppGExtension)->Release();
  2514. m_pFCExt->AddRef();
  2515. *ppGExtension = m_pFCExt;
  2516. return(S_OK);
  2517. }*/
  2518. return(E_NOTIMPL);
  2519. }
  2520. void GRecordset::SyncBuf()
  2521. {
  2522. if(m_iCtidCol < 0)
  2523. {
  2524. GetRecordBin(m_pPGres, m_lCurPos, m_iRowSize, m_plTypes, m_vBuffer.pvData,
  2525. m_pConnStruct->lDBEnc, m_pConnStruct->lGeomOid, m_piGeomDims,
  2526. m_pConnStruct->iCurrDigits);
  2527. }
  2528. else
  2529. {
  2530. GetRecordCtid(m_pPGres, m_lCurPos, m_iRowSize + 1, m_iCtidCol, m_plTypes,
  2531. m_vBuffer.pvData, m_pConnStruct->lDBEnc, m_vBuffer.sCtid,
  2532. &m_vBuffer.iCtidLen, m_pConnStruct->lGeomOid, m_piGeomDims,
  2533. m_pConnStruct->iCurrDigits);
  2534. }
  2535. return;
  2536. }
  2537. void GRecordset::SyncFields()
  2538. {
  2539. if(m_lCurPos < 0)
  2540. {
  2541. m_pFields->SetBuffer(NULL);
  2542. }
  2543. else
  2544. {
  2545. SyncBuf();
  2546. m_pFields->SetBuffer(m_vBuffer.pvData);
  2547. }
  2548. return;
  2549. }
  2550. HRESULT GRecordset::GetRows(VARIANT Rows, VARIANT *pVar)
  2551. {
  2552. #if DBGLEVEL > 0
  2553. WriteLogFile("GRecordset::GetRows\r\n", true);
  2554. #endif // DBGLEVEL
  2555. if(!m_pPGres) return(S_FALSE);
  2556. pVar->vt = VT_NULL;
  2557. int iRows = VarToInt(Rows);
  2558. if(iRows < 1) return(S_OK);
  2559. long lCurPos = m_lCurPos;
  2560. long lAvailRows = 0;
  2561. m_bBOF = false;
  2562. if(m_iCtidCol < 0)
  2563. {
  2564. lAvailRows = m_lBufSize - lCurPos;
  2565. if(lAvailRows > iRows) lAvailRows = iRows;
  2566. }
  2567. else
  2568. {
  2569. while((lAvailRows < iRows) && (lCurPos < m_lBufSize))
  2570. {
  2571. if(!PQgetisnull(m_pPGres, lCurPos, m_iCtidCol))
  2572. {
  2573. lAvailRows++;
  2574. }
  2575. lCurPos++;
  2576. }
  2577. }
  2578. if(lAvailRows < iRows) m_bEOF = true;
  2579. if(lAvailRows < 1) return(S_OK);
  2580. CBufRow cRow;
  2581. cRow.pvData = (VARIANT*)malloc(m_iRowSize*sizeof(VARIANT));
  2582. for(int i = 0; i < m_iRowSize; i++)
  2583. {
  2584. VariantInit(&cRow.pvData[i]);
  2585. }
  2586. SAFEARRAYBOUND sab[2];
  2587. sab[0].lLbound = 0;
  2588. sab[0].cElements = m_pFields->GetCount();
  2589. sab[1].lLbound = 0;
  2590. sab[1].cElements = lAvailRows;
  2591. pVar->vt = VT_ARRAY | VT_VARIANT;
  2592. pVar->parray = SafeArrayCreate(VT_VARIANT, 2, sab);
  2593. VARIANT *pvArray;
  2594. SafeArrayAccessData(pVar->parray, (void**)&pvArray);
  2595. lCurPos = m_lCurPos;
  2596. GField *pFld;
  2597. while(lAvailRows > 0)
  2598. {
  2599. if(GetRow(lCurPos++, &cRow))
  2600. {
  2601. lAvailRows--;
  2602. m_pFields->SetBuffer(cRow.pvData);
  2603. for(unsigned int j = 0; j < sab[0].cElements; j++)
  2604. {
  2605. pFld = (GField*)m_pFields->GetItem(j);
  2606. pFld->get_Value(pvArray++);
  2607. }
  2608. }
  2609. }
  2610. SafeArrayUnaccessData(pVar->parray);
  2611. for(int i = 0; i < m_iRowSize; i++)
  2612. {
  2613. VariantClear(&cRow.pvData[i]);
  2614. }
  2615. free(cRow.pvData);
  2616. m_lCurPos = lCurPos;
  2617. SyncFields();
  2618. return(S_OK);
  2619. }
  2620. HRESULT GRecordset::Move(long Rows, VARIANT StartBookmark)
  2621. {
  2622. #if DBGLEVEL > 0
  2623. WriteLogFile("GRecordset::Move\r\n", true);
  2624. #endif // DBGLEVEL
  2625. if(!m_pPGres) return(S_FALSE);
  2626. m_bBOF = false;
  2627. if(StartBookmark.vt == (VT_ARRAY | VT_UI1))
  2628. {
  2629. long *plng = NULL;
  2630. SafeArrayAccessData(StartBookmark.parray, (void**)&plng);
  2631. m_lCurPos = *plng;
  2632. SafeArrayUnaccessData(StartBookmark.parray);
  2633. }
  2634. if(m_iCtidCol < 0)
  2635. {
  2636. m_lCurPos += Rows;
  2637. if(m_lCurPos >= m_lBufSize) m_lCurPos = m_lBufSize - 1;
  2638. }
  2639. else
  2640. {
  2641. long lSavePos = m_lCurPos;
  2642. while((Rows > 0) && (m_lCurPos < m_lBufSize - 1))
  2643. {
  2644. m_lCurPos++;
  2645. if(!PQgetisnull(m_pPGres, m_lCurPos, m_iCtidCol))
  2646. {
  2647. lSavePos = m_lCurPos;
  2648. Rows--;
  2649. }
  2650. }
  2651. if(PQgetisnull(m_pPGres, m_lCurPos, m_iCtidCol))
  2652. {
  2653. m_lCurPos = lSavePos;
  2654. m_bEOF = true;
  2655. }
  2656. }
  2657. SyncFields();
  2658. return(S_OK);
  2659. }
  2660. HRESULT GRecordset::MoveFirst()
  2661. {
  2662. #if DBGLEVEL > 0
  2663. WriteLogFile("GRecordset::MoveFirst\r\n", true);
  2664. #if DBGLEVEL > 2
  2665. WriteLogFile(" ", false);
  2666. WriteLogFile(m_sName, false);
  2667. WriteLogFile("\r\n", false);
  2668. #endif // DBGLEVEL > 2
  2669. #endif // DBGLEVEL
  2670. if(m_lRecordCount > 0)
  2671. {
  2672. m_bBOF = true;
  2673. m_bEOF = false;
  2674. m_lCurPos = 0;
  2675. if(m_iCtidCol < 0)
  2676. {
  2677. GetRecordBin(m_pPGres, m_lCurPos, m_iRowSize, m_plTypes, m_vBuffer.pvData,
  2678. m_pConnStruct->lDBEnc, m_pConnStruct->lGeomOid, m_piGeomDims,
  2679. m_pConnStruct->iCurrDigits);
  2680. }
  2681. else
  2682. {
  2683. while(PQgetisnull(m_pPGres, m_lCurPos, m_iCtidCol) &&
  2684. (m_lCurPos < m_lBufSize - 1))
  2685. {
  2686. m_lCurPos++;
  2687. }
  2688. GetRecordCtid(m_pPGres, m_lCurPos, m_iRowSize + 1, m_iCtidCol, m_plTypes,
  2689. m_vBuffer.pvData, m_pConnStruct->lDBEnc, m_vBuffer.sCtid,
  2690. &m_vBuffer.iCtidLen, m_pConnStruct->lGeomOid, m_piGeomDims,
  2691. m_pConnStruct->iCurrDigits);
  2692. }
  2693. m_pFields->SetBuffer(m_vBuffer.pvData);
  2694. }
  2695. else
  2696. {
  2697. m_bBOF = true;
  2698. m_bEOF = true;
  2699. m_lCurPos = -1;
  2700. m_pFields->SetBuffer(NULL);
  2701. }
  2702. if(m_lCurPos < 0) return(S_FALSE);
  2703. return S_OK;
  2704. }
  2705. HRESULT GRecordset::MoveLast()
  2706. {
  2707. #if DBGLEVEL > 0
  2708. WriteLogFile("GRecordset::MoveLast\r\n", true);
  2709. #if DBGLEVEL > 2
  2710. WriteLogFile(" ", false);
  2711. WriteLogFile(m_sName, false);
  2712. WriteLogFile("\r\n", false);
  2713. #endif // DBGLEVEL > 2
  2714. #endif // DBGLEVEL
  2715. if(m_lRecordCount > 0)
  2716. {
  2717. m_bBOF = false;
  2718. m_bEOF = true;
  2719. m_lCurPos = m_lBufSize - 1;
  2720. if(m_iCtidCol < 0)
  2721. {
  2722. GetRecordBin(m_pPGres, m_lCurPos, m_iRowSize, m_plTypes, m_vBuffer.pvData,
  2723. m_pConnStruct->lDBEnc, m_pConnStruct->lGeomOid, m_piGeomDims,
  2724. m_pConnStruct->iCurrDigits);
  2725. }
  2726. else
  2727. {
  2728. while(PQgetisnull(m_pPGres, m_lCurPos, m_iCtidCol) &&
  2729. (m_lCurPos > 0))
  2730. {
  2731. m_lCurPos--;
  2732. }
  2733. GetRecordCtid(m_pPGres, m_lCurPos, m_iRowSize + 1, m_iCtidCol, m_plTypes,
  2734. m_vBuffer.pvData, m_pConnStruct->lDBEnc, m_vBuffer.sCtid,
  2735. &m_vBuffer.iCtidLen, m_pConnStruct->lGeomOid, m_piGeomDims,
  2736. m_pConnStruct->iCurrDigits);
  2737. }
  2738. m_pFields->SetBuffer(m_vBuffer.pvData);
  2739. }
  2740. else
  2741. {
  2742. m_bBOF = true;
  2743. m_bEOF = true;
  2744. m_lCurPos = -1;
  2745. m_pFields->SetBuffer(NULL);
  2746. }
  2747. if(m_lCurPos < 0) return(S_FALSE);
  2748. return S_OK;
  2749. }
  2750. HRESULT GRecordset::MoveNext()
  2751. {
  2752. #if DBGLEVEL > 0
  2753. WriteLogFile("GRecordset::MoveNext\r\n", true);
  2754. #if DBGLEVEL > 2
  2755. WriteLogFile(" ", false);
  2756. WriteLogFile(m_sName, false);
  2757. WriteLogFile("\r\n", false);
  2758. #endif // DBGLEVEL > 2
  2759. #endif // DBGLEVEL
  2760. if(m_lRecordCount > 0)
  2761. {
  2762. long lLastPos = m_lCurPos;
  2763. m_bBOF = false;
  2764. if(m_lCurPos < m_lBufSize - 1)
  2765. {
  2766. m_lCurPos++;
  2767. if(m_iCtidCol < 0)
  2768. {
  2769. GetRecordBin(m_pPGres, m_lCurPos, m_iRowSize, m_plTypes, m_vBuffer.pvData,
  2770. m_pConnStruct->lDBEnc, m_pConnStruct->lGeomOid, m_piGeomDims,
  2771. m_pConnStruct->iCurrDigits);
  2772. }
  2773. else
  2774. {
  2775. while(PQgetisnull(m_pPGres, m_lCurPos, m_iCtidCol) &&
  2776. (m_lCurPos < m_lBufSize - 1))
  2777. {
  2778. m_lCurPos++;
  2779. }
  2780. if(PQgetisnull(m_pPGres, m_lCurPos, m_iCtidCol))
  2781. {
  2782. m_lCurPos = lLastPos;
  2783. m_bEOF = true;
  2784. }
  2785. else
  2786. {
  2787. GetRecordCtid(m_pPGres, m_lCurPos, m_iRowSize + 1, m_iCtidCol,
  2788. m_plTypes, m_vBuffer.pvData, m_pConnStruct->lDBEnc,
  2789. m_vBuffer.sCtid, &m_vBuffer.iCtidLen, m_pConnStruct->lGeomOid,
  2790. m_piGeomDims, m_pConnStruct->iCurrDigits);
  2791. }
  2792. }
  2793. m_pFields->SetBuffer(m_vBuffer.pvData);
  2794. }
  2795. else m_bEOF = true;
  2796. }
  2797. else
  2798. {
  2799. m_bBOF = true;
  2800. m_bEOF = true;
  2801. m_lCurPos = -1;
  2802. m_pFields->SetBuffer(NULL);
  2803. }
  2804. if(m_lCurPos < 0) return(S_FALSE);
  2805. return S_OK;
  2806. }
  2807. HRESULT GRecordset::MovePrevious()
  2808. {
  2809. #if DBGLEVEL > 0
  2810. WriteLogFile("GRecordset::MovePrevious\r\n", true);
  2811. #if DBGLEVEL > 2
  2812. WriteLogFile(" ", false);
  2813. WriteLogFile(m_sName, false);
  2814. WriteLogFile("\r\n", false);
  2815. #endif // DBGLEVEL > 2
  2816. #endif // DBGLEVEL
  2817. if(m_lRecordCount > 0)
  2818. {
  2819. long lLastPos = m_lCurPos;
  2820. m_bEOF = false;
  2821. if(m_lCurPos > 0)
  2822. {
  2823. m_lCurPos--;
  2824. if(m_iCtidCol < 0)
  2825. {
  2826. GetRecordBin(m_pPGres, m_lCurPos, m_iRowSize, m_plTypes, m_vBuffer.pvData,
  2827. m_pConnStruct->lDBEnc, m_pConnStruct->lGeomOid, m_piGeomDims,
  2828. m_pConnStruct->iCurrDigits);
  2829. }
  2830. else
  2831. {
  2832. while(PQgetisnull(m_pPGres, m_lCurPos, m_iCtidCol) &&
  2833. (m_lCurPos > 0))
  2834. {
  2835. m_lCurPos--;
  2836. }
  2837. if(PQgetisnull(m_pPGres, m_lCurPos, m_iCtidCol))
  2838. {
  2839. m_lCurPos = lLastPos;
  2840. m_bBOF = true;
  2841. }
  2842. else
  2843. {
  2844. GetRecordCtid(m_pPGres, m_lCurPos, m_iRowSize + 1, m_iCtidCol,
  2845. m_plTypes, m_vBuffer.pvData, m_pConnStruct->lDBEnc,
  2846. m_vBuffer.sCtid, &m_vBuffer.iCtidLen, m_pConnStruct->lGeomOid,
  2847. m_piGeomDims, m_pConnStruct->iCurrDigits);
  2848. }
  2849. }
  2850. m_pFields->SetBuffer(m_vBuffer.pvData);
  2851. }
  2852. else m_bBOF = true;
  2853. }
  2854. else
  2855. {
  2856. m_bBOF = true;
  2857. m_bEOF = true;
  2858. m_lCurPos = -1;
  2859. m_pFields->SetBuffer(NULL);
  2860. }
  2861. if(m_lCurPos < 0) return(S_FALSE);
  2862. return S_OK;
  2863. }
  2864. HRESULT GRecordset::Update()
  2865. {
  2866. #if DBGLEVEL > 0
  2867. WriteLogFile("GRecordset::Update\r\n", true);
  2868. if(m_iSpecType == 2) WriteLogFile(" GCoordSystem\r\n", false);
  2869. #if DBGLEVEL > 2
  2870. else
  2871. {
  2872. WriteLogFile(" ", false);
  2873. WriteLogFile(m_sName, false);
  2874. WriteLogFile("\r\n", false);
  2875. }
  2876. #endif // DBGLEVEL > 2
  2877. #endif // DBGLEVEL
  2878. if(!m_bUpdatable) return(S_FALSE);
  2879. if(m_iEditMode < 1) return S_FALSE;
  2880. bool bFound = false;
  2881. int i = 0, j;
  2882. int n = m_pFields->GetCount();
  2883. int nParams = 0;
  2884. char sBuf[64];
  2885. GField *pfld;
  2886. wchar_t wsOldCSGuid[40];
  2887. wchar_t wsNewCSGuid[40];
  2888. wsOldCSGuid[0] = 0;
  2889. wsNewCSGuid[0] = 0;
  2890. VARIANT *pvSCRow = NULL;
  2891. bool bCS = (m_iSpecType == 2);
  2892. if(bCS && (n == 119))
  2893. {
  2894. VARIANT pvSCTblRow[119];
  2895. for(i = 0; i < 119; i++)
  2896. {
  2897. pfld = (GField*)m_pFields->GetItem(i);
  2898. pvSCTblRow[i] = pfld->GetModVal();
  2899. }
  2900. VarToWideChar(pvSCTblRow[0], wsNewCSGuid, 40, false, false);
  2901. pvSCRow = FindCSTableRowByCS(&m_pConnStruct->cCSTable, pvSCTblRow);
  2902. if(!pvSCRow)
  2903. {
  2904. wchar_t wBuf[128];
  2905. LoadString(m_pConnStruct->hInstance, IDE_UNKNOWNCS, wBuf, 128);
  2906. m_pErrors->HandleGDOError(wBuf, m_pConnStruct->hInstance);
  2907. return(E_FAIL);
  2908. }
  2909. VarToWideChar(pvSCRow[1], wsOldCSGuid, 40, false, false);
  2910. }
  2911. int ilen = 72 + 14*n + 15 + GetTableNameLen(m_pTblDef->GetOrigSchemaPtr(),
  2912. m_pTblDef->GetOrigNamePtr());
  2913. for(j = 0; j < n; j++)
  2914. {
  2915. pfld = (GField*)m_pFields->GetItem(j);
  2916. ilen += 2*strlen(pfld->GetOrigNamePtr());
  2917. }
  2918. LPSTR sCmd = (LPSTR)malloc(ilen*sizeof(char));
  2919. sCmd[0] = 0;
  2920. Oid *plTypes = NULL;
  2921. LPSTR *psParams = NULL;
  2922. int *piLens = NULL;
  2923. int *piForms = NULL;
  2924. VARIANT vVal;
  2925. VariantInit(&vVal);
  2926. i = 0;
  2927. while(!bFound && (i < n))
  2928. {
  2929. pfld = (GField*)m_pFields->GetItem(i++);
  2930. bFound = pfld->Modified();
  2931. }
  2932. long lParams[8];
  2933. CGeomInfo cgInfo;
  2934. if(m_iEditMode == 1)
  2935. {
  2936. if(!bFound)
  2937. {
  2938. // it looks like nothing has changed, so nothing happens
  2939. m_iEditMode = 0;
  2940. m_lModPos = m_lCurPos;
  2941. return(S_OK);
  2942. }
  2943. strcpy(sCmd, "update ");
  2944. CatTableName(sCmd, m_pTblDef->GetOrigSchemaPtr(),
  2945. m_pTblDef->GetOrigNamePtr(), !m_bIsMetatable);
  2946. strcat(sCmd, " set ");
  2947. CatFieldName(sCmd, pfld->GetOrigNamePtr(), !m_bIsMetatable);
  2948. strcat(sCmd, " = $1");
  2949. nParams = 1;
  2950. while(i < n)
  2951. {
  2952. pfld = (GField*)m_pFields->GetItem(i++);
  2953. if(pfld->Modified())
  2954. {
  2955. nParams++;
  2956. strcat(sCmd, ", ");
  2957. CatFieldName(sCmd, pfld->GetOrigNamePtr(), !m_bIsMetatable);
  2958. sprintf(sBuf, " = $%d", nParams);
  2959. strcat(sCmd, sBuf);
  2960. }
  2961. }
  2962. nParams++;
  2963. sprintf(sBuf, " where ctid = $%d", nParams);
  2964. strcat(sCmd, sBuf);
  2965. strcat(sCmd, " returning ctid");
  2966. plTypes = (Oid*)malloc(nParams*sizeof(Oid));
  2967. psParams = (LPSTR*)malloc(nParams*sizeof(LPSTR));
  2968. piLens = (int*)malloc(nParams*sizeof(int));
  2969. piForms = (int*)malloc(nParams*sizeof(int));
  2970. j = 0;
  2971. for(i = 0; i < n; i++)
  2972. {
  2973. pfld = (GField*)m_pFields->GetItem(i);
  2974. if(pfld->Modified())
  2975. {
  2976. pfld->FillGeomInfo(&cgInfo);
  2977. vVal = pfld->GetModVal();
  2978. //plTypes[j] = VarTypeToOid(vVal);
  2979. plTypes[j] = pfld->GetTypeOid();
  2980. if(pfld->GetTypeOid() == NUMERICOID) plTypes[j] = FLOAT8OID;
  2981. piLens[j] = VarToBinaryLen(vVal, plTypes[j],
  2982. m_pConnStruct->lDBEnc, &cgInfo, lParams);
  2983. if((piLens[j] > 0) || pfld->GetRequired())
  2984. {
  2985. psParams[j] = (LPSTR)malloc((piLens[j] + 1)*sizeof(char));
  2986. VarToBinaryBuf(vVal, plTypes[j], m_pConnStruct->lDBEnc,
  2987. psParams[j], piLens[j], &cgInfo, lParams, m_pConnStruct->iCurrDigits);
  2988. }
  2989. else psParams[j] = NULL;
  2990. piForms[j++] = 1;
  2991. }
  2992. }
  2993. plTypes[j] = 0;
  2994. piLens[j] = m_vBuffer.iCtidLen;
  2995. psParams[j] = (LPSTR)malloc((piLens[j] + 1)*sizeof(char));
  2996. memcpy(psParams[j], m_vBuffer.sCtid, piLens[j]);
  2997. (psParams[j])[piLens[j]] = 0;
  2998. piForms[j] = 1;
  2999. }
  3000. else if(m_iEditMode == 2)
  3001. {
  3002. if(!bFound)
  3003. {
  3004. // insert without modification can be a problem
  3005. // we have to insert at least one sequenced field with default
  3006. i = 0;
  3007. while(!bFound && (i < n))
  3008. {
  3009. pfld = (GField*)m_pFields->GetItem(i++);
  3010. bFound = pfld->HasSeq();
  3011. }
  3012. if(!bFound)
  3013. {
  3014. return(S_FALSE);
  3015. }
  3016. strcpy(sCmd, "insert into ");
  3017. CatTableName(sCmd, m_pTblDef->GetOrigSchemaPtr(),
  3018. m_pTblDef->GetOrigNamePtr(), !m_bIsMetatable);
  3019. strcat(sCmd, "(");
  3020. CatFieldName(sCmd, pfld->GetOrigNamePtr(), !m_bIsMetatable);
  3021. strcat(sCmd, ") values (default) returning ctid");
  3022. }
  3023. else
  3024. {
  3025. strcpy(sCmd, "insert into ");
  3026. CatTableName(sCmd, m_pTblDef->GetOrigSchemaPtr(),
  3027. m_pTblDef->GetOrigNamePtr(), !m_bIsMetatable);
  3028. strcat(sCmd, "(");
  3029. CatFieldName(sCmd, pfld->GetOrigNamePtr(), !m_bIsMetatable);
  3030. j = i;
  3031. while(i < n)
  3032. {
  3033. pfld = (GField*)m_pFields->GetItem(i++);
  3034. if(pfld->Modified())
  3035. {
  3036. strcat(sCmd, ", ");
  3037. CatFieldName(sCmd, pfld->GetOrigNamePtr(), !m_bIsMetatable);
  3038. }
  3039. }
  3040. strcat(sCmd, ") values ($1");
  3041. i = j;
  3042. nParams = 1;
  3043. while(i < n)
  3044. {
  3045. pfld = (GField*)m_pFields->GetItem(i++);
  3046. if(pfld->Modified())
  3047. {
  3048. nParams++;
  3049. sprintf(sBuf, ", $%d", nParams);
  3050. strcat(sCmd, sBuf);
  3051. }
  3052. }
  3053. strcat(sCmd, ") returning ctid");
  3054. plTypes = (Oid*)malloc(nParams*sizeof(Oid));
  3055. psParams = (LPSTR*)malloc(nParams*sizeof(LPSTR));
  3056. piLens = (int*)malloc(nParams*sizeof(int));
  3057. piForms = (int*)malloc(nParams*sizeof(int));
  3058. j = 0;
  3059. for(i = 0; i < n; i++)
  3060. {
  3061. pfld = (GField*)m_pFields->GetItem(i);
  3062. if(pfld->Modified())
  3063. {
  3064. pfld->FillGeomInfo(&cgInfo);
  3065. vVal = pfld->GetModVal();
  3066. //plTypes[j] = VarTypeToOid(vVal);
  3067. plTypes[j] = pfld->GetTypeOid();
  3068. if(pfld->GetTypeOid() == NUMERICOID) plTypes[j] = FLOAT8OID;
  3069. piLens[j] = VarToBinaryLen(vVal, plTypes[j],
  3070. m_pConnStruct->lDBEnc, &cgInfo, lParams);
  3071. if((piLens[j] > 0) || pfld->GetRequired())
  3072. {
  3073. psParams[j] = (LPSTR)malloc((piLens[j] + 1)*sizeof(char));
  3074. VarToBinaryBuf(vVal, plTypes[j], m_pConnStruct->lDBEnc,
  3075. psParams[j], piLens[j], &cgInfo, lParams, m_pConnStruct->iCurrDigits);
  3076. }
  3077. else psParams[j] = NULL;
  3078. piForms[j++] = 1;
  3079. }
  3080. }
  3081. }
  3082. //for(i = 0; i < n; i++)
  3083. //{
  3084. // pfld = (GField*)m_pFields->GetItem(i);
  3085. // if(pfld->HasSeq())
  3086. // {
  3087. // strcat(sCmd, ", ");
  3088. // CatFieldName(sCmd, pfld->GetOrigNamePtr(), !m_bIsMetatable);
  3089. // }
  3090. //}
  3091. }
  3092. for(i = 0; i < n; i++)
  3093. {
  3094. pfld = (GField*)m_pFields->GetItem(i);
  3095. strcat(sCmd, ", ");
  3096. CatFieldName(sCmd, pfld->GetOrigNamePtr(), !m_bIsMetatable);
  3097. }
  3098. if(!sCmd[0])
  3099. {
  3100. free(sCmd);
  3101. return(S_FALSE);
  3102. }
  3103. bool bTrans = (m_pConnStruct->iSysTrans | m_pConnStruct->iGdoTrans);
  3104. #if DBGLEVEL > 0
  3105. WriteLogFile(sCmd, true);
  3106. WriteLogFile("\r\n", false);
  3107. WriteMallocReport(10, ilen, strlen(sCmd));
  3108. #endif // DBGLEVEL
  3109. SetSP(m_pConnStruct->pConn, bTrans);
  3110. PGresult *res = PQexecParams(m_pConnStruct->pConn, sCmd, nParams, plTypes,
  3111. psParams, piLens, piForms, 1);
  3112. free(sCmd);
  3113. if(PQresultStatus(res) != PGRES_TUPLES_OK)
  3114. {
  3115. m_pErrors->HandlePQError(m_pConnStruct->pConn, m_pConnStruct->hInstance);
  3116. PQclear(res);
  3117. RollbackSP(m_pConnStruct->pConn, bTrans);
  3118. for(i = 0; i < nParams; i++)
  3119. {
  3120. if(psParams[i]) free(psParams[i]);
  3121. }
  3122. free(plTypes);
  3123. free(psParams);
  3124. free(piLens);
  3125. free(piForms);
  3126. return(E_FAIL);
  3127. }
  3128. long nrows = PQntuples(res);
  3129. //char buf[64];
  3130. if(nrows > 0)
  3131. {
  3132. int ncols = PQnfields(res);
  3133. m_lModPos = m_lCurPos;
  3134. if(m_iEditMode == 2)
  3135. {
  3136. m_lModPos = m_lBufSize;
  3137. m_lCurPos = -1;
  3138. m_lBufSize++;
  3139. m_lRecordCount++;
  3140. m_bEOF = false;
  3141. m_lInserted++;
  3142. }
  3143. if(m_lInserted > 10000)
  3144. {
  3145. // simply reopen the recordset
  3146. PQclear(m_pPGres);
  3147. m_pPGres = PQexecPrepared(m_pConnStruct->pConn, m_sStmtName, m_iParams,
  3148. m_psParams, m_piParLenght, m_piParType, 1);
  3149. m_lBufSize = PQntuples(m_pPGres);
  3150. m_lRecordCount = m_lBufSize;
  3151. m_lInserted = 0;
  3152. }
  3153. else
  3154. {
  3155. int iRes = PQsetvalue(m_pPGres, m_lModPos, m_iCtidCol, PQgetvalue(res, 0, 0),
  3156. PQgetlength(res, 0, 0));
  3157. i = 0;
  3158. while((i < m_iCtidCol) && (iRes != 0))
  3159. {
  3160. if(PQgetisnull(res, 0, i + 1))
  3161. iRes = PQsetvalue(m_pPGres, m_lModPos, i, NULL, -1);
  3162. else
  3163. iRes = PQsetvalue(m_pPGres, m_lModPos, i, PQgetvalue(res, 0, i + 1),
  3164. PQgetlength(res, 0, i + 1));
  3165. i++;
  3166. }
  3167. i = m_iCtidCol + 1;
  3168. while((i < ncols) && (iRes != 0))
  3169. {
  3170. if(PQgetisnull(res, 0, i))
  3171. iRes = PQsetvalue(m_pPGres, m_lModPos, i, NULL, -1);
  3172. else
  3173. iRes = PQsetvalue(m_pPGres, m_lModPos, i, PQgetvalue(res, 0, i),
  3174. PQgetlength(res, 0, i));
  3175. i++;
  3176. }
  3177. if(iRes == 0)
  3178. m_pErrors->HandlePQError(m_pConnStruct->pConn,
  3179. m_pConnStruct->hInstance);
  3180. }
  3181. }
  3182. PQclear(res);
  3183. ReleaseSP(m_pConnStruct->pConn, bTrans);
  3184. for(i = 0; i < nParams; i++)
  3185. {
  3186. if(psParams[i]) free(psParams[i]);
  3187. }
  3188. free(piForms);
  3189. free(piLens);
  3190. free(psParams);
  3191. free(plTypes);
  3192. if(m_lCurPos < 0) m_lCurPos = m_lModPos;
  3193. SyncBuf();
  3194. m_pFields->UpdateRow(m_vBuffer.pvData);
  3195. GDatabase *pDB = (GDatabase*)m_pConnStruct->lSessionId;
  3196. if(pDB->GetModificationLogging())
  3197. WriteModLog(m_pConnStruct, m_lTblOid, m_pFields, 3 - m_iEditMode);
  3198. m_iEditMode = 0;
  3199. if(bCS && wsOldCSGuid[0])
  3200. {
  3201. VariantClear(&pvSCRow[1]);
  3202. pvSCRow[1].vt = VT_BSTR;
  3203. pvSCRow[1].bstrVal = SysAllocString(wsNewCSGuid);
  3204. pDB->BroadcastCSChange(wsOldCSGuid, wsNewCSGuid);
  3205. }
  3206. return(S_OK);
  3207. }
  3208. LPWSTR GRecordset::GetNamePtr()
  3209. {
  3210. return(m_sName);
  3211. }
  3212. bool IsDelim(char c)
  3213. {
  3214. return((c == 0) || (c == ' ') || (c == '.') || (c == '\'') ||
  3215. (c == '\"') || (c == '(') || (c == ')') || (c == '=') ||
  3216. (c == '<') || (c == '>') || (c == ','));
  3217. }
  3218. int GetNextWorld(LPSTR sStr, LPSTR sWordBuf, int iBufLen, int *piToSkip,
  3219. int *piToCopy)
  3220. {
  3221. int iRes = 0;
  3222. *piToSkip = 0;
  3223. LPSTR sStart = sStr;
  3224. while(sStart[0] == ' ')
  3225. {
  3226. sStart++;
  3227. (*piToSkip)++;
  3228. }
  3229. LPSTR sEnd = sStart;
  3230. while(!IsDelim(sEnd[0])) sEnd++;
  3231. if(sEnd[0] == 0)
  3232. {
  3233. iRes = sEnd - sStart;
  3234. *piToCopy = 0;
  3235. if(iRes < iBufLen)
  3236. {
  3237. strncpy(sWordBuf, sStart, iRes);
  3238. sWordBuf[iRes] = 0;
  3239. }
  3240. return(iRes);
  3241. }
  3242. if((sEnd[0] == ' ') || (sEnd[0] == '.'))
  3243. {
  3244. iRes = sEnd - sStart;
  3245. *piToCopy = 1;
  3246. if(iRes < iBufLen)
  3247. {
  3248. strncpy(sWordBuf, sStart, iRes);
  3249. sWordBuf[iRes] = 0;
  3250. }
  3251. return(iRes);
  3252. }
  3253. if(sEnd[0] == '(')
  3254. {
  3255. iRes = sEnd - sStart;
  3256. *piToCopy = 1;
  3257. if(iRes < iBufLen)
  3258. {
  3259. strncpy(sWordBuf, sStart, iRes);
  3260. sWordBuf[iRes] = 0;
  3261. }
  3262. return(iRes);
  3263. }
  3264. if((sEnd[0] == ')') || (sEnd[0] == ','))
  3265. {
  3266. iRes = sEnd - sStart;
  3267. *piToCopy = 1;
  3268. if(iRes < iBufLen)
  3269. {
  3270. strncpy(sWordBuf, sStart, iRes);
  3271. sWordBuf[iRes] = 0;
  3272. }
  3273. sEnd++;
  3274. if(sEnd[0] == ' ') *piToCopy = 2;
  3275. return(iRes);
  3276. }
  3277. if(sEnd[0] == '\'')
  3278. {
  3279. iRes = sEnd - sStart;
  3280. *piToCopy = 0;
  3281. if(iRes < iBufLen)
  3282. {
  3283. strncpy(sWordBuf, sStart, iRes);
  3284. sWordBuf[iRes] = 0;
  3285. }
  3286. sStart = sEnd++;
  3287. sEnd = strchr(sEnd, '\'');
  3288. if(sEnd)
  3289. {
  3290. sEnd++;
  3291. *piToCopy = sEnd - sStart;
  3292. if(sEnd[0] == ' ') (*piToCopy)++;
  3293. }
  3294. return(iRes);
  3295. }
  3296. if(sEnd[0] == '\"')
  3297. {
  3298. iRes = sEnd - sStart;
  3299. *piToCopy = 0;
  3300. if(iRes < iBufLen)
  3301. {
  3302. strncpy(sWordBuf, sStart, iRes);
  3303. sWordBuf[iRes] = 0;
  3304. }
  3305. sStart = sEnd++;
  3306. sEnd = strchr(sEnd, '\"');
  3307. if(sEnd)
  3308. {
  3309. sEnd++;
  3310. *piToCopy = sEnd - sStart;
  3311. if(sEnd[0] == ' ') (*piToCopy)++;
  3312. }
  3313. return(iRes);
  3314. }
  3315. if((sEnd[0] == '=') || (sEnd[0] == '<') || (sEnd[0] == '>'))
  3316. {
  3317. iRes = sEnd - sStart;
  3318. *piToCopy = 1;
  3319. if(iRes < iBufLen)
  3320. {
  3321. strncpy(sWordBuf, sStart, iRes);
  3322. sWordBuf[iRes] = 0;
  3323. }
  3324. sEnd++;
  3325. if(sEnd[0] == ' ') *piToCopy = 2;
  3326. return(iRes);
  3327. }
  3328. return(iRes);
  3329. }
  3330. bool IsSqlKeyWord(LPSTR sWord)
  3331. {
  3332. if(stricmp(sWord, "select") == 0) return(true);
  3333. if(stricmp(sWord, "*") == 0) return(true);
  3334. if(stricmp(sWord, "from") == 0) return(true);
  3335. if(stricmp(sWord, "where") == 0) return(true);
  3336. if(stricmp(sWord, "like") == 0) return(true);
  3337. if(stricmp(sWord, "distinct") == 0) return(true);
  3338. if(stricmp(sWord, "upper") == 0) return(true);
  3339. if(stricmp(sWord, "lower") == 0) return(true);
  3340. if(stricmp(sWord, "left") == 0) return(true);
  3341. if(stricmp(sWord, "right") == 0) return(true);
  3342. if(stricmp(sWord, "outer") == 0) return(true);
  3343. if(stricmp(sWord, "inner") == 0) return(true);
  3344. if(stricmp(sWord, "join") == 0) return(true);
  3345. if(stricmp(sWord, "order") == 0) return(true);
  3346. if(stricmp(sWord, "group") == 0) return(true);
  3347. if(stricmp(sWord, "by") == 0) return(true);
  3348. if(stricmp(sWord, "asc") == 0) return(true);
  3349. if(stricmp(sWord, "desc") == 0) return(true);
  3350. if(stricmp(sWord, "max") == 0) return(true);
  3351. if(stricmp(sWord, "min") == 0) return(true);
  3352. if(stricmp(sWord, "count") == 0) return(true);
  3353. if(stricmp(sWord, "and") == 0) return(true);
  3354. if(stricmp(sWord, "or") == 0) return(true);
  3355. if(stricmp(sWord, "between") == 0) return(true);
  3356. if(stricmp(sWord, "begin") == 0) return(true);
  3357. if(stricmp(sWord, "end") == 0) return(true);
  3358. if(stricmp(sWord, "in") == 0) return(true);
  3359. if(stricmp(sWord, "not") == 0) return(true);
  3360. if(stricmp(sWord, "is") == 0) return(true);
  3361. if(stricmp(sWord, "null") == 0) return(true);
  3362. if(stricmp(sWord, "true") == 0) return(true);
  3363. if(stricmp(sWord, "false") == 0) return(true);
  3364. if(strcmp(sWord, "DATE") == 0) return(true);
  3365. if(strcmp(sWord, "TIME") == 0) return(true);
  3366. if(strcmp(sWord, "DATETIME") == 0) return(true);
  3367. if(strcmp(sWord, "TIMESTAMP") == 0) return(true);
  3368. int iLen = strlen(sWord);
  3369. int i = 0;
  3370. bool bDot = false;
  3371. if((i < iLen) && (sWord[i] == '-')) i++;
  3372. if((i < iLen) && (sWord[i] == '+')) i++;
  3373. while(((i < iLen) && (sWord[i] >= '0') && (sWord[i] <= '9')) ||
  3374. (!bDot && (sWord[i] == '.')))
  3375. {
  3376. if(sWord[i] == '.') bDot = true;
  3377. i++;
  3378. }
  3379. if(i >= iLen) return(true);
  3380. return(false);
  3381. }
  3382. LPSTR CopyWithSelect(LPSTR sOrig, bool bDecorate, GTDFields *pFlds)
  3383. {
  3384. LPSTR sRes = NULL;
  3385. LPSTR sSelect = strstr(sOrig, "SELECT ");
  3386. if(!sSelect) sSelect = strstr(sOrig, "select ");
  3387. if(!sSelect) sSelect = strstr(sOrig, "Select ");
  3388. int ilen = 0;
  3389. if(sSelect)
  3390. {
  3391. ilen = strlen(sOrig) + 1;
  3392. if(bDecorate)
  3393. {
  3394. bool bWhere = false;
  3395. bool bIsBoolField = false;
  3396. GTDField *pFld = NULL;
  3397. int iBoolVal;
  3398. ilen += 20;
  3399. sRes = (LPSTR)malloc(ilen*sizeof(char));
  3400. sRes[0] = 0;
  3401. int iCurLen = 0;
  3402. LPSTR sCurPos = sOrig;
  3403. int iWordBufLen = 128;
  3404. LPSTR sWordBuf = (LPSTR)malloc(iWordBufLen*sizeof(char));
  3405. int iToSkip, iToCopy;
  3406. int iWorldLen = GetNextWorld(sCurPos, sWordBuf, iWordBufLen,
  3407. &iToSkip, &iToCopy);
  3408. if(iWorldLen >= iWordBufLen)
  3409. {
  3410. free(sWordBuf);
  3411. iWordBufLen = iWorldLen + 1;
  3412. sWordBuf = (LPSTR)malloc(iWordBufLen*sizeof(char));
  3413. iWorldLen = GetNextWorld(sCurPos, sWordBuf, iWordBufLen,
  3414. &iToSkip, &iToCopy);
  3415. }
  3416. while((iWorldLen > 0) || (iToCopy > 0))
  3417. {
  3418. sCurPos += iToSkip;
  3419. if(ilen < (iCurLen + iWorldLen + iToCopy + 4))
  3420. {
  3421. ilen = iCurLen + iWorldLen + iToCopy + 4;
  3422. sRes = (LPSTR)realloc(sRes, ilen*sizeof(char));
  3423. }
  3424. if(iWorldLen > 0)
  3425. {
  3426. if(!IsSqlKeyWord(sWordBuf))
  3427. {
  3428. strcat(sRes, "\"");
  3429. strcat(sRes, sWordBuf);
  3430. strcat(sRes, "\"");
  3431. iCurLen += 2;
  3432. if(bWhere && pFlds)
  3433. {
  3434. pFld = pFlds->GetByName(sWordBuf);
  3435. if(pFld)
  3436. {
  3437. bIsBoolField = (pFld->GetType() == gdbBoolean);
  3438. }
  3439. else bIsBoolField = false;
  3440. }
  3441. }
  3442. else
  3443. {
  3444. if(bWhere)
  3445. {
  3446. if(bIsBoolField)
  3447. {
  3448. if(sscanf(sWordBuf, "%d", &iBoolVal) == 1)
  3449. {
  3450. if(iBoolVal)
  3451. {
  3452. strcat(sRes, "true");
  3453. iCurLen += (4 - iWorldLen);
  3454. }
  3455. else
  3456. {
  3457. strcat(sRes, "false");
  3458. iCurLen += (5 - iWorldLen);
  3459. }
  3460. }
  3461. else strcat(sRes, sWordBuf);
  3462. }
  3463. else strcat(sRes, sWordBuf);
  3464. }
  3465. else
  3466. {
  3467. bWhere = (stricmp(sWordBuf, "where") == 0);
  3468. strcat(sRes, sWordBuf);
  3469. }
  3470. }
  3471. sCurPos += iWorldLen;
  3472. iCurLen += iWorldLen;
  3473. }
  3474. if(iToCopy)
  3475. {
  3476. strncat(sRes, sCurPos, iToCopy);
  3477. sCurPos += iToCopy;
  3478. iCurLen += iToCopy;
  3479. sRes[iCurLen] = 0;
  3480. }
  3481. iWorldLen = GetNextWorld(sCurPos, sWordBuf, iWordBufLen,
  3482. &iToSkip, &iToCopy);
  3483. if(iWorldLen >= iWordBufLen)
  3484. {
  3485. free(sWordBuf);
  3486. iWordBufLen = iWorldLen + 1;
  3487. sWordBuf = (LPSTR)malloc(iWordBufLen*sizeof(char));
  3488. iWorldLen = GetNextWorld(sCurPos, sWordBuf, iWordBufLen,
  3489. &iToSkip, &iToCopy);
  3490. }
  3491. }
  3492. free(sWordBuf);
  3493. }
  3494. else
  3495. {
  3496. sRes = (LPSTR)malloc(ilen*sizeof(char));
  3497. strcpy(sRes, sOrig);
  3498. }
  3499. }
  3500. else
  3501. {
  3502. ilen = strlen(sOrig) + 20; // "select * from "
  3503. sRes = (LPSTR)malloc(ilen*sizeof(char));
  3504. strcpy(sRes, "select * from ");
  3505. if(bDecorate)
  3506. {
  3507. strcat(sRes, "\"");
  3508. LPSTR sDot = strchr(sOrig, '.');
  3509. if(sDot)
  3510. {
  3511. strncat(sRes, sOrig, sDot - sOrig);
  3512. sRes[sDot - sOrig + 15] = 0;
  3513. strcat(sRes, "\".\"");
  3514. sDot++;
  3515. strcat(sRes, sDot);
  3516. }
  3517. else strcat(sRes, sOrig);
  3518. strcat(sRes, "\"");
  3519. }
  3520. else strcat(sRes, sOrig);
  3521. }
  3522. return(sRes);
  3523. }
  3524. int GetCtidColumn(PGresult *res)
  3525. {
  3526. int iRes = -1;
  3527. int ncols = PQnfields(res);
  3528. int i = 0;
  3529. while((iRes < 0) && (i < ncols))
  3530. {
  3531. if(stricmp(PQfname(res, i), "ctid") == 0) iRes = i;
  3532. i++;
  3533. }
  3534. return(iRes);
  3535. }
  3536. bool GRecordset::SetAttributes(BSTR Name, VARIANT Type, VARIANT options,
  3537. VARIANT SpatialOp, VARIANT SpatialGeometryFilter, VARIANT GeometryFieldName,
  3538. GTableDefs *pGTbls, PConnStruct pConnStruct)
  3539. {
  3540. m_pConnStruct = pConnStruct;
  3541. m_pErrors = (GErrors*)m_pConnStruct->pErrors;
  3542. m_iParams = 0;
  3543. m_psParams = NULL;
  3544. m_piParType = NULL;
  3545. m_piParLenght = NULL;
  3546. m_pFields->ClearAll();
  3547. m_iOpenStatus = 0;
  3548. m_pConnStruct->lCursors++;
  3549. sprintf(m_sStmtName, "recset%.4ld", m_pConnStruct->lCursors);
  3550. wcsncpy(m_sName, Name, 255);
  3551. m_sName[255] = 0;
  3552. VariantCopy(&m_vType, &Type);
  3553. VariantCopy(&m_voptions, &options);
  3554. VariantCopy(&m_vSpatialOp, &SpatialOp);
  3555. VariantCopy(&m_vSpatialGeometryFilter, &SpatialGeometryFilter);
  3556. VariantCopy(&m_vGeometryFieldName, &GeometryFieldName);
  3557. if(m_vType.vt == VT_ERROR)
  3558. {
  3559. m_vType.vt = VT_I4;
  3560. m_vType.lVal = gdbOpenDynaset;
  3561. }
  3562. if(m_voptions.vt == VT_ERROR)
  3563. {
  3564. m_voptions.vt = VT_I4;
  3565. m_voptions.lVal = 0;
  3566. }
  3567. long lopt = VarToLong(m_voptions);
  3568. if(lopt > -1) m_bUpdatable = !(lopt & 4);
  3569. else m_bUpdatable = true;
  3570. if(m_bUpdatable) m_bUpdatable = pConnStruct->bUpdatable;
  3571. LPWSTR wsName = GetTableName(Name);
  3572. LPSTR sName = WCharToDBStr(wsName, pConnStruct->lDBEnc);
  3573. //m_pFCExt = new GFeatureClassExt(this,
  3574. // ((ITypeLib**)pConnStruct->ppTypeLibs)[1]);
  3575. //m_pFCExt->SetTableName(wsName);
  3576. GTableDef *pTBD = NULL;
  3577. GTDFields *pTBFlds = NULL;
  3578. int idx = pGTbls->FindByWName(wsName);
  3579. if(idx > -1)
  3580. {
  3581. pTBD = (GTableDef*)pGTbls->GetItem(idx);
  3582. pTBD->BuildFromConnection();
  3583. pTBFlds = pTBD->GetFieldsPtr();
  3584. if(m_bUpdatable) m_bUpdatable = pTBD->GetUpdatable();
  3585. }
  3586. LPSTR sOpenStr = NULL;
  3587. int ilen;
  3588. LPSTR sGATStart;
  3589. bool bNull;
  3590. int iGeomParOffs = 0;
  3591. int iCSRows = 0;
  3592. if(stricmp(sName, pConnStruct->sCoordSystems) == 0) // coordsystem
  3593. {
  3594. if(pConnStruct->iConnStat <= icsINGRMetaIncomplete)
  3595. {
  3596. iCSRows = GetNamedCSGuidCount(&m_pConnStruct->cCSTable);
  3597. m_iParams = 119*iCSRows;
  3598. iGeomParOffs = m_iParams;
  3599. }
  3600. }
  3601. iGeomParOffs = m_iParams;
  3602. LPSTR sGeomName = NULL;
  3603. unsigned long ulSrid = 0;
  3604. if(SpatialGeometryFilter.vt == (VT_ARRAY | VT_UI1))
  3605. {
  3606. BSTR bsGeom = NULL;
  3607. if(GeometryFieldName.vt == VT_BSTR) bsGeom = GeometryFieldName.bstrVal;
  3608. else if(GeometryFieldName.vt == (VT_BYREF | VT_BSTR))
  3609. bsGeom = *GeometryFieldName.pbstrVal;
  3610. if(bsGeom)
  3611. {
  3612. GTDField *pFld = NULL;
  3613. if(pTBD) pFld = pTBFlds->GetByWName(bsGeom);
  3614. if(pFld)
  3615. {
  3616. ulSrid = pFld->GetSrid();
  3617. sGeomName = pFld->GetFilterFieldName();
  3618. }
  3619. }
  3620. }
  3621. if(sGeomName) m_iParams++;
  3622. Oid *plDataTypes = NULL;
  3623. if(m_iParams > 0)
  3624. {
  3625. m_psParams = (LPSTR*)malloc(m_iParams*sizeof(LPSTR));
  3626. m_piParType = (int*)malloc(m_iParams*sizeof(int));
  3627. m_piParLenght = (int*)malloc(m_iParams*sizeof(int));
  3628. plDataTypes = (Oid*)malloc(m_iParams*sizeof(Oid));
  3629. for(int i = 0; i < m_iParams; i++) m_piParType[i] = 1;
  3630. for(int i = iGeomParOffs; i < m_iParams; i++) plDataTypes[i] = BYTEAOID;
  3631. }
  3632. LPSTR sOrigName = WCharToDBStr(Name, pConnStruct->lDBEnc);
  3633. LPSTR sRest = strstr(sOrigName, sName);
  3634. if(sRest)
  3635. {
  3636. sRest += strlen(sName);
  3637. if(*sRest == ']') sRest++;
  3638. if(*sRest == 0) sRest = NULL;
  3639. }
  3640. PGresult *res;
  3641. bool bTrans = (m_pConnStruct->iSysTrans | m_pConnStruct->iGdoTrans) > 0;
  3642. m_iCtidCol = -1;
  3643. m_bIsMetatable = false;
  3644. if(stricmp(sName, "GAliasTable") == 0)
  3645. {
  3646. m_bIsMetatable = true;
  3647. m_iSpecType = 1;
  3648. if(pConnStruct->iConnStat > icsNoMetadata)
  3649. sOpenStr = CopyWithSelect(sOrigName, false, NULL);
  3650. else
  3651. {
  3652. m_bUpdatable = false;
  3653. LPCSTR sGATAlias = "select \
  3654. cast('GCoordSystemTable' as varchar(255)) as TableType, \
  3655. cast('GCoordSystem' as varchar(255)) as TableName";
  3656. sGATStart = strstr(sOrigName, sName);
  3657. ilen = sGATStart - sOrigName;
  3658. if(ilen > 0)
  3659. {
  3660. int il1 = ilen;
  3661. ilen += (strlen(sGATAlias) + 20); // strlen("() as GAliasTable");
  3662. if(sRest) ilen += strlen(sRest);
  3663. sOpenStr = (LPSTR)malloc((ilen + 1)*sizeof(char));
  3664. strncpy(sOpenStr, sOrigName, il1);
  3665. sOpenStr[il1] = 0;
  3666. strcat(sOpenStr, "(");
  3667. strcat(sOpenStr, sGATAlias);
  3668. strcat(sOpenStr, ") as GAliasTable");
  3669. if(sRest) strcat(sOpenStr, sRest);
  3670. }
  3671. else
  3672. {
  3673. ilen = 35; // strlen("select * from () as GAliasTable");
  3674. // this should not happen, but for sure:
  3675. ilen += strlen(sGATAlias);
  3676. if(sRest) ilen += strlen(sRest);
  3677. sOpenStr = (LPSTR)malloc((ilen + 1)*sizeof(char));
  3678. strcpy(sOpenStr, "select * from (");
  3679. strcat(sOpenStr, sGATAlias);
  3680. strcat(sOpenStr, ") as GAliasTable");
  3681. // again this should not happen, but for sure:
  3682. if(sRest) strcat(sOpenStr, sRest);
  3683. }
  3684. }
  3685. }
  3686. else if(stricmp(sName, "FieldLookup") == 0)
  3687. {
  3688. // another hack for GI Toolkit
  3689. m_bIsMetatable = true;
  3690. LPSTR sTblName = strstr(sOrigName, " FieldLookup ");
  3691. if(sTblName)
  3692. {
  3693. int slen = (sTblName - sOrigName) +
  3694. strlen(pConnStruct->sFieldLookup) + strlen(sTblName) - 10;
  3695. sOpenStr = (LPSTR)malloc(slen*sizeof(char));
  3696. strncpy(sOpenStr, sOrigName, sTblName - sOrigName + 1);
  3697. sOpenStr[sTblName - sOrigName + 1] = 0;
  3698. sTblName += 12;
  3699. strcat(sOpenStr, pConnStruct->sFieldLookup);
  3700. strcat(sOpenStr, sTblName);
  3701. }
  3702. else sOpenStr = CopyWithSelect(pConnStruct->sFieldLookup, false, NULL);
  3703. }
  3704. else if(stricmp(sName, "AttributeProperties") == 0)
  3705. {
  3706. // another hack for GI Toolkit
  3707. m_bIsMetatable = true;
  3708. LPSTR sTblName = strstr(sOrigName, " AttributeProperties ");
  3709. if(sTblName)
  3710. {
  3711. int slen = (sTblName - sOrigName) +
  3712. strlen(pConnStruct->sAttributeProperties) + strlen(sTblName) - 18;
  3713. sOpenStr = (LPSTR)malloc(slen*sizeof(char));
  3714. strncpy(sOpenStr, sOrigName, sTblName - sOrigName + 1);
  3715. sOpenStr[sTblName - sOrigName + 1] = 0;
  3716. sTblName += 20;
  3717. strcat(sOpenStr, pConnStruct->sAttributeProperties);
  3718. strcat(sOpenStr, sTblName);
  3719. }
  3720. else sOpenStr = CopyWithSelect(pConnStruct->sAttributeProperties,
  3721. false, pTBFlds);
  3722. }
  3723. else if(stricmp(sName, pConnStruct->sCoordSystems) == 0) // coordsystem
  3724. {
  3725. m_bIsMetatable = true;
  3726. m_iSpecType = 2;
  3727. if(pConnStruct->iConnStat > icsINGRMetaIncomplete)
  3728. sOpenStr = CopyWithSelect(sOrigName, false, NULL);
  3729. else
  3730. {
  3731. m_bUpdatable = false;
  3732. LPSTR sTmp = GetCSSelectString(&m_pConnStruct->cCSTable, &bNull,
  3733. m_psParams, m_piParLenght, plDataTypes, pConnStruct->lDBEnc,
  3734. iCSRows);
  3735. sGATStart = strstr(sOrigName, sName);
  3736. ilen = sGATStart - sOrigName;
  3737. if(ilen > 0)
  3738. {
  3739. int il1 = ilen;
  3740. ilen += (strlen(sTmp) + strlen(sName) + 8); // strlen("() as ");
  3741. if(bNull) ilen += 16; // " where 0 = 1"
  3742. else if(sRest) ilen += strlen(sRest);
  3743. sOpenStr = (LPSTR)malloc((ilen + 1)*sizeof(char));
  3744. strncpy(sOpenStr, sOrigName, il1);
  3745. sOpenStr[il1] = 0;
  3746. strcat(sOpenStr, "(");
  3747. strcat(sOpenStr, sTmp);
  3748. strcat(sOpenStr, ") as ");
  3749. strcat(sOpenStr, sName);
  3750. if(bNull) strcat(sOpenStr, " where 0 = 1");
  3751. else if(sRest) strcat(sOpenStr, sRest);
  3752. }
  3753. else
  3754. {
  3755. ilen = 24 + strlen(sName); // strlen("select * from () as ");
  3756. // this should not happen, but for sure:
  3757. ilen += strlen(sTmp);
  3758. if(bNull) ilen += 16;
  3759. else if(sRest) ilen += strlen(sRest);
  3760. sOpenStr = (LPSTR)malloc((ilen + 1)*sizeof(char));
  3761. strcpy(sOpenStr, "select * from (");
  3762. strcat(sOpenStr, sTmp);
  3763. strcat(sOpenStr, ") as ");
  3764. strcat(sOpenStr, sName);
  3765. if(bNull) strcat(sOpenStr, " where 0 = 1");
  3766. // again this should not happen, but for sure:
  3767. else if(sRest) strcat(sOpenStr, sRest);
  3768. }
  3769. free(sTmp);
  3770. }
  3771. }
  3772. else if(stricmp(sName, pConnStruct->sPickLists) == 0) // gpicklist
  3773. {
  3774. m_bIsMetatable = true;
  3775. LPSTR sFtrName = NULL;
  3776. LPSTR sFtrEnd = NULL;
  3777. LPSTR sWhere = strstr(sOrigName, " WHERE ");
  3778. if(!sWhere) sWhere = strstr(sOrigName, " where ");
  3779. if(!sWhere) sWhere = strstr(sOrigName, " Where ");
  3780. if(sWhere)
  3781. {
  3782. sWhere += 6;
  3783. sFtrName = strstr(sWhere, " FeatureName");
  3784. if(!sFtrName) sFtrName = strstr(sWhere, " FEATURENAME");
  3785. if(!sFtrName) sFtrName = strstr(sWhere, " featurename");
  3786. }
  3787. if(sFtrName)
  3788. {
  3789. sFtrEnd = sFtrName + 12;
  3790. while((*sFtrEnd == ' ') && (*sFtrEnd != 0)) sFtrEnd++;
  3791. if(*sFtrEnd != '=') sFtrEnd = NULL;
  3792. }
  3793. if(sFtrEnd)
  3794. {
  3795. int slen = sFtrName - sOrigName + strlen(sFtrEnd) + 21; // " upper(featurename) "
  3796. sOpenStr = (LPSTR)malloc(slen*sizeof(char));
  3797. strncpy(sOpenStr, sOrigName, sFtrName - sOrigName);
  3798. sOpenStr[sFtrName - sOrigName] = 0;
  3799. strcat(sOpenStr, " upper(featurename) ");
  3800. strcat(sOpenStr, sFtrEnd);
  3801. }
  3802. else sOpenStr = CopyWithSelect(sOrigName, false, NULL);
  3803. }
  3804. else
  3805. {
  3806. m_bIsMetatable = IsMetatable(pConnStruct, sName);
  3807. SetSP(m_pConnStruct->pConn, bTrans);
  3808. //res = PQprepare(m_pConnStruct->pConn, m_sStmtName, sOrigName,
  3809. // iGeomParOffs, plDataTypes);
  3810. sOpenStr = CopyWithSelect(sOrigName, !m_bIsMetatable, pTBFlds);
  3811. res = PQprepare(m_pConnStruct->pConn, m_sStmtName, sOpenStr,
  3812. iGeomParOffs, plDataTypes);
  3813. if(PQresultStatus(res) != PGRES_COMMAND_OK)
  3814. {
  3815. // The query is most likely not in the for select ... from
  3816. // so skip the error logging
  3817. //m_pErrors->HandlePQError(m_pConnStruct->pConn,
  3818. // m_pConnStruct->hInstance);
  3819. PQclear(res);
  3820. RollbackSP(m_pConnStruct->pConn, bTrans);
  3821. }
  3822. else
  3823. {
  3824. PQclear(res);
  3825. ReleaseSP(m_pConnStruct->pConn, bTrans);
  3826. SetSP(m_pConnStruct->pConn, bTrans);
  3827. res = PQdescribePrepared(m_pConnStruct->pConn, m_sStmtName);
  3828. if(PQresultStatus(res) != PGRES_COMMAND_OK)
  3829. {
  3830. PQclear(res);
  3831. RollbackSP(m_pConnStruct->pConn, bTrans);
  3832. }
  3833. else
  3834. {
  3835. m_iCtidCol = GetCtidColumn(res);
  3836. PQclear(res);
  3837. ReleaseSP(m_pConnStruct->pConn, bTrans);
  3838. }
  3839. char sCmd[64];
  3840. sprintf(sCmd, "deallocate %s", m_sStmtName);
  3841. SetSP(m_pConnStruct->pConn, bTrans);
  3842. res = PQexec(m_pConnStruct->pConn, sCmd);
  3843. if(PQresultStatus(res) != PGRES_COMMAND_OK)
  3844. {
  3845. PQclear(res);
  3846. RollbackSP(m_pConnStruct->pConn, bTrans);
  3847. }
  3848. else
  3849. {
  3850. PQclear(res);
  3851. ReleaseSP(m_pConnStruct->pConn, bTrans);
  3852. }
  3853. }
  3854. if(sGeomName)
  3855. {
  3856. //free(sOpenStr);
  3857. //sOpenStr = ParseSQLWithSpatialFilter(sOrigName, SpatialOp,
  3858. // SpatialGeometryFilter, sGeomName, ulSrid,
  3859. // &m_psParams[iGeomParOffs], &m_piParLenght[iGeomParOffs], m_iParams);
  3860. LPSTR sNewStr = ParseSQLWithSpatialFilter(sOpenStr, SpatialOp,
  3861. SpatialGeometryFilter, sGeomName, ulSrid,
  3862. &m_psParams[iGeomParOffs], &m_piParLenght[iGeomParOffs], m_iParams);
  3863. free(sOpenStr);
  3864. sOpenStr = sNewStr;
  3865. }
  3866. }
  3867. bool bModLog = (stricmp(pConnStruct->sModLog, sName) == 0);
  3868. if(sGeomName) free(sGeomName);
  3869. if(m_bUpdatable && (m_iCtidCol < 0))
  3870. {
  3871. LPSTR sOldOpen = sOpenStr;
  3872. LPSTR sSelect = strstr(sOldOpen, "SELECT ");
  3873. if(!sSelect) sSelect = strstr(sOldOpen, "select ");
  3874. if(!sSelect) sSelect = strstr(sOldOpen, "Select ");
  3875. if(sSelect)
  3876. {
  3877. sSelect += 7;
  3878. ilen = strlen(sOldOpen) + 8; // "ctid, "
  3879. sOpenStr = (LPSTR)malloc(ilen*sizeof(char));
  3880. strncpy(sOpenStr, sOldOpen, sSelect - sOldOpen);
  3881. sOpenStr[sSelect - sOldOpen] = 0;
  3882. strcat(sOpenStr, "ctid, ");
  3883. strcat(sOpenStr, sSelect);
  3884. }
  3885. else
  3886. {
  3887. ilen = strlen(sOldOpen) + 24; // "select ctid, * from "
  3888. sOpenStr = (LPSTR)malloc(ilen*sizeof(char));
  3889. strcpy(sOpenStr, "select ctid, * from ");
  3890. strcat(sOpenStr, sOldOpen);
  3891. }
  3892. SetSP(m_pConnStruct->pConn, bTrans);
  3893. res = PQprepare(m_pConnStruct->pConn, m_sStmtName, sOpenStr,
  3894. m_iParams, plDataTypes);
  3895. if(PQresultStatus(res) != PGRES_COMMAND_OK)
  3896. {
  3897. WritePQErrorToLog("Spot 5: ", m_pConnStruct->pConn);
  3898. PQclear(res);
  3899. RollbackSP(m_pConnStruct->pConn, bTrans);
  3900. m_bUpdatable = false;
  3901. free(sOpenStr);
  3902. sOpenStr = sOldOpen;
  3903. }
  3904. else
  3905. {
  3906. m_iCtidCol = 0;
  3907. PQclear(res);
  3908. ReleaseSP(m_pConnStruct->pConn, bTrans);
  3909. m_iOpenStatus = 1;
  3910. free(sOldOpen);
  3911. }
  3912. }
  3913. if(m_iOpenStatus < 1)
  3914. {
  3915. SetSP(m_pConnStruct->pConn, bTrans);
  3916. res = PQprepare(m_pConnStruct->pConn, m_sStmtName, sOpenStr,
  3917. m_iParams, plDataTypes);
  3918. if(PQresultStatus(res) != PGRES_COMMAND_OK)
  3919. {
  3920. m_pErrors->HandlePQError(m_pConnStruct->pConn,
  3921. m_pConnStruct->hInstance);
  3922. PQclear(res);
  3923. RollbackSP(m_pConnStruct->pConn, bTrans);
  3924. }
  3925. else
  3926. {
  3927. PQclear(res);
  3928. ReleaseSP(m_pConnStruct->pConn, bTrans);
  3929. m_iOpenStatus = 1;
  3930. }
  3931. }
  3932. if(m_iOpenStatus > 0)
  3933. {
  3934. m_pPGres = PQexecPrepared(m_pConnStruct->pConn, m_sStmtName, m_iParams,
  3935. m_psParams, m_piParLenght, m_piParType, 1);
  3936. if(PQresultStatus(m_pPGres) != PGRES_TUPLES_OK)
  3937. {
  3938. m_pErrors->HandlePQError(m_pConnStruct->pConn,
  3939. m_pConnStruct->hInstance);
  3940. PQclear(m_pPGres);
  3941. m_pPGres = NULL;
  3942. m_iOpenStatus = 0;
  3943. }
  3944. }
  3945. if(plDataTypes) free(plDataTypes);
  3946. free(sOrigName);
  3947. if(m_iOpenStatus < 1)
  3948. {
  3949. for(int i = 0; i < m_iParams; i++)
  3950. {
  3951. if(m_psParams[i]) free(m_psParams[i]);
  3952. }
  3953. free(m_psParams);
  3954. free(m_piParType);
  3955. free(m_piParLenght);
  3956. free(sOpenStr);
  3957. free(sName);
  3958. free(wsName);
  3959. return(false);
  3960. }
  3961. #if DBGLEVEL > 0
  3962. WriteLogFile("GRecordset::SetAttributes\r\n", true);
  3963. WriteLogFile(" request: ", false);
  3964. WriteLogFile(sOpenStr, false);
  3965. WriteLogFile("\r\n", false);
  3966. #endif // DBGLEVEL
  3967. free(sOpenStr);
  3968. m_iRowSize = PQnfields(m_pPGres);
  3969. m_vBuffer.pvData = (VARIANT*)malloc(m_iRowSize*sizeof(VARIANT));
  3970. for(int i = 0; i < m_iRowSize; i++)
  3971. {
  3972. VariantInit(&m_vBuffer.pvData[i]);
  3973. }
  3974. m_plTypes = (Oid*)malloc(m_iRowSize*sizeof(Oid));
  3975. DescribeFieldTypes(m_pPGres, m_iRowSize, m_plTypes);
  3976. if(m_bUpdatable)
  3977. {
  3978. m_lTblOid = PQftable(m_pPGres, 0);
  3979. m_pTblDef = pGTbls->FindByOid(m_lTblOid);
  3980. if(!m_pTblDef) m_bUpdatable = false;
  3981. }
  3982. m_pFields->BuildFromConn(m_pConnStruct, m_pPGres, pGTbls, m_iCtidCol,
  3983. m_plTypes, m_iSpecType, m_bIsMetatable);
  3984. free(sName);
  3985. free(wsName);
  3986. m_piGeomDims = (int*)malloc(m_pFields->GetCount()*sizeof(int));
  3987. m_pFields->UpdateFields(m_piGeomDims);
  3988. if((m_iSpecType > 0) && (pConnStruct->iConnStat < icsINGRMetadata))
  3989. {
  3990. m_vType.vt = VT_I4;
  3991. m_vType.lVal = 4;
  3992. m_voptions.vt = VT_I4;
  3993. m_voptions.lVal = 4;
  3994. }
  3995. m_lRecordCount = PQntuples(m_pPGres);
  3996. m_lBufSize = m_lRecordCount;
  3997. if(m_lRecordCount > 0)
  3998. {
  3999. m_bBOF = false;
  4000. m_bEOF = false;
  4001. m_lCurPos = 0;
  4002. }
  4003. else
  4004. {
  4005. m_bBOF = true;
  4006. m_bEOF = true;
  4007. m_lCurPos = -1;
  4008. }
  4009. SyncFields();
  4010. // this is very tricky part and we are not sure whether it is even needed
  4011. /*if(bModLog && (m_iCtidCol > -1))
  4012. {
  4013. m_pParent->BroadcastDataChanges(this);
  4014. }*/
  4015. return(true);
  4016. }
  4017. void GRecordset::BroadcastCSChange(LPWSTR wsOldCSGuid, LPWSTR wsNewCSGuid)
  4018. {
  4019. m_pFields->BroadcastCSChange(wsOldCSGuid, wsNewCSGuid);
  4020. return;
  4021. }
  4022. /*void GRecordset::RecordDeleted(LPWSTR wsTblName, int iCtidLen, LPSTR sCtid)
  4023. {
  4024. if(!m_pTblDef) return;
  4025. if(wcscmp(wsTblName, m_pTblDef->GetNamePtr()) != 0) return;
  4026. PBufRow pRow = m_pBuffer->FindByCtid(iCtidLen, sCtid);
  4027. if(pRow)
  4028. {
  4029. pRow->iState = 2;
  4030. if(pRow == m_pBuffer->GetCurRow()) m_pFields->SetBuffer(NULL);
  4031. }
  4032. return;
  4033. }*/
  4034. bool GRecordset::GetIsMetatable()
  4035. {
  4036. return(m_bIsMetatable);
  4037. }
  4038. /*long GRecordset::FindByCtid(int iCtidLen, LPSTR pCtid)
  4039. {
  4040. if(m_iCtidCol < 0) return -2;
  4041. long lPos = 0;
  4042. bool bFound = false;
  4043. int iLen;
  4044. while(!bFound && (lPos < m_lBufSize))
  4045. {
  4046. if(!PQgetisnull(m_pPGres, lPos, m_iCtidCol))
  4047. {
  4048. iLen = PQgetlength(m_pPGres, lPos, m_iCtidCol);
  4049. if(iCtidLen == iLen)
  4050. {
  4051. bFound = (memcmp(PQgetvalue(m_pPGres, lPos, m_iCtidCol), pCtid, iCtidLen) == 0);
  4052. }
  4053. }
  4054. lPos++;
  4055. }
  4056. return bFound ? lPos - 1 : -1;
  4057. }
  4058. void GRecordset::UpdateWithDBChanges(int iModType, VARIANT *pvModTable,
  4059. VARIANT *pvModData, PGresult *res, LPSTR sSchema, LPSTR sTable,
  4060. bool bHasCtid, int iCtidLen, LPSTR pCtid)
  4061. {
  4062. if(!m_pPGres) return;
  4063. long lRecPos = -1;
  4064. if(bHasCtid) lRecPos = FindByCtid(iCtidLen, pCtid);
  4065. bool bHasKey = true;
  4066. int i = 2;
  4067. int piKeyMap[10];
  4068. int piFldMap[10];
  4069. char sNameBuf[64];
  4070. while(bHasKey && (i < 12))
  4071. {
  4072. piKeyMap[i - 2] = -1;
  4073. piFldMap[i - 2] = -1;
  4074. if(pvModTable[i].vt == VT_LPSTR)
  4075. {
  4076. piKeyMap[i - 2] =
  4077. m_pFields->HasField(sSchema, sTable, pvModTable[i].pcVal);
  4078. strcpy(sNameBuf, "\"");
  4079. strcat(sNameBuf, pvModTable[i].pcVal);
  4080. strcat(sNameBuf, "\"");
  4081. piFldMap[i - 2] = PQfnumber(res, sNameBuf);
  4082. bHasKey = (piKeyMap[i - 2] > -1) && (piFldMap[i - 2] > -1);
  4083. }
  4084. i++;
  4085. }
  4086. if(!bHasKey) return;
  4087. int nCols = PQnfields(res);
  4088. int iStart = 0;
  4089. if(bHasCtid) iStart++;
  4090. int iPos;
  4091. long lPos;
  4092. int iValSize;
  4093. LPSTR sVal;
  4094. CGeomInfo cgInfo;
  4095. GField *pFld;
  4096. long lParams[8];
  4097. if(iModType == 1) // new record
  4098. {
  4099. lPos = m_lBufSize;
  4100. m_lBufSize++;
  4101. m_lRecordCount++;
  4102. if(m_iCtidCol > -1)
  4103. {
  4104. PQsetvalue(m_pPGres, lPos, m_iCtidCol, pCtid, iCtidLen);
  4105. }
  4106. for(i = iStart; i < nCols; i++)
  4107. {
  4108. iPos = m_pFields->HasField(sSchema, sTable, PQfname(res, i));
  4109. if(iPos > - 1)
  4110. {
  4111. pFld = (GField*)m_pFields->GetItem(iPos);
  4112. pFld->FillGeomInfo(&cgInfo);
  4113. iValSize = VarToBinaryLen(pvModData[i - iStart], m_plTypes[iPos],
  4114. m_pConnStruct->lDBEnc, &cgInfo, lParams);
  4115. if(iValSize > 0)
  4116. {
  4117. sVal = (LPSTR)malloc(iValSize + 1);
  4118. VarToBinaryBuf(pvModData[i - iStart], m_plTypes[iPos], m_pConnStruct->lDBEnc,
  4119. sVal, iValSize, &cgInfo, lParams, m_pConnStruct->iCurrDigits);
  4120. }
  4121. else sVal = NULL;
  4122. if(m_iCtidCol > -1)
  4123. {
  4124. if(iPos >= m_iCtidCol) iPos++;
  4125. }
  4126. PQsetvalue(m_pPGres, lPos, iPos, sVal, iValSize);
  4127. if(sVal) free(sVal);
  4128. }
  4129. }
  4130. SyncFields();
  4131. return;
  4132. }
  4133. lPos = 0;
  4134. if(m_iCtidCol > -1)
  4135. {
  4136. while(PQgetisnull(m_pPGres, lPos, m_iCtidCol) && (lPos < m_lBufSize - 1))
  4137. {
  4138. lPos++;
  4139. }
  4140. if(PQgetisnull(m_pPGres, lPos, m_iCtidCol)) return;
  4141. }
  4142. if(lPos >= m_lBufSize) return;
  4143. bool bFound = true;
  4144. CBufRow cRow;
  4145. cRow.pvData = (VARIANT*)malloc(m_iRowSize*sizeof(VARIANT));
  4146. for(i = 0; i < m_iRowSize; i++) VariantInit(&cRow.pvData[i]);
  4147. GetRow(lPos, &cRow);
  4148. i = 0;
  4149. while(bFound && (i < 10))
  4150. {
  4151. if(piKeyMap[i] > -1)
  4152. {
  4153. bFound = VarArgCompare(cRow.pvData[piKeyMap[i]],
  4154. pvModData[piFldMap[i] - iStart]);
  4155. }
  4156. i++;
  4157. }
  4158. while(!bFound && (lPos < m_lBufSize - 1))
  4159. {
  4160. lPos++;
  4161. if(GetRow(lPos, &cRow))
  4162. {
  4163. bFound = true;
  4164. i = 0;
  4165. while(bFound && (i < 10))
  4166. {
  4167. if(piKeyMap[i] > -1)
  4168. {
  4169. bFound = VarArgCompare(cRow.pvData[piKeyMap[i]],
  4170. pvModData[piFldMap[i] - iStart]);
  4171. }
  4172. i++;
  4173. }
  4174. }
  4175. }
  4176. for(i = 0; i < m_iRowSize; i++) VariantClear(&cRow.pvData[i]);
  4177. free(cRow.pvData);
  4178. if(!bFound) return;
  4179. if(iModType == 3)
  4180. {
  4181. if(m_iCtidCol > -1)
  4182. {
  4183. PQsetvalue(m_pPGres, lPos, m_iCtidCol, NULL, -1);
  4184. m_lRecordCount--;
  4185. if(m_lRecordCount < 1)
  4186. {
  4187. m_bBOF = true;
  4188. m_bEOF = true;
  4189. m_lCurPos = -1;
  4190. }
  4191. }
  4192. return;
  4193. }
  4194. if(m_iCtidCol > -1)
  4195. {
  4196. PQsetvalue(m_pPGres, lPos, m_iCtidCol, pCtid, iCtidLen);
  4197. }
  4198. for(i = iStart; i < nCols; i++)
  4199. {
  4200. iPos = m_pFields->HasField(sSchema, sTable, PQfname(res, i));
  4201. if(iPos > - 1)
  4202. {
  4203. pFld = (GField*)m_pFields->GetItem(iPos);
  4204. pFld->FillGeomInfo(&cgInfo);
  4205. iValSize = VarToBinaryLen(pvModData[i - iStart], m_plTypes[iPos],
  4206. m_pConnStruct->lDBEnc, &cgInfo, lParams);
  4207. if(iValSize > 0)
  4208. {
  4209. sVal = (LPSTR)malloc(iValSize + 1);
  4210. VarToBinaryBuf(pvModData[i - iStart], m_plTypes[iPos], m_pConnStruct->lDBEnc,
  4211. sVal, iValSize, &cgInfo, lParams, m_pConnStruct->iCurrDigits);
  4212. }
  4213. else sVal = NULL;
  4214. if(m_iCtidCol > -1)
  4215. {
  4216. if(iPos >= m_iCtidCol) iPos++;
  4217. }
  4218. PQsetvalue(m_pPGres, lPos, iPos, sVal, iValSize);
  4219. if(sVal) free(sVal);
  4220. }
  4221. }
  4222. SyncFields();
  4223. return;
  4224. }*/
  4225. int GRecordset::GetFieldsCount()
  4226. {
  4227. return m_iRowSize;
  4228. }
  4229. long GRecordset::GetBufSize()
  4230. {
  4231. return m_lBufSize;
  4232. }
  4233. bool GRecordset::GetRow(long lPos, PBufRow pBuf)
  4234. {
  4235. if(m_iCtidCol < 0)
  4236. {
  4237. GetRecordBin(m_pPGres, lPos, m_iRowSize, m_plTypes, pBuf->pvData,
  4238. m_pConnStruct->lDBEnc, m_pConnStruct->lGeomOid, m_piGeomDims,
  4239. m_pConnStruct->iCurrDigits);
  4240. return true;
  4241. }
  4242. if(PQgetisnull(m_pPGres, lPos, m_iCtidCol)) return false;
  4243. GetRecordCtid(m_pPGres, lPos, m_iRowSize + 1, m_iCtidCol, m_plTypes,
  4244. pBuf->pvData, m_pConnStruct->lDBEnc, pBuf->sCtid,
  4245. &pBuf->iCtidLen, m_pConnStruct->lGeomOid, m_piGeomDims,
  4246. m_pConnStruct->iCurrDigits);
  4247. return true;
  4248. }
  4249. // GRecordsets
  4250. GRecordsets::GRecordsets(IUnknown *pUnkOuter, ITypeLib *ALib) :
  4251. _IGCollection(false, pUnkOuter, ALib, 29)
  4252. {
  4253. #if DBGLEVEL > 1
  4254. WriteLogFile("GRecordsets::GRecordsets-1\r\n", true);
  4255. #endif // DBGLEVEL
  4256. m_pConnStruct = NULL;
  4257. m_pGTbls = NULL;
  4258. }
  4259. GRecordsets::GRecordsets(IUnknown *pUnkOuter, ITypeLib *ALib, int iIndex) :
  4260. _IGCollection(false, pUnkOuter, ALib, iIndex)
  4261. {
  4262. #if DBGLEVEL > 1
  4263. WriteLogFile("GRecordsets::GRecordsets-2\r\n", true);
  4264. #endif // DBGLEVEL
  4265. m_pConnStruct = NULL;
  4266. m_pGTbls = NULL;
  4267. }
  4268. GRecordsets::~GRecordsets()
  4269. {
  4270. #if DBGLEVEL > 1
  4271. WriteLogFile("GRecordsets::~GRecordsets\r\n", true);
  4272. #endif // DBGLEVEL
  4273. }
  4274. HRESULT GRecordsets::QueryInterface(REFIID iid, void **ppvObject)
  4275. {
  4276. #if DBGLEVEL > 2
  4277. WriteLogFile("GRecordsets::QueryInterface\r\n", true);
  4278. #endif // DBGLEVEL
  4279. HRESULT hres = _IGCollection::QueryInterface(iid, ppvObject);
  4280. if(hres != S_OK)
  4281. {
  4282. if(IsEqualIID(iid, DIID_GRecordsets))
  4283. {
  4284. hres = S_OK;
  4285. *ppvObject = this;
  4286. ((IUnknown*)*ppvObject)->AddRef();
  4287. }
  4288. else
  4289. {
  4290. hres = E_NOINTERFACE;
  4291. #if DBGLEVEL > 2
  4292. char buf[128];
  4293. FormatGuid(buf, "Unknown Interface: ", "\r\n", iid);
  4294. WriteLogFile(buf, true);
  4295. #endif // DBGLEVEL
  4296. }
  4297. }
  4298. return(hres);
  4299. }
  4300. ULONG GRecordsets::Release()
  4301. {
  4302. ULONG lRest;
  4303. IUnknown *pUnkOuter = m_pUnkOuter;
  4304. if(pUnkOuter) lRest = pUnkOuter->Release();
  4305. else lRest = --m_lRefCount;
  4306. #if DBGLEVEL > 2
  4307. char buf[64];
  4308. if(pUnkOuter) sprintf(buf, "GRecordsets::Release (aggregated) - %d\r\n", lRest);
  4309. else sprintf(buf, "GRecordsets::Release - %d\r\n", lRest);
  4310. WriteLogFile(buf, true);
  4311. #endif // DBGLEVEL
  4312. if(pUnkOuter || (lRest > 0)) return lRest;
  4313. delete(this);
  4314. return(0);
  4315. }
  4316. HRESULT GRecordsets::get_Item(VARIANT index, GRecordset* *ppGRecordset)
  4317. {
  4318. ValidateVariant(&index);
  4319. #if DBGLEVEL > 0
  4320. WriteLogFile("GRecordsets::get_Item\r\n", true);
  4321. #endif // DBGLEVEL
  4322. if(!ppGRecordset) return(E_POINTER);
  4323. // a "get_CollectionItem" method is probably an exception from the COM
  4324. // standards - it can accept uninitialized return values, similary as
  4325. // QueryInterface method
  4326. //if(*ppGRecordset) (*ppGRecordset)->Release();
  4327. *ppGRecordset = NULL;
  4328. bool bFound = false;
  4329. int i = 0;
  4330. GRecordset *pRS = NULL;
  4331. if(index.vt == VT_BSTR)
  4332. {
  4333. while(!bFound && (i < m_iDataLen))
  4334. {
  4335. pRS = (GRecordset*)m_pData[i++];
  4336. bFound = (wcsncmp(pRS->GetNamePtr(), index.bstrVal, 256) == 0);
  4337. }
  4338. if(!bFound) return(S_FALSE);
  4339. }
  4340. else if(index.vt == (VT_BYREF | VT_BSTR))
  4341. {
  4342. while(!bFound && (i < m_iDataLen))
  4343. {
  4344. pRS = (GRecordset*)m_pData[i++];
  4345. bFound = (wcsncmp(pRS->GetNamePtr(), *index.pbstrVal, 256) == 0);
  4346. }
  4347. if(!bFound) return(S_FALSE);
  4348. }
  4349. else
  4350. {
  4351. i = VarToInt(index);
  4352. pRS = (GRecordset*)_IGCollection::GetItem(i);
  4353. if(!pRS) return(S_FALSE);
  4354. }
  4355. pRS->AddRef();
  4356. *ppGRecordset = pRS;
  4357. return(S_OK);
  4358. }
  4359. HRESULT GRecordsets::Append(GRecordset* pObject)
  4360. {
  4361. #if DBGLEVEL > 0
  4362. WriteLogFile("GRecordsets::Append\r\n", true);
  4363. #endif // DBGLEVEL
  4364. return(E_NOTIMPL);
  4365. }
  4366. HRESULT GRecordsets::Remove(VARIANT vIndex)
  4367. {
  4368. #if DBGLEVEL > 0
  4369. WriteLogFile("GRecordsets::Remove\r\n", true);
  4370. #endif // DBGLEVEL
  4371. return(E_NOTIMPL);
  4372. }
  4373. void GRecordsets::ClearAll()
  4374. {
  4375. _IGCollection::ClearAll();
  4376. return;
  4377. }
  4378. GRecordset* GRecordsets::FindRecordset(BSTR Name, VARIANT Type, VARIANT options,
  4379. VARIANT SpatialOp, VARIANT SpatialGeometryFilter, VARIANT GeometryFieldName,
  4380. GTableDefs *pGTbls, PConnStruct pConnStruct)
  4381. {
  4382. m_pConnStruct = pConnStruct;
  4383. m_pGTbls = pGTbls;
  4384. GRecordset *pRes = new GRecordset(this, m_pTypeLib);
  4385. if(pRes->SetAttributes(Name, Type, options, SpatialOp,
  4386. SpatialGeometryFilter, GeometryFieldName, pGTbls, pConnStruct))
  4387. {
  4388. pRes->AddRef();
  4389. _IGCollection::AddItem(pRes);
  4390. }
  4391. else
  4392. {
  4393. pRes->AddRef();
  4394. pRes->Release();
  4395. pRes = NULL;
  4396. }
  4397. return(pRes);
  4398. }
  4399. void GRecordsets::BroadcastCSChange(LPWSTR wsOldCSGuid, LPWSTR wsNewCSGuid)
  4400. {
  4401. GRecordset *pRS;
  4402. for(int i = 0; i < m_iDataLen; i++)
  4403. {
  4404. pRS = (GRecordset*)m_pData[i];
  4405. pRS->BroadcastCSChange(wsOldCSGuid, wsNewCSGuid);
  4406. }
  4407. return;
  4408. }
  4409. /*void GRecordsets::BroadcastDelRecord(LPWSTR wsTblName, int iCtidLen, LPSTR sCtid)
  4410. {
  4411. GRecordset *pRS;
  4412. for(int i = 0; i < m_iDataLen; i++)
  4413. {
  4414. pRS = (GRecordset*)m_pData[i];
  4415. pRS->RecordDeleted(wsTblName, iCtidLen, sCtid);
  4416. }
  4417. return;
  4418. }*/
  4419. void GRecordsets::DeletePtr(GRecordset *pRS)
  4420. {
  4421. bool bFound = false;
  4422. int i = 0;
  4423. while(!bFound && (i < m_iDataLen))
  4424. {
  4425. bFound = (pRS == m_pData[i++]);
  4426. }
  4427. if(bFound) _IGCollection::DeleteItem(i - 1);
  4428. return;
  4429. }
  4430. /*void GRecordsets::ProcessModLogRec(PBufRow pRow)
  4431. {
  4432. int ilen = 64 + strlen(m_pConnStruct->sModTables);
  4433. LPSTR sSql = (LPSTR)malloc(ilen*sizeof(char));
  4434. sprintf(sSql, "select * from %s where modifiedtableid = %ld",
  4435. m_pConnStruct->sModTables, VarToLong(pRow->pvData[2]));
  4436. bool bTrans = (m_pConnStruct->iSysTrans | m_pConnStruct->iGdoTrans);
  4437. SetSP(m_pConnStruct->pConn, bTrans);
  4438. PGresult *res = PQexec(m_pConnStruct->pConn, sSql);
  4439. free(sSql);
  4440. if(PQresultStatus(res) != PGRES_TUPLES_OK)
  4441. {
  4442. WritePQErrorToLog("Spot 87: ", m_pConnStruct->pConn);
  4443. PQclear(res);
  4444. RollbackSP(m_pConnStruct->pConn, bTrans);
  4445. return;
  4446. }
  4447. long nrows = PQntuples(res);
  4448. if(nrows < 1)
  4449. {
  4450. PQclear(res);
  4451. ReleaseSP(m_pConnStruct->pConn, bTrans);
  4452. return;
  4453. }
  4454. VARIANT pvRes[12];
  4455. for(int i = 0; i < 12; i++) VariantInit(&pvRes[i]);
  4456. Oid plType[12];
  4457. DescribeFieldTypes(res, 12, plType);
  4458. GetRecord(res, 0, 12, plType, pvRes, m_pConnStruct->sDecSep);
  4459. PQclear(res);
  4460. ReleaseSP(m_pConnStruct->pConn, bTrans);
  4461. if(pvRes[1].vt != VT_LPSTR) return;
  4462. if(pvRes[2].vt != VT_LPSTR) return;
  4463. int iFldLen = strlen(pvRes[2].pcVal);
  4464. if(iFldLen < 1) return;
  4465. int nParams = 1;
  4466. char* ppParams[10];
  4467. if(pRow->pvData[3].vt == VT_BSTR)
  4468. ppParams[0] = WCharToDBStr(pRow->pvData[3].bstrVal, m_pConnStruct->lDBEnc);
  4469. else ppParams[0] = NULL;
  4470. ilen = 40 + strlen(pvRes[1].pcVal) + iFldLen;
  4471. for(int i = 3; i < 12; i++)
  4472. {
  4473. if(pvRes[i].vt == VT_LPSTR)
  4474. {
  4475. iFldLen = strlen(pvRes[i].pcVal);
  4476. if(iFldLen > 0)
  4477. {
  4478. ilen += (iFldLen + 12);
  4479. if(pRow->pvData[i + 1].vt == VT_LPSTR)
  4480. ppParams[nParams] = WCharToDBStr(pRow->pvData[i + 1].bstrVal,
  4481. m_pConnStruct->lDBEnc);
  4482. else ppParams[nParams] = NULL;
  4483. nParams++;
  4484. }
  4485. }
  4486. }
  4487. LPSTR sTblName = pvRes[1].pcVal;
  4488. LPSTR sSchema = NULL;
  4489. LPSTR sDot = strchr(sTblName, '.');
  4490. if(sDot)
  4491. {
  4492. iFldLen = sDot - sTblName;
  4493. sSchema = (LPSTR)malloc((iFldLen + 1)*sizeof(char));
  4494. strncpy(sSchema, sTblName, iFldLen);
  4495. sSchema[iFldLen] = 0;
  4496. sTblName = sDot + 1;
  4497. }
  4498. char sNumBuf[32];
  4499. int iCtidCol = 0;
  4500. sSql = (LPSTR)malloc(ilen*sizeof(char));
  4501. if(sSchema)
  4502. sprintf(sSql, "select ctid, * from \"%s\".\"%s\" where \"%s\" = $1",
  4503. sSchema, sTblName, pvRes[2].pcVal);
  4504. else
  4505. sprintf(sSql, "select ctid, * from \"%s\" where \"%s\" = $1", sTblName,
  4506. pvRes[2].pcVal);
  4507. for(int i = 3; i < 12; i++)
  4508. {
  4509. if(pvRes[i].vt == VT_LPSTR)
  4510. {
  4511. strcat(sSql, " and \"");
  4512. strcat(sSql, pvRes[i].pcVal);
  4513. sprintf(sNumBuf, "\" = $%d", i - 1);
  4514. strcat(sSql, sNumBuf);
  4515. }
  4516. }
  4517. SetSP(m_pConnStruct->pConn, bTrans);
  4518. res = PQexecParams(m_pConnStruct->pConn, sSql, nParams, NULL, ppParams,
  4519. NULL, NULL, 1);
  4520. if(PQresultStatus(res) != PGRES_TUPLES_OK)
  4521. {
  4522. PQclear(res);
  4523. RollbackSP(m_pConnStruct->pConn, bTrans);
  4524. iCtidCol = -1;
  4525. if(sSchema)
  4526. sprintf(sSql, "select * from \"%s\".\"%s\" where \"%s\" = $1",
  4527. sSchema, sTblName, pvRes[2].pcVal);
  4528. else
  4529. sprintf(sSql, "select * from \"%s\" where \"%s\" = $1", sTblName,
  4530. pvRes[2].pcVal);
  4531. for(int i = 3; i < 12; i++)
  4532. {
  4533. if(pvRes[i].vt == VT_LPSTR)
  4534. {
  4535. strcat(sSql, " and \"");
  4536. strcat(sSql, pvRes[i].pcVal);
  4537. sprintf(sNumBuf, "\" = $%d", i - 1);
  4538. strcat(sSql, sNumBuf);
  4539. }
  4540. }
  4541. SetSP(m_pConnStruct->pConn, bTrans);
  4542. res = PQexecParams(m_pConnStruct->pConn, sSql, nParams, NULL, ppParams,
  4543. NULL, NULL, 1);
  4544. }
  4545. free(sSql);
  4546. for(int i = 0; i < nParams; i++)
  4547. {
  4548. if(ppParams[i]) free(ppParams[i]);
  4549. }
  4550. if(PQresultStatus(res) != PGRES_TUPLES_OK)
  4551. {
  4552. PQclear(res);
  4553. RollbackSP(m_pConnStruct->pConn, bTrans);
  4554. WritePQErrorToLog("Spot 88: ", m_pConnStruct->pConn);
  4555. return;
  4556. }
  4557. nrows = PQntuples(res);
  4558. if(nrows < 1)
  4559. {
  4560. PQclear(res);
  4561. ReleaseSP(m_pConnStruct->pConn, bTrans);
  4562. return;
  4563. }
  4564. int iModType = VarToInt(pRow->pvData[1]);
  4565. int ncols = PQnfields(res);
  4566. Oid *plTypes = (Oid*)malloc(ncols*sizeof(Oid));
  4567. DescribeFieldTypes(res, ncols, plTypes);
  4568. GFields *pFields = new GFields(NULL, m_pTypeLib);
  4569. pFields->AddRef();
  4570. pFields->BuildFromConn(m_pConnStruct, res, m_pGTbls, iCtidCol, plTypes,
  4571. 0, false);
  4572. int *piGeomDims = (int*)malloc(pFields->GetCount()*sizeof(int));
  4573. pFields->UpdateFields(piGeomDims);
  4574. int iCtidLen = 0;
  4575. char sCtid[16];
  4576. int iRowSize = ncols;
  4577. if(iCtidCol > -1) iRowSize--;
  4578. VARIANT *pvData = (VARIANT*)malloc(iRowSize*sizeof(VARIANT));
  4579. for(int i = 0; i < iRowSize; i++) VariantInit(&pvData[i]);
  4580. if(iCtidCol < 0)
  4581. GetRecordBin(res, 0, ncols, plTypes, pvData, m_pConnStruct->lDBEnc,
  4582. m_pConnStruct->lGeomOid, piGeomDims, m_pConnStruct->iCurrDigits);
  4583. else
  4584. GetRecordCtid(res, 0, ncols, iCtidCol, plTypes, pvData,
  4585. m_pConnStruct->lDBEnc, sCtid, &iCtidLen, m_pConnStruct->lGeomOid,
  4586. piGeomDims, m_pConnStruct->iCurrDigits);
  4587. GRecordset *pRS;
  4588. for(int i = 0; i < m_iDataLen; i++)
  4589. {
  4590. pRS = (GRecordset*)m_pData[i];
  4591. if(pRS) pRS->UpdateWithDBChanges(iModType, pvRes, pvData, res, sSchema,
  4592. sTblName, iCtidCol > -1, iCtidLen, sCtid);
  4593. }
  4594. PQclear(res);
  4595. ReleaseSP(m_pConnStruct->pConn, bTrans);
  4596. if(sSchema) free(sSchema);
  4597. for(int i = 0; i < iRowSize; i++) VariantClear(&pvData[i]);
  4598. free(pvData);
  4599. free(piGeomDims);
  4600. pFields->Release();
  4601. free(plTypes);
  4602. for(int i = 0; i < 12; i++)
  4603. {
  4604. if(pvRes[i].vt == VT_LPSTR) free(pvRes[i].pcVal);
  4605. }
  4606. return;
  4607. }
  4608. void GRecordsets::BroadcastDataChanges(GRecordset* pRS)
  4609. {
  4610. long lRecords = pRS->GetBufSize();
  4611. int iFlds = pRS->GetFieldsCount();
  4612. CBufRow cRow;
  4613. cRow.pvData = (VARIANT*)malloc(iFlds*sizeof(VARIANT));
  4614. for(int i = 0; i < iFlds; i++) VariantInit(&cRow.pvData[i]);
  4615. for(long l = 0; l < lRecords; l++)
  4616. {
  4617. if(pRS->GetRow(l, &cRow)) ProcessModLogRec(&cRow);
  4618. }
  4619. for(int i = 0; i < iFlds; i++) VariantClear(&cRow.pvData[i]);
  4620. free(cRow.pvData);
  4621. return;
  4622. }*/