Ejemplo n.º 1
0
 public static void SetSnakeToCamelCase <T>()
 {
     SqlMapper.SetTypeMap(typeof(T), new CustomPropertyTypeMap(typeof(T), (type, name) =>
     {
         var propName = NamingConverter.SnakeToCamel(name);
         return(type.GetProperty(propName));
     }));
 }
Ejemplo n.º 2
0
        public static void CreateTable <T>(SQLiteConnection conn, bool clean = true)
        {
            var tableName = NamingConverter.CamelToSnake(typeof(T).Name) + "s";

            if (clean)
            {
                conn.Execute($"DROP TABLE IF EXISTS `{tableName}`");
            }

            var columnDefs = new List <string>();

            foreach (var prop in typeof(T).GetProperties())
            {
                var name = NamingConverter.CamelToSnake(prop.Name);

                var typeStr = default(string);
                if (prop.PropertyType == typeof(int))
                {
                    typeStr = "INTEGER";
                }
                else if (prop.PropertyType == typeof(string))
                {
                    typeStr = "TEXT";
                }
                if (typeStr == default(string))
                {
                    continue;
                }

                var columnDef = $"`{name}` {typeStr}";
                if (name == "id")
                {
                    columnDef += " PRIMARY KEY AUTOINCREMENT";
                }

                columnDefs.Add(columnDef);
            }

            conn.Execute($"CREATE TABLE `{tableName}` ({string.Join(",", columnDefs)})");
        }