PHP code example of progrmanial / simplemdb

1. Go to this page and download the library: Download progrmanial/simplemdb 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/ */

    

progrmanial / simplemdb example snippets



SimpleMDB\DatabaseFactory;
use SimpleMDB\SchemaBuilder;

// Connect to MySQL database
$db = DatabaseFactory::create('pdo', 'localhost', 'root', 'password', 'myapp');

// Create modern table with enterprise features
$schema = new SchemaBuilder($db);
$schema->increments('id')                           // Auto-increment primary key
       ->string('name', 100)->comment('Full name')  // VARCHAR with comment
       ->string('email', 150)->unique()             // Unique email
       ->boolean('is_active')->default(true)        // Boolean with default
       ->json('preferences')->nullable()            // JSON data storage
       ->ipAddress('last_login_ip')->nullable()     // IPv4/IPv6 address
       ->timestamps()                               // created_at, updated_at
       ->createTable('users');

echo "✅ Modern users table created!\n";


SimpleMDB\DatabaseFactory;
use SimpleMDB\SchemaBuilder_PostgreSQL;

// Connect to PostgreSQL database
$db = DatabaseFactory::create('postgresql', 'localhost', 'postgres', 'password', 'myapp');

// Create modern table with PostgreSQL-specific features
$schema = new SchemaBuilder_PostgreSQL($db);
$schema->increments('id')                           // SERIAL PRIMARY KEY
       ->string('name', 100)->comment('Full name')  // VARCHAR with comment
       ->string('email', 150)->unique()             // Unique email
       ->boolean('is_active')->default(true)        // Boolean with default
       ->jsonb('preferences')->nullable()           // JSONB data storage (PostgreSQL)
       ->inet('last_login_ip')->nullable()          // INET address type (PostgreSQL)
       ->uuidWithDefault('external_id')             // UUID with gen_random_uuid()
       ->textArray('tags')->nullable()              // TEXT[] array (PostgreSQL)
       ->timestamps()                               // created_at, updated_at
       ->createTable('users');

echo "✅ Modern PostgreSQL users table created!\n";

// Familiar fluent syntax
$users = SimpleQuery::create()
    ->select(['id', 'name', 'email'])
    ->from('users')
    ->where('is_active = ?', [true])
    ->orderBy('created_at DESC')
    ->execute($db);

$table->uuid('external_id')                // UUID storage
      ->ipAddress('client_ip')             // IPv4/IPv6 (45 chars)
      ->json('metadata')                   // JSON documents
      ->point('location')                  // Geographic coordinates
      ->morphs('commentable');             // Polymorphic relationships

// Auto-generates context-aware templates
$migrations->create('create_blog_posts_table');
// ✨ Detects "blog posts" and generates complete table structure

// MySQL Connection
$mysql = DatabaseFactory::create('pdo', 'localhost', 'root', 'password', 'myapp');

// PostgreSQL Connection  
$pgsql = DatabaseFactory::create('postgresql', 'localhost', 'postgres', 'password', 'myapp');

// Same API, different engines!
$mysql->write_data('users', ['name' => 'John', 'email' => '[email protected]']);
$pgsql->write_data('users', ['name' => 'Jane', 'email' => '[email protected]']);

$schema->createTable('products', function($table) {
    $table->increments('id');
    $table->string('sku', 50)->unique();
    $table->string('name', 200);
    $table->decimal('price', 10, 2)->unsigned();
    $table->json('attributes')->nullable();             // Color, size, etc.
    $table->enum('status', ['draft', 'published'])->default('draft');
    $table->ipAddress('created_from_ip');
    $table->timestamps();
    
    $table->index(['status', 'price']);
});

$schema->createTable('users', function($table) {
    $table->increments('id');
    $table->string('email')->unique();
    $table->string('password');
    $table->json('preferences')->nullable();
    $table->ipAddress('last_login_ip')->nullable();
    $table->timestamp('email_verified_at')->nullable();
    $table->rememberToken();
    $table->timestamps();
    $table->softDeletes();
});

use SimpleMDB\Backup\BackupManager;

$backupManager = new BackupManager($db, 'backups/');

// Memory-efficient encrypted backup
$backup = $backupManager
    ->backup('daily_backup')
    ->streaming(1000)              // Process in chunks
    ->encrypted($encryptionKey)     // AES-256 encryption
    ->compress('gzip')             // Space efficient
    ->execute();

use SimpleMDB\DatabaseObjects\DatabaseObjectManager;

$objects = new DatabaseObjectManager($db);

// Create a function
$objects->function('calculate_total')
    ->inParameter('amount', 'DECIMAL(10,2)')
    ->returns('DECIMAL(10,2)')
    ->body("RETURN amount * 1.1;")
    ->create();

// Create a view with complex logic
$objects->view('user_summary')
    ->select("
        u.id, u.name, u.email,
        COUNT(o.id) as order_count,
        SUM(o.total) as total_spent
    FROM users u
    LEFT JOIN orders o ON u.id = o.user_id
    GROUP BY u.id
    ")
    ->create();

// Create a trigger for auditing
$objects->trigger('audit_changes')
    ->after()
    ->update()
    ->on('users')
    ->body("INSERT INTO audit_log (table_name, action, record_id) VALUES ('users', 'UPDATE', NEW.id);")
    ->create();


SimpleMDB\DatabaseFactory;

try {
    $db = DatabaseFactory::create('pdo', 'localhost', 'root', 'password', 'test');
    echo "✅ SimpleMDB installed successfully!\n";
} catch (Exception $e) {
    echo "❌ Installation issue: " . $e->getMessage() . "\n";
}

// Column types
$table->string('name', 100)           // VARCHAR
$table->integer('count')              // INT
$table->boolean('active')             // TINYINT(1)
$table->json('data')                  // JSON
$table->timestamp('created_at')       // TIMESTAMP
$table->ipAddress('ip')               // VARCHAR(45)

// Modifiers
$table->nullable()                    // Allow NULL
$table->default('value')              // Default value
$table->unique()                      // Unique constraint
$table->comment('Description')        // Column comment

// Indexes
$table->index(['column'])             // Regular index
$table->unique(['email'])             // Unique index
$table->foreignKey('user_id', 'users', 'id')  // Foreign key

// SELECT
$users = SimpleQuery::create()
    ->select(['id', 'name', 'email'])
    ->from('users')
    ->where('active = ?', [true])
    ->execute($db);

// INSERT
$result = SimpleQuery::create()
    ->insert(['name' => 'John', 'email' => '[email protected]'])
    ->into('users')
    ->execute($db);

// UPDATE
$result = SimpleQuery::create()
    ->update('users')
    ->set(['name' => 'Jane'])
    ->where('id = ?', [1])
    ->execute($db);
bash
git clone https://github.com/imrnansaadullah/SimpleMDB.git
cd SimpleMDB
composer install
php examples/quick_start_example.php  # Test your setup