use of ch.ehi.sqlgen.repository.DbTableName in project ili2db by claeis.
the class TransferFromIli method addModelsTable.
public static void addModelsTable(DbSchema schema, Settings config) {
DbTable tab = new DbTable();
tab.setName(new DbTableName(schema.getName(), DbNames.MODELS_TAB));
DbColVarchar fileCol = new DbColVarchar();
fileCol.setName(DbNames.MODELS_TAB_FILE_COL);
fileCol.setNotNull(true);
fileCol.setSize(250);
tab.addColumn(fileCol);
DbColVarchar iliversionCol = new DbColVarchar();
iliversionCol.setName(DbNames.MODELS_TAB_ILIVERSION_COL);
iliversionCol.setNotNull(true);
iliversionCol.setSize(3);
tab.addColumn(iliversionCol);
DbColVarchar importsCol = new DbColVarchar();
importsCol.setName(DbNames.MODELS_TAB_MODELNAME_COL);
importsCol.setNotNull(true);
int modelNameColSize = DbColVarchar.UNLIMITED;
String modelNameColSizeStr = config.getValue(Config.MODELS_TAB_MODELNAME_COLSIZE);
if (modelNameColSizeStr != null) {
try {
modelNameColSize = Integer.parseInt(modelNameColSizeStr);
} catch (NumberFormatException e) {
}
}
importsCol.setSize(modelNameColSize);
tab.addColumn(importsCol);
DbColVarchar contentCol = new DbColVarchar();
contentCol.setName(DbNames.MODELS_TAB_CONTENT_COL);
contentCol.setNotNull(true);
contentCol.setSize(DbColVarchar.UNLIMITED);
tab.addColumn(contentCol);
DbColDateTime importDateCol = new DbColDateTime();
importDateCol.setName(DbNames.MODELS_TAB_IMPORTDATE_COL);
importDateCol.setNotNull(true);
tab.addColumn(importDateCol);
DbIndex pk = new DbIndex();
pk.setPrimary(true);
pk.addAttr(importsCol);
pk.addAttr(iliversionCol);
tab.addIndex(pk);
schema.addTable(tab);
}
use of ch.ehi.sqlgen.repository.DbTableName in project ili2db by claeis.
the class TransferFromIli method addAttrMappingTable.
public static void addAttrMappingTable(ch.ehi.sqlgen.repository.DbSchema schema) {
ch.ehi.sqlgen.repository.DbTable tab = new ch.ehi.sqlgen.repository.DbTable();
tab.setName(new DbTableName(schema.getName(), DbNames.ATTRNAME_TAB));
ch.ehi.sqlgen.repository.DbColVarchar ilinameCol = new ch.ehi.sqlgen.repository.DbColVarchar();
ilinameCol.setName(DbNames.ATTRNAME_TAB_ILINAME_COL);
ilinameCol.setNotNull(true);
ilinameCol.setSize(1024);
tab.addColumn(ilinameCol);
ch.ehi.sqlgen.repository.DbColVarchar sqlnameCol = new ch.ehi.sqlgen.repository.DbColVarchar();
sqlnameCol.setName(DbNames.ATTRNAME_TAB_SQLNAME_COL);
sqlnameCol.setNotNull(true);
sqlnameCol.setSize(1024);
tab.addColumn(sqlnameCol);
ch.ehi.sqlgen.repository.DbColVarchar ownerCol = new ch.ehi.sqlgen.repository.DbColVarchar();
ownerCol.setName(DbNames.ATTRNAME_TAB_OWNER_COL);
ownerCol.setNotNull(true);
ownerCol.setSize(1024);
tab.addColumn(ownerCol);
ch.ehi.sqlgen.repository.DbColVarchar targetCol = new ch.ehi.sqlgen.repository.DbColVarchar();
targetCol.setName(DbNames.ATTRNAME_TAB_TARGET_COL);
targetCol.setNotNull(false);
targetCol.setSize(1024);
tab.addColumn(targetCol);
DbIndex pk = new DbIndex();
pk.setPrimary(true);
pk.addAttr(ownerCol);
pk.addAttr(sqlnameCol);
tab.addIndex(pk);
schema.addTable(tab);
}
use of ch.ehi.sqlgen.repository.DbTableName in project ili2db by claeis.
the class TransferFromIli method addTrafoConfigTable.
public static void addTrafoConfigTable(ch.ehi.sqlgen.repository.DbSchema schema) {
ch.ehi.sqlgen.repository.DbTable tab = new ch.ehi.sqlgen.repository.DbTable();
tab.setName(new DbTableName(schema.getName(), DbNames.TRAFO_TAB));
ch.ehi.sqlgen.repository.DbColVarchar iliName = new ch.ehi.sqlgen.repository.DbColVarchar();
iliName.setName(DbNames.TRAFO_TAB_ILINAME_COL);
iliName.setNotNull(true);
iliName.setSize(1024);
// iliClassName.setPrimaryKey(true);
tab.addColumn(iliName);
ch.ehi.sqlgen.repository.DbColVarchar configCol = new ch.ehi.sqlgen.repository.DbColVarchar();
configCol.setName(DbNames.TRAFO_TAB_TAG_COL);
configCol.setNotNull(true);
configCol.setSize(1024);
tab.addColumn(configCol);
ch.ehi.sqlgen.repository.DbColVarchar valCol = new ch.ehi.sqlgen.repository.DbColVarchar();
valCol.setName(DbNames.TRAFO_TAB_SETTING_COL);
valCol.setNotNull(true);
valCol.setSize(1024);
tab.addColumn(valCol);
schema.addTable(tab);
}
use of ch.ehi.sqlgen.repository.DbTableName in project ili2db by claeis.
the class TransferFromXtf method createItfLineTableInsertStmt.
private String createItfLineTableInsertStmt(AttributeDef attrDef) {
SurfaceOrAreaType type = (SurfaceOrAreaType) attrDef.getDomainResolvingAliases();
StringBuffer stmt = new StringBuffer();
StringBuffer values = new StringBuffer();
stmt.append("INSERT INTO ");
DbTableName sqlTabName = getSqlTableNameItfLineTable(attrDef);
stmt.append(sqlTabName.getQName());
String sep = " (";
// add T_Id
stmt.append(sep);
sep = ",";
stmt.append(colT_ID);
values.append("?");
// add T_basket
if (createBasketCol) {
stmt.append(sep);
sep = ",";
stmt.append(DbNames.T_BASKET_COL);
values.append(",?");
}
if (createDatasetCol) {
stmt.append(sep);
sep = ",";
stmt.append(DbNames.T_DATASET_COL);
values.append(",?");
}
if (readIliTid) {
stmt.append(sep);
sep = ",";
stmt.append(DbNames.T_ILI_TID_COL);
values.append(",?");
}
// POLYLINE
stmt.append(sep);
sep = ",";
stmt.append(ili2sqlName.getSqlColNameItfLineTableGeomAttr(attrDef, sqlTabName.getName()));
values.append("," + geomConv.getInsertValueWrapperPolyline("?", recConv.getSrsid(attrDef)));
// -> mainTable
if (type instanceof SurfaceType) {
stmt.append(sep);
sep = ",";
stmt.append(ili2sqlName.getSqlColNameItfLineTableRefAttr(attrDef, sqlTabName.getName()));
values.append(",?");
}
Table lineAttrTable = type.getLineAttributeStructure();
if (lineAttrTable != null) {
Iterator attri = lineAttrTable.getAttributes();
while (attri.hasNext()) {
AttributeDef lineattr = (AttributeDef) attri.next();
sep = recConv.addAttrToInsertStmt(false, stmt, values, sep, lineattr, sqlTabName.getName());
}
}
// stdcols
if (createStdCols) {
stmt.append(sep);
sep = ",";
stmt.append(DbNames.T_LAST_CHANGE_COL);
values.append(",?");
stmt.append(sep);
sep = ",";
stmt.append(DbNames.T_CREATE_DATE_COL);
values.append(",?");
stmt.append(sep);
sep = ",";
stmt.append(DbNames.T_USER_COL);
values.append(",?");
}
stmt.append(") VALUES (");
stmt.append(values);
stmt.append(")");
return stmt.toString();
}
use of ch.ehi.sqlgen.repository.DbTableName in project ili2db by claeis.
the class Ili2db method runSchemaImport.
public static void runSchemaImport(Config config, String appHome) throws Ili2dbException {
ch.ehi.basics.logging.FileListener logfile = null;
if (config.getLogfile() != null) {
logfile = new FileLogger(new java.io.File(config.getLogfile()));
EhiLogger.getInstance().addListener(logfile);
}
StdLogger logStderr = new StdLogger(config.getLogfile());
EhiLogger.getInstance().addListener(logStderr);
EhiLogger.getInstance().removeListener(StdListener.getInstance());
try {
boolean connectionFromExtern = config.getJdbcConnection() != null;
logGeneralInfo(config);
Ili2dbLibraryInit ao = null;
try {
ao = getInitStrategy(config);
ao.init();
ch.interlis.ili2c.config.Configuration modelv = new ch.interlis.ili2c.config.Configuration();
String xtffile = config.getXtffile();
String ilifile = null;
if (xtffile != null && xtffile.endsWith(".ili")) {
ilifile = xtffile;
modelv.addFileEntry(new ch.interlis.ili2c.config.FileEntry(ilifile, ch.interlis.ili2c.config.FileEntryKind.ILIMODELFILE));
}
String models = config.getModels();
if (models != null) {
String[] modelnames = models.split(";");
for (int modeli = 0; modeli < modelnames.length; modeli++) {
String m = modelnames[modeli];
if (m != null) {
if (m.equals(XTF)) {
// ignore it
} else {
modelv.addFileEntry(new ch.interlis.ili2c.config.FileEntry(m, ch.interlis.ili2c.config.FileEntryKind.ILIMODELFILE));
}
}
}
}
if (modelv.getSizeFileEntry() == 0) {
throw new Ili2dbException("no models given");
}
String dburl = config.getDburl();
String dbusr = config.getDbusr();
String dbpwd = config.getDbpwd();
if (!connectionFromExtern && dburl == null) {
throw new Ili2dbException("no dburl given");
}
if (dbusr == null) {
// EhiLogger.logError("no dbusr given");
// return;
dbusr = "";
}
if (dbpwd == null) {
// EhiLogger.logError("no dbpwd given");
// return;
dbpwd = "";
}
String dbschema = config.getDbschema();
if (dbschema != null) {
EhiLogger.logState("dbschema <" + dbschema + ">");
}
String geometryConverter = config.getGeometryConverter();
if (geometryConverter == null) {
throw new Ili2dbException("no geoemtry converter given");
}
String ddlGenerator = config.getDdlGenerator();
if (ddlGenerator == null) {
throw new Ili2dbException("no DDL generator given");
}
String idGenerator = config.getIdGenerator();
if (idGenerator == null) {
throw new Ili2dbException("no ID generator given");
}
if (!connectionFromExtern) {
String jdbcDriver = config.getJdbcDriver();
if (jdbcDriver == null) {
throw new Ili2dbException("no JDBC driver given");
}
try {
Class.forName(jdbcDriver);
} catch (Exception ex) {
throw new Ili2dbException("failed to load JDBC driver", ex);
}
}
CustomMapping customMapping = getCustomMappingStrategy(config);
// open db connection
Connection conn = null;
String url = dburl;
try {
if (connectionFromExtern) {
conn = config.getJdbcConnection();
} else {
conn = connect(url, dbusr, dbpwd, config, customMapping);
}
customMapping.postConnect(conn, config);
logDBVersion(conn);
if (!connectionFromExtern) {
// switch off auto-commit
conn.setAutoCommit(false);
}
} catch (SQLException ex) {
throw new Ili2dbException(ex);
}
// run pre-script
if (config.getPreScript() != null) {
try {
EhiLogger.logState("run schemaImport pre-script...");
DbUtility.executeSqlScript(conn, new java.io.FileReader(config.getPreScript()));
} catch (FileNotFoundException e) {
throw new Ili2dbException("schemaImport pre-script statements failed", e);
}
}
// setup ilidirs+pathmap for ili2c
setupIli2cPathmap(config, appHome, ilifile, conn);
Ili2cMetaAttrs ili2cMetaAttrs = new Ili2cMetaAttrs();
setupIli2cMetaAttrs(ili2cMetaAttrs, config, modelv);
// compile required ili files
EhiLogger.logState("compile models...");
TransferDescription td;
modelv.setAutoCompleteModelList(true);
modelv.setGenerateWarnings(false);
td = ch.interlis.ili2c.Main.runCompiler(modelv, config, ili2cMetaAttrs);
if (td == null) {
throw new Ili2dbException("compiler failed");
}
// an INTERLIS 1 model?
if (td.getIli1Format() != null) {
config.setItfTransferfile(true);
}
Generator gen = null;
try {
gen = (Generator) Class.forName(ddlGenerator).newInstance();
} catch (Exception ex) {
throw new Ili2dbException("failed to load/create DDL generator", ex);
}
// create db schema
if (config.getDbschema() != null) {
if (!DbUtility.schemaExists(conn, config.getDbschema())) {
DbUtility.createSchema(conn, config.getDbschema());
}
}
DbIdGen idGen = null;
try {
idGen = (DbIdGen) Class.forName(idGenerator).newInstance();
} catch (Exception ex) {
throw new Ili2dbException("failed to load/create ID generator", ex);
}
idGen.init(config.getDbschema(), config);
// read mapping file
NameMapping mapping = new NameMapping(config);
if (DbUtility.tableExists(conn, new DbTableName(config.getDbschema(), DbNames.CLASSNAME_TAB))) {
// read mapping from db
mapping.readTableMappingTable(conn, config.getDbschema());
}
if (DbUtility.tableExists(conn, new DbTableName(config.getDbschema(), DbNames.ATTRNAME_TAB))) {
// read mapping from db
mapping.readAttrMappingTable(conn, config.getDbschema());
}
TrafoConfig trafoConfig = new TrafoConfig();
trafoConfig.readTrafoConfig(conn, config.getDbschema());
ModelElementSelector ms = new ModelElementSelector();
ArrayList<String> modelNames = new ArrayList<String>();
if (models != null) {
String[] modelnames = models.split(";");
for (int modeli = 0; modeli < modelnames.length; modeli++) {
String m = modelnames[modeli];
if (m != null) {
if (m.equals(XTF)) {
// ignore it
} else {
modelNames.add(m);
}
}
}
}
// use models explicitly given by user (or last model of given ili-file)
java.util.List<Element> eles = ms.getModelElements(modelNames, td, td.getIli1Format() != null && config.getDoItfLineTables(), Config.CREATE_ENUM_DEFS_MULTI.equals(config.getCreateEnumDefs()), config);
Viewable2TableMapping class2wrapper = Viewable2TableMapper.getClass2TableMapping(config, trafoConfig, eles, mapping);
SqlColumnConverter geomConverter = null;
try {
geomConverter = (SqlColumnConverter) Class.forName(geometryConverter).newInstance();
} catch (Exception ex) {
throw new Ili2dbException("failed to load/create geometry converter", ex);
}
geomConverter.setup(conn, config);
if (config.getDefaultSrsCode() != null && config.getDefaultSrsAuthority() != null) {
try {
if (geomConverter.getSrsid(config.getDefaultSrsAuthority(), config.getDefaultSrsCode(), conn) == null) {
throw new Ili2dbException(config.getDefaultSrsAuthority() + "/" + config.getDefaultSrsCode() + " does not exist");
}
} catch (ConverterException ex) {
throw new Ili2dbException("failed to query existence of SRS", ex);
}
}
// create table structure
EhiLogger.logState("create table structure...");
try {
TransferFromIli trsfFromIli = new TransferFromIli();
// map ili-classes to sql-tables
// TODO move default SRS to config
DbSchema schema;
try {
schema = trsfFromIli.doit(td, eles, mapping, config, idGen, trafoConfig, class2wrapper, customMapping);
} catch (Ili2dbException e) {
throw new Ili2dbException("mapping of ili-classes to sql-tables failed", e);
}
if (schema == null) {
return;
}
if (!(conn instanceof GeodbConnection)) {
trsfFromIli.addBasketsTable(schema);
trsfFromIli.addImportsTable(schema);
TransferFromIli.addInheritanceTable(schema, Integer.parseInt(config.getMaxSqlNameLength()));
TransferFromIli.addSettingsTable(schema);
TransferFromIli.addTrafoConfigTable(schema);
TransferFromIli.addModelsTable(schema, config);
trsfFromIli.addEnumTable(schema);
TransferFromIli.addTableMappingTable(schema);
TransferFromIli.addAttrMappingTable(schema);
DbExtMetaInfo.addMetaInfoTables(schema);
idGen.addMappingTable(schema);
if (config.getCreateMetaInfo()) {
MetaAttrUtility.addMetaAttributesTable(schema);
}
}
// TODO create geodb domains
if (conn instanceof GeodbConnection) {
}
GeneratorDriver drv = new GeneratorDriver(gen);
idGen.initDb(conn, dbusr);
idGen.initDbDefs(gen);
drv.visitSchema(config, schema);
// is a create script requested by user?
String createscript = config.getCreatescript();
if (createscript != null && (gen instanceof GeneratorJdbc)) {
writeScript(createscript, ((GeneratorJdbc) gen).iteratorCreateLines());
}
// is a drop script requested by user?
String dropscript = config.getDropscript();
if (dropscript != null && (gen instanceof GeneratorJdbc)) {
writeScript(dropscript, ((GeneratorJdbc) gen).iteratorDropLines());
}
if (!(conn instanceof GeodbConnection)) {
// update mapping table
mapping.updateTableMappingTable(conn, config.getDbschema());
mapping.updateAttrMappingTable(conn, config.getDbschema());
trafoConfig.updateTrafoConfig(conn, config.getDbschema());
// update inheritance table
trsfFromIli.updateInheritanceTable(conn, config.getDbschema());
// update enum table
trsfFromIli.updateEnumTable(conn);
trsfFromIli.updateMetaInfoTables(conn);
TransferFromIli.addModels(conn, td, config.getDbschema());
if (!config.isConfigReadFromDb()) {
TransferFromIli.updateSettings(conn, config, config.getDbschema());
}
// import meta-attributes from .toml file
if (config.getIliMetaAttrsFile() != null) {
if (config.getCreateMetaInfo()) {
try {
EhiLogger.logState("run import meta-attributes from toml file");
MetaAttrUtility.addMetaAttrsFromToml(td, new java.io.FileReader(config.getIliMetaAttrsFile()));
} catch (FileNotFoundException e) {
throw new Ili2dbException("import meta-attributes failed", e);
}
} else {
throw new Ili2dbException("import meta-attributes requires --createMetaInfo option");
}
}
if (config.getCreateMetaInfo()) {
// update meta-attributes table
MetaAttrUtility.updateMetaAttributesTable(conn, config.getDbschema(), td);
// set elements' meta-attributes
MetaAttrUtility.addMetaAttrsFromDb(td, conn, config.getDbschema());
}
}
// run post-script
if (config.getPostScript() != null) {
try {
EhiLogger.logState("run schemaImport post-script...");
DbUtility.executeSqlScript(conn, new java.io.FileReader(config.getPostScript()));
} catch (FileNotFoundException e) {
throw new Ili2dbException("schemaImport post-script statements failed", e);
}
}
// }
if (!connectionFromExtern) {
try {
conn.commit();
} catch (SQLException e) {
throw new Ili2dbException("failed to commit", e);
}
}
} catch (java.io.IOException ex) {
throw new Ili2dbException(ex);
}
try {
if (!connectionFromExtern) {
if (conn != null) {
try {
conn.close();
} finally {
conn = null;
config.setJdbcConnection(null);
}
}
}
EhiLogger.logState("...done");
} catch (java.sql.SQLException ex) {
EhiLogger.logError(ex);
}
} finally {
ao.end();
}
} catch (Ili2dbException ex) {
if (logfile != null) {
logfile.logEvent(new StdLogEvent(LogEvent.ERROR, null, ex, null));
}
throw ex;
} catch (java.lang.RuntimeException ex) {
if (logfile != null) {
logfile.logEvent(new StdLogEvent(LogEvent.ERROR, null, ex, null));
}
throw ex;
} finally {
if (logfile != null) {
EhiLogger.getInstance().removeListener(logfile);
logfile.close();
logfile = null;
}
if (logStderr != null) {
EhiLogger.getInstance().addListener(StdListener.getInstance());
EhiLogger.getInstance().removeListener(logStderr);
}
}
}
Aggregations