henkmollema / Dommel

CRUD operations with Dapper made simple.
MIT License
634 stars 100 forks source link

Sqlite Insert is wrong please use the ; #19

Closed VGib closed 9 years ago

VGib commented 9 years ago

Hello , i tested your library which seems to become prety good. Your Sqlite code is wrong. pkease fill free to use mibne (MySqliteBuilderà) instead. You only need to set a semicolon between the insert and select.

I will had my test project, hope it will be hepfull.

public  class MySqliteSqlBuilder : Dommel.DommelMapper.ISqlBuilder
    {
        public string BuildInsert(string tableName, string[] columnNames, string[] paramNames, PropertyInfo keyProperty)
        {
            return string.Format("insert into {0} ({1}) values ({2}); select last_insert_rowid() id",
                tableName,
                string.Join(", ", columnNames),
                string.Join(", ", paramNames));
        }
    }

Regards

VGib commented 9 years ago

Here is my code (I don't know how to add project, feel free to ask me my project )

using System;
using Dapper.FluentMap.Mapping;
using Dapper.FluentMap.Dommel;

using System.IO;
using Dapper;
using Dommel;
using Dapper.FluentMap.Dommel.Mapping;
using Mono.Data.Sqlite;
using System.Reflection;

namespace DommelSample
{
    public class User
    {
        public int Id { get ; set; }
        public string Name { get; set; }
        public int Counter { get; set; }
    }

    public class UserMap : DommelEntityMap<User>
    {
        public UserMap()
        {
            ToTable ("user");
            Map (x => x.Id).ToColumn ("id").IsKey ();
            Map (x => x.Name).ToColumn ("name");
            Map (x => x.Counter).ToColumn ("counter");
        }
    }

    public  class MySqliteSqlBuilder : Dommel.DommelMapper.ISqlBuilder
    {
        public string BuildInsert(string tableName, string[] columnNames, string[] paramNames, PropertyInfo keyProperty)
        {
            return string.Format("insert into {0} ({1}) values ({2}); select last_insert_rowid() id",
                tableName,
                string.Join(", ", columnNames),
                string.Join(", ", paramNames));
        }
    }

    public class MainClass
    {
        static void CreateDatabaseTables (SqliteConnection connection)
        {
            connection.Execute ("create table user (id integer primary key autoincrement,name text, counter integer)");
        }

        public static void Main (string[] args)
        {
            Dapper.FluentMap.FluentMapper.Intialize (config => {
                config.AddMap(new UserMap());
                config.ForDommel();         
            }
            );
            DommelMapper.AddSqlBuilder (typeof(SqliteConnection), new MySqliteSqlBuilder());
            const string tmptestsqlite = "/tmp/test.sqlite";
            if (File.Exists (tmptestsqlite))
                File.Delete (tmptestsqlite);
            using (var connection = new SqliteConnection ("Data Source=/tmp/test.sqlite")) {

                CreateDatabaseTables (connection);

                connection.Insert (new User () { Name = "toto" });
                connection.Insert (new User () { Name = "tata" });
                connection.Insert (new User () { Name = "titi" });

                foreach (var test in connection.GetAll<User>()) {
                    test.Counter = 10;
                    connection.Update<User> (test);
                }

            }
        }
    }
}
henkmollema commented 9 years ago

Hi, thanks for this. I'll change this ASAP. Next time you could issue a pull-request if you'd like.

henkmollema commented 9 years ago

I deployed v1.3.1 to NuGet.