rtlfield.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "platform.h"
  14. #include <math.h>
  15. #include <stdio.h>
  16. #include "jmisc.hpp"
  17. #include "jlib.hpp"
  18. #include "eclhelper.hpp"
  19. #include "eclrtl_imp.hpp"
  20. #include "rtlfield_imp.hpp"
  21. #include "rtlds_imp.hpp"
  22. #include "nbcd.hpp"
  23. static const char * queryXPath(const RtlFieldInfo * field)
  24. {
  25. const char * xpath = field->xpath;
  26. if (xpath)
  27. {
  28. const char * sep = strchr(xpath, xpathCompoundSeparatorChar);
  29. if (!sep)
  30. return xpath;
  31. return sep+1;
  32. }
  33. return field->name->str();
  34. }
  35. static bool hasOuterXPath(const RtlFieldInfo * field)
  36. {
  37. const char * xpath = field->xpath;
  38. assertex(xpath);
  39. return (*xpath != xpathCompoundSeparatorChar);
  40. }
  41. static void queryNestedOuterXPath(StringAttr & ret, const RtlFieldInfo * field)
  42. {
  43. const char * xpath = field->xpath;
  44. assertex(xpath);
  45. const char * sep = strchr(xpath, xpathCompoundSeparatorChar);
  46. assertex(sep);
  47. ret.set(xpath, (size32_t)(sep-xpath));
  48. }
  49. //-------------------------------------------------------------------------------------------------------------------
  50. class DummyFieldProcessor : public CInterfaceOf<IFieldProcessor>
  51. {
  52. public:
  53. virtual void processString(unsigned len, const char *value, const RtlFieldInfo * field) {}
  54. virtual void processBool(bool value, const RtlFieldInfo * field) {}
  55. virtual void processData(unsigned len, const void *value, const RtlFieldInfo * field) {}
  56. virtual void processInt(__int64 value, const RtlFieldInfo * field) {}
  57. virtual void processUInt(unsigned __int64 value, const RtlFieldInfo * field) {}
  58. virtual void processReal(double value, const RtlFieldInfo * field) {}
  59. virtual void processDecimal(const void *value, unsigned digits, unsigned precision, const RtlFieldInfo * field) {}
  60. virtual void processUDecimal(const void *value, unsigned digits, unsigned precision, const RtlFieldInfo * field) {}
  61. virtual void processUnicode(unsigned len, const UChar *value, const RtlFieldInfo * field) {}
  62. virtual void processQString(unsigned len, const char *value, const RtlFieldInfo * field) {}
  63. virtual void processUtf8(unsigned len, const char *value, const RtlFieldInfo * field) {}
  64. virtual bool processBeginSet(const RtlFieldInfo * field, unsigned numElements, bool isAll, const byte *data) { return false; }
  65. virtual bool processBeginDataset(const RtlFieldInfo * field, unsigned numRows) { return true; }
  66. virtual bool processBeginRow(const RtlFieldInfo * field) { return true; }
  67. virtual void processEndSet(const RtlFieldInfo * field) {}
  68. virtual void processEndDataset(const RtlFieldInfo * field) {}
  69. virtual void processEndRow(const RtlFieldInfo * field) {}
  70. };
  71. //-------------------------------------------------------------------------------------------------------------------
  72. size32_t RtlTypeInfoBase::size(const byte * self, const byte * selfrow) const
  73. {
  74. return length;
  75. }
  76. size32_t RtlTypeInfoBase::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  77. {
  78. rtlFailUnexpected();
  79. return 0;
  80. }
  81. size32_t RtlTypeInfoBase::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & out) const
  82. {
  83. rtlFailUnexpected();
  84. return 0;
  85. }
  86. size32_t RtlTypeInfoBase::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  87. {
  88. rtlFailUnexpected();
  89. return 0;
  90. }
  91. const char * RtlTypeInfoBase::queryLocale() const
  92. {
  93. return NULL;
  94. }
  95. const RtlFieldInfo * const * RtlTypeInfoBase::queryFields() const
  96. {
  97. return NULL;
  98. }
  99. const RtlTypeInfo * RtlTypeInfoBase::queryChildType() const
  100. {
  101. return NULL;
  102. }
  103. //-------------------------------------------------------------------------------------------------------------------
  104. size32_t RtlBoolTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  105. {
  106. builder.ensureCapacity(sizeof(bool)+offset, field->name->str());
  107. bool val = source.getBooleanResult(field);
  108. * (bool *) (builder.getSelf() + offset) = val;
  109. offset += sizeof(bool);
  110. return offset;
  111. }
  112. size32_t RtlBoolTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  113. {
  114. target.processBool(*(const bool *)self, field);
  115. return sizeof(bool);
  116. }
  117. size32_t RtlBoolTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  118. {
  119. target.outputBool(*(const bool *)self, queryXPath(field));
  120. return sizeof(bool);
  121. }
  122. //-------------------------------------------------------------------------------------------------------------------
  123. double RtlRealTypeInfo::value(const byte * self) const
  124. {
  125. if (length == 4)
  126. return *(const float *)self;
  127. return *(const double *)self;
  128. }
  129. size32_t RtlRealTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  130. {
  131. builder.ensureCapacity(length+offset, field->name->str());
  132. double val = source.getRealResult(field);
  133. byte *dest = builder.getSelf() + offset;
  134. if (length == 4)
  135. *(float *) dest = (float) val;
  136. else
  137. *(double *) dest = val;
  138. offset += length;
  139. return offset;
  140. }
  141. size32_t RtlRealTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  142. {
  143. target.processReal(value(self), field);
  144. return length;
  145. }
  146. size32_t RtlRealTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  147. {
  148. target.outputReal(value(self), queryXPath(field));
  149. return length;
  150. }
  151. //-------------------------------------------------------------------------------------------------------------------
  152. size32_t RtlIntTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  153. {
  154. builder.ensureCapacity(length+offset, field->name->str());
  155. __int64 val = isUnsigned() ? (__int64) source.getUnsignedResult(field) : source.getSignedResult(field);
  156. rtlWriteInt(builder.getSelf() + offset, val, length);
  157. offset += length;
  158. return offset;
  159. }
  160. size32_t RtlIntTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  161. {
  162. if (isUnsigned())
  163. target.processUInt(rtlReadUInt(self, length), field);
  164. else
  165. target.processInt(rtlReadInt(self, length), field);
  166. return length;
  167. }
  168. size32_t RtlIntTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  169. {
  170. if (isUnsigned())
  171. target.outputUInt(rtlReadUInt(self, length), length, queryXPath(field));
  172. else
  173. target.outputInt(rtlReadInt(self, length), length, queryXPath(field));
  174. return length;
  175. }
  176. //-------------------------------------------------------------------------------------------------------------------
  177. size32_t RtlSwapIntTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  178. {
  179. builder.ensureCapacity(length+offset, field->name->str());
  180. __int64 val = isUnsigned() ? (__int64) source.getUnsignedResult(field) : source.getSignedResult(field);
  181. // NOTE - we assume that the value returned from the source is already a swapped int
  182. rtlWriteInt(builder.getSelf() + offset, val, length);
  183. offset += length;
  184. return offset;
  185. }
  186. size32_t RtlSwapIntTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  187. {
  188. if (isUnsigned())
  189. target.processUInt(rtlReadSwapUInt(self, length), field);
  190. else
  191. target.processInt(rtlReadSwapInt(self, length), field);
  192. return length;
  193. }
  194. size32_t RtlSwapIntTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  195. {
  196. if (isUnsigned())
  197. target.outputUInt(rtlReadSwapUInt(self, length), length, queryXPath(field));
  198. else
  199. target.outputInt(rtlReadSwapInt(self, length), length, queryXPath(field));
  200. return length;
  201. }
  202. //-------------------------------------------------------------------------------------------------------------------
  203. size32_t RtlPackedIntTypeInfo::size(const byte * self, const byte * selfrow) const
  204. {
  205. return rtlGetPackedSize(self);
  206. }
  207. size32_t RtlPackedIntTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  208. {
  209. unsigned __int64 value = isUnsigned() ? (__int64) source.getUnsignedResult(field) : source.getSignedResult(field);
  210. size32_t sizeInBytes = rtlGetPackedSize(&value);
  211. builder.ensureCapacity(sizeInBytes+offset, field->name->str());
  212. rtlSetPackedUnsigned(builder.getSelf() + offset, value);
  213. offset += sizeInBytes;
  214. return offset;
  215. }
  216. size32_t RtlPackedIntTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  217. {
  218. if (isUnsigned())
  219. target.processUInt(rtlGetPackedUnsigned(self), field);
  220. else
  221. target.processInt(rtlGetPackedSigned(self), field);
  222. return rtlGetPackedSize(self);
  223. }
  224. size32_t RtlPackedIntTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  225. {
  226. size32_t fieldsize = rtlGetPackedSize(self);
  227. if (isUnsigned())
  228. target.outputUInt(rtlGetPackedUnsigned(self), fieldsize, queryXPath(field));
  229. else
  230. target.outputInt(rtlGetPackedSigned(self), fieldsize, queryXPath(field));
  231. return fieldsize;
  232. }
  233. //-------------------------------------------------------------------------------------------------------------------
  234. size32_t RtlStringTypeInfo::size(const byte * self, const byte * selfrow) const
  235. {
  236. if (isFixedSize())
  237. return length;
  238. return sizeof(size32_t) + rtlReadUInt4(self);
  239. }
  240. size32_t RtlStringTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  241. {
  242. size32_t size;
  243. char *value;
  244. source.getStringResult(field, size, value);
  245. if (!isFixedSize())
  246. {
  247. builder.ensureCapacity(offset+size+sizeof(size32_t), field->name->str());
  248. byte *dest = builder.getSelf()+offset;
  249. rtlWriteInt4(dest, size);
  250. #if 0
  251. // NOTE - you might argue that we should convert the incoming data to EBCDIC. But it seems more useful to
  252. // define the semantics as being that the IFieldSource should return EBCDIC if you have declared the matching field as EBCDIC
  253. // (otherwise, why did you bother?)
  254. if (isEbcdic())
  255. rtlStrToEStr(size, (char *) dest+sizeof(size32_t), size, (char *)value);
  256. else
  257. #endif
  258. memcpy(dest+sizeof(size32_t), value, size);
  259. offset += size+sizeof(size32_t);
  260. }
  261. else
  262. {
  263. builder.ensureCapacity(offset+length, field->name->str());
  264. byte *dest = builder.getSelf()+offset;
  265. #if 0
  266. // See above...
  267. if (isEbcdic())
  268. rtlStrToEStr(length, (char *) dest, size, (char *) value);
  269. else
  270. #endif
  271. rtlStrToStr(length, dest, size, value);
  272. offset += length;
  273. }
  274. rtlFree(value);
  275. return offset;
  276. }
  277. size32_t RtlStringTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  278. {
  279. const char * str = reinterpret_cast<const char *>(self);
  280. unsigned thisLength;
  281. unsigned thisSize;
  282. if (isFixedSize())
  283. {
  284. thisLength = length;
  285. thisSize = thisLength;
  286. }
  287. else
  288. {
  289. str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  290. thisLength = rtlReadUInt4(self);
  291. thisSize = sizeof(size32_t) + thisLength;
  292. }
  293. if (isEbcdic())
  294. {
  295. unsigned lenAscii;
  296. rtlDataAttr ascii;
  297. rtlEStrToStrX(lenAscii, ascii.refstr(), thisLength, str);
  298. target.processString(lenAscii, ascii.getstr(), field);
  299. }
  300. else
  301. {
  302. target.processString(thisLength, str, field);
  303. }
  304. return thisSize;
  305. }
  306. size32_t RtlStringTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  307. {
  308. const char * str = reinterpret_cast<const char *>(self);
  309. unsigned thisLength;
  310. unsigned thisSize;
  311. if (isFixedSize())
  312. {
  313. thisLength = length;
  314. thisSize = thisLength;
  315. }
  316. else
  317. {
  318. str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  319. thisLength = rtlReadUInt4(self);
  320. thisSize = sizeof(size32_t) + thisLength;
  321. }
  322. if (isEbcdic())
  323. {
  324. unsigned lenAscii;
  325. rtlDataAttr ascii;
  326. rtlEStrToStrX(lenAscii, ascii.refstr(), thisLength, str);
  327. target.outputString(lenAscii, ascii.getstr(), queryXPath(field));
  328. }
  329. else
  330. {
  331. target.outputString(thisLength, str, queryXPath(field));
  332. }
  333. return thisSize;
  334. }
  335. //-------------------------------------------------------------------------------------------------------------------
  336. size32_t RtlDataTypeInfo::size(const byte * self, const byte * selfrow) const
  337. {
  338. if (isFixedSize())
  339. return length;
  340. return sizeof(size32_t) + rtlReadUInt4(self);
  341. }
  342. size32_t RtlDataTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  343. {
  344. size32_t size;
  345. void *value;
  346. source.getDataResult(field, size, value);
  347. if (!isFixedSize())
  348. {
  349. builder.ensureCapacity(offset+size+sizeof(size32_t), field->name->str());
  350. byte *dest = builder.getSelf()+offset;
  351. rtlWriteInt4(dest, size);
  352. memcpy(dest+sizeof(size32_t), value, size);
  353. offset += size+sizeof(size32_t);
  354. }
  355. else
  356. {
  357. builder.ensureCapacity(offset+length, field->name->str());
  358. byte *dest = builder.getSelf()+offset;
  359. rtlDataToData(length, dest, size, value);
  360. offset += length;
  361. }
  362. rtlFree(value);
  363. return offset;
  364. }
  365. size32_t RtlDataTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  366. {
  367. const char * str = reinterpret_cast<const char *>(self);
  368. unsigned thisLength;
  369. unsigned thisSize;
  370. if (isFixedSize())
  371. {
  372. thisLength = length;
  373. thisSize = thisLength;
  374. }
  375. else
  376. {
  377. str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  378. thisLength = rtlReadUInt4(self);
  379. thisSize = sizeof(size32_t) + thisLength;
  380. }
  381. target.processData(thisLength, str, field);
  382. return thisSize;
  383. }
  384. size32_t RtlDataTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  385. {
  386. const char * str = reinterpret_cast<const char *>(self);
  387. unsigned thisLength;
  388. unsigned thisSize;
  389. if (isFixedSize())
  390. {
  391. thisLength = length;
  392. thisSize = thisLength;
  393. }
  394. else
  395. {
  396. str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  397. thisLength = rtlReadUInt4(self);
  398. thisSize = sizeof(size32_t) + thisLength;
  399. }
  400. target.outputData(thisLength, str, queryXPath(field));
  401. return thisSize;
  402. }
  403. //-------------------------------------------------------------------------------------------------------------------
  404. size32_t RtlVarStringTypeInfo::size(const byte * self, const byte * selfrow) const
  405. {
  406. if (isFixedSize())
  407. return length + 1;
  408. const char * str = reinterpret_cast<const char *>(self);
  409. return (size32_t)strlen(str)+1;
  410. }
  411. size32_t RtlVarStringTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  412. {
  413. size32_t size;
  414. char *value;
  415. source.getStringResult(field, size, value);
  416. if (!isFixedSize())
  417. {
  418. builder.ensureCapacity(offset+size+1, field->name->str());
  419. // See notes re EBCDIC conversion in RtlStringTypeInfo code
  420. byte *dest = builder.getSelf()+offset;
  421. memcpy(dest, value, size);
  422. dest[size] = '\0';
  423. offset += size+1;
  424. }
  425. else
  426. {
  427. builder.ensureCapacity(offset+length, field->name->str());
  428. byte *dest = builder.getSelf()+offset;
  429. rtlStrToVStr(length, dest, size, value);
  430. offset += length;
  431. }
  432. rtlFree(value);
  433. return offset;
  434. }
  435. size32_t RtlVarStringTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  436. {
  437. const char * str = reinterpret_cast<const char *>(self);
  438. unsigned thisLength = (size32_t)strlen(str);
  439. unsigned thisSize;
  440. if (isFixedSize())
  441. thisSize = length+1;
  442. else
  443. thisSize = thisLength+1;
  444. if (isEbcdic())
  445. {
  446. unsigned lenAscii;
  447. rtlDataAttr ascii;
  448. rtlEStrToStrX(lenAscii, ascii.refstr(), thisLength, str);
  449. target.processString(lenAscii, ascii.getstr(), field);
  450. }
  451. else
  452. target.processString(thisLength, str, field);
  453. return thisSize;
  454. }
  455. size32_t RtlVarStringTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  456. {
  457. const char * str = reinterpret_cast<const char *>(self);
  458. unsigned thisLength = (size32_t)strlen(str);
  459. unsigned thisSize;
  460. if (isFixedSize())
  461. thisSize = length+1;
  462. else
  463. thisSize = thisLength+1;
  464. if (isEbcdic())
  465. {
  466. unsigned lenAscii;
  467. rtlDataAttr ascii;
  468. rtlEStrToStrX(lenAscii, ascii.refstr(), thisLength, str);
  469. target.outputString(lenAscii, ascii.getstr(), queryXPath(field));
  470. }
  471. else
  472. target.outputString(thisLength, str, queryXPath(field));
  473. return thisSize;
  474. }
  475. //-------------------------------------------------------------------------------------------------------------------
  476. size32_t RtlQStringTypeInfo::size(const byte * self, const byte * selfrow) const
  477. {
  478. if (isFixedSize())
  479. return rtlQStrSize(length);
  480. return sizeof(size32_t) + rtlQStrSize(rtlReadUInt4(self));
  481. }
  482. size32_t RtlQStringTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  483. {
  484. size32_t size;
  485. char *value;
  486. source.getStringResult(field, size, value);
  487. if (!isFixedSize())
  488. {
  489. size32_t sizeInBytes = rtlQStrSize(size) + sizeof(size32_t);
  490. builder.ensureCapacity(offset+sizeInBytes, field->name->str());
  491. byte *dest = builder.getSelf()+offset;
  492. rtlWriteInt4(dest, size);
  493. rtlStrToQStr(size, (char *) dest+sizeof(size32_t), size, value);
  494. offset += sizeInBytes;
  495. }
  496. else
  497. {
  498. size32_t sizeInBytes = rtlQStrSize(length);
  499. builder.ensureCapacity(offset+sizeInBytes, field->name->str());
  500. byte *dest = builder.getSelf()+offset;
  501. rtlStrToQStr(length, (char *) dest, size, value);
  502. offset += sizeInBytes;
  503. }
  504. rtlFree(value);
  505. return offset;
  506. }
  507. size32_t RtlQStringTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  508. {
  509. const char * str = reinterpret_cast<const char *>(self);
  510. unsigned thisLength;
  511. unsigned thisSize;
  512. if (isFixedSize())
  513. {
  514. thisLength = length;
  515. thisSize = rtlQStrSize(thisLength);
  516. }
  517. else
  518. {
  519. str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  520. thisLength = rtlReadUInt4(self);
  521. thisSize = sizeof(size32_t) + rtlQStrSize(thisLength);
  522. }
  523. target.processQString(thisLength, str, field);
  524. return thisSize;
  525. }
  526. size32_t RtlQStringTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  527. {
  528. const char * str = reinterpret_cast<const char *>(self);
  529. unsigned thisLength;
  530. unsigned thisSize;
  531. if (isFixedSize())
  532. {
  533. thisLength = length;
  534. thisSize = rtlQStrSize(thisLength);
  535. }
  536. else
  537. {
  538. str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  539. thisLength = rtlReadUInt4(self);
  540. thisSize = sizeof(size32_t) + rtlQStrSize(thisLength);
  541. }
  542. target.outputQString(thisLength, str, queryXPath(field));
  543. return thisSize;
  544. }
  545. //-------------------------------------------------------------------------------------------------------------------
  546. size32_t RtlDecimalTypeInfo::calcSize() const
  547. {
  548. if (isUnsigned())
  549. return (getDecimalDigits()+1)/2;
  550. return (getDecimalDigits()+2)/2;
  551. }
  552. size32_t RtlDecimalTypeInfo::size(const byte * self, const byte * selfrow) const
  553. {
  554. return calcSize();
  555. }
  556. size32_t RtlDecimalTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  557. {
  558. Decimal value;
  559. source.getDecimalResult(field, value);
  560. size32_t sizeInBytes = calcSize();
  561. builder.ensureCapacity(sizeInBytes+offset, field->name->str());
  562. if (isUnsigned())
  563. value.getUDecimal(sizeInBytes, getDecimalPrecision(), builder.getSelf()+offset);
  564. else
  565. value.getDecimal(sizeInBytes, getDecimalPrecision(), builder.getSelf()+offset);
  566. offset += sizeInBytes;
  567. return offset;
  568. }
  569. size32_t RtlDecimalTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  570. {
  571. size32_t thisSize = calcSize();
  572. if (isUnsigned())
  573. target.processUDecimal(self, thisSize, getDecimalPrecision(), field);
  574. else
  575. target.processDecimal(self, thisSize, getDecimalPrecision(), field);
  576. return thisSize;
  577. }
  578. size32_t RtlDecimalTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  579. {
  580. size32_t thisSize = calcSize();
  581. if (isUnsigned())
  582. target.outputUDecimal(self, thisSize, getDecimalPrecision(), queryXPath(field));
  583. else
  584. target.outputDecimal(self, thisSize, getDecimalPrecision(), queryXPath(field));
  585. return thisSize;
  586. }
  587. //-------------------------------------------------------------------------------------------------------------------
  588. size32_t RtlCharTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  589. {
  590. throwUnexpected(); // Can't have a field of type char
  591. }
  592. size32_t RtlCharTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  593. {
  594. const char * str = reinterpret_cast<const char *>(self);
  595. char c;
  596. if (isEbcdic())
  597. rtlEStrToStr(1, &c, 1, str);
  598. else
  599. c = *str;
  600. target.processString(1, &c, field);
  601. return 1;
  602. }
  603. size32_t RtlCharTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  604. {
  605. const char * str = reinterpret_cast<const char *>(self);
  606. char c;
  607. if (isEbcdic())
  608. rtlEStrToStr(1, &c, 1, str);
  609. else
  610. c = *str;
  611. target.outputString(1, &c, queryXPath(field));
  612. return 1;
  613. }
  614. //-------------------------------------------------------------------------------------------------------------------
  615. size32_t RtlUnicodeTypeInfo::size(const byte * self, const byte * selfrow) const
  616. {
  617. if (isFixedSize())
  618. return length * sizeof(UChar);
  619. return sizeof(size32_t) + rtlReadUInt4(self) * sizeof(UChar);
  620. }
  621. size32_t RtlUnicodeTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  622. {
  623. size32_t sizeInChars;
  624. UChar *value;
  625. source.getUnicodeResult(field, sizeInChars, value);
  626. if (!isFixedSize())
  627. {
  628. size32_t sizeInBytes = sizeInChars * sizeof(UChar);
  629. builder.ensureCapacity(offset+sizeInBytes+sizeof(size32_t), field->name->str());
  630. byte *dest = builder.getSelf()+offset;
  631. rtlWriteInt4(dest, sizeInChars); // NOTE - in chars!
  632. memcpy(dest+sizeof(size32_t), value, sizeInBytes);
  633. offset += sizeInBytes+sizeof(size32_t);
  634. }
  635. else
  636. {
  637. size32_t sizeInBytes = length * sizeof(UChar);
  638. builder.ensureCapacity(offset+sizeInBytes, field->name->str());
  639. byte *dest = builder.getSelf()+offset;
  640. rtlUnicodeToUnicode(length, (UChar *) dest, sizeInChars, value);
  641. offset += sizeInBytes;
  642. }
  643. rtlFree(value);
  644. return offset;
  645. }
  646. size32_t RtlUnicodeTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  647. {
  648. const UChar * ustr = reinterpret_cast<const UChar *>(self);
  649. unsigned thisLength;
  650. unsigned thisSize;
  651. if (isFixedSize())
  652. {
  653. thisLength = length;
  654. thisSize = thisLength * sizeof(UChar);
  655. }
  656. else
  657. {
  658. ustr = reinterpret_cast<const UChar *>(self + sizeof(size32_t));
  659. thisLength = rtlReadUInt4(self);
  660. thisSize = sizeof(size32_t) + thisLength * sizeof(UChar);
  661. }
  662. target.processUnicode(thisLength, ustr, field);
  663. return thisSize;
  664. }
  665. size32_t RtlUnicodeTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  666. {
  667. const UChar * ustr = reinterpret_cast<const UChar *>(self);
  668. unsigned thisLength;
  669. unsigned thisSize;
  670. if (isFixedSize())
  671. {
  672. thisLength = length;
  673. thisSize = thisLength * sizeof(UChar);
  674. }
  675. else
  676. {
  677. ustr = reinterpret_cast<const UChar *>(self + sizeof(size32_t));
  678. thisLength = rtlReadUInt4(self);
  679. thisSize = sizeof(size32_t) + thisLength * sizeof(UChar);
  680. }
  681. target.outputUnicode(thisLength, ustr, queryXPath(field));
  682. return thisSize;
  683. }
  684. //-------------------------------------------------------------------------------------------------------------------
  685. size32_t RtlVarUnicodeTypeInfo::size(const byte * self, const byte * selfrow) const
  686. {
  687. if (isFixedSize())
  688. return (length+1) * sizeof(UChar);
  689. const UChar * ustr = reinterpret_cast<const UChar *>(self);
  690. return (rtlUnicodeStrlen(ustr)+1) * sizeof(UChar);
  691. }
  692. size32_t RtlVarUnicodeTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  693. {
  694. size32_t sizeInChars;
  695. UChar *value;
  696. source.getUnicodeResult(field, sizeInChars, value);
  697. if (!isFixedSize())
  698. {
  699. size32_t sizeInBytes = (sizeInChars+1) * sizeof(UChar);
  700. builder.ensureCapacity(offset+sizeInBytes, field->name->str());
  701. UChar *dest = (UChar *) builder.getSelf()+offset;
  702. memcpy(dest, value, sizeInBytes - sizeof(UChar));
  703. dest[sizeInChars] = 0;
  704. offset += sizeInBytes;
  705. }
  706. else
  707. {
  708. size32_t sizeInBytes = length * sizeof(UChar);
  709. builder.ensureCapacity(offset+sizeInBytes, field->name->str());
  710. byte *dest = builder.getSelf()+offset;
  711. rtlUnicodeToVUnicode(length, (UChar *) dest, sizeInChars, value);
  712. offset += sizeInBytes;
  713. }
  714. rtlFree(value);
  715. return offset;
  716. }
  717. size32_t RtlVarUnicodeTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  718. {
  719. const UChar * ustr = reinterpret_cast<const UChar *>(self);
  720. unsigned thisLength = rtlUnicodeStrlen(ustr);
  721. unsigned thisSize;
  722. if (isFixedSize())
  723. thisSize = (length + 1) * sizeof(UChar);
  724. else
  725. thisSize = (thisLength + 1) * sizeof(UChar);
  726. target.processUnicode(thisLength, ustr, field);
  727. return thisSize;
  728. }
  729. size32_t RtlVarUnicodeTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  730. {
  731. const UChar * ustr = reinterpret_cast<const UChar *>(self);
  732. unsigned thisLength = rtlUnicodeStrlen(ustr);
  733. unsigned thisSize;
  734. if (isFixedSize())
  735. thisSize = (length + 1) * sizeof(UChar);
  736. else
  737. thisSize = (thisLength + 1) * sizeof(UChar);
  738. target.outputUnicode(thisLength, ustr, queryXPath(field));
  739. return thisSize;
  740. }
  741. //-------------------------------------------------------------------------------------------------------------------
  742. size32_t RtlUtf8TypeInfo::size(const byte * self, const byte * selfrow) const
  743. {
  744. assertex(!isFixedSize());
  745. return sizeof(size32_t) + rtlUtf8Size(rtlReadUInt4(self), self+sizeof(unsigned));
  746. }
  747. size32_t RtlUtf8TypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  748. {
  749. size32_t sizeInChars;
  750. char *value;
  751. source.getUTF8Result(field, sizeInChars, value);
  752. size32_t sizeInBytes = rtlUtf8Size(sizeInChars, value);
  753. assertex(!isFixedSize());
  754. builder.ensureCapacity(offset+sizeInBytes+sizeof(size32_t), field->name->str());
  755. byte *dest = builder.getSelf()+offset;
  756. rtlWriteInt4(dest, sizeInChars); // NOTE - in chars!
  757. memcpy(dest+sizeof(size32_t), value, sizeInBytes);
  758. offset += sizeInBytes+sizeof(size32_t);
  759. rtlFree(value);
  760. return offset;
  761. }
  762. size32_t RtlUtf8TypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  763. {
  764. assertex(!isFixedSize());
  765. const char * str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  766. unsigned thisLength = rtlReadUInt4(self);
  767. unsigned thisSize = sizeof(size32_t) + rtlUtf8Size(thisLength, str);
  768. target.processUtf8(thisLength, str, field);
  769. return thisSize;
  770. }
  771. size32_t RtlUtf8TypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  772. {
  773. assertex(!isFixedSize());
  774. const char * str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  775. unsigned thisLength = rtlReadUInt4(self);
  776. unsigned thisSize = sizeof(size32_t) + rtlUtf8Size(thisLength, str);
  777. target.outputUtf8(thisLength, str, queryXPath(field));
  778. return thisSize;
  779. }
  780. //-------------------------------------------------------------------------------------------------------------------
  781. inline size32_t sizeFields(const RtlFieldInfo * const * cur, const byte * self, const byte * selfrow)
  782. {
  783. unsigned offset = 0;
  784. loop
  785. {
  786. const RtlFieldInfo * child = *cur;
  787. if (!child)
  788. break;
  789. offset += child->size(self+offset, selfrow);
  790. cur++;
  791. }
  792. return offset;
  793. }
  794. inline size32_t processFields(const RtlFieldInfo * const * cur, const byte * self, const byte * selfrow, IFieldProcessor & target)
  795. {
  796. unsigned offset = 0;
  797. loop
  798. {
  799. const RtlFieldInfo * child = *cur;
  800. if (!child)
  801. break;
  802. child->process(self+offset, selfrow, target);
  803. offset += child->size(self+offset, selfrow);
  804. cur++;
  805. }
  806. return offset;
  807. }
  808. inline size32_t buildFields(const RtlFieldInfo * const * cur, ARowBuilder &builder, size32_t offset, IFieldSource &source)
  809. {
  810. loop
  811. {
  812. const RtlFieldInfo * child = *cur;
  813. if (!child)
  814. break;
  815. offset = child->build(builder, offset, source);
  816. cur++;
  817. }
  818. return offset;
  819. }
  820. inline size32_t toXMLFields(const RtlFieldInfo * const * cur, const byte * self, const byte * selfrow, IXmlWriter & target)
  821. {
  822. size32_t offset = 0;
  823. loop
  824. {
  825. const RtlFieldInfo * child = *cur;
  826. if (!child)
  827. break;
  828. size32_t size = child->toXML(self+offset, selfrow, target);
  829. offset += size;
  830. cur++;
  831. }
  832. return offset;
  833. }
  834. //-------------------------------------------------------------------------------------------------------------------
  835. size32_t RtlRecordTypeInfo::size(const byte * self, const byte * selfrow) const
  836. {
  837. return sizeFields(fields, self, self);
  838. }
  839. size32_t RtlRecordTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  840. {
  841. if (target.processBeginRow(field))
  842. {
  843. unsigned offset = processFields(fields, self, self, target);
  844. target.processEndRow(field);
  845. return offset;
  846. }
  847. return size(self, selfrow);
  848. }
  849. size32_t RtlRecordTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  850. {
  851. const char * xpath = queryXPath(field);
  852. if (*xpath)
  853. target.outputBeginNested(xpath, false);
  854. unsigned thisSize = toXMLFields(fields, self, self, target);
  855. if (*xpath)
  856. target.outputEndNested(xpath);
  857. return thisSize;
  858. }
  859. size32_t RtlRecordTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  860. {
  861. source.processBeginRow(field);
  862. offset = buildFields(fields, builder, offset, source);
  863. source.processEndRow(field);
  864. return offset;
  865. }
  866. //-------------------------------------------------------------------------------------------------------------------
  867. size32_t RtlSetTypeInfo::size(const byte * self, const byte * selfrow) const
  868. {
  869. return sizeof(bool) + sizeof(size32_t) + rtlReadUInt4(self + sizeof(bool));
  870. }
  871. size32_t RtlSetTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  872. {
  873. bool isAll;
  874. source.processBeginSet(field, isAll);
  875. size32_t sizeInBytes = sizeof(bool) + sizeof(size32_t);
  876. builder.ensureCapacity(offset+sizeInBytes, field->name->str());
  877. byte *dest = builder.getSelf()+offset;
  878. if (isAll)
  879. {
  880. * (bool *) dest = true;
  881. rtlWriteInt4(dest+1, 0);
  882. offset += sizeInBytes;
  883. }
  884. else
  885. {
  886. * (bool *) dest = false;
  887. size32_t newOffset = offset + sizeInBytes;
  888. RtlFieldStrInfo dummyField("<set element>", NULL, child);
  889. while (source.processNextSet(field))
  890. {
  891. newOffset = child->build(builder, newOffset, &dummyField, source);
  892. }
  893. // Go back in and patch the size, remembering it may have moved
  894. rtlWriteInt4(builder.getSelf()+offset+1, newOffset - (offset+sizeInBytes));
  895. offset = newOffset;
  896. }
  897. source.processEndSet(field);
  898. return offset;
  899. }
  900. size32_t RtlSetTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  901. {
  902. unsigned offset = sizeof(bool) + sizeof(size32_t);
  903. unsigned max = offset + rtlReadUInt4(self + sizeof(bool));
  904. unsigned elements = 0;
  905. if (!*(bool *)self)
  906. {
  907. unsigned tempOffset = sizeof(bool) + sizeof(size32_t);
  908. if (child->isFixedSize())
  909. {
  910. unsigned elemSize = child->size(NULL, NULL);
  911. elements = (max-offset) / elemSize;
  912. assert(elements*elemSize == max-offset);
  913. }
  914. else
  915. {
  916. DummyFieldProcessor dummy;
  917. while (tempOffset < max)
  918. {
  919. tempOffset += child->process(self+tempOffset, selfrow, field, dummy); // NOTE - good thing we can't have a set of sets, or this would recurse
  920. elements++;
  921. }
  922. }
  923. }
  924. if (target.processBeginSet(field, elements, *(bool *)self, self+offset))
  925. {
  926. while (offset < max)
  927. {
  928. offset += child->process(self+offset, selfrow, field, target);
  929. }
  930. }
  931. target.processEndSet(field);
  932. return max;
  933. }
  934. size32_t RtlSetTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  935. {
  936. unsigned offset = sizeof(bool) + sizeof(size32_t);
  937. unsigned max = offset + rtlReadUInt4(self + sizeof(bool));
  938. StringAttr outerTag;
  939. if (hasOuterXPath(field))
  940. {
  941. queryNestedOuterXPath(outerTag, field);
  942. target.outputBeginNested(outerTag, false);
  943. }
  944. if (*(bool *)self)
  945. target.outputSetAll();
  946. else
  947. {
  948. const char *innerPath = queryXPath(field);
  949. target.outputBeginArray(innerPath);
  950. while (offset < max)
  951. {
  952. child->toXML(self+offset, selfrow, field, target);
  953. offset += child->size(self+offset, selfrow);
  954. }
  955. target.outputEndArray(innerPath);
  956. }
  957. if (outerTag)
  958. target.outputEndNested(outerTag);
  959. return max;
  960. }
  961. //-------------------------------------------------------------------------------------------------------------------
  962. size32_t RtlRowTypeInfo::size(const byte * self, const byte * selfrow) const
  963. {
  964. if (isLinkCounted())
  965. return sizeof(void *);
  966. return child->size(self, selfrow);
  967. }
  968. size32_t RtlRowTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  969. {
  970. if (isLinkCounted())
  971. {
  972. const byte * row = *(const byte * *)self;
  973. if (row)
  974. child->process(row, row, field, target);
  975. return sizeof(row);
  976. }
  977. return child->process(self, self, field, target);
  978. }
  979. size32_t RtlRowTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  980. {
  981. if (isLinkCounted())
  982. {
  983. const byte * row = *(const byte * *)self;
  984. child->toXML(row, row, field, target);
  985. return sizeof(row);
  986. }
  987. return child->toXML(self, self, field, target);
  988. }
  989. //-------------------------------------------------------------------------------------------------------------------
  990. size32_t RtlDatasetTypeInfo::size(const byte * self, const byte * selfrow) const
  991. {
  992. if (isLinkCounted())
  993. return sizeof(size32_t) + sizeof(void * *);
  994. return sizeof(size32_t) + rtlReadUInt4(self);
  995. }
  996. size32_t RtlDatasetTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  997. {
  998. source.processBeginDataset(field);
  999. if (isLinkCounted())
  1000. {
  1001. // a 32-bit record count, and a pointer to an array of record pointers
  1002. size32_t sizeInBytes = sizeof(size32_t) + sizeof(void *);
  1003. builder.ensureCapacity(offset+sizeInBytes, field->name->str());
  1004. size32_t numRows = 0;
  1005. Owned<IEngineRowAllocator> childAllocator = builder.queryAllocator()->createChildRowAllocator(child);
  1006. byte **childRows = NULL;
  1007. RtlFieldStrInfo dummyField("<nested row>", NULL, child);
  1008. while (source.processNextRow(field))
  1009. {
  1010. RtlDynamicRowBuilder childBuilder(childAllocator);
  1011. size32_t childLen = child->build(childBuilder, 0, &dummyField, source);
  1012. childRows = childAllocator->appendRowOwn(childRows, ++numRows, (void *) childBuilder.finalizeRowClear(childLen));
  1013. }
  1014. // Go back in and patch the count, remembering it may have moved
  1015. rtlWriteInt4(builder.getSelf()+offset, numRows);
  1016. * ( const void * * ) (builder.getSelf()+offset+sizeof(size32_t)) = childRows;
  1017. offset += sizeInBytes;
  1018. }
  1019. else
  1020. {
  1021. // a 32-bit size, then rows inline
  1022. size32_t sizeInBytes = sizeof(size32_t);
  1023. builder.ensureCapacity(offset+sizeInBytes, field->name->str());
  1024. size32_t newOffset = offset + sizeInBytes;
  1025. RtlFieldStrInfo dummyField("<nested row>", NULL, child);
  1026. while (source.processNextRow(field))
  1027. newOffset = child->build(builder, newOffset, &dummyField, source);
  1028. // Go back in and patch the size, remembering it may have moved
  1029. rtlWriteInt4(builder.getSelf()+offset, newOffset - (offset+sizeInBytes));
  1030. offset = newOffset;
  1031. }
  1032. source.processEndDataset(field);
  1033. return offset;
  1034. }
  1035. size32_t RtlDatasetTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  1036. {
  1037. if (isLinkCounted())
  1038. {
  1039. size32_t thisCount = rtlReadUInt4(self);
  1040. if (target.processBeginDataset(field, thisCount))
  1041. {
  1042. const byte * * rows = *reinterpret_cast<const byte * * const *>(self + sizeof(size32_t));
  1043. for (unsigned i= 0; i < thisCount; i++)
  1044. {
  1045. const byte * row = rows[i];
  1046. child->process(row, row, field, target);
  1047. }
  1048. target.processEndDataset(field);
  1049. }
  1050. return sizeof(size32_t) + sizeof(void * *);
  1051. }
  1052. else
  1053. {
  1054. unsigned offset = sizeof(size32_t);
  1055. unsigned max = offset + rtlReadUInt4(self);
  1056. unsigned thisCount = 0;
  1057. DummyFieldProcessor dummy;
  1058. while (offset < max)
  1059. {
  1060. offset += child->process(self+offset, self+offset, field, dummy);
  1061. thisCount++;
  1062. }
  1063. offset = sizeof(size32_t);
  1064. if (target.processBeginDataset(field, thisCount))
  1065. {
  1066. while (offset < max)
  1067. {
  1068. offset += child->process(self+offset, self+offset, field, target);
  1069. }
  1070. target.processEndDataset(field);
  1071. }
  1072. return max;
  1073. }
  1074. }
  1075. size32_t RtlDatasetTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  1076. {
  1077. StringAttr outerTag;
  1078. if (hasOuterXPath(field))
  1079. {
  1080. queryNestedOuterXPath(outerTag, field);
  1081. target.outputBeginNested(outerTag, false);
  1082. }
  1083. const char *innerPath = queryXPath(field);
  1084. target.outputBeginArray(innerPath);
  1085. unsigned thisSize;
  1086. if (isLinkCounted())
  1087. {
  1088. size32_t thisCount = rtlReadUInt4(self);
  1089. const byte * * rows = *reinterpret_cast<const byte * * const *>(self + sizeof(size32_t));
  1090. for (unsigned i= 0; i < thisCount; i++)
  1091. {
  1092. const byte * row = rows[i];
  1093. if (row)
  1094. child->toXML(row, row, field, target);
  1095. }
  1096. thisSize = sizeof(size32_t) + sizeof(void * *);
  1097. }
  1098. else
  1099. {
  1100. unsigned offset = sizeof(size32_t);
  1101. unsigned max = offset + rtlReadUInt4(self);
  1102. while (offset < max)
  1103. {
  1104. child->toXML(self+offset, self+offset, field, target);
  1105. offset += child->size(self+offset, self+offset);
  1106. }
  1107. thisSize = max;
  1108. }
  1109. target.outputEndArray(innerPath);
  1110. if (outerTag)
  1111. target.outputEndNested(outerTag);
  1112. return thisSize;
  1113. }
  1114. //-------------------------------------------------------------------------------------------------------------------
  1115. size32_t RtlDictionaryTypeInfo::size(const byte * self, const byte * selfrow) const
  1116. {
  1117. if (isLinkCounted())
  1118. return sizeof(size32_t) + sizeof(void * *);
  1119. return sizeof(size32_t) + rtlReadUInt4(self);
  1120. }
  1121. size32_t RtlDictionaryTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  1122. {
  1123. source.processBeginDataset(field);
  1124. if (isLinkCounted())
  1125. {
  1126. // a 32-bit record count, and a pointer to an hash table with record pointers
  1127. size32_t sizeInBytes = sizeof(size32_t) + sizeof(void *);
  1128. builder.ensureCapacity(offset+sizeInBytes, field->name->str());
  1129. Owned<IEngineRowAllocator> childAllocator = builder.queryAllocator()->createChildRowAllocator(child);
  1130. RtlLinkedDictionaryBuilder dictBuilder(childAllocator, hashInfo);
  1131. RtlFieldStrInfo dummyField("<nested row>", NULL, child);
  1132. while (source.processNextRow(field))
  1133. {
  1134. RtlDynamicRowBuilder childBuilder(childAllocator);
  1135. size32_t childLen = child->build(childBuilder, 0, &dummyField, source);
  1136. dictBuilder.appendOwn((void *) childBuilder.finalizeRowClear(childLen));
  1137. }
  1138. // Go back in and patch the count
  1139. rtlWriteInt4(builder.getSelf()+offset, dictBuilder.getcount());
  1140. * ( const void * * ) (builder.getSelf()+offset+sizeof(size32_t)) = dictBuilder.linkrows();
  1141. offset += sizeInBytes;
  1142. }
  1143. else
  1144. UNIMPLEMENTED; // And may never be...
  1145. source.processEndDataset(field);
  1146. return offset;
  1147. }
  1148. size32_t RtlDictionaryTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  1149. {
  1150. if (isLinkCounted())
  1151. {
  1152. size32_t thisCount = rtlReadUInt4(self);
  1153. if (target.processBeginDataset(field, thisCount))
  1154. {
  1155. const byte * * rows = *reinterpret_cast<const byte * * const *>(self + sizeof(size32_t));
  1156. for (unsigned i= 0; i < thisCount; i++)
  1157. {
  1158. const byte * row = rows[i];
  1159. if (row)
  1160. child->process(row, row, field, target);
  1161. }
  1162. target.processEndDataset(field);
  1163. }
  1164. return sizeof(size32_t) + sizeof(void * *);
  1165. }
  1166. else
  1167. {
  1168. //MORE: We could interpret serialized dictionaries if there was ever a need
  1169. UNIMPLEMENTED;
  1170. }
  1171. }
  1172. size32_t RtlDictionaryTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  1173. {
  1174. StringAttr outerTag;
  1175. if (hasOuterXPath(field))
  1176. {
  1177. queryNestedOuterXPath(outerTag, field);
  1178. target.outputBeginNested(outerTag, false);
  1179. }
  1180. const char *innerPath = queryXPath(field);
  1181. target.outputBeginArray(innerPath);
  1182. unsigned thisSize;
  1183. if (isLinkCounted())
  1184. {
  1185. size32_t thisCount = rtlReadUInt4(self);
  1186. const byte * * rows = *reinterpret_cast<const byte * * const *>(self + sizeof(size32_t));
  1187. for (unsigned i= 0; i < thisCount; i++)
  1188. {
  1189. const byte * row = rows[i];
  1190. if (row)
  1191. child->toXML(row, row, field, target);
  1192. }
  1193. thisSize = sizeof(size32_t) + sizeof(void * *);
  1194. }
  1195. else
  1196. {
  1197. //MORE: We could interpret serialized dictionaries if there was ever a need
  1198. UNIMPLEMENTED;
  1199. }
  1200. target.outputEndArray(innerPath);
  1201. if (outerTag)
  1202. target.outputEndNested(outerTag);
  1203. return thisSize;
  1204. }
  1205. //-------------------------------------------------------------------------------------------------------------------
  1206. size32_t RtlIfBlockTypeInfo::size(const byte * self, const byte * selfrow) const
  1207. {
  1208. if (getCondition(selfrow))
  1209. return sizeFields(fields, self, selfrow);
  1210. return 0;
  1211. }
  1212. size32_t RtlIfBlockTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  1213. {
  1214. if (getCondition(selfrow))
  1215. return processFields(fields, self, selfrow, target);
  1216. return 0;
  1217. }
  1218. size32_t RtlIfBlockTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  1219. {
  1220. if (getCondition(selfrow))
  1221. return toXMLFields(fields, self, selfrow, target);
  1222. return 0;
  1223. }
  1224. //-------------------------------------------------------------------------------------------------------------------
  1225. __int64 RtlBitfieldTypeInfo::signedValue(const byte * self) const
  1226. {
  1227. __int64 value = rtlReadInt(self, getBitfieldIntSize());
  1228. unsigned shift = getBitfieldShift();
  1229. unsigned numBits = getBitfieldNumBits();
  1230. value <<= (sizeof(value) - shift - numBits);
  1231. return value >> numBits;
  1232. }
  1233. unsigned __int64 RtlBitfieldTypeInfo::unsignedValue(const byte * self) const
  1234. {
  1235. unsigned __int64 value = rtlReadInt(self, getBitfieldIntSize());
  1236. unsigned shift = getBitfieldShift();
  1237. unsigned numBits = getBitfieldNumBits();
  1238. value <<= (sizeof(value) - shift - numBits);
  1239. return value >> numBits;
  1240. }
  1241. size32_t RtlBitfieldTypeInfo::size(const byte * self, const byte * selfrow) const
  1242. {
  1243. if (fieldType & RFTMislastbitfield)
  1244. return getBitfieldIntSize();
  1245. return 0;
  1246. }
  1247. size32_t RtlBitfieldTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  1248. {
  1249. if (isUnsigned())
  1250. target.processUInt(unsignedValue(self), field);
  1251. else
  1252. target.processInt(signedValue(self), field);
  1253. return size(self, selfrow);
  1254. }
  1255. size32_t RtlBitfieldTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  1256. {
  1257. size32_t fieldsize = size(self, selfrow);
  1258. if (isUnsigned())
  1259. target.outputUInt(unsignedValue(self), fieldsize, queryXPath(field));
  1260. else
  1261. target.outputInt(signedValue(self), fieldsize, queryXPath(field));
  1262. return fieldsize;
  1263. }
  1264. //-------------------------------------------------------------------------------------------------------------------
  1265. size32_t RtlUnimplementedTypeInfo::size(const byte * self, const byte * selfrow) const
  1266. {
  1267. rtlFailUnexpected();
  1268. return 0;
  1269. }
  1270. size32_t RtlUnimplementedTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  1271. {
  1272. rtlFailUnexpected();
  1273. return 0;
  1274. }
  1275. size32_t RtlUnimplementedTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  1276. {
  1277. rtlFailUnexpected();
  1278. return 0;
  1279. }
  1280. //-------------------------------------------------------------------------------------------------------------------
  1281. RtlFieldStrInfo::RtlFieldStrInfo(const char * _name, const char * _xpath, const RtlTypeInfo * _type, const char *_initializer)
  1282. : RtlFieldInfo(rtlCreateFieldNameAtom(_name), _xpath, _type, _initializer)
  1283. {
  1284. }
  1285. /*
  1286. Stack:
  1287. * Change hqlhtcpp so that the correct derived classes are generated.
  1288. * Test so that toXML calls the default implementaions and check that the same values are generated. (Don't if contains ifblocks/alien)
  1289. * Release
  1290. * Think about bitfields - how do I know it is the last bitfield, how am I going to keep track of the offsets.
  1291. * What code would need to be generated for alien datatypes.
  1292. * Could have alien int and alien string varieties????
  1293. * What would an ecl interpreter look like (a special workunit?) helpers are interpreted? What about the graph?
  1294. * Could I add associations to register user attributes - so a callback could know when they were assigned to?
  1295. * Could I add ctx->noteLocation() into the generated code - so could put breakpoints on variables.
  1296. * Add annotation when a member of the target dataset is updated.
  1297. ctx->noteFieldAssigned(self, <field>); - does this include temporary datasets?
  1298. ctx->noteAttributeX(<name>, Int|String|Unicode|
  1299. ctx->noteLocation(location); - reduce locations, so only one returned per line for a non-dataset? Should it just be the first item on the line that is tagged??
  1300. * Need static information about the breakpoints so debugger knows where to put valid brakpoints....
  1301. * Debugger will want to know about the type of the breakpoints.
  1302. * Should try and compress the location format - possibly have a table of <module.attributes>-># with breakpoint as 12:23
  1303. Also need some information about which datasets, and stored variables etc. are used so they can be displayed.
  1304. - Most datasets can be deduced from the parameters passed into the transform
  1305. - Some are trickier e.g., the extract, could possibly define some mappings
  1306. - options to disable projects/other more complex operations inline (so easier to walk through)
  1307. Bitfields:
  1308. - Two separate questions:
  1309. i) How is the meta information generated.
  1310. ii) How is it stored internally in an IHqlExpression * ?
  1311. * Could store the offset in the type - either in the base type of as a qualifier.
  1312. + much easier code generation.
  1313. - Doesn't provie an easy indication of the last field in a bitfield (because can't really modify after the fact)
  1314. - Problematic when fields are removed to merge them.
  1315. * Could add a bitfield container to the record.
  1316. + Makes it easier to handle the last bitfield
  1317. + Matches the structure used for the cursor.
  1318. - Everything needs to walk the bitfield containers similar to ifblocks.
  1319. - Makes it just as tricky to merge
  1320. - Harder to create the record, unless the code is implicitly handled by appendOperand().
  1321. * The type of no_select could contain a modifier to indicate the offset/islast
  1322. + the type of a no_select would have a 1:1 mapping with type info.
  1323. - A bit complicated to calculate, especially when it isn't used much of the time
  1324. => On Reflection is is probably easiest to keep the structure as it is (some comments should go in hqlexpr to avoid revisiting).
  1325. * interperet bitfield offsets and "is last bitfield" dynamically
  1326. + Greatly simplifies generating the meta - you can always use the field.
  1327. - Requires another parameter and significant extra complexity for the uncommon case. (especially incrementing self)
  1328. * Could generate from the expanded record instead of walking the record structure directly
  1329. + That already knows how the bitfields are allocated, and could easily know which is the last field.
  1330. - A field is no longer sufficient as key fr searching for the information.
  1331. - Best would be a createFieldTypeKey(select-expr) which returns field when approriate, or modified if a bitfield. Then the pain is localised.
  1332. * Output a bitfield container item into the type information
  1333. + Solves the size problem
  1334. - Individual bitfields still need to know their offsets, so doesn't solve the full problem.
  1335. =>
  1336. Change so that either use meta to generate the information, or use no_select when appropriate to fidn out the nesc. information.
  1337. Probably the latter for the moment.
  1338. a) Create a key function and make sure it is always used.
  1339. b) Need to work out how to generate no_ifblock.
  1340. - ifblock is context dependent, so need to generate as part of the parent record, and in the parent record context.
  1341. */