Queasy DB: Execute queries by accessing class variables

Recommend this page to a friend!
  Info   View files Documentation   View files View files (29)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2021-04-16 (26 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 109 This week: 67All time: 9,365 This week: 1Up
Version License PHP version Categories
queasy-db 1.0.0Custom (specified...5PHP 5, Databases
Description Author

This package can be used to execute queries by accessing class variables.

It can connect to a given database using the PDO extension.

The package provides classes that have getter functions that when they are accessed they can execute queries to retrieve values from table records.

The classes also have functions to execute SQL INSERT, UPDATE and DELETE queries from given parameter values.

Innovation Award
PHP Programming Innovation award nominee
April 2021
Nominee
Vote
Many applications need to perform database queries to store and retrieve the information that they need to execute their tasks.

The traditional solutions to perform database queries require that developers assemble the SQL query strings combining any parameter values that may be necessary to compose the final SQL query strings.

This package provides a much simpler solutions that just requires accessing class variables to assemble and run the SQL queries that need to be executed.

Manuel Lemos
Picture of Vitaly
  Performance   Level  
Name: Vitaly <contact>
Classes: 2 packages by
Country: Ukraine Ukraine
Age: ???
All time rank: 424475 in Ukraine Ukraine
Week rank: 2 Up1 in Ukraine Ukraine Up
Innovation award
Innovation award
Nominee: 1x

 

Details

Codacy Badge Build Status codecov Total Downloads License

Queasy PHP Framework - Database

Package v-dem/queasy-db

Database access classes. Some the most usual queries can be built automatically, more complex queries can be added into database and/or tables config.

Features

Requirements

  • PHP version 5.3 or higher
  • Package `v-dem/queasy-helper`
  • Package `v-dem/queasy-config` (required for dev only)
  • Package `v-dem/queasy-log` (required for dev only)

Documentation

See our Wiki page.

Installation

composer require v-dem/queasy-db:master-dev

Usage

Notes

  • `queasy\db\Db` class inherits `PDO` class, so any `PDO` methods can be called with it
  • You can use `setLogger()` method which accepts `Psr\Log\LoggerInterface` to log all queries

Initialization

Sample:

$db = new queasy\db\Db(
    [
        'connection' => [
            'driver' => 'mysql',
            'host' => 'localhost',
            'name' => 'test',
            'user' => 'test_user',
            'password' => 'test_password'
        ],
        'fetchMode' => PDO::FETCH_ASSOC // Default fetch mode for all queries
    ]
);

Getting a single record from users table by id key

$user = $db->users->id[$userId];

It will generate the following query:

SELECT  *
FROM    `users`
WHERE   `id` = :id

Inserting a record into users table using associative array

$db->users[] = [
    'email' => 'john.doe@example.com',
    'password_hash' => sha1('myverystrongpassword')
];

It will generate the following query:

INSERT  INTO `users` (`email`, `password_hash`)
VALUES  (:email, :password_hash)

Inserting a record into users table by order (not recommended, keep in mind fields order)

$db->users[] = [
    'john.doe@example.com',
    sha1('myverystrongpassword')
];

Inserting many records into users table using associative array (it will generate single INSERT statement)

$db->users[] = [
    [
        'email' => 'john.doe@example.com',
        'password_hash' => sha1('myverystrongpassword')
    ], [
        'email' => 'mary.joe@example.com',
        'password_hash' => sha1('herverystrongpassword')
    ]
];

Inserting many records into users table by order (not recommended; it will generate single INSERT statement)

$db->users[] = [
    [
        'john.doe@example.com',
        sha1('myverystrongpassword')
    ], [
        'mary.joe@example.com',
        sha1('herverystrongpassword')
    ]
];

Inserting many records into users table with field names denoted separately (it will generate single INSERT statement)

$db->users[] = [
    [
        'email',
        'password_hash'
    ], [
        [
            'john.doe@example.com',
            sha1('myverystrongpassword')
        ], [
            'mary.joe@example.com',
            sha1('herverystrongpassword')
        ]
    ]
];

Getting last insert id (alias of lastInsertId() method)

$newUserId = $db->id();

Updating a record in users table by id key

$db->users->id[$userId] = [
    'password_hash' => sha1('mynewverystrongpassword')
]

Deleting a record in users table by id key

unset($db->users->id[$userId]);

Get count of all records in users table (I know this function is not very useful)

$usersCount = count($db->users);

Using transactions

$db->trans(function() use($db) {
    // Run queries inside a transaction
});

Using foreach with a users table (obviously it will get all table records first)

foreach($db->users as $user) {
    // Do something
}

Run custom query (returns PDOStatement)

$result = $db->run('
    SELECT  *
    FROM    `users`
    WHERE   `name` LIKE concat(\'%\', :searchName, \'%\')',
    [
        ':searchName' => $searchName
    ]
);

  • Possible 3rd argument is `$driverOptions` which will be passed to `PDO::prepare()`
  Files folder image Files  
File Role Description
Files folder imagesrc (5 files, 1 directory)
Files folder imagetests (1 directory)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file codecov.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imagequery (14 files)
  Plain text file Connection.php Class Class source
  Plain text file Db.php Class Class source
  Plain text file DbException.php Class Class source
  Plain text file Field.php Class Class source
  Plain text file Table.php Class Class source

  Files folder image Files  /  src  /  query  
File Role Description
  Plain text file AbstractQuery.php Class Class source
  Plain text file BatchInsertQuery.php Class Class source
  Plain text file BatchNamedInsertQuery.php Class Class source
  Plain text file BatchSeparatelyNamedInsertQuery.php Class Class source
  Plain text file CountQuery.php Class Class source
  Plain text file CustomQuery.php Class Class source
  Plain text file DeleteQuery.php Class Class source
  Plain text file Query.php Class Class source
  Plain text file QueryInterface.php Class Class source
  Plain text file SelectQuery.php Class Class source
  Plain text file SingleInsertQuery.php Class Class source
  Plain text file SingleNamedInsertQuery.php Class Class source
  Plain text file TableQuery.php Class Class source
  Plain text file UpdateQuery.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imagesrc (4 files)

  Files folder image Files  /  tests  /  src  
File Role Description
  Plain text file ConnectionTest.php Class Class source
  Plain text file DbTest.php Class Class source
  Plain text file FieldTest.php Class Class source
  Plain text file TableTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:109
This week:67
All time:9,365
This week:1Up

For more information send a message to info at phpclasses dot org.