Пример #1
0
        //public override void create_table(XmlNode tableNode, bool createIndexes = true, string nameCreated = "", List<string> flds_null = null) {
        //  string tableName = nameCreated != "" ? nameCreated : tableNode.Attributes["name"].Value;

        //  // schema
        //  if (tableName.IndexOf('.') >= 0) {
        //    string schemaName = tableName.Substring(0, tableName.IndexOf('.'));
        //    if (!there_schema(schemaName)) create_schema(schemaName);
        //  }

        //  // creo la tabella
        //  exec("CREATE TABLE " + tableName + "("
        //    + string.Join(",", tableNode.SelectNodes("cols/col").Cast<XmlNode>().Select(x => schema_field.getFieldSqlServer(x.Attributes["name"].Value
        //      , x.Attributes["type"].Value, xmlDoc.nodeValue(x, "numprec"), xmlDoc.nodeValue(x, "numscale"), xmlDoc.nodeValue(x, "maxlength")
        //      , xmlDoc.nodeBool(x, "nullable", false), xmlDoc.nodeValue(x, "default"), xmlDoc.nodeBool(x, "autonumber")))) + ")");

        //  // creo gli indici
        //  if (createIndexes) schema_doc.table_indexes(tableNode).ForEach(i => {
        //    i.TableName = tableName;
        //    if (nameCreated != "") i.Name = i.Name + "_" + nameCreated.Replace(".", "");
        //    create_index(i);
        //  });
        //}

        public override List <idx_table> table_idxs(string table, bool?uniques = null, string index_name = "")
        {
            List <idx_table> indexes = new List <idx_table>();

            DataTable rows = dt_table("select distinct ind.index_name, ind.index_type, ind.non_unique "
                                      + " from information_schema.statistics ind "
                                      + " where ind.table_schema = '" + db_name() + "' and ind.table_name = '" + table + "' "
                                      + (index_name != "" ? " and ind.index_name = '" + index_name + "'" : ""));

            foreach (DataRow row in rows.Rows)
            {
                // clustered
                bool?clustered = false;

                // filtro uniques
                bool unique = row["non_unique"].ToString() == "0", primary = row["index_name"].ToString() == "PRIMARY";
                if (idx_table.filter_unique(uniques, unique, primary))
                {
                    idx_table index = new idx_table(table, row["index_name"].ToString(), clustered.Value, unique, primary);

                    int i = 0;
                    index.Fields.AddRange(dt_table("SELECT ind.column_name as colname, 0 as is_descending_key "
                                                   + " FROM information_schema.statistics ind "
                                                   + " WHERE ind.table_schema = '" + db_name() + "' and ind.table_name = '" + table + "' and ind.index_name = '" + index.Name + "' "
                                                   + " ORDER BY ind.seq_in_index").Rows.Cast <DataRow>()
                                          .Select(dr => new idx_field(dr["colname"].ToString(), !(dr["is_descending_key"].ToString() == "1"), i++)));

                    indexes.Add(index);
                }
            }

            return(indexes);
        }
Пример #2
0
        public override List <idx_table> table_idxs(string table, bool?uniques = null, string index_name = "")
        {
            List <idx_table> indexes = new List <idx_table>();

            foreach (DataRow row in schemaIndexes(table).Rows)
            {
                string nm      = row["index_name"].ToString();
                bool   unique  = bool.Parse(row["unique"].ToString());
                bool   primary = bool.Parse(row["primary_key"].ToString());

                if (index_name != "" && index_name.ToLower() != nm.ToLower() ||
                    constraintType(table, nm) == "FOREIGN KEY" || !idx_table.filter_unique(uniques, unique, primary))
                {
                    continue;
                }

                idx_table index = indexes.FirstOrDefault(x => x.Name == nm);
                if (index == null)
                {
                    indexes.Add(index = new idx_table(table, nm, bool.Parse(row["clustered"].ToString()), unique, primary));
                }

                index.Fields.Add(new idx_field(row["column_name"].ToString(), int.Parse(row["collation"].ToString()) == 1, index.Fields.Count));
            }

            return(indexes);
        }
Пример #3
0
 public override idx_table create_index(idx_table index)
 {
     exec(!index.Primary ? "CREATE " + (index.Unique ? "UNIQUE" : "") + " " + (!index.Clustered ? "NONCLUSTERED" : "CLUSTERED")
          + " INDEX [" + index.Name + "] ON " + index.TableName + " ("
          + string.Join(", ", index.Fields.Select(x => "[" + x.Name + "]" + (x.Ascending ? " ASC" : " DESC"))) + ")"
 : "ALTER TABLE " + index.TableName + " ADD CONSTRAINT " + index.Name + " PRIMARY KEY "
          + (!index.Clustered ? "NONCLUSTERED" : "CLUSTERED") + " ("
          + string.Join(",", index.Fields.Select(x => "[" + x.Name + "]")) + ")");
     return(index);
 }
Пример #4
0
        public override List <idx_table> table_idxs(string table, bool?uniques = null, string index_name = "")
        {
            List <idx_table> indexes = new List <idx_table>();

            if (table.IndexOf(".") < 0)
            {
                table = "dbo." + table;
            }

            string sql = "SELECT distinct ind.name, ind.type_desc, ind.is_unique, ind.is_primary_key "
                         + " FROM sys.indexes ind "
                         + " INNER JOIN sys.tables t ON ind.object_id = t.object_id "
                         + (table.IndexOf('.') > 0 ? " WHERE SCHEMA_NAME(T.schema_id) + '.' + T.NAME = '" + table + "' AND IND.NAME IS NOT NULL"
        : " WHERE T.NAME = '" + table + "' AND IND.NAME IS NOT NULL") + (index_name != "" ? " AND IND.NAME = '" + index_name + "'" : "");

            DataTable rows = dt_table(sql);

            foreach (DataRow row in rows.Rows)
            {
                // clustered
                bool?clustered = row["type_desc"].ToString() == "CLUSTERED" ? true
          : row["type_desc"].ToString() == "NONCLUSTERED" ? false : (bool?)null;
                if (!clustered.HasValue)
                {
                    logWarning("il tipo di indice '" + type + "' non viene gestito verrà adottato il tipo 'NONCLUSTERED'!");
                    clustered = false;
                }

                // filtro uniques
                bool unique = bool.Parse(row["is_unique"].ToString()), primary = bool.Parse(row["is_primary_key"].ToString());
                if (idx_table.filter_unique(uniques, unique, primary))
                {
                    idx_table index = new idx_table(table, row["name"].ToString(), clustered.Value, unique, primary);

                    int i = 0;
                    index.Fields.AddRange(dt_table("SELECT col.name as colname, ic.is_descending_key "
                                                   + " FROM sys.indexes ind "
                                                   + " INNER JOIN sys.index_columns ic ON  ind.object_id = ic.object_id and ind.index_id = ic.index_id "
                                                   + " INNER JOIN sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id "
                                                   + " INNER JOIN sys.tables t ON ind.object_id = t.object_id "
                                                   + ((table.IndexOf('.') >= 0) ? " WHERE SCHEMA_NAME(T.schema_id) + '.' + t.name = '" + table + "' and ind.name = '" + index.Name + "' "
           : " WHERE t.name = '" + table + "' and ind.name = '" + index.Name + "' ")
                                                   + " ORDER BY t.name, ind.name, ind.index_id, ic.index_column_id ").Rows.Cast <DataRow>()
                                          .Select(dr => new idx_field(dr["colname"].ToString(), !bool.Parse(dr["is_descending_key"].ToString()), i++)));

                    indexes.Add(index);
                }
            }

            return(indexes);
        }
Пример #5
0
        static public bool sameIndex(idx_table idx, idx_table idx2)
        {
            if (idx.Clustered != idx2.Clustered || idx.Unique != idx2.Unique || idx.Primary != idx2.Primary)
            {
                return(false);
            }

            foreach (idx_field field in idx.Fields)
            {
                if (idx2.existField(field.Name) == null)
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #6
0
        public override idx_table create_index(idx_table index)
        {
            logInfo("creazione indice '" + index.Name + "'");

            //string clusteredSql = "CLUSTERED";
            //if (!index.Clustered) clusteredSql = "NONCLUSTERED";

            string sql = "";

            foreach (idx_field iField in index.Fields)
            {
                sql += (sql != "" ? ", " : "") + "[" + iField.Name + "]" + (iField.Ascending ? " ASC" : " DESC");
            }

            exec("CREATE " + (index.Unique ? "UNIQUE" : "") + " INDEX [" + index.Name + "] ON [" + index.TableName + "] ("
                 + sql + ")" + (index.Primary ? " WITH PRIMARY" : ""));

            return(index);
        }
Пример #7
0
        static public XmlNode create_idx_node(xmlDoc doc, idx_table idx)
        {
            XmlNode idx_node = doc.doc.CreateElement("index");

            idx_node.Attributes.Append(doc.doc.CreateAttribute("name")).Value      = idx.Name;
            idx_node.Attributes.Append(doc.doc.CreateAttribute("clustered")).Value = idx.Clustered.ToString().ToLower();
            idx_node.Attributes.Append(doc.doc.CreateAttribute("unique")).Value    = idx.Unique.ToString().ToLower();
            idx_node.Attributes.Append(doc.doc.CreateAttribute("primary")).Value   = idx.Primary.ToString().ToLower();

            XmlNode fieldsNode = idx_node.AppendChild(doc.doc.CreateElement("fields"));

            foreach (idx_field field in idx.Fields)
            {
                XmlNode fieldNode = fieldsNode.AppendChild(doc.doc.CreateElement("field"));
                fieldNode.Attributes.Append(doc.doc.CreateAttribute("name")).Value      = field.Name;
                fieldNode.Attributes.Append(doc.doc.CreateAttribute("nameupper")).Value = field.Name.ToUpper();
                fieldNode.Attributes.Append(doc.doc.CreateAttribute("ascending")).Value = field.Ascending.ToString().ToLower();
            }

            return(idx_node);
        }
Пример #8
0
 static public idx_table findIndex(List <idx_table> list, idx_table idx)
 {
     return(list.FirstOrDefault(x => sameIndex(x, idx)));
 }
Пример #9
0
 public XmlNode create_idx_node(idx_table idx)
 {
     return(create_idx_node(_doc, idx));
 }
Пример #10
0
 public XmlNode add_idx(string table, idx_table idx)
 {
     return(xmlDoc.add_node(xmlDoc.add_node(table_node(table), "indexes"), create_idx_node(idx)));
 }
Пример #11
0
 public virtual idx_table create_index(idx_table index)
 {
     throw new Exception("il provider " + _dbType.ToString() + " non supporta la funzionalità createIndex");
 }