コード例 #1
0
 public override string buildDropStatement(QueryBuilder qbl)
 {
     string wSql = "drop ";
     wSql += ((AField)qbl.getFieldList()[0]).value;
     wSql += " " + ((QueryBuilder.SOURCEBINDING)qbl.getSourceList()[0]).sourceObject + ";";
     return wSql;
 }
コード例 #2
0
ファイル: TableReader.cs プロジェクト: andrewjc/sqlconsole
        public bool Bind(QueryBuilder qbl)
        {
            this._queryObject = qbl;
            this._dataReader = qbl.getDatabaseObject().getResult(this._queryObject);
            ATable srcTable = qbl.getDatabaseObject().GetTableCache().getCachedTable(((ATable)((QueryBuilder.SOURCEBINDING)qbl.getSourceList()[0]).sourceObject).name);

            /*
            // remove redundant fields from the field list.
            if (srcTable.getFieldList().Count != this._queryObject.getFieldList().Count)
            {
                object[] test = utils.arrayIntersect(this._queryObject.getFieldList().ToArray(), srcTable.getFieldList().ToArray());
                this.setFieldList(new System.Collections.ArrayList(test));
            }
            else
                this.setFieldList(srcTable.getFieldList());
             * */
            this.setFieldList(this._queryObject.getFieldList());
            this.name = srcTable.name;
            this.BindData(qbl.getFieldList());
            return false;
        }
コード例 #3
0
        public override string buildDeleteStatement(QueryBuilder qbl)
        {
            string wSql = "delete";
            wSql += " from ";

            // sources
            foreach (QueryBuilder.SOURCEBINDING src in qbl.getSourceList())
            {
                if (src.bindType == 0)
                {
                    // This is our main table name
                    wSql += ((ATable)src.sourceObject).name;
                    if ((((ATable)src.sourceObject).alias != null) && (((ATable)src.sourceObject).alias.Length > 0))
                        wSql += " " + ((ATable)src.sourceObject).alias;
                }
            }

            // conditional
            if ((qbl.conditionalString != null) && (qbl.conditionalString.Length > 0))
                wSql += " where " + qbl.conditionalString;

            return wSql;
        }
コード例 #4
0
ファイル: ProviderACCESS.cs プロジェクト: andrewjc/sqlconsole
        public override string buildInsertStatement(QueryBuilder qbl)
        {
            string tblname = ((string)((QueryBuilder.SOURCEBINDING)qbl.getSourceList()[0]).sourceObject);
            ATable tblObj = qbl.getDatabaseObject().GetTableCache().getCachedTable(tblname);
            string wSql = "insert into " + tblname + " ";
            string fieldList = "";
            string valueList = "";
            ABSTRACTDATATYPES fieldType;

            // If our chosen table object does not have an autoincrementing primary key/id field
            // we can check for that here.

            // Build up field list and value list.
            foreach (AField field in qbl.getFieldList())
            {
                fieldList += field.name;
                if (field != qbl.getFieldList()[qbl.getFieldList().Count - 1]) fieldList += ",";

                fieldType = tblObj.getFieldByName(field.name).type;

                if (fieldType == ABSTRACTDATATYPES.ABool)
                {
                    if (((string)field.value == "true") || ((bool)field.value == true) || ((int)field.value == 1) || ((string)field.value == "1") || ((string)field.value == "yes"))
                    {
                        valueList += "1";
                    }

                }
                else if (fieldType == ABSTRACTDATATYPES.AForeignTable)
                {
                    // This is tricky, we have to look up the value type from the
                    // referencing table's file.
                }
                else if (fieldType == ABSTRACTDATATYPES.ASmallInteger)
                {
                    valueList += field.value;
                }
                else if (fieldType == ABSTRACTDATATYPES.AString)
                {
                    valueList += "'" + field.value + "'";
                }

                if (field != qbl.getFieldList()[qbl.getFieldList().Count - 1]) valueList += ",";
            }
            wSql += "(";
            wSql += fieldList;
            wSql += ") VALUES (";
            wSql += valueList;
            wSql += ");";
            return wSql;
        }
コード例 #5
0
ファイル: ProviderACCESS.cs プロジェクト: andrewjc/sqlconsole
        public override string buildSelectStatement(QueryBuilder qbl)
        {
            string wSql = "select ";
            // fields in selection.
            string wGrouping = "";
            foreach (AField field in qbl.getFieldList())
            {
                string wFieldstr;
                wFieldstr = field.owner + "." + field.name;
                if (field.function != 0)
                {
                    if (field.function == ABSTRACTAGGREGATE.Count)
                        wFieldstr = "Count(" + wFieldstr + ")";
                    else if (field.function == ABSTRACTAGGREGATE.Avg)
                        wFieldstr = "Avg(" + wFieldstr + ")";
                    else if (field.function == ABSTRACTAGGREGATE.DistinctCount)
                        wFieldstr = "Count(Distinct " + wFieldstr + ")";
                    else if (field.function == ABSTRACTAGGREGATE.Max)
                        wFieldstr = "Max(" + wFieldstr + ")";
                    else if (field.function == ABSTRACTAGGREGATE.Min)
                        wFieldstr = "Min(" + wFieldstr + ")";
                    else if (field.function == ABSTRACTAGGREGATE.Sum)
                        wFieldstr = "Sum(" + wFieldstr + ")";
                }
                wSql += wFieldstr;
                if (field != qbl.getFieldList()[qbl.getFieldList().Count - 1])
                    wSql += ", ";
                if (field.GroupBy == true)
                {
                    wGrouping += " " + field.owner + "." + field.name;
                    if (field != qbl.getFieldList()[qbl.getFieldList().Count - 1])
                        wGrouping += ", ";
                }
            }
            wSql += " from ";

            // sources
            foreach (QueryBuilder.SOURCEBINDING src in qbl.getSourceList())
            {
                if (src.bindType == 0)
                {
                    // This is our main table name
                    wSql += ((ATable)src.sourceObject).name;
                    if ((((ATable)src.sourceObject).alias != null) && (((ATable)src.sourceObject).alias.Length > 0))
                        wSql += " " + ((ATable)src.sourceObject).alias;
                }
                else if (src.bindType == ABSTRACTSOURCEBINDTYPES.INNERJOIN)
                {

                    wSql += " INNER JOIN " + ((ATable)src.sourceObject).name;
                    if ((((ATable)src.sourceObject).alias != null) && (((ATable)src.sourceObject).alias.Length > 0))
                    {
                        wSql += " " + ((ATable)src.sourceObject).alias;
                        wSql += " on " + ((ATable)src.boundSource).alias + "." + src.srcMatchField + "=" + ((ATable)src.sourceObject).alias + "." + src.dstMatchField + " ";
                    }
                    else
                    {
                        wSql += " on " + ((ATable)src.boundSource).name + "." + src.srcMatchField + "=" + ((ATable)src.sourceObject).name + "." + src.dstMatchField + " ";
                    }

                }
            }

            // conditional
            if ((qbl.conditionalString != null) && (qbl.conditionalString.Length > 0))
                wSql += " where " + qbl.conditionalString + " ";

            // group by
            if (wGrouping.Trim() != "")
            {
                wGrouping = wGrouping.Trim();
                if (wGrouping[wGrouping.Length - 1].Equals(',') == true)
                    wGrouping = wGrouping.Substring(0, wGrouping.Length - 1);
                wSql += " group by " + wGrouping;
            }

            return wSql;
        }
コード例 #6
0
 public abstract string buildSelectStatement(QueryBuilder qbl);
コード例 #7
0
 public abstract string buildUpdateStatement(QueryBuilder qbl);
コード例 #8
0
        static void selectQueryHandler(ref QueryBuilder newQuery, string sqlStr)
        {
            string[] tokenList = new string[] { "select", "from", "inner join",  "where", "group by", "order by", "limit" };
            string tmp = "";
            for(int i=0;i<=tokenList.Length-1;i++) {
                tmp = tokenList[i];
                sqlStr = sqlStr.Replace(tmp, "[" + tmp + "]");
                tokenList[i]="["+tmp+"]";
            }
            System.Collections.ArrayList tokenised = new System.Collections.ArrayList();
            newQuery.setType(ABSTRACTQUERYTYPES.SelectQuery);
            newQuery.QueryLimit.lStart = -1;
            newQuery.QueryLimit.lLimit = -1;

            // build the token list
            tokenised = SQLConsole.Data.utils.tokenizeList(sqlStr, tokenList);

            foreach (tokenItem tokenitem in tokenised)
            {
                string tokenString = tokenitem.tokenValue;
                string comString = tokenitem.tokenName;

                string paramString = tokenString.Substring(tokenString.IndexOf(tokenitem.tokenName) + tokenitem.tokenName.Length + 1).Trim();

                if(paramString.Length > 10)
                if (paramString.Trim().ToLower().Substring(0, "distinct".Length) == "distinct")
                {
                    newQuery.distinct = true;
                    paramString = paramString.Trim().Substring("distinct ".Length);
                }

                //string comString = tokenString.Substring(0, tokenString.IndexOf(" ")).Trim();
                //string paramString = tokenString.Substring(tokenString.IndexOf(" ") + 1).Trim();
                string tableAlias = ""; string tablename = "";
                string[] fieldList;
                switch (comString)
                {
                    case "select":
                        string[] fieldnames = paramString.Split(',');
                        string realname = "";
                        string agg;
                        ABSTRACTAGGREGATE aggMode = ABSTRACTAGGREGATE.None;
                        foreach (string field in fieldnames)
                        {
                            realname = field.Trim();
                            if (field.IndexOf(")") > 0)
                            {
                                realname = field.Substring(field.IndexOf("(") + 1, field.IndexOf(")") - field.IndexOf("(") - 1);
                                realname = realname.Trim();
                                agg = field.Substring(0, field.IndexOf("(") - 1);
                                switch (agg)
                                {
                                    case "count":
                                        aggMode = ABSTRACTAGGREGATE.Count;
                                        break;
                                    case "max":
                                        aggMode = ABSTRACTAGGREGATE.Max;
                                        break;
                                    case "min":
                                        aggMode = ABSTRACTAGGREGATE.Min;
                                        break;
                                }
                            }
                            if (realname.IndexOf(".") > 0)
                                newQuery.addField(new AField(realname.Substring(realname.IndexOf(".") + 1), null, realname.Substring(0, realname.IndexOf("."))));
                            else
                            {
                                if (realname == "*")
                                {
                                    // deal with this seperately.
                                    newQuery.addField(new AField(realname, null));
                                }
                                else
                                {
                                    // Work out what source this field belongs to...
                                    string pureName = realname.Substring(realname.IndexOf(".") + 1);

                                    string[] sources = {};
                                    for (int i = 0; i < tokenised.Count; i++)
                                    {
                                        if (((tokenItem)tokenised[i]).tokenName == "from")
                                        {
                                            string sourcesList = ((tokenItem)tokenised[i]).tokenValue;
                                            sourcesList = sourcesList.Substring(sourcesList.IndexOf(((tokenItem)tokenised[i]).tokenName) + ((tokenItem)tokenised[i]).tokenName.Length + 1);
                                            sourcesList.Trim();
                                            string[] sources2 = sourcesList.Split(',');
                                            sources = (string[])utils.ConcatenateArrays(sources, sources2);
                                        }
                                        else if (((tokenItem)tokenised[i]).tokenName == "inner join") {
                                            string sourcesList = ((tokenItem)tokenised[i]).tokenValue;
                                            sourcesList = sourcesList.Substring(sourcesList.IndexOf(((tokenItem)tokenised[i]).tokenName) + ((tokenItem)tokenised[i]).tokenName.Length + 1);
                                            sourcesList = sourcesList.Substring(0, sourcesList.IndexOf(" on "));
                                            sourcesList.Trim();
                                            string[] sources2 = { sourcesList };
                                            sources = (string[])utils.ConcatenateArrays(sources, sources2);
                                        }
                                        else if (((tokenItem)tokenised[i]).tokenName == "outer join") {
                                            string sourcesList = ((tokenItem)tokenised[i]).tokenValue;
                                            sourcesList = sourcesList.Substring(sourcesList.IndexOf(((tokenItem)tokenised[i]).tokenName) + ((tokenItem)tokenised[i]).tokenName.Length + 1);
                                            sourcesList = sourcesList.Substring(0, sourcesList.IndexOf(" on "));
                                            sourcesList.Trim();
                                            string[] sources2 = { sourcesList };
                                            sources = (string[])utils.ConcatenateArrays(sources, sources2);
                                        }
                                        else if (((tokenItem)tokenised[i]).tokenName == "join") {
                                            string sourcesList = ((tokenItem)tokenised[i]).tokenValue;
                                            sourcesList = sourcesList.Substring(sourcesList.IndexOf(((tokenItem)tokenised[i]).tokenName) + ((tokenItem)tokenised[i]).tokenName.Length + 1);

                                            if(sourcesList.IndexOf(" on ") > 0)
                                                sourcesList = sourcesList.Substring(0, sourcesList.IndexOf(" on "));

                                            sourcesList.Trim();
                                            string[] sources2 = { sourcesList };
                                            sources = (string[])utils.ConcatenateArrays(sources, sources2);
                                        }
                                    }
                                    AField newField = null;
                                    foreach (string source in sources)
                                    {
                                        ATable src = null;

                                        if (source.IndexOf(" ") > 0)
                                            src = newQuery.getDatabaseObject().getTableObject(source.Substring(0, source.IndexOf(" ")));
                                        else
                                            src = newQuery.getDatabaseObject().getTableObject(source);
                                        if (src == null)
                                            throw (new Exception("Bad table name exception"));
                                        if (src.getFieldByName(pureName) != null)
                                        {
                                            if (newField != null) throw (new Exception("Ambiguous field name: " + realname + ". Field could belong to more than one source table."));

                                            newField = src.getFieldByName(pureName);
                                            if (source.IndexOf(" ") > 0)
                                            {
                                                newField.owner = source.Substring(source.IndexOf(" ")+1);
                                            }
                                        }

                                    }

                                    if (newField == null)
                                        throw (new Exception("Unable to determine source for field: " + realname));
                                    newQuery.addField(newField);

                                    //newQuery.addField(new AField(realname, null, (ATable)((QueryBuilder.SOURCEBINDING)newQuery.getSourceList()[0]).sourceObject));
                                    //newQuery.addField(new AField(realname,null,""));
                                }

                            }
                            if (realname.IndexOf(".") > 0)
                                newQuery.getField(realname.Substring(realname.IndexOf(".") + 1), realname.Substring(0, realname.IndexOf("."))).function = aggMode;
                            else
                                newQuery.getField(realname).function = aggMode;
                        }

                        break;
                    case "distinct":
                        newQuery.distinct = true;
                        break;
                    case "from":

                        if (paramString.IndexOf(' ') != -1) {
                            tableAlias = paramString.Substring(paramString.IndexOf(' ') + 1);
                            tablename = paramString.Substring(0, paramString.IndexOf(' '));
                        }
                        else
                            tablename = paramString.Trim();
                        newQuery.addSource(new ATable(tablename, tableAlias));
                        break;
                    case "inner join":
                        System.Collections.ArrayList joinList = new System.Collections.ArrayList();
                        paramString = "inner join " + paramString;
                        if (paramString.ToLower().IndexOf("inner join", "inner join".Length + 1) >= 2)
                        {
                        while (paramString.Length >= 0)
                        {
                            string tmp2 = paramString;
                            if (paramString.ToLower().IndexOf("inner join", "inner join".Length + 1) >= 2)
                                tmp2 = paramString.ToLower().Substring(0, paramString.ToLower().IndexOf("inner join", "inner join".Length + 2)).Trim();
                            else
                            {
                                joinList.Add(tmp2);
                                break;
                            }
                            joinList.Add(tmp2);
                            paramString = paramString.ToLower().Substring(paramString.ToLower().IndexOf("inner join", "inner join".Length + 2)).Trim();

                        }
                        }
                        else
                        {
                            joinList.Add(paramString);
                        }

                        foreach (string tmpstr in joinList)
                        {
                            paramString = tmpstr.Substring("inner join".Length+1);
                            // inner join anothertable on sjlieglij=sliejgil
                            string lTmp = paramString.Substring(0, paramString.IndexOf(" on "));
                            if (lTmp.IndexOf(' ') != -1)
                            {
                                tableAlias = lTmp.Substring(lTmp.IndexOf(' ')).Trim();
                                tablename = lTmp.Substring(0, lTmp.IndexOf(' ')).Trim();
                            }
                            else
                                tablename = lTmp.Trim();

                            lTmp = paramString.Substring(paramString.IndexOf(" on") + " on".Length + 1);
                            string srcTable = ""; string dstTable = "";
                            string srcField = ""; string dstField = "";

                            srcTable = lTmp.Substring(0, lTmp.IndexOf('='));
                            dstTable = lTmp.Substring(lTmp.IndexOf('=') + 1);
                            if (srcTable.IndexOf('.') > 0)
                            {
                                // Assume specifed table.
                                srcField = srcTable.Substring(srcTable.IndexOf(".") + 1);
                                srcTable = srcTable.Substring(0, srcTable.IndexOf("."));
                            }
                            if (dstTable.IndexOf('.') > 0)
                            {
                                // Assume specifed table.
                                dstField = dstTable.Substring(dstTable.IndexOf(".") + 1);
                                dstTable = dstTable.Substring(0, dstTable.IndexOf("."));
                            }

                            newQuery.addSource(new ATable(tablename, tableAlias), ABSTRACTSOURCEBINDTYPES.INNERJOIN, ((QueryBuilder.SOURCEBINDING)newQuery.getSourceList()[newQuery.getSourceList().Count - 1]).sourceObject, srcField, dstField);
                        }
                        //newQuery.addSource(new ATable(tablename, tableAlias), ABSTRACTSOURCEBINDTYPES.INNERJOIN, null, lTmp.Substring(0, lTmp.IndexOf('=')), lTmp.Substring(lTmp.IndexOf('=')+1));
                        break;

                    case "join":
                        joinList = new System.Collections.ArrayList();
                        paramString = "join " + paramString;
                        if (paramString.ToLower().IndexOf("join", "join".Length + 1) >= 2)
                        {
                            while (paramString.Length >= 0)
                            {
                                string tmp2 = paramString;
                                if (paramString.ToLower().IndexOf("join", "join".Length + 1) >= 2)
                                    tmp2 = paramString.ToLower().Substring(0, paramString.ToLower().IndexOf("join", "join".Length + 2)).Trim();
                                else
                                {
                                    joinList.Add(tmp2);
                                    break;
                                }
                                joinList.Add(tmp2);
                                paramString = paramString.ToLower().Substring(paramString.ToLower().IndexOf("join", "join".Length + 2)).Trim();

                            }
                        }
                        else
                        {
                            joinList.Add(paramString);
                        }

                        foreach (string tmpstr in joinList)
                        {
                            paramString = tmpstr.Substring("join".Length + 1);
                            // inner join anothertable on sjlieglij=sliejgil
                            string lTmp = paramString.Substring(0, paramString.IndexOf(" on "));
                            if (lTmp.IndexOf(' ') != -1)
                            {
                                tableAlias = lTmp.Substring(lTmp.IndexOf(' ')).Trim();
                                tablename = lTmp.Substring(0, lTmp.IndexOf(' ')).Trim();
                            }
                            else
                                tablename = lTmp.Trim();

                            lTmp = paramString.Substring(paramString.IndexOf(" on") + " on".Length + 1);
                            string srcTable = ""; string dstTable = "";
                            string srcField = ""; string dstField = "";

                            srcTable = lTmp.Substring(0, lTmp.IndexOf('='));
                            dstTable = lTmp.Substring(lTmp.IndexOf('=') + 1);
                            if (srcTable.IndexOf('.') > 0)
                            {
                                // Assume specifed table.
                                srcField = srcTable.Substring(srcTable.IndexOf(".") + 1);
                                srcTable = srcTable.Substring(0, srcTable.IndexOf("."));
                            }
                            if (dstTable.IndexOf('.') > 0)
                            {
                                // Assume specifed table.
                                dstField = dstTable.Substring(dstTable.IndexOf(".") + 1);
                                dstTable = dstTable.Substring(0, dstTable.IndexOf("."));
                            }

                            newQuery.addSource(new ATable(tablename, tableAlias), ABSTRACTSOURCEBINDTYPES.INNERJOIN, ((QueryBuilder.SOURCEBINDING)newQuery.getSourceList()[newQuery.getSourceList().Count - 1]).sourceObject, srcField, dstField);
                        }
                        //newQuery.addSource(new ATable(tablename, tableAlias), ABSTRACTSOURCEBINDTYPES.INNERJOIN, null, lTmp.Substring(0, lTmp.IndexOf('=')), lTmp.Substring(lTmp.IndexOf('=')+1));
                        break;

                    case "where":
                        // where b.id = sjilegj, b.Name = fjff
                        // where b.id = silegjlsg and
                        newQuery.conditionalString = paramString;
                        break;
                    case "group by":
                        fieldList = paramString.Split(',');
                        foreach(string ff in fieldList) {
                            newQuery.getField(ff).GroupBy = true;
                        }
                        break;
                    case "order by":
                        fieldList = paramString.Split(',');

                        foreach (string ff in fieldList)
                        {
                            /* patch...
                            bugfix: orderby where the field is not in the select field list
                            * ie: select * from [table] order by field
                            */
                            string fname = null;
                            string fOrderType = null;
                            if (ff.IndexOf(' ') > 0)
                            {
                                ff.Trim();
                                fname = ff.Substring(0, ff.IndexOf(' '));
                                fOrderType = ff.Substring(ff.IndexOf(' ') + 1);
                            }
                            else
                                fname = ff;

                            if (newQuery.getField(fname) == null)
                            {
                                if (fname.IndexOf(".") > 0)
                                {
                                    string tblName = ((ATable)newQuery.getSourceTableByAlias(fname.Substring(0, fname.IndexOf(".")))).name;
                                    AField fld = newQuery.getDatabaseObject().getTableObject(tblName).getFieldByName((fname.Substring(fname.IndexOf(".") + 1)));
                                    fld.owner = fname.Substring(0, fname.IndexOf("."));
                                    newQuery.addField(fld);
                                }
                                else
                                newQuery.addField(newQuery.getDatabaseObject().GetTableCache().getCachedTable(((ATable)((QueryBuilder.SOURCEBINDING)newQuery.getSourceList()[0]).sourceObject).name).getFieldByName(fname));
                            }
                            if(fOrderType != null) {
                                if (fOrderType.ToLower() == "asc")
                                    newQuery.getField(fname).OrderBy = ABSTRACTORDERTYPE.Ascending;
                                else
                                    newQuery.getField(fname).OrderBy = ABSTRACTORDERTYPE.Descending;
                            }
                            else { //default to order by asc
                                if (fname.IndexOf(".")>0)
                                {
                                    fname = fname.Substring(fname.IndexOf(".")+1);
                                }
                                if (fname == newQuery.getField(fname).name)
                                {
                                    newQuery.getField(fname).OrderBy = ABSTRACTORDERTYPE.Ascending;
                                }
                            }
                        }
                        break;
                    case "limit":
                        newQuery.QueryLimit.lStart = 0;
                        if (paramString.IndexOf(',') > 0)
                        {
                            // format limit 0,5
                            // format limit offset, count
                            newQuery.QueryLimit.lStart = Convert.ToInt32(paramString.Substring(0, paramString.IndexOf(',')));
                            newQuery.QueryLimit.lLimit = Convert.ToInt32(paramString.Substring(paramString.IndexOf(',') + 1));
                        }
                        else
                            newQuery.QueryLimit.lLimit = Convert.ToInt32(paramString);
                        break;
                }
            }
        }
コード例 #9
0
        static void insertQueryHandler(ref QueryBuilder newQuery, string sqlStr)
        {
            try
            {
                newQuery.setType(ABSTRACTQUERYTYPES.InsertQuery);
                string[] tokens = sqlStr.Split(' ');
                System.Collections.Queue fieldQueue = new System.Collections.Queue(15);
                for (int i = 0; i < tokens.Length; i++)
                {
                    string tokenPeek = tokens[i];
                    if (tokens[i].Equals("into")) //Next token will be the tablename
                        newQuery.addSource(tokens[++i]);
                    if ((tokens[i].Equals("(")) && (!tokens[i - 2].Equals("values")))
                    { //fieldlist
                        // process fieldlist
                        string fieldname;
                        i++;    //just move forward to the tagset.
                        while (!tokens[i].Equals(")"))
                        {
                            fieldname = tokens[i++];
                            //if (fieldname.Trim().Substring(fieldname.Length - 1).Equals(","))
                            //    fieldname = fieldname.Trim().Substring(0, fieldname.Trim().Length - 1);
                            foreach(string field in fieldname.Split(','))
                                if(field.Trim().Length > 0)
                            fieldQueue.Enqueue(field);
                        }

                        string test = tokens[i + 1];

                    }
                    else if ((tokens[i + 1].Equals("(")) && (tokens[i].ToLower().Equals("values")))
                    { //valuelist
                        // process valuelist
                        i++; i++;
                        string restOfString = "";
                        while (i < tokens.Length)
                            restOfString += tokens[i++] + " ";

                        int strquoteon = 0;
                        string fieldVal = "";
                        bool quotedType = false;
                        for (int x = 0; x < restOfString.Length; x++)
                        {
                            if ((restOfString[x].Equals('\'')) && (strquoteon == 0))
                            {
                                strquoteon = 1;
                                quotedType = true;
                                //x++;    //skip the quote
                                fieldVal = "";

                                //fieldVal += restOfString[x];
                            }
                            else if ((strquoteon == 0) && ((restOfString[x].Equals(',')) || (restOfString[x].Equals(')'))))
                            {
                                string fieldname = (string)fieldQueue.Dequeue();
                                // Make sure we're not quoting.

                                newQuery.addField(new AField(fieldname, fieldVal.Trim()));
                                fieldVal = "";
                                quotedType = false;
                            }
                            else if (x > 0)
                            {
                                if (restOfString[x].Equals('\'') && restOfString[x + 1].Equals('\''))
                                {
                                    // escaped quote
                                    fieldVal += "''";
                                    x = x + 1;
                                }
                                // case for if it appears like an
                                // escaped  quote, but it is not
                                // eg: '\\' as the value

                                else if (restOfString[x].Equals('\\') && restOfString[x + 1].Equals('\'') && !restOfString[x - 1].Equals('\''))
                                {
                                    // escaped quote
                                    fieldVal += "\\'";
                                    x = x + 1;
                                }
                                else
                                {
                                    if (restOfString[x].Equals('\''))
                                    {
                                        strquoteon = 0;
                                        //fieldVal += restOfString[x];
                                    }
                                    else
                                    {
                                        if (!((strquoteon == 0) && (quotedType == true)))
                                            fieldVal += restOfString[x];
                                    }
                                }

                            }
                            else
                            {

                                fieldVal += restOfString[x];
                            }

                        }

                        break;
                    }
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.Print(e.Message);
                newQuery = null;
            }
        }
コード例 #10
0
        public override string buildSelectStatement(QueryBuilder qbl)
        {
            // Fix for limit code.
            if ((qbl.QueryLimit.lStart == 0) && (qbl.QueryLimit.lLimit == 0))
            {
                qbl.QueryLimit.lStart = -1;
                qbl.QueryLimit.lLimit = -1;
            }

            string wSql = "select ";
            if(qbl.QueryLimit.lStart != -1) {
                wSql += " TOP " + ((int)(qbl.QueryLimit.lStart + qbl.QueryLimit.lLimit)) + " ";
            }
            // fields in selection.
            if (qbl.distinct)
                wSql += " DISTINCT ";
            string wGrouping = "";
            string wOrdering = "";
            string totFieldList = "";
            foreach (AField field in qbl.getFieldList())
            {
                string fieldName = field.name;
                if (field.name[0] == '[') field.name = field.name.Substring(1);
                if (field.name[field.name.Length - 1] == ']') field.name = field.name.Substring(0, field.name.Length - 1);
                fieldName = "[" + field.name + "]";

                //field.owner = ((ATable)((QueryBuilder.SOURCEBINDING)qbl.getSourceList()[0]).sourceObject).name;
                string wFieldstr = null;
                if ((field.owner == null) && (qbl.getSourceList().Count == 1))
                {
                    wFieldstr = fieldName;
                }
                else
                {

                    //if (((ATable)(((QueryBuilder.SOURCEBINDING)qbl.getSourceList()[0]).sourceObject)).name != field.owner)
                    //    wFieldstr = field.owner + "." + ""+field.name+"";

                    System.Collections.ArrayList al = qbl.getSourceList();
                    wFieldstr = "" + fieldName + "";
                    foreach (QueryBuilder.SOURCEBINDING s in al)
                    {
                        if (((ATable)s.sourceObject).alias == field.owner) //logic using table alias
                        {

                            ATable ownr = ((ATable)s.sourceObject);
                            if (ownr.alias != null)
                            {
                                wFieldstr = ownr.alias + "." + "" + fieldName + "";
                                break;
                            }
                            else
                            {
                                wFieldstr = field.owner + "." + "" + fieldName + "";
                                break;
                            }

                        }

                    }
                    //wFieldstr = field.owner + "." + "" + field.name + "";
                }

                if (field.function != 0)
                {
                    if (field.function == ABSTRACTAGGREGATE.Count)
                        wFieldstr = "Count(" + wFieldstr + ")";
                    else if (field.function == ABSTRACTAGGREGATE.Avg)
                        wFieldstr = "Avg(" + wFieldstr + ")";
                    else if (field.function == ABSTRACTAGGREGATE.DistinctCount)
                        wFieldstr = "Count(Distinct " + wFieldstr + ")";
                    else if (field.function == ABSTRACTAGGREGATE.Max)
                        wFieldstr = "Max(" + wFieldstr + ")";
                    else if (field.function == ABSTRACTAGGREGATE.Min)
                        wFieldstr = "Min(" + wFieldstr + ")";
                    else if (field.function == ABSTRACTAGGREGATE.Sum)
                        wFieldstr = "Sum(" + wFieldstr + ")";
                }
                wSql += wFieldstr;
                if (field != qbl.getFieldList()[qbl.getFieldList().Count - 1])
                    wSql += ", ";
                if (field.GroupBy == true)
                {
                    string wFieldGroup = "";
                    if ((field.owner == null) && (qbl.getSourceList().Count == 1))
                        wFieldGroup = field.name;
                    else
                        if (((ATable)(((QueryBuilder.SOURCEBINDING)qbl.getSourceList()[0]).sourceObject)).name != field.owner)
                            wFieldGroup = field.owner + "." + field.name;
                        else
                            wFieldGroup = field.name;
                    wGrouping += " " + wFieldGroup;

                    if (field != qbl.getFieldList()[qbl.getFieldList().Count - 1])
                        wGrouping += ", ";
                }
                if (field.OrderBy != ABSTRACTORDERTYPE.None)
                {
                    string fStatMode = (field.OrderBy == ABSTRACTORDERTYPE.Ascending)?"asc":"desc";
                    string wFieldOrder = "";
                    if ((field.owner == null) && (qbl.getSourceList().Count == 1))
                        wFieldOrder = field.name;
                    else
                        if (((ATable)(((QueryBuilder.SOURCEBINDING)qbl.getSourceList()[0]).sourceObject)).name != field.owner)
                            wFieldOrder = field.owner + "." + field.name;
                        else
                            wFieldOrder = field.name;

                    wOrdering += " " + wFieldOrder + " "+ fStatMode;

                    if (field != qbl.getFieldList()[qbl.getFieldList().Count - 1])
                        wOrdering += ", ";
                }

                totFieldList += wFieldstr;
            }
            wSql += " from ";

            // sources
            foreach (QueryBuilder.SOURCEBINDING src in qbl.getSourceList())
            {
                if (src.bindType == 0)
                {
                    // This is our main table name
                    wSql += ((ATable)src.sourceObject).name;
                    if ((((ATable)src.sourceObject).alias != null) && (((ATable)src.sourceObject).alias.Length > 0))
                        wSql += " " + ((ATable)src.sourceObject).alias;
                }
                else if (src.bindType == ABSTRACTSOURCEBINDTYPES.INNERJOIN)
                {

                    wSql += " INNER JOIN " + ((ATable)src.sourceObject).name;
                    if ((((ATable)src.sourceObject).alias != null) && (((ATable)src.sourceObject).alias.Length > 0))
                    {
                        wSql += " " + ((ATable)src.sourceObject).alias;

                        try
                        {
                            wSql += " on " + ((ATable)src.boundSource).alias + "." + ((String)src.srcMatchField) + "=" + ((ATable)src.sourceObject).alias + "." + ((String)src.dstMatchField) + " ";
                        }
                        catch
                        {
                            wSql += " on " + ((ATable)src.boundSource).alias + "." + ((AField)src.srcMatchField).name + "=" + ((ATable)src.sourceObject).alias + "." + ((AField)src.dstMatchField).name + " ";
                        }
                    }
                    else
                    {
                        wSql += " on " + ((ATable)src.boundSource).name + "." + ((AField)src.srcMatchField).name + "=" + ((ATable)src.sourceObject).name + "." + ((AField)src.dstMatchField).name + " ";
                    }

                }
            }

            // conditional
            if((qbl.conditionalString != null) && (qbl.conditionalString.Length > 0))
            wSql += " where " + qbl.conditionalString + " ";

            // group by
            if (wGrouping.Trim() != "")
            {
                wGrouping = wGrouping.Trim();
               if (wGrouping[wGrouping.Length-1].Equals(',') == true)
                    wGrouping = wGrouping.Substring(0, wGrouping.Length - 1);
                wSql += " group by " + wGrouping;
            }

            // ordering by
            if (wOrdering.Trim() != "")
            {
                wOrdering = wOrdering.Trim();
                if (wOrdering[wOrdering.Length - 1].Equals(',') == true)
                    wOrdering = wOrdering.Substring(0, wOrdering.Length - 1);

            }

            // LIMIT CLAUSE WRAPPER
            /*
            if (qbl.QueryLimit.lStart > -1)
            {
               //string fStatMode = (field.OrderBy == ABSTRACTORDERTYPE.Ascending)?"asc":"desc";

                string wFieldOrder = "";
                if ((qbl.getField("id").owner == null) && (qbl.getSourceList().Count == 1))
                    wFieldOrder = qbl.getField("id").name;
                else
                    if (((ATable)(((QueryBuilder.SOURCEBINDING)qbl.getSourceList()[0]).sourceObject)).name != qbl.getField("id").owner)
                        wFieldOrder = qbl.getField("id").owner + "." + qbl.getField("id").name;
                    else
                        wFieldOrder = qbl.getField("id").name;

               string fOrderingString = wFieldOrder;
               string sqlInner1 = "";
               string sqlInner2 = "";
                if(wOrdering != "") {
                sqlInner2 = String.Format("{0} order by {1} asc, {2}", wSql, fOrderingString, wOrdering);
                }else {
                sqlInner2 = String.Format("{0} order by {1} asc", wSql, fOrderingString); }

                if(wOrdering != "") {
                sqlInner1 = String.Format("select top {0} {1} from ({3}) as newtbl order by {2} desc, {3}", qbl.QueryLimit.lLimit, totFieldList, fOrderingString, sqlInner2, wOrdering);
                }else
                {sqlInner1 = String.Format("select top {0} {1} from ({3}) as newtbl order by {2} desc", qbl.QueryLimit.lLimit, totFieldList, fOrderingString, sqlInner2); }

                if(wOrdering != "")
                wSql = String.Format("select * from ({0}) as newtbl2 order by {1} asc, {2}", sqlInner1, fOrderingString, wOrdering);
                else
                wSql = String.Format("select * from ({0}) as newtbl2 order by {1} asc", sqlInner1, fOrderingString);
            }
             * */
            if(wOrdering != "")
            wSql += " order by " + wOrdering;
            return wSql;
        }
コード例 #11
0
        public override string buildUpdateStatement(QueryBuilder qbl)
        {
            bool skipped = false;

            string tblname = ((object)((QueryBuilder.SOURCEBINDING)qbl.getSourceList()[0]).sourceObject).ToString();

            ATable tblObj = qbl.getDatabaseObject().GetTableCache().getCachedTable(tblname);
            string wSql = "update " + tblname + " set ";
            foreach(AField field in qbl.getFieldList()) {
                skipped = false;
                if (!field.hasModifier(ABSTRACTFIELDMODIFIERS.PrimaryKey))
                // Skip the primary key as we cannot update it.
                {
                    if (tblObj.getFieldByName(field.name).type == ABSTRACTDATATYPES.AString)
                    {
                        wSql += "[" + field.name + "]" + "='" + field.value + "'";
                    }
                    else if (tblObj.getFieldByName(field.name).type == ABSTRACTDATATYPES.ABool)
                    {
                        if (Convert.ToString(field.value) != "" && field.value != null)
                            if(Convert.ToString(field.value).ToLower()=="true")
                                wSql += "[" + field.name + "]" + "=1";
                            else
                                wSql += "[" + field.name + "]" + "=0";
                        else
                            skipped = true;
                    }
                    else if ((tblObj.getFieldByName(field.name).type == ABSTRACTDATATYPES.ALargeInteger)||(tblObj.getFieldByName(field.name).type == ABSTRACTDATATYPES.ASmallInteger))
                        if (Convert.ToString(field.value) != "" && field.value != null)
                            wSql += "[" + field.name + "]" + "=" + field.value;
                        else
                            skipped = true;
                    else if ((tblObj.getFieldByName(field.name).type == ABSTRACTDATATYPES.ADateTime))
                        if (Convert.ToString(field.value) != "" && field.value != null)
                            wSql += "[" + field.name + "]" + "=CONVERT(datetime, '" + field.value+"')";
                        else
                            skipped = true;
                }
                else skipped = true;
                if ((Convert.ToString(field.value) != "" && field.value != null)||(tblObj.getFieldByName(field.name).type == ABSTRACTDATATYPES.AString))
                    if(!skipped)
                    if (field != qbl.getFieldList()[qbl.getFieldList().Count - 1])
                        wSql += ", ";
                    else
                        wSql += " ";
                else
                    wSql += " ";
            }

            // sometimes the algorithm skips... fix it here
            wSql = wSql.Trim();
            if (wSql[wSql.Length - 1] == ',')
            {
                wSql = wSql.Substring(0, wSql.Length - 1);
            }
            if (wSql[wSql.Length - 1] != ' ') wSql += " ";
            if(qbl.conditionalString != null)
            if(qbl.conditionalString.Length > 2)
            wSql += "where " + qbl.conditionalString;
            return wSql;
        }
コード例 #12
0
ファイル: Database.cs プロジェクト: andrewjc/sqlconsole
 public static void QueueItem(QueryBuilder qb)
 {
 }
コード例 #13
0
ファイル: ProviderACCESS.cs プロジェクト: andrewjc/sqlconsole
 public override string buildUpdateStatement(QueryBuilder qbl)
 {
     string tblname = ((string)((QueryBuilder.SOURCEBINDING)qbl.getSourceList()[0]).sourceObject);
     ATable tblObj = qbl.getDatabaseObject().GetTableCache().getCachedTable(tblname);
     string wSql = "update " + tblname + " set ";
     foreach (AField field in qbl.getFieldList())
     {
         if (tblObj.getFieldByName(field.name).type == ABSTRACTDATATYPES.AString)
         {
             wSql += field.name + "='" + field.value + "'";
         }
         else
         {
             wSql += field.name + "=" + field.value;
         }
         if (field != qbl.getFieldList()[qbl.getFieldList().Count - 1])
             wSql += ", ";
         else
             wSql += " ";
     }
     wSql += "where " + qbl.conditionalString;
     return wSql;
 }
コード例 #14
0
ファイル: Database.cs プロジェクト: andrewjc/sqlconsole
 public System.Data.Odbc.OdbcDataReader getResult(QueryBuilder query)
 {
     if (this.GetDatabaseProvider() != null)
     {
         query.autoExecute = false;
         string compiledSQL = query.Compile();
         try
         {
             return this._dbProvider.getResult(compiledSQL);
         }
         catch (Exception e)
         {
             if ((e.Message.IndexOf("pending local transaction") > 0) | (e.Message.IndexOf("busy with results for another") > 0)) //quick fix.
             {
                 return this._dbProvider.getNewRawConnection().getResult(compiledSQL);
             }
             else
             {
                 //throw (e);
             }
         }
     }
     else
     {
         throw (new Exception("No database provider has been implemented for this datasource"));
     }
     return null;
 }
コード例 #15
0
ファイル: ProviderSQLITE.cs プロジェクト: andrewjc/sqlconsole
        public override string buildInsertStatement(QueryBuilder qbl)
        {
            string tblname = (Convert.ToString(((QueryBuilder.SOURCEBINDING)qbl.getSourceList()[0]).sourceObject));
            ATable tblObj = qbl.getDatabaseObject().GetTableCache().getCachedTable(tblname);
            string wSql = "insert into " + tblname + " ";
            string fieldList = "";
            string valueList = "";
            ABSTRACTDATATYPES fieldType;

            // If our chosen table object does not have an autoincrementing primary key/id field
            // we can check for that here.

            // Build up field list and value list.
            foreach (AField field in qbl.getFieldList())
            {
                fieldList += field.name;
                if (field != qbl.getFieldList()[qbl.getFieldList().Count - 1]) fieldList += ",";

                fieldType = tblObj.getFieldByName(field.name).type;

                if (fieldType == ABSTRACTDATATYPES.ABool)
                {
                    if (((string)field.value == "true") || ((bool)field.value == true) || ((int)field.value == 1) || ((string)field.value == "1") || ((string)field.value == "yes"))
                    {
                        valueList += "1";
                    }

                }
                else if (fieldType == ABSTRACTDATATYPES.AForeignTable)
                {
                    // This is tricky, we have to look up the value type from the
                    // referencing table's file.
                }
                else if (fieldType == ABSTRACTDATATYPES.ASmallInteger)
                {
                    // Deal with blank values and default val...
                    if (field.defaultval == null)
                    {
                        if (field.hasModifier(ABSTRACTFIELDMODIFIERS.NotNull))
                            field.defaultval = 0;
                        else
                            field.defaultval = "NULL";
                    }
                    if (field.value == null || Convert.ToString(field.value) == "")
                        field.value = field.defaultval;

                    valueList += field.value;
                }
                else if (fieldType == ABSTRACTDATATYPES.AString)
                {
                    valueList += "'" + field.value + "'";
                }
                else if (fieldType == ABSTRACTDATATYPES.ADateTime)
                {
                    if (Convert.ToString(field.value).ToLower().Trim() == "now()")
                    {
                        valueList += field.value;
                    }
                    else
                    {
                        valueList += "'" + field.value + "'";
                    }
                }
                else
                {
                    if (field.value == null || Convert.ToString(field.value)== "")
                        field.value = field.defaultval;
                    valueList += field.value;
                }

                if (field != qbl.getFieldList()[qbl.getFieldList().Count - 1]) valueList += ",";
            }
            wSql += "(";
            wSql += fieldList;
            wSql += ") VALUES (";
            wSql += valueList;
            wSql += ");";
            return wSql;
        }
コード例 #16
0
        static void deleteQueryHandler(ref QueryBuilder newQuery, string sqlStr)
        {
            // delete from table where silegjilsejgil=seiljsiegljseg
            // delete from table;
            string[] tokenList = new string[] { "delete", "where" };
            System.Collections.ArrayList tokenised = new System.Collections.ArrayList();
            newQuery.setType(ABSTRACTQUERYTYPES.DeleteQuery);

            // build the token list
            tokenised = SQLConsole.Data.utils.tokenizeList(sqlStr, tokenList);
            foreach (tokenItem tokenitem in tokenised)
            {
                string tokenString = tokenitem.tokenValue;
                string comString = tokenitem.tokenName;
                string paramString = tokenString.Substring(tokenString.IndexOf(comString) + comString.Length + 1).Trim();
                switch (comString)
                {
                    case "delete":
                        paramString = paramString.Substring("from ".Length);
                        string tableAlias = ""; string tablename = "";
                        if (paramString.IndexOf(' ') != -1)
                        {
                            tableAlias = paramString.Substring(paramString.IndexOf(' ') + 1);
                            tablename = paramString.Substring(0, paramString.IndexOf(' '));
                        }
                        else
                            tablename = paramString.Trim();
                        newQuery.addSource(new ATable(tablename, tableAlias));
                        break;
                    case "where":
                        newQuery.conditionalString = paramString;
                        break;
                }
            }
        }
コード例 #17
0
 static void alterQueryHandler(ref QueryBuilder newQuery, string sqlStr)
 {
     newQuery.setType(ABSTRACTQUERYTYPES.AlterQuery);
     // alter table testTable drop column password
     newQuery.conditionalString = sqlStr;
 }
コード例 #18
0
        static void updateQueryHandler(ref QueryBuilder newQuery, string sqlStr)
        {
            string[] tokenList = new string[] { "update", "set", "where", "nullterminator" };
            System.Collections.ArrayList tokenised = new System.Collections.ArrayList();

            newQuery.setType(ABSTRACTQUERYTYPES.UpdateQuery);

            // build the token list
            tokenised = SQLConsole.Data.utils.tokenizeList(sqlStr, tokenList);
            foreach (tokenItem tokenitem in tokenised)
            {
                string tokenString = tokenitem.tokenValue;

                string comString = tokenitem.tokenName;
                string paramString = tokenString.Substring(tokenString.IndexOf(comString) + comString.Length + 1).Trim();

                //string comString = tokenString.Substring(0, tokenString.IndexOf(" ")).Trim();
                //string paramString = tokenString.Substring(tokenString.IndexOf(" ") + 1).Trim();
                string tablename = "";
                switch (comString)
                {
                    case "update":
                        tablename = paramString;
                        newQuery.addSource(tablename);
                        break;
                    case "set":
                        // set something=something, ilsjeiljg='sejil=gji\'seg', sjegiiljseg=slegijl

                        string curKey = ""; string curVal = "";
                        bool strQuoting = false;
                        bool doKey = false;
                        bool doAdd = false;
                        for (int i = 0; i <= paramString.Length; i++)
                        {
                            if (!doKey)
                            {
                                if (i < paramString.Length)
                                {
                                    if (paramString[i].Equals('='))
                                        doKey = true;

                                    else if (!paramString[i].Equals(',') && !paramString[i].Equals(' '))
                                        curKey += paramString[i];
                                }
                            }
                            else
                            {
                                if (i < paramString.Length)
                                {
                                    if ((paramString[i].Equals('\'')) && (!paramString[i - 1].Equals('\\')))
                                    {
                                        if (strQuoting)
                                        {
                                            strQuoting = false;
                                            doAdd = true;
                                        }
                                        else
                                        {
                                            strQuoting = true; i++;
                                        }
                                    }
                                    else if ((paramString[i].Equals(',')) && (strQuoting == false))
                                    {
                                        doAdd = true;
                                        i++;
                                    }
                                }
                                else doAdd = true;
                                if (doAdd)
                                {
                                    string fieldname = curKey;
                                    string fieldValue = curVal;
                                    strQuoting = false;
                                    doAdd = false;
                                    doKey = false;
                                    newQuery.addField(new AField(fieldname.Trim(), fieldValue.Trim()));
                                    curKey = "";
                                    curVal = "";
                                }
                                else
                                {
                                    curVal += paramString[i];
                                }

                            }

                        }

                        break;
                    case "where":
                        newQuery.conditionalString = paramString;
                        break;
                }
            }
        }
コード例 #19
0
 public bool Queue(QueryBuilder qbl)
 {
     qbl.autoExecute = false;
     return Queue(qbl.Compile());
 }
コード例 #20
0
ファイル: ProviderMYSQL.cs プロジェクト: andrewjc/sqlconsole
        public override string buildSelectStatement(QueryBuilder qbl)
        {
            string wSql = "select ";
            // fields in selection.
            string wGrouping = "";
            string wOrdering = "";
            foreach (AField field in qbl.getFieldList())
            {
                string wFieldstr;
                if((field.owner == null) && (qbl.getSourceList().Count == 1))
                    wFieldstr = field.name;
                else
                //if (((ATable)(((QueryBuilder.SOURCEBINDING)qbl.getSourceList()[0]).sourceObject)).Name != field.owner)
                    wFieldstr = field.owner + "." + field.name;
                //else
                //    wFieldstr = field.name;
                if (field.function != 0)
                {
                    if (field.function == ABSTRACTAGGREGATE.Count)
                        wFieldstr = "Count(" + wFieldstr + ")";
                    else if (field.function == ABSTRACTAGGREGATE.Avg)
                        wFieldstr = "Avg(" + wFieldstr + ")";
                    else if (field.function == ABSTRACTAGGREGATE.DistinctCount)
                        wFieldstr = "Count(Distinct " + wFieldstr + ")";
                    else if (field.function == ABSTRACTAGGREGATE.Max)
                        wFieldstr = "Max(" + wFieldstr + ")";
                    else if (field.function == ABSTRACTAGGREGATE.Min)
                        wFieldstr = "Min(" + wFieldstr + ")";
                    else if (field.function == ABSTRACTAGGREGATE.Sum)
                        wFieldstr = "Sum(" + wFieldstr + ")";
                }
                wSql += wFieldstr;
                if (field != qbl.getFieldList()[qbl.getFieldList().Count - 1])
                    wSql += ", ";
                if (field.GroupBy == true)
                {
                    wGrouping += " " + field.owner + "." + field.name;
                    if (field != qbl.getFieldList()[qbl.getFieldList().Count - 1])
                        wGrouping += ", ";
                }
                if (field.OrderBy != ABSTRACTORDERTYPE.None)
                {
                    string fStatMode = (field.OrderBy == ABSTRACTORDERTYPE.Ascending) ? "asc" : "desc";
                    string wFieldOrder = "";
                    if ((field.owner == null) && (qbl.getSourceList().Count == 1))
                        wFieldOrder = field.name;
                    else
                        if (((ATable)(((QueryBuilder.SOURCEBINDING)qbl.getSourceList()[0]).sourceObject)).name != field.owner)
                            wFieldOrder = field.owner + "." + field.name;
                        else
                            wFieldOrder = field.name;

                    wOrdering += " " + wFieldOrder + " " + fStatMode;

                    if (field != qbl.getFieldList()[qbl.getFieldList().Count - 1])
                        wOrdering += ", ";
                }
            }
            wSql += " from ";

            // sources
            foreach (QueryBuilder.SOURCEBINDING src in qbl.getSourceList())
            {
                if (src.bindType == 0)
                {
                    // This is our main table name
                    wSql += ((ATable)src.sourceObject).name;
                    if ((((ATable)src.sourceObject).alias != null) && (((ATable)src.sourceObject).alias.Length > 0))
                        wSql += " " + ((ATable)src.sourceObject).alias;
                }
                else if (src.bindType == ABSTRACTSOURCEBINDTYPES.INNERJOIN)
                {

                    wSql += " INNER JOIN " + ((ATable)src.sourceObject).name;
                    if ((((ATable)src.sourceObject).alias != null) && (((ATable)src.sourceObject).alias.Length > 0))
                    {
                        wSql += " " + ((ATable)src.sourceObject).alias;
                        wSql += " on " + ((ATable)src.boundSource).alias + "." + src.srcMatchField + "=" + ((ATable)src.sourceObject).alias + "." + src.dstMatchField + " ";
                    }
                    else
                    {
                        wSql += " on " + ((ATable)src.boundSource).name + "." + src.srcMatchField + "=" + ((ATable)src.sourceObject).name + "." + src.dstMatchField + " ";
                    }

                }
            }

            // conditional
            if(qbl.conditionalString != null)
            if (qbl.conditionalString.Replace(" ", "").ToLower().IndexOf("=null") > 0)
            {
                qbl.conditionalString = qbl.conditionalString.Replace("=", " IS ");
            }

            if((qbl.conditionalString != null) && (qbl.conditionalString.Length > 0))
            wSql += " where " + qbl.conditionalString + " ";

            // group by
            if (wGrouping.Trim() != "")
            {
                wGrouping = wGrouping.Trim();
               if (wGrouping[wGrouping.Length-1].Equals(',') == true)
                    wGrouping = wGrouping.Substring(0, wGrouping.Length - 1);
                wSql += " group by " + wGrouping;
            }
            if (wOrdering.Trim() != "")
            {
                wOrdering = wOrdering.Trim();
                if (wOrdering[wOrdering.Length - 1].Equals(',') == true)
                    wOrdering = wOrdering.Substring(0, wOrdering.Length - 1);
                wSql += " order by " + wOrdering;
            }
            if (qbl.QueryLimit.lStart > -1)
            {
                wSql += " limit " + qbl.QueryLimit.lStart + "," + qbl.QueryLimit.lLimit;
            }
            return wSql;
        }
コード例 #21
0
 public override string buildAlterStatement(QueryBuilder qbl)
 {
     // This *may* be redone... if problems arise.
     return qbl.conditionalString;   //quick hack.
 }
コード例 #22
0
 public abstract string buildAlterStatement(QueryBuilder qbl);
コード例 #23
0
 public abstract string buildDropStatement(QueryBuilder qbl);
コード例 #24
0
 public abstract string buildInsertStatement(QueryBuilder qbl);
コード例 #25
0
        public override string buildInsertStatement(QueryBuilder qbl)
        {
            string tblname = ((string)((QueryBuilder.SOURCEBINDING)qbl.getSourceList()[0]).sourceObject);
            ATable tblObj = qbl.getDatabaseObject().GetTableCache().getCachedTable(tblname);
            if (tblObj == null)
                throw (new Exception("Invalid table specified. Check query spelling and try again."));
            string wSql = "insert into [" + tblname + "] ";
            string fieldList = "";
            string valueList = "";
            ABSTRACTDATATYPES fieldType;

            // If our chosen table object does not have an autoincrementing primary key/id field
            // we can check for that here.

            // Build up field list and value list.
            foreach (AField field in qbl.getFieldList())
            {
                AField refField = qbl.getDatabaseObject().getTableObject(field.owner).getFieldByName(field.name);
                if (tblObj.getFieldByName(field.name) == null)
                {
                    this._dbObj.GetTableCache().rebuildAll();
                    throw(new Exception("Unable to build insert statement. An invalid field was included with the query."));
                }
                fieldList += "["+field.name+"]";
                if (field != qbl.getFieldList()[qbl.getFieldList().Count - 1]) fieldList += ",";

                fieldType = refField.type;

                if (fieldType == ABSTRACTDATATYPES.ABool)
                {
                    string boolRaw = Convert.ToString(field.value).ToLower();
                    bool relFieldValue = (boolRaw == "true" || boolRaw == "1" || boolRaw == "yes") ? true : false;
                    if (relFieldValue)
                    {
                        valueList += "1";
                    }
                    else
                    {
                        valueList += "0";
                    }

                }
                else if (fieldType == ABSTRACTDATATYPES.AForeignTable)
                {
                    // This is tricky, we have to look up the value type from the
                    // referencing table's file.
                }
                else if ((fieldType == ABSTRACTDATATYPES.ASmallInteger) || (fieldType == ABSTRACTDATATYPES.ALargeInteger ))
                {
                    valueList += field.value;
                }
                else if ((fieldType == ABSTRACTDATATYPES.AString) || (fieldType == ABSTRACTDATATYPES.AChar) || (fieldType == ABSTRACTDATATYPES.ADateTime))
                {
                    // convert mysql/csql style quoting to mssql style quoting
                    //field.value = Convert.ToString(field.value).Replace("''", @"\'");

                    // if the value is longer than the allowed nvarchar, then do a CONVERT/cast to text instead.
                    valueList += "'" + field.value + "'";

                }
                else if (fieldType == ABSTRACTDATATYPES.AData)
                {
                    valueList += field.value;
                }
                else
                {
                    // If we get here, then the field type is possibly unsupported.
                    // try to figure out what type of formatting to use.
                    string sValue = field.value != null?Convert.ToString(field.value):"";
                    if (sValue == "")
                    {
                        valueList += "''";
                    }
                    else
                    {
                        if (sValue.Substring(0, 1) == "'")
                        {
                            // convert mysql/csql style quoting to mssql style quoting
                            //field.value = Convert.ToString(field.value).Replace("''", @"\'");
                            valueList += "'" + field.value + "'";
                        }
                        else
                        {
                            valueList += field.value;
                        }
                    }
                }

                if (field != qbl.getFieldList()[qbl.getFieldList().Count - 1]) valueList += ",";
            }
            wSql += "(";
            wSql += fieldList;
            wSql += ") VALUES (";
            wSql += valueList;
            wSql += ");";
            return wSql;
        }
コード例 #26
0
        public static QueryBuilder CreateQueryBuilder(string csql, Database dbObj)
        {
            // This function will create a querybuilder object from commonsql
            QueryBuilder newQuery = new QueryBuilder(dbObj);

            // normalize commonsql:

            int offset = 0;
            int iQuote = 0;
            if (csql.IndexOf(";") <= 0) csql += ";";
            for (int i = 0; i < (csql.Length-offset); i++)
            {
                if (csql[i] == '\'' && iQuote == 0)
                    iQuote = 1;
                else
                    if (csql[i] == '\'' && iQuote == 1)
                    {
                        if(csql[i-1] != '\'' && csql[i-1] != '\\')
                        iQuote = 0;
                    }
                    else if (iQuote == 0)
                    {
                        if ((csql[i + offset].Equals('(') || csql[i + offset].Equals(')')) && (!csql[(i + offset) - 1].Equals(' ')))
                        { // space to the left
                            csql = csql.Insert((i + (offset++)), " ");
                        }
                        if ((csql[i + offset].Equals('(') || csql[i + offset].Equals(')')) && (!csql[(i + offset) + 1].Equals(' ')))
                        { // space to the left
                            csql = csql.Insert((i + (offset++)) + 1, " ");
                        }
                    }
            }

            // endnormalize

            string[] sqlStatements = csql.Split(';');
            foreach (string sqlStr in sqlStatements)
            {
                if (!sqlStr.Trim().Equals(""))
                {
                    //if (sqlStr.Substring(sqlStr.Length - 1).Equals(";"))
                    //    sqlStr = sqlStr.Substring(0, sqlStr.Length - 1);
                    string[] tokens = sqlStr.Split(' ');
                    switch (tokens[0].ToLower())
                    {
                        case "drop":
                            if (tokens[1].ToLower().Equals("table"))
                                newQuery.setType(ABSTRACTQUERYTYPES.DropQuery);

                            if (tokens[1].ToLower().Equals("database"))
                                newQuery.setType(ABSTRACTQUERYTYPES.DropQuery);
                            newQuery.addSource(tokens[2].ToLower());
                            newQuery.addField(new AField("droptype", tokens[1].ToLower()));
                            break;
                        case "insert":
                            insertQueryHandler(ref newQuery, sqlStr);
                            break;
                        case "delete":
                            deleteQueryHandler(ref newQuery, sqlStr);
                            break;
                        case "show":
                            break;
                        case "select":
                            selectQueryHandler(ref newQuery, sqlStr);
                            break;
                        case "update":
                            updateQueryHandler(ref newQuery, sqlStr);
                            break;
                        case "alter":
                            alterQueryHandler(ref newQuery, sqlStr);
                            break;
                    }
                }
            }
            return newQuery;
        }