예제 #1
0
        public object IndexDefinition(ADOX.Table tblDef, ADOX.Index idxDef)
        {
            int    intLoop  = 0;
            string strIndex = null;

            ADOX.Column colDef = null;

            if (idxDef.PrimaryKey == true)
            {
                strIndex = strIndex + "PRIMARY KEY ";
            }
            else if (idxDef.Unique)
            {
                strIndex = strIndex + "UNIQUE INDEX " + MySQLName(idxDef.Name);
            }
            else
            {
                strIndex = strIndex + "INDEX " + MySQLName(idxDef.Name);
            }

            strIndex = strIndex + "(";
            for (intLoop = 0; intLoop < idxDef.Columns.Count; intLoop++)
            {
                colDef   = idxDef.Columns[intLoop];
                strIndex = strIndex + MySQLName(colDef.Name);
                if (intLoop < idxDef.Columns.Count - 1)
                {
                    strIndex = strIndex + ",";
                }
            }

            strIndex = strIndex + ")";
            return(strIndex);
        }
예제 #2
0
        /// <summary>
        /// Adds key to index of table.
        /// </summary>
        /// <param name="tableDescription">Table description.</param>
        /// <param name="indexDefinition">Index definition.</param>
        /// <param name="indexes">Database indexes.</param>
        private void _AddKeyToTableIndex(TableDescription tableDescription,
                                         TableIndex indexDefinition,
                                         ADOX.Indexes indexes)
        {
            Debug.Assert(null != tableDescription);
            Debug.Assert(null != indexDefinition);
            Debug.Assert(null != indexes);

            var index = new ADOX.Index();
            ADOX.Columns columns = index.Columns;
            switch (indexDefinition.Type)
            {
                case TableIndexType.Primary:
                case TableIndexType.Simple:
                {
                    string field = indexDefinition.FieldNames[0];
                    if (TableIndexType.Primary == indexDefinition.Type)
                    {
                        index.Name = INDEX_PRIMARY_KEY;
                        index.PrimaryKey = true;
                        index.Unique = true;
                    }
                    else // simple
                        index.Name = field;

                    FieldInfo info = tableDescription.GetFieldInfo(field);
                    Debug.Assert(null != info);
                    columns.Append(info.Name, _ConvertType(info.Type), info.Size);
                    break;
                }

                case TableIndexType.Multiple:
                {
                    var sbKeyName = new StringBuilder();
                    foreach (string field in indexDefinition.FieldNames)
                    {
                        FieldInfo info = tableDescription.GetFieldInfo(field);
                        Debug.Assert(null != info);
                        columns.Append(info.Name, _ConvertType(info.Type), info.Size);

                        if (!string.IsNullOrEmpty(sbKeyName.ToString()))
                            sbKeyName.Append(SQL_KEY_SYMBOL);
                        sbKeyName.Append(field);
                    }

                    index.Name = sbKeyName.ToString();
                    break;
                }

                default:
                {
                    Debug.Assert(false); // NOTE: not supported
                    break;
                }
            }

            index.IndexNulls = ADOX.AllowNullsEnum.adIndexNullsAllow;
            indexes.Append(index, null);
        }
예제 #3
0
        protected void CreateNewAccessDatabase(string connstr)
        {
            var cat = new ADOX.Catalog();

            cat.Create(connstr);

            var laptimeTable = new ADOX.Table();

            laptimeTable.Name = "DriverLaptime";
            laptimeTable.Columns.Append("car");
            laptimeTable.Columns.Append("driver");
            laptimeTable.Columns.Append("laptime", ADOX.DataTypeEnum.adInteger);

            var pkIdx = new ADOX.Index();

            pkIdx.PrimaryKey = true;
            pkIdx.Name       = "PK_DriverLaptime";
            pkIdx.Columns.Append("car");
            pkIdx.Columns.Append("driver");
            laptimeTable.Indexes.Append(pkIdx);

            var laptimeIdx = new ADOX.Index();

            laptimeIdx.PrimaryKey = false;
            laptimeIdx.Name       = "LaptimeIndex_DriverLaptime";
            laptimeIdx.Columns.Append("car");
            laptimeIdx.Columns.Append("laptime", ADOX.DataTypeEnum.adInteger);
            laptimeTable.Indexes.Append(laptimeIdx);

            cat.Tables.Append(laptimeTable);

            // track이 바뀌면 데이터를 리셋해야 하므로 track 이름을 저장할 곳이 필요하다.
            var trackTable = new ADOX.Table();

            trackTable.Name = "Track";
            trackTable.Columns.Append("name");

            cat.Tables.Append(trackTable);

            var timestampTable = new ADOX.Table();

            timestampTable.Name = "Timestam";
            timestampTable.Columns.Append("v");

            cat.Tables.Append(timestampTable);

            var conn = cat.ActiveConnection as ADODB.Connection;

            if (conn != null)
            {
                try
                {
                    object recordsAffected;
                    conn.Execute("insert into Track (name) values (' ')", out recordsAffected);
                    conn.Execute($"insert into Timestam (v) values ('{DateTime.Now}')", out recordsAffected);
                }
                finally
                {
                    conn.Close();
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Adds key to index of table.
        /// </summary>
        /// <param name="tableDescription">Table description.</param>
        /// <param name="indexDefinition">Index definition.</param>
        /// <param name="indexes">Database indexes.</param>
        private void _AddKeyToTableIndex(TableDescription tableDescription,
                                         TableIndex indexDefinition,
                                         ADOX.Indexes indexes)
        {
            Debug.Assert(null != tableDescription);
            Debug.Assert(null != indexDefinition);
            Debug.Assert(null != indexes);

            var index = new ADOX.Index();

            ADOX.Columns columns = index.Columns;
            switch (indexDefinition.Type)
            {
            case TableIndexType.Primary:
            case TableIndexType.Simple:
            {
                string field = indexDefinition.FieldNames[0];
                if (TableIndexType.Primary == indexDefinition.Type)
                {
                    index.Name       = INDEX_PRIMARY_KEY;
                    index.PrimaryKey = true;
                    index.Unique     = true;
                }
                else     // simple
                {
                    index.Name = field;
                }

                FieldInfo info = tableDescription.GetFieldInfo(field);
                Debug.Assert(null != info);
                columns.Append(info.Name, _ConvertType(info.Type), info.Size);
                break;
            }

            case TableIndexType.Multiple:
            {
                var sbKeyName = new StringBuilder();
                foreach (string field in indexDefinition.FieldNames)
                {
                    FieldInfo info = tableDescription.GetFieldInfo(field);
                    Debug.Assert(null != info);
                    columns.Append(info.Name, _ConvertType(info.Type), info.Size);

                    if (!string.IsNullOrEmpty(sbKeyName.ToString()))
                    {
                        sbKeyName.Append(SQL_KEY_SYMBOL);
                    }
                    sbKeyName.Append(field);
                }

                index.Name = sbKeyName.ToString();
                break;
            }

            default:
            {
                Debug.Assert(false);     // NOTE: not supported
                break;
            }
            }

            index.IndexNulls = ADOX.AllowNullsEnum.adIndexNullsAllow;
            indexes.Append(index, null);
        }