Beispiel #1
0
        internal static string GetUpdate(this object instance)
        {
            var result = new StringBuilder();

            var type = instance.GetType();

            var tableName = AttributeDiscovery.GetTableName(type);

            result.AppendLine($"UPDATE {tableName}");
            result.AppendLine("SET");

            var columns = new List <string>();

            var properties = PropertyDiscovery.GetProperties(type);

            var idProperty = PropertyDiscovery.GetProperty("Id", type);

            foreach (var property in properties)
            {
                if (property.Name == idProperty.Name)
                {
                    continue;
                }
                var columnName  = AttributeDiscovery.GetColumnName(property);
                var columnValue = ValueDiscovery.GetValue(instance, property);
                columns.Add($"{columnName} = {columnValue}");
            }

            result.AppendLine(string.Join(",\r\n", columns));

            result.AppendLine("WHERE");
            result.AppendLine($"Id = {ValueDiscovery.GetValue(instance, idProperty)}");

            return(result.ToString());
        }
Beispiel #2
0
        internal static string GetInsert(this object instance, bool withIdentity = false)
        {
            var result = new StringBuilder();

            var type = instance.GetType();

            var tableName = AttributeDiscovery.GetTableName(type);

            result.AppendLine($"INSERT INTO {tableName}");
            result.AppendLine("(");

            var columns = new List <string>();

            var properties = PropertyDiscovery.GetProperties(type);

            var idProperty = PropertyDiscovery.GetProperty("Id", type);

            foreach (var property in properties)
            {
                if (property.Name == idProperty.Name && !withIdentity)
                {
                    continue;
                }
                var columnName = AttributeDiscovery.GetColumnName(property);
                columns.Add(columnName);
            }

            result.AppendLine(string.Join(",\r\n", columns));
            result.AppendLine(")");
            result.AppendLine("VALUES");
            result.AppendLine("(");

            var values = new List <string>();

            foreach (var property in properties)
            {
                if (property.Name == idProperty.Name && !withIdentity)
                {
                    continue;
                }
                if (property.Name == idProperty.Name)
                {
                    var value = (string)ValueDiscovery.GetValue(instance, property);
                    values.Add(value == "0" ? "NULL" : value.ToString());
                }
                else
                {
                    var value = (string)ValueDiscovery.GetValue(instance, property);
                    values.Add(value.ToString());
                }
            }

            result.AppendLine(string.Join(",\r\n", values));
            result.AppendLine(");");
            result.AppendLine("SELECT last_insert_rowid();");
            return(result.ToString());
        }
Beispiel #3
0
        internal static string GetDelete(this object instance)
        {
            var result = new StringBuilder();

            var type       = instance.GetType();
            var idProperty = PropertyDiscovery.GetProperty("Id", type);

            var tableName = AttributeDiscovery.GetTableName(type);

            result.AppendLine($"DELETE FROM {tableName} WHERE Id = {ValueDiscovery.GetValue(instance, idProperty)}");

            return(result.ToString());
        }
Beispiel #4
0
        internal static SQLiteCommand GetUpdatePrepared(this object instance, SQLiteConnection connection)
        {
            var command = new SQLiteCommand(connection);

            var result = new StringBuilder();

            var type = instance.GetType();

            var tableName = AttributeDiscovery.GetTableName(type);

            result.AppendLine($"UPDATE {tableName}");
            result.AppendLine("SET");

            var columns = new List <string>();

            var properties = PropertyDiscovery.GetProperties(type);

            var idProperty = PropertyDiscovery.GetProperty("Id", type);

            foreach (var property in properties)
            {
                if (property.Name == idProperty.Name)
                {
                    continue;
                }
                var columnName = AttributeDiscovery.GetColumnName(property);
                columns.Add($"{columnName} = ?");

                var columnValue  = ValueDiscovery.GetValue(instance, property);
                var sqlParameter = new SQLiteParameter(columnValue.GetType().ToDbType(), columnValue);
                command.Parameters.Add(sqlParameter);
            }

            result.AppendLine(string.Join(",\r\n", columns));

            result.AppendLine("WHERE");

            var idValue = ValueDiscovery.GetValue(instance, idProperty);

            result.AppendLine("Id = ?");

            var idSqlParameter = new SQLiteParameter(idValue.GetType().ToDbType(), idValue);

            command.Parameters.Add(idSqlParameter);

            command.CommandText = result.ToString();

            return(command);
        }