コード例 #1
0
        /// <summary>Queries the SQL tables for the count of the phrase.</summary>
        /// <remarks>
        /// Queries the SQL tables for the count of the phrase.
        /// Returns -1 if the phrase doesn't exist
        /// </remarks>
        /// <param name="str">: phrase</param>
        /// <returns>: count, if exists. -1 if not.</returns>
        /// <exception cref="Java.Sql.SQLException"/>
        public static long GetCount(string str)
        {
            string query = null;

            try
            {
                Connect();
                str = str.Trim();
                if (str.Contains("'"))
                {
                    str = StringUtils.EscapeString(str, new char[] { '\'' }, '\'');
                }
                int    ngram = str.Split("\\s+").Length;
                string table = tablenamePrefix + ngram;
                if (!ExistsTable(table))
                {
                    return(-1);
                }
                string phrase = EscapeString(str);
                query = "select count from " + table + " where phrase='" + phrase + "';";
                IStatement stmt   = connection.CreateStatement();
                IResultSet result = stmt.ExecuteQuery(query);
                if (result.Next())
                {
                    return(result.GetLong("count"));
                }
                else
                {
                    return(-1);
                }
            }
            catch (SQLException e)
            {
                log.Info("Error getting count for " + str + ". The query was " + query);
                Sharpen.Runtime.PrintStackTrace(e);
                throw new Exception(e);
            }
        }