summary refs log tree commit diff stats
path: root/generator/notion.h
Commit message (Collapse)AuthorAgeFilesLines
* Migrated generator to hkutilKelly Rauchenberger2018-03-311-8/+14
|
* Started migrating to hkutil (does not build)Kelly Rauchenberger2018-03-301-2/+0
|
* Whitespace changesKelly Rauchenberger2017-01-241-4/+4
|
* Moved some generator classes into the main namespaceKelly Rauchenberger2017-01-211-1/+1
|
* Started structural rewriteKelly Rauchenberger2017-01-161-0/+91
The new object structure was designed to build on the existing WordNet structure, while also adding in all of the data that we get from other sources. More information about this can be found on the project wiki. The generator has already been completely rewritten to generate a datafile that uses the new structure. In addition, a number of indexes are created, which does double the size of the datafile, but also allows for much faster lookups. Finally, the new generator is written modularly and is a lot more readable than the old one. The verbly interface to the new object structure has mostly been completed, but has not been tested fully. There is a completely new search API which utilizes a lot of operator overloading; documentation on how to use it should go up at some point. Token processing and verb frames are currently unimplemented. Source for these have been left in the repository for now.
n130'>130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
#ifndef STATEMENT_H_29F51659
#define STATEMENT_H_29F51659

#include <string>
#include <list>
#include <map>
#include <set>
#include "binding.h"
#include "enums.h"
#include "field.h"
#include "filter.h"

namespace verbly {

  class filter;
  class order;

  class statement {
  public:

    statement(object context, filter queryFilter);

    std::string getQueryString(std::list<std::string> select, order sortOrder, int limit, bool debug = false) const;

    std::list<binding> getBindings() const;

  private:

    class join {
    public:

      join(
        bool outer,
        std::string foreignTableName,
        std::string joinTable,
        std::string joinColumn,
        std::string foreignTable,
        std::string foreignColumn) :
          outer_(outer),
          foreignTableName_(std::move(foreignTableName)),
          joinTable_(std::move(joinTable)),
          joinColumn_(std::move(joinColumn)),
          foreignTable_(std::move(foreignTable)),
          foreignColumn_(std::move(foreignColumn))
      {
      }

      bool isOuterJoin() const
      {
        return outer_;
      }

      const std::string& getForeignTableName() const
      {
        return foreignTableName_;
      }

      const std::string& getJoinTable() const
      {
        return joinTable_;
      }

      const std::string& getJoinColumn() const
      {
        return joinColumn_;
      }

      const std::string& getForeignTable() const
      {
        return foreignTable_;
      }

      const std::string& getForeignColumn() const
      {
        return foreignColumn_;
      }

    private:
      bool outer_ = false;
      std::string foreignTableName_;
      std::string joinTable_;
      std::string joinColumn_;
      std::string foreignTable_;
      std::string foreignColumn_;

    };

    friend std::ostream& operator<<(std::ostream& oss, const join& j);

    class condition {
    public:
      enum class type {
        empty,
        singleton,
        group
      };

      enum class comparison {
        equals,
        does_not_equal,
        is_greater_than,
        is_at_most,
        is_less_than,
        is_at_least,
        is_like,
        is_not_like,
        is_not_null,
        is_null
      };

      // Copy and move constructors

      condition(const condition& other);
      condition(condition&& other);

      // Assignment

      condition& operator=(condition other);

      // Swap

      friend void swap(condition& first, condition& second);

      // Destructor

      ~condition();

      // Accessors

      type getType() const
      {
        return type_;
      }

      // Empty

      condition();

      // Singleton

      condition(std::string table, std::string column, bool isNull);

      condition(std::string table, std::string column, comparison comp, binding value, object parentObject = object::undefined);

      // Group

      explicit condition(bool orlogic);

      condition& operator+=(condition n);

      condition& operator&=(condition n);

      const std::list<condition>& getChildren() const;

      // Utility

      std::string toSql(bool toplevel, bool debug = false) const;

      std::list<binding> flattenBindings() const;

      condition flatten() const;

      condition resolveCompareFields(object context, std::string tableName) const;

    private:
      union {
        struct {
          std::string table_;
          std::string column_;
          comparison comparison_;
          binding value_;
          object parentObject_;
        } singleton_;
        struct {
          std::list<condition> children_;
          bool orlogic_;
        } group_;
      };
      type type_;
    };

    friend void swap(condition& first, condition& second);

    class with {
    public:

      with(
        std::string identifier,
        field f,
        std::map<std::string, std::string> tables,
        std::string topTable,
        condition where,
        std::list<join> joins,
        bool recursive) :
          identifier_(std::move(identifier)),
          field_(f),
          tables_(std::move(tables)),
          topTable_(std::move(topTable)),
          topCondition_(std::move(where)),
          joins_(std::move(joins)),
          recursive_(recursive)
      {
      }

      const std::string& getIdentifier() const
      {
        return identifier_;
      }

      field getField() const
      {
        return field_;
      }

      std::string getTableForId(std::string identifier) const
      {
        return tables_.at(identifier);
      }

      const std::string& getTopTable() const
      {
        return topTable_;
      }

      const condition& getCondition() const
      {
        return topCondition_;
      }

      const std::list<join>& getJoins() const
      {
        return joins_;
      }

      bool isRecursive() const
      {
        return recursive_;
      }

    private:
      std::string identifier_;
      field field_;
      std::map<std::string, std::string> tables_;
      std::string topTable_;
      condition topCondition_;
      std::list<join> joins_;
      bool recursive_;

    };

    static constexpr const char* getTableForContext(object context)
    {
      return (context == object::notion) ? "notions"
        : (context == object::word) ? "words"
        : (context == object::frame) ? "frames"
        : (context == object::part) ? "parts"
        : (context == object::form) ? "forms"
        : (context == object::pronunciation) ? "pronunciations"
        : throw std::domain_error("Provided context has no associated table");
    }

    static const std::list<field> getSelectForContext(object context);

    statement(object context, std::string tableName, filter clause, int nextTableId = 0, int nextWithId = 0);

    condition parseFilter(filter queryFilter);

    std::string instantiateTable(std::string name);

    std::string instantiateWith(std::string name);

    condition integrate(statement subStmt, bool cte = false);

    int nextTableId_;
    int nextWithId_;

    object context_;
    std::map<std::string, std::string> tables_;
    std::string topTable_;
    std::list<join> joins_;
    std::list<with> withs_;
    condition topCondition_;

  };

};

#endif /* end of include guard: STATEMENT_H_29F51659 */