How should I design the interface to a SQL column generator in PHP?
-
20-08-2019 - |
Question
I'm working on a framework in PHP, part of this frameworks job is to write SQL code to generate new MySQL columns.
What is the most elegant way to pass in a bunch of parameters for a column and then turn that into SQL code? As an example here is some YAML I have, that specifies parameters for creating a varchar column:
- type: character
data-type:
type: varchar
length: 255
decimals: null
unsigned: null
zerofill: null
collate: utf8_unicode_ci
character-set: utf8
binary: false
spatial-type: null
values: null
nullify: true
default: null
increment: false
unique: false
primary: false
comment: This is a small general text field.
format: default
storage: default
My design constraints are as follows:
- The YAML cannot contain any SQL syntax
- The PHP code must be as concise as possible.
Solution
I recommend you make a class Column that takes, as a constructor argument, an array that can define the various configuration points you want. The ones that don't matter in each particular case you just don't define.
OTHER TIPS
Not totaly related but do you know the Propel project ?
They use xml to explain model but the symfony project (which use propel as a plugin) use yaml.