/***************************** {{{ LICENSE ********************************\ * dbobj, the relational object persistence module for PHP * * Copyright (C) 2006 Adam Banko * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * \***************************** LICENSE }}} ********************************/ #include "SQL.h" #include "dbobj.h" #include /* * note for length calculating functions: * the sizeof("something") function returns the length of the string inside * plus the terminating '\0'. Therefore the returned length is one byte * longer than the actual string. * A kulcsszavakat koveto szokoz-hoz tartozo hossz ugy van beleszamolva * a teljess hosszba, hogy nem tesszuk be a kulcsso utan a szokozt, de a * lezaro '\0' hosszat sem vonjuk le. Azaz a stringet lezaro '\0'-nek * szukseges helyet a kulcscsot koveto szokoz fogja kitolteni. */ DBOBJ_API uint SQL_SELECTsL(basicPropConfig dbPropConfs[], uint dbPropConfsL, uint tableNameL) { uint commandL = sizeof("SELECT"); //count delimeter commas commandL += dbPropConfsL - 1; { uint i; //count letters in fieldNames for(i = 0; i < dbPropConfsL; i++) commandL += dbPropConfs[i]->dbFieldL; } //FROM string commandL += sizeof(" FROM"); //tableName commandL += tableNameL; return commandL; } DBOBJ_API uint SQL_WHEREPKsL(uint* paramCounter, basicPropConfig PKs[],uint PKsL) { uint commandL = sizeof(" WHERE"); if(PKsL == 0) {//no PKs return 0; } //count ANDs commandL += sizeof(" AND")*(PKsL - 1); //count letter in PK's fieldNames { uint i; for(i = 0; i < PKsL; i++) commandL += PKs[i]->dbFieldL; } //count equal singns commandL += PKsL; *paramCounter += PKsL; return commandL; } DBOBJ_API uint SQL_DELETEsL(uint tableNameL) { uint commandL = sizeof("DELETE FROM"); commandL += tableNameL; return commandL; } DBOBJ_API uint SQL_WHERECONDITIONsL(uint* paramCounter, dbCondition* conditions, uint conditionsL) { uint commandL; if(conditionsL == 0) { return 0; } commandL = sizeof(" WHERE"); //count ANDs commandL += sizeof(" AND")*(conditionsL - 1); //count letter in PK's fieldNames { uint i; for(i = 0; i < conditionsL; i++) commandL += conditions[i].config->dbFieldL; } //count equal singns commandL += conditionsL; *paramCounter += conditionsL; return commandL; } DBOBJ_API uint SQL_INSERTsL(uint* paramCounter, uint tableNameL, basicPropConfig dbPropConfs[], uint dbPropConfsL) { uint commandL = sizeof("INSERT INTO"); commandL += tableNameL; commandL += 4+sizeof("VALUES")-1; //()VALUES() commandL += 2*dbPropConfsL -2; //commas { uint i; for(i=0; idbFieldL; } *paramCounter += dbPropConfsL; return commandL; } DBOBJ_API uint SQL_UPDATEsL(uint* paramCounter, uint tableNameL, basicPropConfig dbPropConfs[], uint dbPropConfsL) { uint commandL = sizeof("UPDATE"); commandL += tableNameL; commandL += sizeof(" SET"); commandL += dbPropConfsL -1; //commas { uint i; for(i=0; idbFieldL +1;//dbfield= } *paramCounter += dbPropConfsL; return commandL; } //DECLARE dbobj_cursor_15 SCROLL CURSOR FOR DBOBJ_API uint SQL_DECLAREsL(uint cursorNumL, bool scroll) { uint commandL = sizeof("DECLARE BINARY CURSOR FOR"); if(scroll){ commandL += sizeof("SCROLL"); } else { commandL += sizeof("NO SCROLL"); } commandL += cursorNumL; return commandL; } ////////////////////////////////////////////// ////////// String management macros ////////// ////////////////////////////////////////////// //add string #define ADDS(what) {memcpy(start, what, sizeof(what)-1); start+=sizeof(what)-1;} //add string with known length #define ADDL(what,size) {memcpy(start, what, size); start+=size;} //add character #define ADDCHAR(c) {start++[0]=c;} //replace the previous character #define REPLACECHAR(c) {start[-1]=c;} //move the cursor back in the string #define STEPBACK(i) {start-=i;} #define AddNextParamIndex() {\ (*paramCounter)++;\ if(*paramCounter < 10) {\ ADDCHAR('0'+ *paramCounter);\ } else if(*paramCounter < 100) {\ ADDCHAR('0'+ *paramCounter/10);\ ADDCHAR('0'+ *paramCounter%10);\ } else {\ ADDCHAR('0'+ *paramCounter/100);\ ADDCHAR('0'+(*paramCounter/10)%10);\ ADDCHAR('0'+ *paramCounter%10);\ };} DBOBJ_API char* SQL_SELECTs(char* start, basicPropConfig dbPropConfs[], uint dbPropConfsL, uint tableNameL, char* tableName) { //SELECT string ADDS("SELECT ") //count letters in fieldNames plus delimeter commas {uint i; for(i = 0; i < dbPropConfsL; i++) { ADDL(dbPropConfs[i]->dbField,dbPropConfs[i]->dbFieldL); ADDCHAR(','); }} STEPBACK(1); //FROM string ADDS(" FROM "); //tableName ADDL(tableName,tableNameL); return start; } DBOBJ_API char* SQL_WHEREPKs(char* start, uint* paramCounter, basicPropConfig PKs[], uint PKsL) { //WHERE if(PKsL == 0) { return start; } ADDS(" WHERE "); //count letter in PK's fieldNames {uint i; for(i = 0; i < PKsL; i++) { ADDL(PKs[i]->dbField,PKs[i]->dbFieldL); ADDCHAR('='); ADDCHAR('$'); AddNextParamIndex(); if(i < PKsL-1) ADDS(" AND "); }} return start; } DBOBJ_API char* SQL_DELETEs(char* start, uint tableNameL, char* tableName) { ADDS("DELETE FROM "); ADDL(tableName,tableNameL); return start; } DBOBJ_API char* SQL_WHERECONDITIONs(char* start, uint* paramCounter, dbCondition* conditions, uint conditionsL) { if(conditionsL == 0) { return start; } //WHERE ADDS(" WHERE "); //count letter in PK's fieldNames {uint i; for(i = 0; i < conditionsL; i++) { ADDL(conditions[i].config->dbField,conditions[i].config->dbFieldL); ADDCHAR('='); ADDCHAR('$'); AddNextParamIndex(); if(i < conditionsL-1) ADDS(" AND "); }} return start; } DBOBJ_API char* SQL_INSERTs(char* start, uint* paramCounter, uint tableNameL, basicPropConfig dbPropConfs[], uint dbPropConfsL, char* tableName) { uint i; ADDS("INSERT INTO "); ADDL(tableName,tableNameL); ADDCHAR('('); for(i = 0; i< dbPropConfsL; i++) { ADDL(dbPropConfs[i]->dbField,dbPropConfs[i]->dbFieldL); ADDCHAR(','); } REPLACECHAR(')'); ADDS("VALUES"); ADDCHAR('('); for(i=0; idbField,dbPropConfs[i]->dbFieldL); ADDCHAR('='); ADDCHAR('$'); AddNextParamIndex(); ADDCHAR(','); } STEPBACK(1); return start; } DBOBJ_API char* SQL_DECLAREs(char* start, char* cursorName, uint cursorNameL, bool scroll) { ADDS("DECLARE "); ADDL(cursorName, cursorNameL); if(scroll){ ADDS(" BINARY SCROLL CURSOR FOR "); } else { ADDS(" BINARY NO SCROLL CURSOR FOR "); } return start; } /* * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */