@@ -743,48 +743,50 @@ static int TableExists(CfdbConn *cfdb, char *name)
743743
744744static bool CreateTableColumns (CfdbConn * cfdb , char * table , Rlist * columns )
745745{
746- char entry [CF_MAXVARSIZE ], query [CF_BUFSIZE ];
747746 int i , * size_table , * done ;
748747 char * * name_table , * * type_table ;
749748 int no_of_cols = RlistLen (columns );
750749
751- Log (LOG_LEVEL_ERR , "Trying to create table '%s'" , table );
750+ Log (LOG_LEVEL_VERBOSE , "Trying to create table '%s'" , table );
752751
753752 if (!NewSQLColumns (table , columns , & name_table , & type_table , & size_table , & done ))
754753 {
755754 return false;
756755 }
757756
758- if (no_of_cols > 0 )
757+ if (no_of_cols <= 0 )
759758 {
760- snprintf (query , CF_BUFSIZE - 1 , "create table %s(" , table );
761-
762- for (i = 0 ; i < no_of_cols ; i ++ )
763- {
764- Log (LOG_LEVEL_VERBOSE , "Forming column template %s %s %d" , name_table [i ], type_table [i ],
765- size_table [i ]);;
759+ Log (LOG_LEVEL_ERR , "Attempted to create table '%s' without any columns" , table );
760+ return false;
761+ }
766762
767- if (size_table [i ] > 0 )
768- {
769- snprintf (entry , CF_MAXVARSIZE - 1 , "%s %s(%d)" , name_table [i ], type_table [i ], size_table [i ]);
770- }
771- else
772- {
773- snprintf (entry , CF_MAXVARSIZE - 1 , "%s %s" , name_table [i ], type_table [i ]);
774- }
763+ Buffer * query = BufferNew ();
764+ BufferPrintf (query , "create table %s(" , table );
775765
776- strcat (query , entry );
766+ for (i = 0 ; i < no_of_cols ; i ++ )
767+ {
768+ Log (LOG_LEVEL_VERBOSE , "Forming column template %s %s %d" , name_table [i ], type_table [i ],
769+ size_table [i ]);;
777770
778- if (i < no_of_cols - 1 )
779- {
780- strcat (query , "," );
781- }
771+ if (size_table [i ] > 0 )
772+ {
773+ BufferAppendF (query , "%s %s(%d)" , name_table [i ], type_table [i ], size_table [i ]);
774+ }
775+ else
776+ {
777+ BufferAppendF (query , "%s %s" , name_table [i ], type_table [i ]);
782778 }
783779
784- strcat (query , ")" );
780+ if (i < no_of_cols - 1 )
781+ {
782+ BufferAppendChar (query , ',' );
783+ }
785784 }
786785
787- CfVoidQueryDB (cfdb , query );
786+ BufferAppendChar (query , ')' );
787+
788+ CfVoidQueryDB (cfdb , BufferData (query ));
789+ BufferDestroy (query );
788790 DeleteSQLColumns (name_table , type_table , size_table , done , no_of_cols );
789791 return true;
790792}
0 commit comments