PHP code example of clearsoft / easysql-sdk

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

    

clearsoft / easysql-sdk example snippets


use Clearsoft\EasySql\Laravel\Facades\EasySQL;

// Ask questions in natural language
$result = EasySQL::createQuery([
    'connector_id' => 'conn_abc123',
    'question'     => 'How many users registered this month?',
]);

// Generated SQL and query results
$sql  = $result['sql'];
$rows = $result['result'];

// List recent queries
$queries = EasySQL::listQueries(['page' => 1, 'per_page' => 10]);

// Manage database connectors
$connectors = EasySQL::listConnectors();
$connector  = EasySQL::getConnector('conn_abc123');

// Access specific named connections defined in config/easysql.php
$analyticsClient = EasySQL::connection('analytics');
$stats = $analyticsClient->dashboardStats();



learsoft\EasySQL\Api\Client;

$client = new Client([
    'base_url'     => 'https://api.easysql.net',
    'access_token' => 'your-access-token',
]);

// Authentication
$tokens = $client->refresh(['refresh_token' => $refreshToken]);
$user = $client->me();

// Managing Database Connectors
$client->createConnector([
    'name'   => 'Production MySQL',
    'type'   => 'mysql',
    'schema' => $schemaPayload, // from the schema-generation package
]);

$connectors = $client->listConnectors();

// Running Natural Language Queries
$query = $client->createQuery([
    'connector_id' => 'conn_abc123',
    'question'     => 'What were the top 5 selling products last week?',
]);

print_r($query['sql']);
print_r($query['result']);

use Clearsoft\EasySQL\Client\Client;
use Clearsoft\EasySQL\Client\Http\TokenStoreInterface;

$client = new Client([
    'base_url'      => 'https://api.easysql.net',
    'access_token'  => $accessToken,
    'refresh_token' => $refreshToken,
]);
$client->setTokenStore(new SessionTokenStore()); // implements TokenStoreInterface

use Clearsoft\EasySQL\Client\Http\EasySQLClient;
use Clearsoft\EasySQL\Client\Http\TokenStoreInterface;

$client = new EasySQLClient([
    'base_url'      => 'https://api.easysql.net',
    'access_token'  => $accessToken,
    'refresh_token' => $refreshToken,
]);

// Implement TokenStoreInterface to persist tokens in Redis, Session, or Database
class SessionTokenStore implements TokenStoreInterface
{
    public function load(): ?array
    {
        return $_SESSION['easysql_tokens'] ?? null;
    }

    public function save(string $accessToken, string $refreshToken): void
    {
        $_SESSION['easysql_tokens'] = [
            'access_token'  => $accessToken,
            'refresh_token' => $refreshToken,
        ];
    }

    public function clear(): void
    {
        unset($_SESSION['easysql_tokens']);
    }
}

$client->setTokenStore(new SessionTokenStore());

use Clearsoft\EasySQL\Client\Models\TokenResponse;

$tokens = TokenResponse::fromArray($client->refresh(['refresh_token' => $refreshToken]));
echo $tokens->access_token;

use Clearsoft\EasySQL\Connectors\MySQL\ConnectionConfig;
use Clearsoft\EasySQL\Connectors\MySQL\Connector as MySQLConnector;
use Clearsoft\EasySQL\SchemaGeneration\SchemaGenerator;

$connector = new MySQLConnector(new ConnectionConfig(
    host: '127.0.0.1',
    user: 'readonly',
    password: 'secret',
    database: 'myapp',
));
$connector->connect();

try {
    $raw = $connector->introspect();
    $payload = (new SchemaGenerator())->generate($raw);

    // Execute a generated SELECT locally
    $result = $connector->execute('SELECT * FROM users LIMIT 10');
} finally {
    $connector->close();
}

// Push the schema to the API
$client->syncConnector(['schema' => $payload], 'conn_abc123');

$client->getConnector('conn_abc123');              // was: getConnector() — broken URL
$client->updateConnector(['name' => 'X'], 'conn_abc123');
$client->syncConnector(['schema' => $payload], 'conn_abc123');
bash
php artisan vendor:publish --tag=easysql-config
bash
php samples/09-schema-generation.php   # no credentials needed