Package ch.uzh.ifi.ddis.ifp.esper.cassandra

Source Code of ch.uzh.ifi.ddis.ifp.esper.cassandra.QueryStringBuilder

package ch.uzh.ifi.ddis.ifp.esper.cassandra;

/*
* #%L
* Cassandra for Esper
* %%
* Copyright (C) 2013 University of Zurich
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program.  If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

import java.util.ArrayList;
import java.util.List;

import com.espertech.esper.client.EventType;
import com.espertech.esper.client.hook.VirtualDataWindowContext;
import com.espertech.esper.client.hook.VirtualDataWindowLookupContext;
import com.espertech.esper.client.hook.VirtualDataWindowLookupFieldDesc;

/**
* <p>
* Helper class for building a CQL query string from context information.
* </p>
*
* <p>
* The projection part (i.e., the fields for the SELECT) are taken from
* {@link com.espertech.esper.client.EventType#getPropertyNames()}. The event
* type is taken from {@link VirtualDataWindowContext#getEventType()}.
* </p>
*
* <p>
* If the {@link VirtualDataWindowLookupContext} provides hash fields, then
* these are added as parameters to the WHERE clause.
* </p>
*
* @author Thomas Scharrenbach
* @version 0.2.3
* @since 0.2.0
*
*/
public class QueryStringBuilder {

  /**
   * The value of the field to use for the primary key.
   */
  public static final String TABLE_ID = "cassandraid";

  /**
   *
   * @param context
   * @param lookupContext
   * @param table
   * @return
   *
   * @deprecated use
   *             {@link #createSelectQueryString(VirtualDataWindowContext, VirtualDataWindowLookupContext, String)}
   */
  public static synchronized String createQueryString(
      VirtualDataWindowContext context,
      VirtualDataWindowLookupContext lookupContext, String table) {
    return createSelectQueryString(context, lookupContext, table);
  }

  /**
   *
   * @param context
   * @param lookupContext
   * @param table
   * @return
   */
  public static synchronized String createSelectQueryString(
      VirtualDataWindowContext context,
      VirtualDataWindowLookupContext lookupContext, String table) {
    final StringBuffer queryBuffer = new StringBuffer();

    final String[] propertyNames = context.getEventType()
        .getPropertyNames();

    boolean firstColumn = true;
    queryBuffer.append("SELECT ");
    for (String propertyName : propertyNames) {
      if (firstColumn) {
        firstColumn = false;
      } else {
        queryBuffer.append(", ");
      }
      queryBuffer.append(String.format("%s ", propertyName));
    }

    queryBuffer.append(String.format("FROM %s ", table));

    // Only add where clause if hints for hash or btree lookups are
    // provided.
    List<VirtualDataWindowLookupFieldDesc> hashAndBTreeFields = new ArrayList<VirtualDataWindowLookupFieldDesc>();
    hashAndBTreeFields.addAll(lookupContext.getHashFields());
    //hashAndBTreeFields.addAll(lookupContext.getBtreeFields());
    if (!hashAndBTreeFields.isEmpty()) {

      queryBuffer.append("WHERE ");

      boolean firstStatement = true;

      for (VirtualDataWindowLookupFieldDesc field : hashAndBTreeFields) {
        if (firstStatement) {
          firstStatement = false;
        } else {
          queryBuffer.append(" AND ");
        }
        queryBuffer.append(String.format(" %s %s ?",
            field.getPropertyName(), field.getOperator().getOp()));
      }
    }
    return queryBuffer.toString();
  }

  /**
   *
   * @param context
   * @param propertyNames
   * @return
   */
  public static synchronized String createUpdateQuery(
      VirtualDataWindowContext context, String[] propertyNames) {
    final String table = context.getNamedWindowName();

    final StringBuffer queryBuffer = new StringBuffer();

    queryBuffer.append(String.format("UPDATE %s SET ", table));

    boolean firstColumn = true;
    for (String propertyName : propertyNames) {
      // never overwrite the primary key
      if (TABLE_ID.equals(propertyName)) {
        continue;
      } else {
        if (firstColumn) {
          firstColumn = false;
        } else {
          queryBuffer.append(", ");
        }
        queryBuffer.append(String.format("%s = ?", propertyName));
      }
    }
    queryBuffer.append(String.format(" WHERE %s = ?", TABLE_ID));

    return queryBuffer.toString();
  }

  /**
   *
   * @param context
   * @return
   */
  public static synchronized String createInsertQuery(
      VirtualDataWindowContext context) {
    final String table = context.getNamedWindowName();
    final EventType eventType = context.getEventType();

    final StringBuffer queryBuffer = new StringBuffer();

    queryBuffer.append(String.format("INSERT INTO %s ", table));
    queryBuffer.append("( ");
    boolean firstColumn = true;
    final String[] propertyNames = eventType.getPropertyNames();
    for (String propertyName : propertyNames) {
      if (firstColumn) {
        firstColumn = false;
      } else {
        queryBuffer.append(", ");
      }
      queryBuffer.append(propertyName);
    }
    queryBuffer.append(" )");
    queryBuffer.append("VALUES ( ");
    firstColumn = true;
    for (String propertyName : propertyNames) {
      if (firstColumn) {
        firstColumn = false;
      } else {
        queryBuffer.append(", ");
      }
      queryBuffer.append(String.format("?", propertyName));
    }
    queryBuffer.append(" )");

    return queryBuffer.toString();
  }
 
  /**
   *
   * @param context
   * @return
   */
  public static synchronized String createDeleteQuery(
      VirtualDataWindowContext context) {
    final String table = context.getNamedWindowName();

    final StringBuffer queryBuffer = new StringBuffer();

    queryBuffer.append(String.format("DELETE FROM %s ", table));
    queryBuffer.append(String.format(" WHERE %s = ?", TABLE_ID));

    return queryBuffer.toString();
  }


  /**
   *
   * @param context
   * @return
   */
  public static synchronized String createCreateTableQuery(
      VirtualDataWindowContext context) {
    final String table = context.getNamedWindowName();
    final EventType eventType = context.getEventType();

    final StringBuffer queryBuffer = new StringBuffer();

    queryBuffer.append(String.format("CREATE TABLE %s ", table));
    queryBuffer.append("( ");
    // queryBuffer.append(String.format("%s uuid, ", TABLE_ID));
    boolean firstColumn = true;
    final DatatypeRegistry registry = DatatypeRegistry.newInstance();

    final String[] propertyNames = eventType.getPropertyNames();
    for (String propertyName : propertyNames) {
      // final EventPropertyDescriptor propertyDescriptor = eventType
      // .getPropertyDescriptor(propertyName);
      if (firstColumn) {
        firstColumn = false;
      } else {
        queryBuffer.append(", ");
      }
      Class<?> esperPropertyType = eventType
          .getPropertyType(propertyName);
      String propertyType = registry.get(esperPropertyType).getName()
          .name();
      queryBuffer.append(String.format("%s %s", propertyName,
          propertyType));
    }
    queryBuffer.append(String.format(", PRIMARY KEY ( %s )", TABLE_ID));
    queryBuffer.append(" )");
    return queryBuffer.toString();
  }

  public static synchronized List<String> createIndexQueryStringList(
      VirtualDataWindowLookupContext lookupContext, String table) {

    List<String> resultsList = new ArrayList<String>();
    List<VirtualDataWindowLookupFieldDesc> hashAndBTreeFields = new ArrayList<VirtualDataWindowLookupFieldDesc>();
    hashAndBTreeFields.addAll(lookupContext.getHashFields());
    hashAndBTreeFields.addAll(lookupContext.getBtreeFields());

    for (VirtualDataWindowLookupFieldDesc fieldDescription : hashAndBTreeFields) {
      final StringBuffer queryBuffer = new StringBuffer();
      queryBuffer.append(String.format("CREATE INDEX ON %s(%s)", table,
          fieldDescription.getPropertyName()));
      resultsList.add(queryBuffer.toString());
    }
    return resultsList;
  }

}
TOP

Related Classes of ch.uzh.ifi.ddis.ifp.esper.cassandra.QueryStringBuilder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.