Skip to content

Commit

Permalink
add the rest of operation
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Oct 17, 2024
1 parent fefa54f commit 88cbcd6
Show file tree
Hide file tree
Showing 7 changed files with 327 additions and 167 deletions.
54 changes: 21 additions & 33 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ export interface QueryBuilderInternal {
selectRawValue?: string;

// General operation values, such as when renaming tables referencing the old and new name
originalValue?: string;
newValue?: string;
newTableName?: string;
}

function buildWhereClause(args: unknown[]): WhereCondition | WhereClaues {
Expand Down Expand Up @@ -329,9 +328,21 @@ class QueryBuilderAlterTable extends IQueryBuilder {
this.state.table = tableName;
}

alterColumn(columnName: string, definition: TableColumnDefinition) {
this.state.action = QueryBuilderAction.UPDATE_COLUMNS;
this.state.columns.push({ name: columnName, definition });
return this;
}

addColumn(name: string, definition: TableColumnDefinition) {
this.state.action = QueryBuilderAction.ADD_COLUMNS;
this.state.columns.push({ name, definition });
return this;
}

renameTable(newTableName: string) {
this.state.originalValue = this.state.table;
this.state.newValue = newTableName;
this.state.action = QueryBuilderAction.RENAME_TABLE;
this.state.newTableName = newTableName;
return this;
}

Expand Down Expand Up @@ -439,37 +450,14 @@ function buildQueryString(
return dialect.createTable(queryBuilder);
case QueryBuilderAction.DELETE_TABLE:
return dialect.dropTable(queryBuilder);
// case QueryBuilderAction.RENAME_TABLE:
// query.query = dialect.renameTable(
// queryBuilder,
// queryType,
// query
// ).query;
// break;

// case QueryBuilderAction.ADD_COLUMNS:
// query.query = dialect.addColumn(
// queryBuilder,
// queryType,
// query
// ).query;
// break;
// case QueryBuilderAction.DROP_COLUMNS:
// query.query = dialect.dropColumn(
// queryBuilder,
// queryType,
// query
// ).query;
// break;
case QueryBuilderAction.RENAME_TABLE:
return dialect.renameTable(queryBuilder);
case QueryBuilderAction.ADD_COLUMNS:
return dialect.addColumn(queryBuilder);
case QueryBuilderAction.UPDATE_COLUMNS:
return dialect.alterColumn(queryBuilder);
case QueryBuilderAction.RENAME_COLUMNS:
return dialect.renameColumn(queryBuilder);
// case QueryBuilderAction.UPDATE_COLUMNS:
// query.query = dialect.updateColumn(
// queryBuilder,
// queryType,
// query
// ).query;
// break;
default:
throw new Error('Invalid action');
}
Expand Down
79 changes: 78 additions & 1 deletion src/connections/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Query } from '../query';
import { Database, TableColumn } from '../models/database';
import {
Database,
TableColumn,
TableColumnDefinition,
} from '../models/database';
import { AbstractDialect } from 'src/query-builder';
import { Outerbase } from 'src/client';

Expand Down Expand Up @@ -88,6 +92,26 @@ export abstract class Connection {
columnName: string,
newColumnName: string
): Promise<QueryResult>;

abstract renameTable(
schemaName: string | undefined,
tableName: string,
newTableName: string
): Promise<QueryResult>;

abstract alterColumn(
schemaName: string | undefined,
tableName: string,
columnName: string,
defintion: TableColumnDefinition
): Promise<QueryResult>;

abstract addColumn(
schemaName: string | undefined,
tableName: string,
columnName: string,
defintion: TableColumnDefinition
): Promise<QueryResult>;
}

export abstract class SqlConnection extends Connection {
Expand Down Expand Up @@ -246,4 +270,57 @@ export abstract class SqlConnection extends Connection {
.toQuery()
);
}

async renameTable(
schemaName: string | undefined,
tableName: string,
newTableName: string
): Promise<QueryResult> {
const qb = Outerbase(this);

return await this.query(
qb
.alterTable(
schemaName ? `${schemaName}.${tableName}` : tableName
)
.renameTable(newTableName)
.toQuery()
);
}

async alterColumn(
schemaName: string | undefined,
tableName: string,
columnName: string,
defintion: TableColumnDefinition
): Promise<QueryResult> {
const qb = Outerbase(this);

return await this.query(
qb
.alterTable(
schemaName ? `${schemaName}.${tableName}` : tableName
)
.alterColumn(columnName, defintion)
.toQuery()
);
}

async addColumn(
schemaName: string | undefined,
tableName: string,
columnName: string,
defintion: TableColumnDefinition
): Promise<QueryResult> {
const qb = Outerbase(this);

return await this.query(
qb
.alterTable(
schemaName ? `${schemaName}.${tableName}` : tableName
)
.addColumn(columnName, defintion)
.toQuery()
);
}
}
24 changes: 24 additions & 0 deletions src/connections/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Database,
Table,
TableColumn,
TableColumnDefinition,
TableIndex,
TableIndexType,
} from '../models/database';
Expand Down Expand Up @@ -296,4 +297,27 @@ export class MongoDBConnection implements Connection {
[this.defaultDatabase]: tableList,
};
}

async addColumn(): Promise<QueryResult> {
// Do nothing, MongoDB does not have a schema
return createOkResult();
}

async alterColumn(): Promise<QueryResult> {
// Do nothing, MongoDB does not have a schema
return createOkResult();
}

async renameTable(
schemaName: string | undefined,
tableName: string,
newTableName: string
): Promise<QueryResult> {
await this.client
.db(schemaName ?? this.defaultDatabase)
.collection(tableName)
.rename(newTableName);

return createOkResult();
}
}
90 changes: 0 additions & 90 deletions src/connections/neon-http.bk.txt

This file was deleted.

95 changes: 55 additions & 40 deletions src/playground.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
import { createConnection } from 'mysql2';
import { Connection, MySQLConnection, QueryResult } from '.';

function log(result: QueryResult) {
console.log('Result');
console.table(result.data);

console.log('Headers');
console.table(result.headers);
}

async function run(db: Connection, sql: string) {
console.log('------------------------------');
console.log(`\x1b[32m${sql}\x1b[0m`);
console.log('------------------------------');

log(await db.raw(sql));
}

async function main() {
const db = new MySQLConnection(
createConnection({
host: 'localhost',
user: 'root',
password: '123456',
database: 'testing',
})
);

await run(db, 'SELECT 1 AS `a`, 2 AS `a`;');

await run(
db,
'SELECT * FROM students INNER JOIN teachers ON (students.teacher_id = teachers.id)'
);
}

main()
.then()
.finally(() => process.exit());
// import { createConnection } from 'mysql2';
// import { Connection, MySQLConnection, QueryResult } from '.';

// function log(result: QueryResult) {
// console.log('Result');
// console.table(result.data);

// console.log('Headers');
// console.table(result.headers);
// }

// async function run(db: Connection, sql: string) {
// console.log('------------------------------');
// console.log(`\x1b[32m${sql}\x1b[0m`);
// console.log('------------------------------');

// log(await db.raw(sql));
// }

// async function main() {
// const db = new MySQLConnection(
// createConnection({
// host: 'localhost',
// user: 'root',
// password: '123456',
// database: 'testing',
// })
// );

// await run(db, 'SELECT 1 AS `a`, 2 AS `a`;');

// await run(
// db,
// 'SELECT * FROM students INNER JOIN teachers ON (students.teacher_id = teachers.id)'
// );
// }

// main()
// .then()
// .finally(() => process.exit());

import duckDB from 'duckdb';

const client = new duckDB.Database('md:my_db', {
motherduck_token:
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImludmlzYWxAZ21haWwuY29tIiwic2Vzc2lvbiI6ImludmlzYWwuZ21haWwuY29tIiwicGF0IjoiVkdfZ1BmRXdaWjN5M29zY0VFemRLWElMVVJ4ZmxFdUpxbktZM3RkVjEtUSIsInVzZXJJZCI6ImVkZjQ4NjAyLTJlZmMtNGU0Ny04Y2VmLWNhNGU5NzQ3OTQ0MSIsImlzcyI6Im1kX3BhdCIsImlhdCI6MTcyOTEzMDcxMX0.ysqXODqC9BpMeOBeedjQW0y6GfiMdpOgHBy1OihUtKI',
});

client
.connect()
.prepare('SELECT 1;')
.all((err, res) => {
console.log(res);
process.exit();
});
Loading

0 comments on commit 88cbcd6

Please sign in to comment.