SQL_get_count.anubis 1.51 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 25/03/2017
 * Time: 01:57
 * © Calexium 
 */

read hayamiki_lib/types/hayamiki.anubis
read tools/basis.anubis
read tools/streams.anubis
read system/string.anubis


public define Maybe(One)
  generate_get_count_by_clause
  (
    Stream    stream,
    HK_Table  table
  )=
  since table is hk_table(name, short_name, model, _),
  since model is hk_model(columns, _),
  with type_name = to_upper(name, 1), //make the first character to upper case to able to be a Anubis Type.
  
  //generate the type of the table that contain all components 
  if write_string(stream, 
     //SQL query
     "public define Int\n"+
     "  get_count_"+name+"_by_clause\n"+
     "  (\n"+
     "    SQLite3DataBase  db,\n"+
     "    String           clause\n"+
     "  )=\n"+
     "  with final_clause = if clause = \"\" then\n"+
     "                         \"\"\n"+
     "                      else\n"+
     "                        \"WHERE \"+clause,\n"+
     "\n"+
     "  if sql_query_timeout(db, \"SELECT COUNT(\\\"id\\\") FROM "+name+" \"+final_clause+\";\", [], \"get_count_"+name+"_by_clause\") is\n"+
     "  {\n"+
     "    error(_)         then 0,\n"+
     "    ok(_, cursor, _) then db_get_integer(cursor)\n"+
     "  }\n"+
     ".\n\n"
     
     ) is                                                           //end of the function
  {
    failure     then  println("can't write generate_get_all "+type_name); failure,
    success(_)  then  success(unique)
  }
.