Commits

tbrugz  committed 194284f

sqlqueries: prop to add schema name, added generated PK name

  • Participants
  • Parent commits ee78887

Comments (0)

Files changed (4)

File sqldump.template.properties

 # extra options for sqlqueries [defaults at first]
 #sqldump.queries.addtomodel=false|true
 #sqldump.queries.runqueries=true|false
+#sqldump.queries.schemaname=MY_QUERIES_SCHEMA
 
 #sqldump.query.table_x.sql=\
 #select * from TABLE_X where abc=?
+#sqldump.query.table_x.schemaname=TABLEX_SCHEMA
 #sqldump.query.table_x.name=TABLE_X
 #sqldump.query.table_x.rowlimit=10000
 #sqldump.query.table_x.keycols=ID

File src/tbrugz/sqldump/JDBCSchemaGrabber.java

 	
 	//Properties dbmsSpecificResource = new ParametrizedProperties();
 	
-	boolean doSchemaGrabPKs = true, 
+	boolean doSchemaGrabTables = true, //TODO: add prop for doSchemaGrabTables
+			doSchemaGrabPKs = true, 
 			doSchemaGrabFKs = true, 
 			doSchemaGrabExportedFKs = false, 
 			doSchemaGrabGrants = false, 
 		
 		schemaModel.setSqlDialect(DBMSResources.instance().dbid());
 
+		if(doSchemaGrabTables) {
+			
 		List<String> tablePatterns = Utils.getStringListFromProp(papp, PROP_SCHEMADUMP_TABLEFILTER, ","); 
 		for(String schemaName: schemasList) {
 			if(tablePatterns==null) {
 			}
 		}
 		
+		}
+		
 		boolean recursivedump = Utils.getPropBool(papp, PROP_DO_SCHEMADUMP_RECURSIVEDUMP, false);
 		if(recursivedump) {
 			boolean grabExportedFKsAlso = Utils.getPropBool(papp, PROP_DO_SCHEMADUMP_RECURSIVEDUMP_EXPORTEDFKS, false);
 					//Columns & Remarks
 					Table t = DBIdentifiable.getDBIdentifiableByTypeSchemaAndName(schemaModel.getTables(), DBObjectType.TABLE, view.getSchemaName(), view.getName());
 					if(t==null) {
-						log.warn("table not found: "+view.getSchemaName()+"."+view.getName());
+						log.warn("view not found in grabbed tables' list: "+view.getSchemaName()+"."+view.getName());
 						continue;
 					}
 					view.setSimpleColumns(t.getColumns());
 	}
 	
 	//XXX: props for setting pk(i)NamePatterns?
-	String pkNamePattern = "${tablename}_pk";
-	String pkiNamePattern = "${tablename}_pki";
+	public static final String pkNamePattern = "${tablename}_pk";
+	public static final String pkiNamePattern = "${tablename}_pki";
 	
-	String newNameFromTableName(String tableName, String pattern) {
+	public static String newNameFromTableName(String tableName, String pattern) {
 		return pattern.replaceAll("\\$\\{tablename\\}", tableName);
 	}
 	

File src/tbrugz/sqldump/datadump/SQLQueries.java

 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import tbrugz.sqldump.JDBCSchemaGrabber;
 import tbrugz.sqldump.dbmodel.Constraint;
 import tbrugz.sqldump.dbmodel.Constraint.ConstraintType;
 import tbrugz.sqldump.dbmodel.Query;
 	static final String PROP_QUERIES = "sqldump.queries";
 	static final String PROP_QUERIES_RUN = PROP_QUERIES+".runqueries";
 	static final String PROP_QUERIES_ADD_TO_MODEL = PROP_QUERIES+".addtomodel";
+	static final String PROP_QUERIES_SCHEMA = PROP_QUERIES+".schemaname";
 
+	static final String DEFAULT_QUERIES_SCHEMA = "SQLQUERY";
+	
 	static Log log = LogFactory.getLog(SQLQueries.class);
 	
 	@Override
 		String charset = prop.getProperty(DataDump.PROP_DATADUMP_CHARSET, DataDump.CHARSET_DEFAULT);
 		//boolean dumpInsertInfoSyntax = false, dumpCSVSyntax = false, dumpXMLSyntax = false, dumpJSONSyntax = false;
 		
+		String defaultSchemaName = prop.getProperty(PROP_QUERIES_SCHEMA, DEFAULT_QUERIES_SCHEMA);
+		
 		String queriesStr = prop.getProperty(PROP_QUERIES);
 		if(queriesStr==null) {
 			log.warn("prop '"+PROP_QUERIES+"' not defined");
 				Query query = new Query();
 				query.id = qid;
 				query.setName(queryName);
+				//add schemaName
+				query.setSchemaName(prop.getProperty("sqldump.query."+qid+".schemaname", defaultSchemaName));
+				
 				query.query = sql;
 				query.parameterValues = params;
+				//XXX: add columns? query.setColumns(columns)...
 				if(keyCols!=null) {
 					Constraint cpk = new Constraint();
 					cpk.type = ConstraintType.PK;
 					cpk.uniqueColumns = keyCols;
+					cpk.setName(JDBCSchemaGrabber.newNameFromTableName(queryName, JDBCSchemaGrabber.pkNamePattern));
 					List<Constraint> lc = query.getConstraints(); 
 					if(lc==null) {
 						lc = new ArrayList<Constraint>();

File src/tbrugz/sqldump/dbmodel/View.java

 	public String getDefinition(boolean dumpSchemaName) {
 		StringBuffer sbConstraints = new StringBuffer();
 		if(constraints!=null) {
-			for(Constraint cons: constraints) {
-				sbConstraints.append(",\n\t"+cons.getDefinition(false));
+			for(int i=0;i<constraints.size();i++) {
+				Constraint cons = constraints.get(i);
+				sbConstraints.append((i==0?"":",\n\t")+cons.getDefinition(false));
 			}
 		}
 
 		
 		return (dumpCreateOrReplace?"create or replace ":"create ") + "view "
 				+ (dumpSchemaName && schemaName!=null?DBObject.getFinalIdentifier(schemaName)+".":"") + DBObject.getFinalIdentifier(name)
-				+ (sbConstraints.length()>0?" (\n\t"+Utils.join(getColumnNames(), ", ")+sbConstraints.toString()+"\n)":"")
+				+ (sbConstraints.length()>0?" (\n\t"
+						+ ((columns!=null&&columns.size()>0)?Utils.join(getColumnNames(), ", ")+",\n\t":"")
+						+ sbConstraints.toString()+"\n)":"")
 				+ " as\n" + query
 				+ (withReadOnly?"\nwith read only":
 					(checkOption!=null && !checkOption.equals(CheckOptionType.NONE)?