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());
        }
Exemple #2
0
        internal static string GetCreateTable(this Type type)
        {
            var result = new StringBuilder();

            var tableName = AttributeDiscovery.GetTableName(type);

            result.AppendLine($"CREATE TABLE IF NOT EXISTS {tableName}");
            result.AppendLine("(");

            var columns = new List <string>();

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

            if (idProperty != null)
            {
                if (idProperty.PropertyType != typeof(int) && idProperty.PropertyType != typeof(long))
                {
                    throw new InvalidSqlLiteIdentityMappingException("Invalid type for 'Id' expected Int32 or Int64.");
                }
                columns.Add($"{idProperty.Name.ToUpper()} INTEGER PRIMARY KEY AUTOINCREMENT");
            }

            foreach (var property in properties)
            {
                if (property.Name == idProperty.Name)
                {
                    continue;
                }
                var columnName     = AttributeDiscovery.GetColumnName(property);
                var columnType     = AttributeDiscovery.GetColumnType(property);
                var columnNullable = AttributeDiscovery.GetColumnNullable(property);
                columns.Add($"{columnName} {columnType.ToString().ToUpper()} {columnNullable}");
            }

            result.AppendLine(string.Join(",\r\n", columns));
            result.AppendLine(");");
            result.AppendLine($"CREATE UNIQUE INDEX IF NOT EXISTS {tableName}_ID_INDEX ON {tableName} (ID);");

            foreach (var property in properties)
            {
                if (property.Name == idProperty.Name)
                {
                    continue;
                }
                var columnName        = AttributeDiscovery.GetColumnName(property);
                var indexName         = AttributeDiscovery.GetIndexName(property);
                var indexUnique       = AttributeDiscovery.GetIndexUnique(property);
                var additionalColumns = AttributeDiscovery.GetIndexAdditionalColumns(property);
                if (!string.IsNullOrEmpty(additionalColumns))
                {
                    columnName = string.Join(",", columnName, additionalColumns);
                }
                result.AppendLine($"CREATE {indexUnique} INDEX IF NOT EXISTS {tableName}_{indexName}_INDEX ON {tableName} ({columnName});");
            }

            return(result.ToString());
        }
Exemple #3
0
 public virtual void Insert <T>(T instance)
 {
     using (var insertCommand = instance.GetInsertPrepared(this.Connection))
     {
         var result     = insertCommand.ExecuteScalar();
         var idProperty = PropertyDiscovery.GetProperty("Id", typeof(T));
         idProperty.SetValue(instance, result);
     }
 }
Exemple #4
0
        internal static List <T> TransformTo <T>(this QueryResponse response) where T : new()
        {
            var results = new List <T>();

            var type       = typeof(T);
            var properties = PropertyDiscovery.GetProperties(type);

            foreach (var row in response.Rows)
            {
                var result = new T();

                foreach (var prop in response.Query.Properties)
                {
                    var ordinal  = response.Query.Properties.IndexOf(prop);
                    var property = properties.FirstOrDefault(x => x.Name.ToUpper() == prop.ToUpper());

                    var columnType = AttributeDiscovery.GetColumnType(property);
                    var columnName = AttributeDiscovery.GetColumnName(property);

                    if (columnName.ToUpper() == "ID")
                    {
                        ReadInteger(property, row[ordinal], result);
                        continue;
                    }

                    switch (columnType)
                    {
                    case SqlLiteType.Integer:
                    {
                        ReadInteger(property, row[ordinal], result);
                        break;
                    }

                    case SqlLiteType.Real:
                    {
                        ReadReal(property, row[ordinal], result);
                        break;
                    }

                    case SqlLiteType.Text:
                    {
                        ReadString(property, row[ordinal], result);
                        break;
                    }

                    case SqlLiteType.Blob:
                    {
                        throw new NotSupportedException("Cannot do blobs yet...");
                    }
                    }
                }

                results.Add(result);
            }

            return(results);
        }
        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());
        }
        internal static SQLiteCommand GetInsertPrepared(this object instance, SQLiteConnection connection, bool withIdentity = false)
        {
            var command = new SQLiteCommand(connection);

            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;
                }
                values.Add("?");
                var instanceValue = property.GetValue(instance);

                var sqlParameter = new SQLiteParameter(instanceValue.GetType().ToDbType(), instanceValue);
                command.Parameters.Add(sqlParameter);
            }

            result.AppendLine(string.Join(",\r\n", values));
            result.AppendLine(");");
            result.AppendLine("SELECT last_insert_rowid();");

            command.CommandText = result.ToString();
            return(command);
        }
        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());
        }
Exemple #8
0
        public virtual void Insert <T>(T instance)
        {
            var command = new Command();

            command.AddInsert(instance);

            var response = PostScalar(command);

            if (response.ScalarResults.Any())
            {
                var idProperty = PropertyDiscovery.GetProperty("Id", typeof(T));
                idProperty.SetValue(instance, response.ScalarResults.First());
            }
        }
        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);
        }
Exemple #10
0
        public virtual async Task InsertManyAsync <T>(IEnumerable <T> instances, bool withIdentity = false)
        {
            var command = new Command();

            command.AddManyInserts(instances.Cast <object>(), withIdentity);

            var response = await PostScalarAsync(command);

            if (response.ScalarResults.Any())
            {
                var instanceList = instances.ToList();
                var idProperty   = PropertyDiscovery.GetProperty("Id", typeof(T));
                for (var index = 0; index < instanceList.Count; index++)
                {
                    var instance = instanceList[index];
                    var identity = Convert.ToInt64(response.ScalarResults[index]);
                    idProperty.SetValue(instance, identity);
                }
            }
        }
        internal void DiscoverAndInitializeCollections()
        {
            var discoveryProperties = new PropertyDiscovery <CosmosDbContext>(this);

            discoveryProperties.Initialize(typeof(CosmosCollection <>), typeof(ICosmosCollection <>), "Collection");
        }
Exemple #12
0
 public PropertyContextAttributeDiscovery(PropertyInfo[] propertyInfo)
 {
     _executor = GetExecutor(propertyInfo);
 }