Beispiel #1
0
        /// <summary>
        /// 升级表补充缺失的列
        /// </summary>
        /// <param name="map"></param>
        private void MigrateTable(TableMapping map)
        {
            try
            {
                var existingCols = GetTableInfo(map.TableName);

                var toBeAdded = new List <TableMapping.Column>();

                foreach (var p in map.Columns)
                {
                    var found = false;
                    foreach (var c in existingCols)
                    {
                        found = (string.Compare(p.Name, c.Name, StringComparison.OrdinalIgnoreCase) == 0);
                        if (found)
                        {
                            break;
                        }
                    }
                    if (!found)
                    {
                        toBeAdded.Add(p);
                    }
                }

                foreach (var p in toBeAdded)
                {
                    var addCol = string.Format("ALTER TABLE \"{0}\" ADD COLUMN {1}", map.TableName, Orm.SqlDecl(p, StoreDateTimeAsTicks));
                    Execute(addCol);
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogError("SQLiteMapping-MigrateTable-TableName:{0}-ex:{1}", map.TableName, ex);
                ex = null;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Internal CreateTable
        /// </summary>
        /// <param name="map"></param>
        /// <returns></returns>
        private int InternalCreateTable(TableMapping map)
        {
            if (map == null)
            {
                throw new ArgumentNullException("map");
            }

            var query = string.Format("CREATE TABLE IF NOT EXISTS \"{0}\"(\n", map.TableName);

            var pks             = map.PKs;
            var isConstraintPKs = pks != null && pks.Length > 1; //复合主键
            var decls           = map.Columns.Select(p => Orm.SqlDecl(p, StoreDateTimeAsTicks, isConstraintPKs));

            query += string.Join(",\n", decls.ToArray());
            if (isConstraintPKs)//处理复合主键
            {
                query += Orm.SqlDeclPKs(map.PKs);
            }
            query += ");";

            var count = Execute(query);

            if (count == 0 || count == -1) //不同版本的 System.Data.SQLite.dll 返回的结果不一样, 老版本永远返回 0 , 新版本永远返回 -1
            {                              //Possible bug: This always seems to return 0?
                // Table already exists, migrate it
                MigrateTable(map);
            }

            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];
                var columns = String.Join("\",\"", index.Columns.OrderBy(i => i.Order).Select(i => i.ColumnName).ToArray());
                count += CreateIndex(indexName, index.TableName, columns, index.Unique);
            }

            return(count);
        }