check.anubis 7.51 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ 
 * Date: 04/02/2016
 * Time: 22:34
 * © Calexium 
 */

read hayamiki_lib/types/hayamiki.anubis
read system/string.anubis
read tools/list.anubis
read tools/basis.anubis
transmit hayamiki_lib/view/types/hk_editor.anubis

public define Maybe(List(String))
  get_all_apps_name
  (
    List(HK_App)  apps,
    List(String)  so_far
  )=
  if apps is
  {
    []      then  success(so_far),
    [h . t] then
      since h is hk_app(name, _, _, _),
      if name:so_far then
        println("The app name "+name+" already exists"); failure
      //'this' is not allowed as app name because the  
      //else name = "this" then
      //  prinln("error: 'this' is forbidden app name "); failure
      else
        get_all_apps_name(t, [name. so_far])
  }.


define Maybe(HK_Model_Column)
  resolve_foreign
  (
    List(String)    apps_name,          //list of all knows application
    String          current_app_name,   
    String          table_name,         //current table name
    HK_Model_Column col
  )=
  since col is hk_column(name, type, attrib, help),
  if type is foreign_key(fk_app, fk_table, fields)  then
  //TODO check for searchable fields if exists in foreign table
    with mb_fk_app =  if (fk_app) is
                      {
                        this            then println("this found replaced by "+current_app_name+" in app '"+current_app_name+"' table '"+table_name+"' column '"+name+"'");
                          success(current_app_name),
                        app_name(_name)  then
                          if _name:apps_name then 
                            success(_name) 
                          else
                            println("foreign key app '"+_name+"' doesn't exists in app '"+current_app_name+"' table '"+table_name+"' column '"+_name+"'");failure,
                        none            then
                          success("")
                      },
    if mb_fk_app is
    {
      failure           then failure,
      success(n_fk_app) then success(hk_column(name, foreign_key(if (fk_app) is app_name(_) then app_name(n_fk_app) else fk_app, (if length(n_fk_app)>0 then n_fk_app+"_"else "")+fk_table, fields), attrib, help))
    }
  else
    success(col).
  
define Maybe(List(HK_Model_Column))
  resolve_foreign
  (
    List(String)    apps_name,
    String          app_name,
    String          table_name,
    List(HK_Model_Column) columns,
    List(HK_Model_Column) so_far
  )=
  if columns is
  {
    []      then  success(reverse(so_far)),
    [h . t] then  
      if resolve_foreign(apps_name, app_name, table_name, h) is 
      {
        failure       then failure,
        success(col)  then resolve_foreign(apps_name, app_name, table_name, t, [col. so_far])
      }
  }.

define Maybe(HK_FK_View)
  resolve_HK_FK_View_foreign
  (
    List(String)    apps_name,          //list of all knows application
    String          current_app_name,   
    HK_FK_View      _fk_view
  )=
  since _fk_view is fk_view(fk_app, fk_table, fk_column),
    with mb_fk_app =  if (fk_app) is
                      {
                        this            then println("this found replaced by "+current_app_name+" in app '"+current_app_name+"' table '"+fk_table+"' column '"+fk_column+"'");
                          success(current_app_name),
                        app_name(_name)  then
                          if _name:apps_name then 
                            success(_name) 
                          else
                            println("foreign key app '"+_name+"' doesn't exists in app '"+current_app_name+"' table '"+fk_table+"' column '"+fk_column+"'");failure,
                        none            then
                          success("")
                      },
    if mb_fk_app is
    {
      failure           then failure,
      success(n_fk_app) then success(fk_view(fk_app, (if length(n_fk_app)>0 then n_fk_app+"_"else "")+fk_table, fk_column))
    }
.

define Maybe(HK_Editor)
  resolve_HK_Edit_View_foreign
  (
    List(String)  apps_name,  //list of all knows app name
    String        app_name,   //current app name
    HK_Editor  _edit_view
  )=
  if _edit_view is
  {
    edit_view_all             then success(edit_view_all),
    edit_view_list_form(v, c, fk_views) then 
      with mb_fk_views = map_escape((HK_FK_View _fk_view) |-> resolve_HK_FK_View_foreign(apps_name, app_name, _fk_view), fk_views),
      if mb_fk_views is
      {
        failure             then  failure,
        success(n_fk_views) then  success(edit_view_list_form(v, c, n_fk_views))
      }
    form_table(v, t)        then  success(form_table(v, t))
  }
.

define Maybe(List(HK_Editor))
  resolve_foreign
  (
    List(String)        apps_name,  //list of all knows app name
    String              app_name,   //current app name
    List(HK_Editor)  list_edits, //edit view to check
    List(HK_Editor)  so_far      //already checked edit view
  )=
  if list_edits is
  {
    []      then  success(reverse(so_far)),
    [h . t] then
      if resolve_HK_Edit_View_foreign(apps_name, app_name, h) is 
      {
        failure             then failure,
        success(edit_view)  then resolve_foreign(apps_name, app_name, t, [edit_view. so_far])
      }
  }.
  
define Maybe(List(HK_Table))
  resolve_foreign
  (
    List(String)    apps_name,
    String          app_name,
    List(HK_Table)  tables,
    List(HK_Table)  so_far
  )=
  if tables is 
  {
    []      then success(reverse(so_far)),
    [h . t] then
      since h is hk_table(name, short_name, model, display),
      since model is hk_model(columns, constraints, display_string),
      with table_name = if length(app_name) > 0 then app_name +"_"+name else name,
      if resolve_foreign(apps_name, app_name, table_name, columns, []) is
      {
        failure                 then failure,
        success(checked_model)  then 
          since display is   hk_display( list_views,  list_edits),
          if resolve_foreign(apps_name, app_name, list_edits, []) is
          {
            failure                     then  failure,
            success(checked_list_edits) then 
              resolve_foreign(apps_name, app_name, t, [hk_table(table_name, name, hk_model(checked_model, constraints, display_string), hk_display(list_views, checked_list_edits)) . so_far])
          }
          
      }
  }.
  
define Maybe(HK_App)
  resolve_foreign
  (
    List(String)  apps_name,
    HK_App        app
  )=
  since app is hk_app(name, _, _, tables),
  if resolve_foreign(apps_name, name, tables, []) is 
  {
    failure                   then  failure,
    success(resolved_tables)  then  success(hk_app(name, resolved_tables))
  }.
  
define Maybe(List(HK_App))
  resolve_foreign
  (
    List(String)  apps_name,
    List(HK_App)  apps,
    List(HK_App)  so_far
  )=
  if apps is
  {
    []      then success(reverse(so_far)),
    [h . t] then 
      if resolve_foreign(apps_name, h) is
      {
        failure       then failure,
        success(app)  then resolve_foreign(apps_name, t, [app . so_far])
      }
  }.
  
public define Maybe(HK_Database)
  check_database
  (
    HK_Database current_db  //database model description
  )=
  since current_db is hk_database(name, apps),
  
  if get_all_apps_name(apps, []) is
  {
    failure           then failure,
    success(chk_apps) then
    //TODO Check for reserved keyword used in column name, table name
    if resolve_foreign(chk_apps, apps, []) is
    {
      failure                 then failure,
      success(resolved_apps)  then
        //with checked_db = current_db,
        success(hk_database(name, resolved_apps))
      
    }
  }.