Skip to content

Commit

Permalink
Merge branch 'load-extension' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
wsporto committed Sep 12, 2024
2 parents e609751 + 2033aa0 commit c4473c7
Show file tree
Hide file tree
Showing 9 changed files with 960 additions and 717 deletions.
118 changes: 99 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typesql-cli",
"version": "0.10.6",
"version": "0.10.7",
"description": "",
"main": "index.js",
"bin": {
Expand Down Expand Up @@ -39,7 +39,7 @@
"code-block-writer": "^13.0.1",
"fp-ts": "^2.16.8",
"glob": "^10.4.5",
"libsql": "^0.3.19",
"libsql": "^0.4.4",
"moment": "^2.30.1",
"mysql2": "^3.10.3",
"yargs": "^17.7.2"
Expand Down
10 changes: 5 additions & 5 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ async function main() {
}

async function compile(watch: boolean, config: TypeSqlConfig) {
const { sqlDir, databaseUri, client: dialect, attach, authToken } = config;
const { sqlDir, databaseUri, client: dialect, attach, loadExtensions, authToken } = config;
validateDirectories(sqlDir);

const databaseClientResult = await createClient(databaseUri, dialect, attach, authToken);
const databaseClientResult = await createClient(databaseUri, dialect, attach, loadExtensions, authToken);
if (isLeft(databaseClientResult)) {
console.error(`Error: ${databaseClientResult.left.description}.`);
return;
Expand Down Expand Up @@ -285,15 +285,15 @@ async function selectAllTables(client: DatabaseClient): Promise<Either<string, T
return selectTablesResult;
}

async function createClient(databaseUri: string, dialect: TypeSqlDialect, attach?: string[], authToken?: string) {
async function createClient(databaseUri: string, dialect: TypeSqlDialect, attach?: string[], loadExtensions?: string[], authToken?: string) {
switch (dialect) {
case 'mysql2':
return createMysqlClient(databaseUri);
case 'better-sqlite3':
case 'bun:sqlite':
return createSqliteClient(dialect, databaseUri, attach || []);
return createSqliteClient(dialect, databaseUri, attach || [], loadExtensions || []);
case 'libsql':
return createLibSqlClient(databaseUri, attach || [], authToken || '');
return createLibSqlClient(databaseUri, attach || [], loadExtensions || [], authToken || '');
}
}
async function loadSchema(databaseClient: DatabaseClient): Promise<Either<TypeSqlError, ColumnSchema[]>> {
Expand Down
5 changes: 4 additions & 1 deletion src/drivers/libsql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type Either, right } from 'fp-ts/lib/Either';
import type { DatabaseClient, TypeSqlError } from '../types';
import Database from 'libsql';

export function createLibSqlClient(url: string, attachList: string[], authToken: string): Either<TypeSqlError, DatabaseClient> {
export function createLibSqlClient(url: string, attachList: string[], loadExtensions: string[], authToken: string): Either<TypeSqlError, DatabaseClient> {
const opts = {
authToken: authToken
} as any;
Expand All @@ -11,6 +11,9 @@ export function createLibSqlClient(url: string, attachList: string[], authToken:
for (const attach of attachList) {
db.exec(`attach database ${attach}`);
}
for (const extension of loadExtensions) {
db.loadExtension(extension);
}

return right({
type: 'libsql',
Expand Down
14 changes: 14 additions & 0 deletions src/mysql-query-analyzer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,17 @@ export type DeleteInfoResult = {
kind: 'Delete';
parameters: ParameterDef[];
};

export type ExtensionFunctionCatalog = {
[functionName: string]: ExtensionFunction
}

export type ExtensionFunction = {
paramsTypes: FunctionType[],
returnType: FunctionType
}

export type FunctionType = {
type: SQLiteType;
notNull: boolean;
}
5 changes: 4 additions & 1 deletion src/sqlite-query-analyzer/query-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import Database, { type Database as DatabaseType } from 'better-sqlite3';
import type { Database as LibSqlDatabase } from 'libsql';
import type { SQLiteType } from './types';

export function createSqliteClient(client: 'better-sqlite3' | 'bun:sqlite', databaseUri: string, attachList: string[]): Either<TypeSqlError, DatabaseClient> {
export function createSqliteClient(client: 'better-sqlite3' | 'bun:sqlite', databaseUri: string, attachList: string[], loadExtensions: string[]): Either<TypeSqlError, DatabaseClient> {
const db = new Database(databaseUri);
for (const attach of attachList) {
db.exec(`attach database ${attach}`);
}
for (const extension of loadExtensions) {
db.loadExtension(extension);
}
return right({
type: client,
client: db
Expand Down
Loading

0 comments on commit c4473c7

Please sign in to comment.