Commit f8f2a4815245ca80e8f93b183199f55ecb096a14

Authored by totoro
1 parent 2c83047c

generate the list of table for create_table function

+ generate an empty db_relations which is a list of foreign keys and their relations
+ create directories if need
calexium_lib @ ec6480d924e
1 -Subproject commit eba0f762de01ad8cd7c4aa41ea78c9f641d74e03 1 +Subproject commit ec6480d924ed1291d9c5017b8f878104544ae1a6
@@ -472,6 +472,7 @@ @@ -472,6 +472,7 @@
472 <Compile Include="src\generation\create_table.anubis" /> 472 <Compile Include="src\generation\create_table.anubis" />
473 <Compile Include="src\generation\files.anubis" /> 473 <Compile Include="src\generation\files.anubis" />
474 <Compile Include="src\generation\header.anubis" /> 474 <Compile Include="src\generation\header.anubis" />
  475 + <Compile Include="src\generation\menu.anubis" />
475 <Compile Include="src\generation\types.anubis" /> 476 <Compile Include="src\generation\types.anubis" />
476 <Compile Include="src\main.anubis" /> 477 <Compile Include="src\main.anubis" />
477 <Compile Include="src\model.3.0.0.anubis" /> 478 <Compile Include="src\model.3.0.0.anubis" />
@@ -42,7 +42,7 @@ define Maybe(HK_Model_Column) @@ -42,7 +42,7 @@ define Maybe(HK_Model_Column)
42 )= 42 )=
43 since col is hk_column(name, type, attrib, help), 43 since col is hk_column(name, type, attrib, help),
44 if type is foreign_key(fk_app, fk_table) then 44 if type is foreign_key(fk_app, fk_table) then
45 - with mb_fk_app = if (fk_app) is 45 + with mb_fk_app = if (fk_app) is
46 { 46 {
47 this then println("this found replaced by "+current_app_name+" in app '"+current_app_name+"' table '"+table_name+"' column '"+name+"'"); 47 this then println("this found replaced by "+current_app_name+" in app '"+current_app_name+"' table '"+table_name+"' column '"+name+"'");
48 success(current_app_name), 48 success(current_app_name),
@@ -145,7 +145,8 @@ public define Maybe(HK_Database) @@ -145,7 +145,8 @@ public define Maybe(HK_Database)
145 if get_all_apps_name(apps, []) is 145 if get_all_apps_name(apps, []) is
146 { 146 {
147 failure then failure, 147 failure then failure,
148 - success(chk_apps) then 148 + success(chk_apps) then
  149 + //TODO Check for reserved keyword used in column name, table name
149 if resolve_foreign(chk_apps, apps, []) is 150 if resolve_foreign(chk_apps, apps, []) is
150 { 151 {
151 failure then failure, 152 failure then failure,
src/generation/create_table.anubis
@@ -11,6 +11,7 @@ read calexium_lib/database/model/db_model.anubis @@ -11,6 +11,7 @@ read calexium_lib/database/model/db_model.anubis
11 read tools/basis.anubis 11 read tools/basis.anubis
12 read tools/int.anubis 12 read tools/int.anubis
13 read system/string.anubis 13 read system/string.anubis
  14 +read system/files.anubis
14 read tools/streams.anubis 15 read tools/streams.anubis
15 read tools/ISO-8601.anubis 16 read tools/ISO-8601.anubis
16 17
@@ -30,6 +31,7 @@ define String @@ -30,6 +31,7 @@ define String
30 List(HK_Model_Column) columns, 31 List(HK_Model_Column) columns,
31 String indent 32 String indent
32 )= 33 )=
  34 + //get the longest column name size, to be able to make formatting according to column name.
33 with column_max_size = max(columns), 35 with column_max_size = max(columns),
34 join(",\n"+indent, map((HK_Model_Column column) |-> create_table_generate_one_column(column, column_max_size+1), columns)) 36 join(",\n"+indent, map((HK_Model_Column column) |-> create_table_generate_one_column(column, column_max_size+1), columns))
35 . 37 .
@@ -60,16 +62,18 @@ define String @@ -60,16 +62,18 @@ define String
60 )= 62 )=
61 if tables is 63 if tables is
62 { 64 {
63 - [] then 65 + [] then //no more table to generate
64 if apps is 66 if apps is
65 { 67 {
66 - [] then so_far,  
67 - [app. t_apps] then 68 + [] then //no more apps to generate
  69 + so_far, //return the all create table generated so far
  70 + [app. t_apps] then //another app available
68 since app is hk_app(name, app_tables), 71 since app is hk_app(name, app_tables),
69 create_table_generate_all_tables(t_apps, app_tables, so_far + "\n /************ TABLES for Application '"+name+"' ***************/") 72 create_table_generate_all_tables(t_apps, app_tables, so_far + "\n /************ TABLES for Application '"+name+"' ***************/")
70 }, 73 },
71 74
72 [table . t] then 75 [table . t] then
  76 + //generate the current table "CREATE TABLE"
73 create_table_generate_all_tables(apps, t, so_far + create_table_generate_one_table(table)) 77 create_table_generate_all_tables(apps, t, so_far + create_table_generate_one_table(table))
74 }. 78 }.
75 79
@@ -92,13 +96,15 @@ define String @@ -92,13 +96,15 @@ define String
92 generate_all_tables_list 96 generate_all_tables_list
93 ( 97 (
94 List(HK_App) apps, 98 List(HK_App) apps,
95 - List(HK_Table) tables, //list of the table model description 99 +// List(HK_Table) tables, //list of the table model description
96 String indent 100 String indent
97 )= 101 )=
98 //calculate then longest table name and add 4 for enclosed quote + coma + final space 102 //calculate then longest table name and add 4 for enclosed quote + coma + final space
99 //hence we can make a beautiful formatting 103 //hence we can make a beautiful formatting
100 with max_space = longest_table_name_size(apps, 0) + 4, 104 with max_space = longest_table_name_size(apps, 0) + 4,
101 - join(",\n",map((HK_Table table) |-> indent+"table("+fill("\""+table.name+"\",", max_space)+"create_table_"+table.name+")", tables)). 105 + join(",\n",
  106 + map_append((HK_App app) |-> since app is hk_app(_, tables),
  107 + map((HK_Table table) |-> indent+"table("+fill("\""+table.name+"\",", max_space)+"create_table_"+table.name+")", tables), apps)).
102 108
103 public define Maybe(One) 109 public define Maybe(One)
104 generate_create_table 110 generate_create_table
@@ -106,33 +112,39 @@ public define Maybe(One) @@ -106,33 +112,39 @@ public define Maybe(One)
106 List(HK_App) apps //list of the table model description 112 List(HK_App) apps //list of the table model description
107 )= 113 )=
108 //TODO manage version 114 //TODO manage version
109 - with version = "1.0.0", 115 + with version = "1.0.0.0",
110 version_str = find_and_replace(version,".","_"), //replace the dot by underscore because dot is not allowed in function name by Anubis language 116 version_str = find_and_replace(version,".","_"), //replace the dot by underscore because dot is not allowed in function name by Anubis language
111 117
112 - with file_name = "to_"+version+".anubis",  
113 - with script_file_name = "to_"+version+"_script.anubis", 118 + with file_name = "database/generated/migration/to_"+version+".anubis",
  119 + with script_file_name = "database/generated/migration/to_"+version+"_scripts.anubis",
  120 +
  121 + //create all necessary directories if doesn't exist.
  122 + make_directories(file_name);
114 123
115 - //create script file name 124 + //create script file name as new file. Previous file with same name will be erased
116 if file(script_file_name, new) is 125 if file(script_file_name, new) is
117 { 126 {
118 - failure then println("Can't create script "+script_file_name);failure,  
119 - success(fd_script) then 127 + failure then println("Can't create script "+script_file_name);failure,
  128 + success(fd_script) then
120 with script_stream = make_stream(fd_script), 129 with script_stream = make_stream(fd_script),
121 //write into script file the generated create table 130 //write into script file the generated create table
122 if write_string(script_stream, 131 if write_string(script_stream,
123 "/* 132 "/*
124 * Created by 早見木 (Hayamiki). 133 * Created by 早見木 (Hayamiki).
125 - * User: Automat 134 + * User: Automaton
126 * Date: "+_Int_to_ISO_8601_date(now)+" 135 * Date: "+_Int_to_ISO_8601_date(now)+"
127 * Time: "+_Int_to_ISO_8601_time(now)+" 136 * Time: "+_Int_to_ISO_8601_time(now)+"
128 * 137 *
129 */ 138 */
130 -read data_base/alter_table.anubis 139 +read data_base/sqlite_foreign_key.anubis
131 read data_base/db_types.anubis\n"+ 140 read data_base/db_types.anubis\n"+
132 //call the generation of all tables 141 //call the generation of all tables
133 create_table_generate_all_tables(apps, [], "")+ 142 create_table_generate_all_tables(apps, [], "")+
134 //TODO make indexes 143 //TODO make indexes
135 - "\npublic define List(String) db_indexes = [].\n" 144 + "\npublic define List(String) db_indexes = [].\n"+
  145 + "// ///////////////////////////////\n"+
  146 + "// Foreign keys \n"+
  147 + "public define List((String, String, String, String, FK_Null, FK_Cascade)) db_relations = [].\n"
136 ) is 148 ) is
137 { 149 {
138 failure then println ("Can't generate script "+script_file_name);failure, 150 failure then println ("Can't generate script "+script_file_name);failure,
@@ -146,7 +158,7 @@ read data_base/db_types.anubis\n&quot;+ @@ -146,7 +158,7 @@ read data_base/db_types.anubis\n&quot;+
146 write_string(stream, 158 write_string(stream,
147 "/* 159 "/*
148 * Created by 早見木 (Hayamiki). 160 * Created by 早見木 (Hayamiki).
149 - * User: Automat 161 + * User: Automaton
150 * Date: "+_Int_to_ISO_8601_date(now)+" 162 * Date: "+_Int_to_ISO_8601_date(now)+"
151 * Time: "+_Int_to_ISO_8601_time(now)+" 163 * Time: "+_Int_to_ISO_8601_time(now)+"
152 * 164 *
@@ -166,7 +178,7 @@ read types/app_types.anubis @@ -166,7 +178,7 @@ read types/app_types.anubis
166 read app/app_constants.anubis 178 read app/app_constants.anubis
167 read app/app_loggers.anubis 179 read app/app_loggers.anubis
168 180
169 -read database/generated/migration/to_"+version_str+"_scripts.anubis 181 +read database/generated/migration/to_"+version+"_scripts.anubis
170 182
171 define String _version = \""+version+"\". 183 define String _version = \""+version+"\".
172 184
@@ -179,7 +191,7 @@ public define Maybe(One) @@ -179,7 +191,7 @@ public define Maybe(One)
179 if drop_all_triggers(db, logger_debug) is failure then failure else 191 if drop_all_triggers(db, logger_debug) is failure then failure else
180 192
181 with tables = (List(DbTable)) [ 193 with tables = (List(DbTable)) [
182 -"+generate_all_tables_list(apps, [], " ")+" 194 +"+generate_all_tables_list(apps, " ")+"
183 ], 195 ],
184 if create_tables(db, logger_debug, tables, _version) is 196 if create_tables(db, logger_debug, tables, _version) is
185 { 197 {
src/generation/files.anubis
@@ -7,6 +7,7 @@ @@ -7,6 +7,7 @@
7 */ 7 */
8 8
9 read system/string.anubis 9 read system/string.anubis
  10 +read system/files.anubis
10 11
11 read calexium_lib/database/model/db_model.anubis 12 read calexium_lib/database/model/db_model.anubis
12 read tools/basis.anubis 13 read tools/basis.anubis
@@ -30,7 +31,11 @@ define Maybe(One) @@ -30,7 +31,11 @@ define Maybe(One)
30 with model = hk_model([hk_column("id", p_key, []). columns], show_string ), 31 with model = hk_model([hk_column("id", p_key, []). columns], show_string ),
31 with table = hk_table(name, model, display), 32 with table = hk_table(name, model, display),
32 println("Generating file for table ["+name+"]"); 33 println("Generating file for table ["+name+"]");
33 - with file_name = name+".anubis", 34 + with file_name = "database/generated/"+name+".anubis",
  35 +
  36 + //create all necessary directories if doesn't exist.
  37 + make_directories(file_name);
  38 +
34 if file(file_name, new) is 39 if file(file_name, new) is
35 { 40 {
36 failure then println("can't create file "+file_name);failure, 41 failure then println("can't create file "+file_name);failure,
@@ -86,15 +91,15 @@ define Maybe(One) @@ -86,15 +91,15 @@ define Maybe(One)
86 List(String) edit, 91 List(String) edit,
87 List(String) writer 92 List(String) writer
88 )= 93 )=
89 - with file_name = "vtm.anubis", 94 + with file_name = "database/generated/vtm.anubis",
90 if file(file_name, new) is 95 if file(file_name, new) is
91 { 96 {
92 failure then println("can't create file "+file_name);failure, 97 failure then println("can't create file "+file_name);failure,
93 success(fd) then 98 success(fd) then
94 if write_string(make_stream(fd), 99 if write_string(make_stream(fd),
95 "/* 100 "/*
96 - * Created by model_manager.  
97 - * User: Automat 101 + * Created by 早見木 (Hayamiki).
  102 + * User: Automaton
98 * Date: "+_Int_to_ISO_8601_date(now)+" 103 * Date: "+_Int_to_ISO_8601_date(now)+"
99 * Time: "+_Int_to_ISO_8601_time(now)+" 104 * Time: "+_Int_to_ISO_8601_time(now)+"
100 * 105 *
src/model.3.0.0.anubis
@@ -103,9 +103,8 @@ public define HK_Table production_macaddress_table = @@ -103,9 +103,8 @@ public define HK_Table production_macaddress_table =
103 hk_model( 103 hk_model(
104 [ 104 [
105 105
106 - hk_column("mac", char_field(20), []),  
107 - hk_column("index", integer_field, []),  
108 - hk_column("mailfountain_id", integer_field, []), 106 + hk_column("mac", char_field(20), []),
  107 + hk_column("mailfountain_id", integer_field, []),
109 ], 108 ],
110 "obj.mac+\" = MailFountain[\"+obj.mailfountain_id+\"]\"" 109 "obj.mac+\" = MailFountain[\"+obj.mailfountain_id+\"]\""
111 ), 110 ),
@@ -709,10 +708,6 @@ public define HK_Database @@ -709,10 +708,6 @@ public define HK_Database
709 [ 708 [
710 //domain manager 709 //domain manager
711 //mf_serial_table 710 //mf_serial_table
712 - hk_app("base",  
713 - [  
714 - settings_table,  
715 - ]),  
716 hk_app("production", 711 hk_app("production",
717 [ 712 [
718 production_mass_storage_table, 713 production_mass_storage_table,
@@ -743,6 +738,7 @@ public define HK_Database @@ -743,6 +738,7 @@ public define HK_Database
743 ]), 738 ]),
744 hk_app("", 739 hk_app("",
745 [ 740 [
  741 + settings_table,
746 django_admin_log_table, 742 django_admin_log_table,
747 address_book_contact_table 743 address_book_contact_table
748 ]) 744 ])