Class NpgsqlCodeGenerator

Namespace
DbExport.Providers.Npgsql
Assembly
DbExport.Api.dll

Provides functionality for generating database-specific code targeting Npgsql (PostgreSQL). This class extends the CodeGenerator base class and overrides certain methods to tailor code generation to the PostgreSQL database platform.

public class NpgsqlCodeGenerator : CodeGenerator, IVisitor, IDisposable
Inheritance
NpgsqlCodeGenerator
Implements
Inherited Members

Constructors

NpgsqlCodeGenerator()

Initializes a new instance of the NpgsqlCodeGenerator class.

public NpgsqlCodeGenerator()

NpgsqlCodeGenerator(TextWriter)

Initializes a new instance of the NpgsqlCodeGenerator class with the specified TextWriter for output.

public NpgsqlCodeGenerator(TextWriter output)

Parameters

output TextWriter

The TextWriter to which the generated SQL will be written. Must not be null.

NpgsqlCodeGenerator(string)

Initializes a new instance of the NpgsqlCodeGenerator class that writes output to a file at the specified path.

public NpgsqlCodeGenerator(string path)

Parameters

path string

The file path where the generated SQL will be written. Must not be null or empty.

Properties

ProviderName

Gets the name of the database provider for which this code generator is designed to generate SQL scripts.

public override string ProviderName { get; }

Property Value

string

Methods

Format(object, ColumnType)

Formats a value for inclusion in a SQL statement, taking into account the value's type and the corresponding column type.

protected override string Format(object value, ColumnType columnType)

Parameters

value object

The value to be formatted for inclusion in a SQL statement. This could be a default value for a column, a value being inserted into a table, or any other value that needs to be represented as a literal in the generated SQL. The method will determine how to format this value based on its type and the specified column type, ensuring that it is correctly represented in the SQL syntax for the target database provider.

columnType ColumnType

The column type that corresponds to the value being formatted. This information is used to determine how to format the value, such as whether to quote it, how to format dates and times, how to represent binary data, etc., according to the conventions of the target database provider.

Returns

string

A string representing the formatted value, ready to be included as a literal in a SQL statement, formatted according to the conventions of the target database provider.

GetKeyName(Key)

Gets the name to be used for a key (such as an index or foreign key constraint) in the generated SQL.

protected override string GetKeyName(Key key)

Parameters

key Key

The key for which to get the name. This method is called when generating SQL for indexes and foreign key constraints, and it determines how to name these keys in the generated SQL. The default implementation returns the escaped name of the key, but derived classes can override this method to provide different naming conventions or to include additional information in the key name as needed by the target database provider.

Returns

string

A string representing the name to be used for the key in the generated SQL, formatted according to the conventions of the target database provider.

GetTypeName(Column)

Gets the SQL type name for the given column, taking into account the column's data type and any provider-specific type mappings.

protected override string GetTypeName(Column column)

Parameters

column Column

The column for which to determine the SQL type name. The method will consider the column's ColumnType and, if it is a user-defined type, its associated DataType to determine the appropriate SQL type name.

Returns

string

A string representing the SQL type name for the column, formatted according to the conventions of the target database provider.

GetTypeName(IDataItem)

Gets the SQL type name for the given data item, which could be a column or a user-defined data type.

protected override string GetTypeName(IDataItem item)

Parameters

item IDataItem

The data item for which to determine the SQL type name. This could be a column or a user-defined data type. The method will use the properties of the data item, such as ColumnType, Size, Precision, and Scale, to determine the appropriate SQL type name, potentially using provider-specific type mappings and formatting rules.

Returns

string

A string representing the SQL type name for the data item, formatted according to the conventions of the target database provider.

GetTypeReference(DataType)

Gets the SQL type reference for a user-defined data type, which may involve referencing the data type by name or using a specific syntax depending on the target database provider.

protected override string GetTypeReference(DataType dataType)

Parameters

dataType DataType

The user-defined data type for which to get the SQL type reference. This method is called when a column has a ColumnType of UserDefined, and the column's DataType property is not null. The method will determine how to reference this user-defined data type in the generated SQL, which may involve using the data type's name or a specific syntax depending on the conventions of the target database provider.

Returns

string

A string representing the SQL type reference for the user-defined data type, formatted according to the conventions of the target database provider.

VisitDataType(DataType)

Visits a DataType object, allowing the visitor to perform operations on the data type schema.

public override void VisitDataType(DataType dataType)

Parameters

dataType DataType

The DataType object to be visited.

WriteDbCreationDirective(Database)

Writes a CREATE DATABASE statement for the given database, according to the syntax rules of the target database provider.

protected override void WriteDbCreationDirective(Database database)

Parameters

database Database

The database for which to write the CREATE DATABASE statement. This method will generate the appropriate SQL to create the database, including the database name and any necessary syntax according to the conventions of the target database provider. This method is called if the code generator supports database creation and if the export options indicate that the schema should be exported.

WriteIdentitySpecification(Column)

Writes the identity specification for the specified column.

protected override void WriteIdentitySpecification(Column column)

Parameters

column Column

The column for which the identity specification is being written.

Remarks

PostgreSQL identifies columns with IDENTITY by their type name (smallserial, serial, bigserial). So this overload actually does nothing.