cristof commented on code in PR #144:
URL: https://github.com/apache/openjpa/pull/144#discussion_r3761215139


##########
openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaTool.java:
##########
@@ -479,71 +509,140 @@ protected void deleteTableContents()
         Collection<Table> tables = new LinkedHashSet<>();
         for (Schema schema : schemas) {
             Table[] ts = schema.getTables();
-            for (Table t : ts) {
-                tables.add(t);
-            }
+            Collections.addAll(tables, ts);
         }
         Table[] tableArray = tables.toArray(new Table[tables.size()]);
         Connection conn = _ds.getConnection();
+        // Truncate is a best-effort operation: the repo SchemaGroup may
+        // declare tables the dialect never actually created (e.g.
+        // OPENJPA_SEQUENCE_TABLE on dialects that prefer native sequences,
+        // or tables in other schemas). Log missing-table errors instead of
+        // aborting.
+        boolean savedIgnore = _ignoreErrs;
+        _ignoreErrs = true;
         try {
             String[] sql = _conf.getDBDictionaryInstance()
                 .getDeleteTableContentsSQL(tableArray, conn);
             if (!executeSQL(sql)) {
                 _log.warn(_loc.get("delete-table-contents"));
             }
         } finally {
+            _ignoreErrs = savedIgnore;
             closeConnection(conn);
         }
     }
 
-    protected void executeScript() throws SQLException {
-        if (_scriptToExecute == null) {
-            _log.warn(_loc.get("generating-execute-script-not-defined"));
-            return;
+    private BufferedReader getScriptReader() throws IOException {
+        BufferedReader reader = null;
+        if (_scriptReader != null) {
+            reader = (_scriptReader instanceof BufferedReader br) ? br : new 
BufferedReader(_scriptReader);
+        } else {
+            URL url = null;
+            // Try file: URI or absolute path first
+            if (_scriptToExecute.startsWith("file:")) {
+                try {
+                    url = new URI(_scriptToExecute).toURL();
+                } catch (Exception e) {
+                    // fall through to classloader lookup
+                }
+            } else {
+                File f = new File(_scriptToExecute);
+                if (f.isAbsolute() && f.exists()) {
+                    url = f.toURI().toURL();
+                }
+            }
+            // Fall back to classloader resource lookup
+            if (url == null) {
+                url = _conf.getClassResolverInstance()
+                    .getClassLoader(SchemaTool.class, null)
+                    .getResource(_scriptToExecute);
+            }
+            if (url == null) {
+                _log.error(_loc.get("generating-execute-script-not-found", 
_scriptToExecute));
+                return null;
+            }
+            _log.info(_loc.get("generating-execute-script", _scriptToExecute));
+            reader = new BufferedReader(new 
InputStreamReader(url.openStream()));
         }
+        return reader;
+    }
 
-        URL url = AccessController.doPrivileged(
-                
J2DoPrivHelper.getResourceAction(_conf.getClassResolverInstance().
-                        getClassLoader(SchemaTool.class, null), 
_scriptToExecute));
-
-        if (url == null) {
-            _log.error(_loc.get("generating-execute-script-not-found", 
_scriptToExecute));
+    protected void executeScript() throws SQLException {
+        if (_scriptReader == null && StringUtil.isBlank(_scriptToExecute)) {
+            _log.warn(_loc.get("generating-execute-script-not-defined"));
             return;
         }
 
-        _log.info(_loc.get("generating-execute-script", _scriptToExecute));
-        BufferedReader reader = null;
-        try {
-            reader = new BufferedReader(new 
InputStreamReader(url.openStream()));
-            String sql;
+        try (BufferedReader reader = getScriptReader()) {
+            if (reader == null) {
+                return;
+            }
+            String line;
             List<String> script = new ArrayList<>();
-            while ((sql = reader.readLine()) != null) {
-                sql = sql.trim();
-                if (sql.startsWith("--") || sql.startsWith("/*") || 
sql.startsWith("//")) {
+            StringBuilder stmt = new StringBuilder();
+            AtomicBoolean insideMultilineComments = new AtomicBoolean(false);
+            Function<String, String> stripComments = (str) -> {

Review Comment:
   Commited the suggested fix with a correspondent test, but I think this 
should be addressed better in the future, maybe with a regex parsing of the 
scripts.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to