Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const TypeScriptFormatter = lazy(
() => import("@/pages/tools/typescript-formatter")
);
const GraphQLFormatter = lazy(() => import("@/pages/tools/graphql-formatter"));
const SqlFormatter = lazy(() => import("@/pages/tools/sql-formatter"));
const TimeFormatter = lazy(() => import("@/pages/tools/time-formatter"));
const Base64Encoder = lazy(() => import("@/pages/tools/base64-encoder"));
const CborEncoder = lazy(() => import("@/pages/tools/cbor-encoder"));
Expand Down Expand Up @@ -132,6 +133,7 @@ function Router() {
component={TypeScriptFormatter}
/>
<Route path="/tools/graphql-formatter" component={GraphQLFormatter} />
<Route path="/tools/sql-formatter" component={SqlFormatter} />
<Route path="/tools/time-formatter" component={TimeFormatter} />

{/* Encoders */}
Expand Down
4 changes: 4 additions & 0 deletions client/src/data/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,10 @@ export const DEFAULT_GRAPHQL = `type User
type Comment{id:ID!content:String!author:User!post:Post!createdAt:String!}type UserProfile{bio:String avatar:String website:String location:String}input CreateUserInput{name:String!email:String!age:Int}input UpdateUserInput{name:String email:String age:Int}type Query{users:[User!]!user(id:ID!):User posts:[Post!]!post(id:ID!):Post searchPosts(query:String!):[Post!]!}type Mutation{createUser(input:CreateUserInput!):User!updateUser(id:ID!,input:UpdateUserInput!):User!deleteUser(id:ID!):Boolean!createPost(title:String!,content:String!,authorId:ID!):Post!}type Subscription{postAdded:Post!commentAdded(postId:ID!):Comment!}schema{query:Query mutation:Mutation subscription:Subscription}
`;

export const DEFAULT_SQL = `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 o.user_id = u.id and o.status = 'completed' where u.created_at >= '2024-01-01' and (u.country = 'US' or u.country = 'CA') and u.id not in (select user_id from banned_users) group by u.id, u.name, u.email having sum(o.total) > 100 order by total_spent desc limit 20;
insert into audit_log (event, created_at) values ('report_generated', current_timestamp);
`;

export const DEFAULT_URL_TO_JSON = `https://api.example.com/users?page=1&limit=10&sort=name&order=asc&filter=active&category=premium#section`;

export const DEFAULT_CSV_TO_JSON = `name,email,age,city
Expand Down
76 changes: 76 additions & 0 deletions client/src/data/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
FileJson,
Braces,
DatabaseBackup,
Database,

// Encoders / Security
KeyRound,
Expand Down Expand Up @@ -2295,6 +2296,81 @@ export const toolsData: ToolData = {
],
},
},
{
name: "SQL Formatter",
path: "/tools/sql-formatter",
icon: Database,
shortcut: "Ctrl+Shift+Alt+S",
metadata: {
title: "SQL Formatter - Format SQL Queries Online",
description:
"Format and beautify SQL queries for PostgreSQL, MySQL, SQL Server, Oracle, SQLite, BigQuery, Snowflake and more. Configure keyword case, indentation and layout.",
keywords: [
"SQL formatter",
"SQL beautifier",
"SQL pretty print",
"PostgreSQL formatter",
"MySQL formatter",
"T-SQL formatter",
],
},
explanations: {
notice: {
type: "info",
title: "SQL Origins",
items: [
{
label: "Created:",
text: "IBM, early 1970s, as SEQUEL by Donald Chamberlin and Raymond Boyce",
},
{
label: "Standardized:",
text: "ANSI in 1986, ISO in 1987, revised many times since (SQL:2023 is the latest)",
},
{
label: "Dialects:",
text: "Every database extends the standard, so pick your dialect for the best results",
},
],
},
sections: [
{
title: "Formatting Settings",
items: [
{
label: "Dialect:",
text: "Teaches the formatter dialect-specific syntax such as ::casts (PostgreSQL), [brackets] (SQL Server) or backticks (MySQL)",
},
{
label: "Keyword case:",
text: "UPPER keywords (SELECT, FROM) are the most common convention; preserve keeps your input as-is",
},
{
label: "Indent style:",
text: "Standard indents clauses; tabular styles align keywords in a column",
},
{
label: "Expression width:",
text: "Parenthesized expressions shorter than this stay on one line",
},
{
label: "Logical operators:",
text: "Put AND / OR at the start or the end of each line",
},
],
},
{
title: "Pro Tips",
items: [
"Format generated ORM queries from logs to understand what actually hits the database",
"Leading AND/OR makes it easy to comment out a single condition",
"Format before committing migrations to keep git diffs readable",
"If formatting fails, try a different dialect: vendor syntax may not parse as standard SQL",
],
},
],
},
},
{
name: "Time Formatter",
path: "/tools/time-formatter",
Expand Down
73 changes: 73 additions & 0 deletions client/src/lib/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1033,3 +1033,76 @@ export async function formatGraphQL(input: string): Promise<{
};
}
}

type SqlDialect =
| "sql"
| "bigquery"
| "clickhouse"
| "db2"
| "db2i"
| "duckdb"
| "hive"
| "mariadb"
| "mysql"
| "n1ql"
| "plsql"
| "postgresql"
| "redshift"
| "singlestoredb"
| "snowflake"
| "spark"
| "sqlite"
| "tidb"
| "transactsql"
| "trino";

type SqlCase = "preserve" | "upper" | "lower";

export interface SqlFormatOptions {
language: SqlDialect;
tabWidth: number;
useTabs: boolean;
keywordCase: SqlCase;
dataTypeCase: SqlCase;
functionCase: SqlCase;
indentStyle: "standard" | "tabularLeft" | "tabularRight";
logicalOperatorNewline: "before" | "after";
expressionWidth: number;
linesBetweenQueries: number;
denseOperators: boolean;
newlineBeforeSemicolon: boolean;
}

export const DEFAULT_SQL_OPTIONS: SqlFormatOptions = {
language: "sql",
tabWidth: 2,
useTabs: false,
keywordCase: "upper",
dataTypeCase: "upper",
functionCase: "upper",
indentStyle: "standard",
logicalOperatorNewline: "before",
expressionWidth: 50,
linesBetweenQueries: 1,
denseOperators: false,
newlineBeforeSemicolon: false,
};

export async function formatSQL(
input: string,
options: Partial<SqlFormatOptions> = {}
): Promise<{
formatted: string;
error?: string;
}> {
try {
const { format } = await import("sql-formatter");
const formatted = format(input, { ...DEFAULT_SQL_OPTIONS, ...options });
return { formatted };
} catch (error) {
return {
formatted: input,
error: `SQL formatting error: ${error instanceof Error ? error.message : "Unknown error"}`,
};
}
}
Loading
Loading