pub fn to_sql(&self, user: &str) -> String {
        let mut sqls = vec![];
        let mut tables = self
            .tables
            .iter()
            .map(|t| Table::new(t))
            .collect::<Vec<Table>>();

        // grant all privileges if grants contains "ALL"
        let grants = if self.grants.contains(&"ALL".to_string()) {
            "ALL PRIVILEGES".to_string()
        } else {
            self.grants.join(", ")
        };

        // if `tables` only contains `ALL`
        if let Some(table_named_all) = tables.iter().find(|t| t.name == "ALL") {
            let sql = match table_named_all.sign.as_str() {
                "+" => format!(
                    "GRANT {} ON ALL TABLES IN SCHEMA {} TO {};",
                    grants,
                    self.schemas.join(", "),
                    user
                ),
                "-" => format!(
                    "REVOKE {} ON ALL TABLES IN SCHEMA {} FROM {};",
                    grants,
                    self.schemas.join(", "),
                    user
                ),
                _ => "".to_string(),
            };
            sqls.push(sql);

            // remove name `ALL` and all tables start with `+`
            for table in tables.clone() {
                if table.name == "ALL" || table.sign == "+" {
                    tables.retain(|x| x != &table);
                }
            }
        }

        // grant on tables sign `+`
        let grant_tables = tables.iter().filter(|x| x.sign == "+").collect::<Vec<_>>();
        if !grant_tables.is_empty() {
            let _with_schema = grant_tables
                .iter()
                .flat_map(|t| {
                    if t.name.contains('.') {
                        vec![t.name.clone()]
                    } else {
                        self.schemas
                            .iter()
                            .map(|s| format!("{}.{}", s, &t.name))
                            .collect::<Vec<_>>()
                    }
                })
                .collect::<Vec<String>>()
                .join(", ");

            let sql = format!("GRANT {} ON {} TO {};", grants, _with_schema, user);
            sqls.push(sql);

            // remove all tables start with `+`
            for table in tables.clone() {
                if table.sign == "+" {
                    tables.retain(|x| x != &table);
                }
            }
        }

        // revoke on tables start with `-`
        let revoke_tables = tables.iter().filter(|x| x.sign == "-").collect::<Vec<_>>();
        if !revoke_tables.is_empty() {
            let _with_schema = revoke_tables
                .iter()
                .flat_map(|t| {
                    if t.name.contains('.') {
                        vec![t.name.clone()]
                    } else {
                        self.schemas
                            .iter()
                            .map(|s| format!("{}.{}", s, &t.name))
                            .collect::<Vec<_>>()
                    }
                })
                .collect::<Vec<String>>()
                .join(", ");

            let sql = format!("REVOKE {} ON {} FROM {};", grants, _with_schema, user);
            sqls.push(sql);
        }

        sqls.join(" ")
    }
/// Generate role table to sql.
///
/// ```sql
/// {GRANT | REVOKE} { { SELECT | INSERT | UPDATE | DELETE | DROP | REFERENCES } [,...] | ALL [ PRIVILEGES ] }
/// ON { [ TABLE ] table_name [, ...] | ALL TABLES IN SCHEMA schema_name [, ...] }
/// TO { username [ WITH GRANT OPTION ] | GROUP group_name | PUBLIC } [, ...]
/// ```
Generate role table to sql.
Generate role table to sql .
pub fn to_sql(&self, user: &str) -> String {
        let mut sqls = vec![];
        let mut tables = self
            .tables
            .iter()
            .map(|t| Table::new(t))
            .collect::<Vec<Table>>();
        // grant all privileges if grants contains "ALL"
        let grants = if self.grants.contains(&"ALL".to_string()) {
            "ALL PRIVILEGES".to_string()
        } else {
            self.grants.join(", ")
        };
        // if `tables` only contains `ALL`
        if let Some(table_named_all) = tables.iter().find(|t| t.name == "ALL") {
            let sql = match table_named_all.sign.as_str() {
                "+" => format!(
                    "GRANT {} ON ALL TABLES IN SCHEMA {} TO {};",
                    grants,
                    self.schemas.join(", "),
                    user
                ),
                "-" => format!(
                    "REVOKE {} ON ALL TABLES IN SCHEMA {} FROM {};",
                    grants,
                    self.schemas.join(", "),
                    user
                ),
                _ => "".to_string(),
            };
            sqls.push(sql);
            // remove name `ALL` and all tables start with `+`
            for table in tables.clone() {
                if table.name == "ALL" || table.sign == "+" {
                    tables.retain(|x| x != &table);
                }
            }
        }
        // grant on tables sign `+`
        let grant_tables = tables.iter().filter(|x| x.sign == "+").collect::<Vec<_>>();
        if !grant_tables.is_empty() {
            let _with_schema = grant_tables
                .iter()
                .flat_map(|t| {
                    if t.name.contains('.') {
                        vec![t.name.clone()]
                    } else {
                        self.schemas
                            .iter()
                            .map(|s| format!("{}.{}", s, &t.name))
                            .collect::<Vec<_>>()
                    }
                })
                .collect::<Vec<String>>()
                .join(", ");
            let sql = format!("GRANT {} ON {} TO {};", grants, _with_schema, user);
            sqls.push(sql);
            // remove all tables start with `+`
            for table in tables.clone() {
                if table.sign == "+" {
                    tables.retain(|x| x != &table);
                }
            }
        }
        // revoke on tables start with `-`
        let revoke_tables = tables.iter().filter(|x| x.sign == "-").collect::<Vec<_>>();
        if !revoke_tables.is_empty() {
            let _with_schema = revoke_tables
                .iter()
                .flat_map(|t| {
                    if t.name.contains('.') {
                        vec![t.name.clone()]
                    } else {
                        self.schemas
                            .iter()
                            .map(|s| format!("{}.{}", s, &t.name))
                            .collect::<Vec<_>>()
                    }
                })
                .collect::<Vec<String>>()
                .join(", ");
            let sql = format!("REVOKE {} ON {} FROM {};", grants, _with_schema, user);
            sqls.push(sql);
        }
        sqls.join(" ")
    }
pub fn to_sql ( & self , user : & str ) -> String { let mut sqls = vec ! [ ] ; let mut tables = self . tables . iter ( ) . map ( | t | Table :: new ( t ) ) . collect :: < Vec < Table > > ( ) ; // grant all privileges if grants contains "ALL" let grants = if self . grants . contains ( & " " . to_string ( ) ) { " " . to_string ( ) } else { self . grants . join ( " " ) } ; // if `tables` only contains `ALL` if let Some ( table_named_all ) = tables . iter ( ) . find ( | t | t . name == " " ) { let sql = match table_named_all . sign . as_str ( ) { " " => format ! ( " " grants self schemas join ( " " ) user ) , " " => format ! ( " " grants self schemas join ( " " ) user ) , _ => " " . to_string ( ) , } ; sqls . push ( sql ) ; // remove name `ALL` and all tables start with `+` for table in tables . clone ( ) { if table . name == " " || table . sign == " " { tables . retain ( | x | x != & table ) ; } } } // grant on tables sign `+` let grant_tables = tables . iter ( ) . filter ( | x | x . sign == " " ) . collect :: < Vec < _ > > ( ) ; if ! grant_tables . is_empty ( ) { let _with_schema = grant_tables . iter ( ) . flat_map ( | t | { if t . name . contains ( '.' ) { vec ! [ t name clone ( ) ] } else { self . schemas . iter ( ) . map ( | s | format ! ( " " s t name ) ) . collect :: < Vec < _ > > ( ) } } ) . collect :: < Vec < String > > ( ) . join ( " " ) ; let sql = format ! ( " " grants _with_schema user ) ; sqls . push ( sql ) ; // remove all tables start with `+` for table in tables . clone ( ) { if table . sign == " " { tables . retain ( | x | x != & table ) ; } } } // revoke on tables start with `-` let revoke_tables = tables . iter ( ) . filter ( | x | x . sign == " " ) . collect :: < Vec < _ > > ( ) ; if ! revoke_tables . is_empty ( ) { let _with_schema = revoke_tables . iter ( ) . flat_map ( | t | { if t . name . contains ( '.' ) { vec ! [ t name clone ( ) ] } else { self . schemas . iter ( ) . map ( | s | format ! ( " " s t name ) ) . collect :: < Vec < _ > > ( ) } } ) . collect :: < Vec < String > > ( ) . join ( " " ) ; let sql = format ! ( " " grants _with_schema user ) ; sqls . push ( sql ) ; } sqls . join ( " " ) }
Generate role table to sql.
Generate role table to sql .
&self
user
&str
&self
user
&str