Пример #1
0
        /// <summary>
        /// Parse the table columns section of the CREATE TABLE DDL statement.
        /// </summary>
        /// <param name="sql">The SQL statement to parse</param>
        /// <param name="tableName">Name of the table.</param>
        /// <returns>
        /// The list of DbColumn objects that represent the meta
        /// data about all table columns.
        /// </returns>
        public static List <DbColumn> ParseTableColumns(ref string sql, string tableName)
        {
            // Skip '('
            if (!ScanToken(ref sql, "("))
            {
                throw DbUpgradeException.SchemaIsNotSupported();
            }

            List <string>   primaryKeys = new List <string>();
            List <DbColumn> res         = new List <DbColumn>();

            do
            {
                if (sql.Trim().StartsWith(")"))
                {
                    break;
                }
                DbColumn col = ParseColumn(ref sql);
                if (col != null)
                {
                    res.Add(col);
                    if (!ScanToken(ref sql, ","))
                    {
                        if (ScanToken(ref sql, ")"))
                        {
                            break;
                        }
                        else
                        {
                            DbUpgradeException.SchemaIsNotSupported();
                        }
                    } // if
                }
                else
                {
                    // Try to parse as a PRIMARY KEY section
                    List <string> keys = ParsePrimaryKeys(ref sql);
                    if (keys != null)
                    {
                        primaryKeys.AddRange(keys);
                    }
                    else
                    {
                        throw DbUpgradeException.SchemaIsNotSupported();
                    }
                } // else
            } while (true);

            // Apply any primary keys found
            foreach (string pkey in primaryKeys)
            {
                bool found = false;
                foreach (DbColumn col in res)
                {
                    if (col.ColumnName == pkey)
                    {
                        col.IsPrimaryKey = true;
                        found            = true;
                        break;
                    }
                } // foreach

                if (!found)
                {
                    throw DbUpgradeException.InvalidPrimaryKeySection(tableName);
                }
            } // foreach

            return(res);
        }