curl -X POST http://localhost:3000/api/query \
-H "Content-Type: application/json" \
-d '{
"host": "localhost",
"port": 5432,
"user": "postgres",
"password": "mypassword",
"database": "mydb",
"sql": "SELECT id, name, email FROM users LIMIT 10;",
"sslMode": "disable"
}'
{
"rows": [
{ "id": 1, "name": "Alice", "email": "alice@example.com" },
{ "id": 2, "name": "Bob", "email": "bob@example.com" }
],
"fields": [
{ "name": "id", "dataTypeID": 23 },
{ "name": "name", "dataTypeID": 1043 },
{ "name": "email", "dataTypeID": 1043 }
],
"rowCount": 2,
"command": "SELECT",
"executionTime": 12,
"statement": "SELECT id, name, email FROM users LIMIT 10;"
}
Execute SQL queries with automatic transaction handling
curl -X POST http://localhost:3000/api/query \
-H "Content-Type: application/json" \
-d '{
"host": "localhost",
"port": 5432,
"user": "postgres",
"password": "mypassword",
"database": "mydb",
"sql": "SELECT id, name, email FROM users LIMIT 10;",
"sslMode": "disable"
}'
{
"rows": [
{ "id": 1, "name": "Alice", "email": "alice@example.com" },
{ "id": 2, "name": "Bob", "email": "bob@example.com" }
],
"fields": [
{ "name": "id", "dataTypeID": 23 },
{ "name": "name", "dataTypeID": 1043 },
{ "name": "email", "dataTypeID": 1043 }
],
"rowCount": 2,
"command": "SELECT",
"executionTime": 12,
"statement": "SELECT id, name, email FROM users LIMIT 10;"
}
disable, require, prefer)true for multi-statement queriesSELECT * FROM users WHERE id = 1;
SELECT COUNT(*) FROM users;
SELECT COUNT(*) FROM orders;
SELECT COUNT(*) FROM products;
INSERT INTO users (name, email) VALUES ('John', 'john@example.com');
UPDATE users SET active = true WHERE name = 'John';
SELECT * FROM users WHERE id = 1;
UPDATE users SET last_login = NOW() WHERE id = 1;
curl -X POST http://localhost:3000/api/query \
-H "Content-Type: application/json" \
-d '{
"host": "localhost",
"port": 5432,
"user": "postgres",
"password": "mypassword",
"database": "mydb",
"sql": "SELECT id, name, email FROM users LIMIT 10;",
"sslMode": "disable"
}'
{
"rows": [
{ "id": 1, "name": "Alice", "email": "alice@example.com" },
{ "id": 2, "name": "Bob", "email": "bob@example.com" }
],
"fields": [
{ "name": "id", "dataTypeID": 23 },
{ "name": "name", "dataTypeID": 1043 },
{ "name": "email", "dataTypeID": 1043 }
],
"rowCount": 2,
"command": "SELECT",
"executionTime": 12,
"statement": "SELECT id, name, email FROM users LIMIT 10;"
}
curl -X POST http://localhost:3000/api/query \
-H "Content-Type: application/json" \
-d '{
"host": "localhost",
"port": 5432,
"user": "postgres",
"password": "mypassword",
"database": "mydb",
"sql": "SELECT COUNT(*) FROM users; SELECT COUNT(*) FROM orders;"
}'
{
"multipleResults": true,
"results": [
{
"rows": [{ "count": "150" }],
"fields": [{ "name": "count", "dataTypeID": 20 }],
"rowCount": 1,
"command": "SELECT",
"executionTime": 8,
"statement": "SELECT COUNT(*) FROM users;"
},
{
"rows": [{ "count": "450" }],
"fields": [{ "name": "count", "dataTypeID": 20 }],
"rowCount": 1,
"command": "SELECT",
"executionTime": 6,
"statement": "SELECT COUNT(*) FROM orders;"
}
],
"totalExecutionTime": 9
}
curl -X POST http://localhost:3000/api/query \
-H "Content-Type: application/json" \
-d '{
"host": "localhost",
"port": 5432,
"user": "postgres",
"password": "mypassword",
"database": "mydb",
"sql": "INSERT INTO users (name) VALUES ('\''Alice'\''); UPDATE users SET active = true WHERE name = '\''Alice'\'';"
}'
{
"multipleResults": true,
"transactionUsed": true,
"results": [
{
"rows": [],
"rowCount": 1,
"command": "INSERT",
"executionTime": 4,
"statement": "INSERT INTO users (name) VALUES ('Alice');"
},
{
"rows": [],
"rowCount": 1,
"command": "UPDATE",
"executionTime": 3,
"statement": "UPDATE users SET active = true WHERE name = 'Alice';"
}
],
"totalExecutionTime": 15
}
{
"error": {
"message": "relation \"nonexistent_table\" does not exist",
"code": "42P01",
"severity": "ERROR",
"routine": "parserOpenTable",
"executionTime": 5
}
}
{
"error": "Transaction failed at statement: UPDATE users SET invalid_column = 'value'... Error: column \"invalid_column\" does not exist"
}
{
"error": "Missing required fields"
}
{
"error": "No valid SQL statements found"
}
/app/api/query/route.ts:34-40:
// Parse the query into individual statements
const statements = QueryParser.parseQuery(sql)
if (statements.length === 0) {
return NextResponse.json({ error: 'No valid SQL statements found' }, { status: 400 })
}
/app/api/query/route.ts:5-13:
interface QueryExecutionResult {
rows?: any[]
fields?: any[]
rowCount?: number
command?: string
executionTime: number
statement: string
error?: string
}
error field if it fails:
{
"multipleResults": true,
"results": [
{ "rows": [...], "executionTime": 10 },
{ "error": "syntax error at or near 'FRON'", "executionTime": 2 }
],
"error": "One or more statements failed"
}
try {
await client.query('BEGIN')
// Execute statements...
await client.query('COMMIT')
} catch (error) {
await client.query('ROLLBACK')
throw error
}
statement_timeout for query limits.