@Override public String identifier(String identifier) { return database.identifier(identifier); }
@Override public TableMate loadMataData(Schema schema, TableRule tableRule) { String identifier = database.identifier(tableRule.getName()); TableMate tableMate = new TableMate(schema, identifier, tableRule); return tableMate; }
@Override public Sequence loadMataData(Schema schema, SequenceRule sequenceRule) { String strategy = sequenceRule.getStrategy(); String name = database.identifier(sequenceRule.getName()); if ("hilo".equals(strategy)) { return new TableHiLoGenerator(schema, name, sequenceRule); } else if ("snowflake".equals(strategy)) { return new SnowflakeGenerator(schema, name, sequenceRule); } else { throw DbException.get(ErrorCode.FEATURE_NOT_SUPPORTED_1, strategy + " sequence"); } }
DataSource dataSource = dsRepository.getDataSourceByShardName(shardName); conn = dataSource.getConnection(); tableName = database.identifier(tableName); if (catalog != null) { catalog = database.identifier(catalog); schema = database.identifier(schema);
public void configure(Properties params) { JdbcRepository repo = (JdbcRepository) database.getRepository(); String shardName = params.getProperty("shard", repo.getPublicDB()); String catalog = params.getProperty("catalog"); String schema = params.getProperty("schema"); String tableName = params.getProperty("tableName", DEF_TABLE); tableName = database.identifier(tableName); if (catalog != null) { catalog = database.identifier(catalog); } if (schema != null) { schema = database.identifier(schema); } tableNode = new ObjectNode(shardName, catalog, schema, tableName, null); nameValue = StringUtils.toLowerEnglish(getName()); nameColumnName = params.getProperty("nameColumnName", DEF_NAME_COLUMN); valueColumnName = params.getProperty("valueColumnName", DEF_VALUE_COLUMN); nameColumnLength = getIntProperty(params, "nameColumnLength", DEF_NAMECOLUMNLENGTH_LENGTH); incrementSize = getIntProperty(params, "cacheSize", (int) getCacheSize()); initialValue = getIntProperty(params, "initialValue", DEFAULT_INITIAL_VALUE); this.selectQuery = buildSelectQuery(); this.updateQuery = buildUpdateQuery(); this.insertQuery = buildInsertQuery(); this.dataSource = repo.getDataSourceByShardName(shardName); this.optimizer = new HiloOptimizer(incrementSize); this.createTableIfNotExits(); }
/** * validation the rule columns is in the table columns */ private void setRuleColumns() { if (tableRule instanceof ShardedTableRule) { ShardedTableRule shardedTableRule = (ShardedTableRule) tableRule; String[] ruleColNames = shardedTableRule.getRuleColumns(); ruleColumns = new Column[ruleColNames.length]; for (int i = 0; i < ruleColNames.length; i++) { String colName = database.identifier(ruleColNames[i]); if(!doesColumnExist(colName)) { throw DbException.get(ErrorCode.SHARDING_COLUMN_NOT_FOUND, colName ,getName()); } ruleColumns[i] = getColumn(colName); } } }
@Override public QueryResult process(String query) { Session session = target.getSession().getDbSession(); QueryResult result = new QueryResult(0); List<SQLStatement> stmts = SQLUtils.parseStatements(query, JdbcConstants.MYSQL); for (SQLStatement stmt : stmts) { if (stmt instanceof SQLUseStatement) { SQLUseStatement s = (SQLUseStatement) stmt; String database = SQLUtils.toMySqlString(s.getDatabase()); database = database.replaceAll("['|`]", ""); Schema schema = session.getDatabase().findSchema(session.getDatabase().identifier(database)); if (schema == null) { throw ServerException.get(ErrorCode.ER_BAD_DB_ERROR, "Unknown database " + database); } result.setWarnings((short) 1); result.setMessage("The database is immutable, there is no need to use the 'use' command"); } else { throw ServerException.get(ErrorCode.ER_NOT_ALLOWED_COMMAND, "not allowed command:" + query); } } return result; }