Commits

Daniel Lyons committed 72fca93

relocating patterns, per previous layout

  • Participants
  • Parent commits 95a95ac

Comments (0)

Files changed (9)

         
     % patterns
     logtalk_load([
-        missing_foreign_key_index,
-        repeating_groups,
-        unnecessary_surrogate_key,
-        missing_foreign_key]))).
+        patterns(missing_foreign_key_index),
+        patterns(repeating_groups),
+        patterns(unnecessary_surrogate_key),
+        patterns(missing_foreign_key)]))).

missing_foreign_key.lgt

-:- object(missing_foreign_key, implements(patternp)).
-
-    %% True if FK is a foreign key to the Source table
-    foreign_key_name(Source, FK) :-
-        nonvar(Source), !,
-        pluralize(SourceCol, Source),
-        atom_concat(SourceCol, '_id', FK).
-    foreign_key_name(Source, FK) :-
-        nonvar(FK), !,
-        atom_concat(SourceCol, '_id', FK),
-        pluralize(SourceCol, Source).
-
-    %% True if Destination table's column FK looks like a foreign key to
-    %% Source table's PK.
-    detect(Schema, missing_foreign_key(Destination, [FK], Source, [id])) :-
-        Schema::column(Destination, FK, _),
-        foreign_key_name(Source, FK),
-        Schema::column(Source, id, _),
-        \+ Schema::foreign_key(Destination, FK, Source, _).
-
-    pluralize(Noun, Nouns) :- atom_concat(Noun, 's', Nouns).
-
-:- end_object.
-
-:- object(missing_foreign_key(_Destination, _FK, _Source, _PK), implements(suggestionp)).
-
-    destination(Destination) :- parameter(1, Destination).
-    fk(FK) :- parameter(2, FK).
-    source(Source) :- parameter(3, Source).
-    pk(PK) :- parameter(4, PK).
-    
-    description(Desc) :-
-        destination(Dest), fk(FK), source(Source),
-        parser::identifiers(FK, FKColumns), util::atom_concat(FKColumns, FKS),
-        util::atom_concat(['Missing foreign key from ', Dest, ' (', FKS, ')', ' to ', Source], Desc).
-        
-    sql(SQL) :-
-        destination(Destination), fk(FK), source(Source), pk(PK),
-        util::atom_concat([Destination, '_', Source, '_fk'], FKName),
-        parser::identifiers(FK, FKColumns), parser::identifiers(PK, PKColumns),
-        list::append([[alter,table,Destination,add,constraint,FKName,foreign,key,'('],FKColumns,[')',references,Source,'('], PKColumns, [')',';']], SQL).
-
-:- end_object.

missing_foreign_key_index.lgt

-%% Pattern
-:- object(missing_foreign_key_index, implements(patternp)).
-
-    detect(Schema, missing_foreign_key_index(Table, Column)) :-
-        Schema::foreign_key(Table, Column, _, _), 
-        \+ Schema::index(Table, Column).
-
-:- end_object.
-
-%% Suggestion
-:- object(missing_foreign_key_index(_Table, _Column), implements(suggestionp)).
-
-    table(Table) :- parameter(1, Table).
-    column(Column) :- parameter(2, Column).
-
-    description(Desc) :-
-        table(Table), column(Column),
-        parser::identifiers(ColumnIdents, Column), 
-        util::atom_concat(ColumnIdents, Columns),
-        util::atom_concat(['Unindexed foreign key on ', Table, ' (', Columns, ')'], Desc).
-        
-    sql(SQL) :-
-        table(Table), column(Column),
-        parser::identifiers(ColumnIdents, Column),
-        list::append([create, index, on, Table, '('], ColumnIdents, FirstHalf),
-        list::append(FirstHalf, [')'], SQL).
-
-:- end_object.

patterns/missing_foreign_key.lgt

+:- object(missing_foreign_key, implements(patternp)).
+
+    %% True if FK is a foreign key to the Source table
+    foreign_key_name(Source, FK) :-
+        nonvar(Source), !,
+        pluralize(SourceCol, Source),
+        atom_concat(SourceCol, '_id', FK).
+    foreign_key_name(Source, FK) :-
+        nonvar(FK), !,
+        atom_concat(SourceCol, '_id', FK),
+        pluralize(SourceCol, Source).
+
+    %% True if Destination table's column FK looks like a foreign key to
+    %% Source table's PK.
+    detect(Schema, missing_foreign_key(Destination, [FK], Source, [id])) :-
+        Schema::column(Destination, FK, _),
+        foreign_key_name(Source, FK),
+        Schema::column(Source, id, _),
+        \+ Schema::foreign_key(Destination, FK, Source, _).
+
+    pluralize(Noun, Nouns) :- atom_concat(Noun, 's', Nouns).
+
+:- end_object.
+
+:- object(missing_foreign_key(_Destination, _FK, _Source, _PK), implements(suggestionp)).
+
+    destination(Destination) :- parameter(1, Destination).
+    fk(FK) :- parameter(2, FK).
+    source(Source) :- parameter(3, Source).
+    pk(PK) :- parameter(4, PK).
+    
+    description(Desc) :-
+        destination(Dest), fk(FK), source(Source),
+        parser::identifiers(FK, FKColumns), util::atom_concat(FKColumns, FKS),
+        util::atom_concat(['Missing foreign key from ', Dest, ' (', FKS, ')', ' to ', Source], Desc).
+        
+    sql(SQL) :-
+        destination(Destination), fk(FK), source(Source), pk(PK),
+        util::atom_concat([Destination, '_', Source, '_fk'], FKName),
+        parser::identifiers(FK, FKColumns), parser::identifiers(PK, PKColumns),
+        list::append([[alter,table,Destination,add,constraint,FKName,foreign,key,'('],FKColumns,[')',references,Source,'('], PKColumns, [')',';']], SQL).
+
+:- end_object.

patterns/missing_foreign_key_index.lgt

+%% Pattern
+:- object(missing_foreign_key_index, implements(patternp)).
+
+    detect(Schema, missing_foreign_key_index(Table, Column)) :-
+        Schema::foreign_key(Table, Column, _, _), 
+        \+ Schema::index(Table, Column).
+
+:- end_object.
+
+%% Suggestion
+:- object(missing_foreign_key_index(_Table, _Column), implements(suggestionp)).
+
+    table(Table) :- parameter(1, Table).
+    column(Column) :- parameter(2, Column).
+
+    description(Desc) :-
+        table(Table), column(Column),
+        parser::identifiers(ColumnIdents, Column), 
+        util::atom_concat(ColumnIdents, Columns),
+        util::atom_concat(['Unindexed foreign key on ', Table, ' (', Columns, ')'], Desc).
+        
+    sql(SQL) :-
+        table(Table), column(Column),
+        parser::identifiers(ColumnIdents, Column),
+        list::append([create, index, on, Table, '('], ColumnIdents, FirstHalf),
+        list::append(FirstHalf, [')'], SQL).
+
+:- end_object.

patterns/repeating_groups.lgt

+%% Pattern
+:- object(repeating_groups, implements(patternp)).
+
+    %% True if Columns is a list of columns ending in integers that share
+    %% Prefix on Table.
+    detect(Schema, repeating_groups(Table, Prefix, Columns)) :-
+        setof(Column, 
+              repeating_group_name(Schema, Table, Prefix, Column), Columns).
+
+    % true if Atom looks like a number
+    is_numeric_atom(Atom) :- catch(atom_number(Atom, _), _, fail).
+    
+    % utility routine: true if Column is Prefix with a number after it on
+    % Table.
+    repeating_group_name(Schema, Table, Prefix, Column) :-
+      Schema::column(Table, Column, _), 
+      atom_concat(Prefix, Num, Column), is_numeric_atom(Num).
+    
+:- end_object.
+
+%% Suggestion
+:- object(repeating_groups(_Table, _Prefix, _Columns), implements(suggestionp)).
+
+    table(Table) :- parameter(1, Table).
+    prefix(Prefix) :- parameter(2, Prefix).
+    columns(Columns) :- parameter(3, Columns).
+
+    description(Desc) :-
+        table(Table), prefix(Prefix),
+        util::atom_concat(['Repeating group on ', Table, ' with name ', 
+                           Prefix], 
+                          Desc).
+    
+    sql(SQL) :-
+        create_new_table(SQL1),
+        migrate_data(SQL2),
+        drop_columns(SQL3),
+        list::append([['begin', ';'], SQL1, [';'], SQL2, [';'], SQL3, [';'], ['commit', ';']], SQL).
+        
+    create_new_table([create, table, NewTableName, '(', ')']) :-
+        table(Table), prefix(Prefix),
+        util::pluralize(Prefix, Prefixes),
+        util::atom_concat([Table, '_', Prefixes], NewTableName).
+        
+    migrate_data([]).
+    drop_columns([]).
+
+:- end_object.

patterns/unnecessary_surrogate_key.lgt

+:- object(unnecessary_surrogate_key, implements(patternp)).
+
+    detect(Schema, unnecessary_surrogate_key(Table, OldId, NewId, Schema)) :-
+      Schema::primary_key(Table, [OldId]),
+      Schema::column(Table, OldId, serial),
+      Schema::unique(Table, NewId).
+
+:- end_object.
+
+:- object(unnecessary_surrogate_key(_Table, _OldId, _NewId, _Schema), implements(suggestionp)).
+
+    table(Table) :- parameter(1, Table).
+    new_id(NewId) :- parameter(3, NewId).
+
+    description(Desc) :-
+        table(Table), new_id(NewId), parser::identifiers(NewIdColumns, NewId),
+        util::atom_concat(NewIdColumns, NewIdText),
+        util::atom_concat(['Unnecessary surrogate key on ', Table, ' (use ', NewIdText, ' instead)'], Desc).
+    
+    sql(SQL) :-
+        table(Table), parameter(2, OldId), new_id(NewId),
+        parser::identifiers(NewIdColumns, NewId),
+        % prior to altering the table, we must modify any foreign keys that point to this database
+        % Schema::foreign_key(Source, SCols, Table, [OldId]) ...
+        list::append([[alter,table,Table,drop,column,OldId,',',add,primary,key,'('],NewIdColumns,[')',';']], SQL).
+
+:- end_object.

repeating_groups.lgt

-%% Pattern
-:- object(repeating_groups, implements(patternp)).
-
-    %% True if Columns is a list of columns ending in integers that share
-    %% Prefix on Table.
-    detect(Schema, repeating_groups(Table, Prefix, Columns)) :-
-        setof(Column, 
-              repeating_group_name(Schema, Table, Prefix, Column), Columns).
-
-    % true if Atom looks like a number
-    is_numeric_atom(Atom) :- catch(atom_number(Atom, _), _, fail).
-    
-    % utility routine: true if Column is Prefix with a number after it on
-    % Table.
-    repeating_group_name(Schema, Table, Prefix, Column) :-
-      Schema::column(Table, Column, _), 
-      atom_concat(Prefix, Num, Column), is_numeric_atom(Num).
-    
-:- end_object.
-
-%% Suggestion
-:- object(repeating_groups(_Table, _Prefix, _Columns), implements(suggestionp)).
-
-    table(Table) :- parameter(1, Table).
-    prefix(Prefix) :- parameter(2, Prefix).
-    columns(Columns) :- parameter(3, Columns).
-
-    description(Desc) :-
-        table(Table), prefix(Prefix),
-        util::atom_concat(['Repeating group on ', Table, ' with name ', 
-                           Prefix], 
-                          Desc).
-    
-    sql(SQL) :-
-        create_new_table(SQL1),
-        migrate_data(SQL2),
-        drop_columns(SQL3),
-        list::append([['begin', ';'], SQL1, [';'], SQL2, [';'], SQL3, [';'], ['commit', ';']], SQL).
-        
-    create_new_table([create, table, NewTableName, '(', ')']) :-
-        table(Table), prefix(Prefix),
-        util::pluralize(Prefix, Prefixes),
-        util::atom_concat([Table, '_', Prefixes], NewTableName).
-        
-    migrate_data([]).
-    drop_columns([]).
-
-:- end_object.

unnecessary_surrogate_key.lgt

-:- object(unnecessary_surrogate_key, implements(patternp)).
-
-    detect(Schema, unnecessary_surrogate_key(Table, OldId, NewId, Schema)) :-
-      Schema::primary_key(Table, [OldId]),
-      Schema::column(Table, OldId, serial),
-      Schema::unique(Table, NewId).
-
-:- end_object.
-
-:- object(unnecessary_surrogate_key(_Table, _OldId, _NewId, _Schema), implements(suggestionp)).
-
-    table(Table) :- parameter(1, Table).
-    new_id(NewId) :- parameter(3, NewId).
-
-    description(Desc) :-
-        table(Table), new_id(NewId), parser::identifiers(NewIdColumns, NewId),
-        util::atom_concat(NewIdColumns, NewIdText),
-        util::atom_concat(['Unnecessary surrogate key on ', Table, ' (use ', NewIdText, ' instead)'], Desc).
-    
-    sql(SQL) :-
-        table(Table), parameter(2, OldId), new_id(NewId),
-        parser::identifiers(NewIdColumns, NewId),
-        % prior to altering the table, we must modify any foreign keys that point to this database
-        % Schema::foreign_key(Source, SCols, Table, [OldId]) ...
-        list::append([[alter,table,Table,drop,column,OldId,',',add,primary,key,'('],NewIdColumns,[')',';']], SQL).
-
-:- end_object.