no-mistakes-postgres-sql 0.109.125

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
use super::{leading_index_column, qualified_relation};
use crate::types::{SqlCreateIndexMetadata, SqlDropIndexMetadata, SqlIndexParam};
use crate::{idents::object_name_identity, order_by_ascending};
use sqlparser::ast::{ColumnOption, IndexColumn, IndexType, ObjectName, TableConstraint};

pub(super) fn from_create_index(
    line: usize,
    source_order: usize,
    index: &sqlparser::ast::CreateIndex,
) -> SqlCreateIndexMetadata {
    let name = index.name.as_ref().map(object_name_identity);
    let columns = index_params(&index.columns);
    let leading_column = columns.first().and_then(|column| column.name.clone());
    SqlCreateIndexMetadata {
        table_name: qualified_relation(&index.table_name),
        table_identity: Some(object_name_identity(&index.table_name)),
        name: name.clone(),
        raw_name: index.name.as_ref().map(qualified_relation),
        leading_column: leading_column.clone(),
        columns,
        include_columns: index
            .include
            .iter()
            .map(|ident| ident.value.clone())
            .collect(),
        access_method: access_method(index.using.as_ref()),
        unique: index.unique,
        has_predicate: index.predicate.is_some(),
        not_null_predicate_column: index
            .predicate
            .as_ref()
            .and_then(super::predicate::not_null_predicate_column),
        predicate_key: index
            .predicate
            .as_ref()
            .map(super::predicate::predicate_key),
        line,
        source_order,
    }
}

pub(super) fn from_drop_index(
    line: usize,
    source_order: usize,
    names: &[ObjectName],
) -> Vec<SqlDropIndexMetadata> {
    drop_names(names, line, source_order, object_name_identity)
}

pub(super) fn from_drop_table(
    line: usize,
    source_order: usize,
    names: &[ObjectName],
) -> Vec<SqlDropIndexMetadata> {
    drop_names(names, line, source_order, qualified_relation)
}

fn drop_names(
    names: &[ObjectName],
    line: usize,
    source_order: usize,
    name_key: fn(&ObjectName) -> String,
) -> Vec<SqlDropIndexMetadata> {
    names
        .iter()
        .map(|name| SqlDropIndexMetadata {
            name: name_key(name),
            line,
            source_order,
        })
        .collect()
}

pub(super) fn covering_from_table(
    table_name: &str,
    table: &sqlparser::ast::CreateTable,
) -> Vec<SqlCreateIndexMetadata> {
    let mut indexes = Vec::new();
    for column in &table.columns {
        let unique_or_pk = column.options.iter().any(|option| {
            matches!(
                option.option,
                ColumnOption::Unique(_) | ColumnOption::PrimaryKey(_)
            )
        });
        if unique_or_pk {
            indexes.push(covering_unique(
                table_name,
                vec![named_param(&column.name.value)],
            ));
        }
    }
    for constraint in &table.constraints {
        match constraint {
            TableConstraint::Unique(unique) => {
                indexes.push(covering_from_index_columns(table_name, &unique.columns));
            }
            TableConstraint::PrimaryKey(primary) => {
                indexes.push(covering_from_index_columns(table_name, &primary.columns));
            }
            _ => {}
        }
    }
    indexes
}

pub(super) fn covering_from_index_columns(
    table_name: &str,
    columns: &[IndexColumn],
) -> SqlCreateIndexMetadata {
    covering_unique(table_name, index_params(columns))
}

pub(super) fn covering_unique(
    table_name: &str,
    columns: Vec<SqlIndexParam>,
) -> SqlCreateIndexMetadata {
    SqlCreateIndexMetadata {
        table_name: table_name.to_string(),
        leading_column: columns.first().and_then(|column| column.name.clone()),
        columns,
        unique: true,
        ..Default::default()
    }
}

fn named_param(name: &str) -> SqlIndexParam {
    SqlIndexParam {
        name: Some(name.to_string()),
        expression: name.to_string(),
        ..Default::default()
    }
}

fn index_params(columns: &[IndexColumn]) -> Vec<SqlIndexParam> {
    columns.iter().map(index_param).collect()
}

fn index_param(column: &IndexColumn) -> SqlIndexParam {
    SqlIndexParam {
        name: leading_index_column(column),
        expression: column.column.expr.to_string(),
        opclass: column.operator_class.as_ref().map(qualified_relation),
        ordering: match order_by_ascending(&column.column.options) {
            Some(true) => Some("asc".to_string()),
            Some(false) => Some("desc".to_string()),
            None => None,
        },
        nulls_ordering: match column.column.options.nulls_first {
            Some(true) => Some("first".to_string()),
            Some(false) => Some("last".to_string()),
            None => None,
        },
    }
}

fn access_method(using: Option<&IndexType>) -> String {
    match using {
        None | Some(IndexType::BTree) => "btree".to_string(),
        Some(IndexType::Hash) => "hash".to_string(),
        Some(other) => other.to_string().to_ascii_lowercase(),
    }
}