Merge pull request #25 from maxlapshin/patch-1
sqlite3:add_columns feature
This commit is contained in:
@@ -29,6 +29,7 @@
|
|||||||
columns_timeout/3]).
|
columns_timeout/3]).
|
||||||
-export([create_table/2, create_table/3, create_table/4, create_table_timeout/4,
|
-export([create_table/2, create_table/3, create_table/4, create_table_timeout/4,
|
||||||
create_table_timeout/5]).
|
create_table_timeout/5]).
|
||||||
|
-export([add_columns/2, add_columns/3]).
|
||||||
-export([list_tables/0, list_tables/1, list_tables_timeout/2,
|
-export([list_tables/0, list_tables/1, list_tables_timeout/2,
|
||||||
table_info/1, table_info/2, table_info_timeout/3]).
|
table_info/1, table_info/2, table_info_timeout/3]).
|
||||||
-export([write/2, write/3, write_timeout/4, write_many/2, write_many/3,
|
-export([write/2, write/3, write_timeout/4, write_many/2, write_many/3,
|
||||||
@@ -367,6 +368,34 @@ create_table(Db, Tbl, Columns, Constraints) ->
|
|||||||
create_table_timeout(Db, Tbl, Columns, Constraints, Timeout) ->
|
create_table_timeout(Db, Tbl, Columns, Constraints, Timeout) ->
|
||||||
gen_server:call(Db, {create_table, Tbl, Columns, Constraints}, Timeout).
|
gen_server:call(Db, {create_table, Tbl, Columns, Constraints}, Timeout).
|
||||||
|
|
||||||
|
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
%% @doc
|
||||||
|
%% Add columns to table structure
|
||||||
|
%% table structure is a list of {column name, column type} pairs.
|
||||||
|
%% e.g. [{name, text}, {age, integer}]
|
||||||
|
%%
|
||||||
|
%% Returns the result of the create table call.
|
||||||
|
%% @end
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
-spec add_columns(table_id(), table_info()) -> sql_non_query_result().
|
||||||
|
add_columns(Tbl, Columns) ->
|
||||||
|
add_columns(?MODULE, Tbl, Columns).
|
||||||
|
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
%% @doc
|
||||||
|
%% Add columns to table structure
|
||||||
|
%% The table structure is a list of {column name, column type} pairs.
|
||||||
|
%% e.g. [{name, text}, {age, integer}]
|
||||||
|
%%
|
||||||
|
%% Returns the result of the create table call.
|
||||||
|
%% @end
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
-spec add_columns(db(), table_id(), table_info()) -> sql_non_query_result().
|
||||||
|
add_columns(Db, Tbl, Columns) ->
|
||||||
|
gen_server:call(Db, {add_columns, Tbl, Columns}).
|
||||||
|
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% @doc
|
%% @doc
|
||||||
%% Returns a list of tables.
|
%% Returns a list of tables.
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
-export([value_to_sql/1, value_to_sql_unsafe/1, sql_to_value/1, escape/1, bin_to_hex/1]).
|
-export([value_to_sql/1, value_to_sql_unsafe/1, sql_to_value/1, escape/1, bin_to_hex/1]).
|
||||||
-export([write_value_sql/1, write_col_sql/1]).
|
-export([write_value_sql/1, write_col_sql/1]).
|
||||||
-export([create_table_sql/2, create_table_sql/3, drop_table_sql/1]).
|
-export([create_table_sql/2, create_table_sql/3, drop_table_sql/1]).
|
||||||
|
-export([add_columns_sql/2]).
|
||||||
-export([write_sql/2, update_sql/4, update_set_sql/1, delete_sql/3]).
|
-export([write_sql/2, update_sql/4, update_set_sql/1, delete_sql/3]).
|
||||||
-export([read_sql/1, read_sql/2, read_sql/3, read_sql/4, read_cols_sql/1]).
|
-export([read_sql/1, read_sql/2, read_sql/3, read_sql/4, read_cols_sql/1]).
|
||||||
|
|
||||||
@@ -203,6 +204,16 @@ create_table_sql(Tbl, Columns, TblConstraints) ->
|
|||||||
table_constraint_sql(TblConstraints),
|
table_constraint_sql(TblConstraints),
|
||||||
", CHECK ('", Type, "'='", Type, "'));"].
|
", CHECK ('", Type, "'='", Type, "'));"].
|
||||||
|
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
%% @doc Generates a alter table stmt in SQL.
|
||||||
|
%% @end
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
-spec add_columns_sql(table_id(),table_info()) -> iolist().
|
||||||
|
add_columns_sql(Tbl, Columns) ->
|
||||||
|
{_, TName} = encode_table_id(Tbl),
|
||||||
|
["ALTER TABLE ", TName, " ADD COLUMN ",
|
||||||
|
map_intersperse(fun column_sql_for_create_table/1, Columns, ", "),";"].
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% @doc
|
%% @doc
|
||||||
%% Using Key as the column name and Data as list of column names
|
%% Using Key as the column name and Data as list of column names
|
||||||
|
|||||||
Reference in New Issue
Block a user