Пример #1
0
  public boolean processIndex(DFUFile indexfiletouse, StringBuilder keyedandwild) {
    boolean isPayloadIndex =
        containsPayload(indexfiletouse.getAllFieldsProps(), parser.getSelectColumns().iterator());

    Vector<String> keyed = new Vector<String>();
    Vector<String> wild = new Vector<String>();

    // Create keyed and wild string
    Properties keyedcols = indexfiletouse.getKeyedColumns();
    for (int i = 1; i <= keyedcols.size(); i++) {
      String keyedcolname = (String) keyedcols.get(i);
      if (parser.whereClauseContainsKey(keyedcolname))
        keyed.add(" " + parser.getExpressionFromName(keyedcolname).toString() + " ");
      else if (keyed.isEmpty()) wild.add(" " + keyedcolname + " ");
    }

    if (isPayloadIndex) {
      if (keyed.size() > 0) {
        keyedandwild.append("KEYED( ");
        for (int i = 0; i < keyed.size(); i++) {
          keyedandwild.append(keyed.get(i));
          if (i < keyed.size() - 1) keyedandwild.append(" AND ");
        }
        keyedandwild.append(" )");
      }
      if (wild.size() > 0) {
        // TODO should I bother making sure there's a KEYED entry ?
        for (int i = 0; i < wild.size(); i++) {
          keyedandwild.append(" and WILD( ");
          keyedandwild.append(wild.get(i));
          keyedandwild.append(" )");
        }
      }

      keyedandwild.append(" and (").append(parser.getWhereClauseString()).append(" )");
    } else // non-payload just AND the keyed expressions
    {
      keyedandwild.append("( ");
      keyedandwild.append(parser.getWhereClauseString());
      keyedandwild.append(" )");
    }

    return isPayloadIndex;
  }
Пример #2
0
  public ArrayList execute() throws Exception {
    int sqlreqtype = parser.getSqlType();

    List<EclColumnMetaData> expectedretcolumns = new ArrayList();
    ArrayList<EclColumnMetaData> storeProcInParams = new ArrayList();
    String hpccfilename = "";
    String indexposfield = null;
    StringBuilder keyedandwild = new StringBuilder();
    DFUFile indexfiletouse = null;
    StringBuilder eclcode = new StringBuilder("");
    int totalparamcount = 0;
    boolean isPayloadIndex = false;

    switch (sqlreqtype) {
      case SqlParser.SQL_TYPE_SELECT:
        {
          hpccfilename = Utils.handleQuotedString(parser.getTableName());
          if (!dbMetadata.tableExists("", hpccfilename))
            throw new Exception("Invalid table found: " + hpccfilename);

          DFUFile dfufile = dbMetadata.getDFUFile(hpccfilename);

          // in future this might need to be a container of dfufile(s)
          parser.verifySelectColumns(dfufile);

          expectedretcolumns = parser.getSelectColumns();
          totalparamcount = parser.getWhereClauseExpressionsCount();

          if (indexToUseName != null) {
            indexfiletouse = dbMetadata.getDFUFile(indexToUseName);
            indexposfield = indexfiletouse.getIdxFilePosField();

            isPayloadIndex = processIndex(indexfiletouse, keyedandwild);

            eclEnteties.put("KEYEDWILD", keyedandwild.toString());
            if (isPayloadIndex) eclEnteties.put("PAYLOADINDEX", "true");
          }

          eclEnteties.put("PARAMCOUNT", Integer.toString(totalparamcount));

          if (dfufile.hasFileRecDef()) {
            if (indexfiletouse != null && indexposfield != null)
              eclcode.append(
                  dfufile.getFileRecDefwithIndexpos(
                      indexfiletouse.getFieldMetaData(indexposfield), "filerecstruct"));
            else eclcode.append(dfufile.getFileRecDef("filerecstruct"));
          } else
            throw new Exception(
                "Target HPCC file (" + hpccfilename + ") does not contain ECL record definition");

          if (!dfufile.isKeyFile())
            eclcode
                .append("fileds := DATASET(\'~")
                .append(dfufile.getFullyQualifiedName())
                .append("\', filerecstruct,")
                .append(dfufile.getFormat())
                .append("); ");
          else {
            eclcode.append("fileds := INDEX( ");
            eclcode.append('{');
            eclcode.append(dfufile.getKeyedFieldsAsDelmitedString(',', "filerecstruct"));
            eclcode.append("},{");
            eclcode.append(dfufile.getNonKeyedFieldsAsDelmitedString(',', "filerecstruct"));
            eclcode.append("},");
            eclcode.append("\'~").append(dfufile.getFullyQualifiedName()).append("\');");
          }

          StringBuilder selectstruct = new StringBuilder(" selectstruct:=RECORD ");
          String datasource = indexToUseName == null || isPayloadIndex ? "fileds" : "fetchedds";

          // boolean usescalar = expectedretcolumns.size() == 1;
          for (int i = 0; i < expectedretcolumns.size(); i++) {
            EclColumnMetaData col = expectedretcolumns.get(i);
            if (col.getColumnType() == EclColumnMetaData.COLUMN_TYPE_CONSTANT) {
              selectstruct
                  .append(col.getEclType())
                  .append(" ")
                  .append(col.getColumnName())
                  .append(" := ")
                  .append(col.getConstantValue())
                  .append("; ");
              if (i == 0 && expectedretcolumns.size() == 1)
                eclEnteties.put("SCALAROUTNAME", col.getColumnName());
            } else if (col.getColumnType() == EclColumnMetaData.COLUMN_TYPE_FNCTION) {
              if (col.getColumnName().equalsIgnoreCase("COUNT")) {
                eclEnteties.put("COUNTFN", "TRUE");
                selectstruct.append("countout := ");
                if (parser.hasGroupByColumns()) {
                  selectstruct.append(col.getColumnName().toUpperCase()).append("( GROUP");
                  List<EclColumnMetaData> funccols = col.getFunccols();

                  if (funccols.size() > 0) {
                    String paramname = funccols.get(0).getColumnName();
                    if (!paramname.equals("*")
                        && funccols.get(0).getColumnType()
                            != EclColumnMetaData.COLUMN_TYPE_CONSTANT) {
                      selectstruct.append(", ");
                      selectstruct.append(datasource);
                      selectstruct.append(".");
                      selectstruct.append(paramname);
                      selectstruct.append("<> \'\'");
                      // if (eclEnteties.size() > 0)
                      //	addFilterClause(selectstruct, eclEnteties);
                    }
                  }
                  selectstruct.append(" );");
                } else {
                  selectstruct.append(" scalarout;");
                  if (expectedretcolumns.size() == 1)
                    eclEnteties.put("SCALAROUTNAME", col.getColumnName());
                }

                col.setSQLType(java.sql.Types.NUMERIC);
              } else if (col.getColumnName().equalsIgnoreCase("MAX")) {
                eclEnteties.put("MAXFN", "TRUE");
                selectstruct.append("maxout :=  ");

                if (parser.hasGroupByColumns()) {
                  selectstruct.append("MAX( GROUP ");
                } else {
                  selectstruct.append("MAX( ").append(datasource);
                  if (eclEnteties.size() > 0) addFilterClause(selectstruct, eclEnteties);
                }

                List<EclColumnMetaData> funccols = col.getFunccols();
                if (funccols.size() > 0) {
                  String paramname = funccols.get(0).getColumnName();
                  eclEnteties.put("FNCOLS", paramname);
                  if (!paramname.equals("*")
                      && funccols.get(0).getColumnType()
                          != EclColumnMetaData.COLUMN_TYPE_CONSTANT) {
                    selectstruct.append(", ");
                    selectstruct.append(datasource);
                    selectstruct.append(".");
                    selectstruct.append(paramname);
                  }
                }
                selectstruct.append(" );");

                /*
                if (parser.hasGroupByColumns())
                {
                	selectstruct.append(col.getColumnName().toUpperCase()).append("( GROUP");
                	List<EclColumnMetaData> funccols = col.getFunccols();

                	if (funccols.size() > 0)
                	{
                		String paramname = funccols.get(0).getColumnName();
                		eclEnteties.put("FNCOLS", paramname);
                		if (!paramname.equals("*") && funccols.get(0).getColumnType() != EclColumnMetaData.COLUMN_TYPE_CONSTANT)
                		{
                			selectstruct.append(", ");
                			selectstruct.append(datasource);
                			selectstruct.append(".");
                			selectstruct.append(paramname);
                		}
                	}
                	selectstruct.append(" );");
                }
                else
                {
                	selectstruct.append(" totalmax;");
                	//if (expectedretcolumns.size() == 1)
                	//	eclEnteties.put("SCALAROUTNAME", col.getColumnName());
                }
                */
                /*

                selectstruct.append(col.getColumnName().toUpperCase()).append(" ( ");
                if (parser.hasGroupByColumns())
                	selectstruct.append("GROUP");
                else
                	selectstruct.append(datasource);

                List<EclColumnMetaData> funccols = col.getFunccols();

                if (funccols.size() > 0)
                {
                	String paramname = funccols.get(0).getColumnName();
                	if (!paramname.equals("*") && funccols.get(0).getColumnType() != EclColumnMetaData.COLUMN_TYPE_CONSTANT)
                	{
                		selectstruct.append(", ");
                		selectstruct.append(datasource);
                		selectstruct.append(".");
                		selectstruct.append(paramname);
                	}
                }
                selectstruct.append(" );");*/
              }
            } else
              selectstruct
                  .append(col.getEclType())
                  .append(" ")
                  .append(col.getColumnName())
                  .append(" := ")
                  .append(datasource)
                  .append(".")
                  .append(col.getColumnName())
                  .append("; ");

            // if (i == 0 && expectedretcolumns.size() == 1 &&  col.getColumnType() !=
            // EclColumnMetaData.COLUMN_TYPE_DATA )
            //	eclEnteties.put("SCALAROUTNAME", col.getColumnName());
          }
          selectstruct.append("END; ");

          eclEnteties.put("SELECTSTRUCT", selectstruct.toString());

          if (parser.hasOrderByColumns()) eclEnteties.put("ORDERBY", parser.getOrderByString());
          if (parser.hasGroupByColumns()) eclEnteties.put("GROUPBY", parser.getGroupByString());
          if (parser.hasLimitBy()) eclEnteties.put("LIMIT", Integer.toString(parser.getLimit()));

          return executeSelect(eclcode.toString(), eclEnteties, indexfiletouse);
        }
      case SqlParser.SQL_TYPE_SELECTCONST:
        {
          System.out.println("Processing test_query...");
          // ArrayList<EclColumnMetaData> columns = new ArrayList();
          eclcode.append("selectstruct:=RECORD ");
          expectedretcolumns = parser.getSelectColumns();
          // defaultEclQueryReturnDatasetName = "ConstECLQueryResult";
          StringBuilder ecloutput = new StringBuilder(" OUTPUT(DATASET([{ ");
          for (int i = 1; i <= expectedretcolumns.size(); i++) {
            EclColumnMetaData col = expectedretcolumns.get(i - 1);
            eclcode.append(col.getEclType()).append(" ").append(col.getColumnName()).append("; ");
            ecloutput.append(col.getConstantValue());
            if (i < expectedretcolumns.size()) ecloutput.append(", ");
          }
          ecloutput.append("}],selectstruct), NAMED(\'");
          // ecloutput.append(defaultEclQueryReturnDatasetName);
          ecloutput.append("ConstECLQueryResult");
          ecloutput.append("\'));");

          eclcode.append(" END; ");
          eclcode.append(ecloutput.toString());

          return executeSelectConstant(eclcode.toString());
        }
      case SqlParser.SQL_TYPE_CALL:
        {
          eclqueryname = Utils.handleQuotedString(parser.getStoredProcName());
          if (!dbMetadata.eclQueryExists("", eclqueryname))
            throw new Exception("Invalid store procedure found");

          return executeCall(null);
        }
      default:
        break;
    }

    return null;
  }
Пример #3
0
  public ArrayList executeSelect(String eclcode, HashMap parameters, DFUFile indexfile) {
    int responseCode = -1;
    try {
      urlString =
          "http://"
              + props.getProperty("ServerAddress")
              + ":"
              + props.getProperty("WsECLDirectPort")
              + "/EclDirect/RunEcl?Submit"; // &eclText=

      StringBuilder sb = new StringBuilder("&eclText=");
      sb.append(eclcode);

      if (indexfile == null) // no indexfile read...
      {
        if (!parameters.containsKey("GROUPBY")) {
          if (parameters.containsKey("COUNTFN")) {
            sb.append("scalarout := COUNT(fileds");
            if (parameters.size() > 0) addFilterClause(sb, parameters);
            sb.append(");");
          }

          if (parameters.containsKey("MAXFN")) {
            sb.append("scalarout := MAX(fileds");
            if (parameters.size() > 0) addFilterClause(sb, parameters);

            sb.append(" , fileds.");
            sb.append(parameters.get("FNCOLS"));
            sb.append(");");
          }
        }

        if (parameters.containsKey("SCALAROUTNAME")) {
          sb.append("OUTPUT(scalarout ,NAMED(\'");
          sb.append(parameters.get("SCALAROUTNAME"));
          sb.append("\'));");
        } else {
          sb.append(parameters.get("SELECTSTRUCT"));
          sb.append("OUTPUT(CHOOSEN(");

          if (parameters.containsKey("ORDERBY")) sb.append("SORT( ");

          sb.append("TABLE(fileds");

          if (parameters.size() > 0) addFilterClause(sb, parameters);

          sb.append(", selectstruct");
          if (parameters.containsKey("GROUPBY")) {
            sb.append(",");
            sb.append(parameters.get("GROUPBY"));
          }

          sb.append(")");

          if (parameters.containsKey("ORDERBY")) {
            sb.append(",");
            sb.append(parameters.get("ORDERBY"));
            sb.append(")");
          }
        }
      } else // use index
      {
        sb.append("IDX := INDEX(fileds, {")
            .append(indexfile.getKeyedFieldsAsDelmitedString(',', null))
            .append("}");

        if (indexfile.getNonKeyedColumnsCount() > 0)
          sb.append(",{ ")
              .append(indexfile.getNonKeyedFieldsAsDelmitedString(',', null))
              .append(" }");

        sb.append(",\'~").append(indexfile.getFullyQualifiedName()).append("\');");

        if (parameters.containsKey("PAYLOADINDEX")) {
          sb.append(" OUTPUT(CHOOSEN(");
          if (parameters.containsKey("ORDERBY")) sb.append("SORT( ");

          sb.append("IDX(").append(parameters.get("KEYEDWILD")).append(")");
          if (parameters.containsKey("ORDERBY")) {
            sb.append(",");
            sb.append(parameters.get("ORDERBY"));
            sb.append(")");
          }
        } else {
          sb.append("fetchedds := FETCH(fileds, IDX( ");

          sb.append(parameters.get("KEYEDWILD"));
          sb.append("), RIGHT.");
          sb.append(indexfile.getIdxFilePosField()).append(");");

          if (!parameters.containsKey("GROUPBY")) {
            if (parameters.containsKey("COUNTFN")) sb.append("scalarout := COUNT(fetchedds);");
            if (parameters.containsKey("MAXFN"))
              sb.append("scalarout := MAX(fetchedds, fileds.zip);");
          }

          if (parameters.containsKey("SCALAROUTNAME")) {
            sb.append("OUTPUT(scalarout ,NAMED(\'");
            sb.append(parameters.get("SCALAROUTNAME"));
            sb.append("\'));");
          } else {

            sb.append(parameters.get("SELECTSTRUCT"));

            sb.append(" IDXTABLE := TABLE(fetchedds , selectstruct ");

            if (parameters.containsKey("GROUPBY")) {
              sb.append(", ");
              sb.append(parameters.get("GROUPBY"));
            }

            sb.append("); ");

            sb.append(" OUTPUT(CHOOSEN(");
            sb.append(" IDXTABLE ");
          }

          /*{
          	sb.append(" OUTPUT(CHOOSEN(");
          	if (parameters.containsKey("ORDERBY"))
          		sb.append("SORT( ");

          	sb.append("fetchedds");
          	if (parameters.size() > 0)
          		addFilterClause(sb, parameters);

          	if (parameters.containsKey("ORDERBY"))
          	{
          		sb.append(",");
          		sb.append(parameters.get("ORDERBY"));
          		sb.append(")");
          	}
          }*/
        }
      }

      if (!parameters.containsKey("SCALAROUTNAME")) {
        sb.append(",");
        if (parameters.containsKey("LIMIT")) sb.append(parameters.get("LIMIT"));
        else sb.append(props.getProperty("EclLimit"));
        sb.append("),NAMED(\'ECLJDBCSelectOutput\'));");
      }

      System.out.println("WSECL:executeSelect: " + urlString + sb.toString());

      // Send data
      long startTime = System.currentTimeMillis();

      URL url = new URL(urlString);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setInstanceFollowRedirects(false);
      conn.setRequestProperty("Authorization", basicAuth);
      conn.setRequestMethod("GET");
      conn.setDoOutput(true);
      conn.setDoInput(true);

      OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
      wr.write(sb.toString());
      wr.flush();

      responseCode = conn.getResponseCode();

      return parse(conn.getInputStream(), startTime);
    } catch (Exception e) {
      if (responseCode != 200) {
        throw new RuntimeException(
            "HTTP Connection Response code: " + responseCode + " verify access to WsECLDirect", e);
      } else throw new RuntimeException(e);
    }
  }