rtlfield.cpp 53 KB

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