I'm trying to wrap some Groovy test around code that modifies a schema. What's the idiomatic Groovy approach towards getting information about a database schema (e.g. determining the names & types of columns on a table and the primary key)?
To show the schema, we can use the DESC command. This gives the description about the table structure. The following is the syntax. DESCRIBE yourDatabasename.
You can get a list of the schemas using an SSMS or T-SQL query. To do this in SSMS, you would connect to the SQL instance, expand the SQL database and view the schemas under the security folder. Alternatively, you could use the sys. schemas to get a list of database schemas and their respective owners.
The Groovy sql API supports a wide variety of databases, some of which are shown below. In our example, we are going to use MySQL DB as an example. In order to use MySQL with Groovy, the first thing to do is to download the MySQL jdbc jar file from the mysql site. The format of the MySQL will be shown below.
You can access metaData on the entire database by doing:
def sql = Sql.newInstance("jdbc:mysql://localhost:3306/DB", "uid", "pwd", "com.mysql.jdbc.Driver")
def metadata = sql.connection.metaData
Thus will give you an instance of DatabaseMetaData to play with
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With