Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oracle provider does not return auto-incremented primary key values #270

Open
stackwave opened this issue Jul 13, 2011 · 0 comments
Open
Labels

Comments

@stackwave
Copy link

The SubSonic providers for SQLite and SQLServer appear to use those databases' special syntax to retrieve auto-incremented primary key values. Oracle does not support auto-incrementing columns; one must explicitly create a sequence object, then use a database trigger to pull a value from the sequence on each insert into the target table. However, this causes a problem in the SimpleRepository if one expects to get a value back from the database after a call to Add, because no such value will be returned.

This is probably best solved heuristically. If a numeric primary key exists for a table, use Oracle's "RETURNING INTO" syntax to retrieve the primary key value in an output parameter.

I've hacked up the OracleGenerator, DbDataProvider, and SimpleRepository to get things working for myself, but I'm sure there's a more elegant way to do it. I've currently got the following at the end of the OracleGenerator's BuildInsertStatement:

sb.AppendFormat(" RETURNING {0}.{1} INTO :row_id", i.Table.Name, i.Table.PrimaryKey.Name);
sb.AppendLine();

Then, in DbDataProvider, I added a new OracleInsertParameter property. If I detect a parameter named ":row_id" just before that method returns the reader, I assign that parameter to the new property.

if (cmd.Parameters.Contains(":row_id"))
{
    OracleInsertParameter = cmd.Parameters[":row_id"];
}

Then in the SimpleRepository.Add method I tweaked the using statement as follows:

using(var rdr = item.ToInsertQuery(_provider).ExecuteReader())
{
    if (rdr.Read())
    {
        result = rdr[0];
    }
    else
    {
        if (_provider is OracleDataProvider)
        {
            return (_provider as OracleDataProvider).OracleInsertParameter.Value;
        }
    }
}

It's ugly, but it did the trick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant