db_meta_derive 0.1.0

db-meta-derive is a wrapper around PostgresSOL using tokio-postgres
Documentation
impl {{ name }} {
	pub fn meta() -> (String, Vec<(String, String, u16, String, bool)>) {
		let meta = {{meta_name}}::default();
		(meta.table_name, meta.fields)
	}

/*
  pub fn to_vec(self) -> Result<Vec<crate::Field>, &'static str> {
      let mut v = vec![];
      {% for field in fields %}
        {% if field.optional %}
          {% if field.ty == "String" %}
            v.push(Field::String(self.{{field.name}}));
          {% else if field.ty == "i64" %}
            v.push(Field::BigInt(self.{{field.name}}));
          {% else if field.ty == "i32" %}
            v.push(Field::Int(self.{{field.name}}));
          {% else if field.ty == "i16" %}
            v.push(Field::SmallInt(self.{{field.name}}));
          {% else if field.ty == "f64" %}
            v.push(Field::Float8(self.{{field.name}}));
          {% else if field.ty == "f32" %}
            v.push(Field::Float4(self.{{field.name}}));
          {% else if field.ty == "bool" %}
            v.push(Field::Bool(self.{{field.name}}));
          {% else if field.ty == "Value" %}
            v.push(Field::Value(self.{{field.name}}));
          {% else if field.ty == "SystemTime" %}
            v.push(Field::SystemTime(self.{{field.name}}));
          {% else %}
          {% endif %}
        {% else %}
          {% if field.ty == "String" %}
            v.push(Field::String(Some(self.{{field.name}})));
          {% else if field.ty == "i64" %}
            v.push(Field::BigInt(Some(self.{{field.name}})));
          {% else if field.ty == "i32" %}
            v.push(Field::Int(Some(self.{{field.name}})));
          {% else if field.ty == "i16" %}
            v.push(Field::SmallInt(Some(self.{{field.name}})));
          {% else if field.ty == "f64" %}
            v.push(Field::Float8(Some(self.{{field.name}})));
          {% else if field.ty == "f32" %}
            v.push(Field::Float4(Some(self.{{field.name}})));
          {% else if field.ty == "bool" %}
            v.push(crate::Field::Bool(Some(self.{{field.name}})));
          {% else if field.ty == "Value" %}
            v.push(Field::Value(Some(self.{{field.name}})));
          {% else if field.ty == "SystemTime" %}
            v.push(Field::SystemTime(Some(self.{{field.name}})));
          {% else %}
          {% endif %}
        {% endif %}
      {% endfor %}
      Ok(v)
  }

*/
}

#[derive(Debug)]
pub struct {{ meta_name }} {
    table_name: String,
    fields: Vec<(String, String, u16, String, bool)>, // 字段名,字段类型、字段长度、默认值,是否是主键
}

impl {{ meta_name }} {
	fn default() -> Self {
		Self {
			table_name: Self::table_name(),
			fields: Self::fields(),
		}
	}

	fn table_name() -> String {
		let mut table_name = String::new();
		{% for meta in metas %}
			{% if meta.name == "pg_mapper" %}
				{% for item in meta.items %}
					{% if item.key == "table" %}
						table_name.push_str("{{item.val}}");
					{% else %}
					{% endif %}
				{% endfor %}
			{% else %}
			{% endif %}
		{% endfor %}
		table_name
	}

	fn fields() -> Vec<(String, String, u16, String, bool)> {
		let mut fields = vec![];
		{% for field in fields %}
			let mut arr = ("".to_string(), "".to_string(), 0, "".to_string(), false);
			let mut ty = "";
      {% if field.ty == "String" %}
        arr.1 = "varchar".to_string();
      {% else if field.ty == "i64" %}
        arr.1 = "bigint".to_string();
      {% else if field.ty == "i32" %}
        arr.1 = "integer".to_string();
      {% else if field.ty == "i16" %}
        arr.1 = "smallint".to_string();
      {% else if field.ty == "f64" %}
        arr.1 = "float".to_string();
      {% else if field.ty == "f32" %}
        arr.1 = "float".to_string();
      {% else if field.ty == "bool" %}
        arr.1 = "boolean".to_string();
      {% else if field.ty == "Value" %}
        arr.1 = "jsonb".to_string();
      {% else if field.ty == "Uuid" %}
        arr.1 = "uuid".to_string();
      {% else if field.ty == "SystemTime" %}
        arr.1 = "timestamp".to_string();
      {% else %}
      {% endif %}

			arr.0 = "{{field.name}}".to_string();
			{% for attr in field.attrs %}
				{% if attr.name == "id" %}
					arr.4 = true;
					arr.1 = "bigserial".to_string();
					{% for item in attr.items %}
						{% if item.key == "name" %}
							arr.0 = "{{item.val}}".to_string();
						{% else if item.key == "type" %}
							arr.1 = "{{item.val}}".to_string();
						{% else if item.key == "length" %}
							arr.2 = {{item.val}} as u16;
						{% else if item.key == "default" %}
							arr.3 = "{{item.val}}".to_string();
						{% else %}
						{% endif %}
					{% endfor %}
				{% else if attr.name == "field" %}
					{% for item in attr.items %}
						{% if item.key == "name" %}
							arr.0 = "{{item.val}}".to_string();
						{% else if item.key == "type" %}
							arr.1 = "{{item.val}}".to_string();
						{% else if item.key == "length" %}
							arr.2 = {{item.val}};
						{% else if item.key == "default" %}
							arr.3 = "{{item.val}}".to_string();
						{% else %}
						{% endif %}
					{% endfor %}
				{% else %}
				{% endif %}
			{% endfor %}

			arr.1 = if arr.2 == 0 {arr.1} else {format!("{}({})", arr.1, arr.2)};
			if arr.4 {
				arr.1 = format!("{} {}", arr.1, "primary key");
			}

			if arr.1 == "" {
				panic!("no field type info");
			}
			fields.push(arr);
		{%endfor%}
		fields
	}
}