Issue #1970 added check for Abstract Entity sql TABLE declarations in SchemaManager.

Amend: Removed second create index pattern in SchemaManager and added a contains check
       instead.  Added a StringUtil replace method.
       In StringUtil replace method, fixed increment of targetIndex before loop indexOf
       check.

Change-Id: Ic3538db7298e295470afad2b6cf63ea9bf0fc5fd

Former-commit-id: 578ce7cfe3 [formerly e80649cadf] [formerly 578ce7cfe3 [formerly e80649cadf] [formerly 355ee727be [formerly 1ed7ce737e6a8d42db2c70892fc2173b5000b44e]]]
Former-commit-id: 355ee727be
Former-commit-id: a34eb2f7fb [formerly 6c32c5a21f]
Former-commit-id: 2b37fefdc7
This commit is contained in:
Brad Gonzales 2013-05-02 17:39:36 -05:00
parent 0bf43c4a4d
commit a1c42b622a
3 changed files with 38 additions and 54 deletions

View file

@ -43,6 +43,7 @@ import org.hibernate.AnnotationException;
import com.raytheon.uf.common.dataplugin.PluginException;
import com.raytheon.uf.common.serialization.ISerializableObject;
import com.raytheon.uf.common.serialization.SerializableManager;
import com.raytheon.uf.common.util.StringUtil;
import com.raytheon.uf.edex.core.EDEXUtil;
import com.raytheon.uf.edex.core.props.PropertiesFactory;
import com.raytheon.uf.edex.database.DatabasePluginProperties;
@ -67,8 +68,8 @@ import com.raytheon.uf.edex.database.plugin.PluginVersionDao;
* 10/8/2008 1532 bphillip Initial checkin
* 2/9/2009 1990 bphillip Fixed index creation
* 03/20/09 njensen Implemented IPluginRegistryChanged
* Mar 02, 2013 1970 bgonzale Updated createIndexTableNamePattern to match text preceeding
* %TABLE%.
* Mar 02, 2013 1970 bgonzale Added check for abstract entities in sql index naming.
* Removed unused private method populateSchema.
* </pre>
*
* @author bphillip
@ -86,6 +87,8 @@ public class SchemaManager implements IDatabasePluginRegistryChanged {
*/
private static final long pluginLockTimeOutMillis = 120000;
private static final String TABLE = "%TABLE%";
/** The singleton instance */
private static SchemaManager instance;
@ -102,7 +105,7 @@ public class SchemaManager implements IDatabasePluginRegistryChanged {
.compile("^create (?:table |index |sequence )(?:[A-Za-z_0-9]*\\.)?(.+?)(?: .*)?$");
private Pattern createIndexTableNamePattern = Pattern
.compile("^create index \\w*?%TABLE%.+? on (.+?) .*$");
.compile("^create index %TABLE%.+? on (.+?) .*$");
/**
* Gets the singleton instance
@ -128,52 +131,6 @@ public class SchemaManager implements IDatabasePluginRegistryChanged {
.getEnvValue("PLUGINDIR");
}
private PluginSchema populateSchema(String pluginName, String database,
PluginSchema schema, List<String> tableNames) {
List<String> ddls = null;
for (String sql : ddls) {
for (String table : tableNames) {
if (sql.startsWith("create table " + table.toLowerCase() + " ")) {
schema.addCreateSql(sql);
break;
} else if (sql.startsWith("drop table " + table.toLowerCase()
+ ";")) {
sql = sql.replace("drop table ", "drop table if exists ");
schema.addDropSql(sql.replace(";", " cascade;"));
break;
} else if (sql.startsWith("create index")
&& sql.contains(" on " + table.toLowerCase())) {
if (sql.contains("%TABLE%")) {
sql = sql.replaceFirst("%TABLE%", table.toLowerCase());
}
String dropIndexSql = sql.replace("create index",
"drop index if exists");
dropIndexSql = dropIndexSql.substring(0,
dropIndexSql.indexOf(" on "))
+ ";";
sql = dropIndexSql + sql;
schema.addCreateSql(sql);
break;
} else if (sql.startsWith("alter table " + table.toLowerCase()
+ " ")
&& sql.contains(" drop ")) {
schema.addDropSql(sql);
break;
} else if (sql.startsWith("alter table " + table.toLowerCase()
+ " ")
&& sql.contains(" add ")) {
if (sql.contains("foreign key")) {
sql = sql.replace(";", " ON DELETE CASCADE;");
}
schema.addCreateSql(sql);
break;
}
}
}
return schema;
}
/**
* Runs all scripts for a particular plugin
*
@ -349,8 +306,12 @@ public class SchemaManager implements IDatabasePluginRegistryChanged {
if (sql.startsWith("create index")) {
Matcher matcher = createIndexTableNamePattern.matcher(sql);
if (matcher.matches()) {
createSql.set(i,
sql.replace("%TABLE%", matcher.group(1)));
createSql.set(i, StringUtil.replace(sql, TABLE,
matcher.group(1)));
} else if (sql.contains(TABLE)) {
// replace %TABLE% in sql statements with an empty
// string
createSql.set(i, StringUtil.replace(sql, TABLE, ""));
}
}
}

View file

@ -26,7 +26,6 @@ import java.util.Map;
import java.util.Properties;
import com.raytheon.edex.plugin.redbook.common.blocks.RedbookBlock.RedbookBlockFactory;
import com.raytheon.edex.plugin.redbook.decoder.RedbookFcstMap;
import com.raytheon.uf.common.status.IUFStatusHandler;
import com.raytheon.uf.common.status.UFStatus;
import com.raytheon.uf.common.util.PropertiesUtil;
@ -55,8 +54,8 @@ import com.raytheon.uf.common.util.ReflectionUtil;
* @version 1.0
*/
public class RedbookBlockBuilder {
private static final transient IUFStatusHandler statusHandler = UFStatus
.getHandler(RedbookFcstMap.class);
private static final IUFStatusHandler statusHandler = UFStatus
.getHandler(RedbookBlockBuilder.class);
private static final int MIN_REMAINING = 4;

View file

@ -38,6 +38,7 @@ import org.apache.commons.lang.StringUtils;
* Oct 20, 2011 rferrel Initial creation
* Jul 13, 2012 740 djohnson Add join.
* Nov 09, 2012 1322 djohnson Add NEWLINE, createMessage.
* Mar 02, 2013 1970 bgonzale Added fast string replacement method.
*
* </pre>
*
@ -185,4 +186,27 @@ public final class StringUtil {
return msg.toString();
}
/**
* Fast replacement of all String target elements in String source with
* String replacement.
*
* @param source
* String that instances will be replaced in.
* @param target
* @param replacement
* @return a new String equivalent to source with target Strings replaced by
* String replacement
*/
public static String replace(final String source, final String target,
final String replacement) {
int targetIndex = 0;
StringBuilder sb = new StringBuilder(source);
while ((targetIndex = sb.indexOf(target, targetIndex)) > -1) {
sb.replace(targetIndex, targetIndex + target.length(), replacement);
targetIndex += replacement.length();
}
return sb.toString();
}
}