PHP code example of v-dem / queasy-db

1. Go to this page and download the library: Download v-dem/queasy-db library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

v-dem / queasy-db example snippets


$db = new queasy\db\Db(
    [
        'dsn' => 'pgsql:host=localhost;dbname=test',
        'user' => 'test_user',
        'password' => 'test_password',
        'options' => [
            ...options...
        ]
    ]
);

$db = new queasy\db\Db(
    [
        'connection' => [
            'dsn' => 'pgsql:host=localhost;dbname=test',
            'user' => 'test_user',
            'password' => 'test_password',
            'options' => [
                ...options...
            ]
        ]
    ]
);

$db = new queasy\db\Db('pgsql:host=localhost;dbname=test', 'test_user', 'test_password', $options);

$users = $db->users->all();

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

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

$users = $db->users->id->select($userId, $options);

$users = $db->users->id[[$userId1, $userId2]];

$db->users[] = [
    'email' => '[email protected]',
    'password_hash' => sha1('myverystrongpassword')
];

$db->users[] = [
    '[email protected]',
    sha1('myverystrongpassword')
];

$db->users[] = [
    [
        'email' => '[email protected]',
        'password_hash' => sha1('myverystrongpassword')
    ], [
        'email' => '[email protected]',
        'password_hash' => sha1('herverystrongpassword')
    ]
];

$db->users[] = [
    [
        '[email protected]',
        sha1('myverystrongpassword')
    ], [
        '[email protected]',
        sha1('herverystrongpassword')
    ]
];

$db->users[] = [
    [
        'email',
        'password_hash'
    ], [
        [
            '[email protected]',
            sha1('myverystrongpassword')
        ], [
            '[email protected]',
            sha1('herverystrongpassword')
        ]
    ]
];

$userId = $db->users->insert([
    'email' => '[email protected]',
    'password_hash' => sha1('myverystrongpassword')
], $options);

$insertedRowsCount = $db->users->insert([
    [
        'email' => '[email protected]',
        'password_hash' => sha1('myverystrongpassword')
    ], [
        'email' => '[email protected]',
        'password_hash' => sha1('herverystrongpassword')
    ]
], $options);

$newUserId = $db->id();

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

$updatedRowsCount = $db->users->id->update($userId, [
    'password_hash' => sha1('mynewverystrongpassword')
], $options);

$db->users->id[[$userId1, $userId2]] = [
    'is_blocked' => true
]

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

unset($db->users->id[[$userId1, $userId2]]);

$deletedRowsCount = $db->users->id->delete([[$userId1, $userId2]], $options);

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

$db->trans(function() use($db) {
    // Run queries inside a transaction, for example:
    $db->users[] = [
        '[email protected]',
        sha1('myverystrongpassword')
    ];
});

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

$db = new queasy\db\Db(
    [
        'connection' => [
            'dsn' => 'pgsql:host=localhost;dbname=test',
            'user' => 'test_user',
            'password' => 'test_password'
        ],
        'queries' => [
            'searchUsersByName' => [
                'sql' => '
                    SELECT  *
                    FROM    "users"
                    WHERE   "name" LIKE concat(\'%\', :searchName, \'%\')',
                'returns' => Db::RETURN_ALL
            ]
        ]
    ]
);

$users = $db->searchUsersByName([
    'searchName' => 'John'
]);

$db = new queasy\db\Db(
    [
        'connection' => [
            'dsn' => 'pgsql:host=localhost;dbname=test',
            'user' => 'test_user',
            'password' => 'test_password'
        ],
        'tables' => [
            'users' => [
                'searchByName' => [
                    'sql' => '
                        SELECT  *
                        FROM    "user_roles"
                        WHERE   "name" LIKE concat(\'%\', :searchName, \'%\')',
                    'returns' => Db::RETURN_ALL
                ]
            ]
        ]
    ]
);

$users = $db->users->searchByName([
    'searchName' => 'John'
]);

return [
    'db' => [
        'connection' => [
            'dsn' => 'pgsql:host=localhost;dbname=test',
            'user' => 'test_user',
            'password' => 'test_password'
        ],
        'tables' => [
            'users' => [
                'searchByName' => [
                    'sql' => '
                        SELECT  *
                        FROM    "users"
                        WHERE   "name" LIKE concat(\'%\', :searchName, \'%\')',
                    'returns' => Db::RETURN_ALL
                ]
            ]
        ]
    ],

    'logger' => [
        [
            'class' => queasy\log\ConsoleLogger::class,
            'minLevel' => Psr\Log\LogLevel::DEBUG
        ]
    ]
];

$config = new queasy\config\Config('config.php'); // Can be also INI, JSON or XML

$logger = new queasy\log\Logger($config->logger);

$db = new queasy\db\Db($config->db);
$db->setLogger($logger);

$users = $db->users->searchByName([
    'searchName' => 'John'
]);