cubicweb/server/sqlutils.py
changeset 11413 c172fa18565e
parent 11412 ac166217bd8c
child 11417 5e5e224239c3
equal deleted inserted replaced
11412:ac166217bd8c 11413:c172fa18565e
   113 
   113 
   114 
   114 
   115 def sqlgrants(schema, driver, user,
   115 def sqlgrants(schema, driver, user,
   116               text_index=True, set_owner=True,
   116               text_index=True, set_owner=True,
   117               skip_relations=(), skip_entities=()):
   117               skip_relations=(), skip_entities=()):
   118     """return sql to give all access privileges to the given user on the system
   118     """Return a list of SQL statements to give all access privileges to the given user on the
   119     schema
   119     database.
   120     """
   120     """
   121     from cubicweb.server.schema2sql import grant_schema
   121     from cubicweb.server.schema2sql import grant_schema
   122     from cubicweb.server.sources import native
   122     from cubicweb.server.sources import native
   123     output = []
   123     stmts = list(native.grant_schema(user, set_owner))
   124     w = output.append
       
   125     w(native.grant_schema(user, set_owner))
       
   126     w('')
       
   127     if text_index:
   124     if text_index:
   128         dbhelper = db.get_db_helper(driver)
   125         dbhelper = db.get_db_helper(driver)
   129         w(dbhelper.sql_grant_user_on_fti(user))
   126         # XXX should return a list of sql statements rather than ';' joined statements
   130         w('')
   127         stmts += dbhelper.sql_grant_user_on_fti(user).split(';')
   131     w(grant_schema(schema, user, set_owner, skip_entities=skip_entities, prefix=SQL_PREFIX))
   128     stmts += grant_schema(schema, user, set_owner, skip_entities=skip_entities, prefix=SQL_PREFIX)
   132     return '\n'.join(output)
   129     return stmts
   133 
   130 
   134 
   131 
   135 def sqlschema(schema, driver, text_index=True,
   132 def sqlschema(schema, driver, text_index=True,
   136               user=None, set_owner=False,
   133               user=None, set_owner=False,
   137               skip_relations=PURE_VIRTUAL_RTYPES, skip_entities=()):
   134               skip_relations=PURE_VIRTUAL_RTYPES, skip_entities=()):
   138     """return the system sql schema, according to the given parameters"""
   135     """Return the database SQL schema as a list of SQL statements, according to the given parameters.
       
   136     """
   139     from cubicweb.server.schema2sql import schema2sql
   137     from cubicweb.server.schema2sql import schema2sql
   140     from cubicweb.server.sources import native
   138     from cubicweb.server.sources import native
   141     if set_owner:
   139     if set_owner:
   142         assert user, 'user is argument required when set_owner is true'
   140         assert user, 'user is argument required when set_owner is true'
   143     output = []
   141     stmts = list(native.sql_schema(driver))
   144     w = output.append
       
   145     w(native.sql_schema(driver))
       
   146     w('')
       
   147     dbhelper = db.get_db_helper(driver)
   142     dbhelper = db.get_db_helper(driver)
   148     if text_index:
   143     if text_index:
   149         w(dbhelper.sql_init_fti().replace(';', ';;'))
   144         stmts += dbhelper.sql_init_fti().split(';')  # XXX
   150         w('')
   145     stmts += schema2sql(dbhelper, schema, prefix=SQL_PREFIX,
   151     w(schema2sql(dbhelper, schema, prefix=SQL_PREFIX,
   146                         skip_entities=skip_entities,
   152                  skip_entities=skip_entities,
   147                         skip_relations=skip_relations)
   153                  skip_relations=skip_relations).replace(';', ';;'))
       
   154     if dbhelper.users_support and user:
   148     if dbhelper.users_support and user:
   155         w('')
   149         stmts += sqlgrants(schema, driver, user, text_index, set_owner,
   156         w(sqlgrants(schema, driver, user, text_index, set_owner,
   150                            skip_relations, skip_entities)
   157                     skip_relations, skip_entities).replace(';', ';;'))
   151     return stmts
   158     return '\n'.join(output)
       
   159 
   152 
   160 
   153 
   161 _SQL_DROP_ALL_USER_TABLES_FILTER_FUNCTION = re.compile('^(?!(sql|pg)_)').match
   154 _SQL_DROP_ALL_USER_TABLES_FILTER_FUNCTION = re.compile('^(?!(sql|pg)_)').match
   162 def sql_drop_all_user_tables(driver_or_helper, sqlcursor):
   155 def sql_drop_all_user_tables(driver_or_helper, sqlcursor):
   163     """Return ths sql to drop all tables found in the database system."""
   156     """Return ths sql to drop all tables found in the database system."""