diff --git a/database/model/db_model.anubis b/database/model/db_model.anubis index 51502ef..599f016 100644 --- a/database/model/db_model.anubis +++ b/database/model/db_model.anubis @@ -20,14 +20,18 @@ *** (1) Formal description of the database. - *** (1.1) 'DB_Model_Field' (data types of table columns). + *** (1.1) 'HK_Model_Field' (data types of table columns). +public type HK_App_Name: + this, + app_name(String name), + none. public type DB_Datetime_Field_Attr: none, //Nothing special, normal behaviour auto_now, //Always update datetime at SQL update auto_now_add. //Set current datetime when row is created and can't be edited anymore -public type DB_Model_Field: +public type HK_Model_Field: //anubis(String _T), // the datum of type _T is serialized and base64 encoded p_key, //binary, // contains a byte array @@ -35,11 +39,18 @@ public type DB_Model_Field: date_field(DB_Datetime_Field_Attr), // date with the precision of the day time_field(DB_Datetime_Field_Attr), // time with the precision of the second datetime_field(DB_Datetime_Field_Attr), // datetime with the precision of the second - foreign_key(String table_name), // foreign key to 'id' in another (or same) table and column_name for choice selector. + foreign_key(HK_App_Name app_name, String table_name), // foreign key to 'id' in another (or same) table and column_name for choice selector. integer_field, // integer of arbitrary size char_field(Int size), // text of maximal size 'size' (number of characters) text_field. // text of variable size +public define HK_Model_Field + foreign_key + ( + String table_name + )= + foreign_key(this, table_name). + From the point of view of your Anubis program, these data will be of types: Name | Anubis type | Type within the database @@ -74,17 +85,19 @@ public type MaybeNull($T): (number of seconds since the epoch) are provided in 'library/tools/ISO-8601.anubis'. - *** (1.3) 'DB_Model_Attr' (attributes of columns). + *** (1.3) 'HK_Model_Attr' (attributes of columns). Possible attributes of columns: (each column has a list of such attributes) -public type DB_Model_Attr: +public type HK_Model_Attr: unique, // by default, the values in a column are not required to be all different indexed, // by default, a column is not indexed - default(String). // default value (used in case of creation of a NON NULL column + default(String), // default value (used in case of creation of a NON NULL column // in a table which already contains some rows) + not_null. + -public type DB_Help_Text: +public type HK_Help_Text: no_help_text, //no help text available help_text_TAG(String), //TAG use with Anubis translation tool /locale/L3.anubis help_text(String). //pure text to show @@ -94,7 +107,7 @@ public define String */ to_Anubis_source ( - DB_Help_Text help + HK_Help_Text help )= if help is { @@ -103,48 +116,48 @@ public define String help_text(str) then "help_text(\""+str+"\")" }. - Of course, 'NOT NULL' is not an alternative of 'DB_Model_Attr' because it is already coded into the - database type itself (type 'DB_Model_Field' above). + Of course, 'NOT NULL' is not an alternative of 'HK_Model_Attr' because it is already coded into the + database type itself (type 'HK_Model_Field' above). - *** (1.4) 'DB_Model_Column' (describing a column in a table). + *** (1.4) 'HK_Model_Column' (describing a column in a table). Description of a column: -public type DB_Model_Column: - db_column (String name, // name of ordinary column (i.e. all but 'id') - DB_Model_Field type, - List(DB_Model_Attr) attributes, - DB_Help_Text help +public type HK_Model_Column: + hk_column (String name, // name of ordinary column (i.e. all but 'id') + HK_Model_Field type, + List(HK_Model_Attr) attributes, + HK_Help_Text help ). -public define DB_Model_Column +public define HK_Model_Column /* Constructor helper for db_column without help attribute. the real type constructor will be called with the attribute no_help_text */ - db_column + hk_column ( String name, - DB_Model_Field type, - List(DB_Model_Attr) attributes, + HK_Model_Field type, + List(HK_Model_Attr) attributes, )= - db_column(name, type, attributes, no_help_text). + hk_column(name, type, attributes, no_help_text). -public define DB_Model_Column - db_column +public define HK_Model_Column + hk_column ( String name, - DB_Model_Field type + HK_Model_Field type )= - db_column(name, type, [], no_help_text). + hk_column(name, type, [], no_help_text). *** (3.7) 'DB_Model_Table' (describing a table). Description of a table: -public type DB_Model: - db_model( List(DB_Model_Column) columns, // columns other than the primary key column (if any) +public type HK_Model: + hk_model( List(HK_Model_Column) columns, // columns other than the primary key column (if any) String show_string). public type HK_List_View: @@ -155,23 +168,29 @@ public type HK_Edit_View: edit_view_all, edit_view(String view_name, List(String) columns). -public type DB_Display: +public type HK_Display: db_display(List(HK_List_View) list_views, //List(HK_Edit_View) edit_views List(String)). -public type DB_Table: - db_table ( String name, // name of the table - DB_Model model, - DB_Display display). +public type HK_Table: + hk_table ( String name, // name of the table + HK_Model model, + HK_Display display). *** (3.8) 'DB_Database' (describing a whole database). Description of a whole database: -public type DB_Database: - db_database (String name, // name of the database - List(DB_Table) tables). +public type HK_App: + hk_app( String app_name, + List(HK_Table) hk_tables + ). + +public type HK_Database: + hk_database (String name, // name of the database + List(HK_App) apps //list of applications + ). // list of all tables in the database @@ -249,7 +268,27 @@ define macro Maybe(String) } }. - +define Int + _max + ( + List(HK_Model_Column) columns, + Int current_max + )= + if columns is + { + [] then current_max, + [h . t] then + since h is hk_column(name, _, _, _), + _max(t, max(current_max, length(name))) + } + . + +public define Int + max + ( + List(HK_Model_Column) columns + )= + _max(columns, 0). *** [2] Verifications concerning the description of the database and queries. @@ -260,8 +299,8 @@ define macro Maybe(String) ( String table_name, // the first 3 arguments concern the current table. MetaSQL_PrimKey pk, - List(DB_Model_Column) columns, - List(DB_Model_Column) forwards, // subsequent tables + List(HK_Model_Column) columns, + List(HK_Model_Column) forwards, // subsequent tables List(String) backwards // list of backwards table names ) = if columns is @@ -277,7 +316,7 @@ define macro Maybe(String) has_forbidden_cycles ( String db_name, - List(DB_Model_Column) tables, + List(HK_Model_Column) tables, List(String) backwards // names of 'backwards' tables ) = if tables is @@ -299,10 +338,10 @@ define macro Maybe(String) define Bool has_forbidden_column_names ( - DB_Model_Column tab + HK_Model_Column tab ) = if tab is table(_,_,cols) then - mapor((DB_Model_Column cs) |-> + mapor((HK_Model_Column cs) |-> if cs is col(name,_,_) then is_forbidden_column_name(name), cols). @@ -323,7 +362,7 @@ define macro Maybe(String) public define String to_Anubis_type ( - DB_Model_Field t + HK_Model_Field t ) = if t is { @@ -334,17 +373,51 @@ public define String date_field(_) then "DB_date", time_field(_) then "DB_time", datetime_field(_) then "DB_datetime", - foreign_key(_) then "Int", + foreign_key(_,_) then "Int", integer_field then "Int", char_field(Int size) then "String", text_field then "String" }. - +define String + format_attributes + ( + List(HK_Model_Attr) attributes + )= + join(" ",map((HK_Model_Attr attr) |-> + if attr is + { + unique then "UNIQUE", + indexed then "INDEXED", // by default, a column is not indexed + default(str) then "DEFAULT "+str, // default value (used in case of creation of a NON NULL column + // in a table which already contains some rows) + not_null then "NOT NULL" + }, attributes)). + +public define String + to_SQL_CREATE + ( + HK_Model_Field t, + List(HK_Model_Attr) attributes, + ) = + if t is + { + //anubis(_T) then "ByteArray", + p_key then fill("INTEGER", 15) +" PRIMARY KEY", + boolean_field then fill("BOOL", 15) +format_attributes(attributes), + date_field(_) then fill("DATE", 15) +format_attributes(attributes), + time_field(_) then fill("TIME", 15) +format_attributes(attributes), + datetime_field(_) then fill("DATETIME", 15)+format_attributes(attributes), + foreign_key(app,fk) then fill("INTEGER", 15) +" NOT NULL REFERENCES "+fk+" (id)", + integer_field then fill("INTEGER", 15) +format_attributes(attributes), + char_field(Int size) then fill("VARCHAR("+size+")", 15) +format_attributes(attributes), + text_field then fill("TEXT", 15) +format_attributes(attributes), + }. + public define String to_Anubis_default ( - DB_Model_Field t + HK_Model_Field t ) = if t is { @@ -355,7 +428,7 @@ public define String date_field(_) then "db_date(\"\")", time_field(_) then "db_time(\"\")", datetime_field(_) then "db_datetime(\"\")", - foreign_key(_) then "(Int)0", + foreign_key(_,_) then "(Int)0", integer_field then "(Int)0", char_field(Int size) then "\"\"", text_field then "\"\"" @@ -364,7 +437,7 @@ public define String public define String from_DB_cursor ( - DB_Model_Field t, + HK_Model_Field t, String cursor, Int index ) = @@ -378,7 +451,7 @@ public define String date_field(_) then "db_date(text"+cursor_index+")", time_field(_) then "db_time(text"+cursor_index+")", datetime_field(_) then "db_datetime(text"+cursor_index+")", - foreign_key(_) then "(Int)db_integer"+cursor_index, + foreign_key(_,_) then "(Int)db_integer"+cursor_index, integer_field then "(Int)db_integer"+cursor_index, char_field(Int size) then "text"+cursor_index, text_field then "text"+cursor_index @@ -389,7 +462,7 @@ define List(String) */ components ( - List(DB_Model_Column) columns, + List(HK_Model_Column) columns, String cursor_name, List(String) so_far, Int idx @@ -398,7 +471,7 @@ define List(String) { [] then reverse(so_far), [h . t ] then - since h is db_column(name, col_type, _, _), + since h is hk_column(name, col_type, _, _), with line = fill(from_DB_cursor(col_type, cursor_name, idx), 35)+ "/* "+name+" */", components(t, cursor_name, [line . so_far], idx + 1 ) }. @@ -408,7 +481,7 @@ public define String ( String constructor_name, String cursor_name, - List(DB_Model_Column) columns, + List(HK_Model_Column) columns, String indent )= indent+constructor_name+"(\n"+indent+" "+join(",\n"+indent+" ", components(columns, cursor_name, [], 0))+"\n"+indent+")". @@ -417,7 +490,7 @@ public define String public define String from_web_arg ( - DB_Model_Field t, + HK_Model_Field t, String name ) = if t is @@ -429,7 +502,7 @@ public define String date_field(attrs) then if attrs = none then "get_DB_date(lwa, \""+name+"\")" else "with "+name+" = get_dummy_DB_date,", time_field(attrs) then if attrs = none then "get_DB_time(lwa, \""+name+"\")" else "with "+name+" = get_dummy_DB_time,", datetime_field(attrs) then if attrs = none then "get_DB_datetime(lwa, \""+name+"\")" else "with "+name+" = get_dummy_DB_datetime,", - foreign_key(String table) then "get_Int(lwa, \""+name+"\")", + foreign_key(_,_) then "get_Int(lwa, \""+name+"\")", integer_field then "get_Int(lwa, \""+name+"\")", char_field(Int size) then "get_String(lwa, \""+name+"\")", text_field then "get_String(lwa, \""+name+"\")" @@ -440,7 +513,7 @@ define List(String) */ _to_List_String ( - List(DB_Model_Column) columns, + List(HK_Model_Column) columns, Bool with_pk, List(String) so_far )= @@ -448,7 +521,7 @@ define List(String) { [] then reverse(so_far), [h . t ] then - since h is db_column(name, _, _, _), + since h is hk_column(name, _, _, _), if name = "id" & with_pk = false then _to_List_String(t, with_pk, so_far) else @@ -460,7 +533,7 @@ public define List(String) */ to_List_String ( - List(DB_Model_Column) columns, + List(HK_Model_Column) columns, Bool with_pk )= _to_List_String(columns, with_pk, []). @@ -470,7 +543,7 @@ public define String generate_constructor ( String constructor_name, - List(DB_Model_Column) columns, + List(HK_Model_Column) columns, String indent )= indent+constructor_name+"(\n"+indent+" "+join(",\n"+indent+" ", to_List_String(columns, true))+")". @@ -478,7 +551,7 @@ public define String public define String to_Bind ( - DB_Model_Field t, + HK_Model_Field t, String type_name, //name of the type String name, //name of component into the type Bool insert //true if insert time else false for update @@ -501,7 +574,7 @@ public define String else "" }, - foreign_key(_) then "bind_Int(\":v_"+name+"\", "+type_name+"."+name+")", + foreign_key(_,_) then "bind_Int(\":v_"+name+"\", "+type_name+"."+name+")", integer_field then "bind_Int(\":v_"+name+"\", "+type_name+"."+name+")", char_field(Int size) then "bind_String(\":v_"+name+"\", "+type_name+"."+name+")", text_field then "bind_String(\":v_"+name+"\", "+type_name+"."+name+")" @@ -512,7 +585,7 @@ public define List(String) */ to_Bind_list ( - List(DB_Model_Column) columns, + List(HK_Model_Column) columns, String type_name, //name of the type List(String) so_far, Bool insert @@ -521,7 +594,7 @@ public define List(String) { [] then reverse(so_far), [h . t ] then - since h is db_column(name, col_type, _, _), + since h is hk_column(name, col_type, _, _), with result = to_Bind(col_type, type_name, name, insert), //we eliminate the empty string because during the join it will have an empty line if result = "" then @@ -533,7 +606,7 @@ public define List(String) public define String generate_Bind_list ( - List(DB_Model_Column) columns, + List(HK_Model_Column) columns, String type_name, String indent, Bool insert @@ -544,10 +617,10 @@ public define String public define Maybe(String) get_column_name ( - DB_Model_Column col, + HK_Model_Column col, Bool insert //true if insert time else false for update ) = - since col is db_column(name, col_type, _, _), + since col is hk_column(name, col_type, _, _), if col_type is { //anubis(_T) then "constant_byte_array(0,0)", @@ -563,7 +636,7 @@ public define Maybe(String) auto_now_add then if insert then success(name) else failure }, - foreign_key(_) then success(name), + foreign_key(_,_) then success(name), integer_field then success(name), char_field(Int size) then success(name), text_field then success(name) @@ -572,7 +645,7 @@ public define Maybe(String) public define List(String) columns_name ( - List(DB_Model_Column) columns, + List(HK_Model_Column) columns, Bool insert, //true if insert time else false for update List(String) so_far ) = @@ -598,7 +671,7 @@ public define String */ insert_values ( - List(DB_Model_Column) columns, + List(HK_Model_Column) columns, String indent )= with columns_list = columns_name(columns, true, []), @@ -612,7 +685,7 @@ public define String */ update_values ( - List(DB_Model_Column) columns, + List(HK_Model_Column) columns, String indent )= with columns_list = columns_name(columns, false, []), @@ -622,10 +695,10 @@ public define String public define Maybe(String) get_VT_edit_entry ( - DB_Model_Column col, + HK_Model_Column col, String type_name ) = - since col is db_column(name, col_type, _, help), + since col is hk_column(name, col_type, _, help), if col_type is { //anubis(_T) then "constant_byte_array(0,0)", @@ -650,7 +723,7 @@ public define Maybe(String) auto_now then success("information(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+".datetime, "+to_Anubis_source(help)+")"), auto_now_add then success("information(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+".datetime, "+to_Anubis_source(help)+")") }, - foreign_key(foreign_table) then success("foreign_text(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", vt_edit_selector(get_selector_"+foreign_table+"), "+to_Anubis_source(help)+")"), + foreign_key(app,foreign_table) then success("foreign_text(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", vt_edit_selector(get_selector_"+foreign_table+"), "+to_Anubis_source(help)+")"), integer_field then success("integer(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", "+to_Anubis_source(help)+")"), char_field(Int size) then success("text(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", "+to_Anubis_source(help)+")"), text_field then success("text_area(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", "+to_Anubis_source(help)+")") @@ -659,7 +732,7 @@ public define Maybe(String) public define String edit_entries ( - List(DB_Model_Column) columns, + List(HK_Model_Column) columns, String type_name, String indent, List(String) so_far @@ -683,17 +756,17 @@ public define String )= indent+join(",\n"+indent, map((String text) |-> "text(\""+to_upper(text)+"\")", list_view)). -public define Maybe(DB_Model_Column) +public define Maybe(HK_Model_Column) get_column_type ( - List(DB_Model_Column) columns, //columns in wich we search + List(HK_Model_Column) columns, //columns in wich we search String name_to_find //Column name to find in previous list )= if columns is { [] then failure, [h . t] then - since h is db_column(name, _, _, _), + since h is hk_column(name, _, _, _), if name_to_find = name then success(h) else @@ -705,11 +778,11 @@ public define String */ to_String ( - DB_Model_Column col, + HK_Model_Column col, String current_type_name, String general_type_name ) = - since col is db_column(name, col_type, _, _), + since col is hk_column(name, col_type, _, _), if col_type is { //anubis(_T) then "constant_byte_array(0,0)", @@ -719,7 +792,7 @@ public define String date_field(attrs) then "to_String("+current_type_name+"."+name+")", time_field(attrs) then "to_String("+current_type_name+"."+name+")", datetime_field(attrs) then "to_String("+current_type_name+"."+name+")" - foreign_key(foreign_table) then "get_"+foreign_table+"_show_string(db, "+current_type_name+"."+name+")", + foreign_key(app,foreign_table) then "get_"+foreign_table+"_show_string(db, "+current_type_name+"."+name+")", integer_field then "to_String("+current_type_name+"."+name+")", char_field(Int size) then current_type_name+"."+name, text_field then current_type_name+"."+name @@ -730,11 +803,11 @@ public define String */ to_Icon ( - DB_Model_Column col, + HK_Model_Column col, String current_type_name, String general_type_name ) = - since col is db_column(name, col_type, _, _), + since col is hk_column(name, col_type, _, _), if col_type is { p_key then icn16_key, @@ -742,7 +815,7 @@ public define String date_field(attrs) then icn16_clock, time_field(attrs) then icn16_clock, datetime_field(attrs) then icn16_clock, - foreign_key(foreign_table) then icn16_question, + foreign_key(app,foreign_table) then icn16_question, integer_field then icn16_question, char_field(Int size) then icn16_question, text_field then icn16_question @@ -751,7 +824,7 @@ public define String public define String to_HTML_cell_text ( - DB_Model_Column column, + HK_Model_Column column, String data_name, String general_type_name )= @@ -760,7 +833,7 @@ public define String public define String to_HTML_cell_link ( - DB_Model_Column column, + HK_Model_Column column, String data_name, String general_type_name )= @@ -769,7 +842,7 @@ public define String public define String to_HTML_cell_link ( - List(DB_Model_Column) columns, + List(HK_Model_Column) columns, String column_name, String data_name, String general_type_name @@ -783,7 +856,7 @@ public define String public define String to_HTML_cell_icon ( - DB_Model_Column column, + HK_Model_Column column, String data_name, String general_type_name )= @@ -793,7 +866,7 @@ public define String public define String to_HTML_cell ( - List(DB_Model_Column) columns, + List(HK_Model_Column) columns, String column_name, String data_name, String general_type_name @@ -802,7 +875,7 @@ public define String { failure then "column name "+column_name+"doesn't exist", success(column) then - since column is db_column(name, col_type, _, _), + since column is hk_column(name, col_type, _, _), if col_type is { //anubis(_T) then "constant_byte_array(0,0)", @@ -812,7 +885,7 @@ public define String date_field(attrs) then to_HTML_cell_text(column, data_name, general_type_name), time_field(attrs) then to_HTML_cell_text(column, data_name, general_type_name), datetime_field(attrs) then to_HTML_cell_text(column, data_name, general_type_name), - foreign_key(foreign_table) then to_HTML_cell_text(column, data_name, general_type_name), + foreign_key(app,foreign_table) then to_HTML_cell_text(column, data_name, general_type_name), integer_field then to_HTML_cell_text(column, data_name, general_type_name), char_field(Int size) then to_HTML_cell_text(column, data_name, general_type_name), text_field then to_HTML_cell_text(column, data_name, general_type_name), @@ -822,13 +895,13 @@ public define String public define String make_list_view_row ( - DB_Table table, //table to format + HK_Table table, //table to format String data_name, //name of data String indent, //indentation string to use HK_List_View list_view //list view to generate )= - since table is db_table(table_name, model, _), //get name and model of the table - since model is db_model( columns, _), //get all columns of table + since table is hk_table(table_name, model, _), //get name and model of the table + since model is hk_model( columns, _), //get all columns of table // since display is db_display(views, _), // with current_list_column = if list_view is { -- libgit2 0.21.4