Пример #1
0
 @Test
 public void test8() {
   String oriSql =
       " /*  */ insert into employee(id,name,sharding_id) values(4, 'myhome', 10011) /**/";
   String tableName = StringUtil.getTableName(oriSql);
   Assert.assertEquals("employee", tableName);
 }
Пример #2
0
 @Test
 public void test5() {
   String oriSql =
       " /* ApplicationName=DBeaver 3.3.1 - Main connection */ insert into employee(id,name,sharding_id) values(4, 'myhome', 10011)";
   String tableName = StringUtil.getTableName(oriSql);
   Assert.assertEquals("employee", tableName);
 }
Пример #3
0
 @Test
 public void test13() {
   StringWriter sw = new StringWriter();
   PrintWriter pw = new PrintWriter(sw);
   pw.println("insert into");
   pw.println("employee(id,name,sharding_id) values(4, 'myhome', 10011)");
   pw.flush();
   String oriSql = sw.toString();
   String tableName = StringUtil.getTableName(oriSql);
   Assert.assertEquals("employee", tableName);
 }
Пример #4
0
 public static boolean processInsert(
     SchemaConfig schema, int sqlType, String origSQL, ServerConnection sc)
     throws SQLNonTransientException {
   String tableName = StringUtil.getTableName(origSQL).toUpperCase();
   TableConfig tableConfig = schema.getTables().get(tableName);
   boolean processedInsert = false;
   if (null != tableConfig && tableConfig.isAutoIncrement()) {
     String primaryKey = tableConfig.getPrimaryKey();
     processedInsert = processInsert(sc, schema, sqlType, origSQL, tableName, primaryKey);
   }
   return processedInsert;
 }
Пример #5
0
  public static boolean processERChildTable(
      final SchemaConfig schema, final String origSQL, final ServerConnection sc)
      throws SQLNonTransientException {
    String tableName = StringUtil.getTableName(origSQL).toUpperCase();
    final TableConfig tc = schema.getTables().get(tableName);

    if (null != tc && tc.isChildTable()) {
      final RouteResultset rrs = new RouteResultset(origSQL, ServerParse.INSERT);
      String joinKey = tc.getJoinKey();
      MySqlInsertStatement insertStmt =
          (MySqlInsertStatement) (new MySqlStatementParser(origSQL)).parseInsert();
      int joinKeyIndex = getJoinKeyIndex(insertStmt.getColumns(), joinKey);

      if (joinKeyIndex == -1) {
        String inf = "joinKey not provided :" + tc.getJoinKey() + "," + insertStmt;
        LOGGER.warn(inf);
        throw new SQLNonTransientException(inf);
      }
      if (isMultiInsert(insertStmt)) {
        String msg = "ChildTable multi insert not provided";
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
      }

      String joinKeyVal = insertStmt.getValues().getValues().get(joinKeyIndex).toString();

      String sql = insertStmt.toString();

      // try to route by ER parent partion key
      RouteResultset theRrs = RouterUtil.routeByERParentKey(sql, rrs, tc, joinKeyVal);

      if (theRrs != null) {
        rrs.setFinishedRoute(true);
        sc.getSession2().execute(rrs, ServerParse.INSERT);
        return true;
      }

      // route by sql query root parent's datanode
      final String findRootTBSql = tc.getLocateRTableKeySql().toLowerCase() + joinKeyVal;
      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("find root parent's node sql " + findRootTBSql);
      }

      ListenableFuture<String> listenableFuture =
          MycatServer.getInstance()
              .getListeningExecutorService()
              .submit(
                  new Callable<String>() {
                    @Override
                    public String call() throws Exception {
                      FetchStoreNodeOfChildTableHandler fetchHandler =
                          new FetchStoreNodeOfChildTableHandler();
                      return fetchHandler.execute(
                          schema.getName(), findRootTBSql, tc.getRootParent().getDataNodes());
                    }
                  });

      Futures.addCallback(
          listenableFuture,
          new FutureCallback<String>() {
            @Override
            public void onSuccess(String result) {
              if (Strings.isNullOrEmpty(result)) {
                StringBuilder s = new StringBuilder();
                LOGGER.warn(
                    s.append(sc.getSession2()).append(origSQL).toString()
                        + " err:"
                        + "can't find (root) parent sharding node for sql:"
                        + origSQL);
                sc.writeErrMessage(
                    ErrorCode.ER_PARSE_ERROR,
                    "can't find (root) parent sharding node for sql:" + origSQL);
                return;
              }

              if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(
                    "found partion node for child table to insert " + result + " sql :" + origSQL);
              }

              RouteResultset executeRrs = RouterUtil.routeToSingleNode(rrs, result, origSQL);
              sc.getSession2().execute(executeRrs, ServerParse.INSERT);
            }

            @Override
            public void onFailure(Throwable t) {
              StringBuilder s = new StringBuilder();
              LOGGER.warn(
                  s.append(sc.getSession2()).append(origSQL).toString() + " err:" + t.getMessage());
              sc.writeErrMessage(ErrorCode.ER_PARSE_ERROR, t.getMessage() + " " + s.toString());
            }
          },
          MycatServer.getInstance().getListeningExecutorService());
      return true;
    }
    return false;
  }
Пример #6
0
 @Test
 public void test4() {
   String oriSql = "INSERT INTO test_activity_input  (id,vip_no";
   String tableName = StringUtil.getTableName(oriSql);
   Assert.assertEquals("test_activity_input", tableName);
 }
Пример #7
0
 @Test
 public void test3() {
   String oriSql = "  insert  into    isd(id) values (s)";
   String tableName = StringUtil.getTableName(oriSql);
   Assert.assertEquals("isd", tableName);
 }