Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Database Schema Information from Groovy

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)?

like image 209
mfollett Avatar asked Feb 29 '12 21:02

mfollett


People also ask

How do I show database schema?

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.

How do I display SQL schema?

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.

Which of the following databases are supported by Groovy SQL API?

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.


1 Answers

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

like image 75
tim_yates Avatar answered Nov 05 '22 12:11

tim_yates