简体   繁体   English

如何在客户端浏览器中执行类似SQL的查询?

[英]How to do SQL-like queries in client side browser?

I've been looking for a way to do complex queries like SQL can perform but totally client side. 我一直在寻找一种方法来执行复杂的查询,例如SQL可以执行但完全在客户端执行。 I know that I can get the exact results that I want from doing SQL queries off of the server and I could even AJAX it so that it looks smooth. 我知道我可以从服务器上执行SQL查询来获得所需的准确结果,甚至可以AJAX使其看起来很平滑。 However for scaleability, performance, and bandwidth reasons I'd prefer to do this all client side. 但是出于可伸缩性,性能和带宽的原因,我宁愿在所有客户端执行此操作。

Some requirements: 一些要求:

  • Wide browser compatibility. 广泛的浏览器兼容性。 Anything that can run jQuery is fine. 任何可以运行jQuery的工具都可以。 I'd actually prefer that it be a jQuery plugin. 我实际上更希望它是一个jQuery插件。
  • Can sort on more than one column. 可以对多个列进行排序。 For instance, order by state alphabetically and list all cities alphabetically within each state. 例如,按字母顺序按州排序,并按字母顺序列出每个州内的所有城市。
  • Can filter results. 可以过滤结果。 For instance, the equivalent of "where state = 'CA' or 'NY' or 'TX'". 例如,等效于“ where state ='CA'或'NY'或'TX'”。
  • Must work completely client side so the user only needs to download a large set of data once and can cut the data however they want without constantly fetching data from the server and would in fact be able to do all queries offline after the initial pull. 必须完全在客户端运行,因此用户只需要下载一次大数据集就可以剪切数据,但是他们希望不断地从服务器获取数据,实际上可以在初始拉动之后脱机进行所有查询。

I've looked around on stackoverflow and found jslinq but it was last updated in 2009 and has no documentation. 我到处查看了stackoverflow,发现了jslinq,但它的最新更新时间为2009年,没有文档。 I also can't tell if it can do more complex queries like ordering on two different columns or doing "and" or "or" filtering. 我也无法确定它是否可以执行更复杂的查询,例如在两个不同的列上进行排序或执行“与”或“或”过滤。

I would think that something like this would have been done already. 我认为这样的事情已经做完了。 I know HTML5 got started down this path but then hit a roadblock. 我知道HTML5是沿着这条道路开始的,但后来遇到了障碍。 I just need basic queries, no joins or anything. 我只需要基本查询,不需要联接或任何东西。 Does anyone know of something that can do this? 有谁知道可以做到这一点的东西? Thanks. 谢谢。

Edit: I think I should include a use case to help clarify what I'm looking for. 编辑:我认为我应该包括一个用例,以帮助阐明我在寻找什么。

For example, I have a list of the 5000 largest cities in the US. 例如,我列出了美国5000个最大的城市。 Each record include Cityname, State, and Population. 每个记录包括城市名称,州和人口。 I would like to be able to download the entire dataset once and populate a JS array with it then, on the client side only, be able to run queries like the following and create a table from the resulting records. 我希望能够一次下载整个数据集并用它填充一个JS数组,然后仅在客户端上就可以运行以下查询,并根据结果记录创建表。

  • Ten largest cities in California 加州十大城市
  • All cities that start with "S" with populations of 1,000,000 or more. 以“ S”开头的所有人口超过1,000,000的城市。
  • Largest three cities in California, New York, Florida, Texas, and Illinois and order them alphabetically by state then by population. 加利福尼亚,纽约,佛罗里达,德克萨斯州和伊利诺伊州的三个最大城市,并按州和人口先后的字母顺序排列。 ie California, Los Angeles, 3,792,621; 即加利福尼亚州洛杉矶市3,792,621; California, San Diego, 1,307,402; 加利福尼亚,圣地亚哥,1,307,402; California, San Jose, 945,942...etc. 加利福尼亚州,圣何塞,945942 ...等。

All of these queries would be trivial to do via SQL but I don't want to keep going back and forth to the server and I also want to allow offline use. 通过SQL进行所有这些查询都是微不足道的,但我不想一直往返于服务器,并且我也希望允许脱机使用。

Take a look at http://linqjs.codeplex.com/ 看看http://linqjs.codeplex.com/

It easily meets all your requirements. 它很容易满足您的所有要求。

As long as the data can fit in memory as an array of objects, you can just use sort and filter . 只要数据可以作为对象数组容纳在内存中,就可以使用sortfilter For example, say you want to filter products. 例如,假设您要过滤产品。 You want to find all products either below $5 or above $100 and you want to sort by price (ascending), and if there are two products with the same price, sort by manufacturer (descending). 您要查找价格在5美元以下或100美元以上的所有产品,并且要按价格排序(升序),如果有两个价格相同的产品,则按制造商排序(降序)。 You could do that like this: 您可以这样做:

var results = products.filter(function(product) {
    // price is in cents
    return product.price < 500 || product.price > 10000;
});
results.sort(function(a, b) {
    var order = a.price - b.price;
    if(order == 0) {
        order = b.manufacturer.localeCompare(a.manufacturer);
    }
    return order;
});

For cross-browser compatibility, just shim filter . 为了实现跨浏览器的兼容性,只需填充shim filter

Try Alasql.js . 试试Alasql.js This is a javascript client-side SQL database. 这是一个JavaScript客户端SQL数据库。

You can do complex queries with joins and grouping, even optimization of joins and where parts. 您可以使用联接和分组进行复杂的查询,甚至可以优化联接和零件的位置。 It does not use WebSQL. 它不使用WebSQL。

Your requirements support: 您的需求支持:

  • Wide browser compatibility - all modern versions of browsers, including mobiles. 广泛的浏览器兼容性-所有现代版本的浏览器,包括手机。
  • Can sort on more than one column.- Alasql does it with ORDER BY clause. 可以对多个列进行排序。-Alasql使用ORDER BY子句进行排序。
  • Can filter results. 可以过滤结果。 - with WHERE clause. -带WHERE子句。
  • Must work completely client side so the user only needs to download a large set of data once and can cut the data however they want without constantly fetching data from the server and would in fact be able to do all queries offline after the initial pull. 必须完全在客户端运行,因此用户只需要下载一次大数据集就可以剪切数据,但是他们希望不断地从服务器获取数据,实际上可以在初始拉动之后脱机进行所有查询。 - you can use pure JavaScript (Array.push(), etc.) operations to modify data (do not forget to set table.dirty flag). -您可以使用纯JavaScript(Array.push()等)操作来修改数据(不要忘记设置table.dirty标志)。

Here is a simple example ( play with it in jsFiddle ): 这是一个简单的示例(在jsFiddle中使用它):

// Fill table with data
var person = [ 
    { name: 'bill' , sex:'M', income:50000 },
    { name: 'sara' , sex:'F', income:100000 },
    { name: 'larry' , sex:'M', income:90000 },
    { name: 'olga' , sex:'F', income:85000 },
];

// Do the query
var res = alasql("SELECT * FROM ? person WHERE sex='F' AND income > 60000", [person]);

How about Yahoo's YQL ? 雅虎的YQL如何? I've only briefly looked at it, but it looks interesting. 我只是简要地看了一下,但是看起来很有趣。

Backbone is a pretty good js library which (their words) "gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface." Backbone是一个相当不错的js库,(用他们的话来说)“通过为模型提供键值绑定和自定义事件,具有丰富的可枚举函数的API集合,具有声明性事件处理的视图并为它们提供连接,从而为Web应用程序构造结构您通过RESTful JSON接口的现有API。”

I am not sure if this is what you are looking for but you can use it to mock up your model and bind event listeners to it. 我不确定这是否是您要寻找的东西,但是您可以使用它来模拟模型并将事件监听器绑定到它。 This seems to be a good tutorial to go through some of the base uses for it. 似乎是学习它的一些基本用法的很好的教程。

You can use CanJS . 您可以使用CanJS Its a relativelly new library that performs better then Backbone and other and its based on the infamous JavaScript MVC library. 它是一个相对较新的库,其性能优于Backbone及其它,并且基于臭名昭著的JavaScript MVC库。 In reallity, its the MVC part of the JS MVC with a bit of spices. 实际上,它是JS MVC的MVC部分,带有一些香料。

You can take a look at this tut by net.tutsplus.com http://net.tutsplus.com/tutorials/javascript-ajax/diving-into-canjs-part-3/ 您可以通过net.tutsplus.com http://net.tutsplus.com/tutorials/javascript-ajax/diving-into-canjs-part-3/查看此tut

It's pretty powerfull and fast. 它非常强大且快速。 Has features like live binding that make's your life easy. 具有实时绑定等功能,使您的生活变得轻松。

Coils is a Clojurescript framework which compiles to Javascropt and has client side SQL queries like this: Coils是一个Clojurescript框架,可编译为Javascropt并具有如下客户端SQL查询:

(go
    (log (sql "SELECT * FROM test_table where name = ?" ["shopping"] )))

: It is full SQL that is passed to a server side relational database: :这是传递到服务器端关系数据库的完整SQL:

https://github.com/zubairq/coils https://github.com/zubairq/coils

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 新的javascript / jquery库可以对JSON数据进行类似SQL的查询吗? - New javascript/jquery library that allows for SQL-like queries on JSON data? JavaScript:两个数组的类似 SQL 的连接 - JavaScript: SQL-like join of two arrays 用于 Javascript 的类似 SQL 的内部联接 - SQL-like Inner Join for Javascript 是否有针对Javascript的类似于SQL的数据访问的实现? - Is there an implementation of SQL-like data access for Javascript? IndexedDB是否支持类似SQL的事务? - Does IndexedDB support SQL-like transactions? 谷歌如何建议在客户端缓存查询? - How google suggest caches queries on client side? 如何在客户端(浏览器)中使用 uWS? - How to use uWS in Client Side(Browser)? 使用浏览器在页面(客户端)上运行自定义JavaScript来模拟点击?怎么做? - Use browser to run custom JavaScript on page (client side) to simulate clicking? How to do? 如何在Javascript中为WebDB或Google Gears等客户端数据库转义数据? - How do I escape data in Javascript for a client-side database like WebDB or Google Gears? 如何从客户端(Javascript)重新排序下拉列表控件,如 NetFlix 队列 - How do I resequence dropdown list controls like the NetFlix queue from the client side (Javascript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM