Download the PHP package ikkez/f3-schema-builder without Composer

On this page you can find all versions of the php package ikkez/f3-schema-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package f3-schema-builder

SQL Schema Builder

An extension for creating and altering SQL database tables.

This plugin provides a SQL table schema builder for the PHP Fat-Free Framework. It might be useful for installation scripts, dynamic applications or CMS environments.

Currently drivers for MySQL, SQLite, PostgreSQL & SQL Server are supported and fully tested. Further support for Sybase, Oracle and DB2 drivers are already included, but experimental. Handle with care and test your application. No warranty at all.

This plugin was made for F3 version 3.x and requires PHP 5.4+


Installation

Just copy schema.php into F3's lib/db/sql folder. Done.

If you use composer, you can add this package by running composer require ikkez/f3-schema-builder:dev-master

Quick Start

To work with the Schema builder, you need an active SQL Connection. Create one like this:

Now create a Schema object to work on. Inject the DB object into its constructor:

Create Tables

Creating new tables is super easy. Let's have a look at this example:

The createTable() method returns a new table object (instance of TableCreator) for creation purpose. You may add new columns, indexes and change the primary key with it. New tables will always contain an auto-incremented, primary-key field named id, which is required for further SQL\Mapper usage. All actions on a table object that affects its schema, are collected first and needs an additional build() command to be executed to really take effect on the database. If you're unsure of the result, you can run a simulation of that build method and have a look at the generated queries, the Schema Builder would have executed, with the following call:

Add Columns

Using the $table->addColumn() method will create a new Column object and adds it to the table object. We can use fluent calls for configuring these columns.

Here is a list of possible configuration methods:

$column->type( $datatype [ bool $force = FALSE])

Set datatype of this column. Usually a constant of type \DB\SQL\Schema::DT_{datatype}. Have a look at the Column Class API for more details about datatypes. When $force is TRUE, the $datatype string is used as a raw value as it is and passed through the creation query (useful for custom data types).

$column->nullable( $state )

Set this column as NULL or NOT NULL. Default is true / nullable.

$column->defaults( $value )

Adds a default value for records.

$column->after( $name )

Trys to place the new column behind an existing one.

$column->index([ bool $unique = false ])

Add an index for that field. $unique makes it a UNIQUE INDEX.

Alter Tables

Altering existing tables is quite similar to creating them, but offers a bunch more possibilities. A basic example:

As you can see, $schema->alterTable() returns a new table object (instance of TableModifier) for altering purpose, which provides all methods of the TableCreator plus some more actions like removing or renaming columns. Here is a list of method you can use:

The SchemaBuilder will quote all your table and column identifiers and should be resistant against preserved word errors.


API Usage

Schema Class

The Schema class provides you the following simple methods for:

Managing databases

$schema->getDatabases();

Returns an array of all databases available (except for SQLite). Can be useful for installation purpose, when you want the user to select a database to work on. Therefor just create your DB connection without selecting a database like:

Some DB engine default setups also grants simple read operations, without setting a user / password.

Managing tables

$schema->getTables();

Returns an array of all tables available within the current database.

$schema->createTable( string $tableName );

Returns a new table object for creation purpose.

$schema->alterTable( string $tableName );

Returns a table object for altering operations on already existing tables.

$schema->renameTable( string $name, string $new_name, [ bool $exec = true ]);

Renames a table. If you set $exec to FALSE, it will return the generated query instead of executing it. You can also use a short-cut on an altering table object, like $table->rename( string $new_name, [ bool $exec = true ]);.

$schema->truncateTable( string $name, [ bool $exec = true ]);

Clear the contents of a table. Set $exec to FALSE will return the generated query instead of executing it.

$schema->dropTable( string $name, [ bool $exec = true ]);

Deletes a table. Set $exec to FALSE will return the generated query instead of executing it. You can also use a short-cut on an altering table object, like $table->drop([ bool $exec = true ]);.

$schema->isCompatible( string $colType, string $colDef );

This is useful for reverse lookup. It checks if a data type is compatible with a given column definition, I.e. $schema->isCompatible('BOOLEAN','tinyint(1)');.

TableCreator Class

This class is meant for creating new tables. It can be created by using $schema->createTable($name).

$table->addColumn($key,$args = null); Column

This creates a new Column object and saves a reference to it. You can configure the Column for your needs using further fluent calls, setting its public parameters or directly via config array like this:

$table->addIndex($columns, $unique = FALSE);

You can add an index to a column by configuring the Column object while adding the new column, or like this:

For adding an combined index on multiple columns, just use an array as parameter:

$table->primary($pkeys);

If you like to change the default id named primary-key right on the creation of a new table, you can use this one:

This will rename the id field to uid. If you like to set a primary key on multiple columns (a composite key), use an array:

The first element of this pkey array will always be treated as an auto-incremented field.

example:

Now your primary key is build upon 2 columns, to use records like id=1, version=1 and id=1, version=2.

$table->setCharset( string $str [, $collation = 'unicode' ]);

This method will set a custom charset and default collation to a new table.

In example, this will set an utf8mb4 charset and a utf8mb4_unicode_ci collation as default for the new table:

NB: currently only effects MySQL. 1-4 Multibyte UTF8 chars work out of the box in Postgre, SQlite. No workaround for SQL Server yet.

$table->build([ bool $exec = true ]);

This will start the table generation process and executes all queries if $exec is TRUE, otherwise it will just return all queries as array.

TableModifier Class

This class is ment for creating new tables. It can be created by using $schema->alterTable($name).

$table->addColumn($key,$args = null); Column

Adds a new column

$table->renameColumn( string $name, string $new_name );

This is used to rename an existing column.

$table->updateColumn( string $name, string datatype, [ bool $force = false ]);

This is used to modify / update the column's datatype.

$table->dropColumn( string $name );

Tries to removes a column from the table, if it exists.

$table->addIndex( string | array $columns, [ bool $unique = false ]);

Creates a index or unique index for one or multiple columns on the table.

$table->dropIndex( string | array $columns );

Drops an index.

$table->listIndex();

Returns an associative array with index name as key and array('unique'=>$value) as value.

$table->primary( string | array $pkeys);

Creates a new primary or composite key on the table.

$table->getCols([ bool $types = false ]);

Returns an array of existing table columns. If $types is set to TRUE, it will return an associative array with column name as key and the schema array as value.

$table->build([ bool $exec = true ]);

This generates the queries needed for the table alteration and executes them when $exec is true, otherwise it returns them as array.

$table->rename( string $new_name, [ bool $exec = true ]);

This will instantly rename the table. Notice: Instead of being executed on calling build() the execution is controlled by $exec.

$table->drop([ bool $exec = true ]);

This will instantly drop the table. Notice: Instead of being executed on calling build() the execution is controlled by $exec.

$table->isCompatible( string $colType, string $column );

This is useful for reverse lookup. It checks if a data type is compatible with an existing column type, I.e. $table->isCompatible('BOOLEAN','hidden');.

$table->setCharset( string $str [, $collation = 'unicode' ]);

This method will set a custom charset and collation and will convert existing tables upon build().

In this example we will convert a table to an utf8mb4 charset and a utf8mb4_general_ci collation:

Column Class

The method $table->addColumn($columnName); adds a further column field to the selected table and creates and returns a new Column object, that can be configured in different ways, before finally building it.

type( string $datatype, [ bool $force = false ])

Set datatype of this column. The $force argument will disable the datatype check with the included mappings and uses your raw string as type definition.

You can use these available mapped types as constants in \DB\SQL\Schema:

Type Description Storage size Save Range
DT_BOOL
DT_BOOLEAN
resolves in a numeric at least 1 byte 0,1
DT_INT1
DT_TINYINT
exact integer at least 1 byte lower: 0, upper; 255
DT_INT2
DT_SMALLINT
exact integer at least 2 bytes ±32,768
DT_INT4
DT_INT
exact integer 4 bytes ±2,147,483,648
DT_INT8
DT_BIGINT
exact integer at most 8 bytes ±2^63
DT_FLOAT approximate numeric 4 bytes ±1.79E + 308
DT_DECIMAL
DT_DOUBLE
exact numeric at least 5 bytes ±10^38+1
DT_VARCHAR128 character string 128 bytes 128 chars
DT_VARCHAR256 character string 256 bytes 256 chars
DT_VARCHAR512 character string 512 bytes 512 chars
DT_TEXT character string max length 2,147,483,647
DT_LONGTEXT character string max length 4,294,967,295
DT_DATE Y-m-d 3 bytes
DT_DATETIME Y-m-d H:i:s 8 bytes
DT_TIMESTAMP Y-m-d H:i:s 8 bytes
DT_BLOB
DT_BINARY
bytes

usage:

there are also a bunch of shorthand methods available, you can use instead of type():

passThrough([ bool $state = TRUE ])

When pass-through is enabled, the datatype value is treated as a raw value, which makes it possible to set any other custom data type that is not covered by the included aliases. This is equivalent to type() with $force = TRUE.

nullable( bool $state )

Set this column as NULL or NOT NULL. Default is TRUE / nullable. You can set defaults to nullable fields as well.

defaults( mixed $value )

Adds a default value for records. Usually a string or integer value or NULL.

CURRENT_TIMESTAMP as dynamic default value

But if you like to add a timestamp of the current time to new inserted records, you can use a TIMESTAMP field with a special default value to achieve this.

after( string $name )

Try to place the new column behind an existing one. (only works for SQLite and MySQL)

index([ bool $unique = false ])

Add an index for that field. $unique makes it a UNIQUE INDEX.

copyfrom( string | array $args )

Feed column from array or hive key.

getColumnArray()

Returns an array of the current column configuration

getTypeVal()

Returns the resolved column data type.

License

GPLv3


Like this Plugin?

buy me a beer


All versions of f3-schema-builder with dependencies

PHP Build Version
Package Version
Requires bcosca/fatfree-core Version ^3.6
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package ikkez/f3-schema-builder contains the following files

Loading the files please wait ....