Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

数据写入时(insert or update)值里有mysql函数包装不能识别? #314

lijunfeng started this conversation in General
Discussion options

类似这样的如何写入?
Some(format!("UNDEX(0x{})", ip))
读取的时候我用select hex(ip)读出来,有没有可以直接读取二进制的方法呢?

You must be logged in to vote

Replies: 6 comments

Comment options

类似这样的如何写入? Some(format!("UNDEX(0x{})", ip)) 读取的时候我用select hex(ip)读出来,有没有可以直接读取二进制的方法呢?

读取的时候反序列化类型使用rbatis::rbdc::types::Bytes类型,尝试一下

You must be logged in to vote
0 replies
Comment options

这个试过了,是空的,所以我才hex转成字符串,但是写的时候unhex不能转了,定义成string后写入是字符串了。我用拦截器把字符串转成binary后无效的,还是按字符串处理的,是否可以增加源字符处理机制?这样可能不安全,也和数据库关系比较密切了。

You must be logged in to vote
0 replies
Comment options

这个试过了,是空的,所以我才hex转成字符串,但是写的时候unhex不能转了,定义成string后写入是字符串了。我用拦截器把字符串转成binary后无效的,还是按字符串处理的,是否可以增加源字符处理机制?这样可能不安全,也和数据库关系比较密切了。

展示一下代码看看?

You must be logged in to vote
0 replies
Comment options

我读的库是wordpress的

/// wp_wfblockediplog 内容主要信息
// #[crud_table(table_name:"wp_wfblockediplog" | formats_pg:"id:{}::uuid,create_time:{}::timestamp")]
// #[py_sql(formats_mysql:"IP:UNHEX({})")]
#[allow(non_snake_case)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct WpWfblockediplog {
    // pub IP: Option<Vec<char>>,
    // pub IP: Option<rbatis::rbdc::bytes::Bytes>,
    // pub IP: Option<serde_bytes::ByteBuf>,
    // pub IP: Option<rbs::Value>,
    // pub IP: Option<Vec<u8>>,
    // pub IP: Option<rbatis::rbdc::bytes::Bytes>,
    pub IP: Option<String>,
    pub countryCode: Option<String>,
    pub blockCount: Option<usize>,
    pub unixday: Option<usize>,
    pub blockType: Option<String>,
}```

IP如果不用hex转换的话用注释掉的读取出来的都是空的数据库IP类型是binary(16)的。
You must be logged in to vote
0 replies
Comment options

UNHEX

我尝试了binary(16)的类型,mysql在插入时会吧字符串类型 转bytes,查询时会吧byte转字符串,因此 ip用Option就行了
例如
*sql

create table mock
(
    id int        null,
    ip binary(16) null
);

*code

#[macro_use]
extern crate rbatis;

pub mod model;

use serde::{Deserialize, Serialize};
use crate::{init_db};
use model::*;
use rbatis::rbatis::Rbatis;
use rbatis::rbdc::bytes::Bytes;
use rbatis::rbdc::datetime::FastDateTime;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Mock {
    pub id: Option<i32>,
    pub ip:Option<String>
}
crud!(Mock{});
/// doc you can see https://rbatis.github.io/rbatis.io
#[sql("select * from mock where id = '22'")]
async fn raw_sql(rb: &Rbatis, delete_flag: &i32) -> rbatis::Result<rbs::Value> {
    impled!()
}

#[tokio::main]
pub async fn main() {
    fast_log::init(fast_log::Config::new().console()).expect("rbatis init fail");
    //use static ref
    let rb = init_db().await;
    Mock::insert(&mut rb.clone(),&Mock{
        id: Some(22),
        ip: Some("192.168.1.199".to_string())
    }).await.unwrap();

    let a = raw_sql(&rb, &0).await.unwrap();
    println!("{:?}", a);
}

*log

2022-10-27 10:10:39.305975 INFO rbatis::plugin::log - [rbatis] [428012949702053888] Exec   ==> insert into mock (id,ip) VALUES (?,?)
                                                      [rbatis]                      Args   ==> [22,"192.168.1.199"]
2022-10-27 10:10:39.309987 INFO rbatis::plugin::log - [rbatis] [428012949702053888] RowsAffected <== 1
2022-10-27 10:10:39.311092 INFO rbatis::plugin::log - [rbatis] [428012949723025408] Fetch  ==> select * from mock where id = '22'
                                                      [rbatis]                      Args   ==> [0]
2022-10-27 10:10:39.312427 INFO rbatis::plugin::log - [rbatis] [428012949723025408] ReturnRows <== 1
2022-10-27 10:10:39.312433 DEBUG rbatis::decode -     [rbatis] [debug_mode] rbs::value::Value => [{"ip":"192.168.1.199���","id":22}]
Array([Map({String("ip"): String("192.168.1.199\0\0\0"), String("id"): I64(22)})])
You must be logged in to vote
0 replies
Comment options

收到,非常感谢,这样是没有问题;wordpress的应该是用PHP处理过的数据,所以不能直接取出,再次感谢

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #304 on October 27, 2022 02:03.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.