-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdbQuery.cfc
More file actions
121 lines (113 loc) · 4.87 KB
/
Copy pathdbQuery.cfc
File metadata and controls
121 lines (113 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<cfcomponent>
<cffunction name="init" access="package" output="no">
<cfargument name="dbObject" type="component" required="yes">
<cfargument name="config" type="struct" required="no">
<cfscript>
variables.config=arguments.dbObject.getConfig();
arguments.config.dbQuery=this;
variables.db=arguments.dbObject;
if(structkeyexists(arguments, 'config')){
structappend(variables.config, arguments.config, true);
if(structkeyexists(arguments.config, 'parseSQLFunctionStruct')){
variables.config.parseSQLFunctionStruct=duplicate(arguments.config.parseSQLFunctionStruct);
}
}
variables.lastSQL="";
variables.lastQueryName="";
variables.tableSQLString=":ztablesql:";
variables.trustSQLString=":ztrustedsql:";
variables.config.arrParam=[];
</cfscript>
</cffunction>
<cffunction name="getLastSQL" access="package" output="no" hint="Returns a COPY of the config data for debugging purposes.">
<cfscript>
return {sql:variables.lastSQL, name:variables.lastQueryName};
</cfscript>
</cffunction>
<cffunction name="getConfig" access="public" output="no" hint="Returns a COPY of the config data for debugging purposes.">
<cfscript>
return duplicate(variables.config);
</cfscript>
</cffunction>
<cffunction name="param" access="public" returntype="string" output="no">
<cfargument name="value" type="string" required="yes">
<cfargument name="cfsqltype" type="string" required="no">
<cfscript>
if(structkeyexists(arguments, 'cfsqltype')){
arrayappend(variables.config.arrParam, {value:arguments.value, cfsqltype:arguments.cfsqltype});
}else{
if(isnumeric(arguments.value) and len(arguments.value) LT 10){
if(find(".", arguments.value)){
arrayappend(variables.config.arrParam, {value:arguments.value, cfsqltype:'cf_sql_decimal'});
}else{
arrayappend(variables.config.arrParam, {value:arguments.value, cfsqltype:'cf_sql_bigint'});
}
}else{
arrayappend(variables.config.arrParam, {value:arguments.value});
}
}
return "?";
</cfscript>
</cffunction>
<cffunction name="table" access="public" returntype="string" output="no">
<cfargument name="name" type="string" required="yes">
<cfargument name="datasource" type="string" required="no" default="#variables.config.datasource#">
<cfscript>
var zt="";
if(variables.config.verifyQueriesEnabled){
zt=variables.tableSQLString;
}
if(len(arguments.datasource)){
variables.config.datasource=arguments.datasource;
return variables.config.identifierQuoteCharacter&arguments.datasource&variables.config.identifierQuoteCharacter&"."&variables.config.identifierQuoteCharacter&zt&variables.config.tablePrefix&arguments.name&variables.config.identifierQuoteCharacter;
}else{
return variables.config.identifierQuoteCharacter&zt&variables.config.tablePrefix&arguments.name&variables.config.identifierQuoteCharacter;
}
</cfscript>
</cffunction>
<cffunction name="trustedSQL" access="public" returntype="string" output="no">
<cfargument name="value" type="string" required="yes">
<cfscript>
if(variables.config.verifyQueriesEnabled){
return variables.trustSQLString&arguments.value&variables.trustSQLString;
}else{
return arguments.value;
}
</cfscript>
</cffunction>
<cffunction name="execute" access="public" returntype="any" output="no" hint="Use for any query.">
<cfargument name="name" type="variablename" required="yes" hint="A variable name for the query result. Helps to identify query when debugging.">
<cfscript>
variables.config.sql=this.sql;
var executeResult=variables.db.execute(arguments.name, variables.config);
variables.lastSQL=this.sql;
variables.lastQueryName=arguments.name;
this.reset();
return executeResult.result;
</cfscript>
</cffunction>
<cffunction name="insert" access="public" returntype="any" output="no" hint="Use for insert statements to auto-retrieve the inserted id more easily.">
<cfargument name="name" type="variablename" required="yes" hint="A variable name for the query result. Helps to identify query when debugging.">
<cfargument name="idColumn" type="string" required="no" default="id" hint="The name of the sql id column.">
<cfscript>
var executeResult=0;
variables.config.sql=this.sql;
executeResult=variables.db.insertAndReturnId(arguments.name, arguments.idColumn, variables.config);
variables.lastSQL=this.sql;
variables.lastQueryName=arguments.name;
this.reset();
return executeResult;
</cfscript>
</cffunction>
<cffunction name="reset" access="public" returntype="any" output="no">
<cfscript>
var c=0;
if(variables.config.autoReset){
c=variables.db.getConfig();
structappend(variables.config, c, true);
variables.config.parseSQLFunctionStruct=duplicate(c.parseSQLFunctionStruct);
}
variables.config.arrParam=[];
</cfscript>
</cffunction>
</cfcomponent>