All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TemplateEncoder.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/dataaccess/query/TemplateEncoder.cpp
22 
23  \brief A query encoder for binary operator expressions.
24 */
25 
26 // TerraLib
27 #include "Function.h"
28 #include "SQLVisitor.h"
29 #include "TemplateEncoder.h"
30 
31 // STL
32 #include <cassert>
33 #include <cstdlib>
34 
36  const std::string& t)
37  : SQLFunctionEncoder(name)
38 {
39  std::size_t len = t.length();
40  std::string chunk;
41  std::string idx;
42 
43  for(std::size_t i = 0; i < len; ++i)
44  {
45  char c = t[i];
46 
47  if(c == '$')
48  {
49  if(chunk.empty() == false)
50  {
51  m_chuncks.push_back(chunk);
52  chunk = "";
53  }
54 
55  while(++i < len)
56  {
57  c = t[i];
58 
59  if(isdigit(c))
60  {
61  idx += c;
62  }
63  else
64  {
65  chunk += c;
66  break;
67  }
68  }
69 
70  m_argIdx.push_back(atoi(idx.c_str()));
71  idx = "";
72  }
73  else
74  {
75  chunk += c;
76  }
77  }
78 
79  if(chunk.empty() == false)
80  m_chuncks.push_back(chunk);
81 }
82 
84  std::string& buff,
85  SQLVisitor& v) const
86 {
87  assert(f.getNumArgs() == m_argIdx.size());
88  std::size_t nchunks = m_chuncks.size();
89  std::size_t nargs = m_argIdx.size();
90 
91  buff += m_name;
92 
93  for(size_t i = 0; i < nchunks; ++i)
94  {
95  if(i < nargs)
96  {
97  buff += m_chuncks[i];
98  assert(f[i]);
99  f[i]->accept(v);
100  }
101  else
102  {
103  buff += m_chuncks[i];
104  }
105  }
106 }
107 
void toSQL(const Function &f, std::string &buff, SQLVisitor &v) const
It encodes the function to a SQL notation.
std::vector< std::size_t > m_argIdx
The arguments index.
A class that models a Function expression.
A base class for encoders of SQL function expressions.
A query encoder for functions that needs special translation.
A visitor for building an SQL statement from a given Query hierarchy.
std::vector< std::string > m_chuncks
We split the input template into some pieces and make them prepared to concatenate.
A class that models a Function expression.
Definition: Function.h:47
virtual ReturnType accept(VisitorType &guest) const =0
It call the visit method from the guest object.
A visitor for building an SQL statement from a given Query hierarchy.
Definition: SQLVisitor.h:58
std::size_t getNumArgs() const
It returns the number of arguments informed to the function.
Definition: Function.cpp:62
TemplateEncoder(const std::string &name, const std::string &t)
Constructor.