No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mosql.py 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # -*- coding: utf-8 -*-
  2. ## @package Lodel.utils.mosql
  3. # @brief Helper function to use mosql library
  4. #
  5. # define create_database and drop_database Query to manipulate databases
  6. # define create, drop and alter Query to manipulate tables
  7. # define create_user, delete_user and grant to manipulate user and rights
  8. from mosql.util import Clause, Statement, Query, value, identifier, concat_by_comma, paren, raw
  9. def col_def(cols):
  10. print("cols", cols)
  11. ret = []
  12. for col in cols:
  13. print("col", col)
  14. name, definition = col
  15. ret.append(identifier(name) + ' ' + raw(definition))
  16. return ret
  17. # chain definitions
  18. column_list = (col_def, concat_by_comma, paren)
  19. # Clauses
  20. create_clause = Clause('create table', alias='create', no_argument=True)
  21. if_not_exists = Clause('if not exists', alias='if_not_exists', no_argument=True, default=True)
  22. table_clause = Clause('table', (identifier, ), alias='table', hidden=True)
  23. character_clause = Clause('CHARACTER SET', (value, ), alias='character')
  24. column_clause = Clause('column', column_list, alias='column', hidden=True)
  25. alter_clause = Clause('alter table', (identifier, ), alias='table')
  26. add_clause = Clause('add column', (col_def, ), alias='column')
  27. # Statements
  28. create_statement = Statement([create_clause, if_not_exists, table_clause, column_clause, character_clause])
  29. alter_add_statement = Statement([alter_clause, add_clause])
  30. # queries
  31. create = Query(create_statement, ('table', 'column', 'if_not_exists'), {'create':True})
  32. alter_add = Query(alter_add_statement, ('table', 'column'))