using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
namespace Dapper
{
///
/// A SQL Compact specific implementation.
///
/// The type of database.
public abstract class SqlCompactDatabase : Database where TDatabase : Database, new()
{
///
/// A SQL Compact specific table, which handles the syntax correctly across operations.
///
/// The type in the table.
public class SqlCompactTable : Table
{
///
/// Creates a table for a SQL Compact database.
///
///
///
public SqlCompactTable(Database database, string likelyTableName)
: base(database, likelyTableName)
{
}
///
/// Insert a row into the db
///
/// Either DynamicParameters or an anonymous type or concrete type
///
public override int? Insert(dynamic data)
{
var o = (object)data;
List paramNames = GetParamNames(o);
paramNames.Remove("Id");
string cols = string.Join(",", paramNames);
string colsParams = string.Join(",", paramNames.Select(p => "@" + p));
var sql = "insert " + TableName + " (" + cols + ") values (" + colsParams + ")";
if (database.Execute(sql, o) != 1)
{
return null;
}
return (int)database.Query("SELECT @@IDENTITY AS LastInsertedId").Single();
}
}
///
/// Initializes the databases.
///
/// The connection to use.
/// The newly created database.
public static TDatabase Init(DbConnection connection)
{
var db = new TDatabase();
db.InitDatabase(connection, 0);
return db;
}
internal override Action CreateTableConstructorForTable()
{
return CreateTableConstructor(typeof(SqlCompactTable<>));
}
}
}