Skip to main content

SimpleNl2SqlEngine

Struct SimpleNl2SqlEngine 

Source
pub struct SimpleNl2SqlEngine { /* private fields */ }
Expand description

基于规则匹配的 NL→SQL 引擎(内存模拟,无需 LLM API)

通过关键词匹配和模式识别,将常见自然语言查询转换为 SQL。 支持以下查询模式:

  • 简单 SELECT(SELECT * / SELECT col1, col2
  • COUNT / COUNT DISTINCT
  • 聚合函数(SUM, AVG, MIN, MAX)
  • WHERE 条件(=, >, <, >=, <=, !=, LIKE)
  • ORDER BY(ASC / DESC)
  • GROUP BY
  • JOIN(通过外键名称推断关联)
  • LIMIT

所有生成的 SQL 使用 $1, $2 等参数化占位符防止注入。

§示例

use sz_orm_ai::nl2sql::{SimpleNl2SqlEngine, Nl2SqlEngine, SchemaContext, TableInfo, ColumnInfo};

let engine = SimpleNl2SqlEngine::new();
let schema = SchemaContext {
    tables: vec![TableInfo {
        name: "users".into(),
        columns: vec![
            ColumnInfo { name: "id".into(), data_type: "INTEGER".into(), nullable: false, is_primary_key: true },
            ColumnInfo { name: "name".into(), data_type: "TEXT".into(), nullable: true, is_primary_key: false },
        ],
    }],
};
let result = engine.generate("show all users", &schema).await?;
assert_eq!(result.sql, "SELECT * FROM users");

Implementations§

Source§

impl SimpleNl2SqlEngine

Source

pub fn new() -> Self

Source

pub fn with_alias(self, alias: &str, table: &str) -> Self

注册表别名(例如 with_alias("user", "users")

Trait Implementations§

Source§

impl Default for SimpleNl2SqlEngine

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Nl2SqlEngine for SimpleNl2SqlEngine

Source§

fn generate<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, nl_query: &'life1 str, schema: &'life2 SchemaContext, ) -> Pin<Box<dyn Future<Output = Result<SqlQuery, Nl2SqlError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

将自然语言查询转换为 SQL Read more
Source§

fn validate<'life0, 'life1, 'async_trait>( &'life0 self, query: &'life1 SqlQuery, ) -> Pin<Box<dyn Future<Output = Result<bool, Nl2SqlError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

验证生成的 SQL 是否安全可用 Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.