antville/code/Global/Sql.js
Tobi Schäfer e0d45711b6 * Rewrote Sql.SEARCH statement to search for regular expression pattern in text and title field of stories and comments
* Modified Site.search_action() to filter comments of closed stories and increased result limit to 50

Fixes issue 38
2010-02-06 17:03:30 +00:00

188 lines
4.8 KiB
JavaScript

//
// The Antville Project
// http://code.google.com/p/antville
//
// Copyright 2001-2007 by The Antville People
//
// Licensed under the Apache License, Version 2.0 (the ``License'');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an ``AS IS'' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// $Revision$
// $LastChangedBy$
// $LastChangedDate$
// $URL$
//
/**
* @fileOverview Defines the Sql prototype, a utility for relational queries
*/
/**
* @constructor
*/
var Sql = function(options) {
options || (options = {});
var db = getDBConnection("antville");
var query;
var log = new function() {
var fname = getProperty("sqlLog", "helma." + app.getName() + ".sql");
return Packages.org.apache.commons.logging.LogFactory.getLog(fname);
}
var SqlData = function(result) {
var columns = [];
this.values = {};
for (var i=1; i<=result.getColumnCount(); i+=1) {
columns.push(result.getColumnName(i).toLowerCase());
}
this.next = function() {
for each (var key in columns) {
this.values[key] = result.getColumnItem(key);
}
return;
}
return this;
}
var quote = function(str) {
if (!options.quote || str === null) {
return str;
}
return str.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
}
var value = function(obj) {
if (obj === null) {
return obj;
}
if (obj === undefined) {
obj = String(obj);
}
switch (obj.constructor) {
case Number:
return obj;
case String:
return quote(obj);
case Date:
return "from_unixtime(" + (obj.getTime() / 1000) + ")";
case HopObject:
case Object:
return quote(obj.toSource());
}
return quote(String(obj));
}
var resolve = function(args) {
var sql = args[0];
if (args.length > 1) {
var values = Array.prototype.splice.call(args, 1);
if (typeof values[0] === "object") {
values = values[0];
}
sql = sql.replace(/\$(\w*)/g, function() {
return value(values[arguments[1]]);
});
}
return sql;
}
/**
*
* @param {String} sql
* @returns {Object}
*/
this.execute = function(sql) {
sql = resolve(arguments);
log.info(sql);
var error;
var result = db.executeCommand(sql);
if (error = db.getLastError()) {
app.log(error);
}
return result;
}
/**
* @returns {String}
*/
this.retrieve = function() {
return log.info(query = resolve(arguments));
}
/**
*
* @param {Function} callback
*/
this.traverse = function(callback) {
var rows = db.executeRetrieval(query);
if (rows && rows.next()) {
do {
var sql = new SqlData(rows);
sql.next();
callback.call(sql.values, rows);
} while (record = rows.next());
rows.release();
}
return;
}
/**
* @return {String}
*/
this.toString = function() {
return query;
}
return this;
}
/** @constant */
Sql.COUNT = "select count(*) as count from $0";
/** @constant */
Sql.REFERRERS = "select referrer, count(*) as requests from " +
"log where context_type = '$0' and context_id = $1 and action = " +
"'main' and created > date_add(now(), interval -1 day) group " +
"by referrer order by requests desc, referrer asc";
/** @constant */
Sql.PURGEREFERRERS = "delete from log where action = 'main' and " +
"created < date_add(now(), interval -2 day)";
/** @constant */
Sql.SEARCH = "select id from content where site_id = $0 and " +
"prototype in ('Story', 'Comment') and status <> 'closed' and " +
"(metadata regexp 'title:\"[^\"]*$1[^\"]*\"' or " +
"metadata regexp 'text:\"[^\"]*$1[^\"]*\"') " +
"order by created desc limit $2";
/** @constant */
Sql.MEMBERSEARCH = "select name from account where name $0 '$1' " +
"order by name asc limit $2";
/** @constant */
Sql.ARCHIVE = "select id from content where site_id = $0 and " +
"prototype = 'Story' and status <> 'closed' $1 $2 limit $3 offset $4";
/** @constant */
Sql.ARCHIVESIZE = "select count(*) as count from content where site_id = $0 " +
"and prototype = 'Story' and status <> 'closed' $1";
/** @constant */
Sql.ARCHIVEPART = " and extract($0 from created) = $1";
/** @constant */
Sql.ARCHIVEORDER = "order by created desc";