As you may know if you have been following my recent posts then I am currently embarking on becoming my own Ishmael and trying to manage Google Cloud Projects!

One of the things that occurred to me is that I do not want to be hand writing a lot of statements (I am up to 33 tables already for the meta data alone) for the administration of the system so I decided to take a break and write some code that will generate that for me.

I am currently looking to use a MySQL database for the backend of the permissions service, and so …..

What does the code do:

In the repo at the moment you will find two files, one for creating simple add (INSERT) stored procedures, and the second for simple get (SELECT) procedures.

The add SELECT statement that generates the add (INSERT) procedure takes in parameters for all the columns in the table and will INSERT them if the unique key(s) on the table are not violated.

The get SELECT statement generates procedures that will get (SELECT) you the primary key for the table based on the unique key of the table.

When you run the SELECT statement for the creation of the simple add or the get stored procedures you will get output of four things:

  1. DROP PROCEDURE statement
  2. CREATE PROCEDURE statement
  3. CALL statement
  4. SELECT of the out variable as an example / test

How does it do it:

The SELECT statements in the repo use the meta tables found in the information_schema of MySQL. Specifically the tables, key_column_useage and columns table.

There is then a lot of string manipulation and use of sub-selects to get the meta data into a format that could be turned into code for a stored procedure.

GOTCHA: There is one assumption that you can change to suit your needs in that it assumes ALL your unique keys are MySQL UNIQUE INDEX’s and are named like: uix%.

Why do it like this:

At the moment these statements provide the functionality that I need and at some point I can add to some form of user interface, with all the security of stored procedures. I also do not have to worry too much about changing the underlying database structure if the output of the stored procedures remain the same VS a hand coded SQL statement for the future.

read more