123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- /*##############################################################################
- HPCC SYSTEMS software Copyright (C) 2014 HPCC Systems.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ############################################################################## */
- #include "HPCCFile.hpp"
- HPCCFileFormat HPCCFile::DEFAULTFORMAT = HPCCFileFormatFlat;
- HPCCFile * HPCCFile::createHPCCFile()
- {
- return new HPCCFile();
- }
- HPCCFile::HPCCFile() : formatEnum(HPCCFileFormatUnknown), iskeyfile(false), issuperfile(false), keyedCount(-1),
- nonKeyedCount(-1), hasNestedColumns(false)
- {}
- HPCCFile::~HPCCFile()
- {
- #ifdef _DEBUG
- fprintf(stderr, "\nLeaving HPCCFile\n\t");
- #endif
- columns.kill(false);
- }
- bool HPCCFile::validateFileName(const char * fullname)
- {
- if (!fullname || !*fullname)
- return false;
- int len = strlen(fullname);
- for (int strindex = 0; strindex < len; strindex++)
- {
- switch (fullname[strindex])
- {
- case '\'':
- case '\"':
- return false;
- case '~':
- if (strindex > 0)
- return false;
- break;
- default:
- break;
- }
- }
- return true;
- }
- bool HPCCFile::getFileRecDef(StringBuffer & out, const char * structname, const char * linedelimiter, const char * recordindent)
- {
- if (this->columns.length() > 0)
- {
- out.append(structname);
- out.append(" := RECORD ");
- out.append(linedelimiter);
- ForEachItemIn(rowindex, this->columns)
- {
- out.append(recordindent);
- this->columns.item(rowindex).toEclRecString(out);
- out.append(";");
- out.append(linedelimiter);
- }
- out.append("END;");
- }
- else
- return false;
- return true;
- }
- void HPCCFile::setKeyedColumn(const char * name)
- {
- ForEachItemIn(colidx, columns)
- {
- if (strcmp(columns.item(colidx).getColumnName(), name)==0)
- {
- columns.item(colidx).setKeyedField(true);
- break;
- }
- }
- }
- bool HPCCFile::getFileRecDefwithIndexpos(HPCCColumnMetaData * fieldMetaData, StringBuffer & out, const char * structname)
- {
- if (fieldMetaData)
- {
- out.append(structname);
- out.append(" := RECORD ");
- out.append("\n");
- ForEachItemIn(rowindex, this->columns)
- {
- out.append("\t");
- this->columns.item(rowindex).toEclRecString(out);
- out.append(";");
- out.append("\n");
- }
- out.append("\t")
- .append(fieldMetaData->getColumnType())
- .append(" ")
- .append(fieldMetaData->getColumnName())
- .append(" {virtual(fileposition)};\n");
- out.append("END;");
- return true;
- }
- return false;
- }
- bool HPCCFile::setFileColumns(const char * eclString)
- {
- StringBuffer text(eclString);
- MultiErrorReceiver errs;
- OwnedHqlExpr record = parseQuery(text.str(), &errs);
- if (errs.errCount())
- {
- StringBuffer errtext;
- IError *first = errs.firstError();
- first->toString(errtext);
- ESPLOG(LogNormal, "Could not set HPCC file columns: %s", errtext.str());
- return false;
- }
- if(!record)
- return false;
- Owned<IPropertyTree> rectree = createPTree("Table", ipt_caseInsensitive);
- exportData(rectree, record);
- StringBuffer ecltype;
- StringBuffer colname;
- int colsize;
- int colindex;
- Owned<IPropertyTreeIterator> fields = rectree->getElements("Field");
- ForEach(*fields)
- {
- fields->query().getProp("@ecltype", ecltype.clear());
- if (strncmp(ecltype, "table of", 8)==0)
- setHasNestedColumns(true);
- fields->query().getProp("@name", colname.clear());
- colsize = fields->query().getPropInt("@size", -1);
- colindex = fields->query().getPropInt("@position", -1);
- Owned<HPCCColumnMetaData> col = HPCCColumnMetaData::createHPCCColumnMetaData(colname.str());
- col->setColumnType(ecltype.str());
- col->setIndex(colindex);
- col->setTableName(this->fullname.str());
- columns.append(*LINK(col));
- }
- return true;
- }
- HPCCColumnMetaData * HPCCFile::getColumn(const char * colname)
- {
- ForEachItemIn(colidx, columns)
- {
- if (strcmp(columns.item(colidx).getColumnName(), colname)==0)
- {
- return &(columns.item(colidx));
- }
- }
- return NULL;
- }
- void HPCCFile::getKeyedFieldsAsDelimitedString(char delim, const char * prefix, StringBuffer & out)
- {
- getFieldsAsDelmitedString(delim, prefix, out, true);
- }
- void HPCCFile::getNonKeyedFieldsAsDelmitedString(char delim, const char * prefix, StringBuffer & out)
- {
- getFieldsAsDelmitedString(delim, prefix, out, false);
- }
- void HPCCFile::getFieldsAsDelmitedString(char delim, const char * prefix, StringBuffer & out, bool onlykeyed)
- {
- bool isFirst = true;
- ForEachItemIn(colidx, columns)
- {
- HPCCColumnMetaData currcol = columns.item(colidx);
- if ((onlykeyed && currcol.isKeyedField()) || (!onlykeyed && !currcol.isKeyedField()))
- {
- if (!isFirst)
- {
- out.append(delim);
- out.append(" ");
- }
- if (prefix && *prefix)
- {
- out.append(prefix);
- out.append('.');
- }
- out.append(currcol.getColumnName());
- isFirst = false;
- }
- }
- }
- bool HPCCFile::containsField(SQLColumn * field, bool verifyEclType) const
- {
- const char * fieldName = field->getName();
- for (int i = 0; i < columns.length(); i++)
- {
- const char * name = columns.item(i).getColumnName();
- if (stricmp(name, fieldName)==0)
- {
- if (!verifyEclType || strcmp(columns.item(i).getColumnType(), field->getColumnType())==0)
- return true;
- return false;
- }
- }
- return false;
- }
- void HPCCFile::setKeyCounts()
- {
- nonKeyedCount = 0;
- keyedCount = 0;
- ForEachItemIn(colidx, columns)
- {
- HPCCColumnMetaData currcol = columns.item(colidx);
- if (!currcol.isKeyedField())
- nonKeyedCount++;
- else
- keyedCount++;
- }
- }
- int HPCCFile::getNonKeyedColumnsCount()
- {
- if (nonKeyedCount == -1)
- setKeyCounts();
- return nonKeyedCount;
- }
- int HPCCFile::getKeyedColumnsCount()
- {
- if (keyedCount == -1)
- setKeyCounts();
- return keyedCount;
- }
- int main()
- {
- HPCCFile * file = new HPCCFile();
- file->setCluster("cluster");
- file->setName("name");
- delete file;
- return 0;
- }
|