Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

updateTitles and updateDataTypes params for GenerateUpdateMaql

Usage:
 - to describe the diff between server and local XML:
   GenerateUpdateMaql(maqlFile = "...")
 - the diff + always update titles to match the local XML:
   GenerateUpdateMaql(maqlFile = "...", updateTitles = "true")
 - the diff + always update data types to match those explicitly
   specified in the local XML:
   GenerateUpdateMaql(maqlFile = "...", updateDataTypes = "true)
  • Loading branch information...
commit 3e695ad8af05710b53f86f3068e8c92765f845db 1 parent 0ac40d3
@koles koles authored
View
8 backend/src/main/java/com/gooddata/connector/AbstractConnector.java
@@ -566,6 +566,8 @@ private void generateUpdateMaql(Command c, CliParams p, ProcessingContext ctx) t
final String pid = ctx.getProjectIdMandatory();
final String maqlFile = c.getParamMandatory("maqlFile");
+ final String updateTitles = c.getParam("updateTitles");
+ final String updateDataTypes = c.getParam("updateDataTypes");
c.paramsProcessed();
final String dataset = schema.getDatasetName();
@@ -587,6 +589,12 @@ private void generateUpdateMaql(Command c, CliParams p, ProcessingContext ctx) t
mg.setSynchronize(false);
maql.append(mg.generateMaqlAdd(newColumns, diffMaker.getLocalColumns()));
}
+ if (updateTitles != null && !updateTitles.equalsIgnoreCase("false")) {
+ maql.append(mg.generateMaqlUpdateTitles(diffMaker.getLocalColumns()));
+ }
+ if (updateDataTypes != null && !updateDataTypes.equalsIgnoreCase("false")) {
+ maql.append(mg.generateMaqlUpdateDataTypes(diffMaker.getLocalColumns()));
+ }
if (maql.length() > 0) {
maql.append(mg.generateMaqlSynchronize());
final String mqqlStr = mg.removeDropAndRecreateOfDateFacts(deletedColumns, newColumns, maql.toString());
View
128 backend/src/main/java/com/gooddata/modeling/generator/MaqlGenerator.java
@@ -23,6 +23,16 @@
package com.gooddata.modeling.generator;
+import static com.gooddata.modeling.model.SourceColumn.LDM_TYPE_ATTRIBUTE;
+import static com.gooddata.modeling.model.SourceColumn.LDM_TYPE_CONNECTION_POINT;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+
import com.gooddata.modeling.generator.MaqlGenerator.State.Attribute;
import com.gooddata.modeling.generator.MaqlGenerator.State.Column;
import com.gooddata.modeling.generator.MaqlGenerator.State.ConnectionPoint;
@@ -31,15 +41,6 @@
import com.gooddata.modeling.model.SourceSchema;
import com.gooddata.naming.N;
import com.gooddata.util.StringUtil;
-import org.apache.log4j.Logger;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import static com.gooddata.modeling.model.SourceColumn.LDM_TYPE_ATTRIBUTE;
-import static com.gooddata.modeling.model.SourceColumn.LDM_TYPE_CONNECTION_POINT;
/**
* GoodData MAQL Generator generates the MAQL from the LDM schema object
@@ -257,6 +258,40 @@ private String generateMaqlAdd(Iterable<SourceColumn> newColumns, Iterable<Sourc
}
return script;
}
+
+ /**
+ * Generate MAQL to alter titles of provided columns
+ * @param columns
+ * @return
+ */
+ public String generateMaqlUpdateTitles(Iterable<SourceColumn> columns) {
+ StringBuffer maql = new StringBuffer("");
+ State state = new State();
+ for (final SourceColumn sc : columns) {
+ state.processColumn(sc);
+ }
+ for (final Column c : state.getColumns()) {
+ maql.append(c.generateMaqlAlterTitle());
+ }
+ return maql.toString();
+ }
+
+ /**
+ * Generate MAQL to alter specified data types of given columns
+ * @param columns
+ * @return
+ */
+ public String generateMaqlUpdateDataTypes(Iterable<SourceColumn> columns) {
+ StringBuffer maql = new StringBuffer("");
+ State state = new State();
+ for (final SourceColumn sc : columns) {
+ state.processColumn(sc);
+ }
+ for (final Column c : state.getColumns()) {
+ maql.append(c.generateMaqlAlterDataType());
+ }
+ return maql.toString();
+ }
/**
* Generate MAQL folders for specified columns
@@ -404,6 +439,15 @@ private void processConnectionPoint(SourceColumn column) {
attributes.put(connectionPoint.columnName, connectionPoint);
}
}
+
+ private Iterable<Column> getColumns() {
+ List<Column> result = new ArrayList<MaqlGenerator.State.Column>();
+ result.addAll(attributes.values());
+ result.addAll(facts);
+ result.addAll(labels);
+ result.addAll(dates);
+ return result;
+ }
// columns
@@ -428,10 +472,17 @@ protected String createForeignKeyMaqlDdl() {
public abstract String generateMaqlDdlAdd();
+ public String generateMaqlAlterTitle() {
+ return ""; // intentionally left blank - no applicable MAQL DDL
+ }
+
+ public String generateMaqlAlterDataType() {
+ return ""; // intentionally left blank - no applicable MAQL DDL
+ }
+
public String generateMaqlDdlDrop() {
return "DROP {" + identifier + "} CASCADE;\n\n";
}
-
}
@@ -491,6 +542,17 @@ public String generateMaqlDdlAdd() {
return script;
}
+
+ public String generateMaqlAlterTitle() {
+ return "ALTER ATTRIBUTE {" + identifier + "} VISUAL(TITLE \"" + lcn + "\");\n";
+ }
+
+ public String generateMaqlAlterDataType() {
+ if (column.getDataType() != null) {
+ return "ALTER DATATYPE {" + table + "." + N.NM_PFX + columnName + "} " + column.getDataType() + ";\n";
+ }
+ return "";
+ }
@Override
public String generateMaqlDdlDrop() {
@@ -519,9 +581,9 @@ public String generateDefaultLabelMaqlDdl() {
//facts
private class Fact extends Column {
+ private final String fcolname;
Fact(SourceColumn column) {
-
super(column, "fact");
// unfortunate backward compatibility fix
// we have converted the date/time facts and the time attribute to explicit schema elements
@@ -534,6 +596,13 @@ public String generateDefaultLabelMaqlDdl() {
this.identifier = N.TM + "." + N.DT + "." + schemaName + "." + columnName.replace(N.TM_SLI_SFX, "");
}
+ String _fcolname = N.FCT_PFX + columnName;
+ if (column.isDateFact())
+ _fcolname = N.DT_PFX + columnName.replace(N.DT_SLI_SFX, "");
+ if (column.isTimeFact())
+ _fcolname = N.TM_PFX + columnName.replace(N.TM_SLI_SFX, "");
+ fcolname = _fcolname;
+
}
@Override
@@ -547,11 +616,6 @@ public String generateMaqlDdlAdd() {
// unfortunate backward compatibility fix
// we have converted the date/time facts and the time attribute to explicit schema elements
// we needed to keep the dt_ and tm_ prefixes instead of the f_
- String fcolname = N.FCT_PFX + columnName;
- if (column.isDateFact())
- fcolname = N.DT_PFX + columnName.replace(N.DT_SLI_SFX, "");
- if (column.isTimeFact())
- fcolname = N.TM_PFX + columnName.replace(N.TM_SLI_SFX, "");
String script = "CREATE FACT {" + identifier + "} VISUAL(TITLE \"" + lcn
+ "\"" + folderStatement + ") AS {" + getFactTableName() + "." + fcolname + "};\n"
+ "ALTER DATASET {" + schema.getDatasetName() + "} ADD {" + identifier + "};\n";
@@ -565,6 +629,17 @@ public String generateMaqlDdlAdd() {
}
return script;
}
+
+ public String generateMaqlAlterTitle() {
+ return "ALTER FACT {" + identifier + "} VISUAL(TITLE \"" + lcn + "\");\n";
+ }
+
+ public String generateMaqlAlterDataType() {
+ if (column.getDataType() != null) {
+ return "ALTER DATATYPE {" + getFactTableName() + "." + fcolname + "} " + column.getDataType() + ";\n";
+ }
+ return "";
+ }
}
//labels
@@ -630,7 +705,28 @@ public String generateMaqlSortLabel(String sortOrder) {
SourceColumn.LDM_SORT_ORDER_DESC.equals(sortOrder)))
sortOrder = SourceColumn.LDM_SORT_ORDER_ASC;
return "ALTER ATTRIBUTE {" + attr.identifier + "} ORDER BY {" + labelId + "} "+sortOrder+";\n";
+ }
+
+ public String generateMaqlAlterTitle() {
+ attr = attributes.get(scnPk);
+ if (attr == null) {
+ throw new IllegalArgumentException("Label " + columnName + " points to non-existing attribute " + scnPk);
+ }
+ // TODO why is this different than this.identifier?
+ final String labelId = getLabelId();
+ return "ALTER ATTRIBUTE {attr." + schemaName + "." + scnPk + "} ALTER LABELS {label." + schemaName + "." + scnPk + "."
+ + columnName + "} VISUAL(TITLE \"" + lcn + "\");\n";
+ }
+ public String generateMaqlAlterDataType() {
+ attr = attributes.get(scnPk);
+ if (attr == null) {
+ throw new IllegalArgumentException("Label " + columnName + " points to non-existing attribute " + scnPk);
+ }
+ if (column.getDataType() != null) {
+ return "ALTER DATATYPE {" + attr.table + "." + N.NM_PFX + columnName + "} " + column.getDataType() + ";\n";
+ }
+ return "";
}
protected String getLabelId() {
View
3  cli-distro/bin/test.sh
@@ -29,6 +29,9 @@ echo "UseProject(fileName = \"examples/quotes_alter/pid\");" > "$tmp"
grep '^\(UseCsv\|Transfer\)' examples/quotes_alter/quotes.txt >> "$tmp"
bin/gdi.sh "$tmp"
+echo 'Testing GenerateUpdateMaql with updateTitles and updateDataTypes'
+bin/gdi.sh examples/quotes_alter/alter_titles_datatypes.txt
+
echo 'Altering the server-side model (-2 attributes -1 fact +1 fact)'
bin/gdi.sh examples/quotes_alter/alter.txt
View
18 cli-distro/examples/quotes_alter/alter_titles_datatypes.txt
@@ -0,0 +1,18 @@
+# A simple example that loads a flat file with a stock quote data and connect a time dimension
+# see full command documentation at http://developer.gooddata.com/gooddata-cl/cli-commands.html
+
+# Retrieve the project ID stored by the quotes.txt
+UseProject(fileName="examples/quotes_alter/pid");
+
+# load the stock quotes data file
+# the data file config has been generated
+UseCsv(csvDataFile="examples/quotes_alter/quotes.csv",hasHeader="true",configFile="examples/quotes_alter/quotes.config.xml");
+
+GenerateUpdateMaql(maqlFile="examples/quotes_alter/quotes_titles.alter.maql",updateTitles="true");
+ExecuteMaql(maqlFile="examples/quotes_alter/quotes_titles.alter.maql");
+
+GenerateUpdateMaql(maqlFile="examples/quotes_alter/quotes_datatypes.alter.maql",updateDataTypes="true");
+ExecuteMaql(maqlFile="examples/quotes_alter/quotes_datatypes.alter.maql");
+
+# transfer the stock quotes data
+TransferData();
View
3  cli-distro/examples/quotes_alter/quotes.config.xml
@@ -12,6 +12,7 @@
<name>COMPANY</name>
<title>Company</title>
<ldmType>LABEL</ldmType>
+ <dataType>VARCHAR(254)</dataType>
<reference>SYMBOL</reference>
<folder>Quotes</folder>
</column>
@@ -31,6 +32,7 @@
<name>INDUSTRY</name>
<title>Industry</title>
<ldmType>ATTRIBUTE</ldmType>
+ <dataType>VARCHAR(255)</dataType>
<folder>Quotes</folder>
</column>
<column>
@@ -51,6 +53,7 @@
<name>OPEN_PRICE</name>
<title>Open Price</title>
<ldmType>FACT</ldmType>
+ <dataType>DECIMAL(18,3)</dataType>
<folder>Quotes</folder>
</column>
<column>
Please sign in to comment.
Something went wrong with that request. Please try again.