Example #1
0
 @Test
 public void testTypes() throws Exception {
   roundTrip(
       "create persistent site s5 url='jdbc:mysql://s5/db1' user='******' password='******'", null);
   roundTrip("create persistent group sg3 add s5", null);
   roundTrip("create database mydb3 default persistent group sg3", null);
   NativeTypeCatalog tc = Singletons.require(HostService.class).getDBNative().getTypeCatalog();
   StringBuilder buf = new StringBuilder();
   buf.append("CREATE TABLE typeRoundTrip ( ").append(PEConstants.LINE_SEPARATOR);
   int counter = 0;
   for (Iterator<Map.Entry<String, NativeType>> iter = tc.getTypeCatalogEntries().iterator();
       iter.hasNext(); ) {
     Map.Entry<String, NativeType> me = iter.next();
     NativeType nt = me.getValue();
     if (nt.isUsedInCreate()) {
       if (counter > 0) buf.append(", ").append(PEConstants.LINE_SEPARATOR);
       buf.append("`f").append(++counter).append("` ").append(nt.getTypeName());
       if (nt.isUnsignedAttribute())
         buf.append(", `f")
             .append(++counter)
             .append("` ")
             .append(nt.getTypeName())
             .append(" ")
             .append(MysqlNativeType.MODIFIER_UNSIGNED);
     }
   }
   buf.append(") ");
   roundTrip(buf.toString(), "mydb3");
 }
 public String[] generateCatalog() throws PEException {
   CatalogDAO c = CatalogDAOFactory.newInstance();
   String[] current = null;
   try {
     CatalogGenerator generator = Singletons.require(CatalogGenerator.class);
     current =
         generator.buildCreateCurrentSchema(
             c,
             Singletons.require(HostService.class)
                 .getProperties()); // TODO: this looks like we are only looking up the host to get
                                    // something for the catalog. -sgossard
   } finally {
     c.close();
   }
   return current;
 }
  @Override
  public void executeRequest(
      final Worker w, final DBResultConsumer resultConsumer, CompletionHandle<Boolean> promise) {

    SQLCommand ddl =
        Singletons.require(HostService.class)
            .getDBNative()
            .getGrantPriviledgesCommand(userDeclaration, "*");
    simpleExecute(w, resultConsumer, ddl, promise);
  }
 @Override
 public String getSQL(SchemaContext sc, Emitter emitter, EmitOptions opts, boolean unused) {
   if (likeClause != null) {
     StringBuilder buf = new StringBuilder();
     buf.append("SHOW TABLE STATUS LIKE ");
     Singletons.require(DBNative.class)
         .getEmitter()
         .emitExpression(sc, sc.getValues(), likeClause, buf, -1);
     return buf.toString();
   } else return "show table status ...";
 }
  private ProjectionInfo buildProjectionMetadata(SchemaContext pc, List<ExpressionNode> proj) {
    Emitter emitter = Singletons.require(HostService.class).getDBNative().getEmitter();
    try {
      emitter.setOptions(EmitOptions.RESULTSETMETADATA);
      emitter.pushContext(pc.getTokens());
      ProjectionInfo pi = new ProjectionInfo(proj.size());
      for (int i = 0; i < proj.size(); i++) {
        ExpressionNode e = proj.get(i);
        String columnName = null;
        String aliasName = null;
        ColumnInstance ci = null;

        if (e.getSourceLocation() != null && e.getSourceLocation().isComputed()) {
          aliasName = e.getSourceLocation().getText();
        }

        if (e instanceof ExpressionAlias) {
          ExpressionAlias ea = (ExpressionAlias) e;
          Alias aname = ea.getAlias();
          if (aname != null) aliasName = PEStringUtils.dequote(aname.getSQL());
          ExpressionNode cname = ea.getTarget();
          StringBuilder buf = new StringBuilder();
          emitter.emitExpression(pc, cname, buf);
          columnName = buf.toString();
          if (cname instanceof ColumnInstance) {
            ci = (ColumnInstance) cname;
          } else {
            aliasName = PEStringUtils.dequote(aliasName);
            columnName = aliasName;
          }
        } else if (e instanceof ColumnInstance) {
          ci = (ColumnInstance) e;
          StringBuilder buf = new StringBuilder();
          emitter.emitExpression(pc, e, buf);
          columnName = buf.toString();
          aliasName = PEStringUtils.dequote(columnName);
        } else {
          if (aliasName != null) {
            // via above
            columnName = aliasName;
          } else {
            StringBuilder buf = new StringBuilder();
            emitter.emitExpression(pc, e, buf);
            columnName =
                (e instanceof LiteralExpression)
                    ? PEStringUtils.dequote(buf.toString())
                    : buf.toString();
            aliasName = columnName;
          }
        }
        ColumnInfo colInfo =
            pi.addColumn(i + 1, columnName, (aliasName == null ? columnName : aliasName));
        if (ci != null) {
          String tblName = null;
          String dbName = null;
          Column<?> backingColumn = ci.getColumn();
          TableKey tk = ci.getTableInstance().getTableKey();
          if (tk instanceof MTTableKey) {
            MTTableKey mtk = (MTTableKey) tk;
            tblName = mtk.getScope().getName().getUnqualified().getUnquotedName().get();
            PETenant tenant = mtk.getScope().getTenant(pc);
            PEDatabase pdb = tenant.getDatabase(pc);
            if (pdb.getMTMode() == MultitenantMode.ADAPTIVE)
              dbName = tenant.getName().getUnqualified().getUnquotedName().get();
            else dbName = pdb.getName().getUnqualified().getUnquotedName().get();
          } else {
            Table<?> tab = tk.getTable();
            if (tab.isInfoSchema()) {
              dbName = PEConstants.INFORMATION_SCHEMA_DBNAME;
            } else {
              Database<?> tabDb = tab.getDatabase(pc);
              if (tab.isTempTable() && (tabDb == null)) {
                tabDb = pc.getCurrentDatabase(false);
                if (tabDb == null) {
                  tabDb = pc.getAnyNonSchemaDatabase();
                }
                final TempTable tabAstempTable = ((TempTable) tab);
                tabAstempTable.setDatabase(pc, (PEDatabase) tabDb, true);
                tabAstempTable.refreshColumnLookupTable();
              }

              if (tabDb != null) {
                dbName = tabDb.getName().getUnqualified().getUnquotedName().get();
              }
            }
            tblName = tab.getName(pc).getUnqualified().getUnquotedName().get();
          }
          if (tblName != null) colInfo.setDatabaseAndTable(dbName, tblName);
          if (backingColumn instanceof PEColumn) {
            // set flags
            PEColumn pec = (PEColumn) backingColumn;
            if (!pec.isNotNullable() || pec.isNullable())
              colInfo.setAttribute(ColumnAttribute.NULLABLE);
            if (pec.isAutoIncrement()) colInfo.setAttribute(ColumnAttribute.AUTO_INCREMENT);
            if (pec.isKeyPart()) {
              colInfo.setAttribute(ColumnAttribute.KEY_PART);
              if (pec.isPrimaryKeyPart()) colInfo.setAttribute(ColumnAttribute.PRIMARY_KEY_PART);
              if (pec.isUniquePart()) colInfo.setAttribute(ColumnAttribute.UNIQUE_PART);
            }
          }
        }
      }
      return pi;
    } finally {
      emitter.popContext();
    }
  }
  @Override
  public void onStatement(String sql, SourcePosition sp, Statement s) throws Throwable {
    saveIntermediateCheckpoint();
    globalCounters.increment(GlobalCounters.PROCESSED_STATEMENTS);
    if (s instanceof EmptyStatement) {
      globalCounters.increment(GlobalCounters.EMPTY_STATEMENTS);
      return;
    }
    if (!s.isDML()) {
      trackNonDML(s, sql);
      return;
    }
    final Emitter emitter = Singletons.require(HostService.class).getDBNative().getEmitter();
    emitter.setOptions(emitOptions);
    final StringBuilder buf = new StringBuilder();
    final DMLStatement dmls = (DMLStatement) s;

    final DMLCounters incCounter = lookupDMLCounter(dmls);

    if (dmls instanceof InsertIntoValuesStatement) {
      final InsertIntoValuesStatement iivs = (InsertIntoValuesStatement) dmls;
      emitter.emitInsertPrefix(tee.getPersistenceContext(), iivs, buf);
      final String prefix = buf.toString();
      InsertEntry ie = inserts.get(prefix);
      if (ie == null) {
        final Database<?> db = dmls.getDatabase(tee.getPersistenceContext());
        ie =
            new InsertEntry(
                corpus,
                prefix,
                iivs.getColumnSpecification().size(),
                iivs.getClass().getSimpleName(),
                (db == null ? null : db.getName().get()));
        inserts.put(prefix, ie);
      }
      ie.bump(iivs.getValues().size());
    } else {
      emitter.emitStatement(tee.getPersistenceContext(), dmls, buf);
      final String p = buf.toString();
      NonInsertEntry se = byParam.get(p);
      if (se == null) {
        String shrunk = null;
        int litCount = -1;
        if (!(dmls instanceof InsertIntoValuesStatement)) {
          final CandidateParser cp = new CandidateParser(sql);
          if (cp.shrink()) {
            shrunk = cp.getShrunk();
            // also verify we get the same number of literals
            final ValueManager valueManager = tee.getPersistenceContext().getValueManager();
            litCount = cp.getLiterals().size();
            if (litCount != valueManager.getNumberOfLiterals()) {
              final ValueManager.CacheStatus cacheStatus = valueManager.getCacheStatus();
              String reason;
              switch (cacheStatus) {
                case NOCACHE_DYNAMIC_FUNCTION:
                  reason = "contains a non-cacheable dynamic function";
                  break;
                case NOCACHE_TOO_MANY_LITERALS:
                  reason = "literal count exceeded configured max_cached_plan_literals";
                  break;
                case CACHEABLE:
                default:
                  reason = "unknown";
              }
              logError(
                  sql,
                  sp,
                  "Mismatched literal size; parse="
                      + valueManager.getNumberOfLiterals()
                      + "/shrink="
                      + litCount
                      + " , reason="
                      + reason,
                  false);
            }
          } else {
            logError(sql, sp, "Unable to shrink", false);
          }
        }
        final Database<?> db = dmls.getDatabase(tee.getPersistenceContext());
        se =
            new NonInsertEntry(
                corpus,
                sql,
                dmls.getClass().getSimpleName(),
                (db == null ? null : db.getName().get()),
                litCount,
                incCounter);
        byParam.put(p, se);
        if (shrunk != null) {
          globalCounters.increment(GlobalCounters.SHRINK_CACHE_ADDS);
          byShrunk.put(shrunk, se);
        }
      }
      if (se.populationObject.getLiteralCount() >= 0) {
        literalCounts.sample(se.populationObject.getLiteralCount());
      }
      se.bump(dmlBreakout, literalCounts);
    }
  }