示例#1
0
        /// <summary>
        /// Create a OfflineEntity Table with metadata information added
        /// </summary>
        internal void CreateTable(Type ty)
        {
            using (SQLiteConnection connection = new SQLiteConnection(localFilePath))
            {
                try
                {
                    // Get mapping from my type
                    var map = manager.GetMapping(ty);

                    // Create 2 tables : One for datas, one for tracking
                    var query         = SQLiteConstants.CreateTable;
                    var queryTracking = SQLiteConstants.CreateTrackingTable;

                    var columnsDcl         = new List <String>();
                    var columnsDclTracking = new List <String>();
                    var columnsPk          = new List <String>();

                    // Foreach columns, create the tsql command to execute
                    foreach (var c in map.Columns)
                    {
                        string dec = "\"" + c.Name + "\" " + Orm.SqlType(c) + " ";

                        columnsDcl.Add(dec);

                        // If it's the PK, add it to tracking
                        if (!c.IsPK)
                        {
                            continue;
                        }

                        columnsDclTracking.Add(dec);
                        columnsPk.Add(c.Name + " ");
                    }


                    var pkTracking = string.Join(",\n", columnsPk.ToArray());
                    // Adding metadatas to tracking table
                    columnsDclTracking.AddRange(GetOfflineEntityMetadataSQlDecl());

                    var decl         = string.Join(",\n", columnsDcl.ToArray());
                    var declTracking = string.Join(",\n", columnsDclTracking.ToArray());

                    string pKeyDecl = String.Empty;
                    if (columnsDclTracking.Count > 0)
                    {
                        pKeyDecl = String.Format(",\n PRIMARY KEY ({0})", pkTracking);
                    }

                    query         = String.Format(query, map.TableName, decl, pKeyDecl);
                    queryTracking = String.Format(queryTracking, map.TableName, declTracking, pKeyDecl);

                    using (var statement = connection.Prepare(query))
                    {
                        statement.Step();
                    }
                    using (var statement = connection.Prepare(queryTracking))
                    {
                        statement.Step();
                    }

                    var indexes = new Dictionary <string, IndexInfo>();
                    foreach (var c in map.Columns)
                    {
                        foreach (var i in c.Indices)
                        {
                            var       iname = i.Name ?? map.TableName + "_" + c.Name;
                            IndexInfo iinfo;
                            if (!indexes.TryGetValue(iname, out iinfo))
                            {
                                iinfo = new IndexInfo
                                {
                                    //IndexName = iname,
                                    TableName = map.TableName,
                                    Unique    = i.Unique,
                                    Columns   = new List <IndexedColumn>()
                                };
                                indexes.Add(iname, iinfo);
                            }

                            if (i.Unique != iinfo.Unique)
                            {
                                throw new Exception(
                                          "All the columns in an index must have the same value for their Unique property");
                            }

                            iinfo.Columns.Add(new IndexedColumn
                            {
                                Order      = i.Order,
                                ColumnName = c.Name
                            });
                        }
                    }

                    foreach (var indexName in indexes.Keys)
                    {
                        var          index     = indexes[indexName];
                        const string sqlFormat = "create {3} index if not exists \"{0}\" on \"{1}\"(\"{2}\")";
                        var          columns   = String.Join("\",\"",
                                                             index.Columns.OrderBy(i => i.Order).Select(i => i.ColumnName).ToArray());
                        var sql = String.Format(sqlFormat, indexName, index.TableName, columns,
                                                index.Unique ? "unique" : "");

                        using (var statement = connection.Prepare(sql))
                        {
                            statement.Step();
                        }
                    }

                    // Create Triggers
                    this.CreateTriggers(ty, connection);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    throw;
                }
            }
        }