Wiki

Clone wiki

Wxen.NET DbHug / DbSettings

##DbHug ##DbSettings


DbHug gives you ability to use it with different Sql databases.
It is already has support for most popular databases, but you can create you own settings for any sql database. Or change default behavior.


###Creating To create own DbSettings you need to inherit it from DbSettings and it must have parametrless constructor.

  • ProviderNamespace property that contains namespace of your DbProviderFactory, for example for MsSql it is System.Data.SqlClient.
  • ParameterNamePrefix property is the prefix of DbParameter name. @ for MsSql, : for Oracle.
  • IsMultipleStatmentsAllowed property allows to run multiple Sql statments in a single command. Some databases like SqlCE does not support it.
  • SetupDbCommand event which is invoked on creation of DbCommand.
  • StringConcat(params string[]) method is the concatenation string like it will appear in the database, it takes string as a params. For MsSql - string.Join("+", strings);, Oracle - return string.Join("||", strings);.
  • PrepareInsertCommandToReturnId(InsertCommandArgument) method to prepare insert command to make it return inserted id. InsertCommandArgument contains needed stuff to do it. For MsSql it is argument.CommandBuilder.Append("; SELECT SCOPE_IDENTITY();");, for Oracle it is bit more complected :).
  • PrepareManyCommands virtual method to change DbCommand to execute multiple statements. For Oracle it adds BEGIN and END; to command text.
  • SetupDbParameter virtual method to change DbParameter before adding to executing DbCommand.
  • Insert virtual method to execute custom INSERT statement. For example for SqlCE it is needed to execute additional command to get inserted identifier.
    #!csharp
    public class MySettings: DbSettings
    {
        public MySettings()
        {
           ProviderNamespace = "MyCustomSqlClient";
           IsMultipleStatmentsAllowed = false;
        }
    
        protected internal override void PrepareInsertCommandToReturnId(InsertCommandArgument argument)
        {
            if (!string.IsNullOrEmpty(argument.KeyColumnName))
                argument.CommandBuilder.Append(string.Format(" RETURNING {0};", argument.KeyColumnName));
            else
                argument.CommandBuilder.Append(";");
        }
    
        protected internal override string StringConcat(params string[] strings)
        {
            return string.Join("||", strings);
        }
    }
    

And then assign it for DbHug if it was not assigned automatically. Automatically DbSettings assigned if it is already supported, or if DbProviderFactory namespace is equal to your settings ProviderNamespace property value.

#!csharp
dbHug.SetDbSettings<MySettings>();
To change the behavior inherit your settings from existing DbSettings (they lives in the Wxen.Data.DbHug.DBSetup namespace) and change needed parts.

####Caching ... Set model

Updated