示例#1
0
    def _parse_format_columns(self, data, mode=None):
        """
        data:
            Data coming from client side

        Returns:
            This function will parse and return formatted list of columns
            added by user
        """
        columns = data['columns']
        # 'EDIT' mode
        if mode is not None:
            for action in ['added', 'changed']:
                if action in columns:
                    final_columns = []
                    for c in columns[action]:
                        if 'inheritedfrom' not in c:
                            final_columns.append(c)

                    for c in final_columns:
                        if 'attacl' in c:
                            c['attacl'] = parse_priv_to_db(
                                c['attacl'], self.column_acl
                            )

                        if 'cltype' in c:
                            # check type for '[]' in it
                            c['cltype'], c['hasSqrBracket'] = \
                                self._cltype_formatter(c['cltype'])

                        c = TableView.convert_length_precision_to_string(c)

                    data['columns'][action] = final_columns
        else:
            # We need to exclude all the columns which are inherited from other
            # tables 'CREATE' mode
            final_columns = []

            for c in columns:
                if 'inheritedfrom' not in c:
                    final_columns.append(c)

            # Now we have all lis of columns which we need
            # to include in our create definition, Let's format them
            for c in final_columns:
                if 'attacl' in c:
                    c['attacl'] = parse_priv_to_db(
                        c['attacl'], self.column_acl
                    )

                if 'cltype' in c:
                    # check type for '[]' in it
                    c['cltype'], c['hasSqrBracket'] = \
                        self._cltype_formatter(c['cltype'])

                c = TableView.convert_length_precision_to_string(c)

            data['columns'] = final_columns

        return data
示例#2
0
    def format_request_acls(self, data, modified=False, specific=None):
        acls = {}
        try:
            acls = render_template(
                "/".join([self.template_path, 'allowed_privs.json'])
            )
            acls = json.loads(acls)
        except Exception as e:
            current_app.logger.exception(e)

        # Privileges
        for aclcol in acls:
            if specific is not None:
                if aclcol not in specific:
                    continue
            if aclcol in data:
                allowedacl = acls[aclcol]
                if modified:
                    for modifier in ['added', 'changed', 'deleted']:
                        if modifier in data[aclcol]:
                            data[aclcol][modifier] = parse_priv_to_db(
                                data[aclcol][modifier], allowedacl['acl']
                            )
                else:
                    data[aclcol] = parse_priv_to_db(data[aclcol], allowedacl['acl'])

        return acls
示例#3
0
    def get_sql(self, data, lid=None):
        """
        This function will generate sql from model data.

        Args:
            data: Contains the data of the selected language node.
            lid: Language ID
        """
        required_args = [
            'name', 'lanowner', 'description'
        ]

        if lid is not None:
            sql = render_template(
                "/".join([self.template_path, 'properties.sql']), lid=lid
            )
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)

            if len(res['rows']) == 0:
                return gone(
                    gettext("Could not find the language information.")
                )

            for key in ['lanacl']:
                if key in data and data[key] is not None:
                    if 'added' in data[key]:
                        data[key]['added'] = parse_priv_to_db(
                            data[key]['added'], ["U"]
                        )
                    if 'changed' in data[key]:
                        data[key]['changed'] = parse_priv_to_db(
                            data[key]['changed'], ["U"]
                        )
                    if 'deleted' in data[key]:
                        data[key]['deleted'] = parse_priv_to_db(
                            data[key]['deleted'], ["U"]
                        )

            old_data = res['rows'][0]
            for arg in required_args:
                if arg not in data:
                    data[arg] = old_data[arg]
            sql = render_template(
                "/".join([self.template_path, 'update.sql']),
                data=data, o_data=old_data, conn=self.conn
            )
            return sql.strip('\n'), data['name'] if 'name' in data \
                else old_data['name']
        else:

            if 'lanacl' in data:
                data['lanacl'] = parse_priv_to_db(data['lanacl'], ["U"])

            sql = render_template("/".join([self.template_path, 'create.sql']),
                                  data=data, conn=self.conn)
            return sql.strip('\n'), data['name']
示例#4
0
    def get_sql(self, gid, sid, data, tsid=None):
        """
        This function will genrate sql from model/properties data
        """
        required_args = [
            'name'
        ]

        if tsid is not None:
            SQL = render_template(
                "/".join([self.template_path, 'properties.sql']),
                tsid=tsid, conn=self.conn
            )
            status, res = self.conn.execute_dict(SQL)
            if not status:
                return internal_server_error(errormsg=res)

            # Making copy of output for further processing
            old_data = dict(res['rows'][0])
            old_data = self._formatter(old_data, tsid)

            # To format privileges data coming from client
            for key in ['spcacl']:
                if key in data and data[key] is not None:
                    if 'added' in data[key]:
                        data[key]['added'] = parse_priv_to_db(data[key]['added'], self.acl)
                    if 'changed' in data[key]:
                        data[key]['changed'] = parse_priv_to_db(data[key]['changed'], self.acl)
                    if 'deleted' in data[key]:
                        data[key]['deleted'] = parse_priv_to_db(data[key]['deleted'], self.acl)

            # If name is not present with in update data then copy it
            # from old data
            for arg in required_args:
                if arg not in data:
                    data[arg] = old_data[arg]

            SQL = render_template(
                "/".join([self.template_path, 'update.sql']),
                data=data, o_data=old_data
            )
        else:
            # To format privileges coming from client
            if 'spcacl' in data:
                data['spcacl'] = parse_priv_to_db(data['spcacl'], self.acl)
            # If the request for new object which do not have tsid
            SQL = render_template(
                "/".join([self.template_path, 'create.sql']),
                data=data
            )
            SQL += "\n"
            SQL += render_template(
                "/".join([self.template_path, 'alter.sql']),
                data=data, conn=self.conn
            )
        SQL = re.sub('\n{2,}', '\n\n', SQL)
        return SQL
示例#5
0
    def getSQL(self, gid, sid, did, data, scid, seid=None):
        """
        This function will generate sql from model data.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            scid: Schema ID
            seid: Sequence ID
        """

        required_args = [
            u'name'
        ]

        if seid is not None:
            SQL = render_template("/".join([self.template_path, 'properties.sql']), scid=scid, seid=seid)
            status, res = self.conn.execute_dict(SQL)
            if not status:
                return internal_server_error(errormsg=res)
            # Making copy of output for further processing
            old_data = dict(res['rows'][0])
            old_data = self._formatter(old_data, scid, seid)

            # To format privileges data coming from client
            for key in ['relacl']:
                if key in data and data[key] is not None:
                    if 'added' in data[key]:
                        data[key]['added'] = parse_priv_to_db(data[key]['added'], self.acl)
                    if 'changed' in data[key]:
                        data[key]['changed'] = parse_priv_to_db(data[key]['changed'], self.acl)
                    if 'deleted' in data[key]:
                        data[key]['deleted'] = parse_priv_to_db(data[key]['deleted'], self.acl)

            # If name is not present with in update data then copy it
            # from old data
            for arg in required_args:
                if arg not in data:
                    data[arg] = old_data[arg]
            SQL = render_template("/".join([self.template_path, 'update.sql']),
                                  data=data, o_data=old_data, conn=self.conn)
        else:
            # To format privileges coming from client
            if 'relacl' in data:
                data['relacl'] = parse_priv_to_db(data['relacl'], self.acl)

            SQL = render_template("/".join([self.template_path, 'create.sql']), data=data, conn=self.conn)
            SQL += render_template("/".join([self.template_path, 'grant.sql']), data=data, conn=self.conn)
        return SQL
示例#6
0
    def get_online_sql(self, gid, sid, data, did=None):
        """
        Generates sql for altering database which don not require
        database to be disconnected before applying.
        """
        acls = []
        try:
            acls = render_template(
                "/".join([self.template_path, 'allowed_privs.json'])
            )
            acls = json.loads(acls, encoding='utf-8')
        except Exception as e:
            current_app.logger.exception(e)

        # Privileges
        for aclcol in acls:
            if aclcol in data:
                allowedacl = acls[aclcol]

                for key in ['added', 'changed', 'deleted']:
                    if key in data[aclcol]:
                        data[aclcol][key] = parse_priv_to_db(
                            data[aclcol][key], allowedacl['acl']
                        )

        return render_template(
            "/".join([self.template_path, 'alter_online.sql']),
            data=data, conn=self.conn
        )
示例#7
0
    def create(self, gid, sid, did):
        """
        This function will create the language object

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
        """
        required_args = [
            'name'
        ]

        data = request.form if request.form else json.loads(
            request.data, encoding='utf-8'
        )
        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=410,
                    success=0,
                    errormsg=gettext(
                        "Could not find the required parameter (%s)." % arg
                    )
                )

        try:
            if 'lanacl' in data:
                data['lanacl'] = parse_priv_to_db(data['lanacl'], ['U'])

            sql = render_template("/".join([self.template_path, 'create.sql']),
                                  data=data, conn=self.conn)
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)

            sql = render_template(
                "/".join([self.template_path, 'properties.sql']),
                lanname=data['name'], conn=self.conn
            )

            status, r_set = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=r_set)

            for row in r_set['rows']:
                return jsonify(
                    node=self.blueprint.generate_browser_node(
                        row['oid'],
                        did,
                        row['name'],
                        icon='icon-language'
                    )
                )

        except Exception as e:
            return internal_server_error(errormsg=str(e))
示例#8
0
    def sql(self, gid, sid, did, fid, fsid):
        """
        This function will generate sql to show it in sql pane for the selected foreign server node.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            fid: Foreign data wrapper ID
            fsid: Foreign server ID
        """

        sql = render_template("/".join([self.template_path, 'properties.sql']), fsid=fsid, conn=self.conn)
        status, res = self.conn.execute_dict(sql)
        if not status:
            return internal_server_error(errormsg=res)

        if res['rows'][0]['fsrvoptions'] is not None:
            res['rows'][0]['fsrvoptions'] = self.tokenizeOptions(res['rows'][0]['fsrvoptions'])

        sql = render_template("/".join([self.template_path, 'acl.sql']), fsid=fsid)
        status, fs_rv_acl_res = self.conn.execute_dict(sql)
        if not status:
            return internal_server_error(errormsg=fs_rv_acl_res)

        for row in fs_rv_acl_res['rows']:
            privilege = parse_priv_from_db(row)
            if row['deftype'] in res['rows'][0]:
                res['rows'][0][row['deftype']].append(privilege)
            else:
                res['rows'][0][row['deftype']] = [privilege]

        # To format privileges
        if 'fsrvacl' in res['rows'][0]:
            res['rows'][0]['fsrvacl'] = parse_priv_to_db(res['rows'][0]['fsrvacl'], ['U'])

        sql = render_template("/".join([self.template_path, 'properties.sql']), fdwid=fid, conn=self.conn)
        status, res1 = self.conn.execute_dict(sql)
        if not status:
            return internal_server_error(errormsg=res1)

        fdw_data = res1['rows'][0]

        sql = ''
        sql = render_template("/".join([self.template_path, 'create.sql']), data=res['rows'][0], fdwdata=fdw_data,
                              conn=self.conn)
        sql += "\n"

        sql_header = """-- Foreign Server: {0}

-- DROP SERVER {0}

""".format(res['rows'][0]['name'])

        sql = sql_header + sql

        return ajax_response(response=sql)
示例#9
0
    def get_sql(self, data, lid=None):
        """
        This function will generate sql from model data.

        Args:
            data: Contains the data of the selected language node.
            lid: Language ID
        """
        required_args = [
            'name', 'lanowner', 'description'
        ]
        try:
            sql = ''
            if lid is not None:
                sql = render_template("/".join([self.template_path, 'properties.sql']), lid=lid)
                status, res = self.conn.execute_dict(sql)
                if not status:
                    return internal_server_error(errormsg=res)

                for key in ['lanacl']:
                    if key in data and data[key] is not None:
                        if 'added' in data[key]:
                            data[key]['added'] = parse_priv_to_db(data[key]['added'])
                        if 'changed' in data[key]:
                            data[key]['changed'] = parse_priv_to_db(data[key]['changed'])
                        if 'deleted' in data[key]:
                            data[key]['deleted'] = parse_priv_to_db(data[key]['deleted'])

                old_data = res['rows'][0]
                for arg in required_args:
                    if arg not in data:
                        data[arg] = old_data[arg]
                sql = render_template("/".join([self.template_path, 'update.sql']), data=data,
                                      o_data=old_data, conn=self.conn)
            return sql
        except Exception as e:
            return internal_server_error(errormsg=str(e))
示例#10
0
    def sql(self, gid, sid, tsid):
        """
        This function will generate sql for sql panel
        """
        SQL = render_template(
            "/".join([self.template_path, 'properties.sql']),
            tsid=tsid, conn=self.conn
        )
        status, res = self.conn.execute_dict(SQL)
        if not status:
            return internal_server_error(errormsg=res)

        if len(res['rows']) == 0:
            return gone(
                _("Could not find the tablespace on the server.")
            )
        # Making copy of output for future use
        old_data = dict(res['rows'][0])

        old_data = self._formatter(old_data, tsid)

        # To format privileges
        if 'spcacl' in old_data:
            old_data['spcacl'] = parse_priv_to_db(old_data['spcacl'], self.acl)

        SQL = ''
        # We are not showing create sql for system tablespace
        if not old_data['name'].startswith('pg_'):
            SQL = render_template(
                "/".join([self.template_path, 'create.sql']),
                data=old_data
            )
            SQL += "\n"
        SQL += render_template(
            "/".join([self.template_path, 'alter.sql']),
            data=old_data, conn=self.conn
        )

        sql_header = u"""
-- Tablespace: {0}

-- DROP TABLESPACE {0};

""".format(old_data['name'])

        SQL = sql_header + SQL
        SQL = re.sub('\n{2,}', '\n\n', SQL)
        return ajax_response(response=SQL.strip('\n'))
示例#11
0
    def get_new_sql(self, gid, sid, data, did=None):
        """
        Generates sql for creating new database.
        """
        required_args = [
            u'name'
        ]

        for arg in required_args:
            if arg not in data:
                return _(" -- definition incomplete")

        acls = []
        SQL_acl = ''

        try:
            acls = render_template(
                "/".join([self.template_path, 'allowed_privs.json'])
            )
            acls = json.loads(acls, encoding='utf-8')
        except Exception as e:
            current_app.logger.exception(e)

        # Privileges
        for aclcol in acls:
            if aclcol in data:
                allowedacl = acls[aclcol]
                data[aclcol] = parse_priv_to_db(
                    data[aclcol], allowedacl['acl']
                )

        SQL_acl = render_template(
            "/".join([self.template_path, 'grant.sql']),
            data=data,
            conn=self.conn
        )

        SQL = render_template(
            "/".join([self.template_path, 'create.sql']),
            data=data, conn=self.conn
        )
        SQL += "\n"
        SQL += SQL_acl
        return SQL
示例#12
0
    def getSQL(self, gid, sid, did, data, scid, seid=None):
        """
        This function will generate sql from model data.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            scid: Schema ID
            seid: Sequence ID
        """

        required_args = [
            u'name'
        ]

        if seid is not None:
            SQL = render_template(
                "/".join([self.template_path, 'properties.sql']),
                scid=scid, seid=seid
            )
            status, res = self.conn.execute_dict(SQL)
            if not status:
                return internal_server_error(errormsg=res)
            if len(res['rows']) == 0:
                return gone(_("Could not find the sequence in the database."))

            # Making copy of output for further processing
            old_data = dict(res['rows'][0])
            old_data = self._formatter(old_data, scid, seid)

            # To format privileges data coming from client
            for key in ['relacl']:
                if key in data and data[key] is not None:
                    if 'added' in data[key]:
                        data[key]['added'] = parse_priv_to_db(
                            data[key]['added'], self.acl
                        )
                    if 'changed' in data[key]:
                        data[key]['changed'] = parse_priv_to_db(
                            data[key]['changed'], self.acl
                        )
                    if 'deleted' in data[key]:
                        data[key]['deleted'] = parse_priv_to_db(
                            data[key]['deleted'], self.acl
                        )

            # If name is not present with in update data then copy it
            # from old data
            for arg in required_args:
                if arg not in data:
                    data[arg] = old_data[arg]
            SQL = render_template(
                "/".join([self.template_path, 'update.sql']),
                data=data, o_data=old_data, conn=self.conn
            )
            return SQL, data['name'] if 'name' in data else old_data['name']
        else:
            # To format privileges coming from client
            if 'relacl' in data:
                data['relacl'] = parse_priv_to_db(data['relacl'], self.acl)

            SQL = render_template(
                "/".join([self.template_path, 'create.sql']),
                data=data, conn=self.conn
            )
            SQL += render_template(
                "/".join([self.template_path, 'grant.sql']),
                data=data, conn=self.conn
            )
            return SQL, data['name']
示例#13
0
    def create(self, gid, sid):
        """
        This function will creates new the tablespace object
        """

        required_args = {
            'name': 'Name',
            'spclocation': 'Location'
        }

        data = request.form if request.form else json.loads(
            request.data, encoding='utf-8'
        )

        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=410,
                    success=0,
                    errormsg=gettext(
                        "Could not find the required parameter (%s)." %
                        required_args[arg]
                    )
                )

        # To format privileges coming from client
        if 'spcacl' in data:
            data['spcacl'] = parse_priv_to_db(data['spcacl'], ['C'])

        try:
            SQL = render_template(
                "/".join([self.template_path, 'create.sql']),
                data=data, conn=self.conn
            )

            status, res = self.conn.execute_scalar(SQL)

            if not status:
                return internal_server_error(errormsg=res)
            SQL = render_template(
                "/".join([self.template_path, 'alter.sql']),
                data=data, conn=self.conn
            )

            # Checking if we are not executing empty query
            if SQL and SQL.strip('\n') and SQL.strip(' '):
                status, res = self.conn.execute_scalar(SQL)
                if not status:
                    return internal_server_error(errormsg=res)

            # To fetch the oid of newly created tablespace
            SQL = render_template(
                "/".join([self.template_path, 'alter.sql']),
                tablespace=data['name'], conn=self.conn
            )

            status, tsid = self.conn.execute_scalar(SQL)

            if not status:
                return internal_server_error(errormsg=tsid)

            return jsonify(
                node=self.blueprint.generate_browser_node(
                    tsid,
                    sid,
                    data['name'],
                    icon="icon-tablespace"
                )
            )
        except Exception as e:
            current_app.logger.exception(e)
            return internal_server_error(errormsg=str(e))
示例#14
0
    def get_sql(self, gid, sid, data, scid, tid=None):
        """
        This function will genrate sql from model data
        """
        if tid is not None:

            for key in ['typacl']:
                if key in data and data[key] is not None:
                    if 'added' in data[key]:
                        data[key]['added'] = parse_priv_to_db(
                            data[key]['added'], self.acl)
                    if 'changed' in data[key]:
                        data[key]['changed'] = parse_priv_to_db(
                            data[key]['changed'], self.acl)
                    if 'deleted' in data[key]:
                        data[key]['deleted'] = parse_priv_to_db(
                            data[key]['deleted'], self.acl)

            SQL = render_template(
                "/".join([self.template_path, 'properties.sql']),
                scid=scid,
                tid=tid,
                datlastsysoid=self.datlastsysoid,
                show_system_objects=self.blueprint.show_system_objects)
            status, res = self.conn.execute_dict(SQL)
            if not status:
                return internal_server_error(errormsg=res)

            # Making copy of output for future use
            old_data = dict(res['rows'][0])

            SQL = render_template("/".join([self.template_path, 'acl.sql']),
                                  scid=scid,
                                  tid=tid)
            status, acl = self.conn.execute_dict(SQL)
            if not status:
                return internal_server_error(errormsg=acl)

            # We will set get privileges from acl sql so we don't need
            # it from properties sql
            old_data['typacl'] = []

            for row in acl['rows']:
                priv = parse_priv_from_db(row)
                if row['deftype'] in old_data:
                    old_data[row['deftype']].append(priv)
                else:
                    old_data[row['deftype']] = [priv]

            # Calling function to check and additional properties if available
            old_data.update(self.additional_properties(old_data, tid))
            old_data = self._convert_for_sql(old_data)

            SQL = render_template("/".join([self.template_path, 'update.sql']),
                                  data=data,
                                  o_data=old_data,
                                  conn=self.conn)
        else:
            required_args = ['name', 'typtype']

            for arg in required_args:
                if arg not in data:
                    return " --definition incomplete"

            # Privileges
            if 'typacl' in data and data['typacl'] is not None:
                data['typacl'] = parse_priv_to_db(data['typacl'], self.acl)
            data = self._convert_for_sql(data)
            SQL = render_template("/".join([self.template_path, 'create.sql']),
                                  data=data,
                                  conn=self.conn)

        return SQL
示例#15
0
    def create(self, gid, sid, did, scid):
        """
        This function will creates new the type object

         Args:
           gid: Server Group ID
           sid: Server ID
           did: Database ID
           scid: Schema ID
           tid: Type ID
        """
        data = request.form if request.form else json.loads(
            request.data.decode())
        required_args = {'name': 'Name', 'typtype': 'Type'}

        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=410,
                    success=0,
                    errormsg=gettext(
                        "Couldn't find the required parameter (%s)." %
                        required_args[arg]))
            # Additional checks goes here
            # If type is composite then check if it has two members
            if data and data[arg] == 'c':
                if len(data['composite']) < 2:
                    return make_json_response(
                        status=410,
                        success=0,
                        errormsg=gettext(
                            'Composite types requires at least two members'))
            # If type is enum then check if it has minimum one label
            if data and data[arg] == 'e':
                if len(data['enum']) < 1:
                    return make_json_response(
                        status=410,
                        success=0,
                        errormsg=gettext(
                            'Enumeration types requires at least one label'))
            # If type is range then check if subtype is defined or not
            if data and data[arg] == 'r':
                if data['typname'] is None:
                    return make_json_response(
                        status=410,
                        success=0,
                        errormsg=gettext(
                            'Subtype must be defined for range types'))
            # If type is external then check if input/output
            # conversion function is defined
            if data and data[arg] == 'b':
                if data['typinput'] is None or \
                                data['typoutput'] is None:
                    return make_json_response(
                        status=410,
                        success=0,
                        errormsg=gettext('External types require both Input & \
                            Output conversion function.'))

        # To format privileges coming from client
        if 'typacl' in data and data['typacl'] is not None:
            data['typacl'] = parse_priv_to_db(data['typacl'], self.acl)

        data = self._convert_for_sql(data)

        try:
            SQL = render_template("/".join([self.template_path, 'create.sql']),
                                  data=data,
                                  conn=self.conn)
            status, res = self.conn.execute_scalar(SQL)
            if not status:
                return internal_server_error(errormsg=res)

            # we need oid to to add object in tree at browser
            SQL = render_template("/".join([self.template_path,
                                            'get_oid.sql']),
                                  scid=scid,
                                  data=data)
            status, tid = self.conn.execute_scalar(SQL)
            if not status:
                return internal_server_error(errormsg=tid)

            return jsonify(node=self.blueprint.generate_browser_node(
                tid, scid, data['name'], icon="icon-type"))
        except Exception as e:
            return internal_server_error(errormsg=str(e))
示例#16
0
def save(gid, sid, did):
    """
    This function will apply the privileges to the selected
    Database Objects
    """
    server_prop = server_info
    data = request.form if request.form else json.loads(request.data.decode())

    # Form db connection and we use conn to execute sql
    manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid)
    conn = manager.connection(did=did)

    acls = []
    try:
        acls = render_template(
            "/".join([server_prop['template_path'], 'acl.json']), )
        acls = json.loads(acls)
    except Exception as e:
        current_app.logger.exception(e)

    try:

        # Parse privileges
        data['priv'] = {}
        if 'acl' in data:
            # Get function acls
            data['priv']['function'] = parse_priv_to_db(
                data['acl'], acls['function']['acl'])

            data['priv']['sequence'] = parse_priv_to_db(
                data['acl'], acls['sequence']['acl'])

            data['priv']['table'] = parse_priv_to_db(data['acl'],
                                                     acls['table']['acl'])

        # Pass database objects and get SQL for privileges
        # Pass database objects and get SQL for privileges
        SQL_data = ''
        data_func = {}
        data_func['objects'] = data['objects']
        data_func['priv'] = data['priv']['function']
        SQL = render_template("/".join(
            [server_prop['template_path'], '/sql/grant_function.sql']),
                              data=data_func,
                              conn=conn)
        if SQL and SQL.strip('\n') != '':
            SQL_data += SQL

        data_seq = {}
        data_seq['objects'] = data['objects']
        data_seq['priv'] = data['priv']['sequence']
        SQL = render_template("/".join(
            [server_prop['template_path'], '/sql/grant_sequence.sql']),
                              data=data_seq,
                              conn=conn)
        if SQL and SQL.strip('\n') != '':
            SQL_data += SQL

        data_table = {}
        data_table['objects'] = data['objects']
        data_table['priv'] = data['priv']['table']
        SQL = render_template("/".join(
            [server_prop['template_path'], '/sql/grant_table.sql']),
                              data=data_table,
                              conn=conn)
        if SQL and SQL.strip('\n') != '':
            SQL_data += SQL

        status, res = conn.execute_dict(SQL_data)
        if not status:
            return internal_server_error(errormsg=res)

        return make_json_response(success=1, info="Privileges applied")

    except Exception as e:
        return internal_server_error(errormsg=e.message)
示例#17
0
    def get_sql(self, gid, sid, data, did, fid=None):
        """
        This function will generate sql from model data.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            data: Contains the data of the selected
                  foreign data wrapper node
            fid: foreign data wrapper ID
        """
        required_args = ['name']

        if fid is not None:
            sql = render_template("/".join(
                [self.template_path, self._PROPERTIES_SQL]),
                                  fid=fid,
                                  conn=self.conn)
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)

            if len(res['rows']) == 0:
                return gone(
                    gettext("Could not find the foreign"
                            " data wrapper information."))

            if res['rows'][0]['fdwoptions'] is not None:
                res['rows'][0]['fdwoptions'] = tokenize_options(
                    res['rows'][0]['fdwoptions'], 'fdwoption', 'fdwvalue')

            for key in ['fdwacl']:
                if key in data and data[key] is not None:
                    if 'added' in data[key]:
                        data[key]['added'] = parse_priv_to_db(
                            data[key]['added'], ['U'])
                    if 'changed' in data[key]:
                        data[key]['changed'] = parse_priv_to_db(
                            data[key]['changed'], ['U'])
                    if 'deleted' in data[key]:
                        data[key]['deleted'] = parse_priv_to_db(
                            data[key]['deleted'], ['U'])

            old_data = res['rows'][0]
            for arg in required_args:
                if arg not in data:
                    data[arg] = old_data[arg]

            # Allow user to set the blank value in fdwvalue
            # field in option model
            is_valid_added_options = is_valid_changed_options = False
            if 'fdwoptions' in data and 'added' in data['fdwoptions']:
                is_valid_added_options, data['fdwoptions']['added'] = \
                    validate_options(
                        data['fdwoptions']['added'],
                        'fdwoption',
                        'fdwvalue')
            if 'fdwoptions' in data and 'changed' in data['fdwoptions']:
                is_valid_changed_options, data['fdwoptions']['changed'] = \
                    validate_options(
                        data['fdwoptions']['changed'],
                        'fdwoption',
                        'fdwvalue')

            sql = render_template(
                "/".join([self.template_path, self._UPDATE_SQL]),
                data=data,
                o_data=old_data,
                is_valid_added_options=is_valid_added_options,
                is_valid_changed_options=is_valid_changed_options,
                conn=self.conn)
            return sql, data['name'] if 'name' in data else old_data['name']
        else:
            for key in ['fdwacl']:
                if key in data and data[key] is not None:
                    data[key] = parse_priv_to_db(data[key], ['U'])

            # Allow user to set the blank value in
            # fdwvalue field in option model
            is_valid_options = False
            if 'fdwoptions' in data:
                is_valid_options, data['fdwoptions'] = validate_options(
                    data['fdwoptions'], 'fdwoption', 'fdwvalue')

            sql = render_template("/".join(
                [self.template_path, self._CREATE_SQL]),
                                  data=data,
                                  conn=self.conn,
                                  is_valid_options=is_valid_options)

        return sql, data['name']
示例#18
0
    def get_sql(self, gid, sid, data, did, fid=None):
        """
        This function will generate sql from model data.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            data: Contains the data of the selected foreign data wrapper node
            fid: foreign data wrapper ID
        """
        required_args = [
            'name'
        ]
        try:
            if fid is not None:
                sql = render_template("/".join([self.template_path, 'properties.sql']), fid=fid, conn=self.conn)
                status, res = self.conn.execute_dict(sql)
                if not status:
                    return internal_server_error(errormsg=res)

                if res['rows'][0]['fdwoptions'] is not None:
                    res['rows'][0]['fdwoptions'] = self.tokenize_options(res['rows'][0]['fdwoptions'])

                for key in ['fdwacl']:
                    if key in data and data[key] is not None:
                        if 'added' in data[key]:
                            data[key]['added'] = parse_priv_to_db(data[key]['added'], ['U'])
                        if 'changed' in data[key]:
                            data[key]['changed'] = parse_priv_to_db(data[key]['changed'], ['U'])
                        if 'deleted' in data[key]:
                            data[key]['deleted'] = parse_priv_to_db(data[key]['deleted'], ['U'])

                old_data = res['rows'][0]
                for arg in required_args:
                    if arg not in data:
                        data[arg] = old_data[arg]

                new_list_add = []
                new_list_change = []

                # Allow user to set the blank value in fdwvalue field in option model
                if 'fdwoptions' in data and 'added' in data['fdwoptions']:
                    for item in data['fdwoptions']['added']:
                        new_dict_add = {}
                        if item['fdwoption']:
                            if 'fdwvalue' in item and item['fdwvalue'] and item['fdwvalue'] != '':
                                new_dict_add.update(item);
                            else:
                                new_dict_add.update({'fdwoption': item['fdwoption'], 'fdwvalue': ''})

                        new_list_add.append(new_dict_add)

                    data['fdwoptions']['added'] = new_list_add

                # Allow user to set the blank value in fdwvalue field in option model
                if 'fdwoptions' in data and 'changed' in data['fdwoptions']:
                    for item in data['fdwoptions']['changed']:
                        new_dict_change = {}
                        if item['fdwoption']:
                            if 'fdwvalue' in item and item['fdwvalue'] and item['fdwvalue'] != '':
                                new_dict_change.update(item);
                            else:
                                new_dict_change.update({'fdwoption': item['fdwoption'], 'fdwvalue': ''})

                        new_list_change.append(new_dict_change)

                    data['fdwoptions']['changed'] = new_list_change

                sql = render_template("/".join([self.template_path, 'update.sql']), data=data, o_data=old_data,
                                      conn=self.conn)
            else:
                for key in ['fdwacl']:
                    if key in data and data[key] is not None:
                        data[key] = parse_priv_to_db(data[key], ['U'])

                new_list = []

                # Allow user to set the blank value in fdwvalue field in option model
                if 'fdwoptions' in data:
                    for item in data['fdwoptions']:
                        new_dict = {}
                        if item['fdwoption']:
                            if 'fdwvalue' in item and item['fdwvalue'] and item['fdwvalue'] != '':
                                new_dict.update(item);
                            else:
                                new_dict.update({'fdwoption': item['fdwoption'], 'fdwvalue': ''})

                        new_list.append(new_dict)

                    data['fdwoptions'] = new_list

                sql = render_template("/".join([self.template_path, 'create.sql']), data=data, conn=self.conn)
                sql += "\n"
            return sql
        except Exception as e:
            return internal_server_error(errormsg=str(e))
示例#19
0
文件: __init__.py 项目: tvar/pgadmin4
    def create(self, gid, sid, did, fid):
        """
        This function will create the foreign server node.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            fid: foreign data wrapper ID
        """

        required_args = [
            'name'
        ]

        data = request.form if request.form else json.loads(
            request.data, encoding='utf-8'
        )
        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=410,
                    success=0,
                    errormsg=gettext(
                        "Could not find the required parameter ({})."
                    ).format(arg)
                )
        try:
            if 'fsrvacl' in data:
                data['fsrvacl'] = parse_priv_to_db(data['fsrvacl'], ['U'])

            sql = render_template("/".join([self.template_path,
                                            self._PROPERTIES_SQL]),
                                  fdwid=fid, conn=self.conn)
            status, res1 = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res1)
            if len(res1['rows']) == 0:
                return gone(
                    gettext("The specified foreign server could not be found.")
                )
            fdw_data = res1['rows'][0]

            is_valid_options = False
            if 'fsrvoptions' in data:
                is_valid_options, data['fsrvoptions'] = validate_options(
                    data['fsrvoptions'], 'fsrvoption', 'fsrvvalue'
                )

            sql = render_template("/".join([self.template_path,
                                            self._CREATE_SQL]),
                                  data=data, fdwdata=fdw_data,
                                  is_valid_options=is_valid_options,
                                  conn=self.conn)
            status, res = self.conn.execute_scalar(sql)
            if not status:
                return internal_server_error(errormsg=res)

            sql = render_template("/".join([self.template_path,
                                            self._PROPERTIES_SQL]),
                                  data=data, fdwdata=fdw_data,
                                  conn=self.conn)
            status, r_set = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=r_set)

            return jsonify(
                node=self.blueprint.generate_browser_node(
                    r_set['rows'][0]['oid'],
                    fid,
                    r_set['rows'][0]['name'],
                    icon="icon-foreign_server"
                )
            )
        except Exception as e:
            return internal_server_error(errormsg=str(e))
示例#20
0
    def _get_sql(self, gid, sid, did, scid, data, fnid=None, is_sql=False):
        """
        Generates the SQL statements to create/update the Function.

        Args:
            gid: Server Group Id
            sid: Server Id
            did: Database Id
            scid: Schema Id
            data: Function data
            fnid: Function Id
        """

        try:
            vol_dict = {'v': 'VOLATILE', 's': 'STABLE', 'i': 'IMMUTABLE'}

            # Get Schema Name from its OID.
            if 'pronamespace' in data:
                data['pronamespace'] = self._get_schema(data['pronamespace'])
            if 'provolatile' in data:
                data['provolatile'] = vol_dict[data['provolatile']]

            if fnid is not None:
                # Edit Mode

                # Fetch Old Data from database.
                old_data = self._fetch_properties(gid, sid, did, scid, fnid)

                # Get Schema Name
                old_data['pronamespace'] = self._get_schema(
                    old_data['pronamespace'])

                if 'provolatile' in old_data:
                    old_data['provolatile'] = vol_dict[old_data['provolatile']]

                # If any of the below argument is changed,
                # then CREATE OR REPLACE SQL statement should be called
                fun_change_args = [
                    'lanname', 'prosrc', 'probin', 'prosrc_c', 'provolatile',
                    'proisstrict', 'prosecdef', 'procost', 'proleakproof',
                    'arguments'
                ]

                data['change_func'] = False
                for arg in fun_change_args:
                    if arg == 'arguments' and arg in data and len(data[arg]) \
                            > 0:
                        data['change_func'] = True
                    elif arg in data:
                        data['change_func'] = True

                # If Function Definition/Arguments are changed then merge old
                #  Arguments with changed ones for Create/Replace Function
                # SQL statement
                if 'arguments' in data and len(data['arguments']) > 0:
                    for arg in data['arguments']['changed']:
                        for old_arg in old_data['arguments']:
                            if arg['argid'] == old_arg['argid']:
                                old_arg.update(arg)
                                break
                    data['arguments'] = old_data['arguments']
                elif data['change_func']:
                    data['arguments'] = old_data['arguments']

                # Parse Privileges
                if 'acl' in data:
                    for key in ['added', 'deleted', 'changed']:
                        if key in data['acl']:
                            data['acl'][key] = parse_priv_to_db(
                                data['acl'][key], ["X"])

                # Parse Variables
                chngd_variables = {}
                data['merged_variables'] = []
                old_data['chngd_variables'] = {}
                del_variables = {}

                # If Function Definition/Arguments are changed then,
                # Merge old, new (added, changed, deleted) variables,
                # which will be used in the CREATE or REPLACE Function sql
                # statement

                if data['change_func']:
                    # To compare old and new variables, preparing name :
                    # value dict

                    # Deleted Variables
                    if 'variables' in data and 'deleted' in data['variables']:
                        for v in data['variables']['deleted']:
                            del_variables[v['name']] = v['value']

                    if 'variables' in data and 'changed' in data['variables']:
                        for v in data['variables']['changed']:
                            chngd_variables[v['name']] = v['value']

                    if 'variables' in data and 'added' in data['variables']:
                        for v in data['variables']['added']:
                            chngd_variables[v['name']] = v['value']

                    for v in old_data['variables']:
                        old_data['chngd_variables'][v['name']] = v['value']

                    # Prepare final dict of new and old variables
                    for name, val in old_data['chngd_variables'].items():
                        if name not in chngd_variables and name not in \
                                del_variables:
                            chngd_variables[name] = val

                    # Prepare dict in [{'name': var_name, 'value': var_val},..]
                    # format
                    for name, val in chngd_variables.items():
                        data['merged_variables'].append({
                            'name': name,
                            'value': val
                        })
                else:
                    if 'variables' in data and 'changed' in data['variables']:
                        for v in data['variables']['changed']:
                            data['merged_variables'].append(v)

                    if 'variables' in data and 'added' in data['variables']:
                        for v in data['variables']['added']:
                            data['merged_variables'].append(v)

                SQL = render_template("/".join(
                    [self.sql_template_path, 'update.sql']),
                                      data=data,
                                      o_data=old_data)
            else:
                # Parse Privileges
                if 'acl' in data:
                    data['acl'] = parse_priv_to_db(data['acl'], ["X"])

                args = ''
                cnt = 1
                if 'arguments' in data:
                    for a in data['arguments']:
                        if (('argmode' in a and a['argmode'] != 'OUT'
                             and a['argmode'] is not None)
                                or 'argnode' not in a):
                            if 'argmode' in a:
                                args += a['argmode'] + " "
                            if 'argname' in a and a['argname'] != '' \
                                    and a['argname'] is not None:
                                args += self.qtIdent(self.conn,
                                                     a['argname']) + " "
                            if 'argtype' in a:
                                args += a['argtype']
                            if cnt < len(data['arguments']):
                                args += ', '
                        cnt += 1

                data['func_args'] = args.strip(' ')
                # Create mode
                SQL = render_template("/".join(
                    [self.sql_template_path, 'create.sql']),
                                      data=data,
                                      is_sql=is_sql)
            return True, SQL.strip('\n')

        except Exception as e:
            return False, e
示例#21
0
    def create(self, gid, sid, did):
        """
        This function will create the foreign data wrapper node.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
        """
        required_args = [
            'name'
        ]

        data = request.form if request.form else json.loads(request.data.decode())
        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=410,
                    success=0,
                    errormsg=gettext(
                        "Could not find the required parameter (%s)." % arg
                    )
                )

        try:

            if 'fdwacl' in data:
                data['fdwacl'] = parse_priv_to_db(data['fdwacl'], ['U'])

            new_list = []

            # Allow user to set the blank value in fdwvalue field in option model
            if 'fdwoptions' in data:
                for item in data['fdwoptions']:
                    new_dict = {}
                    if item['fdwoption']:
                        if 'fdwvalue' in item and item['fdwvalue'] and item['fdwvalue'] != '':
                            new_dict.update(item);
                        else:
                            new_dict.update({'fdwoption': item['fdwoption'], 'fdwvalue': ''})

                    new_list.append(new_dict)

                data['fdwoptions'] = new_list

            sql = render_template("/".join([self.template_path, 'create.sql']), data=data, conn=self.conn)
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)

            sql = render_template("/".join([self.template_path, 'properties.sql']), fname=data['name'], conn=self.conn)

            status, r_set = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=r_set)

            for row in r_set['rows']:
                return jsonify(
                    node=self.blueprint.generate_browser_node(
                        row['fdwoid'],
                        did,
                        row['name'],
                        icon='icon-foreign_data_wrapper'
                    )
                )

        except Exception as e:
            return internal_server_error(errormsg=str(e))
示例#22
0
    def create(self, gid, sid, did, scid):
        """
        This function will creates new the type object

         Args:
           gid: Server Group ID
           sid: Server ID
           did: Database ID
           scid: Schema ID
           tid: Type ID
        """
        data = request.form if request.form else json.loads(request.data,
                                                            encoding='utf-8')
        required_args = {'name': 'Name', 'typtype': 'Type'}

        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=410,
                    success=0,
                    errormsg=gettext(
                        "Could not find the required parameter (%s)." %
                        required_args[arg]))
            # Additional checks goes here
            # If type is composite then check if it has two members
            if data and data[arg] == 'c':
                if len(data['composite']) < 2:
                    return make_json_response(
                        status=410,
                        success=0,
                        errormsg=gettext(
                            'Composite types require at least two members.'))
            # If type is range then check if subtype is defined or not
            if data and data[arg] == 'r':
                if 'typname' not in data or data['typname'] is None:
                    return make_json_response(
                        status=410,
                        success=0,
                        errormsg=gettext(
                            'Subtype must be defined for range types.'))
            # If type is external then check if input/output
            # conversion function is defined
            if data and data[arg] == 'b':
                if ('typinput' not in data or 'typoutput' not in data
                        or data['typinput'] is None
                        or data['typoutput'] is None):
                    return make_json_response(
                        status=410,
                        success=0,
                        errormsg=gettext(
                            'External types require both input and output '
                            'conversion functions.'))

        # To format privileges coming from client
        if 'typacl' in data and data['typacl'] is not None:
            data['typacl'] = parse_priv_to_db(data['typacl'], self.acl)

        data = self._convert_for_sql(data)

        try:
            if 'composite' in data and len(data['composite']) > 0:
                for each_type in data['composite']:
                    each_type = self.convert_length_precision_to_string(
                        each_type)
                    each_type['cltype'] = self._cltype_formatter(
                        each_type['type'])
                    each_type['hasSqrBracket'] = self.hasSqrBracket

            SQL = render_template("/".join([self.template_path, 'create.sql']),
                                  data=data,
                                  conn=self.conn)
            status, res = self.conn.execute_dict(SQL)
            if not status:
                return internal_server_error(errormsg=res)

            if 'schema' in data:
                # we need scid to update in browser tree
                SQL = render_template("/".join(
                    [self.template_path, 'get_scid.sql']),
                                      schema=data['schema'])
                status, scid = self.conn.execute_scalar(SQL)
                if not status:
                    return internal_server_error(errormsg=scid)

            # we need oid to to add object in tree at browser
            SQL = render_template("/".join([self.template_path,
                                            'get_oid.sql']),
                                  scid=scid,
                                  data=data)
            status, tid = self.conn.execute_scalar(SQL)
            if not status:
                return internal_server_error(errormsg=tid)

            return jsonify(node=self.blueprint.generate_browser_node(
                tid, scid, data['name'], icon="icon-type"))
        except Exception as e:
            return internal_server_error(errormsg=str(e))
示例#23
0
    def get_sql(self, gid, sid, data, scid, tid=None, is_sql=False):
        """
        This function will genrate sql from model data
        """
        if tid is not None:

            for key in ['typacl']:
                if key in data and data[key] is not None:
                    if 'added' in data[key]:
                        data[key]['added'] = parse_priv_to_db(
                            data[key]['added'], self.acl)
                    if 'changed' in data[key]:
                        data[key]['changed'] = parse_priv_to_db(
                            data[key]['changed'], self.acl)
                    if 'deleted' in data[key]:
                        data[key]['deleted'] = parse_priv_to_db(
                            data[key]['deleted'], self.acl)

            if 'composite' in data and len(data['composite']) > 0:
                for key in ['added', 'changed', 'deleted']:
                    if key in data['composite']:
                        for each_type in data['composite'][key]:
                            each_type = self. \
                                convert_length_precision_to_string(each_type)
                            if 'type' in each_type:
                                each_type['cltype'] = self._cltype_formatter(
                                    each_type['type'])
                                each_type['hasSqrBracket'] = self.hasSqrBracket

            SQL = render_template(
                "/".join([self.template_path, 'properties.sql']),
                scid=scid,
                tid=tid,
                datlastsysoid=self.datlastsysoid,
                show_system_objects=self.blueprint.show_system_objects)
            status, res = self.conn.execute_dict(SQL)
            if not status:
                return internal_server_error(errormsg=res)
            if len(res['rows']) == 0:
                return gone(
                    gettext("Could not find the type in the database."))

            # Making copy of output for future use
            old_data = dict(res['rows'][0])

            SQL = render_template("/".join([self.template_path, 'acl.sql']),
                                  scid=scid,
                                  tid=tid)
            status, acl = self.conn.execute_dict(SQL)
            if not status:
                return internal_server_error(errormsg=acl)

            # We will set get privileges from acl sql so we don't need
            # it from properties sql
            old_data['typacl'] = []

            for row in acl['rows']:
                priv = parse_priv_from_db(row)
                if row['deftype'] in old_data:
                    old_data[row['deftype']].append(priv)
                else:
                    old_data[row['deftype']] = [priv]

            # Calling function to check and additional properties if available
            old_data.update(self.additional_properties(old_data, tid))
            old_data = self._convert_for_sql(old_data)

            SQL = render_template("/".join([self.template_path, 'update.sql']),
                                  data=data,
                                  o_data=old_data,
                                  conn=self.conn)
        else:
            required_args = ['name', 'typtype']

            for arg in required_args:
                if arg not in data:
                    return "-- definition incomplete"

            # Additional checks go here
            # If type is composite then check if it has two members
            if data and data[arg] == 'c':
                if len(data['composite']) < 2:
                    return "-- definition incomplete"

            # If type is range then check if subtype is defined or not
            if data and data[arg] == 'r':
                if 'typname' not in data or data['typname'] is None:
                    return "-- definition incomplete"

            # If type is external then check if input/output
            # conversion function is defined
            if data and data[arg] == 'b':
                if ('typinput' not in data or 'typoutput' not in data
                        or data['typinput'] is None
                        or data['typoutput'] is None):
                    return "-- definition incomplete"

            # Privileges
            if 'typacl' in data and data['typacl'] is not None:
                data['typacl'] = parse_priv_to_db(data['typacl'], self.acl)

            data = self._convert_for_sql(data)

            if 'composite' in data and len(data['composite']) > 0:
                for each_type in data['composite']:
                    each_type = self.convert_length_precision_to_string(
                        each_type)
                    each_type['cltype'] = self._cltype_formatter(
                        each_type['type'])
                    each_type['hasSqrBracket'] = self.hasSqrBracket

            SQL = render_template("/".join([self.template_path, 'create.sql']),
                                  data=data,
                                  conn=self.conn,
                                  is_sql=is_sql)

        return SQL, data['name'] if 'name' in data else old_data['name']
示例#24
0
    def sql(self, gid, sid, did, scid, tid):
        """
        This function will generates reverse engineered sql for type object

         Args:
           gid: Server Group ID
           sid: Server ID
           did: Database ID
           scid: Schema ID
           tid: Type ID
        """
        SQL = render_template("/".join([self.template_path,
                                        'properties.sql']),
                              scid=scid, tid=tid,
                              datlastsysoid=self.datlastsysoid,
                              show_system_objects=self.blueprint.show_system_objects
                              )
        status, res = self.conn.execute_dict(SQL)
        if not status:
            return internal_server_error(errormsg=res)

        # Making copy of output for future use
        data = dict(res['rows'][0])

        SQL = render_template("/".join([self.template_path, 'acl.sql']),
                              scid=scid, tid=tid)
        status, acl = self.conn.execute_dict(SQL)
        if not status:
            return internal_server_error(errormsg=acl)

        # We will set get privileges from acl sql so we don't need
        # it from properties sql
        data['typacl'] = []

        for row in acl['rows']:
            priv = parse_priv_from_db(row)
            if row['deftype'] in data:
                data[row['deftype']].append(priv)
            else:
                data[row['deftype']] = [priv]

        # Privileges
        if 'typacl' in data and data['typacl'] is not None:
            data['nspacl'] = parse_priv_to_db(data['typacl'], self.acl)

        # Calling function to check and additional properties if available
        data.update(self.additional_properties(data, tid))

        # We do not want to display table which has '-' value
        # setting them to None so that jinja avoid displaying them
        for k in data:
            if data[k] == '-':
                data[k] = None

        SQL = self.get_sql(gid, sid, data, scid, tid=None)

        # We are appending headers here for sql panel
        sql_header = "-- Type: {0}\n\n-- ".format(data['name'])
        sql_header += render_template("/".join([self.template_path,
                                                'delete.sql']),
                                      data=data, conn=self.conn)
        SQL = sql_header + '\n\n' + SQL

        return ajax_response(response=SQL)
示例#25
0
    def get_sql(self, gid, sid, data, did, fid, fsid=None):
        """
        This function will generate sql from model data.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            data: Contains the data of the selected foreign server node
            fid: foreign data wrapper ID
            fsid: foreign server ID
        """

        required_args = [
            'name'
        ]

        if fsid is not None:
            sql = render_template("/".join([self.template_path,
                                            'properties.sql']),
                                  fsid=fsid, conn=self.conn)
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)
            if len(res['rows']) == 0:
                return gone(
                    gettext("Could not find the foreign server information.")
                )

            if res['rows'][0]['fsrvoptions'] is not None:
                res['rows'][0]['fsrvoptions'] = tokenize_options(
                    res['rows'][0]['fsrvoptions'], 'fsrvoption', 'fsrvvalue'
                )

            for key in ['fsrvacl']:
                if key in data and data[key] is not None:
                    if 'added' in data[key]:
                        data[key]['added'] = parse_priv_to_db(
                            data[key]['added'],
                            ['U']
                        )
                    if 'changed' in data[key]:
                        data[key]['changed'] = parse_priv_to_db(
                            data[key]['changed'],
                            ['U']
                        )
                    if 'deleted' in data[key]:
                        data[key]['deleted'] = parse_priv_to_db(
                            data[key]['deleted'],
                            ['U']
                        )

            old_data = res['rows'][0]
            for arg in required_args:
                if arg not in data:
                    data[arg] = old_data[arg]

            is_valid_added_options = is_valid_changed_options = False
            if 'fsrvoptions' in data and 'added' in data['fsrvoptions']:
                is_valid_added_options, data['fsrvoptions']['added'] =\
                    validate_options(
                        data['fsrvoptions']['added'],
                        'fsrvoption',
                        'fsrvvalue')

            if 'fsrvoptions' in data and 'changed' in data['fsrvoptions']:
                is_valid_changed_options, data['fsrvoptions']['changed'] =\
                    validate_options(
                        data['fsrvoptions']['changed'],
                        'fsrvoption',
                        'fsrvvalue')

            sql = render_template(
                "/".join([self.template_path, 'update.sql']),
                data=data,
                o_data=old_data,
                is_valid_added_options=is_valid_added_options,
                is_valid_changed_options=is_valid_changed_options,
                conn=self.conn
            )
            return sql, data['name'] if 'name' in data else old_data['name']
        else:
            sql = render_template("/".join([self.template_path,
                                            'properties.sql']),
                                  fdwid=fid, conn=self.conn)
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)

            fdw_data = res['rows'][0]

            for key in ['fsrvacl']:
                if key in data and data[key] is not None:
                    data[key] = parse_priv_to_db(data[key], ['U'])

            is_valid_options = False
            if 'fsrvoptions' in data:
                is_valid_options, data['fsrvoptions'] = validate_options(
                    data['fsrvoptions'], 'fsrvoption', 'fsrvvalue'
                )

            sql = render_template("/".join([self.template_path, 'create.sql']),
                                  data=data, fdwdata=fdw_data,
                                  is_valid_options=is_valid_options,
                                  conn=self.conn)
            sql += "\n"
        return sql, data['name']
示例#26
0
    def create(self, gid, sid):
        """
        This function will creates new the tablespace object
        """

        required_args = {'name': 'Name', 'spclocation': 'Location'}

        data = request.form if request.form else json.loads(request.data,
                                                            encoding='utf-8')

        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=410,
                    success=0,
                    errormsg=gettext(
                        "Could not find the required parameter ({}).").format(
                            arg))

        # To format privileges coming from client
        if 'spcacl' in data:
            data['spcacl'] = parse_priv_to_db(data['spcacl'], ['C'])

        try:
            SQL = render_template("/".join(
                [self.template_path, self._CREATE_SQL]),
                                  data=data,
                                  conn=self.conn)

            status, res = self.conn.execute_scalar(SQL)

            if not status:
                return internal_server_error(errormsg=res)

            # To fetch the oid of newly created tablespace
            SQL = render_template("/".join(
                [self.template_path, self._ALTER_SQL]),
                                  tablespace=data['name'],
                                  conn=self.conn)

            status, tsid = self.conn.execute_scalar(SQL)

            if not status:
                return internal_server_error(errormsg=tsid)

            SQL = render_template("/".join(
                [self.template_path, self._ALTER_SQL]),
                                  data=data,
                                  conn=self.conn)

            # Checking if we are not executing empty query
            if SQL and SQL.strip('\n') and SQL.strip(' '):
                status, res = self.conn.execute_scalar(SQL)
                if not status:
                    return jsonify(node=self.blueprint.generate_browser_node(
                        tsid, sid, data['name'], icon="icon-tablespace"),
                                   success=0,
                                   errormsg=gettext(
                                       'Tablespace created successfully, '
                                       'Set parameter fail: {0}'.format(res)),
                                   info=gettext(res))

            return jsonify(node=self.blueprint.generate_browser_node(
                tsid, sid, data['name'], icon="icon-tablespace"))
        except Exception as e:
            current_app.logger.exception(e)
            return internal_server_error(errormsg=str(e))
示例#27
0
    def create(self, gid, sid, did, scid, tid):
        """
        This function will creates new the schema object

         Args:
           gid: Server Group ID
           sid: Server ID
           did: Database ID
           scid: Schema ID
           tid: Table ID
        """
        data = request.form if request.form else json.loads(request.data,
                                                            encoding='utf-8')

        for k, v in data.items():
            # comments should be taken as is because if user enters a
            # json comment it is parsed by loads which should not happen
            if k in ('description', ):
                data[k] = v
            else:
                data[k] = json.loads(v,
                                     encoding='utf-8',
                                     cls=ColParamsJSONDecoder)

        required_args = {'name': 'Name', 'cltype': 'Type'}

        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=410,
                    success=0,
                    errormsg=gettext(
                        "Could not find the required parameter ({}).").format(
                            required_args[arg]))

        # Parse privilege data coming from client according to database format
        if 'attacl' in data:
            data['attacl'] = parse_priv_to_db(data['attacl'], self.acl)

        # Adding parent into data dict, will be using it while creating sql
        data['schema'] = self.schema
        data['table'] = self.table
        if len(data['table']) == 0:
            return gone(gettext("The specified table could not be found."))

        # check type for '[]' in it
        data['cltype'], data['hasSqrBracket'] = \
            column_utils.type_formatter(data['cltype'])
        data = column_utils.convert_length_precision_to_string(data)

        SQL = render_template("/".join([self.template_path, 'create.sql']),
                              data=data,
                              conn=self.conn)
        status, res = self.conn.execute_scalar(SQL)
        if not status:
            return internal_server_error(errormsg=res)

        # we need oid to to add object in tree at browser
        SQL = render_template("/".join(
            [self.template_path, 'get_position.sql']),
                              tid=tid,
                              data=data)
        status, clid = self.conn.execute_scalar(SQL)
        if not status:
            return internal_server_error(errormsg=tid)

        return jsonify(node=self.blueprint.generate_browser_node(
            clid, tid, data['name'], icon="icon-column"))
示例#28
0
    def get_sql(self, gid, sid, did, scid, data, foid=None):
        """
        Genrates the SQL statements to create/update the Foreign Table.

        Args:
            gid: Server Group Id
            sid: Server Id
            did: Database Id
            scid: Schema Id
            foid: Foreign Table Id
        """
        if foid is not None:
            old_data = self._fetch_properties(gid, sid, did, scid, foid,
                                              inherits=True)
            if old_data is False:
                return gone(
                    gettext("Could not find the foreign table on the server.")
                )

            # Prepare dict of columns with key = column's attnum
            # Will use this in the update template when any column is
            # changed, to identify the columns.
            col_data = {}
            for c in old_data['columns']:
                col_data[c['attnum']] = c

            old_data['columns'] = col_data

            if 'columns' in data and 'added' in data['columns']:
                data['columns']['added'] = self._format_columns(
                    data['columns']['added'])

            if 'columns' in data and 'changed' in data['columns']:
                data['columns']['changed'] = self._format_columns(
                    data['columns']['changed'])

                # Parse Column Options
                for c in data['columns']['changed']:
                    old_col_options = c['attfdwoptions'] = []
                    if 'attfdwoptions' in c and c['attfdwoptions']:
                        old_col_options = c['attfdwoptions']

                    old_col_frmt_options = {}

                    for o in old_col_options:
                        col_opt = o.split("=")
                        old_col_frmt_options[col_opt[0]] = col_opt[1]

                    c['coloptions_updated'] = {'added': [],
                                               'changed': [],
                                               'deleted': []}

                    if 'coloptions' in c and len(c['coloptions']) > 0:
                        for o in c['coloptions']:
                            if (
                                o['option'] in old_col_frmt_options and
                                o['value'] != old_col_frmt_options[o['option']]
                            ):
                                c['coloptions_updated']['changed'].append(o)
                            elif o['option'] not in old_col_frmt_options:
                                c['coloptions_updated']['added'].append(o)
                            if o['option'] in old_col_frmt_options:
                                del old_col_frmt_options[o['option']]

                    for o in old_col_frmt_options:
                        c['coloptions_updated']['deleted'].append(
                            {'option': o})

            # Parse Privileges
            if 'acl' in data and 'added' in data['acl']:
                data['acl']['added'] = parse_priv_to_db(data['acl']['added'],
                                                        ["a", "r", "w", "x"])
            if 'acl' in data and 'changed' in data['acl']:
                data['acl']['changed'] = parse_priv_to_db(
                    data['acl']['changed'], ["a", "r", "w", "x"])
            if 'acl' in data and 'deleted' in data['acl']:
                data['acl']['deleted'] = parse_priv_to_db(
                    data['acl']['deleted'], ["a", "r", "w", "x"])

            SQL = render_template(
                "/".join([self.template_path, 'update.sql']),
                data=data, o_data=old_data
            )
            return SQL, data['name'] if 'name' in data else old_data['name']
        else:
            data['columns'] = self._format_columns(data['columns'])

            # Parse Privileges
            if 'acl' in data:
                data['acl'] = parse_priv_to_db(data['acl'],
                                               ["a", "r", "w", "x"])

            SQL = render_template("/".join([self.template_path,
                                            'create.sql']), data=data)
            return SQL, data['name']
示例#29
0
    def create(self, gid, sid):
        """Create the database."""
        required_args = [
            'name'
        ]

        data = request.form if request.form else json.loads(
            request.data, encoding='utf-8'
        )

        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=410,
                    success=0,
                    errormsg=_(
                        "Could not find the required parameter ({})."
                    ).format(arg)
                )
        # The below SQL will execute CREATE DDL only
        SQL = render_template(
            "/".join([self.template_path, self._CREATE_SQL]),
            data=data, conn=self.conn
        )
        status, msg = self.conn.execute_scalar(SQL)
        if not status:
            return internal_server_error(errormsg=msg)

        if 'datacl' in data:
            data['datacl'] = parse_priv_to_db(data['datacl'], 'DATABASE')

        # The below SQL will execute rest DMLs because we cannot execute
        # CREATE with any other
        SQL = render_template(
            "/".join([self.template_path, self._GRANT_SQL]),
            data=data, conn=self.conn
        )
        SQL = SQL.strip('\n').strip(' ')
        if SQL and SQL != "":
            status, msg = self.conn.execute_scalar(SQL)
            if not status:
                return internal_server_error(errormsg=msg)

        # We need oid of newly created database
        SQL = render_template(
            "/".join([self.template_path, self._PROPERTIES_SQL]),
            name=data['name'], conn=self.conn, last_system_oid=0,
            show_system_objects=self.blueprint.show_system_objects,
        )
        SQL = SQL.strip('\n').strip(' ')
        if SQL and SQL != "":
            status, res = self.conn.execute_dict(SQL)
            if not status:
                return internal_server_error(errormsg=res)

        response = res['rows'][0]
        # Add database entry into database table with schema_restrictions.
        database = Database(id=response['did'], server=sid,
                            schema_res=','.join(data['schema_res']))
        db.session.add(database)
        db.session.commit()

        return jsonify(
            node=self.blueprint.generate_browser_node(
                response['did'],
                sid,
                response['name'],
                icon="icon-database-not-connected",
                connected=False,
                tablespace=response['default_tablespace'],
                allowConn=True,
                canCreate=response['cancreate'],
                canDisconn=True,
                canDrop=True
            )
        )
示例#30
0
    def get_sql(self, gid, sid, data, scid, tid=None):
        """
        This function will genrate sql from model data
        """
        if tid is not None:

            for key in ['typacl']:
                if key in data and data[key] is not None:
                    if 'added' in data[key]:
                        data[key]['added'] = parse_priv_to_db(data[key]['added'], self.acl)
                    if 'changed' in data[key]:
                        data[key]['changed'] = parse_priv_to_db(data[key]['changed'], self.acl)
                    if 'deleted' in data[key]:
                        data[key]['deleted'] = parse_priv_to_db(data[key]['deleted'], self.acl)

            SQL = render_template("/".join([self.template_path,
                                            'properties.sql']),
                                  scid=scid, tid=tid,
                                  datlastsysoid=self.datlastsysoid,
                                  show_system_objects=self.blueprint.show_system_objects
                                  )
            status, res = self.conn.execute_dict(SQL)
            if not status:
                return internal_server_error(errormsg=res)

            # Making copy of output for future use
            old_data = dict(res['rows'][0])

            SQL = render_template("/".join([self.template_path, 'acl.sql']),
                                  scid=scid, tid=tid)
            status, acl = self.conn.execute_dict(SQL)
            if not status:
                return internal_server_error(errormsg=acl)

            # We will set get privileges from acl sql so we don't need
            # it from properties sql
            old_data['typacl'] = []

            for row in acl['rows']:
                priv = parse_priv_from_db(row)
                if row['deftype'] in old_data:
                    old_data[row['deftype']].append(priv)
                else:
                    old_data[row['deftype']] = [priv]

            # Calling function to check and additional properties if available
            old_data.update(self.additional_properties(old_data, tid))
            old_data = self._convert_for_sql(old_data)

            SQL = render_template(
                "/".join([self.template_path, 'update.sql']),
                data=data, o_data=old_data, conn=self.conn
            )
        else:
            required_args = [
                'name',
                'typtype'
            ]

            for arg in required_args:
                if arg not in data:
                    return " --definition incomplete"

            # Privileges
            if 'typacl' in data and data['typacl'] is not None:
                data['typacl'] = parse_priv_to_db(data['typacl'], self.acl)
            data = self._convert_for_sql(data)
            SQL = render_template("/".join([self.template_path,
                                            'create.sql']),
                                  data=data, conn=self.conn)

        return SQL
示例#31
0
    def get_sql(self, gid, sid, data, did, fid=None):
        """
        This function will generate sql from model data.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            data: Contains the data of the selected foreign data wrapper node
            fid: foreign data wrapper ID
        """
        required_args = [
            'name'
        ]
        try:
            if fid is not None:
                sql = render_template("/".join([self.template_path, 'properties.sql']), fid=fid, conn=self.conn)
                status, res = self.conn.execute_dict(sql)
                if not status:
                    return internal_server_error(errormsg=res)

                if res['rows'][0]['fdwoptions'] is not None:
                    res['rows'][0]['fdwoptions'] = self.tokenize_options(res['rows'][0]['fdwoptions'])

                for key in ['fdwacl']:
                    if key in data and data[key] is not None:
                        if 'added' in data[key]:
                          data[key]['added'] = parse_priv_to_db(data[key]['added'], ['U'])
                        if 'changed' in data[key]:
                          data[key]['changed'] = parse_priv_to_db(data[key]['changed'], ['U'])
                        if 'deleted' in data[key]:
                          data[key]['deleted'] = parse_priv_to_db(data[key]['deleted'], ['U'])

                old_data = res['rows'][0]
                for arg in required_args:
                    if arg not in data:
                        data[arg] = old_data[arg]

                new_list_add = []
                new_list_change = []

                # Allow user to set the blank value in fdwvalue field in option model
                if 'fdwoptions' in data and 'added' in data['fdwoptions']:
                    for item in data['fdwoptions']['added']:
                        new_dict_add = {}
                        if item['fdwoption']:
                            if 'fdwvalue' in item and item['fdwvalue'] and item['fdwvalue'] != '':
                                new_dict_add.update(item);
                            else:
                                new_dict_add.update({'fdwoption': item['fdwoption'], 'fdwvalue': ''})

                        new_list_add.append(new_dict_add)

                    data['fdwoptions']['added'] = new_list_add

                # Allow user to set the blank value in fdwvalue field in option model
                if 'fdwoptions' in data and 'changed' in data['fdwoptions']:
                    for item in data['fdwoptions']['changed']:
                        new_dict_change = {}
                        if item['fdwoption']:
                            if 'fdwvalue' in item and item['fdwvalue'] and item['fdwvalue'] != '':
                                new_dict_change.update(item);
                            else:
                                new_dict_change.update({'fdwoption': item['fdwoption'], 'fdwvalue': ''})

                        new_list_change.append(new_dict_change)

                    data['fdwoptions']['changed'] = new_list_change

                sql = render_template("/".join([self.template_path, 'update.sql']), data=data, o_data=old_data,
                                      conn=self.conn)
            else:
                for key in ['fdwacl']:
                    if key in data and data[key] is not None:
                        data[key] = parse_priv_to_db(data[key], ['U'])

                new_list = []

                # Allow user to set the blank value in fdwvalue field in option model
                if 'fdwoptions' in data:
                    for item in data['fdwoptions']:
                        new_dict = {}
                        if item['fdwoption']:
                            if 'fdwvalue' in item and item['fdwvalue'] and item['fdwvalue'] != '':
                                new_dict.update(item);
                            else:
                                new_dict.update({'fdwoption': item['fdwoption'], 'fdwvalue': ''})

                        new_list.append(new_dict)

                    data['fdwoptions'] = new_list

                sql = render_template("/".join([self.template_path, 'create.sql']), data=data, conn=self.conn)
                sql += "\n"
            return sql
        except Exception as e:
            return internal_server_error(errormsg=str(e))
示例#32
0
    def sql(self, gid, sid, did, fid):
        """
        This function will generate sql to show it in sql pane
        for the selected foreign data wrapper node.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            fid: Foreign data wrapper ID
        """
        sql = render_template("/".join([self.template_path, 'properties.sql']),
                              fid=fid, conn=self.conn
                              )
        status, res = self.conn.execute_dict(sql)
        if not status:
            return internal_server_error(errormsg=res)
        if len(res['rows']) == 0:
            return gone(
                _("Could not find the foreign data wrapper on the server.")
            )

        is_valid_options = False
        if res['rows'][0]['fdwoptions'] is not None:
            res['rows'][0]['fdwoptions'] = tokenize_options(
                res['rows'][0]['fdwoptions'], 'fdwoption', 'fdwvalue'
            )

            if len(res['rows'][0]['fdwoptions']) > 0:
                is_valid_options = True

        sql = render_template("/".join([self.template_path, 'acl.sql']),
                              fid=fid)
        status, fdw_acl_res = self.conn.execute_dict(sql)
        if not status:
            return internal_server_error(errormsg=fdw_acl_res)

        for row in fdw_acl_res['rows']:
            privilege = parse_priv_from_db(row)
            if row['deftype'] in res['rows'][0]:
                res['rows'][0][row['deftype']].append(privilege)
            else:
                res['rows'][0][row['deftype']] = [privilege]

        # To format privileges
        if 'fdwacl' in res['rows'][0]:
            res['rows'][0]['fdwacl'] = parse_priv_to_db(
                res['rows'][0]['fdwacl'],
                ['U']
            )

        sql = ''
        sql = render_template("/".join([self.template_path, 'create.sql']),
                              data=res['rows'][0], conn=self.conn,
                              is_valid_options=is_valid_options
                              )
        sql += "\n"

        sql_header = u"""-- Foreign Data Wrapper: {0}

-- DROP FOREIGN DATA WRAPPER {0}

""".format(res['rows'][0]['name'])

        sql = sql_header + sql

        return ajax_response(response=sql.strip('\n'))
示例#33
0
    def getSQL(self, gid, sid, did, data, scid, pkgid=None):
        """
        This function will generate sql from model data.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            scid: Schema ID
            pkgid: Package ID
        """

        required_args = [
            u'name'
        ]

        if pkgid is not None:
            data['schema'] = self.schema
            SQL = render_template(
                "/".join([self.template_path, 'properties.sql']), scid=scid,
                pkgid=pkgid)
            status, res = self.conn.execute_dict(SQL)
            if not status:
                return internal_server_error(errormsg=res)
            if len(res['rows']) == 0:
                return gone(
                    errormsg=_("Could not find the package in the database.")
                )

            res['rows'][0]['pkgheadsrc'] = self.get_inner(
                res['rows'][0]['pkgheadsrc'])
            res['rows'][0]['pkgbodysrc'] = self.get_inner(
                res['rows'][0]['pkgbodysrc'])

            SQL = render_template("/".join([self.template_path, 'acl.sql']),
                                  scid=scid,
                                  pkgid=pkgid)

            status, rset1 = self.conn.execute_dict(SQL)

            if not status:
                return internal_server_error(errormsg=rset1)

            for row in rset1['rows']:
                priv = parse_priv_from_db(row)
                res['rows'][0].setdefault(row['deftype'], []).append(priv)

            # Making copy of output for further processing
            old_data = dict(res['rows'][0])

            # To format privileges data coming from client
            for key in ['pkgacl']:
                if key in data and data[key] is not None:
                    if 'added' in data[key]:
                        data[key]['added'] = parse_priv_to_db(
                            data[key]['added'], self.acl)
                    if 'changed' in data[key]:
                        data[key]['changed'] = parse_priv_to_db(
                            data[key]['changed'], self.acl)
                    if 'deleted' in data[key]:
                        data[key]['deleted'] = parse_priv_to_db(
                            data[key]['deleted'], self.acl)

            # If name is not present with in update data then copy it
            # from old data
            for arg in required_args:
                if arg not in data:
                    data[arg] = old_data[arg]

            SQL = render_template("/".join([self.template_path, 'update.sql']),
                                  data=data, o_data=old_data, conn=self.conn)
            return SQL, data['name'] if 'name' in data else old_data['name']
        else:
            # To format privileges coming from client
            if 'pkgacl' in data:
                data['pkgacl'] = parse_priv_to_db(data['pkgacl'], self.acl)

            SQL = render_template("/".join([self.template_path, 'create.sql']),
                                  data=data, conn=self.conn)

            return SQL, data['name']
示例#34
0
    def create(self, gid, sid, did, scid):
        """
        Create the sequence.

        Args:
          gid: Server Group ID
          sid: Server ID
          did: Database ID
          scid: Schema ID

        Returns:

        """
        required_args = [
            u'name',
            u'schema',
            u'seqowner',
        ]

        data = request.form if request.form else json.loads(request.data.decode())

        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=400,
                    success=0,
                    errormsg=_(
                        "Could not find the required parameter (%s)." % arg
                    )
                )
        try:
            # The SQL below will execute CREATE DDL only
            SQL = render_template("/".join([self.template_path, 'create.sql']), data=data, conn=self.conn)
            status, msg = self.conn.execute_scalar(SQL)
            if not status:
                return internal_server_error(errormsg=msg)

            if 'relacl' in data:
                data['relacl'] = parse_priv_to_db(data['relacl'], 'DATABASE')

            # The SQL below will execute rest DMLs because we can not execute CREATE with any other
            SQL = render_template("/".join([self.template_path, 'grant.sql']), data=data, conn=self.conn)
            SQL = SQL.strip('\n').strip(' ')
            if SQL and SQL != "":
                status, msg = self.conn.execute_scalar(SQL)
                if not status:
                    return internal_server_error(errormsg=msg)

            # We need oid of newly created sequence.
            SQL = render_template("/".join([self.template_path, 'get_oid.sql']), name=data['name'], scid=scid)
            SQL = SQL.strip('\n').strip(' ')
            if SQL and SQL != "":
                status, seid = self.conn.execute_scalar(SQL)
                if not status:
                    return internal_server_error(errormsg=res)

            return jsonify(
                node=self.blueprint.generate_browser_node(
                    seid,
                    scid,
                    data['name'],
                    icon="icon-%s" % self.node_type
                )
            )

        except Exception as e:
            return make_json_response(
                status=500,
                success=0,
                errormsg=str(e)
            )
示例#35
0
    def sql(self, gid, sid, did, scid, tid):
        """
        This function will generates reverse engineered sql for type object

         Args:
           gid: Server Group ID
           sid: Server ID
           did: Database ID
           scid: Schema ID
           tid: Type ID
        """
        SQL = render_template(
            "/".join([self.template_path, 'properties.sql']),
            scid=scid,
            tid=tid,
            datlastsysoid=self.datlastsysoid,
            show_system_objects=self.blueprint.show_system_objects)
        status, res = self.conn.execute_dict(SQL)
        if not status:
            return internal_server_error(errormsg=res)

        # Making copy of output for future use
        data = dict(res['rows'][0])

        SQL = render_template("/".join([self.template_path, 'acl.sql']),
                              scid=scid,
                              tid=tid)
        status, acl = self.conn.execute_dict(SQL)
        if not status:
            return internal_server_error(errormsg=acl)

        # We will set get privileges from acl sql so we don't need
        # it from properties sql
        data['typacl'] = []

        for row in acl['rows']:
            priv = parse_priv_from_db(row)
            if row['deftype'] in data:
                data[row['deftype']].append(priv)
            else:
                data[row['deftype']] = [priv]

        # Privileges
        if 'typacl' in data and data['typacl'] is not None:
            data['nspacl'] = parse_priv_to_db(data['typacl'], self.acl)

        # Calling function to check and additional properties if available
        data.update(self.additional_properties(data, tid))

        # We do not want to display table which has '-' value
        # setting them to None so that jinja avoid displaying them
        for k in data:
            if data[k] == '-':
                data[k] = None

        SQL = self.get_sql(gid, sid, data, scid, tid=None)

        # We are appending headers here for sql panel
        sql_header = "-- Type: {0}\n\n-- ".format(data['name'])
        sql_header += render_template("/".join(
            [self.template_path, 'delete.sql']),
                                      data=data,
                                      conn=self.conn)
        SQL = sql_header + '\n\n' + SQL

        return ajax_response(response=SQL)
示例#36
0
    def get_sql(self, gid, sid, data, did, fid=None):
        """
        This function will generate sql from model data.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            data: Contains the data of the selected
                  foreign data wrapper node
            fid: foreign data wrapper ID
        """
        required_args = [
            'name'
        ]

        if fid is not None:
            sql = render_template("/".join([self.template_path,
                                            'properties.sql']),
                                  fid=fid,
                                  conn=self.conn
                                  )
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)

            if len(res['rows']) == 0:
                return gone(
                    gettext("Could not find the foreign"
                            " data wrapper information.")
                )

            if res['rows'][0]['fdwoptions'] is not None:
                res['rows'][0]['fdwoptions'] = tokenize_options(
                    res['rows'][0]['fdwoptions'],
                    'fdwoption', 'fdwvalue'
                )

            for key in ['fdwacl']:
                if key in data and data[key] is not None:
                    if 'added' in data[key]:
                        data[key]['added'] = parse_priv_to_db(
                            data[key]['added'],
                            ['U']
                        )
                    if 'changed' in data[key]:
                        data[key]['changed'] = parse_priv_to_db(
                            data[key]['changed'],
                            ['U']
                        )
                    if 'deleted' in data[key]:
                        data[key]['deleted'] = parse_priv_to_db(
                            data[key]['deleted'],
                            ['U']
                        )

            old_data = res['rows'][0]
            for arg in required_args:
                if arg not in data:
                    data[arg] = old_data[arg]

            # Allow user to set the blank value in fdwvalue
            # field in option model
            is_valid_added_options = is_valid_changed_options = False
            if 'fdwoptions' in data and 'added' in data['fdwoptions']:
                is_valid_added_options, data['fdwoptions']['added'] = \
                    validate_options(
                        data['fdwoptions']['added'],
                        'fdwoption',
                        'fdwvalue')
            if 'fdwoptions' in data and 'changed' in data['fdwoptions']:
                is_valid_changed_options, data['fdwoptions']['changed'] = \
                    validate_options(
                        data['fdwoptions']['changed'],
                        'fdwoption',
                        'fdwvalue')

            sql = render_template(
                "/".join([self.template_path, 'update.sql']),
                data=data,
                o_data=old_data,
                is_valid_added_options=is_valid_added_options,
                is_valid_changed_options=is_valid_changed_options,
                conn=self.conn
            )
            return sql, data['name'] if 'name' in data else old_data['name']
        else:
            for key in ['fdwacl']:
                if key in data and data[key] is not None:
                    data[key] = parse_priv_to_db(data[key], ['U'])

            # Allow user to set the blank value in
            # fdwvalue field in option model
            is_valid_options = False
            if 'fdwoptions' in data:
                is_valid_options, data['fdwoptions'] = validate_options(
                    data['fdwoptions'], 'fdwoption', 'fdwvalue'
                )

            sql = render_template("/".join([self.template_path,
                                            'create.sql']),
                                  data=data, conn=self.conn,
                                  is_valid_options=is_valid_options
                                  )

        return sql, data['name']
示例#37
0
    def get_sql(self,
                gid,
                sid,
                did,
                scid,
                data,
                foid=None,
                is_schema_diff=False):
        """
        Genrates the SQL statements to create/update the Foreign Table.

        Args:
            gid: Server Group Id
            sid: Server Id
            did: Database Id
            scid: Schema Id
            foid: Foreign Table Id
            is_schema_diff: True is function gets called from schema diff
        """
        if foid is not None:
            status, old_data = self._fetch_properties(gid,
                                                      sid,
                                                      did,
                                                      scid,
                                                      foid,
                                                      inherits=True)
            if not status:
                return old_data

            if is_schema_diff:
                data['is_schema_diff'] = True
                old_data['columns_for_schema_diff'] = old_data['columns']

            # Prepare dict of columns with key = column's attnum
            # Will use this in the update template when any column is
            # changed, to identify the columns.
            col_data = {}
            for c in old_data['columns']:
                col_data[c['attnum']] = c

            old_data['columns'] = col_data

            if 'columns' in data and 'added' in data['columns']:
                data['columns']['added'] = self._format_columns(
                    data['columns']['added'])

            if 'columns' in data and 'changed' in data['columns']:
                data['columns']['changed'] = self._format_columns(
                    data['columns']['changed'])

                # Parse Column Options
                for c in data['columns']['changed']:
                    old_col_options = c['attfdwoptions'] = []
                    if 'attfdwoptions' in c and c['attfdwoptions']:
                        old_col_options = c['attfdwoptions']

                    old_col_frmt_options = {}

                    for o in old_col_options:
                        col_opt = o.split("=")
                        old_col_frmt_options[col_opt[0]] = col_opt[1]

                    c['coloptions_updated'] = {
                        'added': [],
                        'changed': [],
                        'deleted': []
                    }

                    if 'coloptions' in c and len(c['coloptions']) > 0:
                        for o in c['coloptions']:
                            if (o['option'] in old_col_frmt_options
                                    and o['value'] !=
                                    old_col_frmt_options[o['option']]):
                                c['coloptions_updated']['changed'].append(o)
                            elif o['option'] not in old_col_frmt_options:
                                c['coloptions_updated']['added'].append(o)
                            if o['option'] in old_col_frmt_options:
                                del old_col_frmt_options[o['option']]

                    for o in old_col_frmt_options:
                        c['coloptions_updated']['deleted'].append(
                            {'option': o})

            # Parse Privileges
            if 'acl' in data and 'added' in data['acl']:
                data['acl']['added'] = parse_priv_to_db(
                    data['acl']['added'], ["a", "r", "w", "x"])
            if 'acl' in data and 'changed' in data['acl']:
                data['acl']['changed'] = parse_priv_to_db(
                    data['acl']['changed'], ["a", "r", "w", "x"])
            if 'acl' in data and 'deleted' in data['acl']:
                data['acl']['deleted'] = parse_priv_to_db(
                    data['acl']['deleted'], ["a", "r", "w", "x"])

            # If ftsrvname is changed while comparing two schemas
            # then we need to drop foreign table and recreate it
            if is_schema_diff and 'ftsrvname' in data:
                # Modify the data required to recreate the foreign table.
                self.modify_data_for_schema_diff(data, old_data)

                SQL = render_template("/".join(
                    [self.template_path, 'foreign_table_schema_diff.sql']),
                                      data=data,
                                      o_data=old_data)
            else:
                SQL = render_template("/".join(
                    [self.template_path, 'update.sql']),
                                      data=data,
                                      o_data=old_data)
            return SQL, data['name'] if 'name' in data else old_data['name']
        else:
            data['columns'] = self._format_columns(data['columns'])

            # Parse Privileges
            if 'acl' in data:
                data['acl'] = parse_priv_to_db(data['acl'],
                                               ["a", "r", "w", "x"])

            SQL = render_template("/".join([self.template_path, 'create.sql']),
                                  data=data)
            return SQL, data['name']
示例#38
0
    def getSQL(self,
               gid,
               sid,
               did,
               data,
               scid,
               pkgid=None,
               sqltab=False,
               diff_schema=None):
        """
        This function will generate sql from model data.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            scid: Schema ID
            pkgid: Package ID
            sqltab: True
            diff_schema: Target Schema
        """

        required_args = [u'name']

        if diff_schema:
            data['schema'] = diff_schema
        else:
            data['schema'] = self.schema

        if pkgid is not None and not sqltab:
            SQL = render_template("/".join(
                [self.template_path, 'properties.sql']),
                                  scid=scid,
                                  pkgid=pkgid)
            status, res = self.conn.execute_dict(SQL)
            if not status:
                return internal_server_error(errormsg=res)
            if len(res['rows']) == 0:
                return gone(
                    errormsg=_("Could not find the package in the database."))

            res['rows'][0]['pkgheadsrc'] = self.get_inner(
                res['rows'][0]['pkgheadsrc'])
            res['rows'][0]['pkgbodysrc'] = self.get_inner(
                res['rows'][0]['pkgbodysrc'])

            SQL = render_template("/".join([self.template_path, 'acl.sql']),
                                  scid=scid,
                                  pkgid=pkgid)

            status, rset1 = self.conn.execute_dict(SQL)

            if not status:
                return internal_server_error(errormsg=rset1)

            for row in rset1['rows']:
                priv = parse_priv_from_db(row)
                res['rows'][0].setdefault(row['deftype'], []).append(priv)

            # Making copy of output for further processing
            old_data = dict(res['rows'][0])

            # To format privileges data coming from client
            for key in ['pkgacl']:
                if key in data and data[key] is not None:
                    if 'added' in data[key]:
                        data[key]['added'] = parse_priv_to_db(
                            data[key]['added'], self.acl)
                    if 'changed' in data[key]:
                        data[key]['changed'] = parse_priv_to_db(
                            data[key]['changed'], self.acl)
                    if 'deleted' in data[key]:
                        data[key]['deleted'] = parse_priv_to_db(
                            data[key]['deleted'], self.acl)

            # If name is not present with in update data then copy it
            # from old data
            for arg in required_args:
                if arg not in data:
                    data[arg] = old_data[arg]

            SQL = render_template("/".join([self.template_path, 'update.sql']),
                                  data=data,
                                  o_data=old_data,
                                  conn=self.conn,
                                  is_schema_diff=diff_schema)
            return SQL, data['name'] if 'name' in data else old_data['name']
        else:
            # To format privileges coming from client
            if 'pkgacl' in data:
                data['pkgacl'] = parse_priv_to_db(data['pkgacl'], self.acl)

            SQL = render_template("/".join([self.template_path, 'create.sql']),
                                  data=data,
                                  conn=self.conn)

            return SQL, data['name']
示例#39
0
    def create(self, gid, sid, did, scid):
        """
        Create the sequence.

        Args:
          gid: Server Group ID
          sid: Server ID
          did: Database ID
          scid: Schema ID

        Returns:

        """
        required_args = [
            u'name',
            u'schema',
            u'seqowner',
        ]

        data = request.form if request.form else json.loads(
            request.data, encoding='utf-8'
        )

        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=400,
                    success=0,
                    errormsg=_(
                        "Could not find the required parameter (%s)." % arg
                    )
                )
        # The SQL below will execute CREATE DDL only
        SQL = render_template(
            "/".join([self.template_path, 'create.sql']),
            data=data, conn=self.conn
        )
        status, msg = self.conn.execute_scalar(SQL)
        if not status:
            return internal_server_error(errormsg=msg)

        if 'relacl' in data:
            data['relacl'] = parse_priv_to_db(data['relacl'], 'DATABASE')

        # The SQL below will execute rest DMLs because we cannot execute CREATE with any other
        SQL = render_template("/".join([self.template_path, 'grant.sql']), data=data, conn=self.conn)
        SQL = SQL.strip('\n').strip(' ')
        if SQL and SQL != "":
            status, msg = self.conn.execute_scalar(SQL)
            if not status:
                return internal_server_error(errormsg=msg)

        # We need oid of newly created sequence.
        SQL = render_template(
            "/".join([self.template_path, 'get_oid.sql']),
            name=data['name'],
            schema=data['schema']
        )
        SQL = SQL.strip('\n').strip(' ')

        status, rset= self.conn.execute_2darray(SQL)
        if not status:
            return internal_server_error(errormsg=rset)

        row=rset['rows'][0]
        return jsonify(
            node=self.blueprint.generate_browser_node(
                row['oid'],
                row['relnamespace'],
                data['name'],
                icon="icon-%s" % self.node_type
            )
        )
示例#40
0
    def _get_sql(self, gid, sid, did, scid, data, fnid=None, is_sql=False):
        """
        Generates the SQL statements to create/update the Function.

        Args:
            gid: Server Group Id
            sid: Server Id
            did: Database Id
            scid: Schema Id
            data: Function data
            fnid: Function Id
        """

        try:
            vol_dict = {'v': 'VOLATILE', 's': 'STABLE', 'i': 'IMMUTABLE'}

            # Get Schema Name from its OID.
            if 'pronamespace' in data:
                data['pronamespace'] = self._get_schema(data[
                                                            'pronamespace'])
            if 'provolatile' in data:
                data['provolatile'] = vol_dict[data['provolatile']]

            if fnid is not None:
                # Edit Mode

                # Fetch Old Data from database.
                old_data = self._fetch_properties(gid, sid, did, scid, fnid)

                # Get Schema Name
                old_data['pronamespace'] = self._get_schema(old_data[
                                                                'pronamespace'])

                if 'provolatile' in old_data:
                    old_data['provolatile'] = vol_dict[old_data['provolatile']]

                # If any of the below argument is changed,
                # then CREATE OR REPLACE SQL statement should be called
                fun_change_args = ['lanname', 'prosrc', 'probin', 'prosrc_c',
                                   'provolatile', 'proisstrict', 'prosecdef',
                                   'procost', 'proleakproof', 'arguments']

                data['change_func'] = False
                for arg in fun_change_args:
                    if arg == 'arguments' and arg in data and len(data[arg]) \
                            > 0:
                        data['change_func'] = True
                    elif arg in data:
                        data['change_func'] = True

                # If Function Definition/Arguments are changed then merge old
                #  Arguments with changed ones for Create/Replace Function
                # SQL statement
                if 'arguments' in data and len(data['arguments']) > 0:
                    for arg in data['arguments']['changed']:
                        for old_arg in old_data['arguments']:
                            if arg['argid'] == old_arg['argid']:
                                old_arg.update(arg)
                                break
                    data['arguments'] = old_data['arguments']
                elif data['change_func']:
                    data['arguments'] = old_data['arguments']

                # Parse Privileges
                if 'acl' in data:
                    for key in ['added', 'deleted', 'changed']:
                        if key in data['acl']:
                            data['acl'][key] = parse_priv_to_db(
                                data['acl'][key], ["X"])

                # Parse Variables
                chngd_variables = {}
                data['merged_variables'] = []
                old_data['chngd_variables'] = {}
                del_variables = {}

                # If Function Definition/Arguments are changed then,
                # Merge old, new (added, changed, deleted) variables,
                # which will be used in the CREATE or REPLACE Function sql
                # statement

                if data['change_func']:
                    # To compare old and new variables, preparing name :
                    # value dict

                    # Deleted Variables
                    if 'variables' in data and 'deleted' in data['variables']:
                        for v in data['variables']['deleted']:
                            del_variables[v['name']] = v['value']

                    if 'variables' in data and 'changed' in data['variables']:
                        for v in data['variables']['changed']:
                            chngd_variables[v['name']] = v['value']

                    if 'variables' in data and 'added' in data['variables']:
                        for v in data['variables']['added']:
                            chngd_variables[v['name']] = v['value']

                    for v in old_data['variables']:
                        old_data['chngd_variables'][v['name']] = v['value']

                    # Prepare final dict of new and old variables
                    for name, val in old_data['chngd_variables'].items():
                        if name not in chngd_variables and name not in \
                                del_variables:
                            chngd_variables[name] = val

                    # Prepare dict in [{'name': var_name, 'value': var_val},..]
                    # format
                    for name, val in chngd_variables.items():
                        data['merged_variables'].append({'name': name,
                                                         'value': val})
                else:
                    if 'variables' in data and 'changed' in data['variables']:
                        for v in data['variables']['changed']:
                            data['merged_variables'].append(v)

                    if 'variables' in data and 'added' in data['variables']:
                        for v in data['variables']['added']:
                            data['merged_variables'].append(v)

                SQL = render_template(
                    "/".join([self.sql_template_path, 'update.sql']),
                    data=data, o_data=old_data
                )
            else:
                # Parse Privileges
                if 'acl' in data:
                    data['acl'] = parse_priv_to_db(data['acl'], ["X"])

                args = ''
                cnt = 1
                if 'arguments' in data:
                    for a in data['arguments']:
                        if (('argmode' in a and a['argmode'] != 'OUT' and
                                     a['argmode'] is not None
                             ) or 'argnode' not in a):
                            if 'argmode' in a:
                                args += a['argmode'] + " "
                            if 'argname' in a and a['argname'] != '' \
                                    and a['argname'] is not None:
                                args += self.qtIdent(
                                    self.conn, a['argname']) + " "
                            if 'argtype' in a:
                                args += a['argtype']
                            if cnt < len(data['arguments']):
                                args += ', '
                        cnt += 1

                data['func_args'] = args.strip(' ')
                # Create mode
                SQL = render_template("/".join([self.sql_template_path,
                                                'create.sql']),
                                      data=data, is_sql=is_sql)
            return True, SQL.strip('\n')

        except Exception as e:
            return False, e
示例#41
0
    def create(self, gid, sid, did, scid):
        """
        This function will creates new the table object

         Args:
           gid: Server Group ID
           sid: Server ID
           did: Database ID
           scid: Schema ID
        """
        data = request.form if request.form else json.loads(
            request.data, encoding='utf-8'
        )

        for k, v in data.items():
            try:
                data[k] = json.loads(v, encoding='utf-8')
            except (ValueError, TypeError, KeyError):
                data[k] = v

        required_args = [
            'name'
        ]

        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=410,
                    success=0,
                    errormsg=gettext(
                        "Could not find the required parameter (%s)." % arg
                    )
                )

        # Parse privilege data coming from client according to database format
        if 'relacl' in data:
            data['relacl'] = parse_priv_to_db(data['relacl'], self.acl)

        # Parse & format columns
        data = self._parse_format_columns(data)
        data = TableView.check_and_convert_name_to_string(data)

        # 'coll_inherits' is Array but it comes as string from browser
        # We will convert it again to list
        if 'coll_inherits' in data and \
                isinstance(data['coll_inherits'], str):
            data['coll_inherits'] = json.loads(
                data['coll_inherits'], encoding='utf-8'
            )

        if 'foreign_key' in data:
            for c in data['foreign_key']:
                SQL = render_template(
                    "/".join([
                        self.foreign_key_template_path, 'get_parent.sql'
                    ]),
                    tid=c['columns'][0]['references']
                )
                status, rset = self.conn.execute_2darray(SQL)
                if not status:
                    return internal_server_error(errormsg=rset)

                c['remote_schema'] = rset['rows'][0]['schema']
                c['remote_table'] = rset['rows'][0]['table']

        try:
            partitions_sql = ''
            if self.is_table_partitioned(data):
                data['relkind'] = 'p'
                # create partition scheme
                data['partition_scheme'] = self.get_partition_scheme(data)
                partitions_sql = self.get_partitions_sql(data)

            SQL = render_template(
                "/".join([self.table_template_path, 'create.sql']),
                data=data, conn=self.conn
            )

            # Append SQL for partitions
            SQL += '\n' + partitions_sql

            status, res = self.conn.execute_scalar(SQL)
            if not status:
                return internal_server_error(errormsg=res)

            # PostgreSQL truncates the table name to 63 characters.
            # Have to truncate the name like PostgreSQL to get the
            # proper OID
            CONST_MAX_CHAR_COUNT = 63

            if len(data['name']) > CONST_MAX_CHAR_COUNT:
                data['name'] = data['name'][0:CONST_MAX_CHAR_COUNT]

            # Get updated schema oid
            SQL = render_template(
                "/".join([self.table_template_path, 'get_schema_oid.sql']),
                tname=data['name']
            )

            status, scid = self.conn.execute_scalar(SQL)
            if not status:
                return internal_server_error(errormsg=scid)

            # we need oid to to add object in tree at browser
            SQL = render_template(
                "/".join([self.table_template_path, 'get_oid.sql']),
                scid=scid, data=data
            )

            status, tid = self.conn.execute_scalar(SQL)
            if not status:
                return internal_server_error(errormsg=tid)

            return jsonify(
                node=self.blueprint.generate_browser_node(
                    tid,
                    scid,
                    data['name'],
                    icon=self.get_icon_css_class(data),
                    is_partitioned=self.is_table_partitioned(data)
                )
            )
        except Exception as e:
            return internal_server_error(errormsg=str(e))
示例#42
0
    def create(self, gid, sid):
        """Create the database."""
        required_args = [
            u'name'
        ]

        data = request.form if request.form else json.loads(
            request.data, encoding='utf-8'
        )

        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=410,
                    success=0,
                    errormsg=_(
                        "Could not find the required parameter (%s)." % arg
                    )
                )
        # The below SQL will execute CREATE DDL only
        SQL = render_template(
            "/".join([self.template_path, 'create.sql']),
            data=data, conn=self.conn
        )
        status, msg = self.conn.execute_scalar(SQL)
        if not status:
            return internal_server_error(errormsg=msg)

        if 'datacl' in data:
            data['datacl'] = parse_priv_to_db(data['datacl'], 'DATABASE')

        # The below SQL will execute rest DMLs because we cannot execute
        # CREATE with any other
        SQL = render_template(
            "/".join([self.template_path, 'grant.sql']),
            data=data, conn=self.conn
        )
        SQL = SQL.strip('\n').strip(' ')
        if SQL and SQL != "":
            status, msg = self.conn.execute_scalar(SQL)
            if not status:
                return internal_server_error(errormsg=msg)

        # We need oid of newly created database
        SQL = render_template(
            "/".join([self.template_path, 'properties.sql']),
            name=data['name'], conn=self.conn, last_system_oid=0
        )
        SQL = SQL.strip('\n').strip(' ')
        if SQL and SQL != "":
            status, res = self.conn.execute_dict(SQL)
            if not status:
                return internal_server_error(errormsg=res)

        response = res['rows'][0]

        return jsonify(
            node=self.blueprint.generate_browser_node(
                response['did'],
                sid,
                response['name'],
                icon="icon-database-not-connected",
                connected=False,
                tablespace=response['default_tablespace'],
                allowConn=True,
                canCreate=response['cancreate'],
                canDisconn=True,
                canDrop=True
            )
        )
示例#43
0
    def sql(self, gid, sid, did, fid, fsid):
        """
        This function will generate sql to show it in sql pane for the
        selected foreign server node.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            fid: Foreign data wrapper ID
            fsid: Foreign server ID
        """

        sql = render_template("/".join([self.template_path, 'properties.sql']),
                              fsid=fsid, conn=self.conn)
        status, res = self.conn.execute_dict(sql)
        if not status:
            return internal_server_error(errormsg=res)
        if len(res['rows']) == 0:
            return gone(
                gettext("Could not find the foreign server information.")
            )

        is_valid_options = False
        if res['rows'][0]['fsrvoptions'] is not None:
            res['rows'][0]['fsrvoptions'] = tokenize_options(
                res['rows'][0]['fsrvoptions'], 'fsrvoption', 'fsrvvalue'
            )

            if len(res['rows'][0]['fsrvoptions']) > 0:
                is_valid_options = True

        sql = render_template("/".join([self.template_path, 'acl.sql']),
                              fsid=fsid)
        status, fs_rv_acl_res = self.conn.execute_dict(sql)
        if not status:
            return internal_server_error(errormsg=fs_rv_acl_res)

        for row in fs_rv_acl_res['rows']:
            privilege = parse_priv_from_db(row)
            if row['deftype'] in res['rows'][0]:
                res['rows'][0][row['deftype']].append(privilege)
            else:
                res['rows'][0][row['deftype']] = [privilege]

        # To format privileges
        if 'fsrvacl' in res['rows'][0]:
            res['rows'][0]['fsrvacl'] = parse_priv_to_db(
                res['rows'][0]['fsrvacl'],
                ['U']
            )

        sql = render_template("/".join([self.template_path, 'properties.sql']),
                              fdwid=fid, conn=self.conn)
        status, res1 = self.conn.execute_dict(sql)
        if not status:
            return internal_server_error(errormsg=res1)

        fdw_data = res1['rows'][0]

        sql = ''
        sql = render_template("/".join([self.template_path, 'create.sql']),
                              data=res['rows'][0], fdwdata=fdw_data,
                              is_valid_options=is_valid_options,
                              conn=self.conn)
        sql += "\n"

        sql_header = u"""-- Foreign Server: {0}

-- DROP SERVER {0}

""".format(res['rows'][0]['name'])

        sql = sql_header + sql

        return ajax_response(response=sql.strip('\n'))
示例#44
0
    def create(self, gid, sid, did, scid):
        """
        Create the sequence.

        Args:
          gid: Server Group ID
          sid: Server ID
          did: Database ID
          scid: Schema ID

        Returns:

        """
        required_args = [
            'name',
            'schema',
            'seqowner',
        ]

        data = request.form if request.form else json.loads(
            request.data, encoding='utf-8'
        )

        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=400,
                    success=0,
                    errormsg=_(
                        "Could not find the required parameter ({})."
                    ).format(arg)
                )

        try:
            # The SQL below will execute CREATE DDL only
            sql = render_template(
                "/".join([self.template_path, self._CREATE_SQL]),
                data=data, conn=self.conn
            )
        except Exception as e:
            return internal_server_error(errormsg=e)

        status, msg = self.conn.execute_scalar(sql)
        if not status:
            return internal_server_error(errormsg=msg)

        if 'relacl' in data:
            data['relacl'] = parse_priv_to_db(data['relacl'], self.acl)

        # The SQL below will execute rest DMLs because we cannot execute
        # CREATE with any other
        sql = render_template(
            "/".join([self.template_path, self._GRANT_SQL]),
            data=data, conn=self.conn
        )
        sql = sql.strip('\n').strip(' ')
        if sql and sql != "":
            status, msg = self.conn.execute_scalar(sql)
            if not status:
                return internal_server_error(errormsg=msg)

        # We need oid of newly created sequence.
        sql = render_template(
            "/".join([self.template_path, self._OID_SQL]),
            name=data['name'],
            schema=data['schema']
        )
        sql = sql.strip('\n').strip(' ')

        status, rset = self.conn.execute_2darray(sql)
        if not status:
            return internal_server_error(errormsg=rset)

        row = rset['rows'][0]
        return jsonify(
            node=self.blueprint.generate_browser_node(
                row['oid'],
                row['relnamespace'],
                data['name'],
                icon=self.node_icon
            )
        )
示例#45
0
    def get_sql(self, scid, tid, clid, data, is_sql=False):
        """
        This function will genrate sql from model data
        """
        data = column_utils.convert_length_precision_to_string(data)

        if clid is not None:
            SQL = render_template(
                "/".join([self.template_path, 'properties.sql']),
                tid=tid,
                clid=clid,
                show_sys_objects=self.blueprint.show_system_objects)

            status, res = self.conn.execute_dict(SQL)
            if not status:
                return internal_server_error(errormsg=res)
            if len(res['rows']) == 0:
                return gone(
                    gettext("Could not find the column on the server."))
            old_data = dict(res['rows'][0])
            # We will add table & schema as well
            old_data = column_utils.column_formatter(self.conn, tid, clid,
                                                     old_data)

            # check type for '[]' in it
            if 'cltype' in old_data:
                old_data['cltype'], old_data['hasSqrBracket'] = \
                    column_utils.type_formatter(old_data['cltype'])

                if 'cltype' in data and data['cltype'] != old_data['cltype']:
                    length, precision, typeval = \
                        self.get_length_precision(data['cltype'])

                    # if new datatype does not have length or precision
                    # then we cannot apply length or precision of old
                    # datatype to new one.
                    if not length:
                        old_data['attlen'] = -1
                    if not precision:
                        old_data['attprecision'] = None

            # If name is not present in data then
            # we will fetch it from old data, we also need schema & table name
            if 'name' not in data:
                data['name'] = old_data['name']

            # Convert acl coming from client in db parsing format
            key = 'attacl'
            if key in data and data[key] is not None:
                if 'added' in data[key]:
                    data[key]['added'] = parse_priv_to_db(
                        data[key]['added'], self.acl)
                if 'changed' in data[key]:
                    data[key]['changed'] = parse_priv_to_db(
                        data[key]['changed'], self.acl)
                if 'deleted' in data[key]:
                    data[key]['deleted'] = parse_priv_to_db(
                        data[key]['deleted'], self.acl)

            SQL = render_template("/".join([self.template_path, 'update.sql']),
                                  data=data,
                                  o_data=old_data,
                                  conn=self.conn)
        else:
            required_args = ['name', 'cltype']

            for arg in required_args:
                if arg not in data:
                    return gettext('-- definition incomplete')

            # We will convert privileges coming from client required
            # in server side format
            if 'attacl' in data:
                data['attacl'] = parse_priv_to_db(data['attacl'], self.acl)
            # If the request for new object which do not have did
            SQL = render_template("/".join([self.template_path, 'create.sql']),
                                  data=data,
                                  conn=self.conn,
                                  is_sql=is_sql)
        return SQL, data['name'] if 'name' in data else old_data['name']
示例#46
0
    def get_sql(self, gid, sid, data, scid, tid=None, is_sql=False):
        """
        This function will genrate sql from model data
        """
        if tid is not None:

            for key in ['typacl']:
                if key in data and data[key] is not None:
                    if 'added' in data[key]:
                        data[key]['added'] = parse_priv_to_db(
                            data[key]['added'], self.acl)
                    if 'changed' in data[key]:
                        data[key]['changed'] = parse_priv_to_db(
                            data[key]['changed'], self.acl)
                    if 'deleted' in data[key]:
                        data[key]['deleted'] = parse_priv_to_db(
                            data[key]['deleted'], self.acl)

            if 'composite' in data and len(data['composite']) > 0:
                for key in ['added', 'changed', 'deleted']:
                    if key in data['composite']:
                        for each_type in data['composite'][key]:
                            each_type = self. \
                                convert_length_precision_to_string(each_type)
                            if 'type' in each_type:
                                each_type['cltype'] = self._cltype_formatter(
                                    each_type['type'])
                                each_type['hasSqrBracket'] = self.hasSqrBracket

            SQL = render_template(
                "/".join([self.template_path,
                          'properties.sql']),
                scid=scid, tid=tid,
                datlastsysoid=self.datlastsysoid,
                show_system_objects=self.blueprint.show_system_objects
            )
            status, res = self.conn.execute_dict(SQL)
            if not status:
                return internal_server_error(errormsg=res)
            if len(res['rows']) == 0:
                return gone(
                    gettext("Could not find the type in the database.")
                )

            # Making copy of output for future use
            old_data = dict(res['rows'][0])

            SQL = render_template("/".join([self.template_path, 'acl.sql']),
                                  scid=scid, tid=tid)
            status, acl = self.conn.execute_dict(SQL)
            if not status:
                return internal_server_error(errormsg=acl)

            # We will set get privileges from acl sql so we don't need
            # it from properties sql
            old_data['typacl'] = []

            for row in acl['rows']:
                priv = parse_priv_from_db(row)
                if row['deftype'] in old_data:
                    old_data[row['deftype']].append(priv)
                else:
                    old_data[row['deftype']] = [priv]

            # Calling function to check and additional properties if available
            old_data.update(self.additional_properties(old_data, tid))
            old_data = self._convert_for_sql(old_data)

            SQL = render_template(
                "/".join([self.template_path, 'update.sql']),
                data=data, o_data=old_data, conn=self.conn
            )
        else:
            required_args = [
                'name',
                'typtype'
            ]

            for arg in required_args:
                if arg not in data:
                    return "-- definition incomplete"

            # Additional checks go here
            # If type is composite then check if it has two members
            if data and data[arg] == 'c':
                if len(data['composite']) < 2:
                    return "-- definition incomplete"

            # If type is range then check if subtype is defined or not
            if data and data[arg] == 'r':
                if 'typname' not in data or data['typname'] is None:
                    return "-- definition incomplete"

            # If type is external then check if input/output
            # conversion function is defined
            if data and data[arg] == 'b':
                if (
                    'typinput' not in data or
                    'typoutput' not in data or
                    data['typinput'] is None or
                    data['typoutput'] is None
                ):
                    return "-- definition incomplete"

            # Privileges
            if 'typacl' in data and data['typacl'] is not None:
                data['typacl'] = parse_priv_to_db(data['typacl'], self.acl)

            data = self._convert_for_sql(data)

            if 'composite' in data and len(data['composite']) > 0:
                for each_type in data['composite']:
                    each_type = self.convert_length_precision_to_string(
                        each_type)
                    each_type['cltype'] = self._cltype_formatter(
                        each_type['type'])
                    each_type['hasSqrBracket'] = self.hasSqrBracket

            SQL = render_template("/".join([self.template_path,
                                            'create.sql']),
                                  data=data, conn=self.conn, is_sql=is_sql)

        return SQL, data['name'] if 'name' in data else old_data['name']
示例#47
0
文件: __init__.py 项目: tvar/pgadmin4
    def sql(self, gid, sid, did, fid, fsid, json_resp=True):
        """
        This function will generate sql to show it in sql pane for the
        selected foreign server node.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            fid: Foreign data wrapper ID
            fsid: Foreign server ID
            json_resp:
        """

        sql = render_template("/".join([self.template_path,
                                        self._PROPERTIES_SQL]),
                              fsid=fsid, conn=self.conn)
        status, res = self.conn.execute_dict(sql)
        if not status:
            return internal_server_error(errormsg=res)
        if len(res['rows']) == 0:
            return gone(self.not_found_error_msg())

        if fid is None and 'fdwid' in res['rows'][0]:
            fid = res['rows'][0]['fdwid']

        is_valid_options = False
        if res['rows'][0]['fsrvoptions'] is not None:
            res['rows'][0]['fsrvoptions'] = tokenize_options(
                res['rows'][0]['fsrvoptions'], 'fsrvoption', 'fsrvvalue'
            )

            if len(res['rows'][0]['fsrvoptions']) > 0:
                is_valid_options = True

        sql = render_template("/".join([self.template_path, self._ACL_SQL]),
                              fsid=fsid)
        status, fs_rv_acl_res = self.conn.execute_dict(sql)
        if not status:
            return internal_server_error(errormsg=fs_rv_acl_res)

        for row in fs_rv_acl_res['rows']:
            privilege = parse_priv_from_db(row)
            if row['deftype'] in res['rows'][0]:
                res['rows'][0][row['deftype']].append(privilege)
            else:
                res['rows'][0][row['deftype']] = [privilege]

        # To format privileges
        if 'fsrvacl' in res['rows'][0]:
            res['rows'][0]['fsrvacl'] = parse_priv_to_db(
                res['rows'][0]['fsrvacl'],
                ['U']
            )

        sql = render_template("/".join([self.template_path,
                                        self._PROPERTIES_SQL]),
                              fdwid=fid, conn=self.conn)
        status, res1 = self.conn.execute_dict(sql)
        if not status:
            return internal_server_error(errormsg=res1)

        fdw_data = res1['rows'][0]

        sql = ''
        sql = render_template("/".join([self.template_path,
                                        self._CREATE_SQL]),
                              data=res['rows'][0], fdwdata=fdw_data,
                              is_valid_options=is_valid_options,
                              conn=self.conn)
        sql += "\n"

        sql_header = """-- Foreign Server: {0}

-- DROP SERVER IF EXISTS {0}

""".format(res['rows'][0]['name'])

        sql = sql_header + sql

        if not json_resp:
            return sql.strip('\n')

        return ajax_response(response=sql.strip('\n'))
示例#48
0
def save(sid, did):
    """
    This function will apply the privileges to the selected
    Database Objects
    """
    server_prop = server_info
    data = request.form if request.form else json.loads(request.data.decode())

    # Form db connection and we use conn to execute sql
    manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid)
    conn = manager.connection(did=did)

    acls = []
    try:
        acls = render_template(
            "/".join([server_prop['template_path'], 'acl.json']),
        )
        acls = json.loads(acls)
    except Exception as e:
        current_app.logger.exception(e)

    try:

        # Parse privileges
        data['priv'] = {}
        if 'acl' in data:
            # Get function acls
            data['priv']['function'] = parse_priv_to_db(
                data['acl'],
                acls['function']['acl'])

            data['priv']['sequence'] = parse_priv_to_db(
                data['acl'],
                acls['sequence']['acl'])

            data['priv']['table'] = parse_priv_to_db(
                data['acl'],
                acls['table']['acl'])

        # Pass database objects and get SQL for privileges
        # Pass database objects and get SQL for privileges
        SQL_data = ''
        data_func = {'objects': data['objects'],
                     'priv': data['priv']['function']}
        SQL = render_template(
            "/".join([server_prop['template_path'],
                      '/sql/grant_function.sql']),
            data=data_func, conn=conn)
        if SQL and SQL.strip('\n') != '':
            SQL_data += SQL

        data_seq = {'objects': data['objects'],
                    'priv': data['priv']['sequence']}
        SQL = render_template(
            "/".join([server_prop['template_path'],
                      '/sql/grant_sequence.sql']),
            data=data_seq, conn=conn)
        if SQL and SQL.strip('\n') != '':
            SQL_data += SQL

        data_table = {'objects': data['objects'],
                      'priv': data['priv']['table']}
        SQL = render_template(
            "/".join([server_prop['template_path'], '/sql/grant_table.sql']),
            data=data_table, conn=conn)
        if SQL and SQL.strip('\n') != '':
            SQL_data += SQL

        status, res = conn.execute_dict(SQL_data)
        if not status:
            return internal_server_error(errormsg=res)

        return make_json_response(
            success=1,
            info="Privileges applied"
        )

    except Exception as e:
        return internal_server_error(errormsg=e.message)
示例#49
0
def msql(gid, sid, did):
    """
    This function will return modified SQL
    """

    server_prop = server_info
    data = {}
    for k, v in request.args.items():
        try:
            data[k] = json.loads(v)
        except ValueError:
            data[k] = v

    # Form db connection
    manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid)
    conn = manager.connection(did=did)

    acls = []
    try:
        acls = render_template("/".join(
            [server_prop['template_path'], '/acl.json']))
        acls = json.loads(acls)
    except Exception as e:
        current_app.logger.exception(e)

    try:

        # Parse privileges
        data['priv'] = {}
        if 'acl' in data:
            # Get function acls
            data['priv']['function'] = parse_priv_to_db(
                data['acl'], acls['function']['acl'])

            data['priv']['sequence'] = parse_priv_to_db(
                data['acl'], acls['sequence']['acl'])

            data['priv']['table'] = parse_priv_to_db(data['acl'],
                                                     acls['table']['acl'])

        # Pass database objects and get SQL for privileges
        SQL_data = ''
        data_func = {}
        data_func['objects'] = data['objects']
        data_func['priv'] = data['priv']['function']
        SQL = render_template("/".join(
            [server_prop['template_path'], '/sql/grant_function.sql']),
                              data=data_func,
                              conn=conn)
        if SQL and SQL.strip('\n') != '':
            SQL_data += SQL

        data_seq = {}
        data_seq['objects'] = data['objects']
        data_seq['priv'] = data['priv']['sequence']
        SQL = render_template("/".join(
            [server_prop['template_path'], '/sql/grant_sequence.sql']),
                              data=data_seq,
                              conn=conn)
        if SQL and SQL.strip('\n') != '':
            SQL_data += SQL

        data_table = {}
        data_table['objects'] = data['objects']
        data_table['priv'] = data['priv']['table']
        SQL = render_template("/".join(
            [server_prop['template_path'], '/sql/grant_table.sql']),
                              data=data_table,
                              conn=conn)
        if SQL and SQL.strip('\n') != '':
            SQL_data += SQL

        res = {'data': SQL_data}

        return ajax_response(response=res, status=200)

    except Exception as e:
        return make_json_response(status=410, success=0, errormsg=e.message)
示例#50
0
def msql(sid, did):
    """
    This function will return modified SQL
    """

    server_prop = server_info
    data = {}
    for k, v in request.args.items():
        try:
            data[k] = json.loads(v)
        except ValueError:
            data[k] = v

    # Form db connection
    manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid)
    conn = manager.connection(did=did)

    acls = []
    try:
        acls = render_template(
            "/".join([server_prop['template_path'], '/acl.json'])
        )
        acls = json.loads(acls)
    except Exception as e:
        current_app.logger.exception(e)

    try:

        # Parse privileges
        data['priv'] = {}
        if 'acl' in data:
            # Get function acls
            data['priv']['function'] = parse_priv_to_db(
                data['acl'],
                acls['function']['acl'])

            data['priv']['sequence'] = parse_priv_to_db(
                data['acl'],
                acls['sequence']['acl'])

            data['priv']['table'] = parse_priv_to_db(
                data['acl'],
                acls['table']['acl'])

        # Pass database objects and get SQL for privileges
        SQL_data = ''
        data_func = {'objects': data['objects'],
                     'priv': data['priv']['function']}
        SQL = render_template(
            "/".join([server_prop['template_path'],
                      '/sql/grant_function.sql']),
            data=data_func, conn=conn)
        if SQL and SQL.strip('\n') != '':
            SQL_data += SQL

        data_seq = {'objects': data['objects'],
                    'priv': data['priv']['sequence']}
        SQL = render_template(
            "/".join([server_prop['template_path'],
                      '/sql/grant_sequence.sql']),
            data=data_seq, conn=conn)
        if SQL and SQL.strip('\n') != '':
            SQL_data += SQL

        data_table = {'objects': data['objects'],
                      'priv': data['priv']['table']}
        SQL = render_template(
            "/".join([server_prop['template_path'], '/sql/grant_table.sql']),
            data=data_table, conn=conn)
        if SQL and SQL.strip('\n') != '':
            SQL_data += SQL

        res = {'data': SQL_data}

        return ajax_response(
            response=res,
            status=200
        )

    except Exception as e:
        return make_json_response(
            status=410,
            success=0,
            errormsg=e.message
        )
示例#51
0
    def create(self, gid, sid, did):
        """
        This function will create the foreign data wrapper node.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
        """
        required_args = [
            'name'
        ]

        data = request.form if request.form else json.loads(request.data.decode())
        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=410,
                    success=0,
                    errormsg=gettext(
                        "Couldn't find the required parameter (%s)." % arg
                    )
                )

        try:

            if 'fdwacl' in data:
                data['fdwacl'] = parse_priv_to_db(data['fdwacl'], ['U'])

            new_list = []

            # Allow user to set the blank value in fdwvalue field in option model
            if 'fdwoptions' in data:
                for item in data['fdwoptions']:
                    new_dict = {}
                    if item['fdwoption']:
                        if 'fdwvalue' in item and item['fdwvalue'] and item['fdwvalue'] != '':
                            new_dict.update(item);
                        else:
                            new_dict.update({'fdwoption': item['fdwoption'], 'fdwvalue': ''})

                    new_list.append(new_dict)

                data['fdwoptions'] = new_list

            sql = render_template("/".join([self.template_path, 'create.sql']), data=data, conn=self.conn)
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)

            sql = render_template("/".join([self.template_path, 'properties.sql']), fname=data['name'], conn=self.conn)

            status, r_set = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=r_set)

            for row in r_set['rows']:
                return jsonify(
                    node=self.blueprint.generate_browser_node(
                        row['fdwoid'],
                        did,
                        row['name'],
                        icon='icon-foreign_data_wrapper'
                    )
                )

        except Exception as e:
            return internal_server_error(errormsg=str(e))
示例#52
0
    def get_SQL(self,
                gid,
                sid,
                did,
                data,
                scid,
                seid=None,
                add_not_exists_clause=False):
        """
        This function will generate sql from model data.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            scid: Schema ID
            seid: Sequence ID
        """

        required_args = ['name']

        if seid is not None:
            sql = render_template("/".join(
                [self.template_path, self._PROPERTIES_SQL]),
                                  scid=scid,
                                  seid=seid)
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)
            elif len(res['rows']) == 0:
                return gone(self.not_found_error_msg())

            # Making copy of output for further processing
            old_data = dict(res['rows'][0])
            old_data = self._formatter(old_data, scid, seid)

            self._format_privilege_data(data)

            # If name is not present with in update data then copy it
            # from old data
            for arg in required_args:
                if arg not in data:
                    data[arg] = old_data[arg]
            sql = render_template("/".join(
                [self.template_path, self._UPDATE_SQL]),
                                  data=data,
                                  o_data=old_data,
                                  conn=self.conn)
            return sql, data['name'] if 'name' in data else old_data['name']
        else:
            # To format privileges coming from client
            if 'relacl' in data:
                data['relacl'] = parse_priv_to_db(data['relacl'], self.acl)

            sql = render_template("/".join(
                [self.template_path, self._CREATE_SQL]),
                                  data=data,
                                  conn=self.conn,
                                  add_not_exists_clause=add_not_exists_clause)
            sql += render_template("/".join(
                [self.template_path, self._GRANT_SQL]),
                                   data=data,
                                   conn=self.conn)
            return sql, data['name']
示例#53
0
    def sql(self, gid, sid, did, lid, json_resp=True):
        """
        This function will generate sql to show in the sql pane for the
        selected language node.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            lid: Language ID
            json_resp:
        """
        sql = render_template("/".join(
            [self.template_path, self._PROPERTIES_SQL]),
                              lid=lid)
        status, res = self.conn.execute_dict(sql)
        if not status:
            return internal_server_error(errormsg=res)

        if len(res['rows']) == 0:
            return gone(self._NOT_FOUND_LANG_INFORMATION)

        # Making copy of output for future use
        old_data = dict(res['rows'][0])

        sql = render_template("/".join([self.template_path, self._ACL_SQL]),
                              lid=lid)
        status, result = self.conn.execute_dict(sql)
        if not status:
            return internal_server_error(errormsg=result)

        for row in result['rows']:
            priv = parse_priv_from_db(row)
            if row['deftype'] in old_data:
                old_data[row['deftype']].append(priv)
            else:
                old_data[row['deftype']] = [priv]

        # To format privileges
        if 'lanacl' in old_data:
            old_data['lanacl'] = parse_priv_to_db(old_data['lanacl'], ['U'])

        seclabels = []
        if 'seclabels' in old_data and old_data['seclabels'] is not None:
            import re
            for sec in old_data['seclabels']:
                sec = re.search(r'([^=]+)=(.*$)', sec)
                seclabels.append({
                    'provider': sec.group(1),
                    'label': sec.group(2)
                })

        old_data['seclabels'] = seclabels
        sql = render_template("/".join([self.template_path, 'sqlpane.sql']),
                              data=old_data,
                              conn=self.conn)

        if not json_resp:
            return sql.strip('\n')

        return ajax_response(response=sql.strip('\n'))
示例#54
0
    def create(self, gid, sid, did, scid):
        """
        This function will creates new the table object

         Args:
           gid: Server Group ID
           sid: Server ID
           did: Database ID
           scid: Schema ID
        """
        data = request.form if request.form else json.loads(request.data,
                                                            encoding='utf-8')

        for k, v in data.items():
            try:
                data[k] = json.loads(v, encoding='utf-8')
            except (ValueError, TypeError, KeyError):
                data[k] = v

        required_args = ['name']

        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=410,
                    success=0,
                    errormsg=gettext(
                        "Could not find the required parameter (%s)." % arg))

        # Parse privilege data coming from client according to database format
        if 'relacl' in data:
            data['relacl'] = parse_priv_to_db(data['relacl'], self.acl)

        # Parse & format columns
        data = self._parse_format_columns(data)
        data = TableView.check_and_convert_name_to_string(data)

        # 'coll_inherits' is Array but it comes as string from browser
        # We will convert it again to list
        if 'coll_inherits' in data and \
                isinstance(data['coll_inherits'], str):
            data['coll_inherits'] = json.loads(data['coll_inherits'],
                                               encoding='utf-8')

        if 'foreign_key' in data:
            for c in data['foreign_key']:
                SQL = render_template("/".join(
                    [self.foreign_key_template_path, 'get_parent.sql']),
                                      tid=c['columns'][0]['references'])
                status, rset = self.conn.execute_2darray(SQL)
                if not status:
                    return internal_server_error(errormsg=rset)

                c['remote_schema'] = rset['rows'][0]['schema']
                c['remote_table'] = rset['rows'][0]['table']

        try:
            partitions_sql = ''
            if self.is_table_partitioned(data):
                data['relkind'] = 'p'
                # create partition scheme
                data['partition_scheme'] = self.get_partition_scheme(data)
                partitions_sql = self.get_partitions_sql(data)

            SQL = render_template("/".join(
                [self.table_template_path, 'create.sql']),
                                  data=data,
                                  conn=self.conn)

            # Append SQL for partitions
            SQL += '\n' + partitions_sql

            status, res = self.conn.execute_scalar(SQL)
            if not status:
                return internal_server_error(errormsg=res)

            # PostgreSQL truncates the table name to 63 characters.
            # Have to truncate the name like PostgreSQL to get the
            # proper OID
            CONST_MAX_CHAR_COUNT = 63

            if len(data['name']) > CONST_MAX_CHAR_COUNT:
                data['name'] = data['name'][0:CONST_MAX_CHAR_COUNT]

            # Get updated schema oid
            SQL = render_template("/".join(
                [self.table_template_path, 'get_schema_oid.sql']),
                                  tname=data['name'])

            status, scid = self.conn.execute_scalar(SQL)
            if not status:
                return internal_server_error(errormsg=scid)

            # we need oid to to add object in tree at browser
            SQL = render_template("/".join(
                [self.table_template_path, 'get_oid.sql']),
                                  scid=scid,
                                  data=data)

            status, tid = self.conn.execute_scalar(SQL)
            if not status:
                return internal_server_error(errormsg=tid)

            return jsonify(node=self.blueprint.generate_browser_node(
                tid,
                scid,
                data['name'],
                icon=self.get_icon_css_class(data),
                is_partitioned=self.is_table_partitioned(data)))
        except Exception as e:
            return internal_server_error(errormsg=str(e))
示例#55
0
    def sql(self, gid, sid, did, fid):
        """
        This function will generate sql to show it in sql pane
        for the selected foreign data wrapper node.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            fid: Foreign data wrapper ID
        """
        sql = render_template("/".join(
            [self.template_path, self._PROPERTIES_SQL]),
                              fid=fid,
                              conn=self.conn)
        status, res = self.conn.execute_dict(sql)
        if not status:
            return internal_server_error(errormsg=res)
        if len(res['rows']) == 0:
            return gone(
                gettext(
                    "Could not find the foreign data wrapper on the server."))

        is_valid_options = False
        if res['rows'][0]['fdwoptions'] is not None:
            res['rows'][0]['fdwoptions'] = tokenize_options(
                res['rows'][0]['fdwoptions'], 'fdwoption', 'fdwvalue')

            if len(res['rows'][0]['fdwoptions']) > 0:
                is_valid_options = True

        sql = render_template("/".join([self.template_path, self._ACL_SQL]),
                              fid=fid)
        status, fdw_acl_res = self.conn.execute_dict(sql)
        if not status:
            return internal_server_error(errormsg=fdw_acl_res)

        for row in fdw_acl_res['rows']:
            privilege = parse_priv_from_db(row)
            if row['deftype'] in res['rows'][0]:
                res['rows'][0][row['deftype']].append(privilege)
            else:
                res['rows'][0][row['deftype']] = [privilege]

        # To format privileges
        if 'fdwacl' in res['rows'][0]:
            res['rows'][0]['fdwacl'] = parse_priv_to_db(
                res['rows'][0]['fdwacl'], ['U'])

        sql = ''
        sql = render_template("/".join([self.template_path, self._CREATE_SQL]),
                              data=res['rows'][0],
                              conn=self.conn,
                              is_valid_options=is_valid_options)
        sql += "\n"

        sql_header = u"""-- Foreign Data Wrapper: {0}\n\n""".format(
            res['rows'][0]['name'])

        sql_header += """-- DROP FOREIGN DATA WRAPPER {0}

""".format(self.qtIdent(self.conn, res['rows'][0]['name']))

        sql = sql_header + sql

        return ajax_response(response=sql.strip('\n'))
示例#56
0
    def create(self, gid, sid, did, scid):
        """
        This function will creates new the type object

         Args:
           gid: Server Group ID
           sid: Server ID
           did: Database ID
           scid: Schema ID
           tid: Type ID
        """
        data = request.form if request.form else json.loads(request.data.decode())
        required_args = {
            'name': 'Name',
            'typtype': 'Type'
        }

        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=410,
                    success=0,
                    errormsg=gettext(
                        "Could not find the required parameter (%s)." %
                        required_args[arg]
                    )
                )
            # Additional checks goes here
            # If type is composite then check if it has two members
            if data and data[arg] == 'c':
                if len(data['composite']) < 2:
                    return make_json_response(
                        status=410,
                        success=0,
                        errormsg=gettext(
                            'Composite types require at least two members.'
                        )
                    )
            # If type is enum then check if it has minimum one label
            if data and data[arg] == 'e':
                if len(data['enum']) < 1:
                    return make_json_response(
                        status=410,
                        success=0,
                        errormsg=gettext(
                            'Enumeration types require at least one label.'
                        )
                    )
            # If type is range then check if subtype is defined or not
            if data and data[arg] == 'r':
                if data['typname'] is None:
                    return make_json_response(
                        status=410,
                        success=0,
                        errormsg=gettext(
                            'Subtype must be defined for range types.'
                        )
                    )
            # If type is external then check if input/output
            # conversion function is defined
            if data and data[arg] == 'b':
                if data['typinput'] is None or \
                                data['typoutput'] is None:
                    return make_json_response(
                        status=410,
                        success=0,
                        errormsg=gettext(
                            'External types require both Input and Output conversion functions.'
                        )
                    )

        # To format privileges coming from client
        if 'typacl' in data and data['typacl'] is not None:
            data['typacl'] = parse_priv_to_db(data['typacl'], self.acl)

        data = self._convert_for_sql(data)

        try:
            SQL = render_template("/".join([self.template_path, 'create.sql']),
                                  data=data, conn=self.conn)
            status, res = self.conn.execute_scalar(SQL)
            if not status:
                return internal_server_error(errormsg=res)

            # we need oid to to add object in tree at browser
            SQL = render_template("/".join([self.template_path,
                                            'get_oid.sql']),
                                  scid=scid, data=data)
            status, tid = self.conn.execute_scalar(SQL)
            if not status:
                return internal_server_error(errormsg=tid)

            return jsonify(
                node=self.blueprint.generate_browser_node(
                    tid,
                    scid,
                    data['name'],
                    icon="icon-type"
                )
            )
        except Exception as e:
            return internal_server_error(errormsg=str(e))
示例#57
0
    def get_sql(self, gid, sid, did, scid, data, foid=None):
        """
        Genrates the SQL statements to create/update the Foreign Table.

        Args:
            gid: Server Group Id
            sid: Server Id
            did: Database Id
            scid: Schema Id
            foid: Foreign Table Id
        """
        if foid is not None:
            old_data = self._fetch_properties(gid,
                                              sid,
                                              did,
                                              scid,
                                              foid,
                                              inherits=True)
            if old_data is False:
                return gone(
                    gettext("Could not find the foreign table on the server."))

            # Prepare dict of columns with key = column's attnum
            # Will use this in the update template when any column is
            # changed, to identify the columns.
            col_data = {}
            for c in old_data['columns']:
                col_data[c['attnum']] = c

            old_data['columns'] = col_data

            if 'columns' in data and 'added' in data['columns']:
                data['columns']['added'] = self._format_columns(
                    data['columns']['added'])

            if 'columns' in data and 'changed' in data['columns']:
                data['columns']['changed'] = self._format_columns(
                    data['columns']['changed'])

                # Parse Column Options
                for c in data['columns']['changed']:
                    old_col_options = c['attfdwoptions'] = []
                    if 'attfdwoptions' in c and c['attfdwoptions']:
                        old_col_options = c['attfdwoptions']

                    old_col_frmt_options = {}

                    for o in old_col_options:
                        col_opt = o.split("=")
                        old_col_frmt_options[col_opt[0]] = col_opt[1]

                    c['coloptions_updated'] = {
                        'added': [],
                        'changed': [],
                        'deleted': []
                    }

                    if 'coloptions' in c and len(c['coloptions']) > 0:
                        for o in c['coloptions']:
                            if (o['option'] in old_col_frmt_options
                                    and o['value'] !=
                                    old_col_frmt_options[o['option']]):
                                c['coloptions_updated']['changed'].append(o)
                            elif o['option'] not in old_col_frmt_options:
                                c['coloptions_updated']['added'].append(o)
                            if o['option'] in old_col_frmt_options:
                                del old_col_frmt_options[o['option']]

                    for o in old_col_frmt_options:
                        c['coloptions_updated']['deleted'].append(
                            {'option': o})

            # Parse Privileges
            if 'acl' in data and 'added' in data['acl']:
                data['acl']['added'] = parse_priv_to_db(
                    data['acl']['added'], ["a", "r", "w", "x"])
            if 'acl' in data and 'changed' in data['acl']:
                data['acl']['changed'] = parse_priv_to_db(
                    data['acl']['changed'], ["a", "r", "w", "x"])
            if 'acl' in data and 'deleted' in data['acl']:
                data['acl']['deleted'] = parse_priv_to_db(
                    data['acl']['deleted'], ["a", "r", "w", "x"])

            SQL = render_template("/".join([self.template_path, 'update.sql']),
                                  data=data,
                                  o_data=old_data)
            return SQL, data['name'] if 'name' in data else old_data['name']
        else:
            data['columns'] = self._format_columns(data['columns'])

            # Parse Privileges
            if 'acl' in data:
                data['acl'] = parse_priv_to_db(data['acl'],
                                               ["a", "r", "w", "x"])

            SQL = render_template("/".join([self.template_path, 'create.sql']),
                                  data=data)
            return SQL, data['name']
示例#58
0
    def create(self, gid, sid, did, fid):
        """
        This function will create the foreign server node.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            fid: foreign data wrapper ID
        """

        required_args = [
            'name'
        ]

        data = request.form if request.form else json.loads(
            request.data, encoding='utf-8'
        )
        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=410,
                    success=0,
                    errormsg=gettext(
                        "Could not find the required parameter (%s)." % arg
                    )
                )
        try:
            if 'fsrvacl' in data:
                data['fsrvacl'] = parse_priv_to_db(data['fsrvacl'], ['U'])

            sql = render_template("/".join([self.template_path,
                                            'properties.sql']),
                                  fdwid=fid, conn=self.conn)
            status, res1 = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res1)

            fdw_data = res1['rows'][0]

            is_valid_options = False
            if 'fsrvoptions' in data:
                is_valid_options, data['fsrvoptions'] = validate_options(
                    data['fsrvoptions'], 'fsrvoption', 'fsrvvalue'
                )

            sql = render_template("/".join([self.template_path, 'create.sql']),
                                  data=data, fdwdata=fdw_data,
                                  is_valid_options=is_valid_options,
                                  conn=self.conn)
            status, res = self.conn.execute_scalar(sql)
            if not status:
                return internal_server_error(errormsg=res)

            sql = render_template("/".join([self.template_path,
                                            'properties.sql']),
                                  data=data, fdwdata=fdw_data,
                                  conn=self.conn)
            status, r_set = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=r_set)

            return jsonify(
                node=self.blueprint.generate_browser_node(
                    r_set['rows'][0]['fsrvid'],
                    fid,
                    r_set['rows'][0]['name'],
                    icon="icon-foreign_server"
                )
            )
        except Exception as e:
            return internal_server_error(errormsg=str(e))