MCPcopy Create free account
hub / github.com/erans/pgsqlite / register_math_functions

Function register_math_functions

src/functions/math_functions.rs:54–411  ·  view source on GitHub ↗

Register all PostgreSQL math functions

(conn: &Connection)

Source from the content-addressed store, hash-verified

52
53/// Register all PostgreSQL math functions
54pub fn register_math_functions(conn: &Connection) -> Result<()> {
55 debug!("Registering math functions");
56
57 // Register trunc function (truncate towards zero)
58 conn.create_scalar_function(
59 "trunc",
60 1,
61 FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
62 |ctx| {
63 let value = get_numeric_value(ctx, 0)?;
64 Ok(value.trunc())
65 },
66 )?;
67
68 // Register trunc function with precision
69 conn.create_scalar_function(
70 "trunc",
71 2,
72 FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
73 |ctx| {
74 let value = get_numeric_value(ctx, 0)?;
75 let precision = ctx.get::<i64>(1)?;
76
77 if precision == 0 {
78 Ok(value.trunc())
79 } else {
80 let multiplier = 10_f64.powi(precision as i32);
81 Ok((value * multiplier).trunc() / multiplier)
82 }
83 },
84 )?;
85
86 // Register round function with precision
87 conn.create_scalar_function(
88 "round",
89 2,
90 FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
91 |ctx| {
92 let value = get_numeric_value(ctx, 0)?;
93 let precision = ctx.get::<i64>(1)?;
94
95 if precision == 0 {
96 Ok(value.round())
97 } else {
98 let multiplier = 10_f64.powi(precision as i32);
99 Ok((value * multiplier).round() / multiplier)
100 }
101 },
102 )?;
103
104 // Register ceil function (ceiling)
105 conn.create_scalar_function(
106 "ceil",
107 1,
108 FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
109 |ctx| {
110 let value = ctx.get::<f64>(0)?;
111 Ok(value.ceil())

Callers 9

register_all_functionsFunction · 0.85
test_truncFunction · 0.85
test_ceil_floorFunction · 0.85
test_signFunction · 0.85
test_power_sqrtFunction · 0.85
test_trigonometricFunction · 0.85
test_logarithmsFunction · 0.85
test_angle_conversionFunction · 0.85

Calls 1

get_numeric_valueFunction · 0.85

Tested by 8

test_truncFunction · 0.68
test_ceil_floorFunction · 0.68
test_signFunction · 0.68
test_power_sqrtFunction · 0.68
test_trigonometricFunction · 0.68
test_logarithmsFunction · 0.68
test_angle_conversionFunction · 0.68