Skip to content

SQLite Database

SQLiteDatabase

SQLiteDatabase()

SQLite database management and DataFrame persistence.

Purpose

Provides Data Management table operations, DataFrame transformations, interactive visualizations, and SQLite persistence.

Attributes:

Name Type Description
db_path str

Current SQLite database file path from configuration.

table str

Current table used by a database utility operation.

table_name str

Current table name for DataFrame persistence.

column str

Current column for database schema operations.

col_type str

Column type for schema updates.

limit Optional[int]

Maximum number of rows in the latest table read.

offset int

Starting row offset in the latest table read.

df_input Optional[DataFrame]

Input DataFrame for persistence operations.

Initialize the configured SQLite database path.

Purpose

Initialize the configured SQLite database path.

Returns:

Name Type Description
None None

Completes the operation through side effects.

Source code in storage.py
def __init__( self ) -> None:
	"""Initialize the configured SQLite database path.

	Purpose:
		Initialize the configured SQLite database path.

	Returns:
		None: Completes the operation through side effects.
	"""
	self.db_path = cfg.DB_PATH
	self.table = ''
	self.table_name = ''
	self.column = ''
	self.col_type = ''
	self.limit = None
	self.offset = 0
	self.df_input: Optional[ pd.DataFrame ] = None

Methods:

validate_database_file

validate_database_file() -> Path

Validate the required pre-hydrated SQLite database.

Purpose

Reject missing, invalid, or unhydrated Git LFS files before opening SQLite.

Returns:

Name Type Description
Path Path

Validated absolute SQLite database path.

Raises:

Type Description
FileNotFoundError

When the database file is missing.

ValueError

When the file does not have a SQLite header.

Source code in storage.py
def validate_database_file( self ) -> Path:
	"""Validate the required pre-hydrated SQLite database.

	Purpose:
		Reject missing, invalid, or unhydrated Git LFS files before opening SQLite.

	Returns:
		Path: Validated absolute SQLite database path.

	Raises:
		FileNotFoundError: When the database file is missing.
		ValueError: When the file does not have a SQLite header.
	"""
	self.db_path = cfg.DB_PATH
	path = Path( self.db_path ).resolve( )
	if not path.is_file( ):
		raise FileNotFoundError( f'Pre-hydrated SQLite database missing: {path}. '
		                         'Install it with git lfs pull when using Git LFS.' )
	with path.open( 'rb' ) as source:
		header = source.read( 16 )
	if header != b'SQLite format 3\x00':
		raise ValueError( f'Invalid SQLite database (possibly Git LFS pointer): {path}. '
		                  'Install its contents with git lfs pull.' )
	return path

create_connection

create_connection() -> Connection

Connect to an existing database for explicit Data Management writes.

Purpose

Preserve intentional writes but reject a missing pre-hydrated database.

Returns:

Type Description
Connection

sqlite3.Connection: Read-write connection to the existing database.

Source code in storage.py
def create_connection( self ) -> sqlite3.Connection:
	"""Connect to an existing database for explicit Data Management writes.

	Purpose:
		Preserve intentional writes but reject a missing pre-hydrated database.

	Returns:
		sqlite3.Connection: Read-write connection to the existing database.
	"""
	path = self.validate_database_file( )
	return sqlite3.connect( path.as_uri( ) + '?mode=rw', uri=True )

create_read_connection

create_read_connection() -> Connection

Connect to the operational SQLite database without write privileges.

Purpose

Use read-only connections for map queries and metadata inspection.

Returns:

Type Description
Connection

sqlite3.Connection: Read-only connection to the existing database.

Source code in storage.py
def create_read_connection( self ) -> sqlite3.Connection:
	"""Connect to the operational SQLite database without write privileges.

	Purpose:
		Use read-only connections for map queries and metadata inspection.

	Returns:
		sqlite3.Connection: Read-only connection to the existing database.
	"""
	path = self.validate_database_file( )
	return sqlite3.connect( path.as_uri( ) + '?mode=ro', uri=True )

list_tables

list_tables() -> List[str]

List the tables present in the configured SQLite database.

Purpose

List the tables present in the configured SQLite database.

Returns:

Type Description
List[str]

List[ str ]: Result of the list tables operation.

Source code in storage.py
def list_tables( self ) -> List[ str ]:
	"""List the tables present in the configured SQLite database.

	Purpose:
		List the tables present in the configured SQLite database.

	Returns:
		List[ str ]: Result of the list tables operation.
	"""
	with self.create_read_connection( ) as conn:
		_query = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name;"
		rows = conn.execute( _query ).fetchall( )
		return [ r[ 0 ] for r in rows ]

create_schema

create_schema(table: str) -> List[Tuple]

Read the column metadata of a SQLite table.

Purpose

Read the column metadata of a SQLite table.

Parameters:

Name Type Description Default
table str

SQLite table name.

required

Returns:

Type Description
List[Tuple]

List[ Tuple ]: Result of the create schema operation.

Source code in storage.py
def create_schema( self, table: str ) -> List[ Tuple ]:
	"""Read the column metadata of a SQLite table.

	Purpose:
		Read the column metadata of a SQLite table.

	Args:
		table (str): SQLite table name.

	Returns:
		List[ Tuple ]: Result of the create schema operation.
	"""
	try:
		throw_if( 'table', table )
		if not isinstance( table, str ):
			raise TypeError( 'table must be a string.' )
		self.table = table
		with self.create_read_connection( ) as conn:
			return conn.execute( f'PRAGMA table_info("{self.table}");' ).fetchall( )

	except Exception as e:
		exception = Error( e )
		exception.module = 'storage'
		exception.cause = 'SQLiteDatabase'
		exception.method = "create_schema( self, table: str ) -> List[ Tuple ]"
		Logger( ).write( exception )
		raise exception from e

read_selected_columns

read_selected_columns(
    table: str, columns: List[str]
) -> DataFrame

Load only the requested columns of an existing SQLite table.

Purpose

Support schema-aware interactive mapping without loading unrelated large text and binary fields. Validate identifiers against live SQLite metadata.

Parameters:

Name Type Description Default
table str

Existing SQLite table name.

required
columns List[str]

Nonempty column list drawn from the table schema.

required

Returns:

Type Description
DataFrame

pd.DataFrame: Selected database records in their original column order.

Raises:

Type Description
ValueError

When required arguments are missing or unknown identifiers occur.

TypeError

When argument types are invalid.

Error

When database retrieval fails.

Source code in storage.py
def read_selected_columns( self, table: str, columns: List[ str ] ) -> pd.DataFrame:
	"""Load only the requested columns of an existing SQLite table.

	Purpose:
		Support schema-aware interactive mapping without loading unrelated large
		text and binary fields. Validate identifiers against live SQLite metadata.

	Args:
		table (str): Existing SQLite table name.
		columns (List[str]): Nonempty column list drawn from the table schema.

	Returns:
		pd.DataFrame: Selected database records in their original column order.

	Raises:
		ValueError: When required arguments are missing or unknown identifiers occur.
		TypeError: When argument types are invalid.
		Error: When database retrieval fails.
	"""
	try:
		throw_if( 'table', table )
		throw_if( 'columns', columns )
		if not isinstance( table, str ):
			raise TypeError( 'table must be a string.' )
		if not isinstance( columns, list ) or any(
				not isinstance( column, str ) for column in columns ):
			raise TypeError( 'columns must be a list of strings.' )
		self.table = table
		self.columns = columns
		if self.table not in self.list_tables( ):
			raise ValueError( f'Unknown table: {self.table}' )
		self.available_columns = { column[ 1 ] for column in self.create_schema( self.table ) }
		if any( column not in self.available_columns for column in self.columns ):
			raise ValueError( 'Requested columns are not present in the table schema.' )
		self.quoted_columns = ', '.join(
			'"' + column.replace( '"', '""' ) + '"' for column in self.columns )
		self.quoted_table = '"' + self.table.replace( '"', '""' ) + '"'
		self.query = f'SELECT {self.quoted_columns} FROM {self.quoted_table}'
		with self.create_read_connection( ) as conn:
			cursor = conn.execute( self.query )
			rows = cursor.fetchall( )
		result_columns = [ item[ 0 ] for item in cursor.description ]
		normalized_rows: List[ Dict[ str, Any ] ] = [ ]
		for row in rows:
			record: Dict[ str, Any ] = { }
			for index, column in enumerate( result_columns ):
				value = row[ index ]
				if isinstance( value, bytes ):
					try:
						value = value.decode( 'utf-8' )
					except Exception:
						value = value.hex( )
				elif value is not None and not isinstance( value, (str, int, float, bool) ):
					value = str( value )
				record[ column ] = value
			normalized_rows.append( record )
		return pd.DataFrame( normalized_rows, columns=result_columns )
	except Exception as e:
		exception = Error( e )
		exception.module = 'storage'
		exception.cause = 'SQLiteDatabase'
		exception.method = (
			'read_selected_columns( self, table: str, columns: List[str] ) -> pd.DataFrame')
		Logger( ).write( exception )
		raise exception from e

read_table

read_table(
    table: str, limit: Optional[int] = None, offset: int = 0
) -> DataFrame
    Read a SQLite table into a pandas DataFrame using a normalized scalar-only path.
Purpose

Read a SQLite table into a pandas DataFrame using a normalized scalar-only path.

Parameters:

Name Type Description Default
table str

Table name.

required
limit Optional[int]

Maximum rows to retrieve; None selects all rows.

None
offset int

Number of rows to skip, starting at zero.

0

Returns:

Type Description
DataFrame

pd.DataFrame: DataFrame of plain Python scalar values.

Source code in storage.py
def read_table( self, table: str, limit: Optional[ int ] = None,
	offset: int = 0 ) -> pd.DataFrame:
	"""
		Read a SQLite table into a pandas DataFrame using a normalized scalar-only path.

	Purpose:
		Read a SQLite table into a pandas DataFrame using a normalized scalar-only path.

	Args:
		table (str): Table name.
		limit (Optional[int]): Maximum rows to retrieve; None selects all rows.
		offset (int): Number of rows to skip, starting at zero.

	Returns:
		pd.DataFrame: DataFrame of plain Python scalar values.
		"""
	try:
		throw_if( 'table', table )
		if not isinstance( table, str ):
			raise TypeError( 'table must be a string.' )
		if limit is not None and (type( limit ) is not int or limit <= 0):
			raise ValueError( 'limit must be a positive integer when provided.' )
		if type( offset ) is not int or offset < 0:
			raise ValueError( 'offset must be a nonnegative integer.' )
		self.table = table
		self.limit = limit
		self.offset = offset
		query = f'SELECT * FROM "{self.table}"'
		if self.limit is not None:
			query += f' LIMIT {self.limit} OFFSET {self.offset}'
		with self.create_read_connection( ) as conn:
			cur = conn.cursor( )
			cur.execute( query )
			raw_columns = [ d[ 0 ] for d in (cur.description or [ ]) ]
			rows = cur.fetchall( )
		seen: Dict[ str, int ] = { }
		columns: List[ str ] = [ ]
		for col in raw_columns:
			name = str( col )
			if name not in seen:
				seen[ name ] = 0
				columns.append( name )
			else:
				seen[ name ] += 1
				columns.append( f'{name}_{seen[ name ]}' )

		def scalarize( value: Any ) -> Any:
			"""Prepare SQLite result values for tabular presentation.

			Purpose:
				Decodes binary values and converts unsupported payloads at the display boundary.

			Args:
				value (Any): Value returned from SQLite.

			Returns:
				Any: Scalar or serialized representation of the database value.
			"""
			if value is None or isinstance( value, (str, int, float, bool) ):
				return value
			if isinstance( value, bytes ):
				try:
					return value.decode( 'utf-8' )
				except Exception:
					return value.hex( )
			if isinstance( value, (list, tuple, set, dict) ):
				try:
					return str( normalize( value ) )
				except Exception:
					return str( value )
			try:
				return str( value.model_dump( ) )
			except AttributeError:
				return str( value )
			except Exception:
				return str( value )

		normalized_rows: List[ Dict[ str, Any ] ] = [ ]
		for row in rows:
			record: Dict[ str, Any ] = { }
			for idx, col in enumerate( columns ):
				record[ col ] = scalarize( row[ idx ] )
			normalized_rows.append( record )
		return pd.DataFrame( normalized_rows, columns=columns )

	except Exception as e:
		exception = Error( e )
		exception.module = 'storage'
		exception.cause = 'SQLiteDatabase'
		exception.method = (
			"read_table( self, table: str, limit: Optional[ int ] = None, offset: int = 0 ) -> "
			"pd.DataFrame:")
		Logger( ).write( exception )
		raise exception from e

render_table

render_table(df: DataFrame | None) -> None
    Render a DataFrame safely in Streamlit. Use the normal interactive dataframe.
Purpose

Render a DataFrame safely in Streamlit. Use the normal interactive dataframe first, and fall back to HTML rendering if Streamlit/PyArrow serialization fails.

Parameters:

Name Type Description Default
df DataFrame

The DataFrame to render.

required

Returns:

Name Type Description
None None

Renders the selected database table in Streamlit.

Source code in storage.py
def render_table( self, df: pd.DataFrame | None ) -> None:
	"""
		Render a DataFrame safely in Streamlit. Use the normal interactive dataframe.

	Purpose:
		Render a DataFrame safely in Streamlit. Use the normal interactive dataframe
		first, and fall back to HTML rendering if Streamlit/PyArrow serialization fails.

	Args:
		df (pd.DataFrame): The DataFrame to render.

	Returns:
		None: Renders the selected database table in Streamlit.
		"""
	if df is None:
		st.info( 'No data available.' )
		return
	try:
		st.data_editor( df, use_container_width=True )
		return
	except Exception:
		pass
	fallback_df = df.copy( )
	fallback_df = fallback_df.where( pd.notnull( fallback_df ), '' )
	for col in fallback_df.columns:
		fallback_df[ col ] = fallback_df[ col ].map( lambda x: x if isinstance( x, (str, int,
			float, bool) ) or x == '' else str( x ) )
	st.markdown( fallback_df.to_html( index=False, escape=True ), unsafe_allow_html=True )

make_display_safe

make_display_safe(df: DataFrame) -> DataFrame

Return a DataFrame with values suitable for Streamlit rendering.

Purpose

Return a DataFrame with values suitable for Streamlit rendering.

Parameters:

Name Type Description Default
df DataFrame

Input pandas DataFrame.

required

Returns:

Type Description
DataFrame

pd.DataFrame: Result of the make display safe operation.

Source code in storage.py
def make_display_safe( self, df: pd.DataFrame ) -> pd.DataFrame:
	"""Return a DataFrame with values suitable for Streamlit rendering.

	Purpose:
		Return a DataFrame with values suitable for Streamlit rendering.

	Args:
		df (pd.DataFrame): Input pandas DataFrame.

	Returns:
		pd.DataFrame: Result of the make display safe operation.
	"""
	throw_if( 'df', df )
	if not isinstance( df, pd.DataFrame ):
		raise TypeError( 'df must be a DataFrame.' )
	display_df = df.copy( )
	for col in display_df.columns:
		display_df[ col ] = display_df[ col ].map( lambda x: '' if x is None else str( x ) )
	return display_df

drop_table

drop_table(table: str) -> None
    Safely drop a table if it exists.
Purpose

Safely drop a table if it exists.

Parameters:

Name Type Description Default
table str

Table name.

required

Returns:

Name Type Description
None None

Completes the operation through side effects.

Source code in storage.py
def drop_table( self, table: str ) -> None:
	"""
		Safely drop a table if it exists.

	Purpose:
		Safely drop a table if it exists.

	Args:
		table (str): Table name.

	Returns:
		None: Completes the operation through side effects.
	"""
	try:
		throw_if( 'table', table )
		if not isinstance( table, str ):
			raise TypeError( 'table must be a string.' )
		self.table = table
		with self.create_connection( ) as conn:
			conn.execute( f'DROP TABLE IF EXISTS "{self.table}";' )
			conn.commit( )

	except Exception as e:
		exception = Error( e )
		exception.module = 'storage'
		exception.cause = 'SQLiteDatabase'
		exception.method = "drop_table( self, table: str ) -> None"
		Logger( ).write( exception )
		raise exception from e

create_index

create_index(table: str, column: str) -> None
    Create a safe SQLite index on a specified table column.
Purpose

Create a safe SQLite index on a specified table column.

Handles: - Spaces in column names - Special characters - Reserved words - Duplicate index names - Validation against actual table schema

Parameters:

Name Type Description Default
table str

Table name.

required
column str

Column name to index.

required

Returns:

Name Type Description
None None

Completes the operation through side effects.

Source code in storage.py
def create_index( self, table: str, column: str ) -> None:
	"""
		Create a safe SQLite index on a specified table column.

	Purpose:
		Create a safe SQLite index on a specified table column.

		Handles:
		- Spaces in column names
		- Special characters
		- Reserved words
		- Duplicate index names
		- Validation against actual table schema

	Args:
		table (str): Table name.
		column (str): Column name to index.

	Returns:
		None: Completes the operation through side effects.
	"""
	try:
		throw_if( 'table', table )
		throw_if( 'column', column )
		if not isinstance( table, str ) or not isinstance( column, str ):
			raise TypeError( 'table and column must be strings.' )
		self.table = table
		self.column = column
		# ------------------------------------------------------------------
		# Validate table exists
		# ------------------------------------------------------------------
		tables = self.list_tables( )
		if self.table not in tables:
			raise ValueError( 'Invalid table name.' )
		# ------------------------------------------------------------------
		# Validate column exists
		# ------------------------------------------------------------------
		schema = self.create_schema( self.table )
		valid_columns = [ col[ 1 ] for col in schema ]
		if self.column not in valid_columns:
			raise ValueError( 'Invalid column name.' )
		# ------------------------------------------------------------------
		# Sanitize index name (identifier only)
		# ------------------------------------------------------------------
		safe_index_name = re.sub( r"[^0-9a-zA-Z_]+", "_", f"idx_{self.table}_{self.column}" )
		# ------------------------------------------------------------------
		# Create index safely (quote identifiers)
		# ------------------------------------------------------------------
		sql = f'CREATE INDEX IF NOT EXISTS "{safe_index_name}" ON "{self.table}"("{self.column}");'
		with self.create_connection( ) as conn:
			conn.execute( sql )
			conn.commit( )

	except Exception as e:
		exception = Error( e )
		exception.module = 'storage'
		exception.cause = 'SQLiteDatabase'
		exception.method = "create_index( self, table: str, column: str ) -> None"
		Logger( ).write( exception )
		raise exception from e

apply_filters

apply_filters(df: DataFrame) -> DataFrame

Render advanced DataFrame filters and return the filtered rows.

Purpose

Render advanced DataFrame filters and return the filtered rows.

Parameters:

Name Type Description Default
df DataFrame

Input pandas DataFrame.

required

Returns:

Type Description
DataFrame

pd.DataFrame: Result of the apply filters operation.

Source code in storage.py
def apply_filters( self, df: pd.DataFrame ) -> pd.DataFrame:
	"""Render advanced DataFrame filters and return the filtered rows.

	Purpose:
		Render advanced DataFrame filters and return the filtered rows.

	Args:
		df (pd.DataFrame): Input pandas DataFrame.

	Returns:
		pd.DataFrame: Result of the apply filters operation.
	"""
	throw_if( 'df', df )
	if not isinstance( df, pd.DataFrame ):
		raise TypeError( 'df must be a DataFrame.' )
	st.subheader( 'Advanced Filters' )
	conditions = [ ]
	col1, col2, col3 = st.columns( 3 )
	column = col1.selectbox( 'Column', df.columns )
	operator = col2.selectbox( 'Operator', [ '=', '!=', '>', '<', '>=', '<=', 'contains' ] )
	value = col3.text_input( 'Value' )
	if value:
		if operator == '=':
			df = df[ df[ column ] == value ]
		elif operator == '!=':
			df = df[ df[ column ] != value ]
		elif operator == '>':
			df = df[ df[ column ].astype( float ) > float( value ) ]
		elif operator == '<':
			df = df[ df[ column ].astype( float ) < float( value ) ]
		elif operator == '>=':
			df = df[ df[ column ].astype( float ) >= float( value ) ]
		elif operator == '<=':
			df = df[ df[ column ].astype( float ) <= float( value ) ]
		elif operator == 'contains':
			df = df[ df[ column ].astype( str ).str.contains( value ) ]
	return df

create_aggregation

create_aggregation(df: DataFrame) -> None

Calculate and display a selected numeric aggregation.

Purpose

Calculate and display a selected numeric aggregation.

Parameters:

Name Type Description Default
df DataFrame

Input pandas DataFrame.

required

Returns:

Name Type Description
None None

Displays the selected aggregation result in Streamlit, or an informational message when no numeric column exists.

Source code in storage.py
def create_aggregation( self, df: pd.DataFrame ) -> None:
	"""Calculate and display a selected numeric aggregation.

	Purpose:
		Calculate and display a selected numeric aggregation.

	Args:
		df (pd.DataFrame): Input pandas DataFrame.

	Returns:
		None: Displays the selected aggregation result in Streamlit, or an informational message when no numeric column exists.
	"""
	throw_if( 'df', df )
	if not isinstance( df, pd.DataFrame ):
		raise TypeError( 'df must be a DataFrame.' )
	st.subheader( 'Aggregation Engine' )
	numeric_cols = df.select_dtypes( include=[ 'number' ] ).columns.tolist( )
	if not numeric_cols:
		st.info( 'No numeric columns available.' )
		return
	col = st.selectbox( 'Column', numeric_cols )
	agg = st.selectbox( 'Aggregation', [ 'COUNT', 'SUM', 'AVG', 'MIN', 'MAX', 'MEDIAN' ] )
	if agg == 'COUNT':
		result = df[ col ].count( )
	elif agg == 'SUM':
		result = df[ col ].sum( )
	elif agg == 'AVG':
		result = df[ col ].mean( )
	elif agg == 'MIN':
		result = df[ col ].min( )
	elif agg == 'MAX':
		result = df[ col ].max( )
	elif agg == 'MEDIAN':
		result = df[ col ].median( )
	st.metric( 'Result', result )

create_visualization

create_visualization(df: DataFrame) -> None
    Render data visualizations without passing pandas objects directly into.
Purpose

Render data visualizations without passing pandas objects directly into Plotly/Narwhals.

Parameters:

Name Type Description Default
df DataFrame

The input DataFrame.

required

Returns:

Name Type Description
None None

Renders a visualization in the Streamlit interface.

Source code in storage.py
def create_visualization( self, df: pd.DataFrame ) -> None:
	"""
		Render data visualizations without passing pandas objects directly into.

	Purpose:
		Render data visualizations without passing pandas objects directly into
		Plotly/Narwhals.

	Args:
		df (pd.DataFrame): The input DataFrame.

	Returns:
		None: Renders a visualization in the Streamlit interface.
		"""
	throw_if( 'df', df )
	if not isinstance( df, pd.DataFrame ):
		raise TypeError( 'df must be a DataFrame.' )
	blue_divider( )
	st.markdown( '##### Visualization Engine' )
	if df is None or df.empty:
		st.info( 'No data available.' )
		return
	df_plot = df.copy( )
	for col in df_plot.columns:
		if df_plot[ col ].dtype == object:
			df_plot[ col ] = df_plot[ col ].map( lambda x: '' if x is None else str( x ) )
	numeric_cols: List[ str ] = [ ]
	for col in df_plot.columns:
		series_num = pd.to_numeric( df_plot[ col ], errors='coerce' )
		if series_num.notna( ).any( ):
			numeric_cols.append( col )
	categorical_cols: List[ str ] = [ col for col in df_plot.columns if
		col not in numeric_cols ]
	chart = st.selectbox( 'Chart Type', [ 'Histogram', 'Bar', 'Line', 'Scatter', 'Box', 'Pie',
		'Correlation' ] )
	if chart == 'Histogram':
		if not numeric_cols:
			st.info( 'No numeric columns available.' )
			return
		col = st.selectbox( 'Column', numeric_cols )
		values = pd.to_numeric( df_plot[ col ], errors='coerce' ).dropna( ).tolist( )
		fig = go.Figure( data=[ go.Histogram( x=values ) ] )
		fig.update_layout( xaxis_title=col, yaxis_title='Count' )
		st.plotly_chart( fig, use_container_width=True )

	elif chart == 'Bar':
		if not numeric_cols:
			st.info( 'No numeric columns available.' )
			return
		x = st.selectbox( 'X', df_plot.columns )
		y = st.selectbox( 'Y', numeric_cols )
		x_values = df_plot[ x ].astype( str ).tolist( )
		y_values = pd.to_numeric( df_plot[ y ], errors='coerce' ).fillna( 0 ).tolist( )
		fig = go.Figure( data=[ go.Bar( x=x_values, y=y_values ) ] )
		fig.update_layout( xaxis_title=x, yaxis_title=y )
		st.plotly_chart( fig, use_container_width=True )

	elif chart == 'Line':
		if not numeric_cols:
			st.info( 'No numeric columns available.' )
			return
		x = st.selectbox( 'X', df_plot.columns )
		y = st.selectbox( 'Y', numeric_cols )
		x_values = df_plot[ x ].astype( str ).tolist( )
		y_values = pd.to_numeric( df_plot[ y ], errors='coerce' ).fillna( 0 ).tolist( )
		fig = go.Figure( data=[ go.Scatter( x=x_values, y=y_values, mode='lines' ) ] )
		fig.update_layout( xaxis_title=x, yaxis_title=y )
		st.plotly_chart( fig, use_container_width=True )

	elif chart == 'Scatter':
		if len( numeric_cols ) < 2:
			st.info( 'At least two numeric columns are required.' )
			return
		x = st.selectbox( 'X', numeric_cols, key='viz_scatter_x' )
		y = st.selectbox( 'Y', numeric_cols, key='viz_scatter_y' )
		x_series = pd.to_numeric( df_plot[ x ], errors='coerce' )
		y_series = pd.to_numeric( df_plot[ y ], errors='coerce' )
		mask = x_series.notna( ) & y_series.notna( )
		x_values = x_series[ mask ].tolist( )
		y_values = y_series[ mask ].tolist( )
		fig = go.Figure( data=[ go.Scatter( x=x_values, y=y_values, mode='markers' ) ] )
		fig.update_layout( xaxis_title=x, yaxis_title=y )
		st.plotly_chart( fig, use_container_width=True )

	elif chart == 'Box':
		if not numeric_cols:
			st.info( 'No numeric columns available.' )
			return
		col = st.selectbox( 'Column', numeric_cols, key='viz_box_col' )
		values = pd.to_numeric( df_plot[ col ], errors='coerce' ).dropna( ).tolist( )
		fig = go.Figure( data=[ go.Box( y=values, name=col ) ] )
		fig.update_layout( yaxis_title=col )
		st.plotly_chart( fig, use_container_width=True )

	elif chart == 'Pie':
		if not categorical_cols:
			st.info( 'No categorical columns available.' )
			return
		col = st.selectbox( 'Category Column', categorical_cols )
		counts = df_plot[ col ].astype( str ).value_counts( )
		fig = go.Figure( data=[
			go.Pie( labels=counts.index.tolist( ), values=counts.values.tolist( ) ) ] )
		st.plotly_chart( fig, use_container_width=True )

	elif chart == 'Correlation':
		if len( numeric_cols ) < 2:
			st.info( 'At least two numeric columns are required.' )
			return
		corr_df = pd.DataFrame( )
		for col in numeric_cols:
			corr_df[ col ] = pd.to_numeric( df_plot[ col ], errors='coerce' )
		corr = corr_df.corr( )
		fig = go.Figure( data=[
			go.Heatmap( z=corr.values.tolist( ), x=corr.columns.tolist( ), y=corr.index.tolist( ) ) ] )
		st.plotly_chart( fig, use_container_width=True )

convert_dataframe

convert_dataframe(table_name: str, df: DataFrame) -> None

Create a SQLite table from the DataFrame's column types.

Purpose

Create a SQLite table from the DataFrame's column types.

Parameters:

Name Type Description Default
table_name str

SQLite table name.

required
df DataFrame

Input pandas DataFrame.

required

Returns:

Name Type Description
None None

Creates a SQLite table using the input DataFrame's inferred column types.

Source code in storage.py
def convert_dataframe( self, table_name: str, df: pd.DataFrame ) -> None:
	"""Create a SQLite table from the DataFrame's column types.

	Purpose:
		Create a SQLite table from the DataFrame's column types.

	Args:
		table_name (str): SQLite table name.
		df (pd.DataFrame): Input pandas DataFrame.

	Returns:
		None: Creates a SQLite table using the input DataFrame's inferred column types.
	"""
	try:
		throw_if( 'table_name', table_name )
		throw_if( 'df', df )
		if not isinstance( table_name, str ) or not isinstance( df, pd.DataFrame ):
			raise TypeError( 'Expected a table name string and a pandas DataFrame.' )
		self.table_name = table_name
		self.df_input = df
		columns = [ ]
		for col in self.df_input.columns:
			sql_type = self.get_sqlite_type( self.df_input[ col ].dtype )
			safe_col = '"' + col.replace( '"', '""' ) + '"'
			columns.append( f'{safe_col} {sql_type}' )
		quoted_table = '"' + self.table_name.replace( '"', '""' ) + '"'
		create_stmt = f'CREATE TABLE IF NOT EXISTS {quoted_table} ({", ".join( columns )});'
		with self.create_connection( ) as conn:
			conn.execute( create_stmt )
			conn.commit( )

	except Exception as e:
		exception = Error( e )
		exception.module = 'storage'
		exception.cause = 'SQLiteDatabase'
		exception.method = "convert_dataframe( self, table_name: str, df: pd.DataFrame ) -> None"
		Logger( ).write( exception )
		raise exception from e

insert_data

insert_data(table_name: str, df: DataFrame) -> None

Insert a DataFrame's rows into a SQLite table.

Purpose

Insert a DataFrame's rows into a SQLite table.

Parameters:

Name Type Description Default
table_name str

SQLite table name.

required
df DataFrame

Input pandas DataFrame.

required

Returns:

Name Type Description
None None

Inserts the DataFrame rows into the selected SQLite table.

Source code in storage.py
def insert_data( self, table_name: str, df: pd.DataFrame ) -> None:
	"""Insert a DataFrame's rows into a SQLite table.

	Purpose:
		Insert a DataFrame's rows into a SQLite table.

	Args:
		table_name (str): SQLite table name.
		df (pd.DataFrame): Input pandas DataFrame.

	Returns:
		None: Inserts the DataFrame rows into the selected SQLite table.
	"""
	try:
		throw_if( 'table_name', table_name )
		throw_if( 'df', df )
		if not isinstance( table_name, str ) or not isinstance( df, pd.DataFrame ):
			raise TypeError( 'Expected a table name string and a pandas DataFrame.' )
		self.table_name = table_name
		self.df_input = df
		df = self.df_input.copy( )
		quoted_table = '"' + self.table_name.replace( '"', '""' ) + '"'
		quoted_columns = ', '.join( '"' + col.replace( '"', '""' ) + '"' for col in df.columns )
		placeholders = ', '.join( [ '?' ] * len( df.columns ) )
		stmt = f'INSERT INTO {quoted_table} ({quoted_columns}) VALUES ({placeholders});'
		with self.create_connection( ) as conn:
			conn.executemany( stmt, df.values.tolist( ) )
			conn.commit( )

	except Exception as e:
		exception = Error( e )
		exception.module = 'storage'
		exception.cause = 'SQLiteDatabase'
		exception.method = "insert_data( self, table_name: str, df: pd.DataFrame ) -> None"
		Logger( ).write( exception )
		raise exception from e

get_sqlite_type

get_sqlite_type(dtype: Any) -> str
    Map a pandas dtype to an appropriate SQLite column type.
Purpose

Map a pandas dtype to an appropriate SQLite column type.

Parameters:

Name Type Description Default
dtype pandas dtype

The dtype of a pandas Series.

required

Returns:

Name Type Description
str str

SQLite column type.

Source code in storage.py
def get_sqlite_type( self, dtype: Any ) -> str:
	"""
		Map a pandas dtype to an appropriate SQLite column type.

	Purpose:
		Map a pandas dtype to an appropriate SQLite column type.

	Args:
		dtype (pandas dtype): The dtype of a pandas Series.

	Returns:
		str: SQLite column type.
		"""
	dtype_str = str( dtype ).lower( )
	# ------------------------------------------------------------------
	# Integer Types (including nullable Int64)
	# ------------------------------------------------------------------
	if 'int' in dtype_str:
		return 'INTEGER'
	# ------------------------------------------------------------------
	# Float Types
	# ------------------------------------------------------------------
	if 'float' in dtype_str:
		return 'REAL'
	# ------------------------------------------------------------------
	# Boolean
	# ------------------------------------------------------------------
	if 'bool' in dtype_str:
		return 'INTEGER'
	# ------------------------------------------------------------------
	# Datetime
	# ------------------------------------------------------------------
	if 'datetime' in dtype_str:
		return 'TEXT'
	# ------------------------------------------------------------------
	# Categorical
	# ------------------------------------------------------------------
	if 'category' in dtype_str:
		return 'TEXT'
	# ------------------------------------------------------------------
	# Default fallback
	# ------------------------------------------------------------------
	return 'TEXT'

create_custom_table

create_custom_table(table_name: str, columns: list) -> None
    Create a custom SQLite table from column definitions.
Purpose

Create a custom SQLite table from column definitions.

Parameters:

Name Type Description Default
table_name str

Name of table.

required
columns list of dict

[ { "name": str, "type": str, "not_null": bool, "primary_key": bool, "auto_increment": bool } ]

required

Returns:

Name Type Description
None None

Completes the operation through side effects.

Source code in storage.py
def create_custom_table( self, table_name: str, columns: list ) -> None:
	"""
		Create a custom SQLite table from column definitions.

	Purpose:
		Create a custom SQLite table from column definitions.

	Args:
		table_name (str): Name of table.

		columns (list of dict): [
			{
			"name": str,
			"type": str,
			"not_null": bool,
			"primary_key": bool,
			"auto_increment": bool
			}
			]

	Returns:
		None: Completes the operation through side effects.
	"""
	throw_if( 'table_name', table_name )
	throw_if( 'columns', columns )
	if not isinstance( table_name, str ) or not isinstance( columns, list ):
		raise TypeError( 'table_name must be a string and columns a list.' )
	# Validate identifier
	if not re.match( r"^[A-Za-z_][A-Za-z0-9_]*$", table_name ):
		raise ValueError( 'Invalid table name.' )
	col_defs = [ ]
	for col in columns:
		col_name = col[ 'name' ]
		col_type = col[ 'type' ].upper( )
		if not re.match( r"^[A-Za-z_][A-Za-z0-9_]*$", col_name ):
			raise ValueError( f"Invalid column name: {col_name}" )
		definition = f'"{col_name}" {col_type}'
		if col[ 'primary_key' ]:
			definition += ' PRIMARY KEY'
			if col[ 'auto_increment' ] and col_type == 'INTEGER':
				definition += ' AUTOINCREMENT'
		if col[ "not_null" ]:
			definition += " NOT NULL"
		col_defs.append( definition )
	sql = f'CREATE TABLE IF NOT EXISTS "{table_name}" ({", ".join( col_defs )});'
	with self.create_connection( ) as conn:
		conn.execute( sql )
		conn.commit( )

is_safe_query

is_safe_query(query: str) -> bool
    Determine whether a SQL query is read-only and safe to execute.
Purpose

Determine whether a SQL query is read-only and safe to execute.

Allows: SELECT WITH (CTE returning SELECT) EXPLAIN SELECT PRAGMA (read-only)

Blocks: INSERT, UPDATE, DELETE, DROP, ALTER, CREATE, ATTACH, DETACH, VACUUM, REPLACE, TRIGGER, and multiple statements.

Parameters:

Name Type Description Default
query str

SQL query or identifier to inspect.

required

Returns:

Name Type Description
bool bool

Result of the is safe query operation.

Source code in storage.py
def is_safe_query( self, query: str ) -> bool:
	"""
		Determine whether a SQL query is read-only and safe to execute.

	Purpose:
		Determine whether a SQL query is read-only and safe to execute.

		Allows:
		SELECT
		WITH (CTE returning SELECT)
		EXPLAIN SELECT
		PRAGMA (read-only)

		Blocks:
		INSERT, UPDATE, DELETE, DROP, ALTER, CREATE, ATTACH,
		DETACH, VACUUM, REPLACE, TRIGGER, and multiple statements.

	Args:
		query (str): SQL query or identifier to inspect.

	Returns:
		bool: Result of the is safe query operation.
	"""
	if query is None:
		throw_if( 'query', query )
	if not isinstance( query, str ) or not query.strip( ):
		return False
	q = query.strip( ).lower( )
	# ------------------------------------------------------------------
	# Block multiple statements
	# ------------------------------------------------------------------
	if ';' in q[ :-1 ]:
		return False
	# ------------------------------------------------------------------
	# Remove SQL comments
	# ------------------------------------------------------------------
	q = re.sub( r"--.*?$", "", q, flags=re.MULTILINE )
	q = re.sub( r"/\*.*?\*/", "", q, flags=re.DOTALL )
	q = q.strip( )
	# ------------------------------------------------------------------
	# Allowed starting keywords
	# ------------------------------------------------------------------
	allowed_starts = ('select', 'with', 'explain', 'pragma')
	if not q.startswith( allowed_starts ):
		return False
	# Explicitly allow only read-only PRAGMA inspection names. PRAGMA assignments,
	# parenthesized setters, checkpoints, and other state changes are rejected.
	if q.startswith( 'pragma' ):
		pragma_name = re.match( r'^pragma\s+(?:[a-z_][a-z0-9_]*\.)?([a-z_][a-z0-9_]*)', q )
		allowed_pragmas = { 'table_info', 'table_xinfo', 'table_list', 'index_list',
			'index_info', 'index_xinfo', 'foreign_key_list', 'database_list', 'compile_options',
			'collation_list' }
		if (pragma_name is None or pragma_name.group( 1 ) not in allowed_pragmas or '=' in q):
			return False
	# ------------------------------------------------------------------
	# Block dangerous keywords anywhere
	# ------------------------------------------------------------------
	blocked_keywords = ('insert ', 'update ', 'delete ', 'drop ', 'alter ', 'create ',
		'attach ', 'detach ', 'vacuum ', 'replace ', 'trigger ')
	for keyword in blocked_keywords:
		if keyword in q:
			return False
	return True

create_identifier

create_identifier(name: str) -> str
    Sanitize a string into a safe SQLite identifier.
Purpose

Sanitize a string into a safe SQLite identifier.

  • Replaces invalid characters with underscores
  • Ensures it starts with a letter or underscore
  • Prevents empty names

Parameters:

Name Type Description Default
name str

Original identifier to sanitize.

required

Returns:

Name Type Description
str str

Result of the create identifier operation.

Source code in storage.py
def create_identifier( self, name: str ) -> str:
	"""
		Sanitize a string into a safe SQLite identifier.

	Purpose:
		Sanitize a string into a safe SQLite identifier.

		- Replaces invalid characters with underscores
		- Ensures it starts with a letter or underscore
		- Prevents empty names

	Args:
		name (str): Original identifier to sanitize.

	Returns:
		str: Result of the create identifier operation.
	"""
	throw_if( 'name', name )
	if not isinstance( name, str ):
		raise TypeError( 'name must be a string.' )
	safe = re.sub( r'[^0-9a-zA-Z_]', '_', name.strip( ) )
	if not re.match( r'^[A-Za-z_]', safe ):
		safe = f'_{safe}'
	if not safe:
		raise ValueError( 'Invalid identifier after sanitization.' )
	return safe

get_indexes

get_indexes(table: str) -> List[Tuple]

Return index metadata for a SQLite table.

Purpose

Return index metadata for a SQLite table.

Parameters:

Name Type Description Default
table str

SQLite table name.

required

Returns:

Type Description
List[Tuple]

List[Tuple]: SQLite index metadata rows returned by PRAGMA index_list.

Source code in storage.py
def get_indexes( self, table: str ) -> List[ Tuple ]:
	"""Return index metadata for a SQLite table.

	Purpose:
		Return index metadata for a SQLite table.

	Args:
		table (str): SQLite table name.

	Returns:
		List[Tuple]: SQLite index metadata rows returned by PRAGMA index_list.
	"""
	try:
		throw_if( 'table', table )
		if not isinstance( table, str ):
			raise TypeError( 'table must be a string.' )
		self.table = table
		with self.create_connection( ) as conn:
			rows = conn.execute( f'PRAGMA index_list("{self.table}");' ).fetchall( )
			return rows

	except Exception as e:
		exception = Error( e )
		exception.module = 'storage'
		exception.cause = 'SQLiteDatabase'
		exception.method = "get_indexes( self, table: str ) -> List[ Tuple ]"
		Logger( ).write( exception )
		raise exception from e

add_column

add_column(table: str, column: str, col_type: str) -> None

Add a typed column to an existing SQLite table.

Purpose

Add a typed column to an existing SQLite table.

Parameters:

Name Type Description Default
table str

SQLite table name.

required
column str

SQLite column name.

required
col_type str

SQLite column type.

required

Returns:

Name Type Description
None None

Adds the specified column to the existing SQLite table.

Source code in storage.py
def add_column( self, table: str, column: str, col_type: str ) -> None:
	"""Add a typed column to an existing SQLite table.

	Purpose:
		Add a typed column to an existing SQLite table.

	Args:
		table (str): SQLite table name.
		column (str): SQLite column name.
		col_type (str): SQLite column type.

	Returns:
		None: Adds the specified column to the existing SQLite table.
	"""
	try:
		throw_if( 'table', table )
		throw_if( 'column', column )
		throw_if( 'col_type', col_type )
		if not all( isinstance( item, str ) for item in (table, column, col_type) ):
			raise TypeError( 'table, column, and col_type must be strings.' )
		self.table = table
		self.column = self.create_identifier( column )
		self.col_type = col_type.upper( )
		with self.create_connection( ) as conn:
			conn.execute( f'ALTER TABLE "{self.table}" ADD COLUMN "{self.column}" {self.col_type};' )
			conn.commit( )

	except Exception as e:
		exception = Error( e )
		exception.module = 'storage'
		exception.cause = 'SQLiteDatabase'
		exception.method = "add_column( self, table: str, column: str, col_type: str ) -> None"
		Logger( ).write( exception )
		raise exception from e

rename_column

rename_column(
    table_name: str, old_name: str, new_name: str
) -> None
    Rename a column within an existing SQLite table. Attempts native ALTER TABLE rename.
Purpose

Rename a column within an existing SQLite table. Attempts native ALTER TABLE rename first; if it fails, falls back to a schema-safe rebuild preserving column order, data, and indexes.

Parameters:

Name Type Description Default
table_name str

Table containing the column.

required
old_name str

Existing column name.

required
new_name str

New column name.

required

Returns:

Name Type Description
None None

Renames the selected SQLite column.

Source code in storage.py
	def rename_column( self, table_name: str, old_name: str, new_name: str ) -> None:
		"""
			Rename a column within an existing SQLite table. Attempts native ALTER TABLE rename.

		Purpose:
			Rename a column within an existing SQLite table. Attempts native ALTER TABLE rename
			first; if it fails, falls back to a schema-safe rebuild preserving column order, data,
			and indexes.

		Args:
			table_name (str): Table containing the column.

			old_name (str): Existing column name.

			new_name (str): New column name.

		Returns:
			None: Renames the selected SQLite column.
			"""
		try:
			throw_if( 'table_name', table_name )
			throw_if( 'old_name', old_name )
			throw_if( 'new_name', new_name )
			with self.create_connection( ) as conn:
				try:
					conn.execute( f'ALTER TABLE "{table_name}" RENAME COLUMN "{old_name}" TO "{new_name}";' )
					conn.commit( )
					return
				except Exception:
					pass
				row = conn.execute( """
                                    SELECT sql
                                    FROM sqlite_master
                                    WHERE type ='table' AND name =?
				                    """, (table_name,) ).fetchone( )
				if not row or not row[ 0 ]:
					raise ValueError( "Table definition not found." )
				create_sql = row[ 0 ]
				indexes = conn.execute( """
                                        SELECT sql
                                        FROM sqlite_master
                                        WHERE type ='index' AND tbl_name=? AND sql IS NOT NULL
				                        """, (table_name,) ).fetchall( )
				schema = conn.execute( f'PRAGMA table_info("{table_name}");' ).fetchall( )
				cols = [ r[ 1 ] for r in schema ]
				if old_name not in cols:
					raise ValueError( "Column not found." )
				mapped_cols = [ (new_name if c == old_name else c) for c in cols ]
				temp_table = f"{table_name}__rebuild_temp"
				col_defs: List[ str ] = [ ]
				pk_cols = [ r for r in schema if int( r[ 5 ] or 0 ) > 0 ]
				single_pk = len( pk_cols ) == 1
				for row in schema:
					col_name = row[ 1 ]
					col_type = row[ 2 ] or ''
					not_null = int( row[ 3 ] or 0 )
					default_value = row[ 4 ]
					pk = int( row[ 5 ] or 0 )
					out_name = new_name if col_name == old_name else col_name
					col_def = f'"{out_name}" {col_type}'.strip( )
					if not_null:
						col_def += ' NOT NULL'
					if default_value is not None:
						col_def += f' DEFAULT {default_value}'
					if single_pk and pk == 1:
						col_def += ' PRIMARY KEY'
					col_defs.append( col_def )
				new_create_sql = f'CREATE TABLE "{temp_table}" ({", ".join( col_defs )});'
				old_select = ", ".join( [ f'"{c}"' for c in cols ] )
				new_insert = ", ".join( [ f'"{c}"' for c in mapped_cols ] )
				conn.execute( "BEGIN" )
				conn.execute( new_create_sql )
				conn.execute( f'INSERT INTO "{temp_table}" ({new_insert}) SELECT {old_select} FROM "{table_name}";' )
				conn.execute( f'DROP TABLE "{table_name}";' )
				conn.execute( f'ALTER TABLE "{temp_table}" RENAME TO "{table_name}";' )
				for idx in indexes:
					idx_sql = idx[ 0 ]
					if idx_sql:
						idx_sql = idx_sql.replace( f'"{old_name}"', f'"{new_name}"' )
						conn.execute( idx_sql )
				conn.commit( )

		except Exception as e:
			exception = Error( e )
			exception.module = 'storage'
			exception.cause = 'SQLiteDatabase'
			exception.method = "rename_column( self, table_name: str, old_name: str, new_name: str ) -> None"
			Logger( ).write( exception )
			raise exception from e

create_profile_table

create_profile_table(table: str) -> DataFrame

Build column-level descriptive statistics for a table.

Purpose

Build column-level descriptive statistics for a table.

Parameters:

Name Type Description Default
table str

SQLite table name.

required

Returns:

Type Description
DataFrame

pd.DataFrame: Per-column statistics including null counts, distinct counts, and numeric summaries.

Source code in storage.py
def create_profile_table( self, table: str ) -> pd.DataFrame:
	"""Build column-level descriptive statistics for a table.

	Purpose:
		Build column-level descriptive statistics for a table.

	Args:
		table (str): SQLite table name.

	Returns:
		pd.DataFrame: Per-column statistics including null counts, distinct counts, and numeric summaries.
	"""
	try:
		throw_if( 'table', table )
		if not isinstance( table, str ):
			raise TypeError( 'table must be a string.' )
		self.table = table
		df = self.read_table( self.table )
		profile_rows = [ ]
		total_rows = len( df )
		for col in df.columns:
			series = df[ col ]
			null_count = series.isna( ).sum( )
			distinct_count = series.nunique( dropna=True )
			row = { 'column': col, 'dtype': str( series.dtype ),
				'null_%': round( (null_count / total_rows) * 100, 2 ) if total_rows else 0,
				'distinct_%': round( (
							                     distinct_count / total_rows) * 100, 2 ) if total_rows else 0, }
			if pd.api.types.is_numeric_dtype( series ):
				row[ 'min' ] = series.min( )
				row[ 'max' ] = series.max( )
				row[ 'mean' ] = series.mean( )
			else:
				row[ 'min' ] = None
				row[ 'max' ] = None
				row[ 'mean' ] = None
			profile_rows.append( row )
		return pd.DataFrame( profile_rows )

	except Exception as e:
		exception = Error( e )
		exception.module = 'storage'
		exception.cause = 'SQLiteDatabase'
		exception.method = "create_profile_table( self, table: str ) -> pd.DataFrame"
		Logger( ).write( exception )
		raise exception from e

drop_column

drop_column(table: str, column: str) -> None

Remove a column while rebuilding the table and restoring indexes.

Purpose

Remove a column while rebuilding the table and restoring indexes.

Parameters:

Name Type Description Default
table str

SQLite table name.

required
column str

SQLite column name.

required

Returns:

Name Type Description
None None

Rebuilds the table without the selected column and restores indexes.

Source code in storage.py
	def drop_column( self, table: str, column: str ) -> None:
		"""Remove a column while rebuilding the table and restoring indexes.

		Purpose:
			Remove a column while rebuilding the table and restoring indexes.

		Args:
			table (str): SQLite table name.
			column (str): SQLite column name.

		Returns:
			None: Rebuilds the table without the selected column and restores indexes.
		"""
		try:
			throw_if( 'table', table )
			throw_if( 'column', column )
			with self.create_connection( ) as conn:
				# ------------------------------------------------------------
				# Fetch original CREATE TABLE statement
				# ------------------------------------------------------------
				row = conn.execute( """
                                    SELECT sql
                                    FROM sqlite_master
                                    WHERE type ='table' AND name =?
				                    """, (table,) ).fetchone( )
				if not row or not row[ 0 ]:
					raise ValueError( 'Table definition not found.' )
				create_sql = row[ 0 ]
				# ------------------------------------------------------------
				# Extract column definitions
				# ------------------------------------------------------------
				open_paren = create_sql.find( "(" )
				close_paren = create_sql.rfind( ")" )
				if open_paren == -1 or close_paren == -1:
					raise ValueError( "Malformed CREATE TABLE statement." )
				inner = create_sql[ open_paren + 1: close_paren ]
				column_defs = [ c.strip( ) for c in inner.split( "," ) ]
				# Remove target column
				new_defs = [ ]
				for col_def in column_defs:
					col_name = col_def.split( )[ 0 ].strip( '"' )
					if col_name != column:
						new_defs.append( col_def )
				if len( new_defs ) == len( column_defs ):
					raise ValueError( "Column not found." )
				# ------------------------------------------------------------
				# Build new CREATE TABLE statement
				# ------------------------------------------------------------
				temp_table = f"{table}_rebuild_temp"
				new_create_sql = (f'CREATE TABLE "{temp_table}" (' + ", ".join( new_defs ) + ");")
				# ------------------------------------------------------------
				# Begin transaction
				# ------------------------------------------------------------
				conn.execute( "BEGIN" )
				conn.execute( new_create_sql )
				remaining_cols = [ c.split( )[ 0 ].strip( '"' ) for c in new_defs ]
				col_list = ", ".join( [ f'"{c}"' for c in remaining_cols ] )
				conn.execute( f'INSERT INTO "{temp_table}" ({col_list}) '
				              f'SELECT {col_list} FROM "{table}";' )
				# Preserve indexes
				indexes = conn.execute( """
                                        SELECT sql
                                        FROM sqlite_master
                                        WHERE type ='index' AND tbl_name=? AND sql IS NOT NULL
				                        """, (table,) ).fetchall( )
				conn.execute( f'DROP TABLE "{table}";' )
				conn.execute( f'ALTER TABLE "{temp_table}" RENAME TO "{table}";' )
				# Recreate indexes
				for idx in indexes:
					idx_sql = idx[ 0 ]
					if column not in idx_sql:
						conn.execute( idx_sql )
				conn.commit( )

		except Exception as e:
			exception = Error( e )
			exception.module = 'storage'
			exception.cause = 'SQLiteDatabase'
			exception.method = "drop_column( self, table: str, column: str ) -> None"
			Logger( ).write( exception )
			raise exception from e

rename_table

rename_table(old_name: str, new_name: str) -> None
    Rename an existing SQLite table. Attempts native ALTER TABLE rename first; if it fails,.
Purpose

Rename an existing SQLite table. Attempts native ALTER TABLE rename first; if it fails, falls back to a schema-safe rebuild using the original CREATE TABLE statement and preserves indexes.

Parameters:

Name Type Description Default
old_name str

Existing table name.

required
new_name str

New table name.

required

Returns:

Name Type Description
None None

Renames the selected SQLite table.

Source code in storage.py
	def rename_table( self, old_name: str, new_name: str ) -> None:
		"""
			Rename an existing SQLite table. Attempts native ALTER TABLE rename first; if it fails,.

		Purpose:
			Rename an existing SQLite table. Attempts native ALTER TABLE rename first; if it fails,
			falls back to a schema-safe rebuild using the original CREATE TABLE statement and
			preserves indexes.

		Args:
			old_name (str): Existing table name.

			new_name (str): New table name.

		Returns:
			None: Renames the selected SQLite table.
			"""
		try:
			throw_if( 'old_name', old_name )
			throw_if( 'new_name', new_name )
			with self.create_connection( ) as conn:
				try:
					conn.execute( f'ALTER TABLE "{old_name}" RENAME TO "{new_name}";' )
					conn.commit( )
					return
				except Exception:
					pass
				row = conn.execute( """
                                    SELECT sql
                                    FROM sqlite_master
                                    WHERE type ='table' AND name =?
				                    """, (old_name,) ).fetchone( )
				if not row or not row[ 0 ]:
					raise ValueError( "Table definition not found." )
				create_sql = row[ 0 ]
				indexes = conn.execute( """
                                        SELECT sql
                                        FROM sqlite_master
                                        WHERE type ='index' AND tbl_name=? AND sql IS NOT NULL
				                        """, (old_name,) ).fetchall( )
				open_paren = create_sql.find( "(" )
				if open_paren == -1:
					raise ValueError( "Malformed CREATE TABLE statement." )
				temp_name = f"{new_name}__rebuild_temp"
				conn.execute( "BEGIN" )
				conn.execute( f'CREATE TABLE "{temp_name}" {create_sql[ open_paren: ]}' )
				cols = [ r[ 1 ] for r in
					conn.execute( f'PRAGMA table_info("{old_name}");' ).fetchall( ) ]
				col_list = ", ".join( [ f'"{c}"' for c in cols ] )
				conn.execute( f'INSERT INTO "{temp_name}" ({col_list}) SELECT {col_list} FROM "{old_name}";' )
				conn.execute( f'DROP TABLE "{old_name}";' )
				conn.execute( f'ALTER TABLE "{temp_name}" RENAME TO "{new_name}";' )
				for idx in indexes:
					idx_sql = idx[ 0 ]
					if idx_sql:
						idx_sql = idx_sql.replace( f'ON "{old_name}"', f'ON "{new_name}"' )
						conn.execute( idx_sql )
				conn.commit( )

		except Exception as e:
			exception = Error( e )
			exception.module = 'storage'
			exception.cause = 'SQLiteDatabase'
			exception.method = "rename_table( self, old_name: str, new_name: str ) -> None"
			Logger( ).write( exception )
			raise exception from e