Example #1
0
 public override long GetLastAutoIncrementValue(TableSchema schema)
 {
     return(_lastRowId.Value ?? 0);
 }
Example #2
0
        public static HashSet <TableSchema.Relation> FindRelations(LambdaExpression expression, TableSchema schema)
        {
            var finder = new LambdaRelationFinder(schema);

            finder.Visit(expression.Body);

            return(finder._relations);
        }
Example #3
0
        public static HashSet <TableSchema.Relation> FindRelations(IEnumerable <LambdaExpression> expressions, TableSchema schema)
        {
            if (expressions == null)
            {
                return(null);
            }

            HashSet <TableSchema.Relation> relations = null;

            foreach (var lambdaExpression in expressions)
            {
                if (relations == null)
                {
                    relations = new HashSet <TableSchema.Relation>(FindRelations(lambdaExpression, schema));
                }
                else
                {
                    relations.UnionWith(FindRelations(lambdaExpression, schema));
                }
            }

            return(relations);
        }
Example #4
0
        protected Repository(Type type, StorageContext context)
        {
            Context = context;

            Schema = new TableSchema(type, this);
        }
Example #5
0
 private LambdaRelationFinder(TableSchema schema)
 {
     _schema = schema;
 }
Example #6
0
 public bool CreateOrUpdateTable(TableSchema schema, bool recreateTable, bool recreateIndexes)
 {
     return(true); // NOP
 }
Example #7
0
 public CompositeKey(TableSchema schema, SerializedEntity o)
 {
     _keyValues = schema.PrimaryKeys.ToDictionary(pk => pk.MappedName, pk => o[pk.MappedName]);
 }
Example #8
0
 public bool DeleteObjects(INativeQuerySpec filter, TableSchema schema)
 {
     throw new NotSupportedException();
 }
Example #9
0
 public QuerySpec CreateQuerySpec(FilterSpec filter, ScalarSpec expression, SortOrderSpec sortOrder, int?skip, int?take, TableSchema schema)
 {
     throw new NotSupportedException();
 }
Example #10
0
 public IEnumerable <SerializedEntity> GetObjectsWithPrefetch(INativeQuerySpec filter, TableSchema schema, IEnumerable <TableSchema.Relation> prefetchRelations, out IEnumerable <Dictionary <TableSchema.Relation, SerializedEntity> > relatedEntities)
 {
     throw new NotSupportedException();
 }
Example #11
0
        public ObjectWriteResult WriteObject(SerializedEntity o, bool?createNew, TableSchema schema)
        {
            if (schema.IncrementKey != null && createNew == null)
            {
                return new ObjectWriteResult()
                       {
                           Success = false
                       }
            }
            ;

            var result = new ObjectWriteResult();

            using (var bucket = GetBucket(schema))
            {
                if (createNew == null)
                {
                    if (schema.PrimaryKeys.Length == 0)
                    {
                        return new ObjectWriteResult()
                               {
                                   Success = false
                               }
                    }
                    ;

                    var compositeKey = new CompositeKey(schema.PrimaryKeys.ToDictionary(field => field.MappedName, field => o[field.MappedName]));

                    createNew = !bucket.IndexedObjects.ContainsKey(compositeKey);
                }

                if (createNew.Value)
                {
                    if (schema.IncrementKey != null)
                    {
                        o[schema.IncrementKey.MappedName] = bucket.NextIncrementCounter(schema.IncrementKey.FieldName).Convert(schema.IncrementKey.FieldType);

                        result.OriginalUpdated = true;
                    }

                    var storedObject = o.AsDictionary();

                    bucket.Objects.Add(storedObject);
                    bucket.IndexedObjects[new CompositeKey(schema, o)] = storedObject;

                    result.Added   = true;
                    result.Success = true;
                }
                else
                {
                    if (schema.PrimaryKeys.Length > 0)
                    {
                        for (int i = 0; i < bucket.Objects.Count; i++)
                        {
                            if (schema.PrimaryKeys.All(primaryKey => Equals(bucket.Objects[i][primaryKey.MappedName], o[primaryKey.MappedName])))
                            {
                                var compositeKey = new CompositeKey(schema, o);

                                var storedObject = o.AsDictionary();

                                bucket.Objects[i] = storedObject;
                                bucket.IndexedObjects[compositeKey] = storedObject;
                                result.Success = true;
                                break;
                            }
                        }
                    }
                    else
                    {
                        result.Success = false;
                    }
                }
            }

            return(result);
        }
Example #12
0
 public object GetScalar(Aggregate aggregate, INativeQuerySpec nativeQuerySpec, TableSchema schema)
 {
     throw new NotSupportedException();
 }
Example #13
0
 private StorageBucket.BucketAccessor GetBucket(TableSchema schema)
 {
     return((_buckets[schema] ?? (_buckets[schema] = new StorageBucket())).Accessor());
 }
Example #14
0
        private void _LoadRelations(object obj, IEnumerable <LambdaExpression> relationsToLoad /*, TableSchema parentSchema*/)
        {
            TableSchema parentSchema = GetSchema(obj.GetType());

            Ir.LoadRelations(obj, LambdaRelationFinder.FindRelations(relationsToLoad, parentSchema));
        }
Example #15
0
        public override void CreateOrUpdateTable(TableSchema schema, bool recreateTable, bool recreateIndexes, SqlDataProvider dataProvider)
        {
            const string longTextType = "TEXT";

            var columnMappings = new[]
            {
                new { Flags = TypeFlags.Array | TypeFlags.Byte, ColumnType = "LONGBLOB", DefaultValue = (string)null },
                new { Flags = TypeFlags.Boolean, ColumnType = "TINYINT", DefaultValue = "0" },
                new { Flags = TypeFlags.Integer, ColumnType = "INTEGER", DefaultValue = "0" },
                new { Flags = TypeFlags.Decimal, ColumnType = "DECIMAL({0},{1})", DefaultValue = "0" },
                new { Flags = TypeFlags.FloatingPoint, ColumnType = "REAL", DefaultValue = "0" },
                new { Flags = TypeFlags.String, ColumnType = "VARCHAR({0})", DefaultValue = "''" },
                new { Flags = TypeFlags.DateTime, ColumnType = "DATETIME", DefaultValue = (string)null },
                new { Flags = TypeFlags.Guid, ColumnType = "VARCHAR(32)", DefaultValue = (string)null },
            };

            if (recreateTable)
            {
                recreateIndexes = true;
            }

            HashSet <string> tableNames = new HashSet <string>(dataProvider.ExecuteSqlReader("SELECT name FROM sqlite_master WHERE type='table'", null).Select(rec => rec["name"].ToString()), StringComparer.OrdinalIgnoreCase);

            if (tableNames.Contains(schema.MappedName) && recreateTable)
            {
                dataProvider.ExecuteSql("DROP TABLE " + QuoteTable(schema.MappedName), null);
            }

            var existingColumns = dataProvider.ExecuteSqlReader("pragma table_info(" + QuoteTable(schema.MappedName) + ")", null).ToLookup(rec => rec["name"].ToString());

            var parts = new List <string>();

            bool createNew = true;



            foreach (var field in schema.Fields)
            {
                var columnMapping = columnMappings.FirstOrDefault(mapping => field.FieldInfo.Inspector().Type.Inspector().Is(mapping.Flags));

                if (columnMapping == null)
                {
                    continue;
                }

                if (existingColumns.Contains(field.MappedName) && !recreateTable)
                {
                    createNew = false;
                    continue;
                }

                if (columnMapping.Flags == TypeFlags.String && field.ColumnSize == int.MaxValue)
                {
                    columnMapping = new { columnMapping.Flags, ColumnType = longTextType, DefaultValue = "''" }
                }
                ;

                var part = $"{QuoteField(field.MappedName)} {string.Format(columnMapping.ColumnType, field.ColumnSize, field.ColumnScale)}";

                if (!field.ColumnNullable || field.PrimaryKey)
                {
                    part += " NOT NULL";

                    if (columnMapping.DefaultValue != null)
                    {
                        part += " DEFAULT " + columnMapping.DefaultValue;
                    }
                }
                else
                {
                    part += " NULL";
                }

                if (field.PrimaryKey && schema.PrimaryKeys.Length == 1)
                {
                    part += " PRIMARY KEY";

                    if (field.AutoIncrement)
                    {
                        part += " AUTOINCREMENT";
                    }
                }

                parts.Add(part);
            }

            if (parts.Any() && schema.PrimaryKeys.Length > 1)
            {
                parts.Add("PRIMARY KEY (" + string.Join(",", schema.PrimaryKeys.Select(pk => QuoteField(pk.MappedName))) + ")");
            }

            if (parts.Any())
            {
                if (createNew)
                {
                    dataProvider.ExecuteSql("CREATE TABLE " + QuoteTable(schema.MappedName) + " (" + string.Join(",", parts) + ")", null);
                }
                else
                {
                    foreach (var part in parts)
                    {
                        dataProvider.ExecuteSql("ALTER TABLE " + QuoteTable(schema.MappedName) + " ADD COLUMN " + part + ";", null);
                    }
                }
            }

            var existingIndexes = dataProvider.ExecuteSqlReader("PRAGMA INDEX_LIST(" + QuoteTable(schema.MappedName) + ")", null).ToLookup(rec => rec["name"].ToString());

            foreach (var index in schema.Indexes)
            {
                if (existingIndexes[index.Name].Any())
                {
                    if (recreateIndexes)
                    {
                        dataProvider.ExecuteSql("DROP INDEX " + QuoteTable(index.Name) + " ON " + QuoteTable(schema.MappedName), null);
                    }
                    else
                    {
                        continue;
                    }
                }

                string createIndexSql = "CREATE ";

                if (index.Unique)
                {
                    createIndexSql += "UNIQUE ";
                }

                createIndexSql += "INDEX " + QuoteTable(index.Name) + " ON " + QuoteTable(schema.MappedName) + " (";

                createIndexSql += string.Join(",", index.FieldsWithOrder.Select(field => QuoteField(field.Item1.MappedName) + " " + (field.Item2 == SortOrder.Ascending ? "ASC" : "DESC")));

                createIndexSql += ")";

                dataProvider.ExecuteSql(createIndexSql, null);
            }
        }
Example #16
0
 public abstract void CreateOrUpdateTable(TableSchema schema, bool recreateTable, bool recreateIndexes, SqlDataProvider dataProvider);