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

SqliteCommandBuilder should override InsertCommand for autoincrement #66

Closed
greg9504 opened this issue Apr 3, 2016 · 1 comment
Closed

Comments

@greg9504
Copy link

greg9504 commented Apr 3, 2016

Insert command should do select last_insert_rowid() to get the autoinc key value. See below, also included code for paging, but have not tested it yet.

using System.Data;
using Griffin.Data.Mapper;
using Griffin.Data.Mapper.CommandBuilders;
using System.Linq;

namespace Griffin.Data.Sqlite
{
    /// <summary>
    /// Sqlite specific command builder.
    /// </summary>
    public class SqliteCommandBuilder : CommandBuilder
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SqliteCommandBuilder"/> class.
        /// </summary>
        /// <param name="mapper">The mapper.</param>
        public SqliteCommandBuilder(ICrudEntityMapper mapper) : base(mapper)
        {
        }

        /// <summary>
        /// Generate an insert command, should end with a command that returns the insert identity.
        /// </summary>
        /// <param name="command">Command to add the query to</param>
        /// <param name="entity">Entity to store</param>
        /// <remarks>
        /// Last statement will return @@identity. 
        /// </remarks>
        public override void InsertCommand(IDbCommand command, object entity)
        {
            base.InsertCommand(command, entity);

            var gotAutoIncrement = Mapper.Properties.Any(x => x.Value.IsAutoIncrement && x.Value.IsPrimaryKey);
            if (!gotAutoIncrement)
                return;

            command.CommandText += ";select last_insert_rowid()";
        }

        /// <summary>
        /// Truncate all rows in a table
        /// </summary>
        /// <param name="command">Command that will be executed after this method call</param>
        /// <remarks>
        /// Will do a DELETE statement and reset the identity sequence generator.
        /// </remarks>
        public override void TruncateCommand(IDbCommand command)
        {
            command.CommandText = string.Format("DELETE FROM {0};DELETE FROM SQLITE_SEQUENCE WHERE name='{0}';",
                TableName);
        }

        /// <summary>
        /// Uses the SQLite - LIMIT [pageSize] OFFSET [pageNumber]
        /// LIMIT [no of rows] OFFSET [row num]
        /// </summary>
        /// <param name="command">command to modify</param>
        /// <param name="pageNumber">One based index</param>
        /// <param name="pageSize">Items per page.</param>
        public override void Paging(IDbCommand command, int pageNumber, int pageSize)
        {
            command.CommandText += string.Format(" LIMIT {1} OFFSET {0}",
                (pageNumber - 1) * pageSize, pageSize);
        }
    }
}
@jgauffin
Copy link
Owner

jgauffin commented Apr 6, 2016

Will be added in the next commit.

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

No branches or pull requests

2 participants