1. Go to this page and download the library: Download callismart/dbprism 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/ */
callismart / dbprism example snippets
use Callismart\DBPrism\Database;
use Callismart\DBPrism\Adapters\MysqliAdapter;
use Callismart\DBPrism\DBConfigDTO;
$config = new DBConfigDTO([
'host' => 'localhost',
'username' => 'root',
'password' => 'secret',
'dbname' => 'myapp',
'driver' => 'mysql',
]);
$adapter = new MysqliAdapter($config);
$db = new Database($adapter);
use Callismart\DBPrism\Utils\Column;
use Callismart\DBPrism\Utils\ColumnType;
use Callismart\DBPrism\Utils\Constraint;
$builder = new SQLBuilder($db->get_driver());
$intent = $builder->create_table('users')
->add_columns([
Column::make('id')
->type(ColumnType::BIG_INT)
->auto_increment()
->unsigned()
-> ->b->exec($sql);
use Callismart\DBPrism\Migrations\Helpers\TableHelper;
$helper = new TableHelper($db, new SQLBuilder($db->get_driver()), 'users');
// Rename table
$helper->rename('new_table_name');
// Truncate table
$helper->truncate(restart: true, cascade: false);
// Drop table
$helper->drop(exists_check: true);
// Drop index
$helper->drop_index('idx_created_at');
// Access column operations
$helper->column();
// Access constraint operations
$helper->constraint();
use Callismart\DBPrism\Migrations\Helpers\ColumnHelper;
$helper = new ColumnHelper($db, new SQLBuilder($db->get_driver()), 'users');
// Add column
$helper->add(Column::make('phone')
->type(ColumnType::VARCHAR)
->size(20)
->nullable()
);
// Drop column
$helper->drop('deprecated_field');
// Rename column
$helper->rename('old_name', 'new_name');
// Modify column
$helper->modify(Column::make('name')
->type(ColumnType::VARCHAR)
->size(255)
);
// Change column type
$helper->changeType('status', ColumnType::ENUM);
// Check operations
$helper->exists('email'); // bool
$helper->getType('email'); // string|null
$helper->list(); // array of column names
use Callismart\DBPrism\Inspection\Inspector;
$inspector = new Inspector($db);
// List all tables
$tables = $inspector->get_all_tables();
// Check if table exists
if ($inspector->table_exists('users')) {
echo "Table exists!";
}
// Get table metadata
$meta = $inspector->get_table_metadata('users');
// Returns: ['engine' => 'InnoDB', 'charset' => 'utf8mb4', 'collation' => '...', 'row_count' => 1250, 'comment' => '']
// Get all column names
$columns = $inspector->get_columns('users');
// Check if column exists
$inspector->column_exists('users', 'email');
// Get column type (normalized)
$type = $inspector->get_column_type('users', 'id');
// Get detailed column information
$details = $inspector->get_column_details('users');
// Check if column is nullable
$inspector->is_column_nullable('users', 'email');
// Get column default
$inspector->get_column_default('users', 'is_active');
// Get all indexes
$indexes = $inspector->get_indexes('users');
// Check if index exists
$inspector->has_index('users', 'idx_created_at');
// Get primary key
$pk = $inspector->get_primary_key('users');
// Returns: ['id'] or null
// Get all foreign keys
$fks = $inspector->get_foreign_keys('orders');
// Check if foreign key exists
$inspector->has_foreign_key('orders', 'fk_orders_user');
// Get unique constraints
$unique = $inspector->get_unique_constraints('users');
// Get check constraints
$checks = $inspector->get_check_constraints('products');
// Get database engine type
$engine = $inspector->get_engine_type(); // 'mysql', 'pgsql', 'sqlite'
// Get server version
$version = $inspector->get_server_version();
// Get protocol version
$protocol = $inspector->get_protocol_version();
// Get host info
$host = $inspector->get_host_info();
// ✓ Good
$user = $db->get_row('SELECT * FROM users WHERE id = ?', [$id]);
// ✗ Bad - SQL injection vulnerability
$user = $db->get_row("SELECT * FROM users WHERE id = {$id}");
$db->transactional(function() use ($db) {
$order_id = $db->insert('orders', $order_data);
foreach ($items as $item) {
$db->insert('order_items', [...$item, 'order_id' => $order_id]);
}
});
$inspector = new Inspector($db);
if ($inspector->table_exists('users') &&
$inspector->column_exists('users', 'email')) {
// Safe to query
}