123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- /*##############################################################################
- 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.
- ############################################################################## */
- #ifndef HPCCFILE_HPP_
- #define HPCCFILE_HPP_
- #include "ws_sql.hpp"
- #include "SQLColumn.hpp"
- #include "ws_sql_esp.ipp"
- #include "hqlerror.hpp"
- #include "hqlexpr.hpp"
- /* undef SOCKET definitions to avoid collision in Antlrdefs.h*/
- #ifdef INVALID_SOCKET
- //#pragma message( "UNDEFINING INVALID_SOCKET - Will be redefined by ANTLRDEFS.h" )
- #undef INVALID_SOCKET
- #endif
- #ifdef SOCKET
- //#pragma message( "UNDEFINING SOCKET - Will be redefined by ANTLRDEFS.h" )
- #undef SOCKET
- #endif
- #include "HPCCSQLLexer.h"
- #include "HPCCSQLParser.h"
- typedef enum _HPCCFileFormat
- {
- HPCCFileFormatUnknown=-1,
- HPCCFileFormatFlat,
- HPCCFileFormatCSV,
- HPCCFileFormatXML,
- HPCCFileFormatKey,
- HPCCFileFormatJSON,
- } HPCCFileFormat;
- class HPCCFile : public CInterface, public IInterface
- {
- public:
- static HPCCFileFormat DEFAULTFORMAT;
- IMPLEMENT_IINTERFACE;
- HPCCFile();
- virtual ~HPCCFile();
- const char * getCluster() const
- {
- return cluster.str();
- }
- void setCluster(const char * cluster)
- {
- this->cluster.set(cluster);
- }
- HPCCColumnMetaData * getColumn(const char * colname);
- IArrayOf<HPCCColumnMetaData> * getColumns()
- {
- return &columns;
- }
- const char * getEcl() const
- {
- return ecl;
- }
- bool setEcl(const char * ecl)
- {
- if (setFileColumns(ecl))
- this->ecl = ecl;
- else
- return false;
- return true;
- }
- static const char * formatToString(HPCCFileFormat format)
- {
- switch(format)
- {
- case HPCCFileFormatFlat:
- return "FLAT";
- case HPCCFileFormatCSV:
- return "CSV";
- case HPCCFileFormatXML:
- return "XML";
- case HPCCFileFormatKey:
- return "KEYED";
- case HPCCFileFormatJSON:
- return "JSON";
- case HPCCFileFormatUnknown:
- default:
- return "UNKNOWN";
- }
- }
- const char * getFormat() const
- {
- return formatToString(formatEnum);
- }
- static HPCCFileFormat formatStringToEnum(const char * formatstr)
- {
- if (!formatstr || !*formatstr)
- return HPCCFileFormatUnknown;
- else
- {
- StringBuffer toUpper(formatstr);
- toUpper.trim().toUpperCase();
- if (strcmp(toUpper.str(), "FLAT")==0)
- return HPCCFileFormatFlat;
- else if (strcmp(toUpper.str(), "UTF8N")==0)
- return HPCCFileFormatCSV;
- else if (strcmp(toUpper.str(), "CSV")==0)
- return HPCCFileFormatCSV;
- else if (strcmp(toUpper.str(), "XML")==0)
- return HPCCFileFormatXML;
- else if (strcmp(toUpper.str(), "KEY")==0)
- return HPCCFileFormatKey;
- else if (strcmp(toUpper.str(), "JSON")==0)
- return HPCCFileFormatJSON;
- else
- return HPCCFileFormatUnknown;
- }
- }
- void setFormat(const char * format)
- {
- HPCCFileFormat formatenum = formatStringToEnum(format);
- if (formatenum == HPCCFileFormatUnknown)
- this->formatEnum = DEFAULTFORMAT;
- else
- this->formatEnum = formatenum;
- }
- const char * getFullname() const
- {
- return fullname.str();
- }
- void setFullname(const char * fullname)
- {
- this->fullname.set(fullname);
- }
- bool isFileKeyed() const
- {
- return iskeyfile;
- }
- void setIsKeyedFile(bool iskeyfile)
- {
- this->iskeyfile = iskeyfile;
- }
- bool isFileSuper() const
- {
- return issuperfile;
- }
- void setIsSuperfile(bool issuperfile)
- {
- this->issuperfile = issuperfile;
- }
- const char * getName() const
- {
- return name.str();
- }
- void setName(const char * name)
- {
- this->name.set(name);
- }
- static bool validateFileName(const char * fullname);
- void setKeyedColumn(const char * name);
- bool getFileRecDefwithIndexpos(HPCCColumnMetaData * fieldMetaData, StringBuffer & out, const char * structname);
- bool getFileRecDef(StringBuffer & out, const char * structname, const char * linedelimiter = "\n", const char * recordindent = "\t");
- int getNonKeyedColumnsCount();
- int getKeyedColumnsCount();
- void getKeyedFieldsAsDelimitedString(char delim, const char * prefix, StringBuffer & out);
- void getNonKeyedFieldsAsDelmitedString(char delim, const char * prefix, StringBuffer & out);
- const char * getIdxFilePosField()
- {
- return idxFilePosField.length() > 0 ? idxFilePosField.str() : getLastNonKeyedNumericField();
- }
- bool hasValidIdxFilePosField()
- {
- const char * posfield = getIdxFilePosField();
- return (posfield && *posfield);
- }
- static HPCCFile * createHPCCFile();
- bool containsField(SQLColumn * field, bool verifyEclType) const;
- const char * getOwner() const
- {
- return owner.str();
- }
- void setOwner(const char * owner)
- {
- this->owner.set(owner);
- }
- const char* getDescription() const
- {
- return description.str();
- }
- void setDescription(const char* description)
- {
- if (description && *description)
- {
- this->description.set(description);
- const char * pos = strstr(description, "XDBC:RelIndexes");
- if (pos)
- {
- pos = pos + 15;//advance to end of "XDBC:RelIndexes"
- while(pos && *pos) //find the = char
- {
- if (!isspace(*pos))
- {
- if (*pos == '=' )
- {
- pos++;
- while(pos && *pos) //find the beginning bracket
- {
- if (!isspace(*pos))
- {
- if (*pos == '[' )
- {
- pos++;
- break;
- }
- else
- return;//found invalid char before [
- }
- pos++;
- }
- break;
- }
- else
- return;//found invalid char before = char
- }
- pos++;
- }
- if ( pos && *pos) //found keyword
- setRelatedIndexes(pos);
- }
- }
- }
- static bool parseOutRelatedIndexes(StringBuffer & description, StringBuffer & releatedIndexes)
- {
- if (description.length() > 0)
- {
- const char * head = strstr(description.str(), "XDBC:RelIndexes");
- if (head && *head)
- {
- const char * tail = strchr(head, ']');
- if (tail && *tail)
- {
- description.remove(head-description.str(), tail-description.str()).trim();
- return true;
- }
- }
- }
- return false;
- }
- void getRelatedIndexes(StringArray & indexes)
- {
- ForEachItemIn(c, relIndexFiles)
- {
- indexes.append(relIndexFiles.item(c));
- }
- }
- const char * getRelatedIndex(int relindexpos)
- {
- if (relindexpos > -1 && relindexpos < relIndexFiles.length())
- return relIndexFiles.item(relindexpos);
- else
- return NULL;
- }
- int getRelatedIndexCount()
- {
- return relIndexFiles.length();
- }
- /*
- tutorial::yn::peoplebyzipindex;
- Tutorial.IDX_PeopleByName;
- Tutorial.IDX_PeopleByPhonetic]
- */
- void setRelatedIndexes(const char * str)
- {
- StringBuffer index;
- while (str && *str)
- {
- if (!isspace(*str))
- {
- if (*str == ';' || *str == ']')
- {
- relIndexFiles.append(index.str());
- if (*str == ']')
- break;
- else
- index.clear();
- }
- else
- index.append(*str);
- }
- str++;
- }
- }
- void setIdxFilePosField(const char* idxfileposfieldname)
- {
- idxFilePosField.set(idxfileposfieldname);
- }
- bool containsNestedColumns() const
- {
- return hasNestedColumns;
- }
- void setHasNestedColumns(bool hasNestedColumns)
- {
- this->hasNestedColumns = hasNestedColumns;
- }
- private:
- void getFieldsAsDelmitedString(char delim, const char* prefix, StringBuffer& out, bool onlykeyed);
- bool setFileColumns(const char* eclString);
- void setKeyCounts();
- const char* getLastNonKeyedNumericField()
- {
- for (int i = columns.length() - 1;i >= 0;i--)
- {
- if (!columns.item(i).isKeyedField())
- return columns.item(i).getColumnName();
- }
- return NULL;
- }
- private:
- HPCCFileFormat formatEnum;
- StringBuffer name;
- StringBuffer fullname;
- StringBuffer cluster;
- StringBuffer idxFilePosField;
- bool iskeyfile;
- bool issuperfile;
- StringBuffer ecl;
- IArrayOf<HPCCColumnMetaData> columns;
- int keyedCount;
- int nonKeyedCount;
- bool hasNestedColumns;
- StringBuffer description;
- StringBuffer owner;
- StringArray relIndexFiles;
- }
- ;
- #endif /* HPCCFILE_HPP_ */
|