A ASP (JScript) Simple SQL Statement Build "Class" while supporting client and server execution

xiaoxiao2021-03-06  41

Client effect

<% @ Language = "javascript" CODEPAGE = "936"%>

<%

VAR QuestStringBuilder = function () {

// 1-> query statement, 2-> update statement, 3-> delete statement, 4-> insert statement

VAR QuestTYPE = 0;

Var fields = [];

Var values ​​= [];

VAR CONDition;

VAR PK;

Var pkeysort;

Var Tablename;

Var State = false;

Var clogic = "AND";

Var Coperator = "=";

this.addfield = function (field, value) {

Fields.push (Processfield (Field));

VALUES.PUSH (ProcessValue (Value);

}

this.addcfield = function (cfield, cvalue) {

VAR STEMP = [];

IF (Condition! = Undefined && Condition! = "") {stepp.push (condition);

IF (Coperator! = "LIKE") {

STEMP.PUSH (CField) Coperator ProcessValue (CVALUE));

} else {

STEMP.PUSH (Cfield) "Like '%" CVALUE "%'");

}

Condition = stepp.join (clogic);

IF (condition == ") {condition = stepp.tostring ();

}

THIS.TABLE = Function (s) {TABLENAME = S;};

THIS.PKEY = Function (s) {pk = processfield (s);};

THIS.PKEYASC = Function () {pkeysort = "ASC";

THIS.PKEYDESC = Function () {pkeysort = "dec";

THIS.GetSelect = function () {

QuestType = 1;

Return Result ();

}

THIS.GETUPDATE = Function () {

QuestType = 2;

Return Result ();

}

THIS.GETDELETE = Function () {

QuestType = 3;

Return Result ();

}

THIS.GETINSERT = function () {

QuestType = 4;

Return Result ();

}

this.clear = function () {

Fields = [];

VALUES = [];

PK = Undefined;

PKEYSORT = Undefined;

TABLENAME = undefined;

State = false;

}

THIS.ACTIONSTATE = Function () {Return State;}; this.changelogic = function (s) {clogic = " s.touppercase () " ";

This.changeoperator = function (s) {coperator = S.touppercase ();

Var result = function () {

Var straTemp;

IF (Fields.Length == 0) Fields.push ("*");

Switch (QuestType) {

Case 1:

IF (TableName! = Undefined && Tablename! = "") {

Strtemp = "SELECT" Fields.toString () "from" TableName;

IF (condition! = undefined) {strTemp = "where" condition;

IF (pkeysort! = undefined && pk! = undefined) {straTemp = "Order by" pk "" pkeysort;}

State = true;

} else {

Strtemp = getError (0); / / Require input table name

}

Return strtemp;

Break;

Case 2:

IF (TableName! = Undefined && Tablename! = "") {

Strtemp = "Update" TableName "Set" Process (Fields, Values);

IF (Condition! = undefined) {

State = true;

STRTEMP = "Where" condition;

} else {

Strtemp = geterror (1);

}

} else {

Strtemp = getError (0); / / Require input table name

}

Return strtemp;

Break;

Case 3:

IF (TableName! = Undefined && Tablename! = "") {

Strtemp = "delete from" TableName;

IF (Condition! = undefined) {

State = true;

STRTEMP = "Where" condition;

} else {

Strtemp = geterror (1);

}

} else {

Strtemp = getError (0); / / Require input table name

}

Return strtemp;

Break;

Case 4:

IF (TableName! = Undefined && Tablename! = "") {

Strtemp = "INSERT INTO" TABLENAME;

IF (Fields.Length! = 0) {

State = true; strTemp = "(" Fields.Tostring () ") VALUES (" VALUES.TOSTRING () ")"

} else {

Strtemp = geterror (2);

}

} else {

Strtemp = getError (0); / / Require input table name

}

Return strtemp;

Break;

}

}

Var processValue = function (unknow) {

VAR INPUTTYPE = TypeOf (unknow);

Switch (InputType) {

Case "String":

Return "'" unknow "'";

Break;

Case "Number":

Return unknow;

Break;

Case "Boolean":

Return unknow;

Break;

}

}

Var processfield = function (unknow) {

Return "[" unknow "]";

}

Var process = function (Afield, Avalue) {

VAR ATEMP = [];

For (i = 0; I

ATEMP.PUSH (Afield [i] = " Avalue [i]);

}

Return atemp.toString ();

}

Var getrror = function (interror) {

IF (INTERROR) == "Number") {

Switch (interror) {

Case 0:

State = false;

Return "Need Quest TableName, please use for method Table Input One Tablename!";

Break;

Case 1:

State = false;

Return "Need One Condition! Please use!";

Break;

Case 2:

State = false;

Return "NEED INSERT VALUES!";

Break;

}

}

}

}

Var SQL = New QueststringBuilder ();

SQL.TABLE ("MyTable");

Sql.addfield ("Test", "1");

SQL.Addfield ("Test1", 23);

SQL.Addcfield ("ID", 93);

SQL.PKEY ("ID");

Sql.pKeyDesc ();

//sql.clear ();

Response.write (sql.getdelete () "
" SQL.ActionsTate ());

%>

转载请注明原文地址:https://www.9cbs.com/read-72710.html

New Post(0)