Skip to content

ArtemRochev/SqlGenerator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SqlGenerator

Generate 'CREATE TABLE <table_name> ...' statement for PosqtgreSQL
with triggers that update timestamps
from yaml file

Post:
   fields:
       title: varchar(50)
       content: text

Category:
   fields:
       name: varchar(50)

usage:

$generator = new SqlGenerator;

echo $generator->generateSql('schema.yaml');

output:

CREATE TABLE Post (
	id SERIAL,
	post_title varchar(50),
	post_content text,
	post_created TIMESTAMPTZ DEFAULT now(),
	post_updated TIMESTAMPTZ DEFAULT now()
);

CREATE TABLE Category (
	id SERIAL,
	category_name varchar(50),
	category_created TIMESTAMPTZ DEFAULT now(),
	category_updated TIMESTAMPTZ DEFAULT now()
);

CREATE FUNCTION update_timestamp()	
RETURNS TRIGGER AS $$
BEGIN
    NEW.post_updated := now();
    RETURN NEW;	
END;
$$ language 'plpgsql';

CREATE TRIGGER update_timestamp
	BEFORE UPDATE ON Post
	FOR EACH ROW
	EXECUTE PROCEDURE update_timestamp();

CREATE FUNCTION update_timestamp()	
RETURNS TRIGGER AS $$
BEGIN
    NEW.category_updated := now();
    RETURN NEW;	
END;
$$ language 'plpgsql';

CREATE TRIGGER update_timestamp
	BEFORE UPDATE ON Category
	FOR EACH ROW
	EXECUTE PROCEDURE update_timestamp();

About

(PHP) generate 'CREATE TABLE ..' for PostgreSQL

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages