Class: Json2sql::InsertModel
- Inherits:
-
Object
- Object
- Json2sql::InsertModel
- Defined in:
- lib/json2sql/insert_model.rb
Overview
Builds an INSERT INTO statement for a single table.
Input Hash:
"columns" => { "col_name" => value, ... }
Values:
Integer / Float → inserted as raw numbers
String → wrapped in single quotes with SQL escaping
Auto-injected when absent: created_at and updated_at → NOW()
Instance Method Summary collapse
- #build(params) ⇒ Object
-
#initialize(sql, table) ⇒ InsertModel
constructor
A new instance of InsertModel.
Constructor Details
#initialize(sql, table) ⇒ InsertModel
Returns a new instance of InsertModel.
16 17 18 19 20 21 |
# File 'lib/json2sql/insert_model.rb', line 16 def initialize(sql, table) @sql = sql @table = table.to_s end |
Instance Method Details
#build(params) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/json2sql/insert_model.rb', line 23 def build(params) columns = params["columns"] return unless columns.is_a?(Hash) columns = (columns) @sql << "INSERT INTO " @sql << Sanitizer.keyword_wrap(@table) @sql << " (" build_columns(columns) @sql << ") VALUES (" build_values(columns) @sql << ")" end |