2017 © Pedro Peláez
 

library yasql-php

A PHP implementation for YAML Ain't SQL

image

aryelgois/yasql-php

A PHP implementation for YAML Ain't SQL

  • Monday, May 28, 2018
  • by aryelgois
  • Repository
  • 0 Watchers
  • 0 Stars
  • 63 Installations
  • PHP
  • 3 Dependents
  • 1 Suggesters
  • 0 Forks
  • 0 Open issues
  • 15 Versions
  • 31 % Grown

The README.md

YASQL-PHP

Index:, (*1)

Intro

This is a PHP implementation for YAML Ain't SQL, whose specification can be found in aryelgois/yasql., (*2)

Install

Enter the terminal, navigate to your project root and run:, (*3)

composer require aryelgois/yasql-php, (*4)

Setup

This package provides CLI tools to work with YASQL files. To use them, add the following in your composer.json:, (*5)

{
    "scripts": {
        "yasql-build": "aryelgois\\YaSql\\Composer::build",
        "yasql-generate": "aryelgois\\YaSql\\Composer::generate"
    }
}

You also need a config file for the builder command. The default is to place it in config/databases.yml, but you can choose another place or have more than one configuration.
(see Builder specifications), (*6)

Usage

yasql-build

First, create databases following the YASQL schema and list them in your config file. Then run the following command in your project root:, (*7)

composer yasql-build -- [ config=path/to/config_file.yml | -c ]
                        [ output=path/to/output/ ]
                        [ vendor=vendor/package ]
  • config: Lists YASQL databases to be built and vendors to include
    • The -c flag indicates that you do not want any config file. It is useful when using vendor arguments. It is the same as config=''
  • output: Directory where files are generated
  • vendor: Additional vendor to include (using default config file location)

Notes:, (*8)

  • Paths in the command line are relative to the project root
  • Paths in the config file are relative to the file itself
  • Absolut paths are absolut (they start with /)
  • Vendors are located in Composer's vendor dir
  • You can omit config for using the default config/databases.yml
  • You can omit output for using the default build/
  • You can add multiple vendor arguments
  • It might be a good idea to add the output directory to your .gitignore

This command creates .sql files in the output directory, so you can import them into your sql server., (*9)

yasql-generate

If you only want to generate the SQL from one YASQL schema, run the following command:, (*10)

composer yasql-generate -- path/to/yasql.yml [ indentation ], (*11)

The first argument is the path to a YASQL file, the second is a optional indentation to be used (default is 2 spaces)., (*12)

It will output to stdout, so you can add something like > output_database.sql to write the result in a file., (*13)

API

This package provides some classes to parse YASQL and generate SQL. They are under the namespace aryelgois\YaSql., (*14)

Composer

Provides Composer scripts to use this package from the command line.
(see how to configure the commands in Setup), (*15)

  • static build( Event $event ), (*16)

    It accepts arguments described in yasql-build., (*17)

  • static generate( Event $event ), (*18)

    It accepts arguments described in yasql-generate., (*19)

Controller

This class wrapps others, to make them easier to use., (*20)

  • static build( string $output , string $config , string $vendor [, array $vendors ] ), (*21)

    Use this method to build your databases into a specific directory.
    (see Builder), (*22)

  • static generate( string $yasql [, int $indent ] ), (*23)

    Use this to generate the SQL from a YASQL and get the result in a string.
    (see Generator), (*24)

  • static parse( string $yasql ), (*25)

    Use it to dump the parsed data from a YASQL. Basically, good for debugging.
    (see Parser), (*26)

Parser

  • __construct( string $yasql ), (*27)

    It parses a YASQL and extracts some data from each column, making them ready for the Generator., (*28)

    See the $yasql specification here., (*29)

  • getData(), (*30)

    Retrieves the parsed data in a multidimensional array., (*31)

Generator

  • __construct( Parser $parser [, int $indent ] ), (*32)

    Produces SQL that generates a database. It asks for a Parser object to ensure the data is valid., (*33)

  • output(), (*34)

    Retrieves the generated SQL in a multi line string., (*35)

Builder

  • __construct( [ string $output [, string $vendor ] ] ), (*36)

    Creates a new Builder object. Databases will go into $output, and vendors are searched in $vendors. Both paths can be absolut or relative, and default to build/ and vendor/ in the current working directory, respectively., (*37)

  • build( string $config [, array $vendors ] ), (*38)

    Generates a list of databases listed in $config file into the object's output directory., (*39)

  • getLog(), (*40)

    Retrieves log information from the build process., (*41)

config file

A YAML with the following keys: (all are optional), (*42)

  • databases: sequence of files with YASQL database schemas. It can be a string or a mapping of the YASQL path and a post sql (or a sequence of post files), (*43)

    Also, a name can be defined to overwrite the database's name. It is useful when you want to combine multiple database schemas in a single database. Just be careful with conflicting tables. Also note that external foreigns require special care in the file order that you run in the sql server, (*44)

  • indentation: used during the sql generation, (*45)

  • vendors: a map of vendors installed by Composer to config files inside them. It can be a string (for a single config) or a sequence of paths. They are relative to the vendor package root. Using ~ (yaml null) denotes the default config file path

Example:, (*46)

databases:
  - ../tests/example.yml
  - path: ../data/mydatabase.yml
    post: ../data/mydatabase_populate.sql
    name: AwesomeExample

indentation: 4

vendors:
  someone/package: config/databases.yml

The post file is useful for pre populated rows or to apply sql commands not covered by YASQL specification. Its content is appended to the generated sql., (*47)

Populator

A helper class for Builder. Use it to generate INSERT INTO statements to populate your databases., (*48)

This class is abstract, so you have to write a class that extends it. The reason is that the YAML with the data might be in a arbitrary layout, depending on your database schema., (*49)

To use it, you need a special post in the builder config:, (*50)

Example from aryelgois/databases:, (*51)

databases:
  - path: ../data/address.yml
    post:
      - ../data/address/populate_countries.sql
      - call: aryelgois\Databases\AddressPopulator
        with:
          - ../data/address/source/Brazil.yml

The post must map to a sequence, and the desired item is a map of:, (*52)

  • call: a fully qualified class that extends Populator, autoloadable by Composer
  • with: path to a YAML with the data to be processed. It can be a sequence

Utils

There is also a class with utility methods. They are used internally and can be used by whoever requires this package., (*53)

  • arrayAppendLast( array $array , string $last [, string $others ] ), (*54)

    Appends a string to the last item. Optionally, appends a string to the others. It is useful to generate sql statements with a list of comma separated items, and a semicolon at the last item., (*55)

Changelog

The Versions

28/05 2018

dev-develop

dev-develop

A PHP implementation for YAML Ain't SQL

  Sources   Download

MIT

The Requires

 

by Aryel Mota Góis

database sql schema php yaml yasql

28/05 2018

dev-master

9999999-dev

A PHP implementation for YAML Ain't SQL

  Sources   Download

MIT

The Requires

 

by Aryel Mota Góis

database sql schema php yaml yasql

28/05 2018

v0.6.0

0.6.0.0

A PHP implementation for YAML Ain't SQL

  Sources   Download

MIT

The Requires

 

by Aryel Mota Góis

database sql schema php yaml yasql

20/04 2018

v0.5.2

0.5.2.0

A PHP implementation for YAML Ain't SQL

  Sources   Download

MIT

The Requires

 

by Aryel Mota Góis

database sql schema php yaml yasql

20/04 2018

v0.5.1

0.5.1.0

A PHP implementation for YAML Ain't SQL

  Sources   Download

MIT

The Requires

 

by Aryel Mota Góis

database sql schema php yaml yasql

26/03 2018

v0.5.0

0.5.0.0

A PHP implementation for YAML Ain't SQL

  Sources   Download

MIT

The Requires

 

by Aryel Mota Góis

database sql schema php yaml yasql

08/02 2018

v0.4.2

0.4.2.0

A PHP implementation for YAML Ain't SQL

  Sources   Download

MIT

The Requires

 

by Aryel Mota Góis

database sql schema php yaml yasql

08/02 2018

v0.4.1

0.4.1.0

A PHP implementation for YAML Ain't SQL

  Sources   Download

MIT

The Requires

 

by Aryel Mota Góis

database sql schema php yaml yasql

08/02 2018

v0.4.0

0.4.0.0

A PHP implementation for YAML Ain't SQL

  Sources   Download

MIT

The Requires

 

by Aryel Mota Góis

database sql schema php yaml yasql

08/02 2018

v0.x-dev

0.9999999.9999999.9999999-dev

A PHP implementation for YAML Ain't SQL

  Sources   Download

MIT

The Requires

 

by Aryel Mota Góis

database sql schema php yaml yasql

07/02 2018

v0.4.0-alpha

0.4.0.0-alpha

A PHP implementation for YAML Ain't SQL

  Sources   Download

MIT

The Requires

 

by Aryel Mota Góis

database sql schema php yaml yasql

06/01 2018

v0.3.1

0.3.1.0

A PHP implementation for YAML Ain't SQL

  Sources   Download

MIT

The Requires

 

by Aryel Mota Góis

database sql schema php yaml yasql

06/01 2018

v0.3

0.3.0.0

A PHP implementation for YAML Ain't SQL

  Sources   Download

MIT

The Requires

 

by Aryel Mota Góis

database sql schema php yaml yasql

27/12 2017

v0.2

0.2.0.0

A PHP implementation for YAML Ain't SQL

  Sources   Download

MIT

The Requires

 

by Aryel Mota Góis

database sql schema php yaml yasql

22/12 2017

v0.1

0.1.0.0

A PHP implementation for YAML Ain't SQL

  Sources   Download

MIT

The Requires

 

by Aryel Mota Góis

database sql schema php yaml