Skip to content

Commit

Permalink
update index
Browse files Browse the repository at this point in the history
  • Loading branch information
LinboLen committed Jun 21, 2024
1 parent 1b334f7 commit a815622
Show file tree
Hide file tree
Showing 10 changed files with 783 additions and 707 deletions.
4 changes: 4 additions & 0 deletions libs/fedaco/src/query-builder/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ export class Processor implements ProcessorInterface {
processIndexes(results: any[]) {
return results;
}

processForeignKeys(results: any[]) {
return results;
}
}
14 changes: 14 additions & 0 deletions libs/fedaco/src/query-builder/processor/sqlite-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,18 @@ export class SqliteProcessor extends Processor {

return indexes;
}

public processForeignKeys(results: any[]) {
return results.map((result) => {
return {
'name' : null,
'columns' : result.columns.split(','),
'foreign_schema' : null,
'foreign_table' : result.foreign_table,
'foreign_columns': result.foreign_columns.split(','),
'on_update' : result.on_update.toLowerCase(),
'on_delete' : result.on_delete.toLowerCase(),
};
});
}
}
4 changes: 2 additions & 2 deletions libs/fedaco/src/schema/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ export class Blueprint {
}

/*Get the raw SQL statements for the blueprint.*/
public toSql(connection: Connection, grammar: SchemaGrammar) {
public async toSql(connection: Connection, grammar: SchemaGrammar) {
this.addImpliedCommands(grammar);
let statements: string[] = [];
this.ensureCommandsAreValid(connection);
for (const command of this.commands) {
const method = 'compile' + upperFirst(command.name);
if (method in grammar) {
// @ts-ignore
const sql = grammar[method](this, command, connection);
const sql = await grammar[method](this, command, connection);
if (isArray(sql) && sql.length > 0) {
statements = [...statements, ...sql];
} else if (isString(sql) && sql.length > 0) {
Expand Down
4 changes: 4 additions & 0 deletions libs/fedaco/src/schema/grammar/schema-grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export class SchemaGrammar extends BaseGrammar {
throw new Error('not implemented');
}

public compileForeignKeys(table: string): string {
throw new Error('not implemented');
}

public compileColumns(table: string): string {
throw new Error('not implemented');
}
Expand Down
30 changes: 28 additions & 2 deletions libs/fedaco/src/schema/grammar/sqlite-schema-grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,24 @@ export class SqliteSchemaGrammar extends SchemaGrammar {
'bigInteger', 'integer', 'mediumInteger', 'smallInteger', 'tinyInteger'
];

public compileColumns(table: string): string {
return `select name,
type,
not "notnull" as "nullable",
dflt_value as "default",
pk as "primary",
hidden as "extra"
from pragma_table_xinfo(${
this.quoteString(table.replace(/\./g, '__'))
})
order by cid asc`;
}

public compileIndexes(table: string) {
table = this.quoteString(table.replace(/\./g, '__'));
return `select 'primary' as name, group_concat(col) as columns, 1 as "unique", 1 as "primary"
from (select name as col
from pragma_table_info(${this.quoteString(table.replace(/\./g, '__'))})
from pragma_table_info(${table})
where pk > 0
order by pk, cid)
group by name
Expand All @@ -36,6 +50,18 @@ export class SqliteSchemaGrammar extends SchemaGrammar {
group by name, "unique", "primary"`;
}

public compileForeignKeys(table: string) {
return `select group_concat("from") as columns,
"table" as foreign_table,
group_concat("to") as foreign_columns,
on_update,
on_delete
from (select * from pragma_foreign_key_list(${
this.quoteString(table.replace(/\./g, '__'))
}) order by id desc, seq)
group by id, "table", on_update, on_delete`;
}

/*Compile the query to determine if a table exists.*/
public compileTableExists() {
return 'select * from sqlite_master where type = \'table\' and name = ?';
Expand Down Expand Up @@ -195,7 +221,7 @@ export class SqliteSchemaGrammar extends SchemaGrammar {
const indexes = await connection.getSchemaBuilder().getIndexes(blueprint.getTable());
const index = indexes.find(index => index.name === command.from);
if (!index) {
throw new Error('Index [{$command->from}] does not exist.');
throw new Error(`Index [${command.from}] does not exist.`);
}
if (index['primary']) {
throw new Error('SQLite does not support altering primary keys.');
Expand Down
60 changes: 44 additions & 16 deletions libs/fedaco/src/schema/schema-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*
* Use of this source code is governed by an MIT-style license
*/
import { isObject } from '@gradii/nanofn';
import { intersection, tap } from 'ramda';
import { isBlank, isObject } from '@gradii/nanofn';
import { intersection, pluck, tap } from 'ramda';
import type { Connection } from '../connection';
import { DbalTable } from '../dbal/dbal-table';
import { wrap } from '../helper/arr';
Expand Down Expand Up @@ -93,17 +93,49 @@ export class SchemaBuilder {

/*Get the column listing for a given table.*/
public async getColumnListing(table: string): Promise<string[]> {
const results = await this.connection.selectFromWriteConnection(
this.grammar.compileColumnListing(this.connection.getTablePrefix() + table)
);
return this.connection.getPostProcessor().processColumnListing(results);
return pluck('name', await this.getColumns(table));
}

public async getColumns(table: string, columns: string[]): Promise<string[]> {
public async getColumns(table: string): Promise<any[]> {
table = this.connection.getTablePrefix() + table;
return this.connection.getPostProcessor().processColumns(
await this.connection.selectFromWriteConnection(this.grammar.compileColumns(table))
)
);
}

public async getIndexes(table: string): Promise<any[]> {
table = this.connection.getTablePrefix() + table;

return this.connection.getPostProcessor().processIndexes(
await this.connection.selectFromWriteConnection(this.grammar.compileIndexes(table))
);
}

public async getIndexListing(table: string): Promise<any[]> {
return pluck('name', await this.getIndexes(table));
}

public async hasIndex(table: string, index: string | string[], type?: string): Promise<boolean> {
type = isBlank(type) ? type : type.toLowerCase();
for (const value of await this.getIndexes(table)) {
const typeMatches = isBlank(value)
|| (type === 'primary' && value['primary'])
|| (type === 'unique' && value['unique'])
|| type === value['type'];

if (value['name'] === index || value['column'] === index && typeMatches) {
return true;
}
}
return false;
}

public async getForeignKeys(table: string) {
table = this.connection.getTablePrefix() + table;

return this.connection.getPostProcessor().processForeignKeys(
await this.connection.selectFromWriteConnection(this.grammar.compileForeignKeys(table))
);
}

/*Modify a table on the schema.*/
Expand Down Expand Up @@ -514,6 +546,10 @@ export class SchemaBuilder {
return tables;
}

/**
* @deprecated
* @param tableName
*/
public async listTableDetails(tableName: string): Promise<DbalTable> {
const columns = await this.listTableColumns(tableName);
let foreignKeys = [];
Expand Down Expand Up @@ -586,12 +622,4 @@ export class SchemaBuilder {
public blueprintResolver(resolver: Function) {
this.resolver = resolver;
}

public async getIndexes(table: string) {
table = this.connection.getTablePrefix() + table;

return this.connection.getPostProcessor().processIndexes(
await this.connection.selectFromWriteConnection(this.grammar.compileIndexes(table))
);
}
}
Loading

0 comments on commit a815622

Please sign in to comment.