mirror of
https://github.com/aljazceru/turso.git
synced 2026-02-03 07:14:33 +01:00
feat: add AddColumn instruction
This commit is contained in:
@@ -181,7 +181,7 @@ pub fn translate_alter_table(
|
||||
}
|
||||
}
|
||||
|
||||
btree.columns.push(column);
|
||||
btree.columns.push(column.clone());
|
||||
|
||||
let sql = btree.to_sql();
|
||||
let mut escaped = String::with_capacity(sql.len());
|
||||
@@ -219,9 +219,9 @@ pub fn translate_alter_table(
|
||||
value: schema.schema_version as i32 + 1,
|
||||
p5: 0,
|
||||
});
|
||||
program.emit_insn(Insn::ParseSchema {
|
||||
db: usize::MAX, // TODO: This value is unused, change when we do something with it
|
||||
where_clause: None,
|
||||
program.emit_insn(Insn::AddColumn {
|
||||
table: table_name.to_owned(),
|
||||
column,
|
||||
});
|
||||
},
|
||||
)?
|
||||
|
||||
@@ -6871,13 +6871,13 @@ pub fn op_drop_column(
|
||||
pager: &Rc<Pager>,
|
||||
mv_store: Option<&Arc<MvStore>>,
|
||||
) -> Result<InsnFunctionStepResult> {
|
||||
let Insn::DropColumn {
|
||||
table,
|
||||
column_index,
|
||||
} = insn
|
||||
else {
|
||||
unreachable!("unexpected Insn {:?}", insn)
|
||||
};
|
||||
load_insn!(
|
||||
DropColumn {
|
||||
table,
|
||||
column_index
|
||||
},
|
||||
insn
|
||||
);
|
||||
|
||||
let conn = program.connection.clone();
|
||||
|
||||
@@ -6887,16 +6887,45 @@ pub fn op_drop_column(
|
||||
.get_mut(table)
|
||||
.expect("table being renamed should be in schema");
|
||||
|
||||
{
|
||||
let table = Arc::make_mut(table);
|
||||
let table = Arc::make_mut(table);
|
||||
|
||||
let Table::BTree(btree) = table else {
|
||||
panic!("only btree tables can be renamed");
|
||||
};
|
||||
let Table::BTree(btree) = table else {
|
||||
panic!("only btree tables can be renamed");
|
||||
};
|
||||
|
||||
let btree = Arc::make_mut(btree);
|
||||
btree.columns.remove(*column_index)
|
||||
}
|
||||
let btree = Arc::make_mut(btree);
|
||||
btree.columns.remove(*column_index)
|
||||
});
|
||||
|
||||
state.pc += 1;
|
||||
Ok(InsnFunctionStepResult::Step)
|
||||
}
|
||||
|
||||
pub fn op_add_column(
|
||||
program: &Program,
|
||||
state: &mut ProgramState,
|
||||
insn: &Insn,
|
||||
pager: &Rc<Pager>,
|
||||
mv_store: Option<&Arc<MvStore>>,
|
||||
) -> Result<InsnFunctionStepResult> {
|
||||
load_insn!(AddColumn { table, column }, insn);
|
||||
|
||||
let conn = program.connection.clone();
|
||||
|
||||
conn.with_schema_mut(|schema| {
|
||||
let table = schema
|
||||
.tables
|
||||
.get_mut(table)
|
||||
.expect("table being renamed should be in schema");
|
||||
|
||||
let table = Arc::make_mut(table);
|
||||
|
||||
let Table::BTree(btree) = table else {
|
||||
panic!("only btree tables can be renamed");
|
||||
};
|
||||
|
||||
let btree = Arc::make_mut(btree);
|
||||
btree.columns.push(column.clone())
|
||||
});
|
||||
|
||||
state.pc += 1;
|
||||
|
||||
@@ -1645,6 +1645,15 @@ pub fn insn_to_str(
|
||||
0,
|
||||
format!("drop_column({table}, {column_index})"),
|
||||
),
|
||||
Insn::AddColumn { table, column } => (
|
||||
"AddColumn",
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
Value::build_text(""),
|
||||
0,
|
||||
format!("add_column({table}, {column:?})"),
|
||||
),
|
||||
Insn::MaxPgcnt { db, dest, new_max } => (
|
||||
"MaxPgcnt",
|
||||
*db as i32,
|
||||
|
||||
@@ -5,7 +5,7 @@ use std::{
|
||||
|
||||
use super::{execute, AggFunc, BranchOffset, CursorID, FuncCtx, InsnFunction, PageIdx};
|
||||
use crate::{
|
||||
schema::{Affinity, BTreeTable, Index},
|
||||
schema::{Affinity, BTreeTable, Column, Index},
|
||||
storage::{pager::CreateBTreeFlags, wal::CheckpointMode},
|
||||
translate::collate::CollationSeq,
|
||||
Value,
|
||||
@@ -1034,6 +1034,10 @@ pub enum Insn {
|
||||
table: String,
|
||||
column_index: usize,
|
||||
},
|
||||
AddColumn {
|
||||
table: String,
|
||||
column: Column,
|
||||
},
|
||||
/// Try to set the maximum page count for database P1 to the value in P3.
|
||||
/// Do not let the maximum page count fall below the current page count and
|
||||
/// do not change the maximum page count value if P3==0.
|
||||
@@ -1182,6 +1186,7 @@ impl Insn {
|
||||
Insn::IntegrityCk { .. } => execute::op_integrity_check,
|
||||
Insn::RenameTable { .. } => execute::op_rename_table,
|
||||
Insn::DropColumn { .. } => execute::op_drop_column,
|
||||
Insn::AddColumn { .. } => execute::op_add_column,
|
||||
Insn::MaxPgcnt { .. } => execute::op_max_pgcnt,
|
||||
Insn::JournalMode { .. } => execute::op_journal_mode,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user