C# Class SQLiteConversionEngine.Converters.BaseConverter

This class is resposible to take a single SQL Server database and convert it to an SQLite database file.
The class knows how to convert table and index structures only.
Datei anzeigen Open project: johnmbaughman/SQLiteConverter

Protected Properties

Property Type Description
_cancelled bool
_defaultValueRx System.Text.RegularExpressions.Regex
_isActive bool
_keyRx System.Text.RegularExpressions.Regex

Public Methods

Method Description
CancelConversion ( ) : void

Cancels the conversion.

ConvertToDatabase ( SqlConnection sqlConnection, SQLiteConnection sqliteConnection, SqlConversionHandler sqlConversionHandler, SqlTableSelectionHandler sqlTableSelectionHandler, FailedViewDefinitionHandler failedViewDefinitionHandler, bool createTriggers ) : void

This method takes as input the connection to an SQL Server database and creates a corresponding SQLite database file with a schema derived from the SQL Server database.

The method continues asynchronously in the background and the caller returned immediatly.

ConvertToDatabase ( string sqlServerConnectionString, SQLiteConnection sqliteConnection, SqlConversionHandler sqlConversionHandler, SqlTableSelectionHandler sqlTableSelectionHandler, FailedViewDefinitionHandler failedViewDefinitionHandler, bool createTriggers ) : void

This method takes as input the connection string to an SQL Server database and creates a corresponding SQLite database file with a schema derived from the SQL Server database.

The method continues asynchronously in the background and the caller returned immediatly.

ConvertToDatabase ( string sqlServerConnectionString, string sqliteConnectionString, SqlConversionHandler sqlConversionHandler, SqlTableSelectionHandler sqlTableSelectionHandler, FailedViewDefinitionHandler failedViewDefinitionHandler, bool createTriggers ) : void

This method takes as input the connection string to an SQL Server database and creates a corresponding SQLite database file with a schema derived from the SQL Server database.

The method continues asynchronously in the background and the caller returned immediatly.

InitializeSQLiteConnection ( string sqliteConnectionString ) : SQLiteConnection
InitializeSQLiteDatabase ( SQLiteConnection sqliteConnection, List PragmaCommands ) : void
InitializeSqlServerConnection ( string sqlServerConnectionString ) : SqlConnection
InitializeSqlServerDatabase ( SqlConnection sqlConnection ) : void

Protected Methods

Method Description
AddTriggersForForeignKeys ( SQLiteConnection sqliteConnection, IEnumerable schema, SqlConversionHandler handler ) : void
AddTriggersForForeignKeys ( string sqlitePath, IEnumerable schema, string password, SqlConversionHandler handler ) : void
AdjustDefaultValue ( string val ) : string

More adjustments for the DEFAULT value clause.

BuildIndexSchema ( string indexName, string desc, string keys ) : IndexSchema

Builds an index schema object from the specified components (Read from SQL Server).

BuildSQLiteInsert ( TableSchema ts ) : SQLiteCommand

Creates a command object needed to insert values into a specific SQLite table.

BuildSQLiteUpdate ( TableSchema ts ) : SQLiteCommand

Creates a command object needed to insert values into a specific SQLite table.

BuildSourceTableQuery ( TableSchema tableSchema ) : string

Builds a SELECT query for a specific table. Needed in the process of copying rows from the SQL Server database to the SQLite database.

BuildSqlServerInsert ( TableSchema ts ) : SqlCommand

Creates a command object needed to insert values into a specific SQLite table.

CastValueForColumn ( object val, ColumnSchema columnSchema ) : object

Used in order to adjust the value received from SQL Servr for the SQLite database.

CheckCancelled ( ) : void

Convenience method for checking if the conversion progress needs to be cancelled.

ConvertSourceDatabaseToDestination ( SqlConnection sqlConnection, SQLiteConnection sqliteConnection, SqlConversionHandler sqlConversionHandler, SqlTableSelectionHandler sqlTableSelectionHandler, FailedViewDefinitionHandler failedViewDefinitionHandler, bool createTriggers ) : void

Do the entire process of first reading the SQL Server schema, creating a corresponding SQLite schema, and copying all rows from the SQL Server database to the SQLite database.

CopySourceDatabaseRowsToDestination ( SqlConnection sqlConnection, SQLiteConnection sqliteConnection, List schema, SqlConversionHandler handler ) : void

Copies table rows from the SQL Server database to the SQLite database.

CreateForeignKeySchema ( IDbConnection sourceConnection, TableSchema tableSchema ) : void

Add foreign key schema object from the specified components (Read from SQL Server).

CreateSQLiteConnectionString ( string sqlitePath, string password ) : string

Creates SQLite connection string from the specified DB file path.

CreateSQLiteDatabase ( SQLiteConnection sqliteConnection, DatabaseSchema schema, SqlConversionHandler handler, FailedViewDefinitionHandler viewFailureHandler ) : void

Creates the SQLite database.

CreateSQLiteDatabase ( string sqlitePath, DatabaseSchema schema, string password, SqlConversionHandler handler, FailedViewDefinitionHandler viewFailureHandler ) : void

Creates the SQLite database from the schema read from the SQL Server.

CreateSqlServerConnectionString ( string sqlitePath, string password ) : string

Creates SQLite connection string from the specified DB file path.

CreateSqlServerDatabase ( SQLiteConnection sqliteConnection, DatabaseSchema schema, SqlConversionHandler handler, FailedViewDefinitionHandler viewFailureHandler ) : void

Creates the SQLite database.

CreateSqlServerDatabase ( string sqlitePath, DatabaseSchema schema, string password, SqlConversionHandler handler, FailedViewDefinitionHandler viewFailureHandler ) : void

Creates the SQLite database from the schema read from the SQL Server.

CreateTableSchema ( IDbConnection sourceConnection, string tableName, string tableSchemaName ) : TableSchema

Creates a TableSchema object using the specified SQL Server connection and the name of the table for which we need to create the schema.

FixDefaultValueString ( string colDefault ) : string

Does some necessary adjustments to a value string that appears in a column DEFAULT clause.

GetDbTypeOfColumn ( ColumnSchema columnSchema ) : DbType

Matches SQL Server types to general DB types

GetNormalizedName ( string str, List names ) : string

Used in order to avoid breaking naming rules (e.g., when a table has a name in SQL Server that cannot be used as a basis for a matching index name in SQLite).

ParseBlobAsGuid ( byte blob ) : System.Guid
ParseStringAsGuid ( string str ) : System.Guid
ReadSourceSchema ( SqlConnection sqlConnection, SqlConversionHandler sqlConversionHandler, SqlTableSelectionHandler sqlTableSelectionHandler ) : DatabaseSchema

Reads the entire SQL Server DB schema using the specified connection string.

ValidateDataType ( string dataType ) : void

Small validation method to make sure we don't miss anything without getting an exception.

WriteTriggerSchema ( TriggerSchema ts ) : string

Gets a create script for the triggerSchema in SQLite syntax

Private Methods

Method Description
AddSQLiteTable ( SQLiteConnection conn, TableSchema dt ) : void

Creates the CREATE TABLE DDL for SQLite and a specific table.

AddSQLiteView ( SQLiteConnection conn, ViewSchema vs, FailedViewDefinitionHandler handler ) : void
AddTableTriggers ( SQLiteConnection conn, TableSchema dt ) : void
BuildColumnStatement ( ColumnSchema col, TableSchema ts, bool &pkey ) : string

Used when creating the CREATE TABLE DDL. Creates a single row for the specified column.

BuildCreateIndex ( string tableName, IndexSchema indexSchema ) : string

Creates a CREATE INDEX DDL for the specified table and index schema.

BuildCreateTableQuery ( TableSchema ts ) : string

returns the CREATE TABLE DDL for creating the SQLite table from the specified table schema object.

DiscardNational ( string value ) : string

Discards the national prefix if exists (e.g., N'sometext') which is not supported in SQLite.

IsSingleQuoted ( string value ) : bool
IsValidDefaultValue ( string value ) : bool

Check if the DEFAULT clause is valid by SQLite standards

StripParens ( string value ) : string

Strip any parentheses from the string.

Method Details

AddTriggersForForeignKeys() protected static method

protected static AddTriggersForForeignKeys ( SQLiteConnection sqliteConnection, IEnumerable schema, SqlConversionHandler handler ) : void
sqliteConnection System.Data.SQLite.SQLiteConnection
schema IEnumerable
handler SqlConversionHandler
return void

AddTriggersForForeignKeys() protected static method

protected static AddTriggersForForeignKeys ( string sqlitePath, IEnumerable schema, string password, SqlConversionHandler handler ) : void
sqlitePath string
schema IEnumerable
password string
handler SqlConversionHandler
return void

AdjustDefaultValue() protected static method

More adjustments for the DEFAULT value clause.
protected static AdjustDefaultValue ( string val ) : string
val string The value to adjust
return string

BuildIndexSchema() protected static method

Builds an index schema object from the specified components (Read from SQL Server).
protected static BuildIndexSchema ( string indexName, string desc, string keys ) : IndexSchema
indexName string The name of the index
desc string The description of the index
keys string Key columns that are part of the index.
return SQLiteConversionEngine.ConversionData.IndexSchema

BuildSQLiteInsert() protected static method

Creates a command object needed to insert values into a specific SQLite table.
protected static BuildSQLiteInsert ( TableSchema ts ) : SQLiteCommand
ts SQLiteConversionEngine.ConversionData.TableSchema The table schema object for the table.
return System.Data.SQLite.SQLiteCommand

BuildSQLiteUpdate() protected static method

Creates a command object needed to insert values into a specific SQLite table.
protected static BuildSQLiteUpdate ( TableSchema ts ) : SQLiteCommand
ts SQLiteConversionEngine.ConversionData.TableSchema The table schema object for the table.
return System.Data.SQLite.SQLiteCommand

BuildSourceTableQuery() protected abstract method

Builds a SELECT query for a specific table. Needed in the process of copying rows from the SQL Server database to the SQLite database.
protected abstract BuildSourceTableQuery ( TableSchema tableSchema ) : string
tableSchema SQLiteConversionEngine.ConversionData.TableSchema
return string

BuildSqlServerInsert() protected static method

Creates a command object needed to insert values into a specific SQLite table.
protected static BuildSqlServerInsert ( TableSchema ts ) : SqlCommand
ts SQLiteConversionEngine.ConversionData.TableSchema The table schema object for the table.
return System.Data.SqlClient.SqlCommand

CancelConversion() public method

Cancels the conversion.
public CancelConversion ( ) : void
return void

CastValueForColumn() protected static method

Used in order to adjust the value received from SQL Servr for the SQLite database.
protected static CastValueForColumn ( object val, ColumnSchema columnSchema ) : object
val object The value object
columnSchema ColumnSchema The corresponding column schema
return object

CheckCancelled() protected static method

Convenience method for checking if the conversion progress needs to be cancelled.
protected static CheckCancelled ( ) : void
return void

ConvertSourceDatabaseToDestination() protected abstract method

Do the entire process of first reading the SQL Server schema, creating a corresponding SQLite schema, and copying all rows from the SQL Server database to the SQLite database.
protected abstract ConvertSourceDatabaseToDestination ( SqlConnection sqlConnection, SQLiteConnection sqliteConnection, SqlConversionHandler sqlConversionHandler, SqlTableSelectionHandler sqlTableSelectionHandler, FailedViewDefinitionHandler failedViewDefinitionHandler, bool createTriggers ) : void
sqlConnection System.Data.SqlClient.SqlConnection The SQL connection.
sqliteConnection System.Data.SQLite.SQLiteConnection The SQLite connection.
sqlConversionHandler SqlConversionHandler The SQL conversion handler.
sqlTableSelectionHandler SqlTableSelectionHandler The SQL table selection handler.
failedViewDefinitionHandler FailedViewDefinitionHandler The failed view definition handler.
createTriggers bool if set to true [create triggers].
return void

ConvertToDatabase() public abstract method

This method takes as input the connection to an SQL Server database and creates a corresponding SQLite database file with a schema derived from the SQL Server database.
The method continues asynchronously in the background and the caller returned immediatly.
public abstract ConvertToDatabase ( SqlConnection sqlConnection, SQLiteConnection sqliteConnection, SqlConversionHandler sqlConversionHandler, SqlTableSelectionHandler sqlTableSelectionHandler, FailedViewDefinitionHandler failedViewDefinitionHandler, bool createTriggers ) : void
sqlConnection System.Data.SqlClient.SqlConnection The SQL connection.
sqliteConnection System.Data.SQLite.SQLiteConnection The SQLite connection.
sqlConversionHandler SqlConversionHandler The SQL conversion handler.
sqlTableSelectionHandler SqlTableSelectionHandler The SQL table selection handler.
failedViewDefinitionHandler FailedViewDefinitionHandler The failed view definition handler.
createTriggers bool if set to true [create triggers].
return void

ConvertToDatabase() public abstract method

This method takes as input the connection string to an SQL Server database and creates a corresponding SQLite database file with a schema derived from the SQL Server database.
The method continues asynchronously in the background and the caller returned immediatly.
public abstract ConvertToDatabase ( string sqlServerConnectionString, SQLiteConnection sqliteConnection, SqlConversionHandler sqlConversionHandler, SqlTableSelectionHandler sqlTableSelectionHandler, FailedViewDefinitionHandler failedViewDefinitionHandler, bool createTriggers ) : void
sqlServerConnectionString string The connection string to the SQL Server database.
sqliteConnection System.Data.SQLite.SQLiteConnection The path to the SQLite database file that needs to get created.
sqlConversionHandler SqlConversionHandler The SQL conversion handler.
sqlTableSelectionHandler SqlTableSelectionHandler The SQL table selection handler.
failedViewDefinitionHandler FailedViewDefinitionHandler The failed view definition handler.
createTriggers bool if set to true [create triggers].
return void

ConvertToDatabase() public abstract method

This method takes as input the connection string to an SQL Server database and creates a corresponding SQLite database file with a schema derived from the SQL Server database.
The method continues asynchronously in the background and the caller returned immediatly.
public abstract ConvertToDatabase ( string sqlServerConnectionString, string sqliteConnectionString, SqlConversionHandler sqlConversionHandler, SqlTableSelectionHandler sqlTableSelectionHandler, FailedViewDefinitionHandler failedViewDefinitionHandler, bool createTriggers ) : void
sqlServerConnectionString string The connection string to the SQL Server database.
sqliteConnectionString string The connection string to the SQLite database.
sqlConversionHandler SqlConversionHandler The SQL conversion handler.
sqlTableSelectionHandler SqlTableSelectionHandler
failedViewDefinitionHandler FailedViewDefinitionHandler The failed view definition handler.
createTriggers bool if set to true [create triggers].
return void

CopySourceDatabaseRowsToDestination() protected abstract method

Copies table rows from the SQL Server database to the SQLite database.
protected abstract CopySourceDatabaseRowsToDestination ( SqlConnection sqlConnection, SQLiteConnection sqliteConnection, List schema, SqlConversionHandler handler ) : void
sqlConnection System.Data.SqlClient.SqlConnection The SQL connection.
sqliteConnection System.Data.SQLite.SQLiteConnection The SQLite connection.
schema List The schema of the SQL Server database.
handler SqlConversionHandler A handler to handle progress notifications.
return void

CreateForeignKeySchema() protected abstract method

Add foreign key schema object from the specified components (Read from SQL Server).
protected abstract CreateForeignKeySchema ( IDbConnection sourceConnection, TableSchema tableSchema ) : void
sourceConnection IDbConnection
tableSchema SQLiteConversionEngine.ConversionData.TableSchema
return void

CreateSQLiteConnectionString() protected static method

Creates SQLite connection string from the specified DB file path.
protected static CreateSQLiteConnectionString ( string sqlitePath, string password ) : string
sqlitePath string The path to the SQLite database file.
password string
return string

CreateSQLiteDatabase() protected static method

Creates the SQLite database.
protected static CreateSQLiteDatabase ( SQLiteConnection sqliteConnection, DatabaseSchema schema, SqlConversionHandler handler, FailedViewDefinitionHandler viewFailureHandler ) : void
sqliteConnection System.Data.SQLite.SQLiteConnection The SQLite connection.
schema DatabaseSchema The schema.
handler SqlConversionHandler The handler.
viewFailureHandler FailedViewDefinitionHandler The view failure handler.
return void

CreateSQLiteDatabase() protected static method

Creates the SQLite database from the schema read from the SQL Server.
protected static CreateSQLiteDatabase ( string sqlitePath, DatabaseSchema schema, string password, SqlConversionHandler handler, FailedViewDefinitionHandler viewFailureHandler ) : void
sqlitePath string The path to the generated DB file.
schema DatabaseSchema The schema of the SQL server database.
password string The password to use for encrypting the DB or null if non is needed.
handler SqlConversionHandler A handle for progress notifications.
viewFailureHandler FailedViewDefinitionHandler
return void

CreateSqlServerConnectionString() protected static method

Creates SQLite connection string from the specified DB file path.
protected static CreateSqlServerConnectionString ( string sqlitePath, string password ) : string
sqlitePath string The path to the SQLite database file.
password string
return string

CreateSqlServerDatabase() protected static method

Creates the SQLite database.
protected static CreateSqlServerDatabase ( SQLiteConnection sqliteConnection, DatabaseSchema schema, SqlConversionHandler handler, FailedViewDefinitionHandler viewFailureHandler ) : void
sqliteConnection System.Data.SQLite.SQLiteConnection The SQLite connection.
schema DatabaseSchema The schema.
handler SqlConversionHandler The handler.
viewFailureHandler FailedViewDefinitionHandler The view failure handler.
return void

CreateSqlServerDatabase() protected static method

Creates the SQLite database from the schema read from the SQL Server.
protected static CreateSqlServerDatabase ( string sqlitePath, DatabaseSchema schema, string password, SqlConversionHandler handler, FailedViewDefinitionHandler viewFailureHandler ) : void
sqlitePath string The path to the generated DB file.
schema DatabaseSchema The schema of the SQL server database.
password string The password to use for encrypting the DB or null if non is needed.
handler SqlConversionHandler A handle for progress notifications.
viewFailureHandler FailedViewDefinitionHandler
return void

CreateTableSchema() protected abstract method

Creates a TableSchema object using the specified SQL Server connection and the name of the table for which we need to create the schema.
protected abstract CreateTableSchema ( IDbConnection sourceConnection, string tableName, string tableSchemaName ) : TableSchema
sourceConnection IDbConnection
tableName string The name of the table for which we wants to create the table schema.
tableSchemaName string
return SQLiteConversionEngine.ConversionData.TableSchema

FixDefaultValueString() protected static method

Does some necessary adjustments to a value string that appears in a column DEFAULT clause.
protected static FixDefaultValueString ( string colDefault ) : string
colDefault string The original default value string (as read from SQL Server).
return string

GetDbTypeOfColumn() protected static method

Matches SQL Server types to general DB types
protected static GetDbTypeOfColumn ( ColumnSchema columnSchema ) : DbType
columnSchema ColumnSchema The column schema to use for the match
return DbType

GetNormalizedName() protected static method

Used in order to avoid breaking naming rules (e.g., when a table has a name in SQL Server that cannot be used as a basis for a matching index name in SQLite).
protected static GetNormalizedName ( string str, List names ) : string
str string The name to change if necessary
names List Used to avoid duplicate names
return string

InitializeSQLiteConnection() public method

public InitializeSQLiteConnection ( string sqliteConnectionString ) : SQLiteConnection
sqliteConnectionString string
return System.Data.SQLite.SQLiteConnection

InitializeSQLiteDatabase() public method

public InitializeSQLiteDatabase ( SQLiteConnection sqliteConnection, List PragmaCommands ) : void
sqliteConnection System.Data.SQLite.SQLiteConnection
PragmaCommands List
return void

InitializeSqlServerConnection() public method

public InitializeSqlServerConnection ( string sqlServerConnectionString ) : SqlConnection
sqlServerConnectionString string
return System.Data.SqlClient.SqlConnection

InitializeSqlServerDatabase() public method

public InitializeSqlServerDatabase ( SqlConnection sqlConnection ) : void
sqlConnection System.Data.SqlClient.SqlConnection
return void

ParseBlobAsGuid() protected static method

protected static ParseBlobAsGuid ( byte blob ) : System.Guid
blob byte
return System.Guid

ParseStringAsGuid() protected static method

protected static ParseStringAsGuid ( string str ) : System.Guid
str string
return System.Guid

ReadSourceSchema() protected abstract method

Reads the entire SQL Server DB schema using the specified connection string.
protected abstract ReadSourceSchema ( SqlConnection sqlConnection, SqlConversionHandler sqlConversionHandler, SqlTableSelectionHandler sqlTableSelectionHandler ) : DatabaseSchema
sqlConnection System.Data.SqlClient.SqlConnection The SQL connection.
sqlConversionHandler SqlConversionHandler The SQL conversion handler.
sqlTableSelectionHandler SqlTableSelectionHandler The SQL table selection handler.
return DatabaseSchema

ValidateDataType() protected static method

Small validation method to make sure we don't miss anything without getting an exception.
protected static ValidateDataType ( string dataType ) : void
dataType string The datatype to validate.
return void

WriteTriggerSchema() protected static method

Gets a create script for the triggerSchema in SQLite syntax
protected static WriteTriggerSchema ( TriggerSchema ts ) : string
ts TriggerSchema Trigger to script
return string

Property Details

_cancelled protected_oe static_oe property

protected static bool _cancelled
return bool

_defaultValueRx protected_oe static_oe property

protected static Regex,System.Text.RegularExpressions _defaultValueRx
return System.Text.RegularExpressions.Regex

_isActive protected_oe static_oe property

protected static bool _isActive
return bool

_keyRx protected_oe static_oe property

protected static Regex,System.Text.RegularExpressions _keyRx
return System.Text.RegularExpressions.Regex