Ejemplo n.º 1
0
 public bool IsGenerateSqlToken(ISqlCommandContext <ISqlCommand> sqlCommandContext)
 {
     if (sqlCommandContext.GetSqlCommand() is ShowTablesCommand showTablesCommand)
     {
         return(showTablesCommand.GetFromSchema() != null);
     }
     if (sqlCommandContext.GetSqlCommand() is ShowTableStatusCommand showTableStatusCommand)
     {
         return(showTableStatusCommand.GetFromSchema() != null);
     }
     if (sqlCommandContext.GetSqlCommand() is ShowColumnsCommand showColumnsCommand)
     {
         return(showColumnsCommand.GetFromSchema() != null);
     }
     return(false);
 }
        public static IShardingRouteEngine NewInstance(ShardingRule shardingRule,
                                                       ShardingConnectorMetaData metaData, ISqlCommandContext <ISqlCommand> sqlCommandContext,
                                                       ShardingConditions shardingConditions, ConfigurationProperties properties)
        {
            var sqlStatement = sqlCommandContext.GetSqlCommand();
            ICollection <String> tableNames = sqlCommandContext.GetTablesContext().GetTableNames();

            if (sqlStatement is TCLCommand)
            {
                return(new ShardingDatabaseBroadcastRoutingEngine());
            }
            if (sqlStatement is DDLCommand)
            {
                return(new ShardingTableBroadcastRoutingEngine(metaData.Schema, sqlCommandContext));
            }
            if (sqlStatement is DALCommand)
            {
                return(GetDALRoutingEngine(shardingRule, sqlStatement, tableNames));
            }
            if (sqlStatement is DCLCommand)
            {
                return(GetDCLRoutingEngine(sqlCommandContext, metaData));
            }
            if (shardingRule.IsAllInDefaultDataSource(tableNames))
            {
                return(new ShardingDefaultDatabaseRoutingEngine(tableNames));
            }
            if (shardingRule.IsAllBroadcastTables(tableNames))
            {
                if (sqlStatement is SelectCommand)
                {
                    return(new ShardingUnicastRoutingEngine(tableNames));
                }
                return(new ShardingDatabaseBroadcastRoutingEngine());
            }
            if (sqlCommandContext.GetSqlCommand() is DMLCommand && tableNames.IsEmpty() && shardingRule.HasDefaultDataSourceName())
            {
                return(new ShardingDefaultDatabaseRoutingEngine(tableNames));
            }
            if (sqlCommandContext.GetSqlCommand() is DMLCommand && shardingConditions.IsAlwaysFalse() || tableNames.IsEmpty() || !shardingRule.TableRuleExists(tableNames))
            {
                return(new ShardingUnicastRoutingEngine(tableNames));
            }
            return(GetShardingRoutingEngine(shardingRule, sqlCommandContext, shardingConditions, tableNames, properties));
        }
Ejemplo n.º 3
0
        private ICollection <string> GetLogicTableNames()
        {
            if (sqlCommandContext.GetSqlCommand() is DropIndexCommand dropIndexCommand && dropIndexCommand.Indexes.Any())
            {
                return(GetTableNamesFromMetaData(dropIndexCommand));
            }

            return(sqlCommandContext.GetTablesContext().GetTableNames());
        }
Ejemplo n.º 4
0
        // public ICollection<SqlToken> GenerateSqlTokens(ISqlCommandContext<ISqlCommand> sqlCommandContext)
        // {
        //     if (sqlCommandContext.GetSqlCommand() is ShowTablesStatement) {
        //         Preconditions.checkState(((ShowTablesStatement) sqlStatementContext.getSqlStatement()).getFromSchema().isPresent());
        //         RemoveAvailable removeAvailable = ((ShowTablesStatement) sqlStatementContext.getSqlStatement()).getFromSchema().get();
        //         return Collections.singletonList(new RemoveToken(removeAvailable.getStartIndex(), removeAvailable.getStopIndex()));
        //     }
        //     if (sqlStatementContext.getSqlStatement() instanceof ShowTableStatusStatement) {
        //         Preconditions.checkState(((ShowTableStatusStatement) sqlStatementContext.getSqlStatement()).getFromSchema().isPresent());
        //         RemoveAvailable removeAvailable = ((ShowTableStatusStatement) sqlStatementContext.getSqlStatement()).getFromSchema().get();
        //         return Collections.singletonList(new RemoveToken(removeAvailable.getStartIndex(), removeAvailable.getStopIndex()));
        //     }
        //     if (sqlStatementContext.getSqlStatement() instanceof ShowColumnsStatement) {
        //         Preconditions.checkState(((ShowColumnsStatement) sqlStatementContext.getSqlStatement()).getFromSchema().isPresent());
        //         RemoveAvailable removeAvailable = ((ShowColumnsStatement) sqlStatementContext.getSqlStatement()).getFromSchema().get();
        //         return Collections.singletonList(new RemoveToken(removeAvailable.getStartIndex(), removeAvailable.getStopIndex()));
        //     }
        //     return Collections.emptyList();
        // }
        //
        // public bool IsGenerateSqlToken(ISqlCommandContext<ISqlCommand> sqlCommandContext)
        // {if (sqlStatementContext.getSqlStatement() instanceof ShowTablesStatement) {
        //         return ((ShowTablesStatement) sqlStatementContext.getSqlStatement()).getFromSchema().isPresent();
        //     }
        //     if (sqlStatementContext.getSqlStatement() instanceof ShowTableStatusStatement) {
        //         return ((ShowTableStatusStatement) sqlStatementContext.getSqlStatement()).getFromSchema().isPresent();
        //     }
        //     if (sqlStatementContext.getSqlStatement() instanceof ShowColumnsStatement) {
        //         return ((ShowColumnsStatement) sqlStatementContext.getSqlStatement()).getFromSchema().isPresent();
        //     }
        //     return false;
        // }
        public ICollection <SqlToken> GenerateSqlTokens(ISqlCommandContext <ISqlCommand> sqlCommandContext)
        {
            if (sqlCommandContext.GetSqlCommand() is ShowTablesCommand showTablesCommand)
            {
                if (showTablesCommand.GetFromSchema() == null)
                {
                    throw new ArgumentNullException("showTablesCommand.GetFromSchema");
                }

                var removeAvailable = showTablesCommand.GetFromSchema();
                return(new List <SqlToken>()
                {
                    new RemoveToken(removeAvailable.GetStartIndex(), removeAvailable.GetStopIndex())
                });
            }
            if (sqlCommandContext.GetSqlCommand() is ShowTableStatusCommand showTableStatusCommand)
            {
                if (showTableStatusCommand.GetFromSchema() == null)
                {
                    throw new ArgumentNullException("showTableStatusCommand.GetFromSchema");
                }
                var removeAvailable = showTableStatusCommand.GetFromSchema();
                return(new List <SqlToken>()
                {
                    new RemoveToken(removeAvailable.GetStartIndex(), removeAvailable.GetStopIndex())
                });
            }
            if (sqlCommandContext.GetSqlCommand() is ShowColumnsCommand showColumnsCommand)
            {
                if (showColumnsCommand.GetFromSchema() == null)
                {
                    throw new ArgumentNullException("showColumnsCommand.GetFromSchema");
                }
                var removeAvailable = showColumnsCommand.GetFromSchema();
                return(new List <SqlToken>()
                {
                    new RemoveToken(removeAvailable.GetStartIndex(), removeAvailable.GetStopIndex())
                });
            }
            return(new List <SqlToken>(0));
        }
Ejemplo n.º 5
0
 public IDataReaderMerger NewInstance(IDatabaseType databaseType, ShardingRule rule, ConfigurationProperties properties, ISqlCommandContext <ISqlCommand> sqlCommandContext)
 {
     if (sqlCommandContext is SelectCommandContext)
     {
         return(new ShardingDQLEnumeratorMerger(databaseType));
     }
     if (sqlCommandContext.GetSqlCommand() is DALCommand)
     {
         return(new ShardingDALEnumeratorMerger(rule));
     }
     return(new TransparentDataReaderMerger());
 }
Ejemplo n.º 6
0
 /// <summary>
 /// 记录sql
 /// </summary>
 /// <param name="logicSql"></param>
 /// <param name="showSimple">简单记录</param>
 /// <param name="sqlCommandContext"></param>
 /// <param name="executionUnits"></param>
 public static void LogSql(string logicSql, bool showSimple, ISqlCommandContext <ISqlCommand> sqlCommandContext, ICollection <ExecutionUnit> executionUnits)
 {
     _logger.LogInformation($"Logic SQL: {logicSql}");
     _logger.LogInformation($"SqlCommand: {sqlCommandContext.GetSqlCommand()}");
     if (showSimple)
     {
         LogSimpleMode(executionUnits);
     }
     else
     {
         LogNormalMode(executionUnits);
     }
 }
Ejemplo n.º 7
0
 private ShardingConditions GetShardingConditions(ParameterContext parameterContext,
                                                  ISqlCommandContext <ISqlCommand> sqlStatementContext, SchemaMetaData schemaMetaData, ShardingRule shardingRule)
 {
     if (sqlStatementContext.GetSqlCommand() is DMLCommand)
     {
         if (sqlStatementContext is InsertCommandContext insertCommandContext)
         {
             return(new ShardingConditions(new InsertClauseShardingConditionEngine(shardingRule).CreateShardingConditions(insertCommandContext, parameterContext)));
         }
         return(new ShardingConditions(new WhereClauseShardingConditionEngine(shardingRule, schemaMetaData).CreateShardingConditions(sqlStatementContext, parameterContext)));
     }
     return(new ShardingConditions(new List <ShardingCondition>(0)));
 }
Ejemplo n.º 8
0
        public IStreamDataReader Merge(List <IStreamDataReader> streamDataReaders, ISqlCommandContext <ISqlCommand> sqlCommandContext, SchemaMetaData schemaMetaData)
        {
            var dalStatement = sqlCommandContext.GetSqlCommand();

            if (dalStatement is ShowDatabasesCommand showDatabasesCommand)
            {
                return(new SingleLocalDataMergedDataReader(new List <object>()
                {
                    DefaultSchema.LOGIC_NAME
                }));
            }
            if (dalStatement is ShowTablesCommand || dalStatement is ShowTableStatusCommand || dalStatement is ShowIndexCommand)
            {
                return(new LogicTablesMergedDataReader(shardingRule, schemaMetaData, sqlCommandContext, streamDataReaders));
            }
            if (dalStatement is ShowCreateTableCommand)
            {
                return(new ShowCreateTableMergedDataReader(shardingRule, schemaMetaData, sqlCommandContext, streamDataReaders));
            }
            return(new TransparentMergedDataReader(streamDataReaders[0]));
        }