-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add alternative tables and show ddl sql of the table
- Loading branch information
Showing
4 changed files
with
181 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import Link from 'next/link' | ||
import { CardStackMinusIcon, DotFilledIcon } from '@radix-ui/react-icons' | ||
import { ChevronDownIcon, TableIcon } from 'lucide-react' | ||
|
||
import { fetchData } from '@/lib/clickhouse' | ||
import { Button } from '@/components/ui/button' | ||
import { | ||
DropdownMenu, | ||
DropdownMenuContent, | ||
DropdownMenuItem, | ||
DropdownMenuTrigger, | ||
} from '@/components/ui/dropdown-menu' | ||
|
||
interface AlternativeTablesProps { | ||
database: string | ||
table: string | ||
} | ||
|
||
export async function AlternativeTables({ | ||
database, | ||
table, | ||
}: AlternativeTablesProps) { | ||
let anotherTables: { name: string }[] = [] | ||
try { | ||
anotherTables = await fetchData( | ||
`SELECT name FROM system.tables WHERE database = {database: String}`, | ||
{ database } | ||
) | ||
} catch (error) { | ||
console.log(error) | ||
|
||
return null | ||
} | ||
|
||
return ( | ||
<DropdownMenu> | ||
<DropdownMenuTrigger asChild> | ||
<Button | ||
variant="outline" | ||
size="sm" | ||
className="text-muted-foreground group flex flex-row gap-2" | ||
> | ||
<CardStackMinusIcon className="h-3 w-3" /> | ||
{database} | ||
<ChevronDownIcon | ||
className="h-3 w-3 transition duration-300 group-data-[state=open]:rotate-180" | ||
aria-hidden="true" | ||
/> | ||
</Button> | ||
</DropdownMenuTrigger> | ||
|
||
<DropdownMenuContent> | ||
{anotherTables.map(({ name }) => ( | ||
<DropdownMenuItem key={name}> | ||
<Link | ||
href={`/tables/${database}/${name}`} | ||
className="flex flex-row items-center gap-2" | ||
> | ||
{name == table ? ( | ||
<DotFilledIcon className="h-3 w-3" /> | ||
) : ( | ||
<div className="h-3 w-3" /> | ||
)} | ||
<TableIcon className="h-3 w-3" /> | ||
{name} | ||
</Link> | ||
</DropdownMenuItem> | ||
))} | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { CodeIcon } from '@radix-ui/react-icons' | ||
|
||
import { fetchData } from '@/lib/clickhouse' | ||
import { cn, dedent } from '@/lib/utils' | ||
import { Button } from '@/components/ui/button' | ||
import { | ||
Dialog, | ||
DialogContent, | ||
DialogDescription, | ||
DialogHeader, | ||
DialogTitle, | ||
DialogTrigger, | ||
} from '@/components/ui/dialog' | ||
|
||
interface ShowSQLButtonProps { | ||
database: string | ||
table: string | ||
className?: string | ||
} | ||
|
||
export async function ShowDDL({ | ||
database, | ||
table, | ||
className, | ||
}: ShowSQLButtonProps) { | ||
let showCreateTable: { statement: string }[] = [] | ||
try { | ||
showCreateTable = await fetchData(`SHOW CREATE TABLE ${database}.${table}`) | ||
} catch (error) { | ||
console.log(error) | ||
|
||
return null | ||
} | ||
|
||
if (!showCreateTable?.length || !showCreateTable[0]?.statement) { | ||
return null | ||
} | ||
|
||
const sql = showCreateTable[0].statement | ||
|
||
return ( | ||
<Dialog> | ||
<DialogTrigger asChild> | ||
<Button | ||
variant="outline" | ||
size="sm" | ||
className={cn( | ||
'text-muted-foreground flex flex-row items-center gap-2', | ||
className | ||
)} | ||
aria-label="Show DDL" | ||
title="Show Table DDL Table Definition" | ||
> | ||
<CodeIcon className="h-3 w-3" /> | ||
Show DDL | ||
</Button> | ||
</DialogTrigger> | ||
<DialogContent className="max-w-max"> | ||
<DialogHeader> | ||
<DialogTitle>SQL DDL Code</DialogTitle> | ||
<DialogDescription> | ||
CREATE query used for creating this table | ||
</DialogDescription> | ||
</DialogHeader> | ||
<div className="w-fit overflow-auto"> | ||
<pre className="text-sm">{dedent(sql)}</pre> | ||
</div> | ||
</DialogContent> | ||
</Dialog> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters