Welcome Guest, Not a member yet? Register   Sign In
Simple JavaScript database
#1

[eluser]Cyclops[/eluser]
Hi, everyone.

I just created a very simple JavaScript database which I will be releasing under the GPL pending your review. I just wanted to show it to someone before releasing it to the public at large.

It is a very simple object, and should be fairly simple to use.

Let me know if you see any way to optimize or otherwise make it better.

There are 2 functions preceding the obect: clone() and print_r(). print_r() is only used in the examples. clone() is actually used by the database.

and then there are a plethora of examples of usage following the object. tweak and play around with those to your hearts content.

Basically, the object supports 4 functions (methods): _insert, _select, _update, _delete.

Each of the functions' only argument should be an object (i.e. an associative array) whose members are arrays (or strings, depending on the case). In the case of _insert, the object should only have one array: 'records' (which is an array of objects itself) which are taken and inserted.

_update, _select, and _delete all take a "criteria" object which can have the following members:
'records' ,
'records_ne' , (not equal)
'records_lt' , (less than)
'records_gt' , (greater than)
'limit' ,
'limit_start' ,
'order_by' , (field name)
'order_dir' , (asc or desc)

The 'records/_ne|_lt|_gt' are all an array of objects, which objects should follow the format
{'name':'database field name', 'value':'value to search for'}. These objects are like the SQL WHERE clause. There is currently no functionality for OR or LIKE or pattern matching.

'records' in this context can actually be a string with the value of "*", saying to search for all records. If present, all other 'record_' members are ignored.

'limit' and 'limit_start' should be integers, and correspond to the SQL LIMIT x,x statement (LIMIT limit_start, limit).

'order_by' and 'order_dir' correspond to the SQL ORDER statement (ORDER BY order_by order_dir).



At any rate, take a look at it, and let me know what you think, before I release it.

-----------------------
Michael
-----------------------


Code:
/**
* clone
*
* @param obj $obj
* @access public
* @return void
*/
function clone(obj){
    if(obj == null || typeof(obj) != 'object')
        return obj;

    if ( obj.reverse ){ // array
        var temp = new Array();
        for(var key in obj)
            temp[key] = clone(obj[key]);
        return temp;
    }

    var temp = {}; // object
    for(var key in obj)
        temp[key] = clone(obj[key]);

    return temp;
}

/**
* print_r
*
* prints an array or object into readable format.
*
* I actually took most of this function from:
* http://www.phpbuilder.com/board/archive/index.php/t-10294264.html
*
* and modified it a little to fit my liking ( such as fiddling
* with the _indent and adding a 'function' case statement ).
*
* @param input $input
* @param _indent $_indent
* @access public
* @return void
*/
function print_r(input, _indent)
{
    if(typeof(_indent) == 'string') {
        var indent = _indent + _indent;
        var paren_indent = _indent;
    } else {
        var indent = '    ';
        var paren_indent = ' ';
    }
    switch(typeof(input)) {
        case 'boolean':
            var output = (input ? 'true' : 'false') + "\n";
            break;
        case 'object':
            if ( input === null ) {
                var output = "null\n";
                break;
            }
            var output = ((input.reverse) ? 'Array' : 'Object') + " (\n";
            for(var i in input) {
                output += indent + "[" + i + "] => " + print_r(input[i], indent);
            }
            output += paren_indent + ")\n";
            break;
        case 'string':
            var output = '"' + input + "\"\n";
            break;
        case 'function':
            if ( input.toString().indexOf('{') != -1 ){
                var output = input.toString().substr(0, input.toString().indexOf('{') + 1) + "}\n";
            }
            else {
                var output = input + "\n";
            }
            break;
        case 'number':
        default:
            var output = "" + input + "\n";
    }
    return output;
}


Messages In This Thread
Simple JavaScript database - by El Forum - 07-30-2007, 10:43 AM
Simple JavaScript database - by El Forum - 07-30-2007, 10:45 AM
Simple JavaScript database - by El Forum - 07-30-2007, 10:46 AM
Simple JavaScript database - by El Forum - 07-30-2007, 10:47 AM
Simple JavaScript database - by El Forum - 07-30-2007, 10:50 AM
Simple JavaScript database - by El Forum - 07-30-2007, 10:50 AM
Simple JavaScript database - by El Forum - 07-30-2007, 10:55 AM
Simple JavaScript database - by El Forum - 07-31-2007, 12:19 AM
Simple JavaScript database - by El Forum - 07-31-2007, 11:37 AM
Simple JavaScript database - by El Forum - 07-31-2007, 11:42 AM
Simple JavaScript database - by El Forum - 08-10-2007, 05:48 PM
Simple JavaScript database - by El Forum - 08-10-2007, 06:27 PM
Simple JavaScript database - by El Forum - 08-10-2007, 07:01 PM
Simple JavaScript database - by El Forum - 08-10-2007, 07:07 PM
Simple JavaScript database - by El Forum - 08-10-2007, 07:08 PM
Simple JavaScript database - by El Forum - 08-10-2007, 07:32 PM
Simple JavaScript database - by El Forum - 08-10-2007, 08:10 PM



Theme © iAndrew 2016 - Forum software by © MyBB