简体   繁体   English

F#从CSV生成SQL

[英]F# Generate SQL from CSV

I have recently been learning F# and functional programming. 我最近一直在学习F#和函数式编程。 One application I have found very useful is generating SQL inserts for a data load from a CSV (or excel table) with some associated ID's. 我发现一个非常有用的应用程序是从带有一些相关ID的CSV(或excel表)中为数据加载生成SQL插入。

The following code is my result, which I believe I will find very handy in the future. 以下代码是我的结果,我相信我将来会发现它非常方便。 I thought others could also benefit from this and I welcome suggestions and other scripts which people have found invaluable in their collection: 我认为其他人也可以从中受益,我欢迎人们在他们的收藏中发现宝贵的建议和其他脚本:

// Returns some dataload SQL for area mapping.
open System
open System.IO

// Read and split CSV into lists
let map_ngo_area = File.ReadAllLines(@"P:\MY_TABLE.csv") 
                        |> Array.to_list
                        |> List.map(fun x -> (x.Split([|','|]) 
                                                |> Array.map(fun y -> y.Trim())) 
                                                |> Array.to_list)

// Output Formatting function
let format_sql_record = "INSERT INTO MyTable
     (ID, REF1_ID, REF2_ID, CreatedUser, CreatedDateTime, LastModifiedUser, LastModifiedDateTime)
     VALUES
     ( {0}, {1}, {2}, 'system', getDate(), 'system', getDate() )"

 // Generate the SQL for the given list.  
let generate_sql list = list |> List.mapi(fun index row -> 
                                            match row with
                                                | [ngo_id; area_id] -> String.Format(format_sql_record, ((int index)+1), ngo_id, area_id) |> printfn "%s"
                                                | _ -> printfn "")

// Main Execution
map_ngo_area |> generate_sql |> ignore

// End of program, pause to view console output.
System.Console.ReadKey() |> ignore

Any suggestions on improving my F# code or process? 有关改进我的F#代码或流程的任何建议? Comments also welcome, as I am fairly new at this paradigm and shifting thinking is not as forthcoming as I expected. 评论也很受欢迎,因为我在这个范例中相当新,而且转变思维并不像我预期的那样即将到来。

Thanks :) 谢谢 :)

Here are a few suggestions: 以下是一些建议:

  • Don't use List.mapi with functions that return unit , since there's not much you can do with the resulting unit list . 不要将List.mapi与返回unit函数一起使用,因为对结果unit list不多。 You should use List.iteri instead, which will allow you to omit the |> ignore at the end of your main execution section. 您应该使用List.iteri ,这将允许您在主执行部分的末尾省略|> ignore
  • Going further, rather than having generate_sql print the generated lines one at a time, it might be better to generate a list of strings instead. 更进一步,不是让generate_sql打印一个生成的行,而是生成一个字符串列表可能更好。 Note that in this case, you would go back to using List.mapi since the function that you apply would return a result for each line. 请注意,在这种情况下,您将返回使用List.mapi因为您应用的函数将返回每行的结果。
  • Use F#'s print formatting rather than String.Format . 使用F#的打印格式而不是String.Format For instance rather than having format_sql_record be a string, have it be a function of type int->string->string->string : let format_sql_record = sprintf "INSERT INTO ... VALUES (%i, %s, %s, ...)" 例如,不要让format_sql_record成为字符串,让它是int->string->string->string类型的函数: let format_sql_record = sprintf "INSERT INTO ... VALUES (%i, %s, %s, ...)"
  • Depending on the version of F# that you're using, you should probably be using the renamed function Array.toList instead of Array.to_list , since that's the name that will be used in the final release of F#. 根据您正在使用的F#版本,您可能应该使用重命名的函数Array.toList而不是Array.to_list ,因为这是将在F#的最终版本中使用的名称。

You can use pattern matching on arrays too: 您也可以在数组上使用模式匹配:

let map_ngo_area = File.ReadAllLines(@"P:\MY_TABLE.csv") 
                    |> Array.to_list
                    |> List.map(fun x -> (x.Split([|','|]) 
                                            |> Array.map(fun y -> y.Trim())) 

let generate_sql list = list |> List.mapi(fun index row -> 
                                            match row with
                                            | [| ngo_id; area_id |] -> printfn ...
                                            | _ -> printfn "")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM