Beispiel #1
0
        private static string GetColumnNamesStatement()
        {
            const string columnSeparator = ", ";
            var          statement       = "(";

            foreach (var currentProperty in typeof(SampleData).GetProperties())
            {
                if (!CSharpToSQLTypeConverter.IsTypeValid(currentProperty.PropertyType))
                {
                    continue;
                }
                statement += $"{currentProperty.Name}{columnSeparator}";
            }
            // Removing last extra separator
            statement  = statement.Substring(0, statement.Length - columnSeparator.Length);
            statement += ")";
            return(statement);
        }
        internal static string GenerateBasedOnType(Type type, string tableName)
        {
            var createTableSQL = $"CREATE TABLE {tableName} (\n";
            var properties     = type.GetProperties();

            foreach (var currentProperty in properties)
            {
                if (!CSharpToSQLTypeConverter.IsTypeValid(currentProperty.PropertyType))
                {
                    continue;
                }

                createTableSQL += $"{currentProperty.Name} {CSharpToSQLTypeConverter.GetSQLType(currentProperty.PropertyType)}{columnDefinitionSeparator}";
            }
            createTableSQL  = createTableSQL.Substring(0, createTableSQL.Length - columnDefinitionSeparator.Length);
            createTableSQL += "\n)";

            return(createTableSQL);
        }
Beispiel #3
0
        private static string GetValuesStatement(SampleData data)
        {
            const string valuesSeparator = ", ";
            var          statement       = string.Empty;

            statement += "(";
            foreach (var currentProperty in data.GetType().GetProperties())
            {
                if (!CSharpToSQLTypeConverter.IsTypeValid(currentProperty.PropertyType))
                {
                    continue;
                }

                var propertyType = currentProperty.PropertyType;

                if (propertyType == typeof(int) || propertyType == typeof(long) || propertyType == typeof(float) || propertyType == typeof(double))
                {
                    statement += $"{currentProperty.GetValue(data)}";
                }
                else if (propertyType == typeof(string))
                {
                    statement += $"'{currentProperty.GetValue(data)}'";
                }
                else if (propertyType == typeof(bool))
                {
                    statement += $"{(((bool)currentProperty.GetValue(data)) ? "1" : "0")}";
                }
                else if (propertyType == typeof(DateTime))
                {
                    statement += $"(convert(datetime, '{((DateTime)currentProperty.GetValue(data)).ToString("s", System.Globalization.CultureInfo.InvariantCulture)}', 126))";
                }
                statement += valuesSeparator;
            }
            statement  = statement.Substring(0, statement.Length - valuesSeparator.Length);
            statement += ")";
            return(statement);
        }