📄 databaselib.java
字号:
//--- step 2 : remove objects if (cb != null) cb.schemaObjects(objects.size()); while(true) { boolean removed = false; for (Iterator<ObjectInfo> i=objects.iterator(); i.hasNext();) { ObjectInfo oi = i.next(); String query = "DROP "+ oi.type +" "+ oi.name; if (safeExecute(dbms, query)) { removed = true; i.remove(); if (cb != null) cb.removed(oi.name, oi.type); } } if (objects.size() == 0) return; //--- if no object was removed then we have a cyclic loop if (!removed) { if (cb != null) { ArrayList<String> al = new ArrayList<String>(); for (ObjectInfo oi : objects) al.add(oi.name); cb.cyclicRefs(al); } return; } } } //--------------------------------------------------------------------------- private boolean safeExecute(Dbms dbms, String query) { try { dbms.execute(query); //--- as far as I remember, PostgreSQL needs a commit even for DDL dbms.commit(); return true; } catch (SQLException e) { dbms.abort(); return false; } } //--------------------------------------------------------------------------- public void createSchema(Dbms dbms, CallBack cb) throws FileNotFoundException, IOException, SQLException { Lib.log.info("Creating database schema"); List<String> schema = loadSchemaFile(dbms.getURL()); StringBuffer sb = new StringBuffer(); for (String row : schema) if (!row.toUpperCase().startsWith("REM") && !row.startsWith("--") && !row.trim().equals("")) { sb.append(" "); sb.append(row); if (row.endsWith(";")) { String sql = sb.toString(); sql = sql.substring(0, sql.length() -1); if (cb != null) cb.creating(getObjectName(sql), getObjectType(sql)); dbms.execute(sql); sb = new StringBuffer(); } } } //--------------------------------------------------------------------------- /** Transaction must be aborted on error */ public void fillTables(Dbms dbms, CallBack cb) throws Exception { Lib.log.info("Filling database tables"); List<String> schema = loadSchemaFile(dbms.getURL()); for(String row : schema) if (row.toUpperCase().startsWith("CREATE TABLE ")) { String table = getObjectName(row); String file = appPath +SETUP_DIR+ "/db/"+ table +".ddf"; if (!new File(file).exists()) { if (cb != null) cb.skipping(table); } else { if (cb != null) cb.filling(table, file); Lib.log.debug(" - Filling table : "+ table); Import.load(dbms.getConnection(), table, file); dbms.commit(); } } } //--------------------------------------------------------------------------- //--- //--- Private methods //--- //--------------------------------------------------------------------------- private List<String> loadSchemaFile(String url) throws FileNotFoundException, IOException { //--- find out which dbms schema to load String file = "create-db-mckoi.sql"; if (url.indexOf("oracle") != -1) file = "create-db-oracle.sql"; else if (url.indexOf("mysql") != -1) file = "create-db-mysql.sql"; else if (url.indexOf("postgresql") != -1) file = "create-db-postgres.sql"; //--- load the dbms schema return Lib.text.load(appPath +SETUP_DIR+ "/sql/"+ file); } //--------------------------------------------------------------------------- private String getObjectName(String createStatem) { StringTokenizer st = new StringTokenizer(createStatem, " "); st.nextToken(); st.nextToken(); return st.nextToken(); } //--------------------------------------------------------------------------- private String getObjectType(String createStatem) { StringTokenizer st = new StringTokenizer(createStatem, " "); st.nextToken(); return st.nextToken(); } //--------------------------------------------------------------------------- private void addTemplates(Dbms dbms) throws Exception { String siteId = getSiteId(dbms); String siteURL= Lib.site.getSiteURL(dbms); String date = new ISODate().toString(); int serial = 1; File schemaDir = new File(appPath, "gast/setup/templates"); for (File schema : Lib.io.scanDir(schemaDir)) //--- skip '.svn' folders and other hidden files if (!schema.getName().startsWith(".")) for (File temp : Lib.io.scanDir(schema, "xml")) { Lib.log.debug(" - Adding template file : "+ temp.getName()); Document doc = Lib.xml.load(temp); String uuid = UUID.randomUUID().toString(); Element xml = doc.getRootElement(); String file = temp.getName(); String templ = "y"; String title = null; if (file.startsWith("sub-")) { templ = "s"; title = file.substring(4, file.length() -4); } //--- templates are by default assigned to administrator/intranet group Lib.metadata.insertMetadata(dbms, schema.getName(), xml, serial, siteId, date, date, uuid, 1, null, templ, title); setupTemplatePriv(dbms, serial); dbms.commit(); serial++; } } //--------------------------------------------------------------------------- /** NOT Transactional */ /** This method should be called only during setup, when the database is empty * and there is only 1 siteId string into settings */ private String getSiteId(Dbms dbms) throws SQLException { String query = "SELECT value FROM Settings WHERE name='siteId'"; List list = dbms.select(query).getChildren(); Element rec = (Element) list.get(0); return rec.getChildText("value"); } //--------------------------------------------------------------------------- private void setupTemplatePriv(Dbms dbms, int id) throws SQLException { String query = "INSERT INTO OperationAllowed(groupId, metadataId, operationId) "+ "VALUES(?, ?, ?)"; dbms.execute(query, 1, id, new Integer(AccessManager.OPER_VIEW)); } //--------------------------------------------------------------------------- private void setupSiteId(Dbms dbms) throws SQLException, IOException { String uuid = UUID.randomUUID().toString(); //--- duplicate dummy logo to reflect the uuid FileInputStream is = new FileInputStream (appPath +"/gast/images/dummy.gif"); FileOutputStream os = new FileOutputStream(appPath +"/web/geonetwork/images/logos/"+ uuid +".gif"); BinaryFile.copy(is, os, true, true); dbms.execute("UPDATE Settings SET value=? WHERE name='siteId'", uuid); } //--------------------------------------------------------------------------- private void setupVersion(Dbms dbms) throws SQLException { String version = Lib.server.getVersion(); String subVersion = Lib.server.getSubVersion(); dbms.execute("UPDATE Settings SET value=? WHERE name='version'", version); dbms.execute("UPDATE Settings SET value=? WHERE name='subVersion'", subVersion); } //--------------------------------------------------------------------------- //--- //--- Variables //--- //--------------------------------------------------------------------------- private String appPath; //--------------------------------------------------------------------------- private static final String SETUP_DIR = "/gast/setup";}//=============================================================================class ObjectInfo{ public String name; public String type;}//=============================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -