CFLib.org – Common Function Library Project

EksporSQLData(table, dbsource)

Last updated January 11, 2011

Version: 1 | Requires: ColdFusion MX | Library: DatabaseLib

 
Rated 21 time(s). Average Rating: 5.0

Description:
Export table data to MYSQL in script

Return Values:
Returns a string.

Example:

view plain print about
<cfsetting showdebugoutput="no">
<cfoutput>
<cfset ShowData = EkporSQLTable("tbl_employee""darwanlns.com")>
<pre>
#ShowData#
</pre>  
</cfoutput>

Parameters:

Name Description Required
table Table to read. Yes
dbsource DSN. Yes

Full UDF Source:

view plain print about
<!---
 Eksport SQL Data to Mysql
 
 @param table      Table to read. (Required)
 @param dbsource      DSN. (Required)
 @return Returns a string. 
 @author Darwan Leonardo Sitepu (dlns2001@yahoo.com) 
 @version 1, January 11, 2011 
--->

<cffunction name="EkporSQLTable" returnType="string" output="false">
    <cfargument name="table" type="string" required="true">
    <cfargument name="dbsource" type="string" required="true">
    <cfset var theTable = "#arguments.table#">
    <cfset var theFileContent = ""><!--- to save the content SQL to a file --->        
    <cfset var theFields = "">
    <cfset var theSQL = "">
    <cfset var theFIELD = "">
    <cfset var theVALUE = "">
    <cfset var kolom = "">
    <cfset var theDate = "">
    <cfset var tables = "">
    <cfset var i = "">
    <cfset var qRead = "">
        
    <cfloop list="#theTable#" index="tables" delimiters=",">
        <cfquery name="qRead" datasource="#arguments.dbsource#" debug="no">
            select * from #tables#
        </cfquery>    
        <cfif qRead.recordCount gt 0>
            <cfset theFields = getMetaData(qRead)>    
            <cfloop query="qRead">
                <cfset theSQL = "">
                <cfset theFIELD = "INSERT INTO #tables#_temp (">
                <cfset theVALUE = "VALUES ( ">
                <!--- THE KOLOM N VALUE --------------------------------------------------------------------------------->    
                <cfloop index="i" from="1" to="#arrayLen(theFields) -1#">
                    <cfset kolom = evaluate("qRead.#theFields[i].name#")>
                    <cfset theFIELD = theFIELD & " " & theFields[i].name & ",">                
                    <!--- CHECK THE TYPE -------------------------------------------------------------------------------->
                    <cfswitch expression="#theFields[i].TypeName#">
                        <cfcase value="number">
                            <cfif kolom neq "">
                                <cfset theVALUE = theVALUE & " " & kolom & ",">
                            <cfelse>
                                <cfset theVALUE = theVALUE & " NULL,">
                            </cfif>
                        </cfcase>
                        <cfcase value="numeric">
                            <cfif kolom neq "">
                                <cfset theVALUE = theVALUE & " " & kolom & ",">
                            <cfelse>
                                <cfset theVALUE = theVALUE & " NULL,">
                            </cfif>
                        </cfcase>
                        <cfcase value="float">
                            <cfif kolom neq "">
                                <cfset theVALUE = theVALUE & " " & kolom & ",">
                            <cfelse>
                                <cfset theVALUE = theVALUE & " NULL,">
                            </cfif>
                        </cfcase>
                        <cfcase value="integer">
                            <cfif kolom neq "">
                                <cfset theVALUE = theVALUE & " " & kolom & ",">
                            <cfelse>
                                <cfset theVALUE = theVALUE & " NULL,">
                            </cfif>
                        </cfcase>
                        <cfcase value="text">
                            <cfif FindNoCase("'",kolom,1)>
                                <cfset kolom = ReplaceNoCase(kolom,"'","","ALL")>
                            </cfif>                            
                            <cfif kolom neq "">
                                <cfset theVALUE = theVALUE & " '" & kolom & "',">
                            <cfelse>
                                <cfset theVALUE = theVALUE & " NULL,">
                            </cfif>
                        </cfcase>
                        <cfcase value="varchar">
                            <cfif FindNoCase("'",kolom,1)>
                                <cfset kolom = ReplaceNoCase(kolom,"'","","ALL")>
                            </cfif>
                            <cfif kolom neq "">
                                <cfset theVALUE = theVALUE & " '" & kolom & "',">
                            <cfelse>
                                <cfset theVALUE = theVALUE & " NULL,">
                            </cfif>
                        </cfcase>
                        <cfcase value="char">
                            <cfif FindNoCase("'",kolom,1)>
                                <cfset kolom = ReplaceNoCase(kolom,"'","","ALL")>
                            </cfif>
                            <cfif kolom neq "">
                                <cfset theVALUE = theVALUE & " '" & kolom & "',">
                            <cfelse>
                                <cfset theVALUE = theVALUE & " NULL,">
                            </cfif>
                        </cfcase>
                        <cfcase value="varchar2">
                            <cfif FindNoCase("'",kolom,1)>
                                <cfset kolom = ReplaceNoCase(kolom,"'","","ALL")>
                            </cfif>
                            <cfif kolom neq "">
                                <cfset theVALUE = theVALUE & " '" & kolom & "',">
                            <cfelse>
                                <cfset theVALUE = theVALUE & " NULL,">
                            </cfif>
                        </cfcase>
                        <cfcase value="date">
                            <!--- format TO MYSQL --->
                            <cfif kolom neq "">
                                <cfset theDate = dateformat(kolom, "yyyy-mm-dd")>                                
                                <cfset theVALUE = theVALUE & " '" & theDate & "',">
                            <cfelse>
                                <cfset theVALUE = theVALUE & " NULL,">
                            </cfif>
                        </cfcase>
                        <cfdefaultcase>
                            <cfif kolom neq "">
                                <cfset theVALUE = theVALUE & " '" & kolom & "',">
                            <cfelse>
                                <cfset theVALUE = theVALUE & " NULL,">
                            </cfif>
                        </cfdefaultcase>
                    </cfswitch>
                </cfloop>
                <cfset kolom = evaluate("qRead.#theFields[i].name#")>
                <cfset theFIELD = theFIELD & " " & theFields[i].name & ")">
                <!--- CHECK THE TYPE -------------------------------------------------------------------------------->
                <cfswitch expression="#theFields[i].TypeName#">
                    <cfcase value="number">
                        <cfif kolom neq "">
                            <cfset theVALUE = theVALUE & " " & kolom & ")">
                        <cfelse>
                            <cfset theVALUE = theVALUE & " NULL)">
                        </cfif>
                    </cfcase>
                    <cfcase value="numeric">
                        <cfif kolom neq "">
                            <cfset theVALUE = theVALUE & " " & kolom & ")">
                        <cfelse>
                            <cfset theVALUE = theVALUE & " NULL)">
                        </cfif>
                    </cfcase>
                    <cfcase value="float">
                        <cfif kolom neq "">
                            <cfset theVALUE = theVALUE & " " & kolom & ")">
                        <cfelse>
                            <cfset theVALUE = theVALUE & " NULL)">
                        </cfif>
                    </cfcase>
                    <cfcase value="integer">
                        <cfif kolom neq "">
                            <cfset theVALUE = theVALUE & " " & kolom & ")">
                        <cfelse>
                            <cfset theVALUE = theVALUE & " NULL)">
                        </cfif>
                    </cfcase>
                    <cfcase value="text">
                        <cfif kolom neq "">
                            <cfset theVALUE = theVALUE & " '" & kolom & "')">
                        <cfelse>
                            <cfset theVALUE = theVALUE & " NULL)">
                        </cfif>
                    </cfcase>
                    <cfcase value="varchar">
                        <cfif kolom neq "">
                            <cfset theVALUE = theVALUE & " '" & kolom & "')">
                        <cfelse>
                            <cfset theVALUE = theVALUE & " NULL)">
                        </cfif>
                    </cfcase>
                    <cfcase value="char">
                        <cfif kolom neq "">
                            <cfset theVALUE = theVALUE & " '" & kolom & "')">
                        <cfelse>
                            <cfset theVALUE = theVALUE & " NULL)">
                        </cfif>
                    </cfcase>
                    <cfcase value="varchar2">
                        <cfif kolom neq "">
                            <cfset theVALUE = theVALUE & " '" & kolom & "')">
                        <cfelse>
                            <cfset theVALUE = theVALUE & " NULL)">
                        </cfif>
                    </cfcase>
                    <cfcase value="date">
                        <!--- format TO MYSQL --->
                        <cfif kolom neq "">
                            <cfset theDate = dateformat(kolom, "yyyy-mm-dd")>                                
                            <cfset theVALUE = theVALUE & " '" & theDate & "')">
                        <cfelse>
                            <cfset theVALUE = theVALUE & " NULL)">
                        </cfif>
                    </cfcase>
                    <cfdefaultcase>
                        <cfif kolom neq "">
                            <cfset theVALUE = theVALUE & " '" & kolom & "')">
                        <cfelse>
                            <cfset theVALUE = theVALUE & " NULL)">
                        </cfif>
                    </cfdefaultcase>
                </cfswitch>        
                <cfset theSQL = theSQL & "" & theFIELD & " " & theVALUE>                        
                <cfif theFileContent neq "">
                    <cfset theFileContent = theFileContent &";<br>"& theSQL>

                <cfelse>
                    <cfset theFileContent = theFileContent &""& theSQL>
                </cfif>
            </cfloop>
        </cfif>
    </cfloop>
    <cfreturn theFileContent>
</cffunction>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson