Beispiel #1
0
        public static JsObject GetColumns(string database, string table)
        {
            var ret = new JsObject();
            var columns = new List<JsObject>();
            ret["columns"] = columns;
            using (var reader = DbReader.Create(database, string.Format("SHOW COLUMNS IN `{0}`", table)))
            {
                int index = 0;
                while (reader.Read())
                {
                    ret[index.ToString()] = reader[0] as string;
                    index++;
                }

                ret["length"] = index;
            }

            // TODO: db query security issue
            string query = string.Format("SELECT * FROM `_table_metadata` WHERE name='{0}'", table);
            using (var reader = DbReader.Create(database, query))
            {
                if (reader.Read())
                {
                    var metadata = reader["columns"] as string;
                    ret["_metadata"] = new JsonReader(metadata).ReadValue() as JsObject;
                }
            }

            return ret;
        }
Beispiel #2
0
        public static List<JsObject> GetRows(
            string database, 
            string tableName, 
            int start, 
            int length)
        {
            var ret = new List<JsObject>();
            var query = string.Format("SELECT * FROM `{0}` LIMIT {1},{2}",
                tableName, start, length);
            using (var reader = DbReader.Create(database, query))
            {
                while (reader.Read())
                {
                    var row = new JsObject();
                    for (int i = 0; i < reader.BaseReader.FieldCount; i++)
                    {
                        row[reader.BaseReader.GetName(i)] = reader[i].ToString();
                    }

                    // TODO: Check if editable
                    row["_metadata"] = GetMetadata(reader, database, tableName);
                    ret.Add(row);
                }
            }

            return ret;
        }
Beispiel #3
0
        JsObject ReadObject()
        {
            JsObject record = new JsObject();

            // Consume the '{'
            _reader.Read();
            while (true)
            {
                char ch = PeekNextSignificantCharacter();
                if (ch == '\0')
                {
                    throw new FormatException("Unterminated object literal.");
                }

                if (ch == '}')
                {
                    _reader.Read();
                    return record;
                }

                if (record.Count != 0)
                {
                    if (ch != ',')
                    {
                        throw new FormatException("Invalid object literal.");
                    }
                    else
                    {
                        _reader.Read();
                    }
                }

                string name = ReadName(/* allowQuotes */ true);
                ch = PeekNextSignificantCharacter();

                if (ch != ':')
                {
                    throw new FormatException("Unexpected name/value pair syntax in object literal.");
                }
                else
                {
                    _reader.Read();
                }

                object item = ReadValue();
                record[name] = item;
            }
        }