Esempio n. 1
0
        /// <summary>
        /// Parse the specified SQL string and creates the corresponding
        /// DbSchema
        /// </summary>
        /// <param name="sql">The SQL string to parse</param>
        /// <returns>The DB schema that corresponds to the SQL string.</returns>
        public static DbSchema ParseSql(string sql)
        {
            DbSchema schema = new DbSchema();
            string   newSQL = sql.Replace("IF NOT EXISTS", ""); //remove from query of checking to exist the table in DB

            do
            {
                Match m1 = _tableHeader.Match(newSQL);
                Match m2 = _indexHeader.Match(newSQL);
                if (m1.Success && (!m2.Success || m2.Success && m2.Index > m1.Index))
                {
                    DbTable table = ParseDbTable(ref newSQL);
                    schema._tables.Add(table.TableName, table);
                }
                else
                {
                    if (m2.Success)
                    {
                        DbIndex index = ParseDbIndex(ref newSQL);
                        schema._indexes.Add(index.IndexName, index);
                    }
                    else
                    {
                        break;
                    }
                } // else
            } while (true);

            return(schema);
        }
Esempio n. 2
0
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(this, obj))
            {
                return(true);
            }

            if (!(obj is DbSchema dst))
            {
                return(false);
            }

            if (_tables.Count != dst._tables.Count)
            {
                return(false);
            }
            if (_indexes.Count != dst._indexes.Count)
            {
                return(false);
            }

            foreach (string tableName in _tables.Keys)
            {
                if (!dst._tables.ContainsKey(tableName))
                {
                    return(false);
                }
            }

            foreach (string indexName in _indexes.Keys)
            {
                if (!dst._indexes.ContainsKey(indexName))
                {
                    return(false);
                }
            }

            foreach (string tableName in _tables.Keys)
            {
                DbTable table1 = _tables[tableName];
                DbTable table2 = dst._tables[tableName];
                if (!table1.Equals(table2))
                {
                    return(false);
                }
            } // foreach

            foreach (string indexName in _indexes.Keys)
            {
                DbIndex index1 = _indexes[indexName];
                DbIndex index2 = dst._indexes[indexName];
                if (!index1.Equals(index2))
                {
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// Parse the specified CREATE INDEX statement and returned the
        /// index representation as a DbIndex instance.
        /// </summary>
        /// <param name="sql">The CREATE INDEX sql statement</param>
        /// <returns>The DbIndex representation of the table.</returns>
        private static DbIndex ParseDbIndex(ref string sql)
        {
            DbIndex index = new DbIndex();
            Match   m     = _indexHeader.Match(sql);

            if (m.Success)
            {
                if (m.Groups[1].Success)
                {
                    index.IsUnique = true;
                }

                int start = m.Index + m.Length;
                index.IndexName = ParsePotentiallyDelimitedToken(sql, ref start);
                if (index.IndexName == null)
                {
                    throw DbUpgradeException.SchemaIsNotSupported();
                }

                // Search for occurence of "ON"
                int offset = sql.IndexOf("ON", start);
                if (offset == -1)
                {
                    throw DbUpgradeException.SchemaIsNotSupported();
                }
                start = offset + 2;

                index.TableName = ParsePotentiallyDelimitedToken(sql, ref start);
                if (index.TableName == null)
                {
                    throw DbUpgradeException.SchemaIsNotSupported();
                }

                sql = sql.Substring(start);
                if (!ScanToken(ref sql, "("))
                {
                    throw DbUpgradeException.SchemaIsNotSupported();
                }

                string cols = null;
                for (int i = 0; i < sql.Length; i++)
                {
                    if (sql[i] == ')')
                    {
                        cols = sql.Substring(0, i);
                        if (i < sql.Length - 1)
                        {
                            sql = sql.Substring(i + 1);
                        }
                        else
                        {
                            sql = string.Empty;
                        }
                        break;
                    }
                } // for
                if (cols == null)
                {
                    throw DbUpgradeException.SchemaIsNotSupported();
                }

                string[] parts = cols.Split(',');
                Dictionary <string, DbSortOrder> icols =
                    new Dictionary <string, DbSortOrder>();
                foreach (string p in parts)
                {
                    string      cn     = p.Trim();
                    string[]    cparts = cn.Split(' ', '\t');
                    DbSortOrder order  = DbSortOrder.Ascending;
                    if (cparts.Length == 2)
                    {
                        if (cparts[1].ToUpper() == "DESC")
                        {
                            order = DbSortOrder.Descending;
                        }
                        else
                        {
                            throw DbUpgradeException.SchemaIsNotSupported();
                        }
                    }

                    icols.Add(cparts[0].Trim().Trim('[', ']', '\'', '`', '\"'), order);
                }
                if (icols.Count == 0)
                {
                    throw DbUpgradeException.SchemaIsNotSupported();
                }
                index.IndexColumns = icols;

                return(index);
            }
            else
            {
                throw DbUpgradeException.SchemaIsNotSupported();
            }
        }