Exemple #1
0
        public void Delete(IList <int> docIds)
        {
            StringBuilder sql = new StringBuilder();

            sql.Append("delete ");

            sql.AppendFormat(" {0} where docId in (", Table.DBTableName);

            int i = 0;

            foreach (int docId in docIds)
            {
                if (i++ == 0)
                {
                    sql.AppendFormat("{0}", docId);
                }
                else
                {
                    sql.AppendFormat(",{0}", docId);
                }
            }

            sql.Append(")");

            using (OracleDataProvider sqlData = new OracleDataProvider())
            {
                sqlData.Connect(Table.ConnectionString);
                sqlData.ExcuteSql(sql.ToString());
            }
        }
Exemple #2
0
        public void Truncate(string tableName)
        {
            Debug.Assert(Table != null);

            string sql = string.Format("truncate table {0}",
                                       tableName);

            using (OracleDataProvider sqlData = new OracleDataProvider())
            {
                sqlData.Connect(Table.ConnectionString);
                sqlData.ExcuteSql(sql);
            }
        }
Exemple #3
0
        public System.Data.DataSet GetSchema(string tableName)
        {
            string sql = string.Format("select * from {0}", tableName);

            using (OracleDataProvider sqlData = new OracleDataProvider())
            {
                string connectionString = this.ConnectionString;

                sqlData.Connect(connectionString);

                return(sqlData.GetSchema(sql));
            }
        }
Exemple #4
0
        public void ConnectionTest()
        {
            using (OracleDataProvider sqlData = new OracleDataProvider())
            {
                string connectionString;
                if (Table == null)
                {
                    connectionString = this.ConnectionString;
                }
                else
                {
                    connectionString = Table.ConnectionString;
                }

                sqlData.Connect(connectionString);
            }
        }
Exemple #5
0
        public int ExcuteSql(string sql)
        {
            using (OracleDataProvider sqlData = new OracleDataProvider())
            {
                string connectionString;
                if (Table == null)
                {
                    connectionString = this.ConnectionString;
                }
                else
                {
                    connectionString = Table.ConnectionString;
                }

                sqlData.Connect(connectionString);

                return(sqlData.ExcuteSql(sql));
            }
        }
Exemple #6
0
        public System.Data.DataSet QuerySql(string sql)
        {
            using (OracleDataProvider sqlData = new OracleDataProvider())
            {
                string connectionString;
                if (Table == null)
                {
                    connectionString = this.ConnectionString;
                }
                else
                {
                    connectionString = Table.ConnectionString;
                }

                sqlData.Connect(connectionString);

                return(sqlData.QuerySql(sql));
            }
        }
Exemple #7
0
        public void Drop()
        {
            Debug.Assert(Table != null);

            string sql = string.Format("drop table {0}", Table.DBTableName);

            using (OracleDataProvider sqlData = new OracleDataProvider())
            {
                string testExistSql = string.Format("select * from {0} where rownum=1", Table.DBTableName);

                sqlData.Connect(Table.ConnectionString);

                try
                {
                    sqlData.QuerySql(testExistSql);
                    sqlData.ExcuteSql(sql);
                }
                catch
                {
                }
            }
        }
Exemple #8
0
        public IList <Core.Query.DocumentResultForSort> GetDocumentResultForSortList(int end, string where, string orderby)
        {
            string sql;

            if (end >= 0)
            {
                if (DocIdReplaceField == null)
                {
                    sql = "select docid from (select ";
                }
                else
                {
                    sql = string.Format("select {0} from (select ", DocIdReplaceField);
                }
            }
            else
            {
                sql = "select ";
            }

            if (string.IsNullOrEmpty(where))
            {
                if (DocIdReplaceField == null)
                {
                    sql += string.Format(" docid from {0} ", Table.DBTableName);
                }
                else
                {
                    sql += string.Format(" {0} from {1} ", DocIdReplaceField, Table.DBTableName);
                }
            }
            else
            {
                if (DocIdReplaceField == null)
                {
                    sql += string.Format(" docid from {0} where {1}", Table.DBTableName, where);
                }
                else
                {
                    sql += string.Format(" {0} from {1} where {2}", DocIdReplaceField, Table.DBTableName, where);
                }
            }

            if (!string.IsNullOrEmpty(orderby))
            {
                sql += " order by " + orderby;
            }

            if (end >= 0)
            {
                sql += string.Format(") where rownum <= {0}", end + 1);
            }

            List <Core.Query.DocumentResultForSort> result = new List <Core.Query.DocumentResultForSort>();

            using (OracleDataProvider sqlData = new OracleDataProvider())
            {
                sqlData.Connect(Table.ConnectionString);
                foreach (System.Data.DataRow row in sqlData.QuerySql(sql).Tables[0].Rows)
                {
                    int docId;
                    if (DocIdReplaceField == null)
                    {
                        docId = int.Parse(row[0].ToString());
                    }
                    else
                    {
                        docId = DBProvider.GetDocIdFromDocIdReplaceFieldValue(long.Parse(row[DocIdReplaceField].ToString()));

                        if (docId < 0)
                        {
                            continue;
                        }
                    }

                    result.Add(new Core.Query.DocumentResultForSort(docId));
                }
            }

            return(result);
        }
Exemple #9
0
        public System.Data.DataTable Query(IList <Field> selectFields, IList <Hubble.Core.Query.DocumentResultForSort> docs, int begin, int end)
        {
            StringBuilder sql = new StringBuilder();

            sql.Append("select ");

            int i = 0;

            foreach (Hubble.Core.Data.Field field in selectFields)
            {
                if (DocIdReplaceField != null)
                {
                    if (field.Name.Equals("DocId", StringComparison.CurrentCultureIgnoreCase))
                    {
                        continue;
                    }
                }

                if (i++ == 0)
                {
                    sql.AppendFormat("{0}", GetFieldName(field.Name));
                }
                else
                {
                    sql.AppendFormat(",{0}", GetFieldName(field.Name));
                }
            }

            if (DocIdReplaceField != null)
            {
                sql.AppendFormat(",{0}", DocIdReplaceField);
            }

            if (DocIdReplaceField == null)
            {
                sql.AppendFormat(" from {0} where docId in (", Table.DBTableName);
            }
            else
            {
                sql.AppendFormat(" from {0} where {1} in (", Table.DBTableName, DocIdReplaceField);
            }

            i = 0;

            Dictionary <long, int> replaceFieldValueToDocId = null;

            if (DocIdReplaceField != null)
            {
                replaceFieldValueToDocId = new Dictionary <long, int>();
            }

            for (int j = begin; j <= end; j++)
            {
                if (j >= docs.Count)
                {
                    break;
                }

                int docId = docs[j].DocId;

                if (DocIdReplaceField == null)
                {
                    if (i++ == 0)
                    {
                        sql.AppendFormat("{0}", docId);
                    }
                    else
                    {
                        sql.AppendFormat(",{0}", docId);
                    }
                }
                else
                {
                    long replaceFieldValue = this.DBProvider.GetDocIdReplaceFieldValue(docId);

                    replaceFieldValueToDocId.Add(replaceFieldValue, docId);

                    if (i++ == 0)
                    {
                        sql.AppendFormat("{0}", replaceFieldValue);
                    }
                    else
                    {
                        sql.AppendFormat(",{0}", replaceFieldValue);
                    }
                }
            }

            sql.Append(")");

            using (OracleDataProvider sqlData = new OracleDataProvider())
            {
                sqlData.Connect(Table.ConnectionString);

                System.Data.DataTable table = sqlData.QuerySql(sql.ToString()).Tables[0];

                if (DocIdReplaceField != null)
                {
                    table.Columns.Add(new System.Data.DataColumn("DocId", typeof(int)));

                    for (i = 0; i < table.Rows.Count; i++)
                    {
                        table.Rows[i]["DocId"] =
                            replaceFieldValueToDocId[long.Parse(table.Rows[i][DocIdReplaceField].ToString())];
                    }
                }

                return(table);
            }
        }
Exemple #10
0
        public void Update(Document doc, IList <Hubble.Core.Query.DocumentResultForSort> docs)
        {
            StringBuilder sql = new StringBuilder();

            sql.AppendFormat("update {0} set ", Table.DBTableName);

            int i = 0;

            foreach (Data.FieldValue fv in doc.FieldValues)
            {
                string value;

                if (fv.Value == null)
                {
                    value = "NULL";
                }
                else
                {
                    switch (fv.Type)
                    {
                    case Hubble.Core.Data.DataType.NVarchar:
                    case Hubble.Core.Data.DataType.NChar:
                        value = string.Format("N'{0}'", fv.Value.Replace("'", "''"));
                        break;

                    case Hubble.Core.Data.DataType.Varchar:
                    case Hubble.Core.Data.DataType.Char:
                    case Hubble.Core.Data.DataType.Data:
                        value = string.Format("'{0}'", fv.Value.Replace("'", "''"));
                        break;

                    case Hubble.Core.Data.DataType.DateTime:
                    case Hubble.Core.Data.DataType.Date:
                    case Hubble.Core.Data.DataType.SmallDateTime:
                        DateTime dateTime;
                        if (!DateTime.TryParseExact(fv.Value, "yyyy-MM-dd HH:mm:ss", null,
                                                    System.Globalization.DateTimeStyles.None, out dateTime))
                        {
                            dateTime = DateTime.Parse(fv.Value);
                        }

                        value = string.Format("to_date('{0}','yyyy-mm-dd HH24:MI:SS')", dateTime.ToString("yyyy-MM-dd HH:mm:ss"));
                        break;

                    default:
                        value = string.Format("{0}", fv.Value);
                        break;
                    }
                }

                if (i++ == 0)
                {
                    sql.AppendFormat("{0}={1}", GetFieldName(fv.FieldName), value);
                }
                else
                {
                    sql.AppendFormat(",{0}={1}", GetFieldName(fv.FieldName), value);
                }
            }

            if (DocIdReplaceField == null)
            {
                sql.Append(" where docId in (");
            }
            else
            {
                sql.AppendFormat(" where {0} in (", DocIdReplaceField);
            }


            i = 0;

            foreach (Query.DocumentResultForSort docResult in docs)
            {
                int docId = docResult.DocId;

                if (DocIdReplaceField == null)
                {
                    if (i++ == 0)
                    {
                        sql.AppendFormat("{0}", docId);
                    }
                    else
                    {
                        sql.AppendFormat(",{0}", docId);
                    }
                }
                else
                {
                    long replaceFieldValue = this.DBProvider.GetDocIdReplaceFieldValue(docId);

                    if (i++ == 0)
                    {
                        sql.AppendFormat("{0}", replaceFieldValue);
                    }
                    else
                    {
                        sql.AppendFormat(",{0}", replaceFieldValue);
                    }
                }
            }

            sql.Append(")");

            using (OracleDataProvider sqlData = new OracleDataProvider())
            {
                sqlData.Connect(Table.ConnectionString);
                sqlData.ExcuteSql(sql.ToString());
            }
        }
Exemple #11
0
        public void Insert(IList <Document> docs)
        {
            if (docs.Count <= 0)
            {
                return;
            }

            StringBuilder insertString = new StringBuilder();

            //insertString.AppendLine("DECLARE");
            insertString.Append("BEGIN ");

            foreach (Hubble.Core.Data.Document doc in docs)
            {
                insertString.AppendFormat("Insert into {0} (DocId", _Table.DBTableName);

                foreach (Data.FieldValue fv in doc.FieldValues)
                {
                    if (fv.Value == null)
                    {
                        continue;
                    }

                    insertString.AppendFormat(", {0}", GetFieldName(fv.FieldName));
                }

                insertString.AppendFormat(") Values({0}", doc.DocId);

                foreach (Data.FieldValue fv in doc.FieldValues)
                {
                    if (fv.Value == null)
                    {
                        continue;
                    }

                    switch (fv.Type)
                    {
                    case Hubble.Core.Data.DataType.NVarchar:
                    case Hubble.Core.Data.DataType.NChar:
                        if (fv.DataLength < 0)
                        {
                            insertString.Append(",N'A'");
                        }
                        else
                        {
                            insertString.AppendFormat(",N'{0}'", ReplaceTextValue(fv.Value.Replace("'", "''")));
                        }
                        break;

                    case Hubble.Core.Data.DataType.Varchar:
                    case Hubble.Core.Data.DataType.Char:
                    case Hubble.Core.Data.DataType.Data:
                        if (fv.DataLength < 0)
                        {
                            insertString.Append(",N'A'");
                        }
                        else
                        {
                            insertString.AppendFormat(",'{0}'", ReplaceTextValue(fv.Value.Replace("'", "''")));
                        }
                        break;

                    case Hubble.Core.Data.DataType.DateTime:
                    case Hubble.Core.Data.DataType.Date:
                    case Hubble.Core.Data.DataType.SmallDateTime:
                        DateTime dateTime;
                        if (!DateTime.TryParseExact(fv.Value, "yyyy-MM-dd HH:mm:ss", null,
                                                    System.Globalization.DateTimeStyles.None, out dateTime))
                        {
                            dateTime = DateTime.Parse(fv.Value);
                        }

                        insertString.AppendFormat(",to_date('{0}','yyyy-mm-dd HH24:MI:SS')", dateTime.ToString("yyyy-MM-dd HH:mm:ss"));
                        break;

                    default:
                        insertString.AppendFormat(",{0}", fv.Value);
                        break;
                    }
                }

                insertString.Append("); ");
            }

            insertString.Append(" END;");

            using (OracleDataProvider sqlData = new OracleDataProvider())
            {
                sqlData.Connect(Table.ConnectionString);
                sqlData.ExcuteSql(insertString.ToString());

                string sql = string.Format("select * from {0} where docid >= {1} and docid <= {2} order by docid FOR UPDATE",
                                           _Table.DBTableName, docs[0].DocId, docs[docs.Count - 1].DocId);

                OracleCommand cmd;

                using (OracleDataReader reader = sqlData.ExecuteReader(sql, out cmd))
                {
                    cmd.Transaction = cmd.Connection.BeginTransaction();

                    for (int i = 0; i < docs.Count; i++)
                    {
                        reader.Read();

                        for (int j = 0; j < docs[i].FieldValues.Count; j++)
                        {
                            switch (docs[i].FieldValues[j].Type)
                            {
                            case Hubble.Core.Data.DataType.NVarchar:
                            case Hubble.Core.Data.DataType.NChar:
                            case Hubble.Core.Data.DataType.Varchar:
                            case Hubble.Core.Data.DataType.Char:
                                if (docs[i].FieldValues[j].DataLength < 0)
                                {
                                    OracleLob clob = reader.GetOracleLob(reader.GetOrdinal(docs[i].FieldValues[j].FieldName));
                                    byte[]    head = new byte[2];
                                    //head[0] = 0xCC;
                                    //head[1] = 0xDD;
                                    //clob.Write(head, 0, 2);
                                    //clob.Position = 0;
                                    byte[] buffer = Encoding.Unicode.GetBytes(docs[i].FieldValues[j].Value);
                                    clob.Write(buffer, 0, buffer.Length);

                                    //Console.WriteLine(clob.LobType + ".Write(" + buffer + ", 0, 0) => " + clob.Value);

                                    //OracleLob templob = sqlData.CreateTempLob(clob.LobType);

                                    //long actual = clob.CopyTo(templob);

                                    //Console.WriteLine(clob.LobType + ".CopyTo(" + templob.Value + ") => " + actual);
                                }

                                break;
                            }
                        }
                    }

                    cmd.Transaction.Commit();
                }
            }
        }
Exemple #12
0
        public void Create()
        {
            Debug.Assert(Table != null);

            //List<string> primaryKeys = new List<string>();

            StringBuilder sql = new StringBuilder();

            sql.AppendFormat("create table {0} (", Table.DBTableName);

            sql.Append("DocId Int NOT NULL Primary Key,");

            int i = 0;

            for (i = 0; i < Table.Fields.Count; i++)
            {
                Data.Field field    = Table.Fields[i];
                string     fieldSql = GetFieldLine(field);

                if (fieldSql != null)
                {
                    sql.Append(fieldSql);

                    if (i < Table.Fields.Count - 1)
                    {
                        sql.Append(",");
                    }
                }

                //if (field.PrimaryKey)
                //{
                //    primaryKeys.Add(field.Name);
                //}
            }


            //if (primaryKeys.Count > 0)
            //{
            //    i = 0;
            //    sql.Append(" primary key NONCLUSTERED(");

            //    foreach (string pkName in primaryKeys)
            //    {
            //        if (i == 0)
            //        {
            //            sql.Append(pkName);
            //        }
            //        else
            //        {
            //            sql.Append("," + pkName);
            //        }

            //        i++;
            //    }

            //    sql.Append(")");
            //}

            sql.Append(")");

            using (OracleDataProvider sqlData = new OracleDataProvider())
            {
                sqlData.Connect(Table.ConnectionString);
                sqlData.ExcuteSql(sql.ToString());

                //sqlData.ExcuteSql(string.Format("create UNIQUE CLUSTERED Index I_{0}_DocId on {0}(DocId)",
                //    Table.DBTableName));

                if (!string.IsNullOrEmpty(Table.SQLForCreate))
                {
                    sqlData.ExcuteSql(Table.SQLForCreate);
                }
            }
        }
Exemple #13
0
        public DocumentResultWhereDictionary GetDocumentResults(int end, string where, string orderby)
        {
            string sql;

            if (end >= 0)
            {
                if (DocIdReplaceField == null)
                {
                    sql = "select docid from (select ";
                }
                else
                {
                    sql = string.Format("select {0} from (select ", DocIdReplaceField);
                }
            }
            else
            {
                sql = "select ";
            }

            if (string.IsNullOrEmpty(where))
            {
                if (DocIdReplaceField == null)
                {
                    sql += string.Format(" docid from {0} ", Table.DBTableName);
                }
                else
                {
                    sql += string.Format(" {0} from {1} ", DocIdReplaceField, Table.DBTableName);
                }
            }
            else
            {
                if (DocIdReplaceField == null)
                {
                    sql += string.Format(" docid from {0} where {1}", Table.DBTableName, where);
                }
                else
                {
                    sql += string.Format(" {0} from {1} where {2}", DocIdReplaceField, Table.DBTableName, where);
                }
            }

            if (!string.IsNullOrEmpty(orderby))
            {
                sql += " order by " + orderby;
            }

            if (end >= 0)
            {
                sql += string.Format(") where rownum <= {0}", end + 1);
            }

            Core.SFQL.Parse.DocumentResultWhereDictionary result = new Core.SFQL.Parse.DocumentResultWhereDictionary();

            using (OracleDataProvider sqlData = new OracleDataProvider())
            {
                sqlData.Connect(Table.ConnectionString);
                foreach (System.Data.DataRow row in sqlData.QuerySql(sql).Tables[0].Rows)
                {
                    int docId;
                    if (DocIdReplaceField == null)
                    {
                        docId = int.Parse(row[0].ToString());
                    }
                    else
                    {
                        docId = DBProvider.GetDocIdFromDocIdReplaceFieldValue(long.Parse(row[DocIdReplaceField].ToString()));

                        if (docId < 0)
                        {
                            continue;
                        }
                    }

                    result.Add(docId, new Hubble.Core.Query.DocumentResult(docId));
                }

                System.Data.DataSet ds;

                if (string.IsNullOrEmpty(where))
                {
                    ds = sqlData.QuerySql(string.Format("select count(*) cnt from {0}",
                                                        Table.DBTableName));
                }
                else
                {
                    ds = sqlData.QuerySql(string.Format("select count(*) cnt from {0} where {1}",
                                                        Table.DBTableName, where));
                }

                result.RelTotalCount = int.Parse(ds.Tables[0].Rows[0][0].ToString());
            }

            return(result);
        }