Skip to content

Commit

Permalink
Operator operations (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
dolezel authored Jan 23, 2018
1 parent a9dd40a commit 7cac067
Show file tree
Hide file tree
Showing 6 changed files with 380 additions and 30 deletions.
153 changes: 150 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ This is required for some SQL operations that cannot be run within a transaction
> Drop a function - [postgres docs](http://www.postgresql.org/docs/current/static/sql-dropfunction.html)
**Arguments:**
- `function_name` _[string]_ - name of the the function to drop
- `function_name` _[string]_ - name of the function to drop
- `function_params` _[array]_ - [see](#pgmcreatefunction-function_name-function_params-function_options-definition-)
- `drop_options` _[object]_ - options:
- `ifExists` _[boolean]_ - drops function only if it exists
Expand Down Expand Up @@ -634,7 +634,7 @@ This is required for some SQL operations that cannot be run within a transaction
**Arguments:**
- `table_name` _[string]_ - name of the table where the trigger lives
- `trigger_name` _[string]_ - name of the the trigger to drop
- `trigger_name` _[string]_ - name of the trigger to drop
- `drop_options` _[object]_ - options:
- `ifExists` _[boolean]_ - drops trigger only if it exists
- `cascade` _[boolean]_ - drops also dependent objects
Expand Down Expand Up @@ -673,7 +673,7 @@ This is required for some SQL operations that cannot be run within a transaction
> Drop a schema - [postgres docs](http://www.postgresql.org/docs/current/static/sql-dropschema.html)
**Arguments:**
- `schema_name` _[string]_ - name of the the schema to drop
- `schema_name` _[string]_ - name of the schema to drop
- `drop_options` _[object]_ - options:
- `ifExists` _[boolean]_ - drops schema only if it exists
- `cascade` _[boolean]_ - drops also dependent objects
Expand Down Expand Up @@ -813,6 +813,153 @@ This is required for some SQL operations that cannot be run within a transaction

-----------------------------------------------------

### Operator Operations

#### `pgm.createOperator( operator_name, options )`

> Create a new operator - [postgres docs](https://www.postgresql.org/docs/current/static/sql-createoperator.html)
**Arguments:**
- `operator_name` _[string]_ - name of the new operator
- `options` _[object]_ - options:
- `procedure` _[string]_ - name of procedure performing operation
- `left` _[string]_ - type of left argument
- `right` _[string]_ - type of right argument
- `commutator` _[string]_ - name of commutative operator
- `negator` _[string]_ - name of negating operator
- `restrict` _[string]_ - name of restriction procedure
- `join` _[string]_ - name of join procedure
- `hashes` _[boolean]_ - adds `HASHES` clause
- `merges` _[boolean]_ - adds `MERGES` clause

**Reverse Operation:** `dropOperator`

-----------------------------------------------------

#### `pgm.dropOperator( operator_name, drop_options )`

> Drop a operator - [postgres docs](http://www.postgresql.org/docs/current/static/sql-dropoperator.html)
**Arguments:**
- `operator_name` _[string]_ - name of the operator to drop
- `drop_options` _[object]_ - options:
- `ifExists` _[boolean]_ - drops schema only if it exists
- `cascade` _[boolean]_ - drops also dependent objects
- `left` _[string]_ - type of left argument
- `right` _[string]_ - type of right argument

-----------------------------------------------------

#### `pgm.createOperatorClass( operator_class_name, type, index_method, operator_list, options )`

> Create a new operator class - [postgres docs](https://www.postgresql.org/docs/current/static/sql-createopclass.html)
**Arguments:**
- `operator_class_name` _[string]_ - name of the new operator class
- `type` _[string]_ - data type of the new operator class
- `index_method` _[string]_ - name of the index method of operator class
- `operator_list` _[array]_ - of [operator objects](#operator-list-definitions)
- `options` _[object]_ - options:
- `default` _[boolean]_ - adds `DEFAULT` clause
- `family` _[string]_ - type of left argument

**Reverse Operation:** `dropOperatorClass`

-----------------------------------------------------

#### `pgm.dropOperatorClass( operator_class_name, index_methoddrop_options )`

> Drop a operator class - [postgres docs](http://www.postgresql.org/docs/current/static/sql-dropopclass.html)
**Arguments:**
- `operator_class_name` _[string]_ - name of the operator class to drop
- `index_method` _[string]_ - name of the index method of operator class
- `drop_options` _[object]_ - options:
- `ifExists` _[boolean]_ - drops schema only if it exists
- `cascade` _[boolean]_ - drops also dependent objects

-----------------------------------------------------

#### `pgm.renameOperatorClass( old_operator_class_name, index_method, new_operator_class_name )`

> Rename a operator class - [postgres docs](http://www.postgresql.org/docs/current/static/sql-alteropclass.html)
**Arguments:**
- `old_operator_class_name` _[string]_ - old name of the operator class
- `index_method` _[string]_ - name of the index method of operator class
- `new_operator_class_name` _[string]_ - new name of the operator class

-----------------------------------------------------

#### `pgm.createOperatorFamily( operator_family_name, index_method )`

> Create a new operator family - [postgres docs](https://www.postgresql.org/docs/current/static/sql-createopfamily.html)
**Arguments:**
- `operator_family_name` _[string]_ - name of the new operator family
- `index_method` _[string]_ - name of the index method of operator family

**Reverse Operation:** `dropOperatorFamily`

-----------------------------------------------------

#### `pgm.dropOperatorFamily( operator_family_name, index_methoddrop_options )`

> Drop a operator family - [postgres docs](http://www.postgresql.org/docs/current/static/sql-dropopfamily.html)
**Arguments:**
- `operator_family_name` _[string]_ - name of the operator family to drop
- `index_method` _[string]_ - name of the index method of operator family
- `drop_options` _[object]_ - options:
- `ifExists` _[boolean]_ - drops schema only if it exists
- `cascade` _[boolean]_ - drops also dependent objects

-----------------------------------------------------

#### `pgm.renameOperatorFamily( old_operator_family_name, index_method, new_operator_family_name )`

> Rename a operator family - [postgres docs](http://www.postgresql.org/docs/current/static/sql-alteropfamily.html)
**Arguments:**
- `old_operator_family_name` _[string]_ - old name of the operator family
- `index_method` _[string]_ - name of the index method of operator family
- `new_operator_family_name` _[string]_ - new name of the operator family

-----------------------------------------------------

#### `pgm.addToOperatorFamily( operator_family_name, index_method, operator_list )`

> Rename a operator family - [postgres docs](http://www.postgresql.org/docs/current/static/sql-alteropfamily.html)
**Arguments:**
- `operator_family_name` _[string]_ - name of the operator family
- `index_method` _[string]_ - name of the index method of operator family
- `operator_list` _[array]_ - of [operator objects](#operator-list-definitions)

-----------------------------------------------------

#### `pgm.removeFromOperatorFamily( operator_family_name, index_method, operator_list )`

> Rename a operator family - [postgres docs](http://www.postgresql.org/docs/current/static/sql-alteropfamily.html)
**Arguments:**
- `operator_family_name` _[string]_ - name of the operator family
- `index_method` _[string]_ - name of the index method of operator family
- `operator_list` _[array]_ - of [operator objects](#operator-list-definitions)

-----------------------------------------------------

#### Operator List Definitions

Some functions for defining operators take as parameter `operator_list` which is array of objects with following structure:

- `type` _[string]_ - `function` or `operator`
- `number` _[number]_ - index
- `name` _[string]_ - name of operator or procedure
- `params` _[array]_ - list of argument types of operator or procedure

-----------------------------------------------------

### Miscellaneous Operations

#### `pgm.sql( sql )`
Expand Down
47 changes: 46 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,39 @@ export type SequenceOptionsCreate = SequenceOptionsCreateEn & SequenceOptions

export type SequenceOptionsAlter = SequenceOptionsAlterEn & SequenceOptions

export interface CreateOperatorOptions {
procedure: Name
left?: Name
right?: Name
commutator?: Name
negator?: Name
restrict?: Name
join?: Name
hashes?: boolean
merges?: boolean
}

export interface DropOperatorOptions {
left?: Name
right?: Name
ifExists?: boolean
cascade?: boolean
}

export interface CreateOperatorClassOptions {
default?: boolean
family?: string
}

export interface OperatorListDefinition {
type?: string
number?: number
name?: Name
params?: FunctionParam[]
}

export type TriggerOptions = TriggerOptionsEn & FunctionOptions

export interface MigrationBuilder {
// Tables
createTable(tableName: Name, columns: ColumnDefinitions, options?: TableOptions): void
Expand Down Expand Up @@ -314,12 +347,24 @@ export interface MigrationBuilder {
alterDomain(domain_name: Name, domain_options: DomainOptionsAlter): void
renameDomain(old_domain_name: Name, new_domain_name: Name): void

// Domains
// Sequences
createSequence(sequence_name: Name, sequence_options: SequenceOptionsCreate): void
dropSequence(sequence_name: Name, drop_options: DropOptions): void
alterSequence(sequence_name: Name, sequence_options: SequenceOptionsAlter): void
renameSequence(old_sequence_name: Name, new_sequence_name: Name): void

// Operators
createOperator(operator_name: Name, options: CreateOperatorOptions): void
dropOperator(operator_name: Name, drop_options: DropOperatorOptions): void
createOperatorClass(operator_class_name: Name, type: Name, index_method: Name, operator_list: OperatorListDefinition, options: CreateOperatorClassOptions): void
dropOperatorClass(operator_class_name: Name, index_method: Name, drop_options: DropOptions): void
renameOperatorClass(old_operator_class_name: Name, index_method: Name, new_operator_class_name: Name): void
createOperatorFamily(operator_family_name: Name, index_method: Name): void
dropOperatorFamily(operator_family_name: Name, new_schema_name: Name, drop_options: DropOptions): void
renameOperatorFamily(old_operator_family_name: Name, index_method: Name, new_operator_family_name: Name): void
addToOperatorFamily(operator_family_name: Name, index_method: Name, operator_list: OperatorListDefinition): void
removeFromOperatorFamily(operator_family_name: Name, index_method: Name, operator_list: OperatorListDefinition): void

sql(sql: string, args?: object): void
func(sql: string): PgLiteral
noTransaction(): void
Expand Down
12 changes: 12 additions & 0 deletions lib/migration-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as triggers from './operations/triggers';
import * as schemas from './operations/schemas';
import * as domains from './operations/domains';
import * as sequences from './operations/sequences';
import * as operators from './operations/operators';
import * as other from './operations/other';

export default class MigrationBuilder {
Expand Down Expand Up @@ -107,6 +108,17 @@ export default class MigrationBuilder {
this.alterSequence = wrap(sequences.alter(options.typeShorthands));
this.renameSequence = wrap(sequences.rename);

this.createOperator = wrap(operators.createOperator);
this.dropOperator = wrap(operators.dropOperator);
this.createOperatorClass = wrap(operators.createOperatorClass(options.typeShorthands));
this.dropOperatorClass = wrap(operators.dropOperatorClass);
this.renameOperatorClass = wrap(operators.renameOperatorClass);
this.createOperatorFamily = wrap(operators.createOperatorFamily);
this.dropOperatorFamily = wrap(operators.dropOperatorFamily);
this.renameOperatorFamily = wrap(operators.renameOperatorFamily);
this.addToOperatorFamily = wrap(operators.addToOperatorFamily(options.typeShorthands));
this.removeFromOperatorFamily = wrap(operators.removeFromOperatorFamily(options.typeShorthands)); // eslint-disable-line max-len

this.sql = wrap(other.sql);

// Other utilities which may be useful
Expand Down
27 changes: 1 addition & 26 deletions lib/operations/functions.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,4 @@
import { schemalize, template, escapeValue, applyType } from '../utils';

const formatParam = type_shorthands => (param) => {
const {
mode,
name,
type,
default: defaultValue,
} = applyType(param, type_shorthands);
const options = [];
if (mode) {
options.push(mode);
}
if (name) {
options.push(schemalize(name));
}
if (type) {
options.push(type);
}
if (defaultValue) {
options.push(`DEFAULT ${escapeValue(defaultValue)}`);
}
return options.join(' ');
};

const formatParams = (params = [], type_shorthands) => `(${params.map(formatParam(type_shorthands)).join(', ')})`;
import { template, escapeValue, formatParams } from '../utils';

export const drop = type_shorthands =>
(function_name, function_params = [], { ifExists, cascade } = {}) =>
Expand Down
Loading

0 comments on commit 7cac067

Please sign in to comment.