Esempio n. 1
0
void SQLQuery::parse() {
    string str = "";
    char num[3];
    long int n;
    char option;
    string name;
    char *s, *s0;
    s0 = s = preview_char();
    while (*s) {
        if (*s == '%') {
            s++;
            if (*s == '%') {
                str += *s++;
            } else if (*s >= '0' && *s <= '9') {
                num[0] = *s;
                s++;
                if (*s >= '0' && *s <= '9') {
                    num[1] = *s;
                    num[2] = 0;
                    s++;
                } else {
                    num[1] = 0;
                }
                n = strtol(num,NULL,10);
                option = ' ';
                if (*s == 'q' || *s == 'Q' || *s == 'r' || *s == 'R')
                    option = *s++;
                if (*s == ':') {
                    s++;
                    for (; (*s>='A' && *s<='Z') || *s=='_' || (*s>='a' && *s<='z'); s++) {
                        name += *s;
                    }
                    if (*s == ':') s++;
                    if (n >= (long int)parsed_names.size())
                        parsed_names.insert(parsed_names.end(),
                                            (vector<string>::size_type)(n+1)
                                            - parsed_names.size(), string());
                    parsed_names[n] = name;
                    parsed_nums[name] = n;
                }

                parsed.push_back( SQLParseElement(str,option,n) );
                str = "";
                name = "";
            } else {
                str += '%';
            }
        } else {
            str += *s++;
        }
    }
    parsed.push_back( SQLParseElement(str,' ',-1) );
    delete[] s0;
}
Esempio n. 2
0
void
Query::parse()
{
	std::string str = "";
	char num[4];
	std::string name;
	char *s, *s0;
	s0 = s = preview_char();
	while (*s) {
		if (*s == '%') {
			// Following might be a template parameter declaration...
			s++;
			if (*s == '%') {
				// Doubled percent sign, so insert literal percent sign.
				str += *s++;
			}
			else if (isdigit(*s)) {
				// Number following percent sign, so it signifies a
				// positional parameter.  First step: find position
				// value, up to 3 digits long.
				num[0] = *s;
				s++;
				if (isdigit(*s)) {
					num[1] = *s;
					num[2] = 0;
					s++;
					if (isdigit(*s)) {
						num[2] = *s;
						num[3] = 0;
						s++;
					}
					else {
						num[2] = 0;
					}
				}
				else {
					num[1] = 0;
				}
				signed char n = atoi(num);

				// Look for option character following position value.
				char option = ' ';
				if (*s == 'q' || *s == 'Q' || *s == 'r' || *s == 'R') {
					option = *s++;
				}

				// Is it a named parameter?
				if (*s == ':') {
					// Save all alphanumeric and underscore characters
					// following colon as parameter name.
					s++;
					for (/* */; isalnum(*s) || *s == '_'; ++s) {
						name += *s;
					}

					// Eat trailing colon, if it's present.
					if (*s == ':') {
						s++;
					}

					// Update maps that translate parameter name to
					// number and vice versa.
					if (n >= static_cast<short>(parsed_names_.size())) {
						parsed_names_.insert(parsed_names_.end(),
								static_cast<std::vector<std::string>::size_type>(
										n + 1) - parsed_names_.size(),
								std::string());
					}
					parsed_names_[n] = name;
					parsed_nums_[name] = n;
				}

				// Finished parsing parameter; save it.
				parse_elems_.push_back(SQLParseElement(str, option, n));
				str = "";
				name = "";
			}
			else {
				// Insert literal percent sign, because sign didn't
				// precede a valid parameter string; this allows users
				// to play a little fast and loose with the rules,
				// avoiding a double percent sign here.
				str += '%';
			}
		}
		else {
			// Regular character, so just copy it.
			str += *s++;
		}
	}

	parse_elems_.push_back(SQLParseElement(str, ' ', -1));
	delete[] s0;
}