|
1 |
| -use dolphindb::{ |
2 |
| - client::ClientBuilder, |
3 |
| - types::*, |
4 |
| -}; |
| 1 | +use dolphindb::{client::ClientBuilder, types::*}; |
5 | 2 | use std::collections::HashMap;
|
6 | 3 |
|
7 | 4 | #[tokio::main]
|
8 | 5 | async fn main() {
|
9 | 6 | let mut builder = ClientBuilder::new("127.0.0.1:8848");
|
10 | 7 | builder.with_auth(("admin", "123456"));
|
11 | 8 | let mut client = builder.connect().await.unwrap();
|
12 |
| - let mut variables = HashMap::new(); |
| 9 | + let mut variables: HashMap<String, ConstantImpl> = HashMap::new(); |
13 | 10 |
|
14 | 11 | // basic examples
|
15 |
| - variables.insert("Int".to_string(), ConstantImpl::from(Int::new(1))); |
16 |
| - variables.insert("Double".to_string(), ConstantImpl::from(Double::new(1.0))); |
17 |
| - variables.insert("String".to_string(), ConstantImpl::from(DolphinString::new(String::from("1")))); |
| 12 | + variables.insert("Int".to_owned(), Int::new(1).into()); |
| 13 | + variables.insert("Double".to_owned(), Double::new(1.0).into()); |
| 14 | + variables.insert( |
| 15 | + "String".to_owned(), |
| 16 | + DolphinString::new("str".to_owned()).into(), |
| 17 | + ); |
18 | 18 |
|
19 | 19 | // numeric types
|
20 |
| - variables.insert("myVoid".to_string(), ConstantImpl::from(Void::new(()))); |
21 |
| - variables.insert("myBool".to_string(), ConstantImpl::from(Bool::new(true))); |
22 |
| - variables.insert("myChar".to_string(), ConstantImpl::from(Char::new('a' as i8))); |
23 |
| - variables.insert("myShort".to_string(), ConstantImpl::from(Short::new(1i16))); |
24 |
| - variables.insert("myInt".to_string(), ConstantImpl::from(Int::new(2i32))); |
25 |
| - variables.insert("myLong".to_string(), ConstantImpl::from(Long::new(3i64))); |
26 |
| - variables.insert("myFloat".to_string(), ConstantImpl::from(Float::new(1.1f32))); |
27 |
| - variables.insert("myDouble".to_string(), ConstantImpl::from(Double::new(1.2f64))); |
28 |
| - variables.insert("myDecimal32".to_string(), ConstantImpl::from(Decimal32::from_raw(100i32, 1).unwrap())); |
29 |
| - variables.insert("myDecimal64".to_string(), ConstantImpl::from(Decimal64::from_raw(10000i64, 2).unwrap())); |
30 |
| - variables.insert("myDecimal128".to_string(), ConstantImpl::from(Decimal128::from_raw(1000000i128, 3).unwrap())); |
| 20 | + variables.insert("myVoid".to_owned(), Void::new(()).into()); |
| 21 | + variables.insert("myBool".to_owned(), Bool::new(true).into()); |
| 22 | + variables.insert("myChar".to_owned(), Char::new('a' as i8).into()); |
| 23 | + variables.insert("myShort".to_owned(), Short::new(1i16).into()); |
| 24 | + variables.insert("myInt".to_owned(), Int::new(2i32).into()); |
| 25 | + variables.insert("myLong".to_owned(), Long::new(3i64).into()); |
| 26 | + variables.insert("myFloat".to_owned(), Float::new(1.1f32).into()); |
| 27 | + variables.insert("myDouble".to_owned(), Double::new(1.2f64).into()); |
| 28 | + variables.insert( |
| 29 | + "myDecimal32".to_owned(), |
| 30 | + Decimal32::from_raw(100i32, 1).unwrap().into(), |
| 31 | + ); |
| 32 | + variables.insert( |
| 33 | + "myDecimal64".to_owned(), |
| 34 | + Decimal64::from_raw(10000i64, 2).unwrap().into(), |
| 35 | + ); |
| 36 | + variables.insert( |
| 37 | + "myDecimal128".to_owned(), |
| 38 | + Decimal128::from_raw(1000000i128, 3).unwrap().into(), |
| 39 | + ); |
31 | 40 |
|
32 | 41 | // temporal types
|
33 |
| - variables.insert("myDate".to_string(), ConstantImpl::from(Date::from_raw(20089).unwrap())); |
34 |
| - variables.insert("myTimestamp".to_string(), ConstantImpl::from(Timestamp::from_raw(1735660800_000i64).unwrap())); |
35 |
| - variables.insert("myNanoTimestamp".to_string(), ConstantImpl::from(NanoTimestamp::from_raw(1735660800_000_000_000i64).unwrap())); |
36 |
| - variables.insert("myMonth".to_string(), ConstantImpl::from(Month::from_ym(2025, 1).unwrap())); |
37 |
| - variables.insert("myMinute".to_string(), ConstantImpl::from(Minute::from_hm(17, 30).unwrap())); |
38 |
| - variables.insert("mySecond".to_string(), ConstantImpl::from(Second::from_hms(17, 30, 0).unwrap())); |
39 |
| - variables.insert("myTime".to_string(), ConstantImpl::from(Time::from_hms_milli(17, 30, 0, 100).unwrap())); |
40 |
| - variables.insert("myDateTime".to_string(), ConstantImpl::from(DateTime::from_raw(1735660800).unwrap())); |
41 |
| - variables.insert("myNanoTime".to_string(), ConstantImpl::from(NanoTime::from_hms_nano(17, 30,0,100_000_000u32).unwrap())); |
| 42 | + // unix timestamp (milliseconds) |
| 43 | + let unix_timestamp = 1735660800_000i64; |
| 44 | + variables.insert( |
| 45 | + "myDate".to_owned(), |
| 46 | + Date::from_raw(unix_timestamp / 86400_000).unwrap().into(), |
| 47 | + ); |
| 48 | + variables.insert( |
| 49 | + "myDateTime".to_owned(), |
| 50 | + DateTime::from_raw((unix_timestamp / 1000) as i32) |
| 51 | + .unwrap() |
| 52 | + .into(), |
| 53 | + ); |
| 54 | + variables.insert( |
| 55 | + "myTimestamp".to_owned(), |
| 56 | + Timestamp::from_raw(unix_timestamp).unwrap().into(), |
| 57 | + ); |
| 58 | + variables.insert( |
| 59 | + "myNanoTimestamp".to_owned(), |
| 60 | + NanoTimestamp::from_raw(unix_timestamp * 1000_000) |
| 61 | + .unwrap() |
| 62 | + .into(), |
| 63 | + ); |
| 64 | + variables.insert( |
| 65 | + "myMonth".to_owned(), |
| 66 | + Month::from_ym(2025, 1).unwrap().into(), |
| 67 | + ); |
| 68 | + variables.insert( |
| 69 | + "myMinute".to_owned(), |
| 70 | + Minute::from_hm(17, 30).unwrap().into(), |
| 71 | + ); |
| 72 | + variables.insert( |
| 73 | + "mySecond".to_owned(), |
| 74 | + Second::from_hms(17, 30, 0).unwrap().into(), |
| 75 | + ); |
| 76 | + variables.insert( |
| 77 | + "myTime".to_owned(), |
| 78 | + Time::from_hms_milli(17, 30, 0, 100).unwrap().into(), |
| 79 | + ); |
| 80 | + variables.insert( |
| 81 | + "myNanoTime".to_owned(), |
| 82 | + NanoTime::from_hms_nano(17, 30, 0, 100_000_000u32) |
| 83 | + .unwrap() |
| 84 | + .into(), |
| 85 | + ); |
| 86 | + variables.insert( |
| 87 | + "myDateHour".to_owned(), |
| 88 | + DateHour::from_ymd_h(2025, 1, 1, 17).unwrap().into(), |
| 89 | + ); |
| 90 | + |
| 91 | + // other types |
| 92 | + variables.insert( |
| 93 | + "myString".to_owned(), |
| 94 | + DolphinString::new("str".to_owned()).into(), |
| 95 | + ); |
| 96 | + variables.insert("myBlob".to_owned(), Blob::new(vec![b'a']).into()); |
| 97 | + variables.insert("mySymbol".to_owned(), Symbol::new("1".to_owned()).into()); |
42 | 98 |
|
43 | 99 | client.upload(&variables).await.unwrap();
|
44 | 100 |
|
45 | 101 | // Void is not uploaded
|
46 | 102 | // println!("myVoid: {}", client.run_script("myVoid").await.unwrap().unwrap());
|
47 |
| - println!("myBool: {}", client.run_script("myBool").await.unwrap().unwrap()); |
48 |
| - println!("myChar: {}", client.run_script("myChar").await.unwrap().unwrap()); |
49 |
| - println!("myShort: {}", client.run_script("myShort").await.unwrap().unwrap()); |
50 |
| - println!("myInt: {}", client.run_script("myInt").await.unwrap().unwrap()); |
51 |
| - println!("myLong: {}", client.run_script("myLong").await.unwrap().unwrap()); |
52 |
| - println!("myFloat: {}", client.run_script("myFloat").await.unwrap().unwrap()); |
53 |
| - println!("myDouble: {}", client.run_script("myDouble").await.unwrap().unwrap()); |
54 |
| - println!("myDecimal32: {}", client.run_script("myDecimal32").await.unwrap().unwrap()); |
55 |
| - println!("myDecimal64: {}", client.run_script("myDecimal64").await.unwrap().unwrap()); |
56 |
| - println!("myDecimal128: {}", client.run_script("myDecimal128").await.unwrap().unwrap()); |
57 |
| - println!("myDate: {}", client.run_script("myDate").await.unwrap().unwrap()); |
58 |
| - println!("myTimestamp: {}", client.run_script("myTimestamp").await.unwrap().unwrap()); |
59 |
| - println!("myNanoTimestamp: {}", client.run_script("myNanoTimestamp").await.unwrap().unwrap()); |
60 |
| - println!("myMonth: {}", client.run_script("myMonth").await.unwrap().unwrap()); |
61 |
| - println!("mySecond: {}", client.run_script("mySecond").await.unwrap().unwrap()); |
62 |
| - println!("myTime: {}", client.run_script("myTime").await.unwrap().unwrap()); |
63 |
| - println!("myDateTime: {}", client.run_script("myDateTime").await.unwrap().unwrap()); |
64 |
| - println!("myNanoTime: {}", client.run_script("myNanoTime").await.unwrap().unwrap()); |
| 103 | + let vars = [ |
| 104 | + "myBool", |
| 105 | + "myChar", |
| 106 | + "myShort", |
| 107 | + "myInt", |
| 108 | + "myLong", |
| 109 | + "myFloat", |
| 110 | + "myDouble", |
| 111 | + "myDecimal32", |
| 112 | + "myDecimal64", |
| 113 | + "myDecimal128", |
| 114 | + "myDate", |
| 115 | + "myTimestamp", |
| 116 | + "myNanoTimestamp", |
| 117 | + "myDateTime", |
| 118 | + "myMonth", |
| 119 | + "mySecond", |
| 120 | + "myTime", |
| 121 | + "myNanoTime", |
| 122 | + "myDateHour", |
| 123 | + "myString", |
| 124 | + "myBlob", |
| 125 | + "mySymbol", |
| 126 | + ]; |
| 127 | + for var in vars { |
| 128 | + println!( |
| 129 | + "{}: {}", |
| 130 | + var, |
| 131 | + client.run_script(var).await.unwrap().unwrap() |
| 132 | + ); |
| 133 | + } |
65 | 134 | }
|
0 commit comments