1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
use crate::errors::SqlBuilderError;
use core::fmt;
/// Represents the creation of a table with specified columns and options.
#[derive(Debug)]
pub struct CreateTable {
table: String,
columns: Vec<Column>,
if_not_exists: bool,
}
impl CreateTable {
/// Creates a new `CreateTable` instance with the given table name and columns.
/// # Example
/// ```
/// use lumus_sql_builder::sqlite::{CreateTable, Column};
/// CreateTable::new("users", vec![
/// Column::new("name").text().not_null().primary_key(),
/// ]);
/// ```
pub fn new(table: &str, columns: Vec<Column>) -> Self {
Self {
table: table.to_string(),
columns,
if_not_exists: false,
}
}
/// Specifies that the table should be created only if it does not already exist.
pub fn if_not_exists(mut self) -> Self {
self.if_not_exists = true;
self
}
/// Builds and returns the SQL statement for creating the table.
pub fn build(&self) -> Result<String, SqlBuilderError> {
if self.table.is_empty() {
return Err(SqlBuilderError::EmptyTableName);
}
if self.columns.is_empty() {
return Err(SqlBuilderError::NoColumnsSpecified);
}
let mut statement = if self.if_not_exists {
format!("CREATE TABLE IF NOT EXISTS {} (", self.table)
} else {
format!("CREATE TABLE {} (", self.table)
};
let columns_sql: Result<Vec<String>, SqlBuilderError> =
self.columns.iter().map(|col| col.build()).collect();
statement.push_str(&columns_sql?.join(", "));
statement.push_str(");");
Ok(statement)
}
}
/// Implementation of the Display trait for `CreateTable`, allowing it to be printed.
impl fmt::Display for CreateTable {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.build() {
Err(e) => write!(f, "{}", e),
Ok(s) => write!(f, "{}", s),
}
}
}
/// Represents the possible data types for a table column.
#[derive(Debug)]
pub enum ColumnType {
Integer,
Text,
Real,
Boolean,
Blob,
Numeric,
Date,
Time,
Datetime,
}
/// Implementation of the Display trait for `ColumnType`, allowing it to be printed.
impl fmt::Display for ColumnType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ColumnType::Integer => write!(f, "INTEGER"),
ColumnType::Text => write!(f, "TEXT"),
ColumnType::Real => write!(f, "REAL"),
ColumnType::Boolean => write!(f, "BOOLEAN"),
ColumnType::Blob => write!(f, "BLOB"),
ColumnType::Numeric => write!(f, "NUMERIC"),
ColumnType::Date => write!(f, "DATE"),
ColumnType::Time => write!(f, "TIME"),
ColumnType::Datetime => write!(f, "DATETIME"),
}
}
}
/// Represents the possible options for a table column.
#[derive(Debug)]
pub enum ColumnOption {
NotNull,
Unique,
Default(String),
AutoIncrement,
PrimaryKey,
}
/// Implementation of the Display trait for `ColumnOption`, allowing it to be printed.
impl fmt::Display for ColumnOption {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ColumnOption::NotNull => write!(f, "NOT NULL"),
ColumnOption::Unique => write!(f, "UNIQUE"),
ColumnOption::Default(value) => write!(f, "DEFAULT {}", value),
ColumnOption::AutoIncrement => write!(f, "AUTOINCREMENT"),
ColumnOption::PrimaryKey => write!(f, "PRIMARY KEY"),
}
}
}
/// Represents a table column with a name, data type, and options.
#[derive(Debug)]
pub struct Column {
name: String,
column_type: Option<ColumnType>,
options: Vec<ColumnOption>,
}
impl Column {
/// Creates a new `Column` instance with the given column name.
/// # Example
/// ```
/// use lumus_sql_builder::sqlite::Column;
/// Column::new("name").text().not_null();
/// ```
pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
column_type: None,
options: Vec::new(),
}
}
/// Specifies that the column has an `INTEGER` data type.
pub fn integer(mut self) -> Self {
self.column_type = Some(ColumnType::Integer);
self
}
/// Specifies that the column has a `TEXT` data type.
pub fn text(mut self) -> Self {
self.column_type = Some(ColumnType::Text);
self
}
/// Specifies that the column has a `REAL` data type.
pub fn real(mut self) -> Self {
self.column_type = Some(ColumnType::Real);
self
}
/// Specifies that the column has a `BOOLEAN` data type.
pub fn boolean(mut self) -> Self {
self.column_type = Some(ColumnType::Boolean);
self
}
/// Specifies that the column has a `BLOB` data type.
pub fn blob(mut self) -> Self {
self.column_type = Some(ColumnType::Blob);
self
}
/// Specifies that the column has a `NUMERIC` data type.
pub fn numeric(mut self) -> Self {
self.column_type = Some(ColumnType::Numeric);
self
}
/// Specifies that the column has a `DATE` data type.
pub fn date(mut self) -> Self {
self.column_type = Some(ColumnType::Date);
self
}
/// Specifies that the column has a `TIME` data type.
pub fn time(mut self) -> Self {
self.column_type = Some(ColumnType::Time);
self
}
/// Specifies that the column has a `DATETIME` data type.
pub fn datetime(mut self) -> Self {
self.column_type = Some(ColumnType::Datetime);
self
}
/// Specifies that the column cannot have `NULL` values.
pub fn not_null(mut self) -> Self {
self.options.push(ColumnOption::NotNull);
self
}
/// Specifies that the column values must be unique across rows.
pub fn unique(mut self) -> Self {
self.options.push(ColumnOption::Unique);
self
}
/// Specifies a default value for the column.
pub fn default(mut self, value: &str) -> Self {
self.options.push(ColumnOption::Default(value.to_string()));
self
}
/// Specifies that the column values should auto-increment.
pub fn auto_increment(mut self) -> Self {
self.options.push(ColumnOption::AutoIncrement);
self
}
/// Specifies that the column is a primary key.
pub fn primary_key(mut self) -> Self {
self.options.push(ColumnOption::PrimaryKey);
self
}
/// Builds and returns the SQL representation of the column.
pub fn build(&self) -> Result<String, SqlBuilderError> {
if self.name.is_empty() {
return Err(SqlBuilderError::EmptyColumnName);
}
let column_type_str = match &self.column_type {
Some(ct) => ct.to_string(),
None => return Err(SqlBuilderError::InvalidColumnType),
};
let options_str = self
.options
.iter()
.map(|opt| opt.to_string())
.collect::<Vec<String>>()
.join(" ");
Ok(format!("{} {} {}", self.name, column_type_str, options_str))
}
}
/// Implementation of the Display trait for `Column`, allowing it to be printed.
impl fmt::Display for Column {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.build() {
Err(e) => write!(f, "{}", e),
Ok(s) => write!(f, "{}", s),
}
}
}
/// Represents the creation of a SELECT with specified table and options.
#[derive(Debug)]
pub struct Select {
table: String,
distinct: bool,
condition: Option<String>,
columns: Option<String>,
group: Option<String>,
order: Option<String>,
limit: Option<u32>,
offset: Option<u32>,
}
impl Select {
/// Creates a new `Select` instance with the specified table name.
/// # Example
/// ```
/// use lumus_sql_builder::sqlite::Select;
/// Select::new("users").columns("name, age");
/// ```
pub fn new(table: &str) -> Self {
Self {
table: table.to_string(),
distinct: false,
columns: None,
condition: None,
group: None,
order: None,
limit: None,
offset: None,
}
}
/// Creates a new `Select` instance from a SQL query string.
/// The query string should be in the format "SELECT * FROM table_name [WHERE condition] [GROUP BY column] [ORDER BY column] [LIMIT limit] [OFFSET offset]".
/// # Example
/// ```
/// use lumus_sql_builder::sqlite::Select;
/// let query = "SELECT * FROM users WHERE age > 18 GROUP BY city ORDER BY name LIMIT 10 OFFSET 0";
/// Select::from(query);
/// ```
pub fn from(query: &str) -> Result<Select, SqlBuilderError> {
let mut parts = query.split_whitespace();
let select = parts.next().ok_or(SqlBuilderError::InvalidQuery)?;
if select.to_uppercase() != "SELECT" {
return Err(SqlBuilderError::InvalidQuery);
}
let _ = parts.next().ok_or(SqlBuilderError::InvalidQuery)?; // Skip the "*"
let from = parts.next().ok_or(SqlBuilderError::InvalidQuery)?;
if from.to_uppercase() != "FROM" {
return Err(SqlBuilderError::InvalidQuery);
}
let table = parts.next().ok_or(SqlBuilderError::InvalidQuery)?;
let mut select_builder = Select::new(table);
while let Some(part) = parts.next() {
match part.to_uppercase().as_str() {
"WHERE" => {
let condition = parts.next().ok_or(SqlBuilderError::InvalidQuery)?;
select_builder.condition(condition.to_string());
}
"GROUP" => {
let by = parts.next().ok_or(SqlBuilderError::InvalidQuery)?;
if by.to_uppercase() != "BY" {
return Err(SqlBuilderError::InvalidQuery);
}
let group = parts.next().ok_or(SqlBuilderError::InvalidQuery)?;
select_builder.group(group);
}
"ORDER" => {
let by = parts.next().ok_or(SqlBuilderError::InvalidQuery)?;
if by.to_uppercase() != "BY" {
return Err(SqlBuilderError::InvalidQuery);
}
let order = parts.next().ok_or(SqlBuilderError::InvalidQuery)?;
select_builder.order(order);
}
"LIMIT" => {
let limit = parts.next().ok_or(SqlBuilderError::InvalidQuery)?;
let limit = limit
.parse::<u32>()
.map_err(|_| SqlBuilderError::InvalidQuery)?;
select_builder.limit(limit);
}
"OFFSET" => {
let offset = parts.next().ok_or(SqlBuilderError::InvalidQuery)?;
let offset = offset
.parse::<u32>()
.map_err(|_| SqlBuilderError::InvalidQuery)?;
select_builder.offset(offset);
}
_ => return Err(SqlBuilderError::InvalidQuery),
}
}
Ok(select_builder)
}
/// Specifies that the select statement should return distinct rows.
pub fn distinct(&mut self) -> &mut Self {
self.distinct = true;
self
}
/// Specifies the columns to be selected in the query.
pub fn columns(&mut self, columns: &str) -> &mut Self {
self.columns = Some(columns.to_string());
self
}
/// Specifies the grouping for the query results.
pub fn group(&mut self, group: &str) -> &mut Self {
self.group = Some(group.to_string());
self
}
/// Specifies the ordering for the query results.
pub fn order(&mut self, order: &str) -> &mut Self {
self.order = Some(order.to_string());
self
}
/// Specifies where for `Select`.
pub fn condition(&mut self, condition: String) -> &mut Self {
self.condition = Some(condition);
self
}
/// Specifies the maximum number of rows to be returned by the query.
pub fn limit(&mut self, limit: u32) -> &mut Self {
self.limit = Some(limit);
self
}
/// Specifies the offset for the query results.
pub fn offset(&mut self, offset: u32) -> &mut Self {
self.offset = Some(offset);
self
}
/// Builds and returns the SQL statement for the select query.
pub fn build(&self) -> Result<String, SqlBuilderError> {
if self.table.is_empty() {
return Err(SqlBuilderError::EmptyTableName);
}
let mut statement = String::from("SELECT");
if self.distinct {
statement.push_str(" DISTINCT");
}
if let Some(columns) = &self.columns {
statement.push_str(&format!(" {}", columns));
} else {
statement.push_str(" *");
}
statement.push_str(&format!(" FROM {}", self.table));
if let Some(condition) = &self.condition {
statement.push_str(&format!(" WHERE {}", condition));
}
if let Some(group) = &self.group {
statement.push_str(&format!(" GROUP BY {}", group));
}
if let Some(order) = &self.order {
statement.push_str(&format!(" ORDER BY {}", order));
}
if let Some(limit) = &self.limit {
statement.push_str(&format!(" LIMIT {}", limit));
}
if let Some(offset) = &self.offset {
statement.push_str(&format!(" OFFSET {}", offset));
}
statement.push(';');
Ok(statement)
}
}
/// Implementation of the Display trait for `Select`, allowing it to be printed.
impl fmt::Display for Select {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.build() {
Err(e) => write!(f, "{}", e),
Ok(s) => write!(f, "{}", s),
}
}
}
/// Represents the creation of a INSERT with specified table and values.
#[derive(Debug)]
pub struct Insert {
pub table: String,
pub values: Vec<(String, String)>,
}
impl Insert {
/// Creates a new `Insert` instance with the given table name.
/// # Example
/// ```
/// use lumus_sql_builder::sqlite::Insert;
///
/// Insert::new("metas_clientes_tb").values(vec![
/// ("name", "João"),
/// ("age", "30"),
/// ("department", "TI"),
/// ("salary", "5000.00"),
/// ("hired_date", "2024-03-20"),
/// ("manager_id", "1"),
/// ]);
/// ```
pub fn new(table: &str) -> Insert {
Insert {
table: table.to_string(),
values: Vec::new(),
}
}
/// Sets the values to be inserted.
pub fn values<T: ToString>(mut self, values: Vec<(&str, T)>) -> Self {
self.values = values
.into_iter()
.map(|(col, val)| (col.to_string(), val.to_string()))
.collect();
self
}
/// Builds and returns the SQL statement for the `INSERT` query.
pub fn build(&self) -> Result<String, SqlBuilderError> {
if self.table.is_empty() {
return Err(SqlBuilderError::EmptyTableName);
}
if self.values.is_empty() {
return Err(SqlBuilderError::EmptyColumnAndValue);
}
let mut columns: Vec<String> = vec![];
let mut values: Vec<String> = vec![];
for (col, val) in &self.values {
if col.is_empty() {
return Err(SqlBuilderError::EmptyColumnName);
}
if val.is_empty() {
return Err(SqlBuilderError::EmptyValue);
}
columns.push(col.clone());
values.push(format!("'{}'", val.clone()));
}
Ok(format!(
"INSERT INTO {} ({}) VALUES ({});",
self.table,
columns.join(", "),
values.join(", ")
))
}
}
/// Implementation of the Display trait for `Insert`, allowing it to be printed.
impl fmt::Display for Insert {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.build() {
Err(e) => write!(f, "{}", e),
Ok(s) => write!(f, "{}", s),
}
}
}
/// Represents a WHERE clause builder for SQL queries.
#[derive(Debug)]
pub struct Where {
statement: String,
}
impl Where {
/// Creates a new `Where` instance with an empty statement.
/// # Example
/// ```
/// use lumus_sql_builder::sqlite::Where;
///
/// let mut condition = Where::new();
/// condition.equal_to("name", "Dayvson Spacca");
///
/// assert_eq!(condition.build(), "name = 'Dayvson Spacca'")
/// ```
pub fn new() -> Self {
Self {
statement: String::new(),
}
}
/// Creates a new `Where` instance with a specified initial statement.
/// # Example
/// ```
/// use lumus_sql_builder::sqlite::Where;
///
/// let mut condition = Where::from("name = 'Dayvson Spacca'");
/// condition.and().greater_than("age", "21");
///
/// assert_eq!(condition.build(), "name = 'Dayvson Spacca' AND age > '21'");
/// ```
pub fn from(statement: &str) -> Self {
Self {
statement: statement.to_string(),
}
}
/// Adds an equality condition (`field = value`) to the WHERE clause.
pub fn equal_to(&mut self, field: &str, value: &str) -> &mut Self {
self.add_predicate(field, "=", value).unwrap();
self
}
/// Adds a not equal condition (`field != value`) to the WHERE clause.
pub fn not_equal_to(&mut self, field: &str, value: &str) -> &mut Self {
self.add_predicate(field, "!=", value).unwrap();
self
}
/// Adds a greater than condition (`field > value`) to the WHERE clause.
pub fn greater_than(&mut self, field: &str, value: &str) -> &mut Self {
self.add_predicate(field, ">", value).unwrap();
self
}
/// Adds a greater than or equal condition (`field >= value`) to the WHERE clause.
pub fn greater_than_equal(&mut self, field: &str, value: &str) -> &mut Self {
self.add_predicate(field, ">=", value).unwrap();
self
}
/// Adds a less than condition (`field < value`) to the WHERE clause.
pub fn less_than(&mut self, field: &str, value: &str) -> &mut Self {
self.add_predicate(field, "<", value).unwrap();
self
}
/// Adds a less than or equal condition (`field <= value`) to the WHERE clause.
pub fn less_than_equal(
&mut self,
field: &str,
value: &str,
) -> Result<&mut Self, SqlBuilderError> {
self.add_predicate(field, "<=", value)
}
/// Adds a `IS NULL` condition (`field IS NULL`) to the WHERE clause.
pub fn is_null(&mut self, field: &str) -> &mut Self {
self.add_predicate(field, "IS NULL", "").unwrap();
self
}
/// Adds a `IS NOT NULL` condition (`field IS NOT NULL`) to the WHERE clause.
pub fn is_not_null(&mut self, field: &str) -> &mut Self {
self.add_predicate(field, "IS NOT NULL", "").unwrap();
self
}
/// Adds an `IN` condition (`field IN (values)`) to the WHERE clause.
pub fn inside(&mut self, field: &str, values: Vec<&str>) -> &mut Self {
self.add_predicate(field, "IN", &format!("({})", values.join(", ")))
.unwrap();
self
}
/// Adds a `NOT IN` condition (`field NOT IN (values)`) to the WHERE clause.
pub fn not_inside(&mut self, field: &str, values: Vec<&str>) -> &mut Self {
self.add_predicate(field, "NOT IN", &format!("({})", values.join(", ")))
.unwrap();
self
}
/// Adds a `LIKE` condition (`field LIKE value`) to the WHERE clause.
pub fn like(&mut self, field: &str, value: &str) -> &mut Self {
self.add_predicate(field, "LIKE", value).unwrap();
self
}
/// Adds a `NOT LIKE` condition (`field NOT LIKE value`) to the WHERE clause.
pub fn not_like(&mut self, field: &str, value: &str) -> &mut Self {
self.add_predicate(field, "NOT LIKE", value).unwrap();
self
}
/// Appends `AND` to the current statement in the WHERE clause.
pub fn and(&mut self) -> &mut Self {
self.statement.push_str(" AND ");
self
}
/// Appends `OR` to the current statement in the WHERE clause.
pub fn or(&mut self) -> &mut Self {
self.statement.push_str(" OR ");
self
}
/// Appends a left parenthesis `(` to the current statement in the WHERE clause.
pub fn nest(&mut self) -> &mut Self {
self.statement.push('(');
self
}
/// Appends a right parenthesis `)` to the current statement in the WHERE clause.
pub fn unnest(&mut self) -> &mut Self {
self.statement.push(')');
self
}
/// Constructs and returns the final SQL statement represented by the WHERE clause.
pub fn build(&self) -> String {
self.statement.trim().to_string()
}
/// Internal method to add a predicate (`field predicate value`) to the WHERE clause.
fn add_predicate(
&mut self,
field: &str,
predicate: &str,
value: &str,
) -> Result<&mut Self, SqlBuilderError> {
if field.is_empty() {
return Err(SqlBuilderError::EmptyColumnName);
}
if predicate == "IS NULL" || predicate == "IS NOT NULL" {
self.statement
.push_str(&format!("{} {} ", field, predicate));
return Ok(self);
}
if value.is_empty() {
return Err(SqlBuilderError::EmptyValue);
}
let escaped_value = format!("'{}'", value);
self.statement
.push_str(&format!("{} {} {}", field, predicate, escaped_value));
Ok(self)
}
}
/// Implementation of the Display trait for `Where`, allowing it to be printed.
impl fmt::Display for Where {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.build())
}
}
/// Represents a ´UPDATE´ clause builder for SQL queries
#[derive(Debug)]
pub struct Update {
table: String,
pub set: Vec<(String, String)>,
condition: Option<String>,
}
impl Update {
/// Creates a new `Update` instance with the given table name.
/// # Example
/// ```
/// use lumus_sql_builder::sqlite::{Update, Where};
///
/// let mut condition = Where::new();
/// condition.equal_to("age", "21");
///
/// let update = Update::new("users_tb").set(vec![
/// ("name", "João")
/// ]).condition(condition.build())
/// .build();
///
/// assert_eq!("UPDATE users_tb SET name = 'João' WHERE age = '21';", update.unwrap());
/// ```
pub fn new(table: &str) -> Self {
Self {
table: table.to_string(),
set: Vec::new(),
condition: None,
}
}
/// Sets the values to be updated.
pub fn set<T: ToString>(mut self, set: Vec<(&str, T)>) -> Self {
self.set = set
.into_iter()
.map(|(col, val)| (col.to_string(), val.to_string()))
.collect();
self
}
/// Specifies where for `Update`.
pub fn condition(&mut self, condition: String) -> &mut Self {
self.condition = Some(condition);
self
}
pub fn build(&self) -> Result<String, SqlBuilderError> {
if self.table.is_empty() {
return Err(SqlBuilderError::EmptyTableName);
}
if self.set.is_empty() {
return Err(SqlBuilderError::EmptyColumnAndValue);
}
let mut sets: Vec<String> = vec![];
for (col, val) in &self.set {
if col.is_empty() {
return Err(SqlBuilderError::EmptyColumnName);
}
if val.is_empty() {
return Err(SqlBuilderError::EmptyValue);
}
sets.push(format!("{} = '{}'", col.clone(), val.clone()));
}
if let Some(condition) = &self.condition {
return Ok(format!(
"UPDATE {} SET {} WHERE {};",
self.table,
sets.join(", "),
condition
));
}
Ok(format!("UPDATE {} SET {};", self.table, sets.join(", "),))
}
}
/// Implementation of the Display trait for `Update`, allowing it to be printed.
impl fmt::Display for Update {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.build() {
Err(e) => write!(f, "{}", e),
Ok(s) => write!(f, "{}", s),
}
}
}
/// Represents a ´DELETE´ clause builder for SQL queries
#[derive(Debug)]
pub struct Delete {
table: String,
condition: Option<String>,
}
impl Delete {
/// Creates a new `Delete` instance with the given table name.
/// # Example
/// ```
/// use lumus_sql_builder::sqlite::Delete;
///
/// let delete = Delete::new("users_tb").build();
///
/// assert_eq!("DELETE FROM users_tb;", delete.unwrap());
/// ```
pub fn new(table: &str) -> Self {
Self {
table: table.to_string(),
condition: None,
}
}
/// Specifies where for `Delete`.
pub fn condition(&mut self, condition: String) -> &mut Self {
self.condition = Some(condition);
self
}
pub fn build(&self) -> Result<String, SqlBuilderError> {
if self.table.is_empty() {
return Err(SqlBuilderError::EmptyTableName);
}
if let Some(condition) = &self.condition {
return Ok(format!("DELETE FROM {} WHERE {};", self.table, condition));
}
Ok(format!("DELETE FROM {};", self.table))
}
}
/// Implementation of the Display trait for `Delete`, allowing it to be printed.
impl fmt::Display for Delete {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.build() {
Err(e) => write!(f, "{}", e),
Ok(s) => write!(f, "{}", s),
}
}
}