felipebz / zpa

Parser and static code analysis tool for PL/SQL and Oracle SQL.
https://zpa.felipebz.com
GNU Lesser General Public License v3.0
211 stars 77 forks source link

Provide full path to current file (not just file name) #166

Closed thetroy closed 1 year ago

thetroy commented 1 year ago

I see that I can get the current file's name using context.plSqlFile().fileName(), but I require the full path.

Why do I need the path? our migrates use a separate (non-sql) file that lists the sql files in the order they will be applied. That file is located relative to the sql files for each release. I want to write a rule to ensure the current sql file is included in the migrate file.

If there is a way to do this without changing the code, please let me know.

If a code change is needed I'm happy to submit a pull request. But will need some pointers on the approach. Adding file() to PlSqlFile seems reasonable since InputFile has a File and SonarQubePlSqlFile has an InputFile. TestPlSqlFile also has a File if created with fromFile(). Alternatively, we could add path() returning a String (or a Path), but this does not eliminate the question: what to do in the TestPlSqlFile.fromString() case?

Let me know if I can be of help.

Thanks for creating a great plugin!

felipebz commented 1 year ago

Hi,

I think you could use context.plSqlFile().toString() to get the file path. It returns the path relative to the project base directory (for example: 'src/mydir/myfile.sql'). So you could use new File(context.plSqlFile().toString()) to get a File object.

thetroy commented 1 year ago

Hello Felipe,

Thanks for the tip. Unfortunately, it didn't work, not for TesetPlSqlFile anyway.

image

Here's the class for quick reference:

    private class TestPlSqlFile private constructor(private val contents: String,
                                                    private val name: String) : PlSqlFile {
        override fun contents(): String = contents
        override fun fileName(): String = name
        override fun type(): PlSqlFile.Type = PlSqlFile.Type.MAIN

        companion object {
            fun fromString(contents: String): PlSqlFile = TestPlSqlFile(contents.trim(), "unnamed")

            fun fromFile(file: File): PlSqlFile =
                try {
                    TestPlSqlFile(String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8), file.name)
                } catch (e: IOException) {
                    throw IllegalStateException("Cannot read $file", e)
                }
        }
    }
felipebz commented 1 year ago

Thank you for the feedback.

You're correct, unfortunately, it doesn't work on the tests. It only works correctly on a "real" scan with SonarScanner.

As requested, I have added the path() method to the PlSqlFile interface. 😊

thetroy commented 1 year ago

Awesome! we tried this out yesterday and to worked for us. Thank you!