[英]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: 一些要求:
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数组,然后仅在客户端上就可以运行以下查询,并根据结果记录创建表。
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
. 只要数据可以作为对象数组容纳在内存中,就可以使用
sort
和filter
。 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: 您的需求支持:
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]);
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.