HPCCFile.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2014 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 "HPCCFile.hpp"
  14. HPCCFileFormat HPCCFile::DEFAULTFORMAT = HPCCFileFormatFlat;
  15. HPCCFile * HPCCFile::createHPCCFile()
  16. {
  17. return new HPCCFile();
  18. }
  19. HPCCFile::HPCCFile() : formatEnum(HPCCFileFormatUnknown), iskeyfile(false), issuperfile(false), keyedCount(-1),
  20. nonKeyedCount(-1), hasNestedColumns(false)
  21. {}
  22. HPCCFile::~HPCCFile()
  23. {
  24. #ifdef _DEBUG
  25. fprintf(stderr, "\nLeaving HPCCFile\n\t");
  26. #endif
  27. columns.kill(false);
  28. }
  29. bool HPCCFile::validateFileName(const char * fullname)
  30. {
  31. if (!fullname || !*fullname)
  32. return false;
  33. int len = strlen(fullname);
  34. for (int strindex = 0; strindex < len; strindex++)
  35. {
  36. switch (fullname[strindex])
  37. {
  38. case '\'':
  39. case '\"':
  40. return false;
  41. case '~':
  42. if (strindex > 0)
  43. return false;
  44. break;
  45. default:
  46. break;
  47. }
  48. }
  49. return true;
  50. }
  51. bool HPCCFile::getFileRecDef(StringBuffer & out, const char * structname, const char * linedelimiter, const char * recordindent)
  52. {
  53. if (this->columns.length() > 0)
  54. {
  55. out.append(structname);
  56. out.append(" := RECORD ");
  57. out.append(linedelimiter);
  58. ForEachItemIn(rowindex, this->columns)
  59. {
  60. out.append(recordindent);
  61. this->columns.item(rowindex).toEclRecString(out);
  62. out.append(";");
  63. out.append(linedelimiter);
  64. }
  65. out.append("END;");
  66. }
  67. else
  68. return false;
  69. return true;
  70. }
  71. void HPCCFile::setKeyedColumn(const char * name)
  72. {
  73. ForEachItemIn(colidx, columns)
  74. {
  75. if (strcmp(columns.item(colidx).getColumnName(), name)==0)
  76. {
  77. columns.item(colidx).setKeyedField(true);
  78. break;
  79. }
  80. }
  81. }
  82. bool HPCCFile::getFileRecDefwithIndexpos(HPCCColumnMetaData * fieldMetaData, StringBuffer & out, const char * structname)
  83. {
  84. if (fieldMetaData)
  85. {
  86. out.append(structname);
  87. out.append(" := RECORD ");
  88. out.append("\n");
  89. ForEachItemIn(rowindex, this->columns)
  90. {
  91. out.append("\t");
  92. this->columns.item(rowindex).toEclRecString(out);
  93. out.append(";");
  94. out.append("\n");
  95. }
  96. out.append("\t")
  97. .append(fieldMetaData->getColumnType())
  98. .append(" ")
  99. .append(fieldMetaData->getColumnName())
  100. .append(" {virtual(fileposition)};\n");
  101. out.append("END;");
  102. return true;
  103. }
  104. return false;
  105. }
  106. bool HPCCFile::setFileColumns(const char * eclString)
  107. {
  108. StringBuffer text(eclString);
  109. MultiErrorReceiver errs;
  110. OwnedHqlExpr record = parseQuery(text.str(), &errs);
  111. if (errs.errCount())
  112. {
  113. StringBuffer errtext;
  114. IError *first = errs.firstError();
  115. first->toString(errtext);
  116. ESPLOG(LogNormal, "Could not set HPCC file columns: %s", errtext.str());
  117. return false;
  118. }
  119. if(!record)
  120. return false;
  121. Owned<IPropertyTree> rectree = createPTree("Table", ipt_caseInsensitive);
  122. exportData(rectree, record);
  123. StringBuffer ecltype;
  124. StringBuffer colname;
  125. int colsize;
  126. int colindex;
  127. Owned<IPropertyTreeIterator> fields = rectree->getElements("Field");
  128. ForEach(*fields)
  129. {
  130. fields->query().getProp("@ecltype", ecltype.clear());
  131. if (strncmp(ecltype, "table of", 8)==0)
  132. setHasNestedColumns(true);
  133. fields->query().getProp("@name", colname.clear());
  134. colsize = fields->query().getPropInt("@size", -1);
  135. colindex = fields->query().getPropInt("@position", -1);
  136. Owned<HPCCColumnMetaData> col = HPCCColumnMetaData::createHPCCColumnMetaData(colname.str());
  137. col->setColumnType(ecltype.str());
  138. col->setIndex(colindex);
  139. col->setTableName(this->fullname.str());
  140. columns.append(*LINK(col));
  141. }
  142. return true;
  143. }
  144. HPCCColumnMetaData * HPCCFile::getColumn(const char * colname)
  145. {
  146. ForEachItemIn(colidx, columns)
  147. {
  148. if (strcmp(columns.item(colidx).getColumnName(), colname)==0)
  149. {
  150. return &(columns.item(colidx));
  151. }
  152. }
  153. return NULL;
  154. }
  155. void HPCCFile::getKeyedFieldsAsDelimitedString(char delim, const char * prefix, StringBuffer & out)
  156. {
  157. getFieldsAsDelmitedString(delim, prefix, out, true);
  158. }
  159. void HPCCFile::getNonKeyedFieldsAsDelmitedString(char delim, const char * prefix, StringBuffer & out)
  160. {
  161. getFieldsAsDelmitedString(delim, prefix, out, false);
  162. }
  163. void HPCCFile::getFieldsAsDelmitedString(char delim, const char * prefix, StringBuffer & out, bool onlykeyed)
  164. {
  165. bool isFirst = true;
  166. ForEachItemIn(colidx, columns)
  167. {
  168. HPCCColumnMetaData currcol = columns.item(colidx);
  169. if ((onlykeyed && currcol.isKeyedField()) || (!onlykeyed && !currcol.isKeyedField()))
  170. {
  171. if (!isFirst)
  172. {
  173. out.append(delim);
  174. out.append(" ");
  175. }
  176. if (prefix && *prefix)
  177. {
  178. out.append(prefix);
  179. out.append('.');
  180. }
  181. out.append(currcol.getColumnName());
  182. isFirst = false;
  183. }
  184. }
  185. }
  186. bool HPCCFile::containsField(SQLColumn * field, bool verifyEclType) const
  187. {
  188. const char * fieldName = field->getName();
  189. for (int i = 0; i < columns.length(); i++)
  190. {
  191. const char * name = columns.item(i).getColumnName();
  192. if (stricmp(name, fieldName)==0)
  193. {
  194. if (!verifyEclType || strcmp(columns.item(i).getColumnType(), field->getColumnType())==0)
  195. return true;
  196. return false;
  197. }
  198. }
  199. return false;
  200. }
  201. void HPCCFile::setKeyCounts()
  202. {
  203. nonKeyedCount = 0;
  204. keyedCount = 0;
  205. ForEachItemIn(colidx, columns)
  206. {
  207. HPCCColumnMetaData currcol = columns.item(colidx);
  208. if (!currcol.isKeyedField())
  209. nonKeyedCount++;
  210. else
  211. keyedCount++;
  212. }
  213. }
  214. int HPCCFile::getNonKeyedColumnsCount()
  215. {
  216. if (nonKeyedCount == -1)
  217. setKeyCounts();
  218. return nonKeyedCount;
  219. }
  220. int HPCCFile::getKeyedColumnsCount()
  221. {
  222. if (keyedCount == -1)
  223. setKeyCounts();
  224. return keyedCount;
  225. }
  226. int main()
  227. {
  228. HPCCFile * file = new HPCCFile();
  229. file->setCluster("cluster");
  230. file->setName("name");
  231. delete file;
  232. return 0;
  233. }