Search in sources :

Example 1 with MycatException

use of io.mycat.MycatException in project Mycat2 by MyCATApache.

the class HandshakePacket method readPayload.

public void readPayload(MySQLPayloadReadView buffer) {
    protocolVersion = buffer.readByte();
    if (protocolVersion != 0x0a) {
        throw new MycatException("unsupport HandshakeV9");
    }
    serverVersion = buffer.readNULString();
    connectionId = buffer.readFixInt(4);
    authPluginDataPartOne = buffer.readFixString(8);
    buffer.skipInReading(1);
    capabilities = new MySQLServerCapabilityFlags((int) buffer.readFixInt(2) & 0x0000ffff);
    if (!buffer.readFinished()) {
        hasPartTwo = true;
        characterSet = buffer.readByte();
        statusFlags = (int) buffer.readFixInt(2);
        long l = buffer.readFixInt(2) << 16;
        capabilities.value |= l;
        if (capabilities.isPluginAuth()) {
            byte b = buffer.readByte();
            authPluginDataLen = Byte.toUnsignedInt(b);
        } else {
            buffer.skipInReading(1);
        }
        // reserved = buffers.readFixString(10);
        buffer.skipInReading(10);
        if (capabilities.isCanDo41Anthentication()) {
            // todo check length in range [13.authPluginDataLen)
            // authPluginDataPartTwo = buffers.readFixString(13);
            authPluginDataPartTwo = buffer.readNULString();
        }
        if (capabilities.isPluginAuth()) {
            authPluginName = buffer.readNULString();
        }
    }
}
Also used : MycatException(io.mycat.MycatException) MySQLServerCapabilityFlags(io.mycat.config.MySQLServerCapabilityFlags)

Example 2 with MycatException

use of io.mycat.MycatException in project Mycat2 by MyCATApache.

the class DefaultConnection method executeQuery.

public RowBaseIterator executeQuery(String sql) {
    try {
        Statement statement = connection.createStatement();
        statement.setFetchSize(1);
        ResultSet resultSet = statement.executeQuery(sql);
        return new JdbcRowBaseIterator(null, this, statement, resultSet, new RowIteratorCloseCallback() {

            @Override
            public void onClose(long rowCount) {
            }
        }, sql);
    } catch (Exception e) {
        throw new MycatException(e);
    }
}
Also used : JdbcRowBaseIterator(io.mycat.beans.mycat.JdbcRowBaseIterator) MycatException(io.mycat.MycatException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) MycatException(io.mycat.MycatException) SQLException(java.sql.SQLException)

Example 3 with MycatException

use of io.mycat.MycatException in project Mycat2 by MyCATApache.

the class JdbcConnectionManager method getConnection.

public DefaultConnection getConnection(String name, Boolean autocommit, int transactionIsolation) {
    name = resolveDataSource(name);
    final JdbcDataSource key = dataSourceMap.get(name);
    Objects.requireNonNull(key, "unknown target:" + name);
    DefaultConnection defaultConnection;
    Connection connection = null;
    switch(key.getDbType()) {
        default:
            {
                try {
                    DatasourceConfig config = key.getConfig();
                    connection = key.getDataSource().getConnection();
                    defaultConnection = new DefaultConnection(connection, key, autocommit, transactionIsolation, this);
                    LOGGER.debug("get connection:{} {}", name, defaultConnection);
                    if (config.isInitSqlsGetConnection()) {
                        if (config.getInitSqls() != null && !config.getInitSqls().isEmpty()) {
                            try (Statement statement = connection.createStatement()) {
                                for (String initSql : config.getInitSqls()) {
                                    statement.execute(initSql);
                                }
                            }
                        }
                    }
                    key.counter.getAndIncrement();
                    return defaultConnection;
                } catch (SQLException e) {
                    if (connection != null) {
                        JdbcUtils.close(connection);
                    }
                    LOGGER.debug("", e);
                    throw new MycatException(e);
                }
            }
        case "mongodb":
            {
                try {
                    Properties properties = new Properties();
                    String username = key.getUsername();
                    String password = key.getPassword();
                    if (username != null) {
                        properties.setProperty("username", username);
                    }
                    if (password != null) {
                        properties.setProperty("password", password);
                    }
                    Connection connect = new MongoDriver().connect(key.getUrl(), properties);
                    return new DefaultConnection(connect, key, autocommit, transactionIsolation, this);
                } catch (SQLException ex) {
                    throw new MycatException(ex);
                }
            }
    }
}
Also used : DatasourceConfig(io.mycat.config.DatasourceConfig) MycatException(io.mycat.MycatException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) MongoDriver(io.mycat.datasource.jdbc.mongodb.MongoDriver)

Example 4 with MycatException

use of io.mycat.MycatException in project Mycat2 by MyCATApache.

the class JdbcConnectionManager method createDatasourceProvider.

private static DatasourceProvider createDatasourceProvider(String customerDatasourceProvider) {
    String defaultDatasourceProvider = Optional.ofNullable(customerDatasourceProvider).orElse(DruidDatasourceProvider.class.getName());
    try {
        DatasourceProvider o = (DatasourceProvider) Class.forName(defaultDatasourceProvider).getDeclaredConstructor().newInstance();
        ServerConfig serverConfig = MetaClusterCurrent.exist(ServerConfig.class) ? MetaClusterCurrent.wrapper(ServerConfig.class) : new ServerConfig();
        o.init(serverConfig);
        return o;
    } catch (Exception e) {
        throw new MycatException("can not load datasourceProvider:{}", customerDatasourceProvider);
    }
}
Also used : DruidDatasourceProvider(io.mycat.datasource.jdbc.DruidDatasourceProvider) DruidDatasourceProvider(io.mycat.datasource.jdbc.DruidDatasourceProvider) DatasourceProvider(io.mycat.datasource.jdbc.DatasourceProvider) ServerConfig(io.mycat.config.ServerConfig) MycatException(io.mycat.MycatException) MycatException(io.mycat.MycatException) SQLException(java.sql.SQLException)

Example 5 with MycatException

use of io.mycat.MycatException in project Mycat2 by MyCATApache.

the class MycatCalciteMySqlNodeVisitor method buildIdentifier.

SqlNode buildIdentifier(SQLPropertyExpr x) {
    String name = SQLUtils.normalize(x.getName());
    if ("*".equals(name)) {
        name = "";
    }
    SQLExpr owner = x.getOwner();
    List<String> names;
    if (owner instanceof SQLIdentifierExpr) {
        names = Arrays.asList(((SQLIdentifierExpr) owner).normalizedName(), name);
    } else if (owner instanceof SQLPropertyExpr) {
        names = new ArrayList<String>();
        buildIdentifier((SQLPropertyExpr) owner, names);
        names.add(name);
    } else if (owner instanceof SQLVariantRefExpr) {
        return handleSQLVariantRefExpr(name, (SQLVariantRefExpr) owner);
    } else {
        throw new MycatException("not support : " + owner);
    }
    return new SqlIdentifier(names, SqlParserPos.ZERO);
}
Also used : MycatException(io.mycat.MycatException) TimeString(org.apache.calcite.util.TimeString) DateString(org.apache.calcite.util.DateString) TimestampString(org.apache.calcite.util.TimestampString)

Aggregations

MycatException (io.mycat.MycatException)31 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)3 SQLName (com.alibaba.druid.sql.ast.SQLName)3 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)3 MySQLClientSession (io.mycat.proxy.session.MySQLClientSession)3 SQLException (java.sql.SQLException)3 CalciteSqlBasicCall (com.alibaba.druid.support.calcite.CalciteSqlBasicCall)2 ColumnDefPacket (io.mycat.beans.mysql.packet.ColumnDefPacket)2 DatasourceConfig (io.mycat.config.DatasourceConfig)2 ProxyBufferImpl (io.mycat.proxy.buffer.ProxyBufferImpl)2 ReactorEnvThread (io.mycat.proxy.reactor.ReactorEnvThread)2 Future (io.vertx.core.Future)2 PromiseInternal (io.vertx.core.impl.future.PromiseInternal)2 Statement (java.sql.Statement)2 DateString (org.apache.calcite.util.DateString)2 TimeString (org.apache.calcite.util.TimeString)2 TimestampString (org.apache.calcite.util.TimestampString)2 SQLBooleanExpr (com.alibaba.druid.sql.ast.expr.SQLBooleanExpr)1 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)1 SQLShowTablesStatement (com.alibaba.druid.sql.ast.statement.SQLShowTablesStatement)1