Search in sources :

Example 6 with StringPtr

use of com.actiontech.dble.plan.common.ptr.StringPtr in project dble by actiontech.

the class ItemFuncDateFormat method valStr.

@Override
public String valStr() {
    String format;
    MySQLTime lTime = new MySQLTime();
    int size;
    if (!isTimeFormat) {
        if (getArg0Date(lTime, MyTime.TIME_FUZZY_DATE))
            return null;
    } else {
        if (getArg0Time(lTime))
            return null;
        lTime.setDay(0);
        lTime.setMonth(0);
        lTime.setYear(0);
    }
    if ((format = args.get(1).valStr()) == null || format.length() == 0) {
        nullValue = true;
        return null;
    }
    size = formatLength(format);
    if (size < MyTime.MAX_DATE_STRING_REP_LENGTH)
        size = MyTime.MAX_DATE_STRING_REP_LENGTH;
    DateTimeFormat dateTimeFormat = new DateTimeFormat();
    dateTimeFormat.setFormat(format);
    StringPtr strPtr = new StringPtr("");
    if (!MyTime.makeDateTime(dateTimeFormat, lTime, isTimeFormat ? MySQLTimestampType.MYSQL_TIMESTAMP_TIME : MySQLTimestampType.MYSQL_TIMESTAMP_DATE, strPtr)) {
        return strPtr.get();
    }
    nullValue = true;
    return null;
}
Also used : StringPtr(com.actiontech.dble.plan.common.ptr.StringPtr) MySQLTime(com.actiontech.dble.plan.common.time.MySQLTime) DateTimeFormat(com.actiontech.dble.plan.common.time.DateTimeFormat)

Example 7 with StringPtr

use of com.actiontech.dble.plan.common.ptr.StringPtr in project dble by actiontech.

the class DruidDeleteParser method visitorParse.

@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
    String schemaName = schema == null ? null : schema.getName();
    MySqlDeleteStatement delete = (MySqlDeleteStatement) stmt;
    SQLTableSource tableSource = delete.getTableSource();
    SQLTableSource fromSource = delete.getFrom();
    if (fromSource != null) {
        tableSource = fromSource;
    }
    if (tableSource instanceof SQLJoinTableSource) {
        StringPtr sqlSchema = new StringPtr(null);
        if (!SchemaUtil.isNoSharding(sc, (SQLJoinTableSource) tableSource, stmt, schemaName, sqlSchema)) {
            String msg = "DELETE query with multiple tables is not supported, sql:" + stmt;
            throw new SQLNonTransientException(msg);
        } else {
            if (delete.getWhere() != null && !SchemaUtil.isNoSharding(sc, delete.getWhere(), schemaName, sqlSchema)) {
                String msg = "DELETE query with sub-query is not supported, sql:" + stmt;
                throw new SQLNonTransientException(msg);
            }
            String realSchema = sqlSchema.get() == null ? schemaName : sqlSchema.get();
            SchemaConfig schemaConfig = DbleServer.getInstance().getConfig().getSchemas().get(realSchema);
            rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), realSchema));
            RouterUtil.routeToSingleNode(rrs, schemaConfig.getDataNode());
            rrs.setFinishedRoute(true);
            return schema;
        }
    } else {
        SQLExprTableSource deleteTableSource = (SQLExprTableSource) tableSource;
        SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, deleteTableSource);
        if (!ServerPrivileges.checkPrivilege(sc, schemaInfo.getSchema(), schemaInfo.getTable(), CheckType.DELETE)) {
            String msg = "The statement DML privilege check is not passed, sql:" + stmt;
            throw new SQLNonTransientException(msg);
        }
        schema = schemaInfo.getSchemaConfig();
        rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema()));
        if (RouterUtil.isNoSharding(schema, schemaInfo.getTable())) {
            if (delete.getWhere() != null && !SchemaUtil.isNoSharding(sc, delete.getWhere(), schemaName, new StringPtr(schemaInfo.getSchema()))) {
                String msg = "DELETE query with sub-query is not supported, sql:" + stmt;
                throw new SQLNonTransientException(msg);
            }
            RouterUtil.routeToSingleNode(rrs, schema.getDataNode());
            return schema;
        }
        super.visitorParse(schema, rrs, stmt, visitor, sc);
        if (visitor.isHasSubQuery()) {
            String msg = "DELETE query with sub-query  is not supported, sql:" + stmt;
            throw new SQLNonTransientException(msg);
        }
        TableConfig tc = schema.getTables().get(schemaInfo.getTable());
        if (tc != null && tc.isGlobalTable()) {
            RouterUtil.routeToMultiNode(false, rrs, tc.getDataNodes(), tc.isGlobalTable());
            rrs.setFinishedRoute(true);
            return schema;
        }
    }
    return schema;
}
Also used : MySqlDeleteStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement) SQLNonTransientException(java.sql.SQLNonTransientException) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) StringPtr(com.actiontech.dble.plan.common.ptr.StringPtr) TableConfig(com.actiontech.dble.config.model.TableConfig) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLJoinTableSource(com.alibaba.druid.sql.ast.statement.SQLJoinTableSource) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Example 8 with StringPtr

use of com.actiontech.dble.plan.common.ptr.StringPtr in project dble by actiontech.

the class DruidUpdateParser method visitorParse.

public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
    MySqlUpdateStatement update = (MySqlUpdateStatement) stmt;
    SQLTableSource tableSource = update.getTableSource();
    String schemaName = schema == null ? null : schema.getName();
    if (tableSource instanceof SQLJoinTableSource) {
        StringPtr sqlSchema = new StringPtr(null);
        if (!SchemaUtil.isNoSharding(sc, (SQLJoinTableSource) tableSource, stmt, schemaName, sqlSchema)) {
            String msg = "UPDATE query with multiple tables is not supported, sql:" + stmt;
            throw new SQLNonTransientException(msg);
        } else {
            if (update.getWhere() != null && !SchemaUtil.isNoSharding(sc, update.getWhere(), schemaName, sqlSchema)) {
                String msg = "UPDATE query with sub-query is not supported, sql:" + stmt;
                throw new SQLNonTransientException(msg);
            }
            String realSchema = sqlSchema.get() == null ? schemaName : sqlSchema.get();
            SchemaConfig schemaConfig = DbleServer.getInstance().getConfig().getSchemas().get(realSchema);
            rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), realSchema));
            RouterUtil.routeToSingleNode(rrs, schemaConfig.getDataNode());
            rrs.setFinishedRoute(true);
            return schema;
        }
    } else {
        SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, (SQLExprTableSource) tableSource);
        if (!ServerPrivileges.checkPrivilege(sc, schemaInfo.getSchema(), schemaInfo.getTable(), ServerPrivileges.CheckType.UPDATE)) {
            String msg = "The statement DML privilege check is not passed, sql:" + stmt;
            throw new SQLNonTransientException(msg);
        }
        schema = schemaInfo.getSchemaConfig();
        String tableName = schemaInfo.getTable();
        rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema()));
        if (RouterUtil.isNoSharding(schema, tableName)) {
            if (update.getWhere() != null && !SchemaUtil.isNoSharding(sc, update.getWhere(), schemaName, new StringPtr(schemaInfo.getSchema()))) {
                String msg = "UPDATE query with sub-query is not supported, sql:" + stmt;
                throw new SQLNonTransientException(msg);
            }
            RouterUtil.routeToSingleNode(rrs, schema.getDataNode());
            rrs.setFinishedRoute(true);
            return schema;
        }
        super.visitorParse(schema, rrs, stmt, visitor, sc);
        if (visitor.isHasSubQuery()) {
            String msg = "UPDATE query with sub-query  is not supported, sql:" + stmt;
            throw new SQLNonTransientException(msg);
        }
        TableConfig tc = schema.getTables().get(tableName);
        if (tc.isGlobalTable()) {
            if (GlobalTableUtil.useGlobalTableCheck()) {
                String sql = convertUpdateSQL(schemaInfo, update, rrs.getStatement());
                rrs.setStatement(sql);
            }
            RouterUtil.routeToMultiNode(false, rrs, tc.getDataNodes(), tc.isGlobalTable());
            rrs.setFinishedRoute(true);
            return schema;
        }
        String partitionColumn = tc.getPartitionColumn();
        String joinKey = tc.getJoinKey();
        confirmShardColumnNotUpdated(update, schema, tableName, partitionColumn, joinKey, rrs);
        confirmChildColumnNotUpdated(update, schema, tableName);
        if (schema.getTables().get(tableName).isGlobalTable() && ctx.getRouteCalculateUnit().getTablesAndConditions().size() > 1) {
            throw new SQLNonTransientException("global table is not supported in multi table related update " + tableName);
        }
        if (ctx.getTables().size() == 0) {
            ctx.addTable(schemaInfo.getTable());
        }
    }
    return schema;
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) StringPtr(com.actiontech.dble.plan.common.ptr.StringPtr) TableConfig(com.actiontech.dble.config.model.TableConfig) MySqlUpdateStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Example 9 with StringPtr

use of com.actiontech.dble.plan.common.ptr.StringPtr in project dble by actiontech.

the class MyTime method myDatetimeToStr.

/**
 * Print a datetime value with an optional fractional part.
 *
 * @return The length of the result string.
 * @l_time The MYSQL_TIME value to print.
 * @to OUT The string pointer to print at.
 */
public static String myDatetimeToStr(final MySQLTime lTime, long dec) {
    StringPtr ptrtmp = new StringPtr("");
    timeToDatetimeStr(ptrtmp, lTime);
    String res = ptrtmp.get();
    if (dec != 0) {
    // TODO: 6 digits after Decimal point
    // String stmp = String.format("%06d", l_time.second_part);
    // res += "." + stmp.substring(0, (int) dec);
    }
    return res;
}
Also used : StringPtr(com.actiontech.dble.plan.common.ptr.StringPtr)

Aggregations

StringPtr (com.actiontech.dble.plan.common.ptr.StringPtr)9 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)4 SchemaInfo (com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)3 SQLNonTransientException (java.sql.SQLNonTransientException)3 TableConfig (com.actiontech.dble.config.model.TableConfig)2 DateTimeFormat (com.actiontech.dble.plan.common.time.DateTimeFormat)1 MySQLTime (com.actiontech.dble.plan.common.time.MySQLTime)1 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)1 SQLJoinTableSource (com.alibaba.druid.sql.ast.statement.SQLJoinTableSource)1 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)1 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)1 MySqlDeleteStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement)1 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)1 MySqlUnionQuery (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUnionQuery)1 MySqlUpdateStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement)1