Giter Site home page Giter Site logo

auraphp / aura.sqlschema Goto Github PK

View Code? Open in Web Editor NEW
41.0 14.0 14.0 458 KB

Independent schema discovery tools for MySQL, PostgreSQL, SQLite, and Microsoft SQL Server.

License: BSD 2-Clause "Simplified" License

PHP 100.00%
aura database php pdo sql

aura.sqlschema's Introduction

Aura.SqlSchema

Provides facilities to read table names and table columns from a database using a PDO connection.

Foreword

Installation

This library requires PHP 5.3 or later; we recommend using the latest available version of PHP as a matter of principle. It has no userland dependencies.

It is installable and autoloadable via Composer as aura/sqlschema.

Alternatively, download a release or clone this repository, then require or include its autoload.php file.

Quality

Scrutinizer Code Quality Code Coverage Build Status

To run the unit tests at the command line, issue phpunit at the package root. (This requires PHPUnit to be available as phpunit.)

This library attempts to comply with PSR-1, PSR-2, and PSR-4. If you notice compliance oversights, please send a patch via pull request.

Community

To ask questions, provide feedback, or otherwise communicate with the Aura community, please join our Google Group, follow @auraphp on Twitter, or chat with us on #auraphp on Freenode.

Getting Started

Instantiation

Instantiate a driver-specific schema object with a matching PDO instance:

<?php
use Aura\SqlSchema\ColumnFactory;
use Aura\SqlSchema\MysqlSchema; // for MySQL
use Aura\SqlSchema\PgsqlSchema; // for PostgreSQL
use Aura\SqlSchema\SqliteSchema; // for Sqlite
use Aura\SqlSchema\SqlsrvSchema; // for Microsoft SQL Server
use PDO;

// a PDO connection
$pdo = new PDO(...);

// a column definition factory
$column_factory = new ColumnFactory();

// the schema discovery object
$schema = new MysqlSchema($pdo, $column_factory);
?>

Fetching Table Lists

To get a list of tables in the database, issue fetchTableList():

<?php
$tables = $schema->fetchTableList();
foreach ($tables as $table) {
    echo $table . PHP_EOL;
}
?>

Fetching Column Information

To get information about the columns in a table, issue fetchTableCols():

<?php
$cols = $schema->fetchTableCols('table_name');
foreach ($cols as $name => $col) {
    echo "Column $name is of type "
       . $col->type
       . " with a size of "
       . $col->size
       . PHP_EOL;
}
?>

Each column description is a Column object with the following properties:

  • name: (string) The column name

  • type: (string) The column data type. Data types are as reported by the database.

  • size: (int) The column size.

  • scale: (int) The number of decimal places for the column, if any.

  • notnull: (bool) Is the column marked as NOT NULL?

  • default: (mixed) The default value for the column. Note that sometimes this will be null if the underlying database is going to set a timestamp automatically.

  • autoinc: (bool) Is the column auto-incremented?

  • primary: (bool) Is the column part of the primary key?

aura.sqlschema's People

Contributors

arabcoders avatar fale avatar harikt avatar henriquemoody avatar iansltx avatar jakejohns avatar maxakawizard avatar mindplay-dk avatar nyamsprod avatar pborreli avatar ptarjan avatar ramirovarandas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aura.sqlschema's Issues

Add functionality to alter/update existing database schema

I'd like to be able to have a database/table schema defined in some type of config file.

Then I'd like to use Sql_Schema to compare the existing database schema with the new schema in my config file.

I would then like to be able to use the comparison to build the database queries needed to update the database schema to my new schema and pass it to something similar to __toString(), which can be passed to PDO or ExtendedPdo.

I am prepared to create my own library to do this, but wanted to know if this would fit in the domain of what Sql_Schema does. Or should this go into another library, like Sql_SchemaManager?

What do you think?

I'd be happy to send PR's if you'd like.

Made changes to my fork of this repo to make it compatible with PHP 8.1

I have been using this package in some applications running in a PHP 7.4 environment. When I upgraded to PHP 8.1 and ran the applications I got the following deprecation warnings:

PHP Deprecated:  strtoupper(): Passing null to parameter #1 ($string) of type string is deprecated in C:\SoftwareProjects\test-php-project\vendor\aura\sqlschema\src\MysqlSchema.php on line 121

Deprecated: strtoupper(): Passing null to parameter #1 ($string) of type string is deprecated in C:\SoftwareProjects\test-php-project\vendor\aura\sqlschema\src\MysqlSchema.php on line 121
PHP Deprecated:  strtoupper(): Passing null to parameter #1 ($string) of type string is deprecated in C:\SoftwareProjects\test-php-project\vendor\aura\sqlschema\src\MysqlSchema.php on line 121

Deprecated: strtoupper(): Passing null to parameter #1 ($string) of type string is deprecated in C:\SoftwareProjects\test-php-project\vendor\aura\sqlschema\src\MysqlSchema.php on line 121
PHP Deprecated:  strtoupper(): Passing null to parameter #1 ($string) of type string is deprecated in C:\SoftwareProjects\test-php-project\vendor\aura\sqlschema\src\MysqlSchema.php on line 121

Deprecated: strtoupper(): Passing null to parameter #1 ($string) of type string is deprecated in C:\SoftwareProjects\test-php-project\vendor\aura\sqlschema\src\MysqlSchema.php on line 121
PHP Deprecated:  strtoupper(): Passing null to parameter #1 ($string) of type string is deprecated in C:\SoftwareProjects\test-php-project\vendor\aura\sqlschema\src\MysqlSchema.php on line 121

Deprecated: strtoupper(): Passing null to parameter #1 ($string) of type string is deprecated in C:\SoftwareProjects\test-php-project\vendor\aura\sqlschema\src\MysqlSchema.php on line 121
PHP Deprecated:  strtoupper(): Passing null to parameter #1 ($string) of type string is deprecated in C:\SoftwareProjects\test-php-project\vendor\aura\sqlschema\src\MysqlSchema.php on line 121

Deprecated: strtoupper(): Passing null to parameter #1 ($string) of type string is deprecated in C:\SoftwareProjects\test-php-project\vendor\aura\sqlschema\src\MysqlSchema.php on line 121
PHP Deprecated:  strtoupper(): Passing null to parameter #1 ($string) of type string is deprecated in C:\SoftwareProjects\test-php-project\vendor\aura\sqlschema\src\MysqlSchema.php on line 121

Deprecated: strtoupper(): Passing null to parameter #1 ($string) of type string is deprecated in C:\SoftwareProjects\test-php-project\vendor\aura\sqlschema\src\MysqlSchema.php on line 121
PHP Deprecated:  strtoupper(): Passing null to parameter #1 ($string) of type string is deprecated in C:\SoftwareProjects\test-php-project\vendor\aura\sqlschema\src\MysqlSchema.php on line 121

Deprecated: strtoupper(): Passing null to parameter #1 ($string) of type string is deprecated in C:\SoftwareProjects\test-php-project\vendor\aura\sqlschema\src\MysqlSchema.php on line 121
PHP Deprecated:  strtoupper(): Passing null to parameter #1 ($string) of type string is deprecated in C:\SoftwareProjects\test-php-project\vendor\aura\sqlschema\src\MysqlSchema.php on line 121

Deprecated: strtoupper(): Passing null to parameter #1 ($string) of type string is deprecated in C:\SoftwareProjects\test-php-project\vendor\aura\sqlschema\src\MysqlSchema.php on line 121

So I decided to fork this project and created a new branch rotexsoft/Aura.SqlSchema@2.x...3.0.1 in my fork that makes the code compatible with PHP 8.1 & also made it support mariadb & Postgres (which doesn't work on the latest version 2.0.3). Details of the changes I made can be found here https://github.com/rotexdegba/Aura.SqlSchema/releases

If accepted, these changes should be merged into a 3.x branch and a new 3.0.0 major release should be created.

Looking forward to hearing back from you.

If I don't hear back by the end of this week (Sunday Feb 11, 2024), I will just go ahead to create a new package on packagist from my fork & just adjust the namespaces, because I really need these changes in an application that I will be updating to PHP 8.1 in production. I have tried to look for an email address for Paul Jones or Hari K T but couldn't find any and I could not post to the Google group it seems to have posting disabled or maybe because I just joined the group.

Thanks.

Fetch Microsoft SQL Server table by dot notation

Being able to fetch a table by dot notation.

I am using sqlsrv.

Sometimes my connection is set to use ex. table "global.dbo.some_table" and when it is set to this, which is perfectly valid, it unfortunately fails to retrieve the colums.

What is the future of this project?

What is the future of this project? In my opinion, it is feature complete and only needs to have the latest versions of PHP (7.3, 7.4 & 8.0 as of this writing) tested via travis or Github Actions to ensure it continues to work with the currently supported version of PHP. Will anyone be updating the CI tests for this project?

Missing table reflection

Table reflection is missing - we need a table model to reflect things like comments on tables, storage types, and whatever other table metadata each storage engine might provide.

This will be a BC break, since tables are currently reflected as strings, so there is no existing table model we can flesh out with these properties.

on renaming

Hi guys,

This is regarding the renaming discussion happening at

https://groups.google.com/d/msg/auraphp/JNnklDBaGnU/kKG2MoC8Jm0J

I noticed @cordoval 's work on https://github.com/cordoval/random-packagist-test/blob/master/composer.json#L3 .

So I feel it is good to have a composer.json in Aura.Sql_Schema with

{
    "name": "aura/sql-query",
    "replace": {
        "aura/sqlquery": "no-sure on version"
    }
}

So the new package aura/sqlquery can be found by the old one aura/sql-query .
If you guys have any insights on this it will be great help. / cc @simensen

Thank you.

Doc is wrong about the constructor

The doc affirms:

$schema = new MysqlSchema($pdo);

while in the source the __constructor is:

public function __construct(PDO $pdo, ColumnFactory $column_factory)

so it will always be an error, since no ColumnFactory is provided.
The error is:

PHP Catchable fatal error:  Argument 2 passed to Aura\Sql_Schema\AbstractSchema::__construct() must be an instance of Aura\Sql_Schema\ColumnFactory, none given, called in /home/fale/sql2md/src/sql2json.php on line 17 and defined in /home/fale/sql2md/vendor/aura/sql-schema/src/AbstractSchema.php on line 69

Automatic db type discovery

Add functionality to discover the database type (from the connection string for example) and use the apropriate methods to list tables, fields, indexes automatically - so there is no need to create driver-specific classes. Thanks.

Fatal error under 5.3

Composer.json specifies 5.3 compatibility, but a fatal error is thrown under 5.3:

Fatal error: Can't inherit abstract function Aura\SqlSchema\SchemaInterface::fetchTableList() (previously declared abstract in Aura\SqlSchema\AbstractSchema) in C:\...\sqlschema\src\AbstractSchema.php on line 23

I'm going to suggest removing the abstract methods in AbstractSchema - the same methods (and documentation) are already defined in SchemaInterface, so they are redundant to begin with. The abstract class isn't required to complete the interface in the first place, and doesn't complete it by adding abstract methods anyway, since abstract methods are basically just an anonymous interface embedded in an abstract class...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.