rtlfield.cpp 52 KB

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